allocnew in for loops

General OpenMP discussion

allocnew in for loops

Postby chen » Tue Nov 13, 2007 1:43 am

Hi,
i am a freshman in openMP. I am trying now to parallize our program with openMP and i have met the following problem. I wonder if someone here could help me. The following codes behaves strange, sometime it runs rightly, sometimes not. I wonder if it is the problem with my allocate function in the loop
int main()
{
double *test;
int i;
#pragma omp private(test)
{
#pragma omp for
for (i=0; i < NUM; i++)
test = allocnew(sizeof(double)*10);
}
free(test);
}
chen
 

Re: allocnew in for loops

Postby lfm » Tue Nov 27, 2007 2:27 am

Here is your code:
chen wrote:int main()
{
double *test;
int i;
#pragma omp private(test)
{
#pragma omp for
for (i=0; i < NUM; i++)
test = allocnew(sizeof(double)*10);
}
free(test);
}


First, the line "#pragma omp private(test)" is illegal OpenMP. Your compiler should have issued a diagnostic. You probably meant "#pragma omp parallel private(test)"

Second, if the allocnew() function is not threadsafe, then you will get unpredictable results. The C function malloc() is threadsafe. It may not give good performance when called in parallel because of internal locking; this is very system dependent.

Also, your code doesn't really do anything useful, which is OK, but it does have a memory leak (it would have a memory leak even if it didn't have any OpenMP directives).

-- Larry


Last bumped by Anonymous on Tue Nov 27, 2007 2:27 am.
lfm
 
Posts: 135
Joined: Sun Oct 21, 2007 4:58 pm
Location: OpenMP ARB


Return to Using OpenMP

Who is online

Users browsing this forum: Yahoo [Bot] and 7 guests