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

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

Postby wssddtc » Wed Dec 19, 2012 8:31 pm

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 ;
wssddtc
 
Posts: 1
Joined: Wed Dec 19, 2012 2:53 pm

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

Postby MarkB » Fri Dec 21, 2012 3:56 am

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
}


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


Return to Using OpenMP - The Book and Examples

Who is online

Users browsing this forum: No registered users and 2 guests