How to serialise only certain functions in a parallel loop?

General OpenMP discussion

How to serialise only certain functions in a parallel loop?

Postby ausairman » Thu Dec 01, 2011 8:14 pm

Ok I probably didn't explain that very well in the title. I have a structure called "net", of which there are many instances. I have these running in parallel just fine. Now, I also have a structure called "graveyard", where I store "net" structure instances that have been "killed", awaiting reimplementation. I only want to have a single instance of this "graveyard" structure.

Basically the graveyard structure adds and removes "net" structure pointers from a list, returning them to the caller. However, if this function is called twice in any "instant" point in time (ie. between when another function is called and ends) it will break, so how do I make processes wait until the function is not being called? I was thinking of creating an "in_use" bool variable that I would use to indicate that something is currently happening:

Code: Select all
net * graveyard::retrieve_net() {
    while (in_use); // wait
    in_use = true;
    // do something, modify arrays, etc.
    in_use = false;
    return net_instance;
}


This seems a bit dodgy though, so I was wondering if there was some standard way of achieving this?
ausairman
 
Posts: 12
Joined: Mon Jul 11, 2011 5:40 am

Re: How to serialise only certain functions in a parallel lo

Postby ausairman » Thu Dec 01, 2011 8:27 pm

Also, is it possible that one process checks for in_use after another process has checked whether it is true or not, but before it can modify in_use to true? Could this create problems
ausairman
 
Posts: 12
Joined: Mon Jul 11, 2011 5:40 am

Re: How to serialise only certain functions in a parallel lo

Postby ftinetti » Fri Dec 02, 2011 3:27 am

I think you need to use the critical directive.
ftinetti
 
Posts: 567
Joined: Wed Feb 10, 2010 2:44 pm

Re: How to serialise only certain functions in a parallel lo

Postby ausairman » Tue Dec 06, 2011 1:20 am

Ok, so now I have this, will the following code ensure that this section of code is only executed by one thread at a time, regardless from where (or how deeply nested) it is called from?

also, what should I put in shared()? Only variables that I want to modify?

Code: Select all
net * graveyard::retrieve_net() {
  #pragma omp parallel shared(?)
  {
  #pragma omp critical
    // do something, modify arrays, etc.
    return net_instance;
  }
}
ausairman
 
Posts: 12
Joined: Mon Jul 11, 2011 5:40 am

Re: How to serialise only certain functions in a parallel lo

Postby ftinetti » Tue Dec 06, 2011 4:04 am

Hmmm... I think I misunderstood something... from the first post:
I have these running in parallel just fine.

I thought you need to call retrieve_net() from different threads and, thus, you need to protect access to shared/pointed to data... now that you are including
Code: Select all
#pragma omp parallel shared(?)

I see I was wrong... so maybe I need some more information or, better, a reduced complete example so if I have some time I could play with and understand the problem. Also, from
what should I put in shared()? Only variables that I want to modify?

I think you should follow some tutorial. There are tutorials (or links to) in the OpenMP site.

HTH.
ftinetti
 
Posts: 567
Joined: Wed Feb 10, 2010 2:44 pm

Re: How to serialise only certain functions in a parallel lo

Postby ausairman » Thu Dec 08, 2011 2:06 am

So I have some class, let's call it net, which has a function called activate:

Code: Select all
struct net {
void activate();

graveyard  * g;
};

net::activate() {
net * some_ptr = g->retrieve_net();
}


many instances of "net" contain the value g. I call the "activate" function of many instances of "net" in a parallelised loop:

Code: Select all
#pragma omp parallel
   {
#pragma omp for schedule(dynamic)
      for (unsigned i = 0; i < lim; i++) {
                    array_of_net_ptrs[i]->activate;
      }
   }


So I have to ensure that although the code is executed in parallel, each "net" instance must wait for the "g.retrieve_net()" function to be available. I hope this clarifies it. In the meantime I'll start reading up as much as I can about the CRITICAL command but it would be really helpful if you could provide a simple example of making this happen, so I know that I'm heading in the right direction.

Thanks!
ausairman
 
Posts: 12
Joined: Mon Jul 11, 2011 5:40 am


Return to Using OpenMP

Who is online

Users browsing this forum: Google [Bot] and 13 guests