Repository navigation
r.sim.water: fix flaky buffer overflow and OpenMP thread-safety issues #7521
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -185,6 +185,8 @@ void main_loop(const Setup *setup, const Geometry *geometry, | |||||||||||
| printf(" nwalka,nwalk=%d %d", sim->nwalka, | ||||||||||||
| sim->nwalk); | ||||||||||||
| G_debug(2, " "); | ||||||||||||
|
|
||||||||||||
| continue; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| if (grids->zz[k][l] != UNDEF) { | ||||||||||||
|
|
@@ -195,22 +197,30 @@ void main_loop(const Setup *setup, const Geometry *geometry, | |||||||||||
| double decr = pow( | ||||||||||||
| addac * sim->w[lw].m, | ||||||||||||
| 3. / 5.); /* decreasing factor in m */ | ||||||||||||
| if (grids->inf[k][l] > decr) { | ||||||||||||
| grids->inf[k][l] -= | ||||||||||||
| decr; /* decrease infilt. in cell | ||||||||||||
| and eliminate the walker */ | ||||||||||||
| sim->w[lw].m = 0.; | ||||||||||||
| } | ||||||||||||
| else { | ||||||||||||
| sim->w[lw].m -= | ||||||||||||
| pow(grids->inf[k][l], 5. / 3.) / | ||||||||||||
| addac; /* use just proportional part | ||||||||||||
| of the walker weight */ | ||||||||||||
| grids->inf[k][l] = 0.; | ||||||||||||
| #if defined(_OPENMP) | ||||||||||||
| #pragma omp critical | ||||||||||||
| #endif | ||||||||||||
| { | ||||||||||||
| if (grids->inf[k][l] > decr) { | ||||||||||||
| grids->inf[k][l] -= | ||||||||||||
| decr; /* decrease infilt. in cell | ||||||||||||
| and eliminate the walker */ | ||||||||||||
|
Comment on lines
+206
to
+207
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [pre-commit] reported by reviewdog 🐶
Suggested change
|
||||||||||||
| sim->w[lw].m = 0.; | ||||||||||||
| } | ||||||||||||
| else { | ||||||||||||
| sim->w[lw].m -= | ||||||||||||
| pow(grids->inf[k][l], 5. / 3.) / | ||||||||||||
| addac; /* use just proportional part | ||||||||||||
| of the walker weight */ | ||||||||||||
|
Comment on lines
+213
to
+214
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [pre-commit] reported by reviewdog 🐶
Suggested change
|
||||||||||||
| grids->inf[k][l] = 0.; | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| #if defined(_OPENMP) | ||||||||||||
| #pragma omp atomic | ||||||||||||
| #endif | ||||||||||||
| grids->gama[k][l] += | ||||||||||||
| (addac * sim->w[lw].m); /* add walker weigh to | ||||||||||||
| water depth or conc. */ | ||||||||||||
|
|
||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -7,9 +7,41 @@ | |||||||
| #include <grass/bitmap.h> | ||||||||
| #include <grass/linkm.h> | ||||||||
|
|
||||||||
| #if defined(_OPENMP) | ||||||||
| #include <omp.h> | ||||||||
| #define MAX_SIMWE_THREADS 1024 | ||||||||
| static unsigned int thread_seeds[MAX_SIMWE_THREADS]; | ||||||||
| static int seeds_initialized = 0; | ||||||||
| #endif | ||||||||
|
|
||||||||
| void simwe_rand_init(long global_seed) | ||||||||
| { | ||||||||
| #if defined(_OPENMP) | ||||||||
| int i; | ||||||||
| int max_threads = omp_get_max_threads(); | ||||||||
| if (max_threads > MAX_SIMWE_THREADS) max_threads = MAX_SIMWE_THREADS; | ||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [pre-commit] reported by reviewdog 🐶
Suggested change
|
||||||||
| for (i = 0; i < max_threads; i++) { | ||||||||
| thread_seeds[i] = (unsigned int)(global_seed + i * 314159); | ||||||||
| } | ||||||||
| seeds_initialized = 1; | ||||||||
| #endif | ||||||||
| } | ||||||||
|
|
||||||||
| double simwe_rand(void) | ||||||||
| { | ||||||||
| #if defined(_OPENMP) | ||||||||
| int tid = omp_get_thread_num(); | ||||||||
| if (tid >= MAX_SIMWE_THREADS) tid = MAX_SIMWE_THREADS - 1; | ||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [pre-commit] reported by reviewdog 🐶
Suggested change
|
||||||||
|
|
||||||||
| if (!seeds_initialized) { | ||||||||
| thread_seeds[tid] = 12345 + tid; // Fallback | ||||||||
| } | ||||||||
|
|
||||||||
| thread_seeds[tid] = (thread_seeds[tid] * 1103515245 + 12345) & 0x7fffffff; | ||||||||
| return (double)thread_seeds[tid] / 2147483648.0; | ||||||||
| #else | ||||||||
| return G_drand48(); | ||||||||
| #endif | ||||||||
| } /* ulec */ | ||||||||
|
|
||||||||
| double gasdev(void) | ||||||||
|
|
||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[pre-commit] reported by reviewdog 🐶