[Omp] Is this code wrong?

John van der Burg john at mysticgd.com
Wed May 11 10:24:17 PDT 2005


Hi,

I am new to OpenMP and am experimenting a bit using the Visual Studio .NET
2005 beta 2 compiler. First of all I think it is really a great API!

I made the following piece of test code while playing around with OpenMP. I
call this code a couple of times in a row (it is inside a function and I
call this function 4 times in a row). The code is totally useless and just
as test.

-------------------------------------
float result = 0.0f;
#pragma omp parallel shared(result)
{
	#pragma omp for
		for (int x=0; x<1000; x++)
		{
			for (int i=0; i<5000; i++)
			{
				#pragma omp atomic
					result += cosf(i*0.1f) *
sinf(i*1.0f) * cosf(i*0.1f) * sinf(i*0.1f);
				}
		}
}

#pragma omp barrier

LOG("Test1 - end - result=%f", result);
-------------------------------------



However, the problem I encounter is that the value of the variable "result"
is not always exactly the same as you can see in the following output:

Test1 - end - result=-91.146599
Test1 - end - result=-91.146812
Test1 - end - result=-91.147438
Test1 - end - result=-91.146141

It is not far off, but I expected the same values. What am I doing wrong? :)

Kind regards,
- John









More information about the Omp mailing list