Notes on OpenMP and Freesurfer
- in the source, grep for OPENMP in dev/utils and mri_ca_register.c for examples
Online Tutorial: https://computing.llnl.gov/tutorials/openMP/
- Conditional compilation: to speed up compilation on non OpenMP systems
#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);