最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c - OpenMP reduce on large heap array cause segment fault - Stack Overflow

programmeradmin3浏览0评论

When I try to reduce a large heap array with OpenMP reduction, it segment fault:

#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
  double *test = NULL;
  size_t size = (size_t)1024 * 1024 * 16; // large enough to overflow openmp threads' stack

  test = malloc(size * sizeof(double));

#pragma omp parallel reduction(+ : test[0 : size]) num_threads(2) 
  {
    test[0] = 0;
#pragma omp critical
    {
      printf("frame address: %p\n", __builtin_frame_address(0));
      printf("test: %p\n", test);
    }
  }
  free(test);
  printf("Allocated %zu doubles\n\n", size);
}

Please note that double *test is allocated on heap, thus not a duplication of this and this.

This example works with small size array, but segment fault with large array. The array is allocated on heap, and the system memory is enough.

Simimar issue but segment fault still happens even when the array is allocated on heap.

There are same issue on other community:

but all the solution I found is about increase openmp stack size.

When I try to reduce a large heap array with OpenMP reduction, it segment fault:

#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
  double *test = NULL;
  size_t size = (size_t)1024 * 1024 * 16; // large enough to overflow openmp threads' stack

  test = malloc(size * sizeof(double));

#pragma omp parallel reduction(+ : test[0 : size]) num_threads(2) 
  {
    test[0] = 0;
#pragma omp critical
    {
      printf("frame address: %p\n", __builtin_frame_address(0));
      printf("test: %p\n", test);
    }
  }
  free(test);
  printf("Allocated %zu doubles\n\n", size);
}

Please note that double *test is allocated on heap, thus not a duplication of this and this.

This example works with small size array, but segment fault with large array. The array is allocated on heap, and the system memory is enough.

Simimar issue but segment fault still happens even when the array is allocated on heap.

There are same issue on other community:

https://community.intel/t5/Intel-Fortran-Compiler/Segmentation-fault-when-using-large-array-with-OpenMP/m-p/758829

https://forums.oracle/ords/apexds/post/segmentation-fault-with-large-arrays-and-openmp-1728

but all the solution I found is about increase openmp stack size.

Share Improve this question edited 2 days ago LXYan asked 2 days ago LXYanLXYan 615 bronze badges 3
  • 1 if(test == NULL), check if malloc failed in the first place. – Lundin Commented 2 days ago
  • @Lundin on most systems, malloc provides a virtual allocation and will not fail. If the system runs out of memory, the failure will occur once a new page should be mapped when writing to the page. – Joachim Commented 2 days ago
  • @Joachim True but this is requesting hundreds of Mb so I wouldn't assume anything. – Lundin Commented 2 days ago
Add a comment  | 

1 Answer 1

Reset to default 3

I thought there should be a real solution so I issue a bug on gcc's bugzilla:

https://gcc.gnu./bugzilla/show_bug.cgi?id=118909

Thanks to Jakub Jelinek, the reason of this bug is, most compiler allocates privatized data on stack, which is good for performance. If you do need a large privatized data, you can either increase the OMP stack size by set OMP_STACKSIZE environment variable or use allocate clause to specify it should be allocated on heap.

So the solution is adding allocate(test) to make the privatized test array on heap:

#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
  double *test = NULL;
  size_t size = (size_t)1024 * 1024 * 16;

  test = malloc(size * sizeof(double));

#pragma omp parallel reduction(+ : test[0 : size]) num_threads(2) allocate(test)
  {
    test[0] = 0;
#pragma omp critical
    {
      printf("frame address: %p\n", __builtin_frame_address(0));
      printf("test: %p\n", test);
    }
  }
  free(test);
  printf("Allocated %zu doubles\n\n", size);
}
发布评论

评论列表(0)

  1. 暂无评论