Logo AND Algorithmique Numérique Distribuée

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