Skip to content

Commit 7fc6208

Browse files
refactor a little
1 parent efb0563 commit 7fc6208

File tree

3 files changed

+250
-93
lines changed

3 files changed

+250
-93
lines changed

benchmarks/cloverleaf/CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ if(NOT TARGET alpaka::alpaka)
1919
endif()
2020
endif()
2121

22+
if (alpaka_USE_MDSPAN STREQUAL "OFF")
23+
message(STATUS "The conv2DWithMdspan example requires mdspan. Please set alpaka_USE_MDSPAN accordingly. Example disabled.")
24+
return()
25+
endif ()
2226

2327
set(_TARGET_NAME "cloverleaf")
2428
append_recursive_files_add_to_src_group("src/" "src/" "cpp" _FILES_SOURCE)

benchmarks/cloverleaf/src/cloverLeafKernels.hpp

+71-9
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,62 @@ const Idx nx = 512; // Number of cells in x direction
1212
const Idx ny = 512; // Number of cells in y direction
1313
const Idx nz = 512; // Number of cells in z direction
1414

15+
// Kernel to update the halo regions
16+
struct UpdateHaloKernel
17+
{
18+
template<typename TAcc, typename MdSpan>
19+
ALPAKA_FN_ACC auto operator()(TAcc const& acc, MdSpan density) const -> void
20+
{
21+
auto const i = alpaka::getIdx<alpaka::Grid, alpaka::Threads>(acc)[0];
22+
auto const j = alpaka::getIdx<alpaka::Grid, alpaka::Threads>(acc)[1];
23+
auto const k = alpaka::getIdx<alpaka::Grid, alpaka::Threads>(acc)[2];
24+
25+
if(i < nx && j < ny && k < nz)
26+
{
27+
// Update halo cells for density (simplified example)
28+
// Assuming a single layer halo, and periodic boundary conditions
29+
if(i == 0)
30+
density(i, j, k) = density(nx - 2, j, k);
31+
if(i == nx - 1)
32+
density(i, j, k) = density(1, j, k);
33+
34+
if(j == 0)
35+
density(i, j, k) = density(i, ny - 2, k);
36+
if(j == ny - 1)
37+
density(i, j, k) = density(i, 1, k);
38+
39+
if(k == 0)
40+
density(i, j, k) = density(i, j, nz - 2);
41+
if(k == nz - 1)
42+
density(i, j, k) = density(i, j, 1);
43+
}
44+
}
45+
};
46+
47+
// Kernel to compute the ideal gas equation of state
48+
struct IdealGasKernel
49+
{
50+
template<typename TAcc, typename MdSpan>
51+
ALPAKA_FN_ACC auto operator()(
52+
TAcc const& acc,
53+
MdSpan density,
54+
MdSpan energy,
55+
MdSpan pressure,
56+
MdSpan soundspeed,
57+
float gamma) const -> void
58+
{
59+
auto const i = alpaka::getIdx<alpaka::Grid, alpaka::Threads>(acc)[0];
60+
auto const j = alpaka::getIdx<alpaka::Grid, alpaka::Threads>(acc)[1];
61+
auto const k = alpaka::getIdx<alpaka::Grid, alpaka::Threads>(acc)[2];
62+
63+
if(i < nx && j < ny && k < nz)
64+
{
65+
pressure(i, j, k) = (gamma - 1.0f) * density(i, j, k) * energy(i, j, k);
66+
soundspeed(i, j, k) = sqrt(gamma * pressure(i, j, k) / density(i, j, k));
67+
}
68+
}
69+
};
70+
1571
// Kernel to initialize the simulation variables
1672
struct InitializerKernel
1773
{
@@ -25,19 +81,22 @@ struct InitializerKernel
2581
MdSpan velocityY,
2682
MdSpan velocityZ) const -> void
2783
{
28-
// Get thread index, the center of filter-matrix is positioned to the item on this index.
2984
auto const i = alpaka::getIdx<alpaka::Grid, alpaka::Threads>(acc)[0];
3085
auto const j = alpaka::getIdx<alpaka::Grid, alpaka::Threads>(acc)[1];
3186
auto const k = alpaka::getIdx<alpaka::Grid, alpaka::Threads>(acc)[2];
3287

3388
if(i < nx && j < ny && k < nz)
3489
{
3590
density(i, j, k) = 1.0f; // Initial density
36-
energy(i, j, k) = 1.0f; // Initial energy
91+
energy(i, j, k) = 2.5f; // Initial energy
3792
pressure(i, j, k) = 1.0f; // Initial pressure
38-
velocityX(i, j, k) = 0.0f; // Initial velocity in x direction
39-
velocityY(i, j, k) = 0.0f; // Initial velocity in y direction
40-
velocityZ(i, j, k) = 0.0f; // Initial velocity in z direction
93+
velocityX(i, j, k) = 0.0f; // Initial velocity
94+
95+
if(i < nx && j < ny && k < nz)
96+
{
97+
// Simple advection calculation (this is a simplified example)
98+
density(i, j, k) += (velocityX(i, j, k) + velocityY(i, j, k) + velocityZ(i, j, k)) * 0.01f;
99+
}
41100
}
42101
}
43102
};
@@ -130,6 +189,7 @@ struct AdvectionKernel
130189
}
131190
};
132191

