Skip to content

Commit 6fca533

Browse files
committed
TEMP: reducers, initial grid changes, no vector
1 parent 79bfa0d commit 6fca533

File tree

7 files changed

+75
-27
lines changed

7 files changed

+75
-27
lines changed

cpp/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ FetchContent_Declare(
4040
option(EVE_BUILD_TEST "Build EVE tests" OFF)
4141
FetchContent_MakeAvailable(argparse mdspan cnpy spdlog eve)
4242
target_include_directories(cnpy PUBLIC ${CNPY_SOURCE_DIR})
43+
#target_include_directories(cnpy-static PUBLIC ${CNPY_SOURCE_DIR})
4344

4445
# ==========================
4546
# Project code
@@ -55,10 +56,21 @@ if (ENABLE_AVX512)
5556
endif ()
5657

5758
option(ENABLE_CILK "Enable Cilk parallelism" ON)
59+
option(ENABLE_CILKSAN "Enable Cilk Sanitizer" OFF)
60+
option(ENABLE_CILKSAN_WORKAROUND "Enable beta feature" OFF)
5861
if (ENABLE_CILK)
5962
add_compile_options("-fopencilk")
6063
add_link_options("-fopencilk")
6164
add_compile_definitions(CILK_ENABLED)
65+
if (ENABLE_CILKSAN)
66+
add_compile_options("-fsanitize=cilk")
67+
add_link_options("-fsanitize=cilk")
68+
add_compile_definitions(CILKSAN_ENABLED)
69+
if (ENABLE_CILKSAN_WORKAROUND)
70+
add_compile_options("-mllvm" "-cilksan-bc-path=/opt/OpenCilk-2.0.0/lib/clang/14.0.6/lib/x86_64-unknown-linux-gnu/libcilksan.bc")
71+
add_link_options("-mllvm" "-cilksan-bc-path=/opt/OpenCilk-2.0.0/lib/clang/14.0.6/lib/x86_64-unknown-linux-gnu/libcilksan.bc")
72+
endif ()
73+
endif ()
6274
endif ()
6375

6476
# TODO: cleanup (currently just to test formatting)

cpp/lib/Brackets.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@
55
namespace ahr {
66

77
class Transformer;
8-
class HouLiFilterCached1DVector;
8+
class HouLiFilterCached1D;
99

1010
class Brackets {
1111
public:
12-
Brackets(Grid const &grid, Transformer const &tf, HouLiFilterCached1DVector const &hlFilter)
12+
Brackets(Grid const &grid, Transformer const &tf, HouLiFilterCached1D const &hlFilter)
1313
: grid(grid), tf(tf), hlFilter(hlFilter) {}
1414

1515
private:
1616
Grid const &grid;
1717
Transformer const &tf;
18-
HouLiFilterCached1DVector const &hlFilter; // TODO vectorized
19-
PrepareDerivativesVector prepareDXY{grid};
18+
HouLiFilterCached1D const &hlFilter; // TODO vectorized
19+
PrepareDerivatives prepareDXY{grid};
2020

2121
using View = Grid::View;
2222
using Buf = Grid::Buf;

cpp/lib/Naive.cpp

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "Naive.hpp"
22
#include "equillibrium.hpp"
3+
#include "reducer.hpp"
34

45
#include <cnpy.h>
56
#include <cstdlib>
@@ -255,7 +256,7 @@ void Naive::run(Dim N, Dim saveInterval) {
255256

256257
/// f_pred from Viriato
257258
auto GM_Nonlinear_K_Loop = g.cBufMXY();
258-
Real sumAParRelError = 0;
259+
SumReducer<Real> sumAParRelError = 0;
259260
g.for_each_kxky([&](Dim kx, Dim ky) {
260261
GM_Nonlinear_K_Loop(kx, ky, A_PAR) = nonlinear::A(
261262
bracketAParPhiG2Ne_K_Loop(kx, ky), bracketUEParPhi_K_Loop(kx, ky), g.kPerp2(kx, ky));
@@ -493,8 +494,7 @@ Naive::Buf::R_XY Naive::getMoment(Dim m) const {
493494
Real Naive::getTimestep(DxDy<View::R_XY> dPhi, DxDy<View::R_XY> dNE, DxDy<View::R_XY> dAPar) {
494495
// compute flows
495496
DxDy<View::R_XY> ve, b;
496-
Real vyMax{0}, vxMax{0}, bxMax{0}, byMax{0};
497-
bPerpMax = 0;
497+
MaxReducer<Real> vyMax{0}, vxMax{0}, bxMax{0}, byMax{0}, bPerpMaxRed{0};
498498

499499
// Note that this is minus in Viriato, but we don't care because we're taking the absolute value
500500
// anyway.
@@ -504,17 +504,20 @@ Real Naive::getTimestep(DxDy<View::R_XY> dPhi, DxDy<View::R_XY> dNE, DxDy<View::
504504
b.DY = dAPar.DX;
505505

506506
g.for_each_xy([&](Dim x, Dim y) {
507-
bxMax = std::max(bxMax, std::abs(b.DX(x, y)));
508-
byMax = std::max(byMax, std::abs(b.DY(x, y)));
509-
bPerpMax = std::max(bPerpMax, std::sqrt(b.DX(x, y) * b.DX(x, y) + b.DY(x, y) * b.DY(x, y)));
510-
vxMax = std::max(vxMax, std::abs(ve.DX(x, y)));
511-
vyMax = std::max(vyMax, std::abs(ve.DY(x, y)));
507+
bxMax = std::max<Real>(bxMax, std::abs(b.DX(x, y)));
508+
byMax = std::max<Real>(byMax, std::abs(b.DY(x, y)));
509+
bPerpMaxRed =
510+
std::max<Real>(bPerpMaxRed, std::sqrt(b.DX(x, y) * b.DX(x, y) + b.DY(x, y) * b.DY(x, y)));
511+
vxMax = std::max<Real>(vxMax, std::abs(ve.DX(x, y)));
512+
vyMax = std::max<Real>(vyMax, std::abs(ve.DY(x, y)));
512513
if (rhoI >= smallRhoI) {
513-
vxMax = std::max(vxMax, rhoS * rhoS * std::abs(dNE.DX(x, y)));
514-
vyMax = std::max(vyMax, rhoS * rhoS * std::abs(dNE.DY(x, y)));
514+
vxMax = std::max<Real>(vxMax, rhoS * rhoS * std::abs(dNE.DX(x, y)));
515+
vyMax = std::max<Real>(vyMax, rhoS * rhoS * std::abs(dNE.DY(x, y)));
515516
}
516517
});
517518

519+
bPerpMax = bPerpMaxRed;
520+
518521
Real kperpDum2 = std::pow(g.ky_(g.KY / 2), 2) + std::pow(Real(g.KX), 2);
519522
Real omegaKaw;
520523
if (rhoI < smallRhoI) {

cpp/lib/Naive.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class Naive : public ahr::HermiteRunner {
3939
Grid g;
4040
Transformer tf{g};
4141
Exporter exporter{g, tf};
42-
HouLiFilterCached1DVector hlFilter{g};
42+
HouLiFilterCached1D hlFilter{g};
4343
Brackets br{g, tf, hlFilter};
4444

4545
private:

cpp/lib/cilk.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#define cilk_spawn /* empty */
66
#define cilk_sync /* empty */
77
#define cilk_scope /* empty */
8+
#define cilk_reducer(init, reduce) /* empty */
9+
810
#else
911
#include <cilk/cilk.h>
1012
#endif

cpp/lib/grid.hpp

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,20 @@
66

77
#include <fftw-cpp/fftw-cpp.h>
88

9+
#define FOREACH_XY(grid, ...) \
10+
cilk_for (Dim y = 0; y < (grid).Y; ++y) { \
11+
for (Dim x = 0; x < (grid).X; ++x) { \
12+
__VA_ARGS__; \
13+
} \
14+
}
15+
16+
#define FOREACH_KXKY(grid, ...) \
17+
cilk_for (Dim ky = 0; ky < (grid).KY; ++ky) { \
18+
for (Dim kx = 0; kx < (grid).KX; ++kx) { \
19+
__VA_ARGS__; \
20+
} \
21+
}
22+
923
namespace ahr {
1024
namespace stdex = std::experimental;
1125

@@ -103,21 +117,11 @@ struct Grid {
103117
// TODO move iteration to a separate class
104118

105119
/// Iterate in real space
106-
void for_each_xy(std::invocable<Dim, Dim> auto fun) const {
107-
cilk_for (Dim y = 0; y < Y; ++y) {
108-
for (Dim x = 0; x < X; ++x) {
109-
fun(x, y);
110-
}
111-
}
112-
}
120+
void for_each_xy(std::invocable<Dim, Dim> auto fun) const { FOREACH_XY((*this), fun(x, y)); }
113121

114122
/// Iterate in phase space
115123
void for_each_kxky(std::invocable<Dim, Dim> auto fun) const {
116-
cilk_for (Dim ky = 0; ky < KY; ++ky) {
117-
for (Dim kx = 0; kx < KX; ++kx) {
118-
fun(kx, ky);
119-
}
120-
}
124+
FOREACH_KXKY((*this), fun(kx, ky));
121125
}
122126

123127
[[nodiscard]] Real ky_(Dim ky) const {

cpp/lib/reducer.hpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#pragma once
2+
3+
#include "cilk.hpp"
4+
5+
#include <algorithm>
6+
#include <limits>
7+
8+
namespace detail {
9+
template <typename T> void sum_init(void *v) { *reinterpret_cast<T *>(v) = T{}; }
10+
11+
template <typename T> void sum_reduce(void *v, void *v2) {
12+
T *a = reinterpret_cast<T *>(v), *b = reinterpret_cast<T *>(v2);
13+
*a += *b;
14+
}
15+
16+
template <typename T> void max_init(void *v) {
17+
*reinterpret_cast<T *>(v) = std::numeric_limits<T>::min();
18+
}
19+
20+
template <typename T> void max_reduce(void *v, void *v2) {
21+
T *a = reinterpret_cast<T *>(v), *b = reinterpret_cast<T *>(v2);
22+
*a = std::max(*a, *b);
23+
}
24+
} // namespace detail
25+
26+
template <typename T> using SumReducer = T cilk_reducer(detail::sum_init<T>, detail::sum_reduce<T>);
27+
template <typename T> using MaxReducer = T cilk_reducer(detail::max_init<T>, detail::max_reduce<T>);

0 commit comments

Comments
 (0)