Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to parallelize this for-loop using reduction?

I am trying to make this for-loop parallelized by using Openmp, i recognized that there reduction in this loop so i added "#pragma omp parallel for reduction(+,ftab)",but it did not work and it gave me this error : error: user defined reduction not found for ‘ftab’.

   #pragma omp parallel for reduction(+:ftab)
    for (i = 1; i <= 65536; i++) ftab[i] += ftab[i-1];
like image 325
elias rizik Avatar asked Oct 18 '22 07:10

elias rizik


1 Answers

The operation you want to do is prefix sum. It can be done in parallel. A simple way is to use thrust::inclusive_scan with OpenMP or TBB backend.

thrust::inclusive_scan(thrust::omp::par, ftab, ftab + 65536, fab);

or

thrust::inclusive_scan(thrust::tbb::par, ftab, ftab + 65536, fab);

You could also implement it by yourself as referenced in the Wikipedia page.

like image 162
kangshiyin Avatar answered Nov 01 '22 13:11

kangshiyin