0

I have the c++ code written here below. My purpose is to set as private the two dimensional array indicated as A inside the OpenMP parallel region in particular inside the parallel for loop. However, when I run the code I get the following error: segmentation fault. (As compiler I'm using g++ in VSC).

Any suggestions?

#pragma omp parallel
{ 
  //Allocation of A
  double** A = new double*[3]
  for(int i = 0; i<3; i++){
     A[i]= new double[3];
  }

  #pragma omp for private(A)
  for(int k = 0; k <N; k++)
  {
    //code
  }

  //Deallocation
  for(int i=0;i<3;i++)
  {
    delete[] A[i];
  }
  delete[] A;

}
  • Is 3 an example number or the actual value you're using? If it's the actual value then you probably should just allocate a contiguous block of memory on the stack. – vandench Mar 10 '22 at 14:17
  • Three is the actual value. Do you mean I should allocate it statically? – Nicola Comerci Mar 10 '22 at 14:22
  • 2
    @NicolaComerci -- There is a big difference between an actual array, and what you created. You did not create the items in contiguous memory. If you want contiguous memory, look at [this](https://stackoverflow.com/questions/21943621/how-to-create-a-contiguous-2d-array-in-c/21944048#21944048). – PaulMcKenzie Mar 10 '22 at 14:23
  • @PaulMcKenzie -- I have to use a pointer since I have to pass it as argument in a function inside the for loop. – Nicola Comerci Mar 10 '22 at 14:29
  • Your array is 3x3. Allocate it statically `double A[3][3]`. Nothing prevents you from passing that to a function. And the segmentation fault is probably in the code you are not showing us. – Victor Eijkhout Mar 10 '22 at 14:35
  • @NicolaComerci *I have to use a pointer since I have to pass it as argument in a function.* - The issue is not simply a passing of a pointer. The issue is how that function interprets what you are giving it. If the function expects you are pointing to contiguous memory for the data, your dynamic allocations you're doing now doesn't guarantee this. The link I posted guarantees that the memory allocated is contiguous. Also, if you look at your code, you start of with `double**` -- look at the link I posted -- it also starts of with a `double**`, so there is no difference to the pointer type. – PaulMcKenzie Mar 10 '22 at 14:54
  • Thanks to everyone for your help. I solved my problem by statically allocating the array A. I changed the argument of the function within the for loop from double** to double [][]. Now, it is working properly. – Nicola Comerci Mar 10 '22 at 15:31
  • @NicolaComerci what is `double[][]`? That is not a C++ declaration or type. Also, if you don't know *exactly* why your solution works, it isn't a solution. Every code change you make, you have to know why it was made and what makes it fix the issue. At this point, all you did was move the potential bug to some other place in your program, or worse, you are now covering the bug with different code that doesn't expose it (for now). – PaulMcKenzie Mar 10 '22 at 15:43

0 Answers0