Skip to content

Commit

Permalink
Use standard CMake find_package(OpenACC) instead of handcrafted flags
Browse files Browse the repository at this point in the history
  • Loading branch information
wdeconinck committed Apr 26, 2024
1 parent d136165 commit 7b2691a
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 85 deletions.
41 changes: 16 additions & 25 deletions cmake/features/ACC.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,25 @@

if( atlas_HAVE_ATLAS_FIELD )

set( ATLAS_ACC_CAPABLE FALSE )
if( HAVE_CUDA )
if( CMAKE_Fortran_COMPILER_ID MATCHES "PGI|NVHPC" )
set( ATLAS_ACC_CAPABLE TRUE )
endif()
endif()

ecbuild_add_option( FEATURE ACC
DESCRIPTION "OpenACC capable data structures"
CONDITION ATLAS_ACC_CAPABLE )

if( atlas_HAVE_ACC )
if( CMAKE_Fortran_COMPILER_ID MATCHES "PGI|NVHPC" )
#set( ACC_Fortran_FLAGS -acc -ta=tesla,nordc )
set( ACC_Fortran_FLAGS "-acc=gpu;-gpu=gvmode,lineinfo,fastmath,rdc" )

if( OpenACC_Fortran_FLAGS )
set( ACC_Fortran_FLAGS ${OpenACC_Fortran_FLAGS} )
set( ATLAS_FIND_OPENACC ${HAVE_CUDA} )
if (DEFINED ATLAS_ENABLE_ACC)
if(NOT ATLAS_ENABLE_ACC)
set( ATLAS_FIND_OPENACC OFF )
endif()

set( ACC_C_FLAGS ${ACC_Fortran_FLAGS} )

find_program( ACC_C_COMPILER NAMES pgcc HINTS ${PGI_DIR} ${NVPHC_DIR} ENV PGI_DIR NVHPC_DIR PATH_SUFFIXES bin )
if( NOT ACC_C_COMPILER )
ecbuild_error( "Could not find OpenACC capable C compiler" )
elseif (DEFINED ENABLE_ACC)
if (NOT ENABLE_ACC)
set( ATLAS_FIND_OPENACC OFF )
endif()
endif()
endif()

if (ATLAS_FIND_OPENACC)
enable_language(C) # So that find_package(OpenACC) looks C components
find_package(OpenACC)
endif()

ecbuild_add_option( FEATURE ACC
DESCRIPTION "OpenACC capable data structures"
CONDITION HAVE_CUDA AND OpenACC_C_FOUND )

else()
set( HAVE_ACC 0 )
Expand Down
6 changes: 3 additions & 3 deletions cmake/project_summary.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ endif()
if( atlas_HAVE_ACC )

ecbuild_info( "ACC" )
ecbuild_info( " ACC_C_COMPILER : [${ACC_C_COMPILER}]" )
ecbuild_info( " ACC_C_FLAGS : [${ACC_C_FLAGS}]" )
ecbuild_info( " ACC_Fortran_FLAGS : [${ACC_Fortran_FLAGS}]" )
ecbuild_info( " OpenACC_C_FLAGS : [${OpenACC_C_FLAGS}]" )
ecbuild_info( " OpenACC_CXX_FLAGS : [${OpenACC_C_FLAGS}]" )
ecbuild_info( " OpenACC_Fortran_FLAGS : [${OpenACC_Fortran_FLAGS}]" )

endif()

Expand Down
31 changes: 4 additions & 27 deletions src/atlas_acc_support/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,10 @@

if( atlas_HAVE_ACC )

if (FALSE)
#if( NOT (CMAKE_C_COMPILER_ID MATCHES ${CMAKE_Fortran_COMPILER_ID}) )

