How do I make a static const member private?

General OpenMP discussion

How do I make a static const member private?

Postby ausairman » Tue Apr 23, 2013 4:57 pm

I have a class called world, which contains a static class S instance called scheme, which I would like to make private for every cell member of world. My understanding is that openmp cannot make class members local, but I wasn't sure a) if this applies to static members and b) if there is an elegant work-around I hadn't yet thought of with my non-parallel 90s programming brain. My world class looks functionally like this:

Code: Select all
class World
{
  world() : scheme() {};
  static const S Scheme; // I only want each thread to have a local instance of this
  std::vector<Cell*> cells; // methods of Cell will be called in parallel
 
  void step(); // this function is called a lot
}


Each instance of S is processor-intensive to construct, so I would only like to do it once for each thread. However, the program enters and exists the cell members frequently. Is it possible to easily keep a copy of scheme available to each thread?

In case what I want isn't clear, here is a simple example of what World::Step() might look like:

Code: Select all
void World::Step()
{
#pragma omp parallel for private (scheme) // I don't want a scheme copy to be rebuilt for every thread every time this function is called
  for(int i=0; i < size_of_cells_vector; i++)
  {
    cells[i]->MethodThatAccessesScheme(this); // I want this method to access a local variable when it tries to access this->scheme
  }
}


I'd be grateful for any guidance on how I could elegantly ensure that each thread has a copy of World::scheme and that the scheme instance does not have to be rebuilt (preferably not even copied - is that possible?) everytime World::Step() is called.

EDIT: One solution which came to mind was to store a vector of scheme instances in World, of length equal to the number of threads, and then handing these out to every thread. But this seems ugly and would probably slow down caching efficiency as each element has to be copied to each CPU at each iteration, correct? BTW these S classes are not large, they are just fibonacci sequences that are costly to reproduce.
ausairman
 
Posts: 12
Joined: Mon Jul 11, 2011 5:40 am

Re: How do I make a static const member private?

Postby MarkB » Wed Apr 24, 2013 2:23 am

You can make a per-thread copy of static class members by declaring them threadprivate, but note that the values are only guaranteed to persist between parallel regions under certain circumstances (see page 88 in Version 3.1 of the spec).

ausairman wrote:EDIT: One solution which came to mind was to store a vector of scheme instances in World, of length equal to the number of threads, and then handing these out to every thread. But this seems ugly and would probably slow down caching efficiency as each element has to be copied to each CPU at each iteration, correct?


It is a little ugly, but it should work fine. If you create the vector once and index into it using omp_get_thread_num(), there should not be any copying overhead.

Hope that helps,
Mark.
MarkB
 
Posts: 428
Joined: Thu Jan 08, 2009 10:12 am


Return to Using OpenMP

Who is online

Users browsing this forum: No registered users and 14 guests