4 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_base, smpi,
5 "Logging specific to SMPI (base)");
6 XBT_LOG_EXTERNAL_CATEGORY(smpi_base);
7 XBT_LOG_EXTERNAL_CATEGORY(smpi_bench);
8 XBT_LOG_EXTERNAL_CATEGORY(smpi_kernel);
9 XBT_LOG_EXTERNAL_CATEGORY(smpi_mpi);
10 XBT_LOG_EXTERNAL_CATEGORY(smpi_receiver);
11 XBT_LOG_EXTERNAL_CATEGORY(smpi_sender);
12 XBT_LOG_EXTERNAL_CATEGORY(smpi_util);
14 smpi_mpi_global_t smpi_mpi_global = NULL;
17 * Operations of MPI_OP : implemented=land,sum,min,max
19 void smpi_mpi_land_func(void *a, void *b, int *length,
20 MPI_Datatype * datatype);
22 void smpi_mpi_land_func(void *a, void *b, int *length,
23 MPI_Datatype * datatype)
26 if (*datatype == smpi_mpi_global->mpi_int) {
28 for (i = 0; i < *length; i++) {
35 * sum two vectors element-wise
37 * @param a the first vectors
38 * @param b the second vectors
39 * @return the second vector is modified and contains the element-wise sums
41 void smpi_mpi_sum_func(void *a, void *b, int *length,
42 MPI_Datatype * datatype);
44 void smpi_mpi_sum_func(void *a, void *b, int *length, MPI_Datatype * datatype)
47 if ((*datatype == smpi_mpi_global->mpi_byte) || (*datatype == smpi_mpi_global->mpi_char)) {
49 for (i = 0; i < *length; i++) {
52 } else if (*datatype == smpi_mpi_global->mpi_int) {
54 for (i = 0; i < *length; i++) {
57 } else if (*datatype == smpi_mpi_global->mpi_float) {
59 for (i = 0; i < *length; i++) {
62 } else if (*datatype == smpi_mpi_global->mpi_double) {
63 double *x = a, *y = b;
64 for (i = 0; i < *length; i++) {
70 *i multiply two vectors element-wise
72 * @param a the first vectors
73 * @param b the second vectors
74 * @return the second vector is modified and contains the element-wise products
76 void smpi_mpi_prod_func(void *a, void *b, int *length,
77 MPI_Datatype * datatype);
79 void smpi_mpi_prod_func(void *a, void *b, int *length, MPI_Datatype * datatype)
82 if ((*datatype == smpi_mpi_global->mpi_byte) || (*datatype == smpi_mpi_global->mpi_char)) {
84 for (i = 0; i < *length; i++) {
87 } else if (*datatype == smpi_mpi_global->mpi_int) {
89 for (i = 0; i < *length; i++) {
92 } else if (*datatype == smpi_mpi_global->mpi_float) {
94 for (i = 0; i < *length; i++) {
97 } else if (*datatype == smpi_mpi_global->mpi_double) {
98 double *x = a, *y = b;
99 for (i = 0; i < *length; i++) {
105 * compute the min of two vectors element-wise
107 void smpi_mpi_min_func(void *a, void *b, int *length,
108 MPI_Datatype * datatype);
110 void smpi_mpi_min_func(void *a, void *b, int *length, MPI_Datatype * datatype)
113 if ((*datatype == smpi_mpi_global->mpi_byte) || (*datatype == smpi_mpi_global->mpi_char)) {
115 for (i = 0; i < *length; i++) {
116 y[i] = x[i] < y[i] ? x[i] : y[i];
119 if (*datatype == smpi_mpi_global->mpi_int) {
121 for (i = 0; i < *length; i++) {
122 y[i] = x[i] < y[i] ? x[i] : y[i];
125 if (*datatype == smpi_mpi_global->mpi_float) {
126 float *x = a, *y = b;
127 for (i = 0; i < *length; i++) {
128 y[i] = x[i] < y[i] ? x[i] : y[i];
131 if (*datatype == smpi_mpi_global->mpi_double) {
132 double *x = a, *y = b;
133 for (i = 0; i < *length; i++) {
134 y[i] = x[i] < y[i] ? x[i] : y[i];
144 * compute the max of two vectors element-wise
146 void smpi_mpi_max_func(void *a, void *b, int *length,
147 MPI_Datatype * datatype);
149 void smpi_mpi_max_func(void *a, void *b, int *length, MPI_Datatype * datatype)
152 if ((*datatype == smpi_mpi_global->mpi_byte) || (*datatype == smpi_mpi_global->mpi_char)) {
154 for (i = 0; i < *length; i++) {
155 y[i] = x[i] > y[i] ? x[i] : y[i];
157 } else if (*datatype == smpi_mpi_global->mpi_int) {
159 for (i = 0; i < *length; i++) {
160 y[i] = x[i] > y[i] ? x[i] : y[i];
162 } else if (*datatype == smpi_mpi_global->mpi_float) {
163 float *x = a, *y = b;
164 for (i = 0; i < *length; i++) {
165 y[i] = x[i] > y[i] ? x[i] : y[i];
167 } else if (*datatype == smpi_mpi_global->mpi_double) {
168 double *x = a, *y = b;
169 for (i = 0; i < *length; i++) {
170 y[i] = x[i] > y[i] ? x[i] : y[i];
180 * tell the MPI rank of the calling process (from its SIMIX process id)
182 int smpi_mpi_comm_rank(smpi_mpi_communicator_t comm)
184 return comm->index_to_rank_map[smpi_process_index()];
187 void smpi_process_init(int *argc, char ***argv)
189 smpi_process_data_t pdata;
191 // initialize some local variables
193 pdata = xbt_new(s_smpi_process_data_t, 1);
194 SIMIX_process_set_data(SIMIX_process_self(), pdata);
196 /* get rank from command line, and remove it from argv */
197 pdata->index = atoi((*argv)[1]);
198 DEBUG1("I'm rank %d", pdata->index);
200 memmove((*argv)[1], (*argv)[2], sizeof(char *) * (*argc - 2));
201 (*argv)[(*argc) - 1] = NULL;
205 pdata->mutex = SIMIX_mutex_init();
206 pdata->cond = SIMIX_cond_init();
209 pdata->pending_recv_request_queue = xbt_fifo_new();
210 pdata->pending_send_request_queue = xbt_fifo_new();
211 pdata->received_message_queue = xbt_fifo_new();
213 pdata->main = SIMIX_process_self();
214 pdata->sender = SIMIX_process_create("smpi_sender",
216 SIMIX_host_get_name(SIMIX_host_self()),
219 pdata->receiver = SIMIX_process_create("smpi_receiver",
220 smpi_receiver, pdata,
221 SIMIX_host_get_name(SIMIX_host_self
225 smpi_global->main_processes[pdata->index] = SIMIX_process_self();
229 void smpi_process_finalize()
231 smpi_process_data_t pdata = SIMIX_process_get_data(SIMIX_process_self());
233 pdata->finalize = 2; /* Tell sender and receiver to quit */
234 SIMIX_process_resume(pdata->sender);
235 SIMIX_process_resume(pdata->receiver);
236 while (pdata->finalize > 0) { /* wait until it's done */
237 SIMIX_cond_wait(pdata->cond, pdata->mutex);
240 SIMIX_mutex_destroy(pdata->mutex);
241 SIMIX_cond_destroy(pdata->cond);
242 xbt_fifo_free(pdata->pending_recv_request_queue);
243 xbt_fifo_free(pdata->pending_send_request_queue);
244 xbt_fifo_free(pdata->received_message_queue);
249 /*int smpi_mpi_barrier(smpi_mpi_communicator_t comm)
252 SIMIX_mutex_lock(comm->barrier_mutex);
253 ++comm->barrier_count;
254 if (comm->barrier_count > comm->size) { // only happens on second barrier...
255 comm->barrier_count = 0;
256 } else if (comm->barrier_count == comm->size) {
257 SIMIX_cond_broadcast(comm->barrier_cond);
259 while (comm->barrier_count < comm->size) {
260 SIMIX_cond_wait(comm->barrier_cond, comm->barrier_mutex);
262 SIMIX_mutex_unlock(comm->barrier_mutex);
268 int smpi_mpi_isend(smpi_mpi_request_t request)
270 smpi_process_data_t pdata = SIMIX_process_get_data(SIMIX_process_self());
271 int retval = MPI_SUCCESS;
273 if (NULL == request) {
274 retval = MPI_ERR_INTERN;
276 xbt_fifo_push(pdata->pending_send_request_queue, request);
277 SIMIX_process_resume(pdata->sender);
283 int smpi_mpi_irecv(smpi_mpi_request_t request)
285 int retval = MPI_SUCCESS;
286 smpi_process_data_t pdata = SIMIX_process_get_data(SIMIX_process_self());
288 if (NULL == request) {
289 retval = MPI_ERR_INTERN;
291 xbt_fifo_push(pdata->pending_recv_request_queue, request);
293 if (SIMIX_process_is_suspended(pdata->receiver)) {
294 SIMIX_process_resume(pdata->receiver);
301 void print_req( smpi_mpi_request_t r );
302 void print_req( smpi_mpi_request_t r ) {
303 printf("***req %p-> src=%d dst=%d tag=%d completed=0x%x consumed=0x%x\n",r,r->src,r->dst,r->tag,r->completed,r->consumed);
308 * wait and friends ...
310 int smpi_mpi_wait(smpi_mpi_request_t request, smpi_mpi_status_t * status)
312 int retval = MPI_SUCCESS;
314 if (NULL == request) {
315 retval = MPI_ERR_INTERN;
317 SIMIX_mutex_lock(request->mutex);
318 //#define DEBUG_STEPH
320 print_req( request ); //@@
322 while (!request->completed) {
323 SIMIX_cond_wait(request->cond, request->mutex);
325 if (NULL != status) {
326 status->MPI_SOURCE = request->src;
327 status->MPI_TAG = request->tag;
328 status->MPI_ERROR = MPI_SUCCESS;
330 SIMIX_mutex_unlock(request->mutex);
339 int smpi_mpi_waitall(int count, smpi_mpi_request_t requests[],
340 smpi_mpi_status_t status[])
345 smpi_mpi_status_t stat;
347 for (cpt = 0; cpt < count; cpt++) {
348 retval = smpi_mpi_waitany(count, requests, &index, &stat);
349 if (retval != MPI_SUCCESS)
351 if (MPI_STATUS_IGNORE != status)
352 memcpy(&(status[index]), &stat, sizeof(stat));
360 int smpi_mpi_waitany(int count, smpi_mpi_request_t * requests, int *index,
361 smpi_mpi_status_t * status)
365 *index = MPI_UNDEFINED;
366 if (NULL == requests) {
367 return MPI_ERR_INTERN;
369 /* First check if one of them is already done */
370 for (cpt = 0; cpt < count; cpt++) {
372 printf("...exam req[%d] of msg from [%d]\n",cpt,requests[cpt]->src);
374 if (requests[cpt]->completed && !requests[cpt]->consumed) { /* got ya */
376 printf("...found match req[%d] of msg from [%d]\n",cpt,requests[cpt]->src);
382 /* If none found, block */
383 /* FIXME: should use a SIMIX_cond_waitany, when implemented. For now, block on the first one */
385 for (cpt = 0; cpt < count; cpt++) {
388 print_req( requests[cpt] );
390 if (!requests[cpt]->completed) { /* this one is not done, wait on it */
392 printf("... blocked waiting a msg %d->%d, tag=%d\n",requests[cpt]->src,requests[cpt]->dst,requests[cpt]->tag);
394 while (!requests[cpt]->completed)
395 SIMIX_cond_wait(requests[cpt]->cond, requests[cpt]->mutex);
401 if (cpt == count) /* they are all done. Damn user */
402 return MPI_ERR_REQUEST;
407 print_req( requests[cpt] );
409 requests[*index]->consumed = 1;
411 print_req( requests[cpt] );
412 printf("...accessing *req[%d]->consumed\n",cpt);
414 if (NULL != status) {
415 status->MPI_SOURCE = requests[*index]->src;
416 status->MPI_TAG = requests[*index]->tag;
417 status->MPI_ERROR = MPI_SUCCESS;