Skip to content

Commit 668d71a

Browse files
authored
Merge pull request #437 from lanl/jmm/sanitize
Run singularity-eos through clang address sanitizer... and fix the HIP segfault!
2 parents 14bc298 + 596b086 commit 668d71a

26 files changed

+181
-63
lines changed

.github/workflows/sanitizer.yml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
name: Sanitizer
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
jobs:
10+
sanitizer:
11+
name: Run clang sanitizer on minimal code subset
12+
runs-on: ubuntu-latest
13+
14+
steps:
15+
- name: Checkout code
16+
uses: actions/checkout@v3
17+
with:
18+
submodules: recursive
19+
- name: Set system to non-interactive mode
20+
run: export DEBIAN_FRONTEND=noninteractive
21+
- name: install dependencies
22+
run: |
23+
sudo apt-get update -y -qq
24+
sudo apt-get install -y --allow-downgrades --allow-remove-essential --allow-change-held-packages -qq build-essential clang llvm
25+
- name: build and run tests
26+
run: |
27+
mkdir -p bin
28+
cd bin
29+
cmake -DCMAKE_CXX_COMPILER=clang++ \
30+
-DCMAKE_CXX_FLAGS="-fsanitize=address -fsanitize=undefined -fno-omit-frame-pointer" \
31+
-DCMAKE_BUILD_TYPE=Debug \
32+
-DSINGULARITY_STRICT_WARNINGS=ON \
33+
-DSINGULARITY_USE_FORTRAN=OFF \
34+
-DSINGULARITY_BUILD_FORTRAN_BACKEND=ON \
35+
-DSINGULARITY_BUILD_TESTS=ON \
36+
-DSINGULARITY_FORCE_SUBMODULE_MODE=ON \
37+
-DSINGULARITY_USE_KOKKOS=ON \
38+
..
39+
make -j4
40+
ctest --output-on-failure

.github/workflows/warnings.yml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: Warnings
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
jobs:
10+
warnings-gcc:
11+
name: Ensure no warnings from gcc
12+
runs-on: ubuntu-latest
13+
14+
steps:
15+
- name: Checkout code
16+
uses: actions/checkout@v3
17+
with:
18+
submodules: recursive
19+
- name: Set system to non-interactive mode
20+
run: export DEBIAN_FRONTEND=noninteractive
21+
- name: install dependencies
22+
run: |
23+
sudo apt-get update -y -qq
24+
sudo apt-get install -y --allow-downgrades --allow-remove-essential --allow-change-held-packages -qq build-essential
25+
- name: build and run tests
26+
run: |
27+
mkdir -p bin
28+
cd bin
29+
cmake -DCMAKE_BUILD_TYPE=Debug \
30+
-DSINGULARITY_STRICT_WARNINGS=ON \
31+
-DSINGULARITY_USE_FORTRAN=OFF \
32+
-DSINGULARITY_BUILD_FORTRAN_BACKEND=ON \
33+
-DSINGULARITY_BUILD_TESTS=ON \
34+
-DSINGULARITY_FORCE_SUBMODULE_MODE=ON \
35+
-DSINGULARITY_USE_KOKKOS=ON \
36+
..
37+
make -j4

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Added (new features/APIs/variables/...)
66

