1 #ifndef MPI_GridEnvironment_H
2 #define MPI_GridEnvironment_H
5 #include "MPI_Environment.h"
8 #include "CORE_OptionsList.h"
11 #include "functions_array.h"
12 #include "functions_grid.h"
49 std::array<tMPICoreId,N> mGridSize;
52 std::array<tBoolean,N> mGridPeriodicity;
55 std::array<tMPICoreId,N> mIndices;
114 mem+=
sizeof(tMPICoreId)*mGridSize.size();
115 mem+=
sizeof(tBoolean)*mGridPeriodicity.size();
116 mem+=
sizeof(tMPICoreId)*mIndices.size();
129 inline static CORE_UniquePointer<SelfClass>
New(
const std::array<tMPICoreId,N>& nCoresPerDirection,
130 int& argc,
char * argv[],
const tBoolean& isOpenMPEnabled) {
132 CORE_UniquePointer<SelfClass> p(
new SelfClass(argc,argv,isOpenMPEnabled),
138 std::array<tMPIBoolean,N> isPeriodicPerDirection;
139 for(
auto& p: isPeriodicPerDirection) p=
false;
144 std::vector<tBoolean> Vs;
145 functions_array::parse(gridP,Vs);
146 for(
int i=Vs.size();i<N;i++) {
149 auto iVs=Vs.cbegin();
150 for(
auto& p:isPeriodicPerDirection) {
156 MPI_Cart_create(MPI_COMM_WORLD,
157 (
int) N,nCoresPerDirection.data(),isPeriodicPerDirection.data(),
161 if (p->getWorld()==MPI_COMM_NULL) {
190 const Q* iNs=Ns.data();
191 for(
auto& Nk:mGridSize) {
210 const Q* iIs=indices.data();
211 for(
auto& Ik:mIndices) {
247 inline static tMPIError
ComputeGridSize(
const tMPICoreId& nAvailableCores,std::array<tMPICoreId,N>& coresNumber) {
251 for(
const auto& Ck:coresNumber) nGrids*=Ck;
255 return MPI_Dims_create(nAvailableCores,N,coresNumber.data());
258 return MPI_Dims_create(nGrids,N,coresNumber.data());
277 inline static tBoolean
ComputeGridSize(
const std::array<tReal,N>& Hs,
const std::array<tInteger,N>& Ns,
278 const tMPICoreId& nCores,std::array<tMPICoreId,N>& Gs){
282 std::array<tReal,N> invLs;
283 for(k=0;k<N;k++) invLs[k]=1./(Hs[k]*Ns[k]);
286 std::valarray<tMPICoreId> divisors(nCores);
290 tMPICoreId *iDivisors=&divisors[0];
291 for (tMPICoreId i=1;i<=nCores;i++){
298 tMPICoreId nDivisors=(iDivisors-&divisors[0]);
301 tReal surfaceVolume=0.0;
302 tReal minSurfaceVolume=functions_numeric::getInfinity<tReal>();
305 std::array<tMPICoreId,N> GSample;
306 tMPICoreId *iGSample=GSample.data();
307 const tMPICoreId *eGSample=iGSample;eGSample+=N;
310 std::array<tInteger,N> GNSamples;
311 for(
auto& GNS:GNSamples) GNS=nDivisors;
314 std::array<tInteger,N> GIndices;
315 const tInteger *iGIndices=GIndices.data();;
317 memset(GIndices.data(),0,N*
sizeof(tInteger));
322 iGIndices=GIndices.data();
323 iGSample=GSample.data();
324 while (iGSample!=eGSample) {
325 (*iGSample)=divisors[(*iGIndices)];
331 if (functions_array::product(GSample)==nCores) {
333 surfaceVolume=2.0*functions_array::scalarProduct(invLs,GSample);
335 if (surfaceVolume <minSurfaceVolume) {
337 minSurfaceVolume=surfaceVolume;
344 }
while (functions_grid::hasNextElement(GNSamples,GIndices));
357 return mGridPeriodicity;
364 return mGridPeriodicity[k];
374 const Q* iTs=Ts.data();
375 for(
auto& Tk:mGridPeriodicity) {
376 Tk=(tBoolean) (*iTs);
393 std::array<tMPICoreId,N> nGrids;
394 std::array<tMPIBoolean,N> periodicity;
395 std::array<tMPICoreId,N> indices;
401 MPI_Cart_get(this->
getWorld(),N,nGrids.data(),periodicity.data(),indices.data());
419 inline tMPIError
getCoreIdAtIndices(
const std::array<tMPICoreId,N>& indices,tMPICoreId& coreId)
const {
420 return MPI_Cart_rank(this->
getWorld(),indices.data(),&coreId);
428 inline tMPIError
getCoreIndices(
const tMPICoreId& coreId,std::array<tMPICoreId,N>& indices)
const {
429 return MPI_Cart_coords(this->
getWorld(),coreId,N,indices.data());
class Free introduced for deleting a smart pointer
Definition: CORE_Object.h:113
static tBoolean ReadOption(int nArgs, char *argv[], const tBoolean &isCaseSensitive, const tString &optionName, tString &optionValue)
read the only the option with optionName in command line
Definition: CORE_OptionsList.cpp:260
This class is a Environment class to define MPI world.
Definition: MPI_Environment.h:36
const tMPICoreId & getCoresNumber() const
get the number of cores of this environment of common environment
Definition: MPI_Environment.h:180
void setIsRootEnvironment(const tBoolean &isRoot)
set true when the environemnt created is a root one but not a common world
Definition: MPI_Environment.h:156
const tMPIComm & getWorld() const
get the world of the environment for reading
Definition: MPI_Environment.h:165
virtual tMemSize getContentsMemorySize() const override
return the memory size of the included associations
Definition: MPI_Environment.h:148
virtual void initialize()
initialize the environment Sets the cores number and the core id of the environment
Definition: MPI_Environment.h:103
This class is a grid Environment of templated dimension N=1,2,3.
Definition: MPI_GridEnvironment.h:34
const std::array< tMPICoreId, N > & getGridSize() const
get the grids number
Definition: MPI_GridEnvironment.h:200
virtual ~MPI_GridEnvironment(void)
destroy
Definition: MPI_GridEnvironment.h:88
tMPIError getCoreIndices(const tMPICoreId &coreId, std::array< tMPICoreId, N > &indices) const
get the indices of the core with coreId
Definition: MPI_GridEnvironment.h:428
tMPIError getCoreIdAtIndices(const std::array< tMPICoreId, N > &indices, tMPICoreId &coreId) const
the core id within the grid at indices
Definition: MPI_GridEnvironment.h:419
const std::array< tBoolean, N > & getGridPeriodicity() const
return the periodicity of the grid
Definition: MPI_GridEnvironment.h:356
const std::array< tMPICoreId, N > & getIndices() const
get the indices of the current core within the grid environment
Definition: MPI_GridEnvironment.h:219
static tBoolean ComputeGridSize(const std::array< tReal, N > &Hs, const std::array< tInteger, N > &Ns, const tMPICoreId &nCores, std::array< tMPICoreId, N > &Gs)
compute the grid size in order to minimize the ratio S/V where S is the surface of 1 cpu block and V ...
Definition: MPI_GridEnvironment.h:277
void setIndices(const std::array< Q, N > &indices)
set the indices of the current core
Definition: MPI_GridEnvironment.h:209
static tMPIError ComputeGridSize(const MPI_Environment &parent, std::array< tMPICoreId, N > &coresNumber)
get the cores number per direction if possible
Definition: MPI_GridEnvironment.h:238
void setGridPeriodicity(const std::array< Q, N > &Ts)
set grid periodicity
Definition: MPI_GridEnvironment.h:373
void setGridSize(const std::array< Q, N > &Ns)
get the grids number
Definition: MPI_GridEnvironment.h:189
MPI_GridEnvironment()
create a root environment
Definition: MPI_GridEnvironment.h:64
static CORE_UniquePointer< SelfClass > New(const std::array< tMPICoreId, N > &nCoresPerDirection, int &argc, char *argv[], const tBoolean &isOpenMPEnabled)
create a new cart environment
Definition: MPI_GridEnvironment.h:129
virtual void initialize()
initialize the environment
Definition: MPI_GridEnvironment.h:388
MPI_GridEnvironment(int &argc, char *argv[], const tBoolean &isOpenMPEnabled)
create a root environment
Definition: MPI_GridEnvironment.h:79
virtual tMemSize getMemorySize() const override
return the memory size of the class
Definition: MPI_GridEnvironment.h:100
virtual tMemSize getContentsMemorySize() const override
return the memory size of the included associations
Definition: MPI_GridEnvironment.h:112
static tMPIError ComputeGridSize(const tMPICoreId &nAvailableCores, std::array< tMPICoreId, N > &coresNumber)
get the cores number per direction if possible
Definition: MPI_GridEnvironment.h:247
const tBoolean & isPeriodic(const tUCInt &k) const
return true if the grid is periodic in direction k
Definition: MPI_GridEnvironment.h:363