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

Use this forum to discuss the book: Using OpenMP - Portable Shared Memory Parallel Programming, by Barbara Chapman, Gabriele Jost and Ruud van der Pas Read the http://openmp.org/forum/viewtopic.php?f=8&t=465 for book info and to download the examples. Post your feedback about the book and examples to this forum
Post Reply
wssddtc

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

Post 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 ;

MarkB
Posts: 803
Joined: Thu Jan 08, 2009 10:12 am
Location: EPCC, University of Edinburgh

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

Post 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 
}


}

Post Reply