Notes on OpenMP and Freesurfer

#ifdef _OPENMP
#include <omp.h>
#endif

#ifdef _OPENMP
   printf(ā€œ%d avail.processors\nā€,omp_get_num_procs());
#endif

For Loops

To parallelize a for loop make sure you only read from common variables inside the loop. Writing (storing) information in different locations for each iteration (e.g. when running through an image and writing to a different image) is OK, as done in the example below. If you need to write to the same variable, you need to look at different ways for conflict management (keywords: critical, atomic, reduction). Also be aware that the order of the code inside the loop usually is not sorted anymore, but can be pretty random depending on the scheduling scheme.

Simple example initializing a table:

   const int size = 256;
   double sinTable[size];
    
#ifdef _OPENMP
    #pragma omp parallel for
#endif
   for(int n=0; n<size; ++n)
     sinTable[n] = std::sin(2 * M_PI * n / size);

OpenMP (last edited 2013-09-05 14:15:52 by MartinReuter)