More details on openmp
There are a few little things I've learned that I want to share in hopes others using the openmp will not spend the 6 hours "debugging" the same types of mistakes I did.
(This is meant to clear up some stuff that I found unclear on other pages but is in no way complete).
The first thing to try is compiling without openmp (usually by not including the -openmp flag) and see if there are any normal mistakes. If the code is mistakeless (no hanging parenthesis or undeclared variables) then it is still compilable without openmp it will just run as a serial program.
The next thing is that directs for improving loops is incredibly sensitive and they really mean the loop must be on the very line. For instance
#pragma omp parallel for private(i)
norm =1; for(i=0 ;i <N ; i++) {...}
gave me errors of: syntax error before '{' token but on a line that was 10 or so lines preceding this code. That took me a long time to figure out. It doesn't complain about the code itself but says there are errors in parenthesis in preceding lines (at least that's what happened for me). But just putting the norm = 1; before the #pragma directive it all works. Putting parenthesis encapsulating the for loop also seem to be a source of error. So it wants
#pragma omp parallel for private(i)
for(i=0 ;i <N ; i++) {...}
NOT
#pragma omp parallel for private(i)
{for(i=0 ;i <N ; i++) {...} }
Also there seems to be no reason to do everything under a giant #pragma omp parallel code segment with subsections for #pragma omp for or #pragma omp sections. In fact it seems much easier to do it in small forks. As in having a segment:
...
#pragma omp parallel sections
{
#pragma omp section
{ code; code(A,B); etc... }
#pragma omp section
{ code; code(B,A); etc... }
}
...
printf(...); other regular code...
...
#pragma omp parallel for schedule(static) private (i,j)
for(i=0 ;i <N ; i++) {...}
Well I hope this save someone time and effort.
(This is meant to clear up some stuff that I found unclear on other pages but is in no way complete).
The first thing to try is compiling without openmp (usually by not including the -openmp flag) and see if there are any normal mistakes. If the code is mistakeless (no hanging parenthesis or undeclared variables) then it is still compilable without openmp it will just run as a serial program.
The next thing is that directs for improving loops is incredibly sensitive and they really mean the loop must be on the very line. For instance
#pragma omp parallel for private(i)
norm =1; for(i=0 ;i <N ; i++) {...}
gave me errors of: syntax error before '{' token but on a line that was 10 or so lines preceding this code. That took me a long time to figure out. It doesn't complain about the code itself but says there are errors in parenthesis in preceding lines (at least that's what happened for me). But just putting the norm = 1; before the #pragma directive it all works. Putting parenthesis encapsulating the for loop also seem to be a source of error. So it wants
#pragma omp parallel for private(i)
for(i=0 ;i <N ; i++) {...}
#pragma omp parallel for private(i)
{for(i=0 ;i <N ; i++) {...} }
Also there seems to be no reason to do everything under a giant #pragma omp parallel code segment with subsections for #pragma omp for or #pragma omp sections. In fact it seems much easier to do it in small forks. As in having a segment:
...
#pragma omp parallel sections
{
#pragma omp section
{ code; code(A,B); etc... }
#pragma omp section
{ code; code(B,A); etc... }
}
...
printf(...); other regular code...
...
#pragma omp parallel for schedule(static) private (i,j)
for(i=0 ;i <N ; i++) {...}
Well I hope this save someone time and effort.
Labels: programming
0 Comments:
Post a Comment
<< Home