English 中文(简体)
Why can JNI libraries compiled using OpenMP only be called on Java s "main" thread?
原标题:
  • 时间:2010-01-03 22:38:52
  •  标签:
  • openmp

I have written a library in C using a variety of the #pragma omp directives to parallelize execution. I am on Mac OS X 10.6. I have then wrapped these functions in a JNI library and called them from my Java application.

It seems that calls to native functions containing OpenMP directives crash with EXC_BAD_ACCCESS if they are called from a Java thread besides the main one - in other words,

public static void main(String[] args) { nativeCall(); }

works, but calling the library on another thread, say the EventQueue

    public static void main(String[] args) { 
         SwingUtilities.invokeLater(
             new Runnable(){
                public void run() { nativeCall(); }
});}

crashes with EXC_BAD_ACCESS.

I am a newcomer to OpenMP so I am not sure what could be causing this. What is special about the java "main" thread? Does it run in some privileged mode such that only it can access the resources necessary to initialize OpenMP parallelization tasks? I am also not intimately familiar with the inner workings of the JVM on OS X so I am not sure what the relationship between Java threads and native threads is.

Any help is appreciated!

最佳回答

I have a strong suspicion that I can explain your problem based on my limited experience programming OpenMP. Since no one else has checked in, here goes.

OpenMP wants to manage the threads in the process. That s how it works; it makes threads. It expects to launch from the main thread and go from there.

I would hypothesize that even without JNI, starting OpenMP from a non-main thread would be fraught. Can you post a backtrace from the problem, which might also give a clue?

Keep in mind that the whole rest of the JVM is not compiled with the compiler option that enables OpenMP.

问题回答

You did not show the native code that you call via JNI and that utilizes OpenMP so this is just a guess: if you access any jvm resources using the JNIEnv* (that you obtained as a parameter to the native method) inside the parallelized code, you are asking for trouble. The JNIEnv* should only be used from within the thread that it was obtained in. Please see http://java.sun.com/docs/books/jni/html/pitfalls.html#11233

There s a further link there that points to explanation how you can obtain the JNIEnv* for the current thread. I have no idea how fast or slow that might be, so it s possible that it may eat up the benefits you gain by parallelizing in the first place if your parallelized calculation is smallish.

OpenMP is not supported with Java officially afaik. The thread-execution model is being explored by several institutions and one such exploration is JOMP (Java for OpenMP)

Use gcc 4.7.3 or higher.

When I used gcc 4.2.1(installed with Xcode), I see the same error. But using gcc 4.7.3, the error did not occurred.





相关问题
Enabling OpenMp on visual studio gives an error message

Why would enabling OpenMp for a certain application return this error message "Fatal User Error 1002: Not all work-sharing constructs executed by all threads" when I try to run it on Visual Studio? ...

Behavior of omp_get_max_threads in parallel regions

I compile this bit of code on Snow Leopard and linux and I get different results. On Snow leopard, the first call of omp_get_max_threads returns 2, which is my number of cores, while the second ...

Parallel for_each using openmp

Why does this code not parallelize std::for_each() when it works perfectly fine with std::sort()? How do I fix it? g++ -fopenmp -D_GLIBCXX_PARALLEL=1 -o p p.cc && time ./p sort GCC 4.3 on ...

OpenMP in Visual C++ 2008 Standard Edition

How can I get OpenMP set up in VC++ 2008? The compiler supports it, I believe I am only missing headers/libs. Can I obtain them similar to how the framework SDK can be added to VC++ Express for MFC ...

热门标签