0

I have a particular set of loops for building a matrix and I was wondering if anyone knows of a way to fuse them so I can use OpenMP parallel for pragma.

for( int i = 0; i < nbas*nchannels; ++i){
  int ket_r = i / nchannels;
  int ket_l = i % nchannels;
  for( int j = 0; j < nbas*nchannels; ++j){
    int bra_r = j / nchannels;
    int bra_l = j % nchannels;
    ...stuff_calculated...
    matrix[ i*nbas*nchannels + j ] = stuff_calculated;
  }
}

Each for loop walks along a dimension I need to further divide (to get variables like ket_r). It looks like the loops are independent to me so I would thought this would be as simple as

#pragma parallel for collapse(2)
for( int i = 0; i < nbas*nchannels; ++i){
  for( int j = 0; j < nbas*nchannels; ++j){
    int ket_r = i / nchannels;
    int ket_l = i % nchannels;
    int bra_r = j / nchannels;
    int bra_l = j % nchannels;
    ...stuff_calculated...
    matrix[ i*nbas*nchannels + j ] = stuff_calculated;
  }
}

But I get different answers. If this looks correct, I will do further investigation down this route.

I cannot provide the full code because it's part of my research group so I apologize for not posting a full working question.

1
  • Try to run your second code without any parallelism and check the answers. Then run with parallelism. Commented Dec 2, 2020 at 7:30

1 Answer 1

1

Looks like quantum mechanics I guess :)

I suppose with different answers you mean a non-deterministic output? And I suppose your matrix to be some kind of standard vector of ints/floats/doubles? At first, your parallel for usage looks ok but keep in mind that the behavior is undefined for collapse-usage if the iteration limits are altered in a dynamic way via the loop executions themselves!

Without having the exact code, some further ideas:

  • Did the code run fine before parallelization?
  • What kind of operations are done in detail to set the matrix values? Look for possible shared dependencies there! Are further threads involved somehow?
  • Ensure all variables are (correctly) initialized before used!
Sign up to request clarification or add additional context in comments.

Comments

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.