how to use "private" ?

General OpenMP discussion

how to use "private" ?

Postby roosevelt » Wed Apr 16, 2008 3:48 am

I am new to openMP, can i claim a pointer to be private in c++? I encountered a problem that "memory can not be read".

another problem: how can i private data members in a class?

for example:

class a{
private:
int x[3];
public:
void func();
}

void a::func()
{
#pragma omp parallel private(x)
..
..
}

I have met with this problem that it can not be privated, can you help me?
thanks.
roosevelt
 
Posts: 4
Joined: Wed Apr 16, 2008 3:07 am

Re: how to use "private" ?

Postby RodriguezFatz » Wed Apr 16, 2008 4:44 am

Hi, i have a quite similar problem in C, i want a struct to be private, but only have a pointer on that struct. So if i declare the pointer private, the program does not work. Any help is wellcome... :)
RodriguezFatz
 

Re: how to use "private" ?

Postby ejd » Wed Apr 16, 2008 6:33 am

In C/C++ if you declare a pointer to be private it is making the pointer private and not the area that it points to. In C++, you can not make class members private. The OpenMP spec allows variables to be made private but not class members.
ejd
 
Posts: 1025
Joined: Wed Jan 16, 2008 7:21 am

Re: how to use "private" ?

Postby peter » Wed Apr 16, 2008 11:01 am

Hello,
thanks to address this topic: i have the same problem.
how can i do?
i have a pointer on a variable and i can get only a pointer on the variable because the a pointer will be returned from a CAD-function that i am not allowed to change.
for example:
vartyp * myvar = functioncal();

if i make the variable private with private(myvar) then only the pointer will become private. therefore it's not difference if myvar is private or shared.
the problem that i have that i get also always a read-error when i access later the variable (which i called here myvar) in parallel from different CPU-Cores.
i dont understand why i get a read-error because i think that its no problem to read variables simultaneously from different CPU-cores.
thanks for your help.

greetings, peter
peter
 

Re: how to use "private" ?

Postby ejd » Wed Apr 16, 2008 11:48 am

It would be helpful to have a more complete example to discuss. Do you have something showing the problem that is relatively small that you can post here?
ejd
 
Posts: 1025
Joined: Wed Jan 16, 2008 7:21 am

Re: how to use "private" ?

Postby peter » Wed Apr 16, 2008 11:58 am

i will try to post little "pseudo"-code:

myclass * myvar = function(); // this function returns a pointer to a myclass-Object
...
int i;
int n = 1000;
int z = 2;

#pragma omp parallel for private(i) shared(myvar, n) firstprivate(z) default(shared)
for (i = 1; i <n; i++)
{
api_function(myvar); // its an api-function from the CAD-API ACIS which reads the myclass-variable
}

Info: if you now little about the CAD-API ACIS: i use a pointer to a BODY- and a FACE-object

it's sure that the api_function only reads the myclass-variable!!!

thx
peter
 

Re: how to use "private" ?

Postby roosevelt » Wed Apr 16, 2008 5:43 pm

does it mean that when I claim a pointer to be private, all the threads are actually reading and writing the same area and hence may arise r-w problems?
roosevelt
 
Posts: 4
Joined: Wed Apr 16, 2008 3:07 am

Re: how to use "private" ?

Postby roosevelt » Wed Apr 16, 2008 5:47 pm

but what if I claim a pointer to be private in the parallel region? is this claim correlated with the r-w error? thx very much.
roosevelt
 
Posts: 4
Joined: Wed Apr 16, 2008 3:07 am

Re: how to use "private" ?

Postby peter » Wed Apr 16, 2008 10:39 pm

What i dont undestand in my program and the explane that i showed before is why i get a read-error in the api_function because the function only reads the pointer and the data which the pointers shows to.
i dont know why parallel reading from different CPU's is a problem in my example because i though that parallel reading is no problem and that only parallel-writing makes problems.
plz help me.
thx
peter
 

Re: how to use "private" ?

Postby ejd » Thu Apr 17, 2008 6:47 am

roosevelt -

Here is a stupid little program to try and show you what is going on.
Code: Select all
#include <omp.h>
#include <stdio.h>

int main(void)
{
  int a = 5;
  int *p;

  omp_set_dynamic(0);
  omp_set_num_threads(2);

  p = &a;
  printf("loc(p): %u  loc(*p): %u  *p = %i\n", &p, &*p, *p);

  #pragma omp parallel firstprivate(p)
  {
    printf("t#: %i  loc(p): %u  loc(*p): %u  *p = %i\n", omp_get_thread_num(), &p, &*p, *p); 
    *p = 10;
  }

  return 0;
}

The output looks like:
Code: Select all
loc(p): 4290772056  loc(*p): 4290772060  *p = 5
t#: 0  loc(p): 4290771020  loc(*p): 4290772060  *p = 5
t#: 1  loc(p): 4273995260  loc(*p): 4290772060  *p = 10

As you can see from this, the pointer p has been privatized (all of the loc(p) values are different). However, what p is pointing to - the variable a - has not (all of the loc(*p) are the same). If you run this program multiple times, you will see that the value of *p printed within the parallel region is sometimes 5 for both print lines and sometimes 5 for one line and 10 for the other. There is indeed a data race between lines 17 (the print of "a" or *p) and 18 (the setting of "a" or *p).
Last edited by ejd on Thu Apr 17, 2008 7:05 am, edited 1 time in total.
ejd
 
Posts: 1025
Joined: Wed Jan 16, 2008 7:21 am

Next

Return to Using OpenMP

Who is online

Users browsing this forum: No registered users and 12 guests