I\'m getting a \"bus error\" from an OpenMP parallel section of code. I recreated a simple version of my problem below. The code essentially makes many calls to the function
When you parallelize some code, you must consider the shared resource, which can cause data races, in turn, eventually may break your program. (Note: not all data races will break your program.)
In your case, as you expected correctly, eng
is the shared by two or more threads, which must be avoided for the correct execution.
A solution for your case is privatization: making a per-thread copy for the shared resources. You need to create a separate copy of eng
.
There are a number of way to do privatization for eng
:
(1) Try to use threadprivate
directive (link): For example, #pragma omp threadprivate(eng)
. However, some compilers may not support non-POD structures for this directive.
(2) In case where threadprivate
is not available, use an array of eng
and access with thread id: declare such as eng[MAX_THREAD]
. Then, access with thread id: eng[omp_get_thread()]
.
However, the second solution needs to consider false sharing, which can severely hurt the performance. It's best to guarantee each item in eng[MAX_THREAD]
is allocated on separate cache line boundary, which is typically 64-byte in modern desktop CPUs. There are also several ways to avoid false sharing. The simplest solution would be using padding: e.g., char padding[x]
in a struct
that holds eng
.