21 #ifdef ENABLE_MULTI_INSTANCE
22 int Communicator_impl::m_n_instance = 1;
23 int Communicator_impl::m_instance_id = 0;
26 int Communicator_impl::m_global_rank = 0;
27 int Communicator_impl::m_global_size = 1;
42 int is_initialized = 0;
43 MPI_Initialized(&is_initialized);
46 fprintf(stderr,
"Communicator: MPI already initialized. skip.\n");
49 int required = MPI_THREAD_FUNNELED;
52 MPI_Init_thread(pargc, pargv, required, &provided);
54 if (provided < required) {
55 fprintf(stderr,
"Communicator: MPI not supporting sufficient thread level. exiting.\n");
59 MPI_Init(pargc, pargv);
63 MPI_Comm_size(MPI_COMM_WORLD, &m_global_size);
64 MPI_Comm_rank(MPI_COMM_WORLD, &m_global_rank);
71 MPI_Comm_dup(MPI_COMM_WORLD, &
m_comm);
81 return MPI_Finalize();
90 #ifdef ENABLE_MULTI_INSTANCE
91 if ((ninstance == 0) || (m_global_size % ninstance != 0)) {
92 printf(
"%s: invalid number of instance: %d\n",
"Communicator::init", ninstance);
96 m_n_instance = ninstance;
98 int gsize = m_global_size / ninstance;
99 m_instance_id = m_global_rank / gsize;
101 MPI_Comm_split(MPI_COMM_WORLD, m_instance_id, 0 , &
m_comm);
106 MPI_Comm_dup(MPI_COMM_WORLD, &
m_comm);
122 const std::vector<int>& lattice_size,
123 std::vector<int>& grid_size,
128 #ifdef ENABLE_MULTI_INSTANCE
129 if ((ninstance == 0) || (m_global_size % ninstance != 0)) {
130 printf(
"%s: invalid number of instance: %d\n",
"Communicator::init", ninstance);
134 m_n_instance = ninstance;
136 int gsize = m_global_size / ninstance;
137 m_instance_id = m_global_rank / gsize;
139 MPI_Comm_split(MPI_COMM_WORLD, m_instance_id, 0 , &
m_comm);
144 MPI_Comm_dup(MPI_COMM_WORLD, &
m_comm);
162 MPI_Abort(MPI_COMM_WORLD, EXIT_FAILURE);
190 bool Communicator_impl::is_primary_master()
192 return m_global_rank == 0;
197 #ifdef ENABLE_MULTI_INSTANCE
199 int Communicator_impl::self_global()
201 return m_global_rank;
206 int Communicator_impl::world_id()
208 return m_instance_id;
219 return MPI_Barrier(
m_comm);
228 const int interval=100000;
230 MPI_Ibcast(&dummy, 1, MPI_BYTE, 0,
m_comm, &req);
237 MPI_Test(&req, &finished, &status);
244 #ifdef ENABLE_MULTI_INSTANCE
246 int Communicator_impl::sync_global()
249 return MPI_Barrier(MPI_COMM_WORLD);
260 return MPI_Bcast(data, size, MPI_BYTE, sender,
m_comm);
272 assert(ipm == 1 || ipm == -1);
275 memcpy(recv_buf, send_buf, size);
291 send_buf, size, MPI_BYTE, p_send, tag_send,
292 recv_buf, size, MPI_BYTE, p_recv, tag_recv,
304 if (send_to == recv_from) {
305 memcpy(recv_buf, send_buf, size);
307 if (
self() == recv_from)
308 MPI_Send(send_buf, size, MPI_BYTE, send_to, tag,
m_comm);
310 if (
self() == send_to)
311 MPI_Recv(recv_buf, size, MPI_BYTE, recv_from, tag,
m_comm, &status);
324 return MPI_Allreduce((
void *)send_buf, (
void *)recv_buf, count, type, op,
Layout::m_sub_comm[pattern]);
341 MPI_Bcast((
void *)&size,
sizeof(
size_t), MPI_BYTE, sender,
m_comm);
344 char *buf =
new char[size + 1];
345 memset(buf,
'\0', size + 1);
348 data.copy(buf, size, 0);
352 int retv = MPI_Bcast((
void *)buf, size, MPI_BYTE, sender,
m_comm);
377 #ifdef ENABLE_MULTI_INSTANCE
378 printf(
"global_rank=%2d/%2d: ngrid=%d, grid_id=%d: grid_rank=%2d/%2d\n",
379 m_global_rank, m_global_size,
380 m_n_instance, m_instance_id,
383 printf(
"grid_rank=%2d/%2d\n",
static int send_1to1(size_t size, void *recv_buf, void *send_buf, int send_to, int recv_from, int tag)
static int broadcast(size_t size, void *data, int sender)
static int layout_setup()
initialise layout.
static char default_grid_map[16]
static void abort()
terminate communicator
static int self()
rank within small world.
static int setup(int ninstance=1)
static int * m_ipe_up
rank of upward neighbour in directions.
static int * m_ipe_dn
rank of downward neighbour in directions.
static int * m_grid_dims
grid dimensions in directions.
static int init(int *pargc, char ***pargv)
static MPI_Comm * m_sub_comm
subgrid
static int broadcast_string(int count, string &data, int sender)
static int reduce(int count, void *recv_buf, void *send_buf, MPI_Datatype type, MPI_Op op, int pattern)
static int tag(int rank, int idir, int ipm)
generate tag for communication.
static int exchange(size_t size, void *recv_buf, void *send_buf, int idir, int ipm, int tag)
static std::terminate_handler default_handler