Using OpenMP on the CS Linux Machines

The versions of the GNU compilers installed on the classroom/lab machines include support for the OpenMP extensions, so you should be able to compile and run OpenMP programs on any of them. Whether multithreading helps performance will depend on the program and the hardware configuration. You can find out information about processors by looking at the pseudofile /proc/cpuinfo (looks like a text file).

Compiling OpenMP programs

Use the command gcc (g++ for C++) with the -fopenmp flag to compile C programs with OpenMP extensions, e.g.

gcc -fopenmp -o foo foo.c
(I recommend using several other flags too; see my notes on the GNU compilers for how to use make to help with that.)

Running OpenMP programs

To execute the compiled program, just type the name of the executable, as with any other C or C++ program. By default, the number of threads is set to the number of processors (with hyperthreaded processors counting as two each). You can override this by setting the environment variable OMP_NUM_THREADS. Examples:

./foo
OMP_NUM_THREADS=4 ./foo

With some compilers, using schedule(runtime) in your program will result in the runtime environment picking what it thinks is the best schedule. How well this works may depend on the compiler; it may be useful to experiment with specifying the type of thread scheduling in your program or using schedule(runtime) and setting the environment variable OMP_SCHEDULE. Examples:

OMP_SCHEDULE=static ./foo
OMP_SCHEDULE=static OMP_NUM_THREADS=10 ./foo

Useful links




2019-06-05