Using firstprivate and lastprivate with pointers

General OpenMP discussion

Using firstprivate and lastprivate with pointers

Postby newbie2guru » Thu Jun 05, 2008 1:29 pm

Dear all,

Allow me to ask my question in terms of a short description. I have the following:
class A {
T *ptr;
....
public:
//constructors, copy assignment
//some members
};

class U {
A a_obj;
public:
.........
void u_function(T* );
.........
};

class W {
U u_obj;
public:
...........
void w_function();
..........
};

Implementation of w_function() is as follows:
void W::w_function(){
const int size = ##
T *ptr_1 = new T[size];
T *ptr_2 = new T[size];
.........
T *ptr_m = new T[size]; //m >= 1
//Each of the ptr_i pointers points to a substring of the string pointed to by T* (in class A), whose length can vary from size to m*size.
//initialize each ptr_i
//The idea is to then do
#pragma omp parallel firstprivate(ptr_1, ptr_2, ...) lastprivate(ptr_1, ptr_2, ...) //is this correct?
{
#pragma omp for
for (i = 1; i <= m; i++)
u_function(ptr_i);
}

delete [] ptr_i's;
}

Suppose the original string is of size mn. Each pointer ptr_i points to a (distinct) substring of size n? At the end of the parallel section I want to put the contents of each ptr_i back in that section of memory (from which the original contents came) that is part of the memory pointed to by T*. One does not seem to find examples like this anywhere. Any assistance is appreciated.
As an aside, I want to put the #pragma omp parallel inside a for loop (iteration), which include a serial operation on the memory pointed to by T*.Like so:
for (j = 1; j <= N; j++){
#pragma omp parallel firstprivate(ptr_1, ptr_2, ...) lastprivate(ptr_1, ptr_2, ...)
{
#pragma omp for
for (i = 1; i <= m; i++)
u_function(ptr_i);
}
serial_operation(ptr); //T* ptr
}

Is the above the way to go? Again, thanks for any assistance.
newbie2guru
 
Posts: 4
Joined: Thu Jun 05, 2008 12:26 pm

Re: Using firstprivate and lastprivate with pointers

Postby newbie2guru » Thu Jun 05, 2008 1:39 pm

Apologies. The following should not be a question: Each pointer ptr_i points to a (distinct) substring of size n. (no question mark).
newbie2guru
 
Posts: 4
Joined: Thu Jun 05, 2008 12:26 pm

Re: Using firstprivate and lastprivate with pointers

Postby ejd » Thu Jun 05, 2008 4:58 pm

I am not sure I fully understand what you are trying to do, but let's see if I can help. In w_function, you are allocating storage (with new) and assigning it to each of the pointers (ptr_1, ptr_2, etc). Somewhere before the parallel region, it looks like you are copying some portion of the original string into each of the substrings pointed at by some ptr_i. When you encounter the parallel region, the firstprivate will create a private copy of the pointer (and not what the pointer points to), so the firstprivate pointer points to the same substring as the "original" pointer. You are then calling u_function (in a loop) with each of these private pointers. At the end of the parallel region, because of the lastprivate clause, each private pointer (and not what it points to) will be copied back to the original shared pointer variable.

Since u_function seems to be operating on each substring being passed to it by ptr_i independently, why do you think that they have to be private? I don't see why you are using firstprivate and lastprivate here. It should work, but seems like unnecessary overhead.
ejd
 
Posts: 1025
Joined: Wed Jan 16, 2008 7:21 am

Re: Using firstprivate and lastprivate with pointers

Postby newbie2guru » Mon Jun 09, 2008 5:41 am

Thanks ejd. It is what each pointer points to that I want to initialize with the substrings of the original string. And after the parallel region, it is what is contained in the substrings that I want to concatenate into the original string.
newbie2guru
 
Posts: 4
Joined: Thu Jun 05, 2008 12:26 pm

Re: Using firstprivate and lastprivate with pointers

Postby ejd » Mon Jun 09, 2008 8:00 am

I understand that. I don't understand what you are doing in the parallel region (in u_function) that requires a private pointer to a substring (rather than a private substring). Like I said, from what I can see of your code, it would seem like it should work without having to make the substring pointers (ptr_i) firstprivate or lastprivate.
ejd
 
Posts: 1025
Joined: Wed Jan 16, 2008 7:21 am

Re: Using firstprivate and lastprivate with pointers

Postby newbie2guru » Mon Jun 09, 2008 11:18 am

u_function has a bit string as input and a bit string as output and these do not fit into a built-in data type, hence my use of a pointer. Also the bit operations of class A are implemented to work on bit strings of arbitrary length. For instance I wanted to be able to manipulate any string of type unsigned char or unsigned int at a bit level. I could then be able to get and set bits at any position I choose, and be able to rotate entire bit strings in an efficient manner. Furthermore I want u_function to operate on distinct substrings of some string, and pointers make things convenient and fast. But Yes I want private substrings, and that was really what the question was about. Given this situation, how can I proceed?
newbie2guru
 
Posts: 4
Joined: Thu Jun 05, 2008 12:26 pm


Return to Using OpenMP

Who is online

Users browsing this forum: No registered users and 9 guests