tags:

views:

136

answers:

3

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!

A: 

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.

bmargulies
A: 

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)

Raymond Tay
I understand that there right now is no way to write Java code such that it executes concurrently using OpenMP - however, can the JVM really determine what resources are used by native code that it calls through JNI?
Ben Englert
afaik, its not possible because the JVM doesn't understand the context of the JNI environment or whatever functions it calls, data on the stack/heap. i find that jni is like a quick hack when java was involving in the initial stages where the jvm wasn't as fast or stable (not to mention the lack of libraries back then). As an example, i remember there was a time when i was still using java 1.1 and the math library for Java wasn't as mature as it is now so our team had to hook to a custom built math library so that it could run faster but that approach is absolutely redundant now
Raymond Tay
A: 

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.

Antti Karanta