I would like to know where do we need to set critical sections?
If there are multiple threads with a shared array, and each one want to write in different place does it need to be in a critical section, even though each thread write to a different place in the array?
lets say that I have 2 dimensional array M[3][3], initial_array[3] and some double variable and I want to calculate something and store it at the first column of M. I can use with a for loop, but I want to use with openMP , so I did:
omp_set_num_threads(3);
#pragma omp parallel shared(M,variable)
{
int id = omp_get_thread_num();
double init = initial_array[id]*variable;
M[id][0] = init;
}
It works fine, but I know that it can cause to deadlock or for bad running time. I mean what if I had more threads and even a larger M.. what is the correct way to set critical section? another thing i want to ask is about the initial_array, is it also need to be shared?