 # Is this code parallelize ?

Hello All,

I have a code that is written in C and I want to write it in CUDA. But due to dependency I am not able to write in CUDA. My C code is :

``````int fun_2( int *i, int *k, int J, int size,  float* Array_1)

{

int k1 = (*j1), k2 = (*j2);

while ( Array_1[k1] < J )

{

k1++;

(*i) = k1;

if ( k1 >= size )

return 10;

}

k2 = k1 - 1;

(*k) = k2;

return 0;

}

void fun_1(  float * Array_1,  float * Array_2, int size )

{

int i =  1;

int j = 0;

int k = 0;

while ( i < size )

{

Array_2[j] = ( j - Array_1[k] ) / ( Array_1[i] - Array_1[k]  );

j++;

fun_2( &i, &k, j, size, Array_1);

}

}
``````

What are *j1 and *j2 in fun_2? What good are the return codes in fun_2 if you don’t check them? You are right there are dependencies.

It might help to explain what this code does, as it may be necessary to change the algorithm.

CUDAkk,

PMs to you are bouncing because you have chosen a setting of not to be contactable by the board messenger. Check out.

Here is what I wantd to tell you.

Generally, You cant parallelize code that is cumulative.

For example:

``````for(int i=1; i<=N; i++)

a[i] = a[i] + a[i-1];
``````

Things that accumulate across iterations dont lend them for parallel decomposition unless you find an alternate way out.

In your code, I dont know what is “j1” and “j2”.

But it looks like there is some cumulation for “i” and “k”. I am not very sure. I dont understand what is the instance.

A general way of doing this is : you could make a thread correspond to eac value of “j”. So, for each such “j” , you can go around and find the solution – but i am not sure if it will work - espeically if there is cumulation you generally cant