How can I set the number of OpenMP threads from within the program?
问题 Running the program as $ OMP_NUM_TRHEADS=4 ./a.out limits the number of active OpenMP threads to 4, as evidenced by htop . However, if instead of binding the OMP_NUM_THREADS environment variable in Bash , I call setenv("OMP_NUM_THREADS", "4", 1); from main before calling any OpenMP-enabled functions, this seems to have no effect. Why is this happening? How can I set the number of OpenMP threads from within the program, if it's possible at all? 回答1: There are two ways 1 one can use to set the