FindOpenACC.cmake 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. FindOpenACC
  5. -----------
  6. Detect OpenACC support by the compiler.
  7. This module can be used to detect OpenACC support in a compiler.
  8. If the compiler supports OpenACC, the flags required to compile with
  9. OpenACC support are returned in variables for the different languages.
  10. Currently, only PGI, GNU and Cray compilers are supported.
  11. Variables
  12. ^^^^^^^^^
  13. This module will set the following variables per language in your
  14. project, where ``<lang>`` is one of C, CXX, or Fortran:
  15. ``OpenACC_<lang>_FOUND``
  16. Variable indicating if OpenACC support for ``<lang>`` was detected.
  17. ``OpenACC_<lang>_FLAGS``
  18. OpenACC compiler flags for ``<lang>``, separated by spaces.
  19. ``OpenACC_<lang>_OPTIONS``
  20. OpenACC compiler flags for ``<lang>``, as a list. Suitable for usage
  21. with target_compile_options or target_link_options.
  22. Additionally, the module provides :prop_tgt:`IMPORTED` targets:
  23. ``OpenACC::OpenACC_<lang>``
  24. Target for using OpenACC from ``<lang>``.
  25. The module will also try to provide the OpenACC version variables:
  26. ``OpenACC_<lang>_SPEC_DATE``
  27. Date of the OpenACC specification implemented by the ``<lang>`` compiler.
  28. ``OpenACC_<lang>_VERSION_MAJOR``
  29. Major version of OpenACC implemented by the ``<lang>`` compiler.
  30. ``OpenACC_<lang>_VERSION_MINOR``
  31. Minor version of OpenACC implemented by the ``<lang>`` compiler.
  32. ``OpenACC_<lang>_VERSION``
  33. OpenACC version implemented by the ``<lang>`` compiler.
  34. The specification date is formatted as given in the OpenACC standard:
  35. ``yyyymm`` where ``yyyy`` and ``mm`` represents the year and month of
  36. the OpenACC specification implemented by the ``<lang>`` compiler.
  37. Input Variables
  38. ^^^^^^^^^^^^^^^
  39. ``OpenACC_ACCEL_TARGET=<target>``
  40. If set, will the correct target accelerator flag set to the <target> will
  41. be returned with OpenACC_<lang>_FLAGS.
  42. #]=======================================================================]
  43. set(OpenACC_C_CXX_TEST_SOURCE
  44. "
  45. int main(){
  46. #ifdef _OPENACC
  47. return 0;
  48. #else
  49. breaks_on_purpose
  50. #endif
  51. }
  52. "
  53. )
  54. set(OpenACC_Fortran_TEST_SOURCE
  55. "
  56. program test
  57. #ifndef _OPENACC
  58. breaks_on_purpose
  59. #endif
  60. endprogram test
  61. "
  62. )
  63. set(OpenACC_C_CXX_CHECK_VERSION_SOURCE
  64. "
  65. #include <stdio.h>
  66. const char accver_str[] = { 'I', 'N', 'F', 'O', ':', 'O', 'p', 'e', 'n', 'A',
  67. 'C', 'C', '-', 'd', 'a', 't', 'e', '[',
  68. ('0' + ((_OPENACC/100000)%10)),
  69. ('0' + ((_OPENACC/10000)%10)),
  70. ('0' + ((_OPENACC/1000)%10)),
  71. ('0' + ((_OPENACC/100)%10)),
  72. ('0' + ((_OPENACC/10)%10)),
  73. ('0' + ((_OPENACC/1)%10)),
  74. ']', '\\0' };
  75. int main()
  76. {
  77. puts(accver_str);
  78. return 0;
  79. }
  80. ")
  81. set(OpenACC_Fortran_CHECK_VERSION_SOURCE
  82. "
  83. program acc_ver
  84. implicit none
  85. integer, parameter :: zero = ichar('0')
  86. character, dimension(25), parameter :: accver_str =&
  87. (/ 'I', 'N', 'F', 'O', ':', 'O', 'p', 'e', 'n', 'A', 'C', 'C', '-',&
  88. 'd', 'a', 't', 'e', '[',&
  89. char(zero + mod(_OPENACC/100000, 10)),&
  90. char(zero + mod(_OPENACC/10000, 10)),&
  91. char(zero + mod(_OPENACC/1000, 10)),&
  92. char(zero + mod(_OPENACC/100, 10)),&
  93. char(zero + mod(_OPENACC/10, 10)),&
  94. char(zero + mod(_OPENACC/1, 10)), ']' /)
  95. print *, accver_str
  96. end program acc_ver
  97. "
  98. )
  99. function(_OPENACC_WRITE_SOURCE_FILE LANG SRC_FILE_CONTENT_VAR SRC_FILE_NAME SRC_FILE_FULLPATH)
  100. set(WORK_DIR ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/FindOpenACC)
  101. if("${LANG}" STREQUAL "C")
  102. set(SRC_FILE "${WORK_DIR}/${SRC_FILE_NAME}.c")
  103. file(WRITE "${SRC_FILE}" "${OpenACC_C_CXX_${SRC_FILE_CONTENT_VAR}}")
  104. elseif("${LANG}" STREQUAL "CXX")
  105. set(SRC_FILE "${WORK_DIR}/${SRC_FILE_NAME}.cpp")
  106. file(WRITE "${SRC_FILE}" "${OpenACC_C_CXX_${SRC_FILE_CONTENT_VAR}}")
  107. elseif("${LANG}" STREQUAL "Fortran")
  108. set(SRC_FILE "${WORK_DIR}/${SRC_FILE_NAME}.F90")
  109. file(WRITE "${SRC_FILE}_in" "${OpenACC_Fortran_${SRC_FILE_CONTENT_VAR}}")
  110. configure_file("${SRC_FILE}_in" "${SRC_FILE}" @ONLY)
  111. endif()
  112. set(${SRC_FILE_FULLPATH} "${SRC_FILE}" PARENT_SCOPE)
  113. endfunction()
  114. function(_OPENACC_GET_FLAGS_CANDIDATE LANG FLAG_VAR)
  115. set(ACC_FLAG_PGI "-acc")
  116. set(ACC_FLAG_GNU "-fopenacc")
  117. set(ACC_FLAG_Cray "-h acc")
  118. if(DEFINED ACC_FLAG_${CMAKE_${LANG}_COMPILER_ID})
  119. set("${FLAG_VAR}" "${ACC_FLAG_${CMAKE_${LANG}_COMPILER_ID}}" PARENT_SCOPE)
  120. else()
  121. # Fall back to a few common flags.
  122. set("${FLAG_VAR}" ${ACC_FLAG_GNU} ${ACC_FLAG_PGI})
  123. endif()
  124. endfunction()
  125. function(_OPENACC_GET_ACCEL_TARGET_FLAG LANG TARGET FLAG_VAR)
  126. # Find target accelerator flags.
  127. set(ACC_TARGET_FLAG_PGI "-ta")
  128. if(DEFINED ACC_TARGET_FLAG_${CMAKE_${LANG}_COMPILER_ID})
  129. set("${FLAG_VAR}" "${ACC_TARGET_FLAG_${CMAKE_${LANG}_COMPILER_ID}}=${TARGET}" PARENT_SCOPE)
  130. endif()
  131. endfunction()
  132. function(_OPENACC_GET_VERBOSE_FLAG LANG FLAG_VAR)
  133. # Find compiler's verbose flag for OpenACC.
  134. set(ACC_VERBOSE_FLAG_PGI "-Minfo=accel")
  135. if(DEFINED ACC_VERBOSE_FLAG_${CMAKE_${LANG}_COMPILER_ID})
  136. set("${FLAG_VAR}" "${ACC_VERBOSE_FLAG_${CMAKE_${LANG}_COMPILER_ID}}" PARENT_SCOPE)
  137. endif()
  138. endfunction()
  139. function(_OPENACC_GET_FLAGS LANG FLAG_VAR)
  140. set(FLAG_CANDIDATES "")
  141. _OPENACC_GET_FLAGS_CANDIDATE("${LANG}" FLAG_CANDIDATES)
  142. _OPENACC_WRITE_SOURCE_FILE("${LANG}" "TEST_SOURCE" OpenACCTryFlag _OPENACC_TEST_SRC)
  143. foreach(FLAG IN LISTS FLAG_CANDIDATES)
  144. try_compile(OpenACC_FLAG_TEST_RESULT ${CMAKE_BINARY_DIR} ${_OPENACC_TEST_SRC}
  145. CMAKE_FLAGS "-DCOMPILE_DEFINITIONS:STRING=${FLAG}"
  146. OUTPUT_VARIABLE OpenACC_TRY_COMPILE_OUTPUT
  147. )
  148. if(OpenACC_FLAG_TEST_RESULT)
  149. set("${FLAG_VAR}" "${FLAG}")
  150. if(DEFINED OpenACC_ACCEL_TARGET)
  151. _OPENACC_GET_ACCEL_TARGET_FLAG("${LANG}" "${OpenACC_ACCEL_TARGET}" TARGET_FLAG)
  152. string(APPEND "${FLAG_VAR}" " ${TARGET_FLAG}")
  153. endif()
  154. if(CMAKE_VERBOSE_MAKEFILE)
  155. # -Minfo=accel prints out OpenACC's messages on optimizations.
  156. _OPENACC_GET_VERBOSE_FLAG("${LANG}" OpenACC_VERBOSE_FLAG)
  157. string(APPEND "${FLAG_VAR}" " ${OpenACC_VERBOSE_FLAG}")
  158. endif()
  159. set("${FLAG_VAR}" "${${FLAG_VAR}}" PARENT_SCOPE)
  160. break()
  161. endif()
  162. endforeach()
  163. endfunction()
  164. function(_OPENACC_GET_SPEC_DATE LANG SPEC_DATE)
  165. _OPENACC_WRITE_SOURCE_FILE("${LANG}" "CHECK_VERSION_SOURCE" OpenACCCheckVersion _OPENACC_TEST_SRC)
  166. set(BIN_FILE "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/FindOpenACC/accver_${LANG}.bin")
  167. try_compile(OpenACC_SPECTEST_${LANG} "${CMAKE_BINARY_DIR}" "${_OPENACC_TEST_SRC}"
  168. CMAKE_FLAGS "-DCOMPILE_DEFINITIONS:STRING=${OpenACC_${LANG}_FLAGS}"
  169. COPY_FILE ${BIN_FILE}
  170. OUTPUT_VARIABLE OUTPUT)
  171. if(${OpenACC_SPECTEST_${LANG}})
  172. file(STRINGS ${BIN_FILE} specstr LIMIT_COUNT 1 REGEX "INFO:OpenACC-date")
  173. set(regex_spec_date ".*INFO:OpenACC-date\\[0*([^]]*)\\].*")
  174. if("${specstr}" MATCHES "${regex_spec_date}")
  175. set(${SPEC_DATE} "${CMAKE_MATCH_1}" PARENT_SCOPE)
  176. endif()
  177. endif()
  178. endfunction()
  179. macro(_OPENACC_SET_VERSION_BY_SPEC_DATE LANG)
  180. set(OpenACC_SPEC_DATE_MAP
  181. # Combined versions, 2.5 onwards
  182. "201510=2.5"
  183. # 2013 08 is the corrected version.
  184. "201308=2.0"
  185. "201306=2.0"
  186. "201111=1.0"
  187. )
  188. string(REGEX MATCHALL "${OpenACC_${LANG}_SPEC_DATE}=([0-9]+)\\.([0-9]+)" _version_match "${OpenACC_SPEC_DATE_MAP}")
  189. if(NOT _version_match STREQUAL "")
  190. set(OpenACC_${LANG}_VERSION_MAJOR ${CMAKE_MATCH_1})
  191. set(OpenACC_${LANG}_VERSION_MINOR ${CMAKE_MATCH_2})
  192. set(OpenACC_${LANG}_VERSION "${OpenACC_${LANG}_VERSION_MAJOR}.${OpenACC_${LANG}_VERSION_MINOR}")
  193. else()
  194. unset(OpenACC_${LANG}_VERSION_MAJOR)
  195. unset(OpenACC_${LANG}_VERSION_MINOR)
  196. unset(OpenACC_${LANG}_VERSION)
  197. endif()
  198. unset(_version_match)
  199. unset(OpenACC_SPEC_DATE_MAP)
  200. endmacro()
  201. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  202. foreach (LANG IN ITEMS C CXX Fortran)
  203. if(CMAKE_${LANG}_COMPILER_LOADED)
  204. set(OpenACC_${LANG}_FIND_QUIETLY ${OpenACC_FIND_QUIETLY})
  205. set(OpenACC_${LANG}_FIND_REQUIRED ${OpenACC_FIND_REQUIRED})
  206. set(OpenACC_${LANG}_FIND_VERSION ${OpenACC_FIND_VERSION})
  207. set(OpenACC_${LANG}_FIND_VERSION_EXACT ${OpenACC_FIND_VERSION_EXACT})
  208. if(NOT DEFINED OpenACC_${LANG}_FLAGS)
  209. _OPENACC_GET_FLAGS("${LANG}" OpenACC_${LANG}_FLAGS)
  210. endif()
  211. if(NOT DEFINED OpenACC_${LANG}_OPTIONS)
  212. separate_arguments(OpenACC_${LANG}_OPTIONS NATIVE_COMMAND "${OpenACC_${LANG}_FLAGS}")
  213. endif()
  214. _OPENACC_GET_SPEC_DATE("${LANG}" OpenACC_${LANG}_SPEC_DATE)
  215. _OPENACC_SET_VERSION_BY_SPEC_DATE("${LANG}")
  216. find_package_handle_standard_args(OpenACC_${LANG}
  217. REQUIRED_VARS OpenACC_${LANG}_FLAGS
  218. VERSION_VAR OpenACC_${LANG}_VERSION
  219. )
  220. endif()
  221. endforeach()
  222. foreach (LANG IN ITEMS C CXX Fortran)
  223. if(OpenACC_${LANG}_FOUND AND NOT TARGET OpenACC::OpenACC_${LANG})
  224. add_library(OpenACC::OpenACC_${LANG} INTERFACE IMPORTED)
  225. endif()
  226. if(OpenACC_${LANG}_LIBRARIES)
  227. set_property(TARGET OpenACC::OpenACC_${LANG} PROPERTY
  228. INTERFACE_LINK_LIBRARIES "${OpenACC_${LANG}_LIBRARIES}")
  229. endif()
  230. if(OpenACC_${LANG}_FLAGS)
  231. set_property(TARGET OpenACC::OpenACC_${LANG} PROPERTY
  232. INTERFACE_COMPILE_OPTIONS "$<$<COMPILE_LANGUAGE:${LANG}>:${OpenACC_${LANG}_OPTIONS}>")
  233. set_property(TARGET OpenACC::OpenACC_${LANG} PROPERTY
  234. INTERFACE_LINK_OPTIONS "$<$<COMPILE_LANGUAGE:${LANG}>:${OpenACC_${LANG}_OPTIONS}>")
  235. unset(_OpenACC_${LANG}_OPTIONS)
  236. endif()
  237. endforeach()
  238. unset(OpenACC_C_CXX_TEST_SOURCE)
  239. unset(OpenACC_Fortran_TEST_SOURCE)
  240. unset(OpenACC_C_CXX_CHECK_VERSION_SOURCE)
  241. unset(OpenACC_Fortran_CHECK_VERSION_SOURCE)