Skip to content

Commit 9d63d59

Browse files
Improvements to AMREX_ASSERT (#4581)
## Summary Previously the `AMREX_ASSERT_WITH_MESSAGE` and `AMREX_ALWAYS_ASSERT_WITH_MESSAGE` macros converted the messege to a string using `# MSG` which resulted in strange formatting and the printing of runtime values to not work. Additionally, `ALWAYS_ASSERT` and `ASSERT` did not work in device code even when compiling with `AMREX_USE_ASSERTION` due to the `#if defined(NDEBUG)` in `Assert (...)`. Now `ALWAYS_ASSERT` and `ASSERT` will work in device code with `AMREX_USE_ASSERTION`, but not without so the behaviour doesn't change of the few places that use `ALWAYS_ASSERT` on the GPU (meaning `ALWAYS_ASSERT` still does not always assert). Test of the assert: https://github.com/AMReX-Codes/amrex/blob/5bd9494f9229b6a7a60cbc6f2560e5a831aaa3de/Src/Particle/AMReX_StructOfArrays.H#L122 Dev: ``` 0::Assertion `pos != m_rdata_names->end()' failed, file "/users/sinnalex/hipace/build/_deps/fetchedamrex-src/Src/Particle/AMReX_StructOfArrays.H", line 122, Msg: "Soa Real name='" + name + "' was not found components" !!! SIGABRT See Backtrace.0 file for details ``` PR: ``` 0::Assertion `pos != m_rdata_names->end()' failed, file "/users/sinnalex/amrex/Src/Particle/AMReX_StructOfArrays.H", line 122, Msg: Soa Real name='wrong name' was not found components !!! SIGABRT See Backtrace.0 file for details ```
1 parent 7d82235 commit 9d63d59

File tree

5 files changed

+41
-24
lines changed

5 files changed

+41
-24
lines changed

Src/Base/AMReX.H

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <AMReX_Extension.H>
1212

1313
#include <cstdint>
14+
#include <cstring>
1415
#include <functional>
1516
#include <iostream>
1617
#include <memory>
@@ -188,25 +189,40 @@ namespace amrex
188189
* in <AMReX_BLassert.H>.
189190
*/
190191

191-
void Assert_host (const char* EX, const char* file, int line, const char* msg);
192+
void Assert_host (const char* EX, const char* file, int line, const char* msg,
193+
std::size_t msg_size=0);
192194

193195
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
194-
void Assert (const char* EX, const char* file, int line, const char* msg = nullptr) {
195-
#if defined(NDEBUG)
196+
void Assert (const char* EX, const char* file, int line) {
197+
#if defined(NDEBUG) && !defined(AMREX_USE_ASSERTION)
198+
AMREX_IF_ON_DEVICE((amrex::ignore_unused(EX,file,line);))
199+
#else
200+
AMREX_IF_ON_DEVICE((
201+
AMREX_DEVICE_PRINTF("Assertion `%s' failed, file \"%s\", line %d",
202+
EX, file, line);
203+
AMREX_DEVICE_ASSERT(0);
204+
))
205+
#endif
206+
AMREX_IF_ON_HOST((Assert_host(EX,file,line,nullptr,0U);))
207+
}
208+
209+
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
210+
void Assert (const char* EX, const char* file, int line, const char* msg) {
211+
#if defined(NDEBUG) && !defined(AMREX_USE_ASSERTION)
196212
AMREX_IF_ON_DEVICE((amrex::ignore_unused(EX,file,line,msg);))
197213
#else
198214
AMREX_IF_ON_DEVICE((
199-
if (msg) {
200-
AMREX_DEVICE_PRINTF("Assertion `%s' failed, file \"%s\", line %d, Msg: %s",
201-
EX, file, line, msg);
202-
} else {
203-
AMREX_DEVICE_PRINTF("Assertion `%s' failed, file \"%s\", line %d",
204-
EX, file, line);
205-
}
206-
AMREX_DEVICE_ASSERT(0);
215+
AMREX_DEVICE_PRINTF("Assertion `%s' failed, file \"%s\", line %d, Msg: %s",
216+
EX, file, line, msg);
217+
AMREX_DEVICE_ASSERT(0);
207218
))
208219
#endif
209-
AMREX_IF_ON_HOST((Assert_host(EX,file,line,msg);))
220+
AMREX_IF_ON_HOST((Assert_host(EX,file,line,msg,std::strlen(msg));))
221+
}
222+
223+
AMREX_FORCE_INLINE
224+
void Assert (const char* EX, const char* file, int line, const std::string& msg) {
225+
Assert_host(EX,file,line,msg.c_str(),msg.size());
210226
}
211227

212228
/**

Src/Base/AMReX.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -269,25 +269,26 @@ amrex::Warning_host (const char * msg)
269269
}
270270

271271
void
272-
amrex::Assert_host (const char* EX, const char* file, int line, const char* msg)
272+
amrex::Assert_host (const char* EX, const char* file, int line, const char* msg,
273+
std::size_t msg_size)
273274
{
274275
#ifdef AMREX_USE_COVERITY
275276
amrex_coverity_abort();
276277
#else
277-
const int N = 512;
278+
const std::size_t N = 512 + msg_size;
278279

279-
char buf[N];
280+
std::vector<char> buf(N);
280281

281282
if (msg) {
282-
snprintf(buf,
283+
snprintf(buf.data(),
283284
N,
284285
"Assertion `%s' failed, file \"%s\", line %d, Msg: %s",
285286
EX,
286287
file,
287288
line,
288289
msg);
289290
} else {
290-
snprintf(buf,
291+
snprintf(buf.data(),
291292
N,
292293
"Assertion `%s' failed, file \"%s\", line %d",
293294
EX,
@@ -296,11 +297,11 @@ amrex::Assert_host (const char* EX, const char* file, int line, const char* msg)
296297
}
297298

298299
if (system::error_handler) {
299-
system::error_handler(buf);
300+
system::error_handler(buf.data());
300301
} else if (system::throw_exception) {
301-
throw RuntimeError(buf);
302+
throw RuntimeError(buf.data());
302303
} else {
303-
write_to_stderr_without_buffering(buf);
304+
write_to_stderr_without_buffering(buf.data());
304305
#ifdef AMREX_USE_OMP
305306
#pragma omp critical (amrex_abort_omp_critical)
306307
#endif

Src/Base/AMReX_BLassert.H

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@
4040

4141
#else
4242

43-
#define AMREX_ASSERT_WITH_MESSAGE(EX,MSG) (EX)?((void)0):amrex::Assert( # EX , __FILE__, __LINE__ , # MSG)
43+
#define AMREX_ASSERT_WITH_MESSAGE(EX,MSG) (EX)?((void)0):amrex::Assert( # EX , __FILE__, __LINE__ , MSG)
4444
#define AMREX_ASSERT(EX) (EX)?((void)0):amrex::Assert( # EX , __FILE__, __LINE__)
4545
#define BL_ASSERT(EX) (EX)?((void)0):amrex::Assert( # EX , __FILE__, __LINE__)
4646

4747
#endif
4848

49-
#define AMREX_ALWAYS_ASSERT_WITH_MESSAGE(EX,MSG) (EX)?((void)0):amrex::Assert( # EX , __FILE__, __LINE__ , # MSG)
49+
#define AMREX_ALWAYS_ASSERT_WITH_MESSAGE(EX,MSG) (EX)?((void)0):amrex::Assert( # EX , __FILE__, __LINE__ , MSG)
5050
#define AMREX_ALWAYS_ASSERT(EX) (EX)?((void)0):amrex::Assert( # EX , __FILE__, __LINE__)
5151

5252

Src/Base/AMReX_DistributionMapping.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2044,7 +2044,7 @@ DistributionMapping MakeSimilarDM (const BoxArray& ba, const BoxArray& src_ba,
20442044
const DistributionMapping& src_dm, const IntVect& ng)
20452045
{
20462046
AMREX_ASSERT_WITH_MESSAGE(ba.ixType() == src_ba.ixType(),
2047-
"input BoxArrays must have the same centering.";);
2047+
"input BoxArrays must have the same centering.");
20482048

20492049
Vector<int> pmap(ba.size());
20502050
for (int i = 0; i < static_cast<int>(ba.size()); ++i) {

Src/Base/AMReX_MFIter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ MFIter::Initialize ()
279279
{
280280
++depth;
281281
AMREX_ALWAYS_ASSERT_WITH_MESSAGE(depth == 1 || MFIter::allow_multiple_mfiters,
282-
"Nested or multiple active MFIters is not supported by default. This can be changed by calling MFIter::allowMultipleMFIters(true)".);
282+
"Nested or multiple active MFIters is not supported by default. This can be changed by calling MFIter::allowMultipleMFIters(true)");
283283
}
284284

285285
#if defined(AMREX_USE_GPU) && defined(AMREX_USE_OMP)

0 commit comments

Comments
 (0)