c++ - Simple task-based OpenMP application hangs -


the following small program (online version) attempts calculate area of 64 64 square recursively dividing 4 squares until smallest square has unit length (hardly optimal). reason program hangs. doing wrong?

#include <iostream>  unsigned compute( unsigned length ) {     if( length == 1 ) return length * length;      unsigned a[4] , area = 0 , len = length/2;      for( unsigned = 0; < 4; ++i )     {         #pragma omp task         {             a[i] = compute( len );         }          #pragma omp single         {             area += a[i];         }     }      return area; }  int main() {     unsigned area , length = 64;      #pragma omp parallel     {         area = compute( length );     }      std::cout << area << std::endl; } 

the single construct acts implicit barrier threads in team. however, not threads in team encounter single block, because different threads working @ different recursion depths. why application hangs.

in case code not correct. after task block, a[i] not yet assigned, cannot use it! must wait task completed. of course shouldn't inside loop, otherwise tasking wouldn't exploit parallelism. solution @ end of loop. must specify a shared output become visible:

for( unsigned = 0; < 4; ++i ) {     #pragma omp task shared(a)     {         a[i] = compute( len );     } } #pragma omp taskwait for( unsigned = 0; < 4; ++i ) {     area += a[i]; } 

note reduction not wrapped single construct! compute executed task, 1 thread should ever have it's own local area. however, need 1 single construct before first spawn tasks:

#pragma omp parallel #pragma omp single {     area = compute( length ); } 

simply speaking opens parallel region team of threads, , 1 thread begins initial computation. other threads pick tasks later spawned initial thread task construct. tasking about.


Comments

Popular posts from this blog

java - Suppress Jboss version details from HTTP error response -

gridview - Yii2 DataPorivider $totalSum for a column -

Sass watch command compiles .scss files before full sftp upload -