Search code examples
c++openmp

openMP for loop Segmentation fault (core dumped)


I recently started learning openMP and I am trying to parallelize my code for convolution. Once I added #pragma to the for loop which is initializing the image array and there's no data dependency, the code broke and threw Segmentation fault (core dumped). I couldn't figure out what's wrong. Please help!

  // map values from original image to padded image
#pragma omp parallel for schedule(static)  
  for (size_t j = 0; j < n * n; j++) {
    size_t row = (j / n) + padding;
    size_t col = (j % n) + padding;
    size_t pos = (n + (padding * 2)) * row + col;
    padded_image[pos] = image[j];
  }

Solution

  • The computed indices does not match with the allocated size. Indeed, the size of the allocated array padded_image does not depend on n while the accesses to the same in the targeted loop does. Regarding the loop, the size of the array should probably be: (n + (padding * 2)) * (n + (padding * 2)).

    Please note that padded_image is not deleted. Moreover, modulus are very slow, please consider using two loops with an OpenMP clause collapse(2).