0

I want to use openmp to parallelize my program. But on some devices it will trigger __kmp_abort_process. Openmp produced the following log .And it's quite hard to reproduce.

OMP: Error #100: Fatal system error detected.                                                                 
OMP: System error #1: Operation not permitted.

I found from the openmp source code that a "fatal system error" will only occur when openmp init env and set thread affinity.

I extracted the part of the code that I was most suspicious of.Here is my simplified pseudocode. I init a static global variable with openmp and set thread affinity manually.

#include <omp.h>

int test(){
  int num_threads= getFromCpuInfo();
  std::vector<int> cpu_ids = getBigCoreCpu();
  omp_set_dynamic(0);
  // set affinity for each thread
  omp_set_num_threads(num_threads);

#pragma omp parallel for
  for (int i = 0; i < num_threads; i++) {
    setSchedAffinity(cpu_ids);  // this func call syscall(__NR_sched_setaffinity,...);
  }
  return num_threads;
}

static const int thread_num = test();

int main(){
  printf("thread_num  %d", thread_num );
}

Since this problem is not easy to reproduce, I am not sure if it has been fixed. Can anyone help me with this question?

Lundin
  • 195,001
  • 40
  • 254
  • 396
ZhuXy
  • 3
  • 2
  • What about your OS and compiler? – sxu Nov 15 '22 at 03:18
  • Most of crash happend at Android 8.1.0,level 27 but Also at Android 9.0,Android 6.0.1 etc. compiler is ndk 20b [ 0] Android (5220042 based on r346389c) clang version 8.0.7 (based on LLVM 8.0.7svn) [ 108] GCC: (GNU) 4.9.x 20150123 (prerelease) – ZhuXy Nov 15 '22 at 03:48
  • OpenMP depends on implementation related to OS and compiler. – sxu Nov 15 '22 at 04:23
  • Which language? C and C++ have quite different rules in this area. The example code is C++, so is that what you're actually using? – John Bollinger Nov 15 '22 at 04:38
  • 1
    I don't know whether this is permitted, but it seems unnecessarily risky either way. If the goal is just to cache the number of threads, then `static auto thread_num() { static auto num = test(); return num; }` used as `thread_num()` instead of `thread_num` will do fine without any dynamic initialization before `main` is entered. (Also is `static` there really intended? Do you want to repeat the initialization for each translation unit using it? Maybe you want `inline` instead?) – user17732522 Nov 15 '22 at 04:49
  • It‘s a C++ program. – ZhuXy Nov 15 '22 at 06:29
  • Yes , I try to use static function instead of global static variable .But I am not sure whether it can cause " OMP: Error #100: Fatal system error detected. OMP: System error #1: Operation not permitted." – ZhuXy Nov 15 '22 at 06:33
  • Possibly related: [Can threads be safely created during static initialization?](https://stackoverflow.com/q/1445141/580083). It's about MSVC, but the same problem may hold for other implementations as well. – Daniel Langr Nov 15 '22 at 07:53

0 Answers0