Skip to content

Commit 9d5d850

Browse files
lukasm91havogt
authored andcommitted
communication: fix modulo (#1356)
If `m_coordines[x] == 0` and `X == -1`, we might do `-1 % N`. The sign of the result was implementation-defined pre-stdc++11; in GCC 7.3 this is incorrectly still negative, which leads to problems with MPI later in the code. Backport of #1355
1 parent 803983b commit 9d5d850

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

include/gridtools/communication/low_level/proc_grids_3D.hpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -183,23 +183,23 @@ namespace gridtools {
183183
int _coords[3];
184184

185185
if (m_cyclic.value(0))
186-
_coords[0] = (m_coordinates[0] + I) % m_dimensions[0];
186+
_coords[0] = (m_coordinates[0] + I + m_dimensions[0]) % m_dimensions[0];
187187
else {
188188
_coords[0] = m_coordinates[0] + I;
189189
if (_coords[0] < 0 || _coords[0] >= m_dimensions[0])
190190
return -1;
191191
}
192192

193193
if (m_cyclic.value(1))
194-
_coords[1] = (m_coordinates[1] + J) % m_dimensions[1];
194+
_coords[1] = (m_coordinates[1] + J + m_dimensions[1]) % m_dimensions[1];
195195
else {
196196
_coords[1] = m_coordinates[1] + J;
197197
if (_coords[1] < 0 || _coords[1] >= m_dimensions[1])
198198
return -1;
199199
}
200200

201201
if (m_cyclic.value(2))
202-
_coords[2] = (m_coordinates[2] + K) % m_dimensions[2];
202+
_coords[2] = (m_coordinates[2] + K + m_dimensions[2]) % m_dimensions[2];
203203
else {
204204
_coords[2] = m_coordinates[2] + K;
205205
if (_coords[2] < 0 || _coords[2] >= m_dimensions[2])

0 commit comments

Comments
 (0)