Skip to content

Commit a4281f9

Browse files
lukasm91havogt
authored andcommitted
Add cuda tests (#18)
Adds a specific test for OpenACC support in array wrappers. Tests only compilation, but not execution because obviously, it's nearly impossible to have only one toolchain that compiles fortran and C++ successfully. Enabling the test for execution should be revisited as gcc gets better in OpenACC support.
1 parent 54b3317 commit a4281f9

File tree

13 files changed

+215
-21
lines changed

13 files changed

+215
-21
lines changed

cmake/cpp_bindgen.cmake.in

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ function(cpp_bindgen_add_library target_name)
110110

111111
add_library(${target_name} ${ARG_SOURCES})
112112
target_link_libraries(${target_name} PRIVATE cpp_bindgen_generator)
113-
# target_include_directories(${target_name} PRIVATE ${PROJECT_SOURCE_DIR}/include) #TODO probably wrong
114113

115114
if(GT_ENABLE_BINDINGS_GENERATION)
116115
# generator

cmake/detect_features.cmake

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
if(NOT DEFINED CPP_BINDGEN_ENABLE_COMPILER_DETECTION)
22
if(${PROJECT_SOURCE_DIR} STREQUAL ${CMAKE_SOURCE_DIR})
3+
set(CPP_BINDGEN_ENABLE_COMPILER_DETECTION ON)
4+
else()
35
# If turned OFF, it will still use the compilers,
46
# - if they are available from a super-project, or
57
# - if compilers are forced to on via other options.
68
set(CPP_BINDGEN_ENABLE_COMPILER_DETECTION OFF)
7-
else()
8-
set(CPP_BINDGEN_ENABLE_COMPILER_DETECTION ON)
99
endif()
1010
endif()
1111

example/simple_fetch_content/CMakeLists.txt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
cmake_minimum_required(VERSION 3.12.4) #TODO replace with generated version
22
project(simple_example_with_fetch_content LANGUAGES CXX Fortran)
33

4-
# 1) fetch c_bindings from the repository
4+
# 1) fetch cpp_bindgen from the repository
55
# TODO: use FetchContent_MakeAvailable() once we require CMake 3.14+
66
include(FetchContent)
77
FetchContent_Declare(
8-
c_bindings
8+
cpp_bindgen
99
# In your application remove SOURCE_DIR ...
1010
SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/../..
1111
# By the following 2 lines to fetch content directly from github
12-
# GIT_REPOSITORY https://github.com/GridTools/c_bindings.git
12+
# GIT_REPOSITORY https://github.com/GridTools/cpp_bindgen.git
1313
# GIT_TAG master # consider replacing master by a tagged version
1414
)
15-
FetchContent_GetProperties(c_bindings)
15+
# FetchContent_MakeAvailable(cpp_bindgen) for CMake >= 3.14
16+
FetchContent_GetProperties(cpp_bindgen)
1617
if(NOT c_bindings_POPULATED)
17-
FetchContent_Populate(c_bindings)
18-
add_subdirectory(${c_bindings_SOURCE_DIR} ${c_bindings_BINARY_DIR})
18+
FetchContent_Populate(cpp_bindgen)
19+
add_subdirectory(${cpp_bindgen_SOURCE_DIR} ${cpp_bindgen_BINARY_DIR})
1920
endif()
2021

2122
# 2) create a library with bindings. This will generate the files simple.h and simple.f90 which can be included within C and Fortran. In CMake you can use them by linking against `simple_c`or `simple_fortran`.

jenkins/build.sh

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
set -e
44

55
HOST=$(hostname)
6+
test -n "${CSCS_ACCOUNT}" || CSCS_ACCOUNT=d75
67

78
if [[ "$HOST" == kesch* || "$HOST" == escha* ]]; then
89
module load cmake/3.13.4
@@ -15,11 +16,15 @@ if [[ "$HOST" == kesch* || "$HOST" == escha* ]]; then
1516
export CUDATOOLKIT_HOME=$CUDA_PATH
1617
export CUDA_ARCH=sm_37
1718
export FC=`which gfortran`
19+
RUN_PREFIX="srun -p pp-short -c 12 --time=00:30:00"
20+
1821
elif [[ "$HOST" == tave* ]]; then
1922
module switch PrgEnv-cray PrgEnv-gnu
2023
module rm CMake
2124
module load /users/jenkins/easybuild/tave/modules/all/CMake/3.12.4
2225
export BOOST_ROOT=/project/c14/install/kesch/boost/boost_1_67_0
26+
RUN_PREFIX=""
27+
2328
elif [[ "$HOST" == daint* ]]; then
2429
module load daint-gpu
2530
module load cudatoolkit/9.2.148_3.19-6.0.7.1_2.1__g3d9acc8
@@ -28,6 +33,8 @@ elif [[ "$HOST" == daint* ]]; then
2833
module load /users/jenkins/easybuild/daint/haswell/modules/all/CMake/3.12.4
2934
export BOOST_ROOT=/project/c14/install/daint/boost/boost_1_67_0
3035
export CUDA_ARCH=sm_60
36+
RUN_PREFIX="srun -C gpu -p cscsci --account=$CSCS_ACCOUNT --time=00:30:00"
37+
3138
else
3239
echo "Unknown host ${HOST}, using current environment."
3340
fi
@@ -38,7 +45,7 @@ cwd=$(pwd)
3845
mkdir -p build && cd build
3946
cmake .. -DCMAKE_INSTALL_PREFIX=${cwd}/install -DCPP_BINDGEN_REQUIRE_TEST_C=ON -DCPP_BINDGEN_REQUIRE_TEST_Fortran=ON
4047
nice make -j8 install
41-
ctest .
48+
$RUN_PREFIX ctest .
4249

4350
# compile example using the config from build tree (test for export(package))
4451
cd ${cwd}/example/simple
@@ -68,5 +75,5 @@ if [[ "$HOST" != kesch* ]]; then
6875
mkdir -p build_legacy && cd build_legacy
6976
cmake .. -DCMAKE_INSTALL_PREFIX=${cwd}/install -DCPP_BINDGEN_GT_LEGACY=ON
7077
nice make -j8 install
71-
ctest .
78+
$RUN_PREFIX ctest .
7279
fi

tests/regression/array/CMakeLists.txt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,17 @@ add_executable(gen_regression_array_driver_fortran driver.f90)
44
target_link_libraries(gen_regression_array_driver_fortran gen_regression_array_fortran)
55
add_test(NAME gen_regression_array_driver_fortran
66
COMMAND $<TARGET_FILE:gen_regression_array_driver_fortran>)
7-
set_target_properties(gen_regression_array_driver_fortran PROPERTIES LINKER_LANGUAGE Fortran)
7+
8+
if(CUDA_AVAILABLE AND "${CMAKE_CXX_COMPILER_VERSION}" VERSION_GREATER_EQUAL "6")
9+
cpp_bindgen_add_library(gen_regression_array_cu SOURCES implementation.cu)
10+
cpp_bindgen_enable_fortran_openacc_on_target(gen_regression_array_cu_fortran)
11+
12+
add_executable(gen_regression_array_driver_cu_fortran driver_cu.f90)
13+
target_link_libraries(gen_regression_array_driver_cu_fortran gen_regression_array_cu_fortran)
14+
cpp_bindgen_enable_fortran_openacc_on_target(gen_regression_array_driver_cu_fortran)
15+
16+
# TODO
17+
# we don't add this test on purpose for now, because it is likely that this test does not work
18+
# out of the box. The Fortran code would need to be compiled separately with a different
19+
# compiler than the C++ code with a compiler that has proper OpenACC support.
20+
endif()

tests/regression/array/driver.f90

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@ program main
1717

1818
call fill_array(arr)
1919

20-
DO i=1, ie
21-
DO j=1, je
22-
DO k=1, ke
20+
do i=1, ie
21+
do j=1, je
22+
do k=1, ke
2323
expected(i,j,k) = (i-1)*10000 + (j-1)*100 + (k-1)
24-
END DO
25-
END DO
26-
END DO
24+
end do
25+
end do
26+
end do
2727

2828
if (any(arr /= expected)) stop 1
29+
30+
print *, "Success!"
2931
end

tests/regression/array/driver_cu.f90

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
! GridTools
2+
!
3+
! Copyright (c) 2014-2019, ETH Zurich
4+
! All rights reserved.
5+
!
6+
! Please, refer to the LICENSE file in the root directory.
7+
! SPDX-License-Identifier: BSD-3-Clause
8+
9+
program main
10+
use iso_c_binding
11+
use gen_handle
12+
use gen_regression_array_cu
13+
implicit none
14+
integer, parameter :: ie = 9, je = 10, ke = 11
15+
integer :: i, j, k
16+
real(8), dimension(ie, je, ke) :: arr, expected
17+
18+
!$acc enter data create(arr)
19+
20+
call fill_gpu_array(arr)
21+
22+
!$acc exit data copyout(arr)
23+
24+
do i=1, ie
25+
do j=1, je
26+
do k=1, ke
27+
expected(i,j,k) = (i-1)*10000 + (j-1)*100 + (k-1)
28+
end do
29+
end do
30+
end do
31+
32+
if (any(arr /= expected)) stop 1
33+
34+
print *, "Success!"
35+
end
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
! This file is generated!
2+
module gen_regression_array_cu
3+
implicit none
4+
interface
5+
6+
subroutine fill_array_impl(arg0) bind(c, name="fill_array")
7+
use iso_c_binding
8+
use gen_array_descriptor
9+
type(gen_fortran_array_descriptor) :: arg0
10+
end subroutine
11+
subroutine fill_gpu_array_impl(arg0) bind(c, name="fill_gpu_array")
12+
use iso_c_binding
13+
use gen_array_descriptor
14+
type(gen_fortran_array_descriptor) :: arg0
15+
end subroutine
16+
17+
end interface
18+
contains
19+
subroutine fill_array(arg0)
20+
use iso_c_binding
21+
use gen_array_descriptor
22+
real(c_double), dimension(:,:,:), target :: arg0
23+
type(gen_fortran_array_descriptor) :: descriptor0
24+
25+
descriptor0%rank = 3
26+
descriptor0%type = 6
27+
descriptor0%dims = reshape(shape(arg0), &
28+
shape(descriptor0%dims), (/0/))
29+
descriptor0%data = c_loc(arg0(lbound(arg0, 1),lbound(arg0, 2),lbound(arg0, 3)))
30+
31+
call fill_array_impl(descriptor0)
32+
end subroutine
33+
subroutine fill_gpu_array(arg0)
34+
use iso_c_binding
35+
use gen_array_descriptor
36+
real(c_double), dimension(:,:,:), target :: arg0
37+
type(gen_fortran_array_descriptor) :: descriptor0
38+
39+
!$acc data present(arg0)
40+
!$acc host_data use_device(arg0)
41+
descriptor0%rank = 3
42+
descriptor0%type = 6
43+
descriptor0%dims = reshape(shape(arg0), &
44+
shape(descriptor0%dims), (/0/))
45+
descriptor0%data = c_loc(arg0(lbound(arg0, 1),lbound(arg0, 2),lbound(arg0, 3)))
46+
!$acc end host_data
47+
!$acc end data
48+
49+
call fill_gpu_array_impl(descriptor0)
50+
end subroutine
51+
end
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// This file is generated!
2+
#pragma once
3+
4+
#include <cpp_bindgen/array_descriptor.h>
5+
#include <cpp_bindgen/handle.h>
6+
7+
#ifdef __cplusplus
8+
extern "C" {
9+
#endif
10+
11+
void fill_array(gen_fortran_array_descriptor*);
12+
void fill_gpu_array(gen_fortran_array_descriptor*);
13+
14+
#ifdef __cplusplus
15+
}
16+
#endif

tests/regression/array/implementation.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include <array>
1212

1313
#include <cpp_bindgen/export.hpp>
14-
#include <type_traits>
14+
#include <iostream>
1515

1616
namespace custom_array {
1717
template <class T>

0 commit comments

Comments
 (0)