commit 722272532c4d654562730ce686b88e90441ea6f7
parent 226cb3f14e393e9186ecb67d85b20a6e6e0d9035
Author: Eduardo Fontana Lazzari <edufonlaz@gmail.com>
Date: Tue, 21 Oct 2025 17:22:21 +0200
Refine error handling in the realizations loop
Previously, if a call to the realization function returned an error, all
subsequent realizations were skipped and no results were written. This
behavior could be counterproductive and lead to unnecessary data loss,
especially when errors arise from isolated edge cases while other
realizations remain valid.
This commit introduces a more robust error-handling strategy. A
dedicated error code (RES_UNKNOWN_ERR) is now reserved for fatal errors.
Calls to the realization function that return any other error code are
skipped, allowing the loop to continue processing remaining
realizations.
If no fatal error occurs, results are written regardless. A counter
tracks the number of rejected realizations, which can be used later to
assess the overall validity of the simulation.
Diffstat:
1 file changed, 19 insertions(+), 4 deletions(-)
diff --git a/src/sphor_compute_mvrea.c b/src/sphor_compute_mvrea.c
@@ -542,8 +542,10 @@ sphor_compute_MVREA
(struct sphor* sphor)
{
res_T res = RES_OK;
- size_t nthreads, samples;
- size_t i; /* iterator */
+ size_t nthreads = 0;
+ size_t samples = 0;
+ size_t nfailures = 0;
+ size_t i = 0; /* iterator */
struct ssp_rng_proxy *rng_proxy = NULL;
struct ssp_rng **rngs = NULL;
struct darray_accum accums;
@@ -595,17 +597,30 @@ sphor_compute_MVREA
omp_set_num_threads((int)nthreads);
/* Realizations loop */
-#pragma omp parallel for schedule(static)
+ nfailures = 0;
+ #pragma omp parallel for schedule(static)
for(i=0; i<samples; i++) {
const int ithread = omp_get_thread_num();
res_T res_local = RES_OK;
+ /* Ignore the rest of the for loop if there is a fatal (not failure) error
+ * in the previous realizations */
if (RES_OK != res) continue;
res_local = compute_MVREA_realization
(sphor, rngs[ithread], &accum2id, &accums_threads[ithread]);
if (RES_OK != res_local) {
- res = res_local;
+ /*Protect res and nfailures from concurrent write accesses*/
+ #pragma omp critical
+ switch(res_local) {
+ /* Fatal, all remaining realizations will be skipped */
+ case RES_UNKNOWN_ERR:
+ res = res_local;
+ break;
+ default:
+ nfailures += 1;
+ break;
+ }
}
}
if (res != RES_OK) { goto error; }