3

I have a long doubly-nested for loop to go over for many trials. I want to do this in parallel because these trials are independent of one another. How do I implement this efficiently in Java similar to OpenMP in C++? I would be running this on a node with 64 processors, so I want each core to do one measure.

Relevant code:

//I want each measure to perform the doubly nested loop at the same time.

for (int i : measures) {

  for (int j = 0; j < N; j++) {
    for (int k = 0; k < N; k++) {
    array[i*N*N + j*N + k] = someFunc(i,j,k);
    }
  }

}

Edit: Still having issues:

//sim is a 1D array of type double
//gM is an array of type SMconf
//gene[foo].annot is a LinkedHashSet of URIs.
//Javadoc http://www.semantic-measures-library.org/sml/docs/apidocs/

Arrays.parallelSetAll( sim, i -> {
    try {
        engine.compare( gM[i/(N*N)], gene[(i/N)%N].annot, gene[i % N].annot );
    }
    catch (SLIB_Ex_Critic ex) {
        Logger.getLogger(Exp2.class.getName()).log(Level.SEVERE, null, ex);
    }
});

Error:

enter image description here

2

2 Answers 2

1
    int N=5;
    int array[]=new int[200];
    int [] measures={1,2,3,4,5};

    Arrays.stream(measures).parallel().forEach(i->{
        IntStream.range(0, N).parallel().forEach(j->{
            IntStream.range(0, N).parallel().forEach(k->{
                array[i*N*N+j*N + k]= someFunc(i,j,k);
            });
        });
    });


    Arrays.stream(array).forEach(System.out::println);

Considering measures is an array and not an ArrayList. You may want to put locks while writing to array[]. I hope you are not using array as a variable name.

Sign up to request clarification or add additional context in comments.

4 Comments

What if measures is not an array of int, but an array of objects of class Measure?
if measures is not int, how is your code for (int i : measures) running? Also why are you skipping index while setting array[i*N*N+j*N + k]?
Sorry, where am i skipping index?
in array[i*N*N+j*N + k] you are multiplying with N and adding k and j so you are bound to skip elements
0

Probably it would be moreless efficient to use Java-8 method Arrays.parallelSetAll:

Arrays.parallelSetAll(array, idx -> someFunc(idx/(N*N), (idx/N)%N, idx % N));

This sets the array elements independently in parallel. While you may have an overhead for division, it's probably much less than overhead for creating nested parallel streams as in @pallavt answer. Though it may depend on problem size.

If your someFunc throws checked exception, rethrow it as unchecked one:

Arrays.parallelSetAll(array, idx -> {
    try {
        return someFunc(idx/(N*N), (idx/N)%N, idx % N);
    }
    catch(MyCheckedException ex) {
        throw new RuntimeException(ex);
    }
});

6 Comments

What if the generator function throws an exception. It's happening to me and the try catch that Netbeans makes by default is not working within the parallelSetall function.
@henri, edited the answer to show how to handle checked exception. In general in Java-8 you should prefer unchecked exceptions everywhere.
Thank you so much Tagir. I tried again, but it says "bad return type in lamdba expression missing return value."
It should return a double.
@henri, you're missing the return statement in try block which is present in my code. Look carefully.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.