2016-11-01 11:35:43 +00:00
|
|
|
|
2018-01-13 00:17:16 +00:00
|
|
|
/*************************************************************************************
|
2016-01-02 14:51:32 +00:00
|
|
|
|
|
|
|
Grid physics library, www.github.com/paboyle/Grid
|
|
|
|
|
|
|
|
Source file: ./lib/communicator/Communicator_base.h
|
|
|
|
|
|
|
|
Copyright (C) 2015
|
|
|
|
|
|
|
|
Author: Peter Boyle <paboyle@ph.ed.ac.uk>
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along
|
|
|
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
|
|
|
|
See the full license in the file "LICENSE" in the top level distribution directory
|
2018-01-13 00:17:16 +00:00
|
|
|
*************************************************************************************/
|
|
|
|
/* END LEGAL */
|
2015-05-11 19:09:49 +01:00
|
|
|
#ifndef GRID_COMMUNICATOR_BASE_H
|
|
|
|
#define GRID_COMMUNICATOR_BASE_H
|
|
|
|
|
|
|
|
///////////////////////////////////
|
|
|
|
// Processor layout information
|
|
|
|
///////////////////////////////////
|
2018-01-08 11:31:32 +00:00
|
|
|
#include <Grid/communicator/SharedMemory.h>
|
2016-10-24 17:30:43 +01:00
|
|
|
|
2018-01-13 00:16:34 +00:00
|
|
|
NAMESPACE_BEGIN(Grid);
|
2016-10-24 17:30:43 +01:00
|
|
|
|
2018-01-08 11:31:32 +00:00
|
|
|
class CartesianCommunicator : public SharedMemory {
|
2015-05-11 19:09:49 +01:00
|
|
|
|
2018-01-08 11:31:32 +00:00
|
|
|
public:
|
2017-08-20 01:10:50 +01:00
|
|
|
|
|
|
|
////////////////////////////////////////////
|
2018-01-08 11:31:32 +00:00
|
|
|
// Policies
|
2017-08-20 01:10:50 +01:00
|
|
|
////////////////////////////////////////////
|
|
|
|
enum CommunicatorPolicy_t { CommunicatorPolicyConcurrent, CommunicatorPolicySequential };
|
|
|
|
static CommunicatorPolicy_t CommunicatorPolicy;
|
|
|
|
static void SetCommunicatorPolicy(CommunicatorPolicy_t policy ) { CommunicatorPolicy = policy; }
|
2017-07-29 18:06:53 +01:00
|
|
|
static int nCommThreads;
|
2016-10-20 16:58:01 +01:00
|
|
|
|
2018-01-08 11:31:32 +00:00
|
|
|
////////////////////////////////////////////
|
2016-10-24 17:30:43 +01:00
|
|
|
// Communicator should know nothing of the physics grid, only processor grid.
|
2018-01-08 11:31:32 +00:00
|
|
|
////////////////////////////////////////////
|
2016-10-24 17:30:43 +01:00
|
|
|
int _Nprocessors; // How many in all
|
2018-02-24 22:23:18 +00:00
|
|
|
Coordinate _processors; // Which dimensions get relayed out over processors lanes.
|
2016-10-24 17:30:43 +01:00
|
|
|
int _processor; // linear processor rank
|
2018-02-24 22:23:18 +00:00
|
|
|
Coordinate _processor_coor; // linear processor coordinate
|
2018-01-08 11:31:32 +00:00
|
|
|
unsigned long _ndimension;
|
|
|
|
static Grid_MPI_Comm communicator_world;
|
|
|
|
Grid_MPI_Comm communicator;
|
|
|
|
std::vector<Grid_MPI_Comm> communicator_halo;
|
2016-10-24 17:30:43 +01:00
|
|
|
|
|
|
|
////////////////////////////////////////////////
|
|
|
|
// Must call in Grid startup
|
|
|
|
////////////////////////////////////////////////
|
|
|
|
static void Init(int *argc, char ***argv);
|
2017-06-22 08:14:34 +01:00
|
|
|
|
2016-10-24 17:30:43 +01:00
|
|
|
////////////////////////////////////////////////
|
2017-06-22 08:14:34 +01:00
|
|
|
// Constructors to sub-divide a parent communicator
|
|
|
|
// and default to comm world
|
2016-10-24 17:30:43 +01:00
|
|
|
////////////////////////////////////////////////
|
2018-02-24 22:23:18 +00:00
|
|
|
CartesianCommunicator(const Coordinate &processors,const CartesianCommunicator &parent,int &srank);
|
|
|
|
CartesianCommunicator(const Coordinate &pdimensions_in);
|
2017-10-25 10:24:14 +01:00
|
|
|
virtual ~CartesianCommunicator();
|
2017-06-22 08:14:34 +01:00
|
|
|
|
2018-01-13 00:17:16 +00:00
|
|
|
private:
|
2018-01-08 11:31:32 +00:00
|
|
|
|
2017-06-22 08:14:34 +01:00
|
|
|
////////////////////////////////////////////////
|
|
|
|
// Private initialise from an MPI communicator
|
|
|
|
// Can use after an MPI_Comm_split, but hidden from user so private
|
|
|
|
////////////////////////////////////////////////
|
2018-02-24 22:23:18 +00:00
|
|
|
void InitFromMPICommunicator(const Coordinate &processors, Grid_MPI_Comm communicator_base);
|
2018-01-08 11:31:32 +00:00
|
|
|
|
2018-01-13 00:17:16 +00:00
|
|
|
public:
|
2016-10-24 17:30:43 +01:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Wraps MPI_Cart routines, or implements equivalent on other impls
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
void ShiftedRanks(int dim,int shift,int & source, int & dest);
|
2018-02-24 22:23:18 +00:00
|
|
|
int RankFromProcessorCoor(Coordinate &coor);
|
|
|
|
void ProcessorCoorFromRank(int rank,Coordinate &coor);
|
2016-10-24 17:30:43 +01:00
|
|
|
|
2017-05-30 23:39:16 +01:00
|
|
|
int Dimensions(void) ;
|
2016-10-24 17:30:43 +01:00
|
|
|
int IsBoss(void) ;
|
|
|
|
int BossRank(void) ;
|
|
|
|
int ThisRank(void) ;
|
2018-02-24 22:23:18 +00:00
|
|
|
const Coordinate & ThisProcessorCoor(void) ;
|
|
|
|
const Coordinate & ProcessorGrid(void) ;
|
2016-10-24 17:30:43 +01:00
|
|
|
int ProcessorCount(void) ;
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// very VERY rarely (Log, serial RNG) we need world without a grid
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
static int RankWorld(void) ;
|
|
|
|
static void BroadcastWorld(int root,void* data, int bytes);
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// Reduction
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
void GlobalSum(RealF &);
|
|
|
|
void GlobalSumVector(RealF *,int N);
|
|
|
|
void GlobalSum(RealD &);
|
|
|
|
void GlobalSumVector(RealD *,int N);
|
|
|
|
void GlobalSum(uint32_t &);
|
|
|
|
void GlobalSum(uint64_t &);
|
|
|
|
void GlobalSum(ComplexF &c);
|
|
|
|
void GlobalSumVector(ComplexF *c,int N);
|
|
|
|
void GlobalSum(ComplexD &c);
|
|
|
|
void GlobalSumVector(ComplexD *c,int N);
|
2017-06-11 23:14:10 +01:00
|
|
|
void GlobalXOR(uint32_t &);
|
|
|
|
void GlobalXOR(uint64_t &);
|
2016-10-24 17:30:43 +01:00
|
|
|
|
|
|
|
template<class obj> void GlobalSum(obj &o){
|
|
|
|
typedef typename obj::scalar_type scalar_type;
|
|
|
|
int words = sizeof(obj)/sizeof(scalar_type);
|
|
|
|
scalar_type * ptr = (scalar_type *)& o;
|
|
|
|
GlobalSumVector(ptr,words);
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// Face exchange, buffer swap in translational invariant way
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
void SendToRecvFrom(void *xmit,
|
|
|
|
int xmit_to_rank,
|
|
|
|
void *recv,
|
|
|
|
int recv_from_rank,
|
|
|
|
int bytes);
|
|
|
|
|
|
|
|
void SendRecvPacket(void *xmit,
|
|
|
|
void *recv,
|
|
|
|
int xmit_to_rank,
|
|
|
|
int recv_from_rank,
|
|
|
|
int bytes);
|
|
|
|
|
|
|
|
void SendToRecvFromBegin(std::vector<CommsRequest_t> &list,
|
|
|
|
void *xmit,
|
|
|
|
int xmit_to_rank,
|
|
|
|
void *recv,
|
|
|
|
int recv_from_rank,
|
|
|
|
int bytes);
|
|
|
|
|
|
|
|
void SendToRecvFromComplete(std::vector<CommsRequest_t> &waitall);
|
|
|
|
|
2017-07-29 18:06:53 +01:00
|
|
|
double StencilSendToRecvFrom(void *xmit,
|
|
|
|
int xmit_to_rank,
|
|
|
|
void *recv,
|
|
|
|
int recv_from_rank,
|
|
|
|
int bytes,int dir);
|
|
|
|
|
2017-02-20 22:47:40 +00:00
|
|
|
double StencilSendToRecvFromBegin(std::vector<CommsRequest_t> &list,
|
2017-06-24 23:10:24 +01:00
|
|
|
void *xmit,
|
|
|
|
int xmit_to_rank,
|
|
|
|
void *recv,
|
|
|
|
int recv_from_rank,
|
|
|
|
int bytes,int dir);
|
2016-10-24 17:30:43 +01:00
|
|
|
|
|
|
|
|
2017-06-24 23:10:24 +01:00
|
|
|
void StencilSendToRecvFromComplete(std::vector<CommsRequest_t> &waitall,int i);
|
2016-10-24 17:30:43 +01:00
|
|
|
void StencilBarrier(void);
|
2015-05-11 19:09:49 +01:00
|
|
|
|
2016-10-24 17:30:43 +01:00
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// Barrier
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
void Barrier(void);
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// Broadcast a buffer and composite larger
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
void Broadcast(int root,void* data, int bytes);
|
2017-10-09 23:16:51 +01:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// All2All down one dimension
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
template<class T> void AllToAll(int dim,std::vector<T> &in, std::vector<T> &out){
|
|
|
|
assert(dim>=0);
|
|
|
|
assert(dim<_ndimension);
|
|
|
|
assert(in.size()==out.size());
|
2018-01-08 14:06:53 +00:00
|
|
|
int numnode = _processors[dim];
|
2017-10-27 14:20:35 +01:00
|
|
|
uint64_t bytes=sizeof(T);
|
|
|
|
uint64_t words=in.size()/numnode;
|
|
|
|
assert(numnode * words == in.size());
|
2017-12-05 11:43:25 +00:00
|
|
|
assert(words < (1ULL<<31));
|
2017-10-27 14:20:35 +01:00
|
|
|
AllToAll(dim,(void *)&in[0],(void *)&out[0],words,bytes);
|
2017-10-09 23:16:51 +01:00
|
|
|
}
|
2017-10-27 14:20:35 +01:00
|
|
|
void AllToAll(int dim ,void *in,void *out,uint64_t words,uint64_t bytes);
|
|
|
|
void AllToAll(void *in,void *out,uint64_t words ,uint64_t bytes);
|
2016-10-24 17:30:43 +01:00
|
|
|
|
|
|
|
template<class obj> void Broadcast(int root,obj &data)
|
2018-01-13 00:17:16 +00:00
|
|
|
{
|
|
|
|
Broadcast(root,(void *)&data,sizeof(data));
|
2018-01-24 13:31:17 +00:00
|
|
|
}
|
2015-05-11 19:09:49 +01:00
|
|
|
|
|
|
|
};
|
2018-01-13 00:16:34 +00:00
|
|
|
|
|
|
|
NAMESPACE_END(Grid);
|
2015-05-11 19:09:49 +01:00
|
|
|
|
|
|
|
#endif
|