add_custom_command(
OUTPUT ${CMAKE_BINARY_DIR}/lib/libatlas_acc_support.so ${CMAKE_CURRENT_BINARY_DIR}/atlas_acc_map_data.c.o
COMMAND ${ACC_C_COMPILER} ${ACC_C_FLAGS} ${ACC_C_INCLUDE} -fPIC -o ${CMAKE_CURRENT_BINARY_DIR}/atlas_acc_map_data.c.o
-c ${CMAKE_CURRENT_SOURCE_DIR}/atlas_acc_map_data.c
COMMAND mkdir -p ${CMAKE_BINARY_DIR}/lib
COMMAND ${ACC_C_COMPILER} ${ACC_C_FLAGS} -shared -o ${CMAKE_BINARY_DIR}/lib/libatlas_acc_support.so
${CMAKE_CURRENT_BINARY_DIR}/atlas_acc_map_data.c.o
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/atlas_acc_map_data.c
COMMENT "Building atlas_acc_support with ${ACC_C_COMPILER}"
)
add_custom_target( build-atlas_acc_support ALL DEPENDS ${CMAKE_BINARY_DIR}/lib/libatlas_acc_support.so )
add_library( atlas_acc_support SHARED IMPORTED GLOBAL )
set_property( TARGET atlas_acc_support PROPERTY IMPORTED_LOCATION ${CMAKE_BINARY_DIR}/lib/libatlas_acc_support.so )
set_property( TARGET atlas_acc_support PROPERTY IMPORTED_NO_SONAME TRUE )
set_property( TARGET atlas_acc_support PROPERTY IMPORTED_INCLUDE_DIRECTORIES ${CMAKE_CURRENT_SOURCE_DIR} )
add_dependencies( atlas_acc_support build-atlas_acc_support )
install( FILES ${CMAKE_BINARY_DIR}/lib/libatlas_acc_support.so DESTINATION ${INSTALL_LIB_DIR}/ )

else()

