0

When profiling my C code, I would like to disable/reduce the number of OMP threads to 1. After a brief search, I found this question. I would therefore decided to do something like

#ifdef foo
    #define omp_get_thread_num() 0
#endif

where foo is a macro that is true if the -pg flag is set when compiling with GCC.

My question then is, what is the value of foo and will this method now allow me to get sensible profiling information (by forcing OpenMP to just use one thread).

Community
  • 1
  • 1
rwolst
  • 12,904
  • 16
  • 54
  • 75

1 Answers1

3

The easiest way to change the number of threads for OpenMP is during program launch with the environment variable OMP_NUM_THREADS. To get a single-threaded execution of a.out:

$> OMP_NUM_THREADS=1 ./a.out

This should return sensible data for profiling. If you completely remove OpenMP, you will be somewhat changing your application so profiling may not be as relevant.

user2548418
  • 1,531
  • 10
  • 17
  • Or even (but not as portable as `export OMP_NUM_THREADS=1`) `taskset -c 0 ./a.out`, to limit CPU cores, available to the omp runtime. – osgx Mar 03 '15 at 06:05