Here's the basic premise of my code:
while(norm_of_error > tol){
#pragma omp parallel for
for(i = 1; i <= N*N; i++){
//printf("thread id: %d\n",omp_get_thread_num());
:
int val = based on i
:
#pragma omp critical
x[i-1] = val;
}
#pragma omp barrier
iter++;
}
In short, I am solving Ax = b using the Jacobi iterative method. My problem is that, with the printf()
statement uncommented, the norm_of_error
tends to zero and the while loop ends. However, by simply commenting out the printf()
statement, this doesn't happen. Can anyone give me a hint as to why the printf()
statement has any impact? I'm guessing that the issue has to do with the call to omp_get_thread_num(), but I don't see why that would make any difference.
Edit: I changed the printf()
statement to printf("hi\n");
and the code works. Comment that out, and the code doesn't work.
Code that works with a printf()
statement present, but fails when it is removed, is usually a sign of some invalid operation affecting memory in the program somewhere (e.g. falling off the end of an array, dereferencing NULL
, etc). The misbehaving code may be in some other section of the program entirely (e.g. not within the function that contains the printf()
statement)
That is even more likely when the offending printf()
statement is something obviously innocent, and without any side effects that can affect behaviour of other code (such as printf("Hi\n")
).
The reason is that the presence of the extra printf()
does actually affect layout of memory for the program as a whole. So the offending code (which may be in some completely unrelated part of the program) still overwrites memory, but the consequence changes (e.g. overwriting some data the program is permitted to change, rather than some area of memory that causes the operating system to terminate the program).
This is true whether or not the code is multithreaded.
Without complete code that illustrates the problem (i.e. a small sample that someone else can compile, build, and run to get the same symptom) it is not possible to be more specific.