find_package( OpenACC REQUIRED )
ecbuild_add_library( TARGET atlas_acc_support TYPE STATIC SOURCES atlas_acc_map_data.c
ecbuild_add_library(
TARGET atlas_acc_support
SOURCES atlas_acc_map_data.c
TYPE STATIC
PRIVATE_LIBS OpenACC::OpenACC_C)

endif()

endif()
6 changes: 2 additions & 4 deletions src/sandbox/fortran_acc_fields/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@
# granted to it by virtue of its status as an intergovernmental organisation nor
# does it submit to any jurisdiction.

if( atlas_HAVE_ACC )
if( atlas_HAVE_ACC AND OpenACC_Fortran_FOUND )
ecbuild_add_executable(
TARGET atlas-acc-fields
SOURCES atlas-acc-fields.F90
LIBS atlas_f
LIBS atlas_f OpenACC::OpenACC_Fortran
LINKER_LANGUAGE Fortran
NOINSTALL
)
target_compile_options( atlas-acc-fields PUBLIC ${ACC_Fortran_FLAGS} )
target_link_libraries( atlas-acc-fields ${ACC_Fortran_FLAGS} )
endif()
14 changes: 3 additions & 11 deletions src/tests/acc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,25 @@
# granted to it by virtue of its status as an intergovernmental organisation nor
# does it submit to any jurisdiction.

if( HAVE_CUDA AND HAVE_TESTS AND HAVE_FCTEST AND HAVE_ACC )
if( HAVE_CUDA AND HAVE_TESTS AND HAVE_FCTEST AND HAVE_ACC AND OpenACC_Fortran_FOUND )

string (REPLACE ";" " " ACC_Fortran_FLAGS_STR "${ACC_Fortran_FLAGS}")


set_source_files_properties( fctest_unified_memory_with_openacc.F90 PROPERTIES COMPILE_FLAGS ${ACC_Fortran_FLAGS_STR} )
add_fctest(
TARGET atlas_test_unified_memory_with_openacc
SOURCES fctest_unified_memory_with_openacc.F90
fctest_unified_memory_with_openacc_cxx.cc
LIBS atlas_f
LIBS atlas_f OpenACC::OpenACC_Fortran CUDA::cudart
LINKER_LANGUAGE Fortran
ENVIRONMENT ${ATLAS_TEST_ENVIRONMENT} ATLAS_RUN_NGPUS=1
)
target_link_libraries( atlas_test_unified_memory_with_openacc ${ACC_Fortran_FLAGS} CUDA::cudart )

set_tests_properties( atlas_test_unified_memory_with_openacc PROPERTIES LABELS "gpu;acc")

add_fctest(
TARGET atlas_test_connectivity_openacc
SOURCES fctest_connectivity_openacc.F90
LIBS atlas_f
LIBS atlas_f OpenACC::OpenACC_Fortran
LINKER_LANGUAGE Fortran
ENVIRONMENT ${ATLAS_TEST_ENVIRONMENT} ATLAS_RUN_NGPUS=1
)
target_link_libraries( atlas_test_connectivity_openacc ${ACC_Fortran_FLAGS} )
set_target_properties( atlas_test_connectivity_openacc PROPERTIES COMPILE_FLAGS "${ACC_Fortran_FLAGS_STR}" )
set_tests_properties ( atlas_test_connectivity_openacc PROPERTIES LABELS "gpu;acc")

endif()
24 changes: 9 additions & 15 deletions src/tests/field/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,11 @@ ecbuild_add_test( TARGET atlas_test_field_foreach

ecbuild_add_test( TARGET atlas_test_field_acc
SOURCES test_field_acc.cc
LIBS atlas
LIBS atlas OpenACC::OpenACC_CXX
ENVIRONMENT ${ATLAS_TEST_ENVIRONMENT}
CONDITION atlas_HAVE_ACC
)
if( TEST atlas_test_field_acc )
target_compile_options( atlas_test_field_acc PRIVATE "${ACC_C_FLAGS}")
target_link_options( atlas_test_field_acc PRIVATE "${ACC_C_FLAGS}")
set_tests_properties ( atlas_test_field_acc PROPERTIES LABELS "gpu;acc")
endif()

Expand Down Expand Up @@ -70,18 +68,17 @@ if( HAVE_FCTEST )
)

add_fctest( TARGET atlas_fctest_field_device
CONDITION atlas_HAVE_ACC
CONDITION atlas_HAVE_ACC AND OpenACC_Fortran_FOUND
LINKER_LANGUAGE Fortran
SOURCES fctest_field_gpu.F90 external_acc_routine.F90
LIBS atlas_f
LIBS atlas_f OpenACC::OpenACC_Fortran
ENVIRONMENT ${ATLAS_TEST_ENVIRONMENT} ATLAS_RUN_NGPUS=1
)

if( TARGET atlas_fctest_field_device )
target_compile_options( atlas_fctest_field_device PUBLIC ${ACC_Fortran_FLAGS} )
target_link_libraries( atlas_fctest_field_device ${ACC_Fortran_FLAGS} )
target_link_options( atlas_fctest_field_device PRIVATE "${ACC_Fortran_FLAGS}")
if( TEST atlas_fctest_field_device )
set_tests_properties ( atlas_fctest_field_device PROPERTIES LABELS "gpu;acc")
endif()
if( TARGET atlas_fctest_field_device )
if( HAVE_GRIDTOOLS_STORAGE )
target_compile_definitions(atlas_fctest_field_device PUBLIC ATLAS_HAVE_GRIDTOOLS_STORAGE=1)
else()
Expand All @@ -90,17 +87,14 @@ if( HAVE_FCTEST )
endif()

add_fctest( TARGET atlas_fctest_field_wrap_device
CONDITION atlas_HAVE_ACC
CONDITION atlas_HAVE_ACC AND OpenACC_Fortran_FOUND
LINKER_LANGUAGE Fortran
SOURCES fctest_field_wrap_gpu.F90 external_acc_routine.F90
LIBS atlas_f
LIBS atlas_f OpenACC::OpenACC_Fortran
ENVIRONMENT ${ATLAS_TEST_ENVIRONMENT} ATLAS_RUN_NGPUS=1
)

if( TARGET atlas_fctest_field_wrap_device )
target_compile_options( atlas_fctest_field_wrap_device PUBLIC ${ACC_Fortran_FLAGS} )
target_link_libraries( atlas_fctest_field_wrap_device ${ACC_Fortran_FLAGS} )
target_link_options( atlas_fctest_field_wrap_device PRIVATE "${ACC_Fortran_FLAGS}")
if( TEST atlas_fctest_field_wrap_device )
set_tests_properties ( atlas_fctest_field_wrap_device PROPERTIES LABELS "gpu;acc")
endif()

Expand Down

0 comments on commit 7b2691a

Please sign in to comment.