0

Hello I'm doing a simple C++ practice for the classic "monte carlo pi approximation" question using openMP. My code is like this:

int main () {
    Ran ran(time( NULL ) );
    int N = 2000000;       //run the loop for N times
    int a[N];
    double sum=0;
 // for loop execution
    #pragma omp for private(a[i]) reduction(+:sum)
    for( int i = 0; i < N; i = i + 1 ) {
            double xx=ran.doub();
            double yy=ran.doub();
            double rr=r(xx, yy);
            double cc=count(rr);
            a[i]=cc;
            sum+=a[i];
            //cout << "element of a: " << a[i] << endl;
            //cout << "x: " << xx << endl;
            //cout << "sum: " << sum << endl;
    }
    cout << "sum: " << sum << endl;
    cout << "pi?: " << 4*sum/N << endl;
    return sum/N;
}

It runs fine with N=2000000, with 20 cores. But as I make N larger, like 2.5e6, it begins to have segmentation fault.

Jude
  • 11
  • 2
  • What is the purpose of the array A? Would a scalar suffice? How much memory does A consume? (Does that change as you run more iterations?) Where is A allocated? Is that space limited? – Jim Cownie Mar 12 '18 at 09:37
  • @JimCownie Thanks a lot! I removed array A and now it's working for more iterations. I think the array A was consuming the bulk part of virtual memory – Jude Mar 12 '18 at 16:12
  • Why do you have vector a in private? wouldn't shared suffice? – itsnevertoobadtoaskforhelp Mar 15 '18 at 13:54

0 Answers0