3 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_mpi, smpi,
4 "Logging specific to SMPI (mpi)");
6 int SMPI_MPI_Init(int *argc, char ***argv)
8 smpi_process_init(argc,argv);
13 int SMPI_MPI_Finalize()
16 smpi_process_finalize();
20 // right now this just exits the current node, should send abort signal to all
21 // hosts in the communicator (TODO)
22 int SMPI_MPI_Abort(MPI_Comm comm, int errorcode)
28 int SMPI_MPI_Comm_size(MPI_Comm comm, int *size)
30 int retval = MPI_SUCCESS;
35 retval = MPI_ERR_COMM;
36 } else if (NULL == size) {
47 int SMPI_MPI_Comm_rank(MPI_Comm comm, int *rank)
49 int retval = MPI_SUCCESS;
54 retval = MPI_ERR_COMM;
55 } else if (NULL == rank) {
58 *rank = smpi_mpi_comm_rank(comm);
66 int SMPI_MPI_Type_size(MPI_Datatype datatype, size_t * size)
68 int retval = MPI_SUCCESS;
72 if (NULL == datatype) {
73 retval = MPI_ERR_TYPE;
74 } else if (NULL == size) {
77 *size = datatype->size;
85 int SMPI_MPI_Barrier(MPI_Comm comm)
87 int retval = MPI_SUCCESS;
92 retval = MPI_ERR_COMM;
94 retval = smpi_mpi_barrier(comm);
102 int SMPI_MPI_Irecv(void *buf, int count, MPI_Datatype datatype, int src,
103 int tag, MPI_Comm comm, MPI_Request * request)
105 int retval = MPI_SUCCESS;
109 retval = smpi_create_request(buf, count, datatype, src, 0, tag, comm,
111 if (NULL != *request && MPI_SUCCESS == retval) {
112 retval = smpi_mpi_irecv(*request);
120 int SMPI_MPI_Recv(void *buf, int count, MPI_Datatype datatype, int src,
121 int tag, MPI_Comm comm, MPI_Status * status)
123 int retval = MPI_SUCCESS;
124 smpi_mpi_request_t request;
128 retval = smpi_create_request(buf, count, datatype, src, 0, tag, comm,
130 if (NULL != request && MPI_SUCCESS == retval) {
131 retval = smpi_mpi_irecv(request);
132 if (MPI_SUCCESS == retval) {
133 retval = smpi_mpi_wait(request, status);
135 xbt_mallocator_release(smpi_global->request_mallocator, request);
143 int SMPI_MPI_Isend(void *buf, int count, MPI_Datatype datatype, int dst,
144 int tag, MPI_Comm comm, MPI_Request * request)
146 int retval = MPI_SUCCESS;
150 retval = smpi_create_request(buf, count, datatype, 0, dst, tag, comm,
152 if (NULL != *request && MPI_SUCCESS == retval) {
153 retval = smpi_mpi_isend(*request);
161 int SMPI_MPI_Send(void *buf, int count, MPI_Datatype datatype, int dst,
162 int tag, MPI_Comm comm)
164 int retval = MPI_SUCCESS;
165 smpi_mpi_request_t request;
169 retval = smpi_create_request(buf, count, datatype, 0, dst, tag, comm,
171 if (NULL != request && MPI_SUCCESS == retval) {
172 retval = smpi_mpi_isend(request);
173 if (MPI_SUCCESS == retval) {
174 smpi_mpi_wait(request, MPI_STATUS_IGNORE);
176 xbt_mallocator_release(smpi_global->request_mallocator, request);
184 int SMPI_MPI_Wait(MPI_Request * request, MPI_Status * status)
186 return smpi_mpi_wait(*request, status);
189 int SMPI_MPI_Waitall(int count, MPI_Request requests[], MPI_Status status[]) {
190 return smpi_mpi_waitall(count, requests,status);
192 int SMPI_MPI_Waitany(int count, MPI_Request requests[], int *index, MPI_Status status[]) {
193 return smpi_mpi_waitany(count, requests, index,status);
198 int SMPI_MPI_Bcast(void *buf, int count, MPI_Datatype datatype, int root,
202 int retval = MPI_SUCCESS;
204 smpi_mpi_request_t request;
208 rank = smpi_mpi_comm_rank(comm);
211 retval = smpi_create_request(buf, count, datatype, root,
212 (root + 1) % comm->size, 0, comm, &request);
213 request->forward = comm->size - 1;
214 smpi_mpi_isend(request);
216 retval = smpi_create_request(buf, count, datatype, MPI_ANY_SOURCE, rank,
218 smpi_mpi_irecv(request);
221 smpi_mpi_wait(request, MPI_STATUS_IGNORE);
222 xbt_mallocator_release(smpi_global->request_mallocator, request);
233 int SMPI_MPI_Reduce( void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype,
234 MPI_Op op, int root, MPI_Comm comm )
236 int retval = MPI_SUCCESS;
240 smpi_mpi_request_t *tabrequest;
244 rank = smpi_mpi_comm_rank(comm);
247 tabrequest = xbt_malloc((size)*sizeof(smpi_mpi_request_t));
249 if (rank != root) { // if i am not root, simply send my buffer to root
250 retval = smpi_create_request(sendbuf, count, datatype,
251 rank, root, 0, comm, &(tabrequest[rank]));
252 smpi_mpi_isend(tabrequest[rank]);
253 smpi_mpi_wait(tabrequest[rank], MPI_STATUS_IGNORE);
254 //printf("DEBUG: rank %d sent my sendbuf to root (rank %d)\n",rank,root);
256 // i am the root: wait for all buffers by creating requests
257 // i can not use: 'request->forward = size-1;' (which would progagate size-1 receive reqs)
258 // since we should op values as soon as one receiving request matches.
259 for (i=0; i<comm->size; i++) {
260 if ( rank != i ) { // except for me
261 // reminder: for smpi_create_request() the src is always the process sending.
262 retval = smpi_create_request(recvbuf, count, datatype, MPI_ANY_SOURCE, root,
263 0, comm, &(tabrequest[i]));
264 if (NULL != tabrequest[i] && MPI_SUCCESS == retval) {
265 if (MPI_SUCCESS == retval) {
266 smpi_mpi_irecv(tabrequest[i]);
271 // now, wait for completion of all irecv's.
272 // FIXME: we should implement smpi_wait_all for a more asynchronous behavior
273 for (i=0; i<comm->size; i++) {
274 if ( rank != i ) { // except for me
275 smpi_mpi_wait(tabrequest[i], MPI_STATUS_IGNORE);
277 // FIXME: the core part is here. To be written ...
279 fprintf(stderr,"[smpi] %s:%d : MPI_Reduce *Not yet implemented*.\n",__FILE__,__LINE__);
284 for (i=0; i<comm->size; i++)
285 xbt_mallocator_release(smpi_global->request_mallocator, tabrequest[i]);
292 // used by comm_split to sort ranks based on key values
293 int smpi_compare_rankkeys(const void *a, const void *b);
294 int smpi_compare_rankkeys(const void *a, const void *b)
313 int SMPI_MPI_Comm_split(MPI_Comm comm, int color, int key,
316 int retval = MPI_SUCCESS;
319 smpi_mpi_request_t request;
321 smpi_mpi_status_t status;
325 // FIXME: need to test parameters
327 index = smpi_process_index();
328 rank = comm->index_to_rank_map[index];
333 // root node does most of the real work
335 int colormap[comm->size];
336 int keymap[comm->size];
337 int rankkeymap[comm->size * 2];
339 smpi_mpi_communicator_t tempcomm = NULL;
346 // FIXME: use scatter/gather or similar instead of individual comms
347 for (i = 1; i < comm->size; i++) {
348 retval = smpi_create_request(colorkey, 2, MPI_INT, MPI_ANY_SOURCE,
349 rank, MPI_ANY_TAG, comm, &request);
350 smpi_mpi_irecv(request);
351 smpi_mpi_wait(request, &status);
352 colormap[status.MPI_SOURCE] = colorkey[0];
353 keymap[status.MPI_SOURCE] = colorkey[1];
354 xbt_mallocator_release(smpi_global->request_mallocator, request);
357 for (i = 0; i < comm->size; i++) {
358 if (MPI_UNDEFINED == colormap[i]) {
361 // make a list of nodes with current color and sort by keys
363 for (j = i; j < comm->size; j++) {
364 if (colormap[i] == colormap[j]) {
365 colormap[j] = MPI_UNDEFINED;
366 rankkeymap[count * 2] = j;
367 rankkeymap[count * 2 + 1] = keymap[j];
371 qsort(rankkeymap, count, sizeof(int) * 2, &smpi_compare_rankkeys);
374 tempcomm = xbt_new(s_smpi_mpi_communicator_t, 1);
375 tempcomm->barrier_count = 0;
376 tempcomm->size = count;
377 tempcomm->barrier_mutex = SIMIX_mutex_init();
378 tempcomm->barrier_cond = SIMIX_cond_init();
379 tempcomm->rank_to_index_map = xbt_new(int, count);
380 tempcomm->index_to_rank_map = xbt_new(int, smpi_global->process_count);
381 for (j = 0; j < smpi_global->process_count; j++) {
382 tempcomm->index_to_rank_map[j] = -1;
384 for (j = 0; j < count; j++) {
385 indextmp = comm->rank_to_index_map[rankkeymap[j * 2]];
386 tempcomm->rank_to_index_map[j] = indextmp;
387 tempcomm->index_to_rank_map[indextmp] = j;
389 for (j = 0; j < count; j++) {
390 if (rankkeymap[j * 2]) {
391 retval = smpi_create_request(&j, 1, MPI_INT, 0,
392 rankkeymap[j * 2], 0, comm, &request);
393 request->data = tempcomm;
394 smpi_mpi_isend(request);
395 smpi_mpi_wait(request, &status);
396 xbt_mallocator_release(smpi_global->request_mallocator, request);
398 *comm_out = tempcomm;
405 retval = smpi_create_request(colorkey, 2, MPI_INT, rank, 0, 0, comm,
407 smpi_mpi_isend(request);
408 smpi_mpi_wait(request, &status);
409 xbt_mallocator_release(smpi_global->request_mallocator, request);
410 if (MPI_UNDEFINED != color) {
411 retval = smpi_create_request(colorkey, 1, MPI_INT, 0, rank, 0, comm,
413 smpi_mpi_irecv(request);
414 smpi_mpi_wait(request, &status);
415 *comm_out = request->data;
424 double SMPI_MPI_Wtime( void )
426 return ( SIMIX_get_clock() );