| 1 |
/* |
| 2 |
* SPDX-FileCopyrightText: 2026 Oak Ridge National Laboratory and Contributors |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: Apache-2.0 |
| 5 |
*/ |
| 6 |
|
| 7 |
#include "ReadSettings.h" |
| 8 |
|
| 9 |
#include <cstdlib> |
| 10 |
#include <errno.h> |
| 11 |
#include <iomanip> |
| 12 |
#include <iostream> |
| 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 |
ReadSettings::ReadSettings(int argc, char *argv[], int rank, int nproc) : rank{rank} |
| 32 |
{ |
| 33 |
if (argc < 6) |
| 34 |
{ |
| 35 |
throw std::invalid_argument("Not enough arguments"); |
| 36 |
} |
| 37 |
this->nproc = (unsigned int)nproc; |
| 38 |
|
| 39 |
configfile = argv[1]; |
| 40 |
inputfile = argv[2]; |
| 41 |
outputfile = argv[3]; |
| 42 |
npx = convertToUint("N", argv[4]); |
| 43 |
npy = convertToUint("M", argv[5]); |
| 44 |
|
| 45 |
if (npx * npy != static_cast<unsigned int>(this->nproc)) |
| 46 |
{ |
| 47 |
throw std::invalid_argument("N*M must equal the number of processes"); |
| 48 |
} |
| 49 |
posx = rank % npx; |
| 50 |
posy = rank / npx; |
| 51 |
} |
| 52 |
|
| 53 |
void ReadSettings::DecomposeArray(int gndx, int gndy) |
| 54 |
{ |
| 55 |
// 2D decomposition of global array reading |
| 56 |
size_t ndx = gndx / npx; |
| 57 |
size_t ndy = gndy / npy; |
| 58 |
size_t offsx = ndx * posx; |
| 59 |
size_t offsy = ndy * posy; |
| 60 |
if (posx == npx - 1) |
| 61 |
{ |
| 62 |
// right-most processes need to read all the rest of rows |
| 63 |
ndx = gndx - ndx * (npx - 1); |
| 64 |
} |
| 65 |
|
| 66 |
if (posy == npy - 1) |
| 67 |
{ |
| 68 |
// bottom processes need to read all the rest of columns |
| 69 |
ndy = gndy - ndy * (npy - 1); |
| 70 |
} |
| 71 |
readsize.push_back(ndx); |
| 72 |
readsize.push_back(ndy); |
| 73 |
offset.push_back(offsx); |
| 74 |
offset.push_back(offsy); |
| 75 |
|
| 76 |
std::cout << "rank " << rank << " reads 2D slice " << ndx << " x " << ndy << " from offset (" |
| 77 |
<< offsx << "," << offsy << ")" << std::endl; |
| 78 |
} |