Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
SMPI: Pass the rank from smpirun to processes through their command line in deploymen...
[simgrid.git] / src / smpi / smpi_base.c
1 #include "private.h"
2 #include "xbt/time.h"
3
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);
13
14 smpi_mpi_global_t smpi_mpi_global = NULL;
15
16 void smpi_mpi_land_func(void *a, void *b, int *length,
17                         MPI_Datatype * datatype);
18
19 void smpi_mpi_land_func(void *a, void *b, int *length,
20                         MPI_Datatype * datatype)
21 {
22   int i;
23   if (*datatype == smpi_mpi_global->mpi_int) {
24     int *x = a, *y = b;
25     for (i = 0; i < *length; i++) {
26       y[i] = x[i] && y[i];
27     }
28   }
29 }
30
31 void smpi_mpi_sum_func(void *a, void *b, int *length,
32                        MPI_Datatype * datatype);
33
34 void smpi_mpi_sum_func(void *a, void *b, int *length, MPI_Datatype * datatype)
35 {
36   int i;
37   if (*datatype == smpi_mpi_global->mpi_int) {
38     int *x = a, *y = b;
39     for (i = 0; i < *length; i++) {
40       y[i] = x[i] + y[i];
41     }
42   }
43 }
44
45 int smpi_mpi_comm_rank(smpi_mpi_communicator_t comm)
46 {
47   return comm->index_to_rank_map[smpi_host_index()];
48 }
49
50 void smpi_process_init(int *argc, char***argv)
51 {
52   smx_host_t host;
53   smpi_host_data_t hdata;
54
55   // initialize some local variables
56   host = SIMIX_host_self();
57
58   hdata = xbt_new(s_smpi_host_data_t, 1);
59   SIMIX_host_set_data(host, hdata);
60   SIMIX_process_set_data(SIMIX_process_self(),hdata);
61
62   /* get rank from command line, and remove it from argv */
63   hdata->index = atoi( (*argv)[1] );
64   if (*argc>2) {
65           memmove((*argv)[1],(*argv)[2], sizeof(char*)* (*argc-2));
66           (*argv)[ (*argc)-1] = NULL;
67   }
68   (*argc)--;
69
70   hdata->mutex = SIMIX_mutex_init();
71   hdata->cond = SIMIX_cond_init();
72   hdata->finalize = 0;
73
74   hdata->pending_recv_request_queue = xbt_fifo_new();
75   hdata->pending_send_request_queue = xbt_fifo_new();
76   hdata->received_message_queue = xbt_fifo_new();
77
78   hdata->main = SIMIX_process_self();
79   hdata->sender = SIMIX_process_create("smpi_sender",
80           smpi_sender, hdata,
81           SIMIX_host_get_name(SIMIX_host_self()), 0, NULL,
82           /*props */ NULL);
83   hdata->receiver = SIMIX_process_create("smpi_receiver",
84           smpi_receiver, hdata,
85           SIMIX_host_get_name(SIMIX_host_self()), 0, NULL,
86           /*props */ NULL);
87
88   smpi_global->main_processes[hdata->index] = SIMIX_process_self();
89   return;
90 }
91
92 void smpi_process_finalize()
93 {
94   smpi_host_data_t hdata =  SIMIX_host_get_data(SIMIX_host_self());
95
96   hdata->finalize = 2; /* Tell sender and receiver to quit */
97   SIMIX_process_resume(hdata->sender);
98   SIMIX_process_resume(hdata->receiver);
99   while (hdata->finalize>0) { /* wait until it's done */
100           SIMIX_cond_wait(hdata->cond,hdata->mutex);
101   }
102
103   SIMIX_mutex_destroy(hdata->mutex);
104   SIMIX_cond_destroy(hdata->cond);
105   xbt_fifo_free(hdata->pending_recv_request_queue);
106   xbt_fifo_free(hdata->pending_send_request_queue);
107   xbt_fifo_free(hdata->received_message_queue);
108 }
109
110 int smpi_mpi_barrier(smpi_mpi_communicator_t comm)
111 {
112
113   SIMIX_mutex_lock(comm->barrier_mutex);
114   ++comm->barrier_count;
115   if (comm->barrier_count > comm->size) {       // only happens on second barrier...
116     comm->barrier_count = 0;
117   } else if (comm->barrier_count == comm->size) {
118     SIMIX_cond_broadcast(comm->barrier_cond);
119   }
120   while (comm->barrier_count < comm->size) {
121     SIMIX_cond_wait(comm->barrier_cond, comm->barrier_mutex);
122   }
123   SIMIX_mutex_unlock(comm->barrier_mutex);
124
125   return MPI_SUCCESS;
126 }
127
128 int smpi_mpi_isend(smpi_mpi_request_t request)
129 {
130         smpi_host_data_t hdata =  SIMIX_host_get_data(SIMIX_host_self());
131   int retval = MPI_SUCCESS;
132
133   if (NULL == request) {
134     retval = MPI_ERR_INTERN;
135   } else {
136     xbt_fifo_push(hdata->pending_send_request_queue, request);
137     SIMIX_process_resume(hdata->sender);
138   }
139
140   return retval;
141 }
142
143 int smpi_mpi_irecv(smpi_mpi_request_t request)
144 {
145   int retval = MPI_SUCCESS;
146   smpi_host_data_t hdata =  SIMIX_host_get_data(SIMIX_host_self());
147
148   if (NULL == request) {
149     retval = MPI_ERR_INTERN;
150   } else {
151     xbt_fifo_push(hdata->pending_recv_request_queue, request);
152
153     if (SIMIX_process_is_suspended(hdata->receiver)) {
154       SIMIX_process_resume(hdata->receiver);
155     }
156   }
157
158   return retval;
159 }
160
161 int smpi_mpi_wait(smpi_mpi_request_t request, smpi_mpi_status_t * status)
162 {
163   int retval = MPI_SUCCESS;
164
165   if (NULL == request) {
166     retval = MPI_ERR_INTERN;
167   } else {
168     SIMIX_mutex_lock(request->mutex);
169     while (!request->completed) {
170       SIMIX_cond_wait(request->cond, request->mutex);
171     }
172     if (NULL != status) {
173       status->MPI_SOURCE = request->src;
174       status->MPI_TAG = request->tag;
175       status->MPI_ERROR = MPI_SUCCESS;
176     }
177     SIMIX_mutex_unlock(request->mutex);
178   }
179
180   return retval;
181 }