kokkos / kokkos-tutorials

Tutorials for the Kokkos C++ Performance Portability Programming Ecosystem
https://kokkos.org
Other
298 stars 100 forks source link

Use CMake FetchContent to build Kokkos Core #65

Closed dalg24 closed 1 year ago

dalg24 commented 1 year ago

Enables doing

cmake -B build-cuda -DKokkos_ENABLE_CUDA=ON
cmake --build build-cuda
./build-cuda/<exe>
cmake -B build-openmp -DKokkos_ENABLE_OPENMP=ON
...

One caveat is that, as currently proposed would download Kokkos instead of using an external install. If using CMake 3.24 one can do

cmake -B build -DKokkos_ROOT=/path/to/kokkos/install
...

which would bypass the fetch content.

Let me know what you think. I would do the same for KokkosKernels once we can agree on something

masterleinad commented 1 year ago

For me,

diff --git a/Exercises/common.cmake b/Exercises/common.cmake
index ed081b7..933fcf0 100644
--- a/Exercises/common.cmake
+++ b/Exercises/common.cmake
@@ -8,21 +8,12 @@ endif()

 set(Kokkos_COMMON_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../dep/Kokkos)

-include(FetchContent)
-if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.24)
-  FetchContent_Declare(
-    Kokkos
-    GIT_REPOSITORY https://github.com/kokkos/kokkos.git
-    GIT_TAG        4.0.01
-    SOURCE_DIR ${Kokkos_COMMON_SOURCE_DIR}
-    FIND_PACKAGE_ARGS
-  )
-  FetchContent_MakeAvailable(Kokkos)
-  
-  find_package(Kokkos REQUIRED)
-else()
-  find_package(Kokkos)
-  if(NOT Kokkos_FOUND)
+find_package(Kokkos QUIET)
+if(NOT Kokkos_FOUND)
+  if(EXISTS ${Kokkos_COMMON_SOURCE_DIR})
+    add_subdirectory(${Kokkos_COMMON_SOURCE_DIR} Kokkos)
+  else()
+    include(FetchContent)
     FetchContent_Declare(
       Kokkos
       GIT_REPOSITORY https://github.com/kokkos/kokkos.git

works well and only downloads Kokkos once.

dalg24 commented 1 year ago

One downside with the approach you suggested is that if Kokkos is found, because of the QUIET option, it will not print what backends are enabled.

masterleinad commented 1 year ago

One downside with the approach you suggested is that if Kokkos is found, because of the QUIET option, it will not print what backends are enabled.

Then do

find_package (Kokkos CONFIG)