diff options
author | Brad King <brad.king@kitware.com> | 2017-04-26 12:58:54 +0000 |
---|---|---|
committer | Kitware Robot <kwrobot@kitware.com> | 2017-04-26 08:59:02 -0400 |
commit | f03bbc3970ce71ca4dd11b26ffab02e9ac62b7fe (patch) | |
tree | 1929278a68619a3c5b1105b1ef6fabadfda5c1a5 /Tests | |
parent | 8985c1dfd79804d6318e6dae6e47597a7b71dcd4 (diff) | |
parent | 99ac0940ade3172d7b91a2215c8fb14cb8c0de1f (diff) | |
download | cmake-f03bbc3970ce71ca4dd11b26ffab02e9ac62b7fe.tar.gz |
Merge topic 'findopenmp-modernized'
99ac0940 FindOpenMP: Add tests
bb032c1b FindOpenMP: Complete overhaul.
Acked-by: Kitware Robot <kwrobot@kitware.com>
Merge-request: !733
Diffstat (limited to 'Tests')
-rw-r--r-- | Tests/CMakeLists.txt | 4 | ||||
-rw-r--r-- | Tests/FindOpenMP/CMakeLists.txt | 21 | ||||
-rw-r--r-- | Tests/FindOpenMP/Test/CMakeLists.txt | 73 | ||||
-rw-r--r-- | Tests/FindOpenMP/Test/main.c | 7 | ||||
-rw-r--r-- | Tests/FindOpenMP/Test/main.f90.in | 5 | ||||
-rw-r--r-- | Tests/FindOpenMP/Test/scalprod.c | 16 | ||||
-rw-r--r-- | Tests/FindOpenMP/Test/scalprod.f90.in | 19 | ||||
-rw-r--r-- | Tests/FindOpenMP/Test/scaltest.c | 20 | ||||
-rw-r--r-- | Tests/FindOpenMP/Test/scaltest.f90.in | 21 |
9 files changed, 186 insertions, 0 deletions
diff --git a/Tests/CMakeLists.txt b/Tests/CMakeLists.txt index 60a2cbbef6..2f53cfc9a7 100644 --- a/Tests/CMakeLists.txt +++ b/Tests/CMakeLists.txt @@ -1421,6 +1421,10 @@ ${CMake_BINARY_DIR}/bin/cmake -DDIR=dev -P ${CMake_SOURCE_DIR}/Utilities/Release add_subdirectory(FindOpenGL) endif() + if(CMake_TEST_FindOpenMP) + add_subdirectory(FindOpenMP) + endif() + if(CMake_TEST_FindOpenSSL) add_subdirectory(FindOpenSSL) endif() diff --git a/Tests/FindOpenMP/CMakeLists.txt b/Tests/FindOpenMP/CMakeLists.txt new file mode 100644 index 0000000000..e64885d09e --- /dev/null +++ b/Tests/FindOpenMP/CMakeLists.txt @@ -0,0 +1,21 @@ +foreach(c C CXX Fortran) + if(CMake_TEST_FindOpenMP_${c}) + set(CMake_TEST_FindOpenMP_FLAG_${c} 1) + else() + set(CMake_TEST_FindOpenMP_FLAG_${c} 0) + endif() +endforeach() + +add_test(NAME FindOpenMP.Test COMMAND + ${CMAKE_CTEST_COMMAND} -C $<CONFIGURATION> + --build-and-test + "${CMake_SOURCE_DIR}/Tests/FindOpenMP/Test" + "${CMake_BINARY_DIR}/Tests/FindOpenMP/Test" + ${build_generator_args} + --build-project TestFindOpenMP + --build-options ${build_options} + -DOpenMP_TEST_C=${CMake_TEST_FindOpenMP_FLAG_C} + -DOpenMP_TEST_CXX=${CMake_TEST_FindOpenMP_FLAG_CXX} + -DOpenMP_TEST_Fortran=${CMake_TEST_FindOpenMP_FLAG_Fortran} + --test-command ${CMAKE_CTEST_COMMAND} -V -C $<CONFIGURATION> + ) diff --git a/Tests/FindOpenMP/Test/CMakeLists.txt b/Tests/FindOpenMP/Test/CMakeLists.txt new file mode 100644 index 0000000000..4ba0e5cc45 --- /dev/null +++ b/Tests/FindOpenMP/Test/CMakeLists.txt @@ -0,0 +1,73 @@ +cmake_minimum_required(VERSION 3.8) +project(TestFindOpenMP) +include(CTest) + +macro(source_code_mapper_helper LANG_NAME SRC_FILE_NAME) + if("${LANG_NAME}" STREQUAL "C") + set(OpenMPTEST_SOURCE_FILE "${SRC_FILE_NAME}.c") + elseif("${LANG_NAME}" STREQUAL "CXX") + configure_file("${SRC_FILE_NAME}.c" "${SRC_FILE_NAME}.cxx" COPYONLY) + set(OpenMPTEST_SOURCE_FILE "${SRC_FILE_NAME}.cxx") + elseif("${LANG_NAME}" STREQUAL "Fortran") + set(OpenMPTEST_SOURCE_FILE "${CMAKE_CURRENT_BINARY_DIR}/${SRC_FILE_NAME}.f90") + if(OpenMP_Fortran_HAVE_OMPLIB_MODULE) + set(OpenMP_Fortran_INCLUDE_LINE "use omp_lib\n implicit none") + else() + set(OpenMP_Fortran_INCLUDE_LINE "implicit none\n include 'omp_lib.h'") + endif() + configure_file("${SRC_FILE_NAME}.f90.in" "${OpenMPTEST_SOURCE_FILE}" @ONLY) + endif() +endmacro() + +foreach(c C CXX Fortran) + if("${OpenMP_TEST_${c}}") + message("Testing ${c}") + enable_language(${c}) + endif() +endforeach() + +find_package(OpenMP REQUIRED) + +foreach(c C CXX Fortran) + if(NOT "${OpenMP_TEST_${c}}") + continue() + endif() + source_code_mapper_helper(${c} main) + add_executable(test_tgt_${c} ${OpenMPTEST_SOURCE_FILE}) + target_link_libraries(test_tgt_${c} PRIVATE OpenMP::OpenMP_${c}) + set_property(TARGET test_tgt_${c} PROPERTY LINKER_LANGUAGE ${c}) + add_test(NAME test_tgt_${c} COMMAND test_tgt_${c}) + + add_executable(test_var_${c} ${OpenMPTEST_SOURCE_FILE}) + if(CMAKE_HOST_WIN32) + separate_arguments(_OpenMP_${c}_OPTIONS WINDOWS_COMMAND "${OpenMP_${c}_FLAGS}") + else() + separate_arguments(_OpenMP_${c}_OPTIONS UNIX_COMMAND "${OpenMP_${c}_FLAGS}") + endif() + target_compile_options(test_var_${c} PRIVATE "${_OpenMP_${c}_OPTIONS}") + target_link_libraries(test_var_${c} PRIVATE "${OpenMP_${c}_FLAGS}") + set_property(TARGET test_var_${c} PROPERTY LINKER_LANGUAGE ${c}) + add_test(NAME test_var_${c} COMMAND test_var_${c}) + + source_code_mapper_helper(${c} scalprod) + add_library(scalprod_${c} STATIC ${OpenMPTEST_SOURCE_FILE}) + target_link_libraries(scalprod_${c} PRIVATE OpenMP::OpenMP_${c}) + set_property(TARGET scalprod_${c} PROPERTY LINKER_LANGUAGE ${c}) +endforeach() + +foreach(c C CXX Fortran) + if(NOT "${OpenMP_TEST_${c}}") + continue() + endif() + foreach(d C CXX Fortran) + if(NOT "${OpenMP_TEST_${d}}") + continue() + endif() + source_code_mapper_helper(${c} scaltest) + add_executable(scaltest_${c}_${d} ${OpenMPTEST_SOURCE_FILE}) + target_link_libraries(scaltest_${c}_${d} PRIVATE scalprod_${d}) + set_property(TARGET scaltest_${c}_${d} PROPERTY LINKER_LANGUAGE ${c}) + add_test(NAME test_omp_${c}_${d} COMMAND scaltest_${c}_${d}) + set_property(TEST test_omp_${c}_${d} PROPERTY PASS_REGULAR_EXPRESSION "^[ \t]*70\\.?0*") + endforeach() +endforeach() diff --git a/Tests/FindOpenMP/Test/main.c b/Tests/FindOpenMP/Test/main.c new file mode 100644 index 0000000000..4f0e87483b --- /dev/null +++ b/Tests/FindOpenMP/Test/main.c @@ -0,0 +1,7 @@ +#include <omp.h> +int main() +{ +#ifndef _OPENMP + breaks_on_purpose +#endif +} diff --git a/Tests/FindOpenMP/Test/main.f90.in b/Tests/FindOpenMP/Test/main.f90.in new file mode 100644 index 0000000000..9da22a1cc6 --- /dev/null +++ b/Tests/FindOpenMP/Test/main.f90.in @@ -0,0 +1,5 @@ + program test + @OpenMP_Fortran_INCLUDE_LINE@ + !$ integer :: n + n = omp_get_num_threads() + end program test diff --git a/Tests/FindOpenMP/Test/scalprod.c b/Tests/FindOpenMP/Test/scalprod.c new file mode 100644 index 0000000000..24c4587af3 --- /dev/null +++ b/Tests/FindOpenMP/Test/scalprod.c @@ -0,0 +1,16 @@ +#include <omp.h> + +#ifdef __cplusplus +extern "C" +#endif + void + scalprod(int n, double* x, double* y, double* res) +{ + int i; + double res_v = 0.; +#pragma omp parallel for reduction(+ : res_v) + for (i = 0; i < n; ++i) { + res_v += x[i] * y[i]; + } + *res = res_v; +} diff --git a/Tests/FindOpenMP/Test/scalprod.f90.in b/Tests/FindOpenMP/Test/scalprod.f90.in new file mode 100644 index 0000000000..efc7ea0e40 --- /dev/null +++ b/Tests/FindOpenMP/Test/scalprod.f90.in @@ -0,0 +1,19 @@ +subroutine scalprod(n, x_p, y_p, res) bind(c) + use iso_c_binding + implicit none + integer(c_int), intent(in), value :: n + type(c_ptr), intent(in), value :: x_p, y_p + real(c_double), pointer :: x(:), y(:) + integer :: i + + real(c_double) :: res + real(c_double) :: scalpt = 0 + call c_f_pointer(x_p, x, shape=[n]) + call c_f_pointer(y_p, y, shape=[n]) + res = 0 +!$omp parallel do private(scalpt), reduction(+:res) + do i=1,n + scalpt = y(i) * x(i) + res = res + scalpt + end do +end subroutine scalprod diff --git a/Tests/FindOpenMP/Test/scaltest.c b/Tests/FindOpenMP/Test/scaltest.c new file mode 100644 index 0000000000..2ee57f8336 --- /dev/null +++ b/Tests/FindOpenMP/Test/scaltest.c @@ -0,0 +1,20 @@ +#ifdef __cplusplus +#include <iostream> +extern "C" +#else +#include <stdio.h> +#endif +int scalprod(int n, double* x, double* y, double* res); + +int main() +{ + double a[5] = { 1., 2., 3., 4., 5. }; + double b[5] = { 2., 3., 4., 5., 6. }; + double rk; + scalprod(5, a, b, &rk); +#ifdef __cplusplus + std::cout << rk << std::endl; +#else + printf("%f\n", rk); +#endif +} diff --git a/Tests/FindOpenMP/Test/scaltest.f90.in b/Tests/FindOpenMP/Test/scaltest.f90.in new file mode 100644 index 0000000000..64c20d285a --- /dev/null +++ b/Tests/FindOpenMP/Test/scaltest.f90.in @@ -0,0 +1,21 @@ +program scaltest + use iso_c_binding + implicit none + interface + subroutine scalprod(n, x_p, y_p, res) bind(c) + use iso_c_binding + integer(c_int), value :: n + type(c_ptr), value :: x_p, y_p + real(c_double) :: res + end subroutine scalprod + end interface + type(c_ptr) :: x_pt, y_pt + real(c_double), dimension(5), target :: a = (/ 1, 2, 3, 4, 5 /) + real(c_double), dimension(5), target :: b = (/ 2, 3, 4, 5, 6 /) + integer(c_int) :: n = size(a) + real(c_double) :: res + x_pt = c_loc(a) + y_pt = c_loc(b) + call scalprod(n, x_pt, y_pt, res) + print *, res +end program scaltest |