21 #ifdef ENABLE_MULTI_INSTANCE
22 int Communicator_impl::m_Ninstance = 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_Ninstance = 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_Ninstance = 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);
229 const int interval = 100000;
231 MPI_Ibcast(&dummy, 1, MPI_BYTE, 0,
m_comm, &req);
239 MPI_Test(&req, &finished, &
status);
246 #ifdef ENABLE_MULTI_INSTANCE
248 int Communicator_impl::sync_global()
251 return MPI_Barrier(MPI_COMM_WORLD);
262 return MPI_Bcast(data,
size, MPI_BYTE, sender,
m_comm);
274 assert(ipm == 1 || ipm == -1);
277 memcpy(recv_buf, send_buf,
size);
293 send_buf,
size, MPI_BYTE, p_send, tag_send,
294 recv_buf,
size, MPI_BYTE, p_recv, tag_recv,
306 if (send_to == recv_from) {
307 memcpy(recv_buf, send_buf,
size);
309 if (
self() == recv_from)
310 MPI_Send(send_buf,
size, MPI_BYTE, send_to, tag,
m_comm);
312 if (
self() == send_to)
343 MPI_Bcast((
void *)&
size,
sizeof(
size_t), MPI_BYTE, sender,
m_comm);
346 char *buf =
new char[
size + 1];
347 memset(buf,
'\0',
size + 1);
350 data.copy(buf,
size, 0);
354 int retv = MPI_Bcast((
void *)buf,
size, MPI_BYTE, sender,
m_comm);
379 #ifdef ENABLE_MULTI_INSTANCE
380 printf(
"global_rank=%2d/%2d: ngrid=%d, grid_id=%d: grid_rank=%2d/%2d\n",
381 m_global_rank, m_global_size,
382 m_Ninstance, m_instance_id,
385 printf(
"grid_rank=%2d/%2d\n",