0
votes

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];
  }
1
Where is output allocated / declared? Adding (or removing) code, and all of a sudden things break for no apparent reason, has all the earmarks of memory corruption.PaulMcKenzie
You should provide the inputs for the function. It seems like your segfault happens because you are accessing memory which does not belong to you.shargors
output is allocated in main() in another file. the code runs in sequential version but breaks in parallel versionlookingglass

1 Answers

1
votes

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).