Skip to content

Commit 7767559

Browse files
committed
Kokkos OpenACC build (almost done)
1 parent f02431e commit 7767559

File tree

2 files changed

+73
-9
lines changed

2 files changed

+73
-9
lines changed

cpp-mdspan/CMakeLists.txt

+31-5
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ endif()
106106
# Users can set FETCHCONTENT_SOURCE_DIR_KOKKOS
107107
# to a local path to Kokkos, to override automatic downloading.
108108

109+
set(Kokkos_ENABLE_SERIAL ON CACHE INTERNAL "")
110+
set(Kokkos_ENABLE_OPENACC ON CACHE INTERNAL "")
111+
109112
FetchContent_Declare(
110113
Kokkos
111114
GIT_REPOSITORY https://github.com/kokkos/kokkos.git
@@ -181,13 +184,36 @@ target_compile_options(miniWeather_serial PRIVATE
181184
)
182185

183186
if (kokkos_POPULATED)
184-
add_executable(miniWeather_kokkos miniWeather_mdspan.cpp miniWeather_common.cpp)
185-
target_include_directories(miniWeather_kokkos PRIVATE "${PROJECT_SOURCE_DIR}")
187+
add_executable(miniWeather_kokkos_serial miniWeather_mdspan.cpp miniWeather_common.cpp)
188+
target_include_directories(miniWeather_kokkos_serial PRIVATE "${PROJECT_SOURCE_DIR}")
189+
190+
target_link_libraries(miniWeather_kokkos_serial PRIVATE Kokkos::kokkos)
191+
target_compile_definitions(miniWeather_kokkos_serial PRIVATE MINIWEATHER_KOKKOS)
192+
target_compile_definitions(miniWeather_kokkos_serial PRIVATE MINIWEATHER_KOKKOS_SERIAL)
193+
194+
# We got mdspan from Kokkos.
195+
target_compile_options(miniWeather_kokkos_serial PRIVATE
196+
$<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>,$<CXX_COMPILER_ID:GNU>>:
197+
-Wall>
198+
$<$<CXX_COMPILER_ID:MSVC>:
199+
/W4>
200+
)
201+
endif()
202+
203+
# FIXME (mfh 2025/04/04) Figure out how to pass OpenACC flags to Kokkos.
204+
if (kokkos_POPULATED AND OpenACC_FOUND)
205+
set(Kokkos_ENABLE_OPENACC ON)
206+
207+
add_executable(miniWeather_kokkos_openacc miniWeather_mdspan.cpp miniWeather_common.cpp)
208+
target_include_directories(miniWeather_kokkos_openacc PRIVATE "${PROJECT_SOURCE_DIR}")
209+
210+
target_link_libraries(miniWeather_kokkos_openacc PRIVATE Kokkos::kokkos)
211+
target_compile_definitions(miniWeather_kokkos_openacc PRIVATE MINIWEATHER_KOKKOS)
212+
target_compile_definitions(miniWeather_kokkos_openacc PRIVATE MINIWEATHER_KOKKOS_OPENACC)
186213

187-
target_link_libraries(miniWeather_kokkos PRIVATE Kokkos::kokkos)
188-
target_compile_definitions(miniWeather_kokkos PRIVATE MINIWEATHER_KOKKOS)
189214
# We got mdspan from Kokkos.
190-
target_compile_options(miniWeather_kokkos PRIVATE
215+
target_link_libraries(miniWeather_kokkos_openacc PRIVATE OpenACC::OpenACC_CXX)
216+
target_compile_options(miniWeather_kokkos_openacc PRIVATE
191217
$<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>,$<CXX_COMPILER_ID:GNU>>:
192218
-Wall>
193219
$<$<CXX_COMPILER_ID:MSVC>:

cpp-mdspan/miniWeather_kokkos.hpp

+42-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,29 @@
11
#pragma once
22

33
#include "miniWeather_common.hpp"
4-
#include <Kokkos_Core.hpp>
4+
#include "Kokkos_Core.hpp"
55

6-
using kokkos_execution_policy = Kokkos::DefaultExecutionSpace;
6+
#if defined(MINIWEATHER_KOKKOS_OPENACC)
7+
# if ! defined(KOKKOS_ENABLE_OPENACC)
8+
# error "Kokkos OpenACC is not enabled"
9+
# endif
10+
11+
// Users aren't allowed to include OpenACC/Kokkos_OpenACC.hpp directly.
12+
//#include "OpenACC/Kokkos_OpenACC.hpp"
13+
using kokkos_execution_policy = Kokkos::Experimental::OpenACC;
14+
15+
#elif defined(MINIWEATHER_KOKKOS_SERIAL)
16+
# if ! defined(KOKKOS_ENABLE_SERIAL)
17+
# error "Kokkos Serial is not enabled"
18+
# endif
19+
20+
// Users aren't allowed to include Serial/Kokkos_Serial.hpp directly.
21+
//#include "Serial/Kokkos_Serial.hpp"
22+
using kokkos_execution_policy = Kokkos::Serial;
23+
24+
#else
25+
# error "No Kokkos execution policy defined"
26+
#endif
727

828
template<int MyRank>
929
using md_range_policy = Kokkos::MDRangePolicy<
@@ -369,9 +389,28 @@ reduction_result local_reductions(
369389
auto hy_dens_cell = const_arrays.hy_dens_cell();
370390
auto hy_dens_theta_cell = const_arrays.hy_dens_theta_cell();
371391

392+
// Kokkos doesn't currently implement parallel_reduce(MDRangePolicy) for OpenACC.
393+
#if defined(MINIWEATHER_KOKKOS_OPENACC)
394+
# if defined(KOKKOS_ENABLE_CUDA)
395+
auto my_exec_policy = Kokkos::Cuda{};
396+
# elif defined(KOKKOS_ENABLE_SERIAL)
397+
auto my_exec_policy = Kokkos::Serial{};
398+
# else
399+
# error "No fall-back execution policy defined"
400+
# endif
401+
#else
402+
auto my_exec_policy = exec_policy;
403+
#endif
404+
405+
Kokkos::MDRangePolicy<
406+
std::remove_cvref_t<decltype(my_exec_policy)>,
407+
Kokkos::Rank<2>,
408+
Kokkos::IndexType<int>>
409+
range(my_exec_policy, {0, 0}, {nz, nx});
410+
372411
Kokkos::parallel_reduce(
373412
"local_reductions",
374-
md_range_policy<2>(exec_policy, {0, 0}, {nz, nx}),
413+
range,
375414
KOKKOS_LAMBDA(int k, int i, reduction_result& thread_local_result) {
376415
double r = state(ID_DENS, k+hs, i+hs) + hy_dens_cell[hs+k]; // Density
377416
double u = state(ID_UMOM, k+hs, i+hs) / r; // U-wind
@@ -384,7 +423,6 @@ reduction_result local_reductions(
384423
thread_local_result.mass += r *dx*dz; // Accumulate domain mass
385424
thread_local_result.te += (ke + ie)*dx*dz; // Accumulate domain total energy
386425
}, result);
387-
388426
return result;
389427
}
390428

0 commit comments

Comments
 (0)