Rocstar  1.0
Rocstar multiphysics simulation application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
commpi.C
Go to the documentation of this file.
1 /* *******************************************************************
2  * Rocstar Simulation Suite *
3  * Copyright@2015, Illinois Rocstar LLC. All rights reserved. *
4  * *
5  * Illinois Rocstar LLC *
6  * Champaign, IL *
7  * www.illinoisrocstar.com *
8  * sales@illinoisrocstar.com *
9  * *
10  * License: See LICENSE file in top level of distribution package or *
11  * http://opensource.org/licenses/NCSA *
12  *********************************************************************/
13 /* *******************************************************************
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, *
15  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES *
16  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND *
17  * NONINFRINGEMENT. IN NO EVENT SHALL THE CONTRIBUTORS OR *
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
20  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE *
21  * USE OR OTHER DEALINGS WITH THE SOFTWARE. *
22  *********************************************************************/
23 // $Id: commpi.C,v 1.14 2009/01/22 23:56:54 gzagaris Exp $
24 
34 #include "commpi.h"
35 #include <cstring>
36 #include <cstdlib>
37 #include <iostream>
38 #include <map>
39 
41 typedef std::map<int, void*> Msg_Queue;
42 
44 static int get_sizeof( MPI_Datatype i) throw(int) {
45  if(i == MPI_DOUBLE)
46  return(sizeof(double));
47  else if(i == MPI_FLOAT)
48  return(sizeof(float));
49  else if(i == MPI_INT)
50  return(sizeof(int));
51  else if(i == MPI_BYTE)
52  return(sizeof(char));
53  else if(i == MPI_CHAR)
54  return(sizeof(char));
55  else
56  throw(-1);
57 }
58 // switch (i) {
59 // case MPI_DOUBLE: return sizeof(double);
60 // case MPI_FLOAT: return sizeof(float);
61 // case MPI_INT: return sizeof(int);
62 // case MPI_BYTE: return sizeof(char);
63 // case MPI_CHAR: return sizeof(char);
64 // default: throw(-1);
65 // }
66 // }
67 
69  void *buf, int count, MPI_Datatype datatype, int tag) {
70  try {
71  Msg_Queue::iterator it;
72  if ( (it=recvQ.find( tag)) != recvQ.end()) {
73  int extent=count*get_sizeof(datatype);
74  std::memcpy( it->second, (char*)buf, extent);
75  recvQ.erase(it);
76  }
77  else
78  sendQ[tag] = buf;
79  }
80  catch (int) {
81  std::cerr << "Unsupported data type " << datatype
82  << " used for MPI_Isend" << std::endl;
83  std::abort();
84  }
85  return 0;
86 }
87 
89  void *buf, int count, MPI_Datatype datatype, int tag ) {
90  try {
91  Msg_Queue::iterator it;
92  if ( (it=sendQ.find( tag)) != sendQ.end()) {
93  int extent=count*get_sizeof(datatype);
94  std::memcpy( buf, it->second, extent);
95  sendQ.erase(it);
96  }
97  else
98  recvQ[tag] = buf;
99  }
100  catch (int) {
101  std::cerr << "Unsupported data type " << datatype
102  << " used for MPI_Irecv" << std::endl;
103  std::abort();
104  }
105  return 0;
106 }
107 
108 static Msg_Queue *sendQ=NULL;
109 static Msg_Queue *recvQ=NULL;
110 
112 int COMMPI_Isend( void *buf, int count, MPI_Datatype datatype,
113  int dest, int tag,
114  MPI_Comm comm, MPI_Request *request ) {
115 #ifndef DUMMY_MPI
116  if ( COMMPI_Initialized())
117  return MPI_Isend( buf, count, datatype, dest, tag, comm, request);
118 #endif
119  try {
120  if ( sendQ == NULL) sendQ = new Msg_Queue();
121  if ( recvQ == NULL) recvQ = new Msg_Queue();
122  }
123  catch (...) {
124  std::cerr << "Out of memory" << std::endl;
125  std::abort();
126  }
127  return COM_send( *sendQ, *recvQ, buf, count, datatype, tag);
128 }
129 
131 int COMMPI_Irecv( void *buf, int count, MPI_Datatype datatype,
132  int src, int tag,
133  MPI_Comm comm, MPI_Request *request ) {
134 #ifndef DUMMY_MPI
135  if ( COMMPI_Initialized())
136  return MPI_Irecv( buf, count, datatype, src, tag, comm, request);
137 #endif
138  try {
139  if ( sendQ == NULL) sendQ = new Msg_Queue();
140  if ( recvQ == NULL) recvQ = new Msg_Queue();
141  }
142  catch (...) {
143  std::cerr << "Out of memory" << std::endl;
144  std::abort();
145  }
146  return COM_recv( *sendQ, *recvQ, buf, count, datatype, tag);
147 }
148 
149 #ifdef DUMMY_MPI
150 
152 int MPI_Isend( void *buf, int count, MPI_Datatype datatype,
153  int dest, int tag,
154  MPI_Comm comm, MPI_Request *request ) {
155  return COMMPI_Isend( buf, count, datatype, dest, tag, comm, request);
156 }
157 
159 int MPI_Irecv( void *buf, int count, MPI_Datatype datatype,
160  int src, int tag,
161  MPI_Comm comm, MPI_Request *request ) {
162  return COMMPI_Irecv( buf, count, datatype, src, tag, comm, request);
163 }
164 
165 int MPI_Init( int*, char***) {
166  return 0;
167 }
168 
169 int MPI_Finalize() { return 0;
170  if ( sendQ != NULL) { delete sendQ; sendQ = NULL; }
171  if ( recvQ != NULL) { delete recvQ; recvQ = NULL; }
172  return 0;
173 }
174 
175 int MPI_Allreduce( void *sendbuf, void *recvbuf, int count,
176  MPI_Datatype datatype, MPI_Op op, MPI_Comm comm ) {
177  try {
178  std::memcpy( recvbuf, sendbuf, count*get_sizeof(datatype));
179  }
180  catch (int) {
181  std::cerr << "Unsupported data type " << datatype
182  << " used for MPI_Allreduce" << std::endl;
183  std::abort();
184  }
185  return 0;
186 }
187 
188 int MPI_Allgather( void *sendbuf, int sendcount, MPI_Datatype sendtype,
189  void *recvbuf, int recvcount, MPI_Datatype recvtype,
190  MPI_Comm comm ) {
191  try {
192  std::memcpy( recvbuf, sendbuf, sendcount*get_sizeof(sendtype));
193  }
194  catch (int) {
195  std::cerr << "Unsupported data type " << sendtype
196  << " used for MPI_Allgather" << std::endl;
197  std::abort();
198  }
199  return 0;
200 }
201 
202 int MPI_Allgatherv ( void *sendbuf, int sendcount, MPI_Datatype sendtype,
203  void *recvbuf, int *recvcounts, int *displs,
204  MPI_Datatype recvtype, MPI_Comm comm ) {
205  try {
206  std::memcpy( (char*)recvbuf+*displs*get_sizeof(sendtype),
207  sendbuf, sendcount*get_sizeof(sendtype));
208  }
209  catch (int) {
210  std::cerr << "Unsupported data type " << sendtype
211  << " used for MPI_Allgatherv" << std::endl;
212  std::abort();
213  }
214  return 0;
215 }
216 
217 int MPI_Alltoall( void *sendbuf, int sendcount, MPI_Datatype sendtype,
218  void *recvbuf, int recvcnt, MPI_Datatype recvtype,
219  MPI_Comm comm ) {
220  return MPI_Allgather( sendbuf, sendcount, sendtype,
221  recvbuf, recvcnt, recvtype, comm);
222 }
223 
224 int MPI_Alltoallv( void *sendbuf, int *sendcounts,
225  int *senddispls, MPI_Datatype sendtype,
226  void *recvbuf, int *recvcounts, int *recvdispls,
227  MPI_Datatype recvtype, MPI_Comm comm ) {
228  return MPI_Allgather( (char*)sendbuf+*senddispls, *sendcounts, sendtype,
229  (char*)recvbuf+*recvdispls, *recvcounts, recvtype, comm);
230 }
231 
232 #endif /* DUMMY_MPI */
233 
234 
235 
236 
237 
238 
239 
int COMMPI_Irecv(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm, MPI_Request *request)
Begins a nonblocking receive.
Definition: commpi.C:131
here we put it at the!beginning of the common block The point to point and collective!routines know about but MPI_TYPE_STRUCT as yet does not!MPI_STATUS_IGNORE and MPI_STATUSES_IGNORE are similar objects!Until the underlying MPI library implements the C version of these are declared as arrays of MPI_STATUS_SIZE!The types and are OPTIONAL!Their values are zero if they are not available Note that!using these reduces the portability of MPI_IO INTEGER MPI_BOTTOM INTEGER MPI_DOUBLE_PRECISION INTEGER MPI_LOGICAL INTEGER MPI_BYTE
int COM_send(Msg_Queue &sendQ, Msg_Queue &recvQ, void *buf, int count, MPI_Datatype datatype, int tag)
Definition: commpi.C:68
int COMMPI_Isend(void *buf, int count, MPI_Datatype datatype, int dest, int tag, MPI_Comm comm, MPI_Request *request)
Begins a nonblocking send.
Definition: commpi.C:112
std::map< int, void * > Msg_Queue
A map from message tags to data addresses.
Definition: commpi.C:41
blockLoc i
Definition: read.cpp:79
static Msg_Queue * sendQ
Definition: commpi.C:108
Contains declarations of MPI subroutines used in Roccom.
int COM_recv(Msg_Queue &sendQ, Msg_Queue &recvQ, void *buf, int count, MPI_Datatype datatype, int tag)
Definition: commpi.C:88
static int get_sizeof(MPI_Datatype i)
Get the size of a given MPI data type.
Definition: commpi.C:44
static Msg_Queue * recvQ
Definition: commpi.C:109
int COMMPI_Initialized()
Definition: commpi.h:168