Skip to content

Commit

Permalink
Add functions to check if OpenMP is working (#1000)
Browse files Browse the repository at this point in the history
* Add check_openmp function

* Fix for non-Apple Clang

* Include omp.h

* Add function checking openmp version

* Use omp_get_num_procs instead

* cmake fixes for openmp

* Link OpenMP with link_libraries

* Return numbers instead of strings

Co-authored-by: Tilman Troester <[email protected]>
  • Loading branch information
hsinfan1996 and tilmantroester authored Dec 9, 2022
1 parent 0708c59 commit bc129ec
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 13 deletions.
26 changes: 13 additions & 13 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/Modules/")

project(ccl VERSION 2.0.0)

# Uncomment to get detailed build outputs
# set( CMAKE_VERBOSE_MAKEFILE on )

option(FORCE_OPENMP "Forcibly use OpenMP " NO)

# Defines list of CCL src files
Expand Down Expand Up @@ -61,25 +64,22 @@ find_package(OpenMP)
set(CMAKE_C_FLAGS_RELEASE "-O3 -fomit-frame-pointer -fno-common -fPIC -std=gnu99 -DHAVE_ANGPOW")
set(CMAKE_C_FLAGS_DEBUG "-O0 -g -fomit-frame-pointer -fno-common -fPIC -std=gnu99 -DHAVE_ANGPOW")

if ("${CMAKE_C_COMPILER_ID}" MATCHES "^(Apple)?Clang$")
if(OpenMP_C_FOUND)
# OpenMP flags for macOS clang
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -Xpreprocessor -fopenmp")
if(FORCE_OPENMP)
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -Xpreprocessor -fopenmp")
endif()
endif()
else()
# OpenMP flags for all other compilers clang
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -fopenmp")
if(OpenMP_C_FOUND)
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} ${OpenMP_C_FLAGS}")
include_directories(${OpenMP_C_INCLUDE_DIR})
link_libraries(${OpenMP_libomp_LIBRARY})
if(FORCE_OPENMP)
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -fopenmp")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} ${OpenMP_C_FLAGS}")
endif()
else()
set(openmp_lib "")
endif()

if(DEFINED ENV{CONDA_PREFIX})
include_directories("$ENV{CONDA_PREFIX}/include")
add_link_options("LINKER:-rpath,$ENV{CONDA_PREFIX}/lib")
# Use conda libraries if available
add_link_options("-L$ENV{CONDA_PREFIX}/lib")
endif()

# Define include and library directories for external dependencies
Expand Down Expand Up @@ -111,7 +111,7 @@ endif()
# The reason for building ccl as a shared library is that we can link it to
# class directly, and it's not a dependency anymore
add_library(ccl SHARED $<TARGET_OBJECTS:objlib>)
target_link_libraries(ccl ${GSL_LIBRARIES} ${FFTW_LIBRARIES} ${CLASS_LIBRARIES} ${ANGPOW_LIBRARIES} m $<$<BOOL:${OpenMP_C_FOUND}>:OpenMP::OpenMP_C>)
target_link_libraries(ccl ${GSL_LIBRARIES} ${FFTW_LIBRARIES} ${CLASS_LIBRARIES} ${ANGPOW_LIBRARIES} m)

# Builds the test suite
add_executable(check_ccl ${TEST_SRC})
Expand Down
4 changes: 4 additions & 0 deletions include/ccl_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ void ccl_integ_spline(int ny, int nx,double *x,double **y,
double a, double b, double *result,
const gsl_interp_type *T, int *status);

int ccl_openmp_version();

int ccl_openmp_threads();

CCL_END_DECLS

#endif
16 changes: 16 additions & 0 deletions pyccl/pyutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -664,3 +664,19 @@ def _get_spline3d_arrays(gsl_spline, length):
check(status)

return xarr, yarr, zarr.reshape((length, x_size, y_size))


def check_openmp_version():
"""Return the OpenMP specification release date.
Return 0 if OpenMP is not working.
"""

return lib.openmp_version()


def check_openmp_threads():
"""Returns the number of processors available to the device.
Return 0 if OpenMP is not working.
"""

return lib.openmp_threads()
21 changes: 21 additions & 0 deletions src/ccl_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
#include <math.h>

#include <gsl/gsl_errno.h>
#ifdef _OPENMP
#include "omp.h"
#endif

#include "ccl.h"

Expand Down Expand Up @@ -340,3 +343,21 @@ void ccl_integ_spline(int ny, int nx,double *x,double **y,
} //end omp parallel
}
}

int ccl_openmp_version()
{
#ifdef _OPENMP
return _OPENMP;
#else
return 0;
#endif
}

int ccl_openmp_threads()
{
#ifdef _OPENMP
return omp_get_num_procs();
#else
return 0;
#endif
}

0 comments on commit bc129ec

Please sign in to comment.