Dynamic array allocation in OpenMP

General OpenMP discussion

Dynamic array allocation in OpenMP

Postby openmp_guy » Sat Aug 09, 2008 4:58 pm

Hello,

Which of the following code is more efficient, or are they equivalent? Thanks!

///////////////////////////////////////////////////////
int main()
{
int i, M;
M = 30000;

float *data;

#pragma omp parallel private(i,data)
{ data= (float*)malloc(M*sizeof(float));
#pragma omp for
for(i=0; i<M; ++)
long_calculation(data);
free(data);
}
}

///////////////////////////////////////////////////////
int main()
{
int i, M;
M = 30000;


#pragma omp parallel for private(i)
{
float * data= (float*)malloc(M*sizeof(float));
for(i=0; i<M; ++)
long_calculation(data);
free(data);
}
}
openmp_guy
 
Posts: 7
Joined: Fri Jul 25, 2008 4:55 pm

Re: Dynamic array allocation in OpenMP

Postby ejd » Sat Aug 09, 2008 5:20 pm

The second one is invalid. The for statement has to immediately follow the pragma.
ejd
 
Posts: 1025
Joined: Wed Jan 16, 2008 7:21 am

Re: Dynamic array allocation in OpenMP

Postby openmp_guy » Sat Aug 09, 2008 6:53 pm

Hi,

Yes, you're right. The corrected version is shown below. As compared to the first listing in previous post, which one is more efficient? or, are they the same?

Thanks!



///////////////////////////////////////////////////////
int main()
{
int i, M;
M = 30000;


#pragma omp parallel for private(i)
for(i=0; i<M; ++)
{
float * data= (float*)malloc(M*sizeof(float));
long_calculation(data);
free(data);
}
}
openmp_guy
 
Posts: 7
Joined: Fri Jul 25, 2008 4:55 pm

Re: Dynamic array allocation in OpenMP

Postby ejd » Sun Aug 10, 2008 7:48 am

In the first example, you allocate and free storage once for each thread. In the last example, you allocate and free storage for every iteration of the loop - all 30,000 iterations. I would say that there is no question that the first code (when corrected for the missing "i" in the increment of the for loop) would be more efficient.
ejd
 
Posts: 1025
Joined: Wed Jan 16, 2008 7:21 am

Re: Dynamic array allocation in OpenMP

Postby openmp_guy » Tue Aug 12, 2008 11:09 am

Thanks for the comment.

Regards,
Long
openmp_guy
 
Posts: 7
Joined: Fri Jul 25, 2008 4:55 pm


Return to Using OpenMP

Who is online

Users browsing this forum: No registered users and 7 guests