Intel® oneAPI Threading Building Blocks
Ask questions and share information about adding parallelism to your applications when using this threading library.

Return an 2D array in parallel_for

m_enayah1
Beginner
291 Views

Hi,

How can I return back the 2D Array after parallel_for to main code?

I have tried to do that, but unluckly I couldnt manage to return the array and accessing a wrong memory location. The source per below:

const size_t M = 1000;

const size_t N = 1000;

class pSubstitutionClass {

public:

int (*my_a);

int (*my_xor1);

void operator()(const blocked_range2d& r) const {

int (*a) = my_a;

int (*xor1) = my_xor1;

for (size_t i = r.rows().begin(); i!=r.rows().end(); i++)

for (size_t j = r.cols().begin(); j!=r.cols().end(); j++) {

a = xor1[i*N + j];

}

}

pSubstitutionClass( int (*a), int xor1[M*N]) :

my_a(a), my_xor1(xor1)

{}

};

int(*(ParallelSubstitution)( int (*a), int xor1[M*N])){

pSubstitutionClass pSub(a,xor1);

parallel_for(blocked_range2d(0, M, 0, N), pSub, auto_partitioner());

return pSub.my_a;

}

0 Kudos
2 Replies
Elena_G_Intel
Employee
291 Views

Hi,

You don't need to return the array because it is enough just to callParallelSubstitution(A, XOR1) (where A is your 2D array). After that you can use changed array A.For that ParallelSubstitution() function should be changed andshould be like the function below:

voidParallelSubstitution( int (*a), int xor1[M*N]){

parallel_for(blocked_range2d(0, M, 0, N), pSubstitutionClass(a, xor1), auto_partitioner());

}

Then call ParallelSubstitution(A, XOR1) and usearray A. For example,

for (int i = 0; i < M; i++)

for (int j = 0; j < N; j++)

printf (

"A[%d][%d] = %d ", i, j, A);

0 Kudos
m_enayah1
Beginner
291 Views
Got it, Thanks :)
0 Kudos
Reply