Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fbc34fb0a2a4679b5dda08c8f4f70f9e59e01fa4
[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()
51 {
52   smx_host_t host;
53   int i;
54   smpi_host_data_t hdata;
55
56   smpi_global->running_hosts_count++;
57
58   // initialize some local variables
59   host = SIMIX_host_self();
60
61   hdata = xbt_new(s_smpi_host_data_t, 1);
62   SIMIX_host_set_data(host, hdata);
63
64   for (i = 0; i < smpi_global->host_count && host != smpi_global->hosts[i]; i++);
65
66   hdata->index = i;
67   hdata->mutex = SIMIX_mutex_init();
68   hdata->cond = SIMIX_cond_init();
69
70   hdata->pending_recv_request_queue = xbt_fifo_new();
71
72   hdata->main = SIMIX_process_self();
73   hdata->sender = SIMIX_process_create("smpi_sender",
74           smpi_sender, hdata,
75           SIMIX_host_get_name(SIMIX_host_self()), 0, NULL,
76           /*props */ NULL);
77   hdata->receiver = SIMIX_process_create("smpi_receiver",
78           smpi_receiver, hdata,
79           SIMIX_host_get_name(SIMIX_host_self()), 0, NULL,
80           /*props */ NULL);
81
82   smpi_global->main_processes[hdata->index] = SIMIX_process_self();
83   return;
84 }
85
86 void smpi_process_finalize()
87 {
88   int i;
89   smpi_host_data_t hdata =  SIMIX_host_get_data(SIMIX_host_self());
90
91   i = --smpi_global->running_hosts_count;
92
93   SIMIX_mutex_destroy(smpi_host_mutex());
94   SIMIX_cond_destroy(smpi_host_cond());
95   xbt_fifo_free(hdata->pending_recv_request_queue);
96
97   if (0 >= i) {
98
99     // wake up senders/receivers
100     for (i = 0; i < smpi_global->host_count; i++) {
101       if (SIMIX_process_is_suspended(smpi_global->sender_processes[i])) {
102         SIMIX_process_resume(smpi_global->sender_processes[i]);
103       }
104       if (SIMIX_process_is_suspended(smpi_global->receiver_processes[i])) {
105         SIMIX_process_resume(smpi_global->receiver_processes[i]);
106       }
107     }
108
109     SIMIX_mutex_destroy(smpi_mpi_global->mpi_comm_world->barrier_mutex);
110     SIMIX_cond_destroy(smpi_mpi_global->mpi_comm_world->barrier_cond);
111     xbt_free(smpi_mpi_global->mpi_comm_world);
112
113     xbt_free(smpi_mpi_global->mpi_byte);
114     xbt_free(smpi_mpi_global->mpi_int);
115     xbt_free(smpi_mpi_global->mpi_double);
116
117     xbt_free(smpi_mpi_global->mpi_land);
118     xbt_free(smpi_mpi_global->mpi_sum);
119
120     xbt_free(smpi_mpi_global);
121
122   }
123
124 }
125
126 int smpi_mpi_barrier(smpi_mpi_communicator_t comm)
127 {
128
129   SIMIX_mutex_lock(comm->barrier_mutex);
130   ++comm->barrier_count;
131   if (comm->barrier_count > comm->size) {       // only happens on second barrier...
132     comm->barrier_count = 0;
133   } else if (comm->barrier_count == comm->size) {
134     SIMIX_cond_broadcast(comm->barrier_cond);
135   }
136   while (comm->barrier_count < comm->size) {
137     SIMIX_cond_wait(comm->barrier_cond, comm->barrier_mutex);
138   }
139   SIMIX_mutex_unlock(comm->barrier_mutex);
140
141   return MPI_SUCCESS;
142 }
143
144 int smpi_mpi_isend(smpi_mpi_request_t request)
145 {
146   int retval = MPI_SUCCESS;
147   int index = smpi_host_index();
148
149   if (NULL == request) {
150     retval = MPI_ERR_INTERN;
151   } else {
152     xbt_fifo_push(smpi_global->pending_send_request_queues[index], request);
153
154     if (SIMIX_process_is_suspended(smpi_global->sender_processes[index])) {
155       SIMIX_process_resume(smpi_global->sender_processes[index]);
156     }
157   }
158
159   return retval;
160 }
161
162 int smpi_mpi_irecv(smpi_mpi_request_t request)
163 {
164   int retval = MPI_SUCCESS;
165   smpi_host_data_t hdata =  SIMIX_host_get_data(SIMIX_host_self());
166
167   if (NULL == request) {
168     retval = MPI_ERR_INTERN;
169   } else {
170     xbt_fifo_push(hdata->pending_recv_request_queue, request);
171
172     if (SIMIX_process_is_suspended(smpi_global->receiver_processes[hdata->index])) {
173       SIMIX_process_resume(smpi_global->receiver_processes[hdata->index]);
174     }
175   }
176
177   return retval;
178 }
179
180 int smpi_mpi_wait(smpi_mpi_request_t request, smpi_mpi_status_t * status)
181 {
182   int retval = MPI_SUCCESS;
183
184   if (NULL == request) {
185     retval = MPI_ERR_INTERN;
186   } else {
187     SIMIX_mutex_lock(request->mutex);
188     while (!request->completed) {
189       SIMIX_cond_wait(request->cond, request->mutex);
190     }
191     if (NULL != status) {
192       status->MPI_SOURCE = request->src;
193       status->MPI_TAG = request->tag;
194       status->MPI_ERROR = MPI_SUCCESS;
195     }
196     SIMIX_mutex_unlock(request->mutex);
197   }
198
199   return retval;
200 }