Page 1 of 1

Can I use the struct as the share variable in the #omp for

Posted: Wed Dec 19, 2012 8:31 pm
by wssddtc
Can I use the struct as the share variable in the #omp for loop? If it is possible, then how can I do that.
For example, a structure looks like this.
typedef struct
{
lda_suffstats * thread_lda_ss;
char filename[100];
int start_doc;
int end_doc;
double likelihood;
corpus * corpus;
double ** var_gamma;
double ** phi;
lda_model * model;
} thread_info ;

Re: Can I use the struct as the share variable in the #omp f

Posted: Fri Dec 21, 2012 3:56 am
by MarkB
Yes, no problem, though you cannot explicitly declare variables as shared on a for directive.

The typical usage would be:

Code: Select all

struct thread_info foo; 

// initialise foo 

#pragma omp parallel shared(foo) 
{

// foo is shared here 

#pragma omp for 
for (...){
/// foo is shared here too 
}


}
If you need to allocate foo inside the parallel region, then you can use a shared pointer:

Code: Select all

struct thread_info *pfoo; 



#pragma omp parallel shared(pfoo) 
{

#pragma omp single
{
pfoo =  malloc(...) 
// initialise structure contents
}


#pragma omp for 
for (...){
// pfoo points to the same struct for all threads 
}


}