[Omp] Privatizing a class member?

Olaf Lenz olenz at Physik.Uni-Bielefeld.DE
Wed Jun 22 23:46:02 PDT 2005


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hello!

I'm using OpenMP with the IBM Visual Age C++ Compiler (ver. 6, I think)
on an AIX pSeries Computer (JUPP at the FZ Jülich).

I'm doing an off-lattice Monte-Carlo simulation - and I want to do it in
parallel. The simulation program is written in C++ and I've actually
tried to use some object-orientation.

To make it easier to reason about it, I've boiled down the problem to a
simpler one: I simply want each thread to add 1000000 random numbers
from its private random number generator (RNG).

Here comes how I would expect it to work:
- ----------------------
class A {
  RNG rng;

  void sum() {
#pragma omp parallel private(rng)
    {
       double sum = 0.0;
       for (int i = 0; i < 1000000; i++)
         sum += rng();
#pragma omp critical(output)
       cout << "sum=" << sum;
    }
  }
};

int main() {
  A a;
  a.sum();
}
- -----------------------
Unfortunately, OpenMP seems not to allow to privatize a class member.
Why is that? Is there any way to do it?

Of course it is possible in this case to privatize the class itself:
- ----------------------
class A {
  RNG rng;

  void sum() {
    double sum = 0.0;
    for (int i = 0; i < 1000000; i++)
      sum += rng();
#pragma omp critical(output)
    cout << "sum=" << sum;
  }
};

int main() {
  A a;
#pragma omp parallel private(a)
  {
    a.sum();
  }
}
- -----------------------
However, this is not what I really want to do.

Instead, I've tried to emulate it by creating an array of RNGs and have
each thread use only one of these:
- -----------------------
class A {
  RNG rng[num_threads];

  void sum() {
#pragma omp parallel
    {
       int thread_num = omp_get_thread_num();
       double sum = 0.0;
       for (int i = 0; i < 1000000; i++)
         sum += rng[thread_num]();
#pragma omp critical(output)
       cout << "sum=" << sum;
    }
  }
};

int main() {
  A a;
  a.sum();
}
- -----------------------
This does actually work, but it is very slow (almost factor 2 compared
to the second version above) and creates a lot of system time.

What is happening there? Is this an OpenMP problem, or is it a problem
of cache coherence?

Best regards
	Olaf Lenz
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFCulqqtQ3riQ3oo/oRAmoCAJ417KLThiv0cxJ9cXhOOHnhJt0K+QCfaskd
RwpIPzCDzIbVolo3Zo/GS7E=
=VuyA
-----END PGP SIGNATURE-----




More information about the Omp mailing list