| 1 |
/* |
| 2 |
* SPDX-FileCopyrightText: 2026 Oak Ridge National Laboratory and Contributors |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: Apache-2.0 |
| 5 |
*/ |
| 6 |
|
| 7 |
#include "Settings.h" |
| 8 |
|
| 9 |
#include <errno.h> |
| 10 |
|
| 11 |
#include <cstdlib> |
| 12 |
|
| 13 |
#include <stdexcept> |
| 14 |
|
| 15 |
static unsigned int convertToUint(std::string varName, char *arg) |
| 16 |
{ |
| 17 |
char *end; |
| 18 |
long retval = std::strtol(arg, &end, 10); |
| 19 |
if (end[0] || errno == ERANGE) |
| 20 |
{ |
| 21 |
throw std::invalid_argument("Invalid value given for " + varName + ": " + std::string(arg)); |
| 22 |
} |
| 23 |
if (retval < 0) |
| 24 |
{ |
| 25 |
throw std::invalid_argument("Negative value given for " + varName + ": " + |
| 26 |
std::string(arg)); |
| 27 |
} |
| 28 |
return static_cast<unsigned int>(retval); |
| 29 |
} |
| 30 |
|
| 31 |
Settings::Settings(int argc, char *argv[], int rank, int nproc) : rank{rank} |
| 32 |
{ |
| 33 |
if (argc < 9) |
| 34 |
{ |
| 35 |
throw std::invalid_argument("Not enough arguments"); |
| 36 |
} |
| 37 |
this->nproc = (unsigned int)nproc; |
| 38 |
|
| 39 |
configfile = argv[1]; |
| 40 |
outputfile = argv[2]; |
| 41 |
npx = convertToUint("N", argv[3]); |
| 42 |
npy = convertToUint("M", argv[4]); |
| 43 |
ndx = convertToUint("nx", argv[5]); |
| 44 |
ndy = convertToUint("ny", argv[6]); |
| 45 |
steps = convertToUint("steps", argv[7]); |
| 46 |
iterations = convertToUint("iterations", argv[8]); |
| 47 |
|
| 48 |
if (npx * npy != this->nproc) |
| 49 |
{ |
| 50 |
throw std::invalid_argument("N*M must equal the number of processes"); |
| 51 |
} |
| 52 |
|
| 53 |
// calculate global array size and the local offsets in that global space |
| 54 |
gndx = npx * ndx; |
| 55 |
gndy = npy * ndy; |
| 56 |
posx = rank % npx; |
| 57 |
posy = rank / npx; |
| 58 |
offsx = posx * ndx; |
| 59 |
offsy = posy * ndy; |
| 60 |
|
| 61 |
// determine neighbors |
| 62 |
if (posx == 0) |
| 63 |
rank_up = -1; |
| 64 |
else |
| 65 |
rank_up = rank - 1; |
| 66 |
|
| 67 |
if (posx == npx - 1) |
| 68 |
rank_down = -1; |
| 69 |
else |
| 70 |
rank_down = rank + 1; |
| 71 |
|
| 72 |
if (posy == 0) |
| 73 |
rank_left = -1; |
| 74 |
else |
| 75 |
rank_left = rank - npx; |
| 76 |
|
| 77 |
if (posy == npy - 1) |
| 78 |
rank_right = -1; |
| 79 |
else |
| 80 |
rank_right = rank + npx; |
| 81 |
} |