views:

119

answers:

3
+3  Q: 

openMP question

hello all,

is it suitable parallelizing loops containing function call(s). or is it much more convenient parallelization of loops which are doing basic operation inside.

for example is it suitable putting parallelization directives as below ?

main(){
..
#omp paralel ..
for (i=0;i<100;i++){
a[i] = foo(&datatype , ...);
...
}
..
}

int foo(datatype *a,...){
//doing complex operations here
//calling other functions etc.
}

Regards

+1  A: 

Three bits of information need to be known:

  1. does it matter the order that you are executing foo?
  2. does foo() affect shared state, and if so is there appropriate locking?
  3. how long does the loop take to run without openmp?

If you have task that takes a long time - several seconds or more - and it can be broken into independent parts (sometimes by refactoring, e.g. by dividing into jobs and gathering the results for each job before combining) then it can be worth trying to parallelize it.

Profile!

Will
A: 

Answers to questions like this can be found in the MIT Press Book, Using OpenMP, which was written by some of the people who developed the OpenMP specifications.

You can find more information at the openmp.org website, including a forum with the OpenMP experts. http://openmp.org/

rchrd
A: 

Thank you Will Richard and Phkahler , that comments were helpful and i will have a deep look to the book rchrd had suggested. But before end of the day, it is wanted from me to make an existing C code ( indeed a big loop which stays at the top of the program) parallelized with openMP if possible.

At this point i need some help about making at least some parts of the loop parallelized. To make things simple, instead of parellelezing whole loop contents how can i make only a part of it work parallel

for(i to N){   
  work1() --(serial)
  work2() --(serial)
  Work3() --( PARALLEL)
  work4() --(serial)
}



  does it make sense adding critical sections except work3

#omp parallel for private(Ptr)
for(i to N){   
 #omp single
 {
  work1() --(serial)
  work2() --(serial)
 }
  Work3(Ptr) --( PARALLEL)
 #omp single
 {
  work4() --(serial)
 }
}

Regards

selcuk