WSL/SLF GitLab Repository

Commit 14ac27f9 authored by Mathias Bavay's avatar Mathias Bavay
Browse files

A new generic method "getArraySliceParams" has been added to IOUtils. This...

A new generic method "getArraySliceParams" has been added to IOUtils. This method computes the indices for splitting an array in several, balanced sub-arrays. This is what is needed to distribute a computation on an array among several computers. This method computes the indices in one dimension, if the user wants to split along several dimensions, he can just call it again on another dimension (the splitting being usually only along one dimension).
parent 9e92bae3
......@@ -601,4 +601,24 @@ std::string IOUtils::printFractionalDay(const double& fractional) {
return tmp.str();
}
void IOUtils::getArraySliceParams(const unsigned int& dimx, const unsigned int& nbworkers, const unsigned int &wk, unsigned int& startx, unsigned int& nx)
{
if(nbworkers>dimx) {
std::stringstream ss;
ss << "Can not split " << dimx << " columns in " << nbworkers << " bands!";
throw InvalidArgumentException(ss.str(), AT);
}
const unsigned int nx_avg = dimx / nbworkers;
const unsigned int remainder = dimx % nbworkers;
if(wk<=remainder) { //distribute remainder, 1 extra column per worker, on first workers
nx = nx_avg+1;
startx = (wk-1)*nx;
} else { //all remainder has been distributed, we now attribute a normal number of columns
nx = nx_avg;
startx = remainder*(nx+1) + (wk-1-remainder)*nx;
}
}
} //namespace
......@@ -308,6 +308,18 @@ namespace IOUtils {
*/
std::string printFractionalDay(const double& fractional);
/**
* @brief Returns the parameters for splitting an array in several, balanced sub-arrays.
* This is mostly usefull for parallel calculations, where an array will be split and sent to different
* workers.
* @param[in] dimx number of cells in the desired dimension
* @param[in] nbworkers total number of slices
* @param[in] wk current slice index
* @param[out] startx calculated start index for the current slice
* @param[out] nx calculated number of cells (in the desired dimension) of the current slice
*/
void getArraySliceParams(const unsigned int& dimx, const unsigned int& nbworkers, const unsigned int &wk, unsigned int& startx, unsigned int& nx);
} //end namespace IOUtils
} //end namespace mio
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment