Skip to content

Instantly share code, notes, and snippets.

@dmikushin
Created October 22, 2024 09:26
Show Gist options
  • Save dmikushin/0a30a337b5206cd7aa4e828df48b8b4b to your computer and use it in GitHub Desktop.
Save dmikushin/0a30a337b5206cd7aa4e828df48b8b4b to your computer and use it in GitHub Desktop.
OpenMP status tests

OpenMP status tests

These tests show 3 different outcomes of the OpenMP C code execution:

  1. OpenMP is not enabled in the compiler: this situation can be detected by checking whether the _OPENMP macro is defined or not
  2. OpenMP is enabled and deployed at full scale: the master thread in an OpenMP parallel region checks the omp_get_num_threads() value, and the system-default number of available hardware threads is returned
  3. OpenMP is enabled, but the parallelism is limited to 1 thread, by user's choice

Testing

►make
gcc openmp_check_enabled.c -o test1 && ./test1
OpenMP is not enabled in compile time!
gcc -fopenmp openmp_check_enabled.c -o test2 && ./test2
nthreads = 8
gcc -fopenmp openmp_check_enabled.c -o test3 && OMP_NUM_THREADS=1 ./test3
nthreads = 1
warning: only one thread is deployed!
all: test1 test2 test3
test1: openmp_check_enabled.c
gcc $< -o $@ && ./test1
test2: openmp_check_enabled.c
gcc -fopenmp $< -o $@ && ./test2
test3: openmp_check_enabled.c
gcc -fopenmp $< -o $@ && OMP_NUM_THREADS=1 ./test3
clean:
rm -rf test1 test2 test3
#include <omp.h>
#include <stdio.h>
int main()
{
#ifdef _OPENMP // automatically defined if OpenMP is enabled during compile-time
#pragma omp parallel
{
#pragma omp master
{
int nthreads = omp_get_num_threads();
printf("nthreads = %d\n", nthreads);
if (nthreads == 1)
printf("warning: only one thread is deployed!");
}
}
#else
printf("OpenMP is not enabled in compile time!\n");
#endif
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment