17 static const char rcsid[] =
"$Id: communicator_mpi.cpp 942 2013-07-22 06:50:06Z aoym $";
35 fprintf(stderr,
"abort() called. terminate.\n");
42 #ifdef ENABLE_MULTI_INSTANCE
43 int Communicator_impl::m_n_instance = 1;
44 int Communicator_impl::m_instance_id = 0;
47 int Communicator_impl::m_global_rank = 0;
48 int Communicator_impl::m_global_size = 1;
62 MPI_Init(pargc, pargv);
64 MPI_Comm_size(MPI_COMM_WORLD, &m_global_size);
65 MPI_Comm_rank(MPI_COMM_WORLD, &m_global_rank);
72 MPI_Comm_dup(MPI_COMM_WORLD, &
m_comm);
81 return MPI_Finalize();
89 #ifdef ENABLE_MULTI_INSTANCE
90 if ((ninstance == 0) || (m_global_size % ninstance != 0)) {
91 printf(
"%s: invalid number of instance: %d\n",
"Communicator::init", ninstance);
95 m_n_instance = ninstance;
97 int gsize = m_global_size / ninstance;
98 m_instance_id = m_global_rank / gsize;
100 MPI_Comm_split(MPI_COMM_WORLD, m_instance_id, 0 , &
m_comm);
106 MPI_Comm_dup(MPI_COMM_WORLD, &
m_comm);
123 MPI_Abort(MPI_COMM_WORLD, EXIT_FAILURE);
150 bool Communicator_impl::is_primary_master()
152 return m_global_rank == 0;
156 #ifdef ENABLE_MULTI_INSTANCE
158 int Communicator_impl::self_global()
160 return m_global_rank;
164 int Communicator_impl::world_id()
166 return m_instance_id;
177 return MPI_Barrier(
m_comm);
181 #ifdef ENABLE_MULTI_INSTANCE
183 int Communicator_impl::sync_global()
186 return MPI_Barrier(MPI_COMM_WORLD);
197 return MPI_Bcast(data, size, MPI_BYTE, sender,
m_comm);
208 assert(ipm == 1 || ipm == -1);
211 memcpy(recv_buf, send_buf, size);
227 send_buf, size, MPI_BYTE, p_send, tag_send,
228 recv_buf, size, MPI_BYTE, p_recv, tag_recv,
239 if (send_to == recv_from) {
240 memcpy(recv_buf, send_buf, size);
242 if (
self() == recv_from)
243 MPI_Send(send_buf, size, MPI_BYTE, send_to, tag,
m_comm);
245 if (
self() == send_to)
246 MPI_Recv(recv_buf, size, MPI_BYTE, recv_from, tag,
m_comm, &status);
258 return MPI_Allreduce((
void *)send_buf, (
void *)recv_buf, count, type, op,
Layout::m_sub_comm[pattern]);
276 MPI_Bcast((
void *)&size,
sizeof(
size_t), MPI_BYTE, sender,
m_comm);
279 char *buf =
new char[size + 1];
280 memset(buf,
'\0', size + 1);
283 data.copy(buf, size, 0);
287 int retv = MPI_Bcast((
void *)buf, size, MPI_BYTE, sender,
m_comm);
314 #ifdef ENABLE_MULTI_INSTANCE
315 printf(
"global_rank=%2d/%2d: ngrid=%d, grid_id=%d: grid_rank=%2d/%2d\n",
316 m_global_rank, m_global_size,
317 m_n_instance, m_instance_id,
321 printf(
"grid_rank=%2d/%2d\n",