I use my own C ++ library inside a Java program. The Java program is written to use multi-core systems, but it does not scale: the best speed is about 6 cores, i.e. Adding more cores slows it down. My tests show that calling the native code itself is causing a problem, so I want to make sure that different threads access different instances of the native library and, therefore, remove any hidden (memory) dependency between parallel tasks. In other words, instead of a static block
static { System.loadLibrary("theNativeLib"); }
I want several instances of the library to load dynamically for each thread. The main question , if at all possible. And then how to do it!
Notes: - I have implementations in Java 7 fork / join, as well as Scala / akka. Therefore, any help on each platform is appreciated. - Parallel tasks are completely independent. In fact, each task can create a couple of new tasks, and then ends; no additional dependency!
Here is a fork / join-style test program in which processNative is basically a bunch of its own calls:
class Repeater extends RecursiveTask<Long> { final int n; final processor mol; public Repeater(final int m, final processor o) { n=m; mol = o; } @Override protected Long compute() { processNatively(mol); final List<RecursiveTask<Long>> tasks = new ArrayList<>(); for (int i=n; i<9; i++) { tasks.add(new Repeater(n+1,mol)); } long count = 1; for(final RecursiveTask<Long> task : invokeAll(tasks)) { count += task.join(); } return count; } } private final static ForkJoinPool forkJoinPool = new ForkJoinPool(); public void repeat(processor mol) { final long middle = System.currentTimeMillis(); final long count = forkJoinPool.invoke(new Repeater(0, mol)); System.out.println("Count is "+count); final long after = System.currentTimeMillis(); System.out.println("Time elapsed: "+(after-middle)); }
Put it another way: If I have N threads that use the native library, what happens if each of them calls System.loadLibrary ("theNativeLib"); dynamically, instead of calling it once in a static block? Do they share the library? If so, how can I trick the JVM into seeing it as N different libraries loaded independently? (The value of N is not known statically)
Mahdi
source share