77
### Fixed (Repair bugs, etc)
8+
- [[OR437]](https://github.com/lanl/singularity-eos/pull/437) Fix segfault on HIP, clean up warnings, add strict sanitizer test
89

910
### Changed (changing behavior/API/variables/...)
1011

CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ cmake_dependent_option(
4646
"SINGULARITY_USE_SPINER" OFF)
4747

4848
option(SINGULARITY_USE_FORTRAN "Enable fortran bindings" ON)
49+
# Optionally build these if you want to build/test the fortran
50+
# infrastructure without invoking a fortran compiler If fortran is
51+
# off, you can set this. If fortran is on, it's forced to ON and is
52+
# not set-able.
53+
cmake_dependent_option(SINGULARITY_BUILD_FORTRAN_BACKEND
54+
"Build the C++ code to which the fortran bindings bind"
55+
OFF "NOT SINGULARITY_USE_FORTRAN" ON)
56+
4957
option(SINGULARITY_USE_KOKKOS "Use Kokkos for portability" OFF)
5058
option(SINGULARITY_USE_EOSPAC "Enable eospac backend" OFF)
5159
option(SINGULARITY_EOSPAC_ENABLE_SHMEM
@@ -95,6 +103,7 @@ cmake_dependent_option(
95103
# modify flags options
96104
option(SINGULARITY_BETTER_DEBUG_FLAGS "Better debug flags for singularity" ON)
97105
option(SINGULARITY_HIDE_MORE_WARNINGS "hide more warnings" OFF)
106+
option(SINGULARITY_STRICT_WARNINGS "Make warnings strict" OFF)
98107

99108
# toggle code options
100109
option(SINGULARITY_USE_TRUE_LOG_GRIDDING
@@ -552,6 +561,10 @@ target_compile_options(
552561
> # release
553562
> # cuda
554563
)
564+
if (SINGULARITY_STRICT_WARNINGS)
565+
target_compile_options(singularity-eos_Interface INTERFACE
566+
-Wall -Werror -Wno-unknown-pragmas)
567+
endif()
555568

556569
if(TARGET singularity-eos_Library)
557570
target_compile_options(singularity-eos_Library PRIVATE ${xlfix})
@@ -590,6 +603,7 @@ endif()
590603

591604
if(SINGULARITY_BUILD_TESTS)
592605
include(CTest)
606+
enable_testing()
593607
add_subdirectory(test)
594608
endif()
595609

doc/sphinx/src/building.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ The main CMake options to configure building are in the following table:
118118
``SINGULARITY_USE_SINGLE_LOGS`` OFF Use single precision logarithms (may degrade accuracy).
119119
``SINGULARITY_NQT_ORDER_1`` OFF For fast logs, use the less accurate but faster 1st-order version.
120120
``SINGULARITY_NQT_PORTABLE`` OFF For fast logs, use the slower but endianness-independent implementation.
121+
``SINGULARITY_STRICT_WARNINGS`` OFF For testing. Adds -Wall and -Werror to builds.
121122
====================================== ======= ===========================================
122123

123124
More options are available to modify only if certain other options or
@@ -159,6 +160,7 @@ preconditions:
159160
``SINGULARITY_TEST_PYTHON`` ``SINGULARITY_BUILD_TESTS=ON`` ``SINGULARITY_BUILD_PYTHON=ON`` Test the Python bindings.
160161
``SINGULARITY_USE_HELMHOLTZ`` ``SINGULARITY_USE_SPINER=ON`` ``SINGULARITY_USE_SPINER_WITH_HDF5=ON`` Use Helmholtz equation of state.
161162
``SINGULARITY_TEST_HELMHOLTZ`` ``SINGULARITY_USE_HELMHOLTZ`` Build Helmholtz equation of state tests.
163+
``SINGULARITY_BUILD_FORTRAN_BACKEND`` ``NOT SINGULARITY_USE_FORTRAN`` For testing, you may build the C++ code to which the fortran bindings bind without building the bindings themselves.
162164
============================================== ================================================================================= ===========================================
163165

164166
When installing ``singularity-eos``, data files are also installed. The

singularity-eos/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,13 @@ if (SINGULARITY_BUILD_CLOSURE)
7373
closure/kinetic_phasetransition_utils.hpp
7474
closure/kinetic_phasetransition_methods.hpp
7575
)
76-
if (SINGULARITY_USE_FORTRAN OR SINGULARITY_BUILD_TESTS)
76+
if (SINGULARITY_BUILD_FORTRAN_BACKEND OR SINGULARITY_BUILD_TESTS)
7777
# while these are C++ files, they
7878
# are only needed for the fortran backend or unit testing
7979
register_srcs(eos/singularity_eos.cpp)
8080
register_headers(eos/singularity_eos.hpp)
8181
endif()
82-
if (SINGULARITY_USE_FORTRAN)
82+
if (SINGULARITY_BUILD_FORTRAN_BACKEND)
8383
register_srcs(eos/get_sg_eos.cpp)
8484
if (SINGULARITY_USE_KOKKOS)
8585
register_srcs(

singularity-eos/base/root-finding-1d/root_finding.hpp

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -70,45 +70,45 @@ enum class Status { SUCCESS = 0, FAIL = 1 };
7070
*/
7171
class RootCounts {
7272
private:
73-
static constexpr int nbins_{15};
73+
static constexpr std::size_t nbins_{15};
7474
mutable Real counts_[nbins_];
7575

7676
public:
7777
PORTABLE_INLINE_FUNCTION
7878
RootCounts() {
79-
for (int i{0}; i < nbins_; ++i)
79+
for (std::size_t i{0}; i < nbins_; ++i)
8080
counts_[i] = 0;
8181
}
8282
PORTABLE_INLINE_FUNCTION void reset() {
83-
for (int i{0}; i < nbins_; ++i)
83+
for (std::size_t i{0}; i < nbins_; ++i)
8484
counts_[i] = 0;
8585
}
86-
PORTABLE_INLINE_FUNCTION void increment(int i) const {
86+
PORTABLE_INLINE_FUNCTION void increment(std::size_t i) const {
8787
assert(i < nbins_ && i >= 0);
8888
#ifdef PORTABILITY_STRATEGY_NONE
8989
counts_[i] += 1;
9090
#endif // PORTABILITY_STRATEGY_NONE
9191
}
9292
PORTABLE_INLINE_FUNCTION Real total() const {
9393
Real tot{1.e-20};
94-
for (int i{0}; i < nbins_; ++i)
94+
for (std::size_t i{0}; i < nbins_; ++i)
9595
tot += counts_[i];
9696
return tot;
9797
}
98-
PORTABLE_INLINE_FUNCTION const Real &operator[](const int i) const {
98+
PORTABLE_INLINE_FUNCTION const Real &operator[](const std::size_t i) const {
9999
assert(i < nbins_ && i >= 0);
100100
return counts_[i];
101101
}
102-
PORTABLE_INLINE_FUNCTION Real &operator[](const int i) {
102+
PORTABLE_INLINE_FUNCTION Real &operator[](const std::size_t i) {
103103
assert(i < nbins_ && i >= 0);
104104
return counts_[i];
105105
}
106106
PORTABLE_INLINE_FUNCTION void print_counts() const {
107-
for (int i{0}; i < nbins_; ++i)
107+
for (std::size_t i{0}; i < nbins_; ++i)
108108
printf("%e\n", counts_[i]);
109109
}
110-
PORTABLE_INLINE_FUNCTION int nBins() const { return nbins_; }
111-
PORTABLE_INLINE_FUNCTION int more() const { return nbins_ - 1; }
110+
PORTABLE_INLINE_FUNCTION std::size_t nBins() const { return nbins_; }
111+
PORTABLE_INLINE_FUNCTION std::size_t more() const { return nbins_ - 1; }
112112
};
113113

114114
PORTABLE_INLINE_FUNCTION bool check_bracket(const Real ya, const Real yb) {
@@ -119,11 +119,11 @@ template <typename T>
119119
PORTABLE_INLINE_FUNCTION bool set_bracket(const T &f, Real &a, const Real guess, Real &b,
120120
Real &ya, const Real yg, Real &yb,
121121
const bool &verbose = false) {
122-
constexpr int max_search_depth = 6;
122+
constexpr std::size_t max_search_depth = 6;
123123
Real dx = b - a;
124-
for (int level = 0; level < max_search_depth; level++) {
125-
const int nlev = (1 << level);
126-
for (int i = 0; i < nlev; i++) {
124+
for (std::size_t level = 0; level < max_search_depth; level++) {
125+
const std::size_t nlev = (1 << level);
126+
for (std::size_t i = 0; i < nlev; i++) {
127127
const Real x = a + (i + 0.5) * dx;
128128
const Real yx = f(x);
129129
if (check_bracket(yx, yg)) {
@@ -158,7 +158,7 @@ PORTABLE_INLINE_FUNCTION Status regula_falsi(const T &f, const Real ytarget,
158158
Real &xroot,
159159
const RootCounts *counts = nullptr,
160160
const bool &verbose = false) {
161-
constexpr int max_iter = SECANT_NITER_MAX;
161+
constexpr std::size_t max_iter = SECANT_NITER_MAX;
162162
auto func = [&](const Real x) { return f(x) - ytarget; };
163163
Real ya = func(a);
164164
Real yg = func(guess);
@@ -187,9 +187,9 @@ PORTABLE_INLINE_FUNCTION Status regula_falsi(const T &f, const Real ytarget,
187187
ya *= sign;
188188
yb *= sign;
189189

190-
int b1 = 0;
191-
int b2 = 0;
192-
int iteration_count = 0;
190+
std::size_t b1 = 0;
191+
std::size_t b2 = 0;
192+
std::size_t iteration_count = 0;
193193
while (b - a > 2.0 * xtol && (std::abs(ya) > ytol || std::abs(yb) > ytol) &&
194194
iteration_count < max_iter) {
195195
Real c = (a * yb - b * ya) / (yb - ya);
@@ -251,15 +251,15 @@ PORTABLE_INLINE_FUNCTION Status newton_raphson(const T &f, const Real ytarget,
251251
const bool &verbose = false,
252252
const bool &fail_on_bound_root = true) {
253253

254-
constexpr int max_iter = NEWTON_RAPHSON_NITER_MAX;
254+
constexpr std::size_t max_iter = NEWTON_RAPHSON_NITER_MAX;
255255
Real _x = guess;
256256
Real _xold = 0.0;
257257
auto status = Status::SUCCESS;
258258

259259
Real yg;
260260
Real dfunc;
261261

262-
int iter;
262+
std::size_t iter;
263263

264264
for (iter = 0; iter < max_iter; iter++) {
265265
std::tie(yg, dfunc) = f(_x); // C++11 tuple unpacking
@@ -383,7 +383,7 @@ PORTABLE_INLINE_FUNCTION Status secant(const T &f, const Real ytarget, const Rea
383383
Real x_last, y, yp, ym, dyNum, dyDen, dy;
384384

385385
Real x = xguess;
386-
unsigned int iter{0};
386+
std::size_t iter{0};
387387
for (iter = 0; iter < SECANT_NITER_MAX; ++iter) {
388388
x_last = x;
389389
dx = fabs(1.e-7 * x) + xtol;
@@ -490,7 +490,7 @@ PORTABLE_INLINE_FUNCTION Status bisect(const T &f, const Real ytarget, const Rea
490490
x += 2. * xtol;
491491
}
492492
// do { // Try to find reasonable region for bisection
493-
for (int i{0}; i < BISECT_REG_MAX; ++i) {
493+
for (std::size_t i{0}; i < BISECT_REG_MAX; ++i) {
494494
dx = fabs(grow * x);
495495
xl = x - dx;
496496
xr = x + dx;
@@ -547,10 +547,10 @@ PORTABLE_INLINE_FUNCTION Status bisect(const T &f, const Real ytarget, const Rea
547547
"\til = %.10e\n"
548548
"\tir = %.10e\n",
549549
xguess, ytarget, xl, xr, fl, fr, il, ir);
550-
int nx = 300;
550+
std::size_t nx = 300;
551551
Real dx = (xmax - xmin) / (nx - 1);
552552
fprintf(stderr, "Area map:\nx\ty\n");
553-
for (int i = 0; i < nx; i++) {
553+
for (std::size_t i = 0; i < nx; i++) {
554554
fprintf(stderr, "%.4f\t%.4e\n", x + i * dx, f(x + i * dx));
555555
}
556556
#endif
@@ -559,7 +559,7 @@ PORTABLE_INLINE_FUNCTION Status bisect(const T &f, const Real ytarget, const Rea
559559
}
560560
}
561561

562-
for (int i{0}; i < BISECT_NITER_MAX; ++i) {
562+
for (std::size_t i{0}; i < BISECT_NITER_MAX; ++i) {
563563
Real xm = 0.5 * (xl + xr);
564564
Real fm = f(xm) - ytarget;
565565
if (fl * fm <= 0) {

singularity-eos/closure/mixed_cell_models.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,9 @@ class PTESolverBase {
209209
const RealIndexer &vfrac_, const RealIndexer &sie_,
210210
const RealIndexer &temp_, const RealIndexer &press_, Real *&scratch,
211211
Real Tnorm, const MixParams &params = MixParams())
212-
: nmat(nmats), neq(neqs), niter(0), eos(eos_), vfrac_total(vfrac_tot),
213-
sie_total(sie_tot), rho(rho_), vfrac(vfrac_), sie(sie_), temp(temp_),
214-
press(press_), Tnorm(Tnorm), params_(params) {
212+
: params_(params), nmat(nmats), neq(neqs), niter(0), vfrac_total(vfrac_tot),
213+
sie_total(sie_tot), eos(eos_), rho(rho_), vfrac(vfrac_), sie(sie_), temp(temp_),
214+
press(press_), Tnorm(Tnorm) {
215215
jacobian = AssignIncrement(scratch, neq * neq);
216216
dx = AssignIncrement(scratch, neq);
217217
sol_scratch = AssignIncrement(scratch, 2 * neq);

singularity-eos/eos/eos_base.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,18 @@ constexpr std::size_t MAX_NUM_CHARS = 121;
4141
// Cuda doesn't have strcat, so we implement it ourselves
4242
PORTABLE_FORCEINLINE_FUNCTION
4343
char *StrCat(char *destination, const char *source) {
44-
int i, j; // not in loops because they're re-used.
44+
std::size_t i, j; // not in loops because they're re-used.
4545

4646
// specifically avoid strlen, which isn't on GPU
4747
for (i = 0; destination[i] != '\0'; i++) {
4848
}
4949
// assumes destination has enough memory allocated
5050
for (j = 0; source[j] != '\0'; j++) {
5151
// MAX_NUM_CHARS-1 to leave room for null terminator
52-
PORTABLE_REQUIRE((i + j) < MAX_NUM_CHARS - 1,
52+
std::size_t ipj = i + j;
53+
PORTABLE_REQUIRE(ipj < MAX_NUM_CHARS - 1,
5354
"Concat string must be within allowed size");
54-
destination[i + j] = source[j];
55+
destination[ipj] = source[j];
5556
}
5657
// null terminate destination string
5758
destination[i + j] = '\0';

singularity-eos/eos/eos_gruneisen.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -359,14 +359,14 @@ PORTABLE_INLINE_FUNCTION Real Gruneisen::PressureFromDensityInternalEnergy(
359359
template <typename Indexer_t>
360360
PORTABLE_INLINE_FUNCTION Real
361361
Gruneisen::MinInternalEnergyFromDensity(const Real rho_in, Indexer_t &&lambda) const {
362-
const Real rho = std::min(rho_in, _rho_max);
362+
// const Real rho = std::min(rho_in, _rho_max);
363363
MinInternalEnergyIsNotEnabled("Gruneisen");
364364
return 0.0;
365365
}
366366
template <typename Indexer_t>
367367
PORTABLE_INLINE_FUNCTION Real Gruneisen::EntropyFromDensityInternalEnergy(
368368
const Real rho_in, const Real sie, Indexer_t &&lambda) const {
369-
const Real rho = std::min(rho_in, _rho_max);
369+
// const Real rho = std::min(rho_in, _rho_max);
370370
EntropyIsNotEnabled("Gruneisen");
371371
return 1.0;
372372
}

0 commit comments

Comments
 (0)