oprnmp question

oprnmp question

hello,

let's say i want to do this:

int test;

#pragma omp private(test)

for (int i=0;i<5;i++)

{

//do this for the first time, but i get theRun-Time Check Failure #3 - The variable 'test' is being used without being initialized. (VS2010)

if(!test) //if test was never initialized, then initialize it

{

test = 2;

}

}

any idea how i can do this? thanks.

12 posts / 0 nouveau(x)
Dernière contribution
Reportez-vous à notre Notice d'optimisation pour plus d'informations sur les choix et l'optimisation des performances dans les produits logiciels Intel.

Hi!

"Variables declared PRIVATE should be assumed to be
uninitialized for each thread
"

So I believe you should re-initialize test after the #pragma omp, something like:

int test;

#pragma omp private(test)
test=0;
for (int i=0;i<5;i++)
{
if(!test) //if test was never initialized, then initialize it
{
test = 2;
}
}

-Jeff

You should always initialise your variables. Also, check out the "firstprivate" keyword in openmp documentation to see if it might help you.

Hi Jef,

I cannot do that test = 0 because the for i want to do it in a "parralel for". and after #pragma omp parallel for, I cannot add anything else than for (....)

Well, the problem is that I want to initialize that variable only the 1st time the current thread needs it. after, it should use the same one. Imagine I want to counter the number of times a thread is used in a parallel for. I cannot set it to 0, because my count will be lost.

i also want to use this with a pointer, and firstprivate cannot be used with pointers.

I don't really understand what you want to do here. But you can initialize variables in the parallel part before the loop if you do this:

int test;
#pragma omp private(test)
{
test=0;
#pragma omp for
for (int i=0;i<5;i++)
{
if(!test) //if test was never initialized, then initialize it
{
test = 2;
}
}

}

You might also want to check out the 'reduction' clause of OpenMP...

I think what he asks about is this:int test = 0;
#pragma omp for firstprivate(test)
for (int i=0;i<5;i++)
{
if(!test) //if test was never initialized, then initialize it
{
test = 2;
}

}

The variable need to be initialized before the pragma.

Ok, so from what I understand you want to be able to do some operations in parallel but not in the for. Operations which work on pointers.
Here is a bit of code that just might help you.
#pragma omp parallel
{
char *s=malloc(100*sizeof(char));
sprintf(s,"Hello from thread %d .",omp_get_thread_num());

#pragma omp for private(i)
for(i=0;i<5;i++){
printf("%s at iteration %d\n",s,i);
}
}

I don't understand what do you want but here is my idea:

int test = 0;

#pragma omp parallel for private(test) // the variable test is initialised only once by a thread. If you use firstprivate (test) so each thread begin by test=0

for (int i=0;i<5;i++)

{

//do this for the first time, but i get theRun-Time Check Failure
#3 - The variable 'test' is being used without being initialized.
(VS2010)

if(!test) //if test was never initialized, then initialize it

{

test = 2;

}

}

hi..thanks. it works

Laisser un commentaire

Veuillez ouvrir une session pour ajouter un commentaire. Pas encore membre ? Rejoignez-nous dès aujourd’hui