SamatsWiki

Examples

Using task

From: https://software.intel.com/en-us/forums/intel-moderncode-for-parallel-architectures/topic/544780

   1 struct Frame
   2 {
   3   int bufferNumber; // 0, 1, ... nFrames-1
   4   uint8_t* buffer; // buffer[bufferSize];
   5   ...
   6   Frame(int n, size_t size)
   7   {
   8     bufferNumber = n;
   9     buffer = new uint8_t[size]; 
  10   }
  11   ~Frame() { if(buffer) delete [] buffer;
  12 };
  13 
  14 int nFrames = 0; // You determine number of buffers (should be > nThreads)
  15 Frame* Frames = NULL; // [nFrames];
  16 ...
  17 nFrames = omp_get_max_threads() + someMore;
  18 Frames = new Frame*[nFrames];
  19 for(int i=0; i < nFrames - 1; ++i)
  20   Frames[i] = new Frame(i, YourFrameSize);
  21 
  22 volatile int frame_empty_index = 0;
  23 volatile int frame_fill_index = 0;
  24 #pragma omp parallel
  25 {
  26   #pragma omp master
  27   {
  28     for(Frame* frame=get_next_frame(); frame;  frame=get_next_frame())
  29     {
  30       #pragma omp task
  31       {
  32          doWork(frame);
  33          while(frame->bufferNumber != frame_fill_index)
  34            Sleep(0); // release time slice
  35          write_frame(frame);
  36          frame_fill_index = (frame_fill_index + 1) % nFrames;
  37       }
  38     } // for
  39   } // master
  40 }
  41 
  42 
  43 //
  44 Frame* get_next_frame()
  45 {
  46    // wait for buffer
  47    while((frame_empty_index + 1) % nFrames == frame_fill_index)
  48      Sleep(0); // release time slice
  49    Frame* frame = frames[frame_empty_index];
  50    frame_empty_index = (frame_empty_index +1) % nFrameBuffers;
  51    read_frame(frame);
  52    return frame;
  53 }
  54 void write_frame(Frame* frame)
  55 {
  56   while(frame->bufferNumber != frame_fill_index)
  57     Sleep(0);
  58   write(frame);
  59   frame_fill_index = (frame_fill_index + 1) % nFrames;
  60 }
  61 
  62 void doWork(Frame* frame)
  63 {
  64 ...
  65 }

Use of OpenMP in ImageMagick: https://github.com/ImageMagick/ImageMagick/search?utf8=%E2%9C%93&q=pragma+omp&type=Code

Improve Performance with Vectorization

Speed Up Image Processing with OpenMP - YouTube

Guide into OpenMP: Easy multithreading programming for C++

[Bf-blender-cvs] [e6c6b5d] buildbot_linux_cmake: Buildbot: Link statically against gomp for CMake Linux slave: Linking libgomp (OpenMP runtime) statically w/ CMake/gcc

SamatsWiki: OpenMP (last edited 2016-09-30 21:05:14 by SamatJain)