192+
// Kernel for the Lagrangian step
133193
struct LagrangianKernel
134194
{
135195
template<typename TAcc, typename MdSpan>
@@ -162,6 +222,7 @@ struct LagrangianKernel
162222
}
163223
};
164224

225+
// Kernel for viscosity calculations
165226
struct ViscosityKernel
166227
{
167228
template<typename TAcc, typename MdSpan>
@@ -178,9 +239,8 @@ struct ViscosityKernel
178239
auto const j = alpaka::getIdx<alpaka::Grid, alpaka::Threads>(acc)[1];
179240
auto const k = alpaka::getIdx<alpaka::Grid, alpaka::Threads>(acc)[2];
180241

181-
if(i < nx && j < ny && k < nz)
242+
if(i > 0 && i < nx - 1 && j > 0 && j < ny - 1 && k > 0 && k < nz - 1)
182243
{
183-
// Calculate artificial viscosity (this is a simplified example)
184244
float gradVx = (velocityX(i + 1, j, k) - velocityX(i - 1, j, k)) * 0.5f;
185245
float gradVy = (velocityY(i, j + 1, k) - velocityY(i, j - 1, k)) * 0.5f;
186246
float gradVz = (velocityZ(i, j, k + 1) - velocityZ(i, j, k - 1)) * 0.5f;
@@ -193,6 +253,7 @@ struct ViscosityKernel
193253
}
194254
};
195255

256+
// Kernel to find the maximum velocity
196257
struct MaxVelocityKernel
197258
{
198259
template<typename TAcc, typename MdSpan>
@@ -201,7 +262,7 @@ struct MaxVelocityKernel
201262
MdSpan velocityX,
202263
MdSpan velocityY,
203264
MdSpan velocityZ,
204-
float* maxVelocity) const -> void
265+
Data* maxVelocity) const -> void
205266
{
206267
auto const i = alpaka::getIdx<alpaka::Grid, alpaka::Threads>(acc)[0];
207268
auto const j = alpaka::getIdx<alpaka::Grid, alpaka::Threads>(acc)[1];
@@ -215,7 +276,8 @@ struct MaxVelocityKernel
215276
float v = alpaka::math::sqrt(acc, (vx * vx + vy * vy + vz * vz));
216277

217278
// Atomic operation to find the maximum velocity
218-
alpaka::atomicMax(acc, maxVelocity, v);
279+
float val = alpaka::atomicMax(acc, maxVelocity, v);
280+
maxVelocity[0] = val;
219281
}
220282
}
221283
};

0 commit comments

Comments
 (0)