Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
SMPI: move the last queue (received_message_queue) from global to process data
[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   // initialize some local variables
57   host = SIMIX_host_self();
58
59   hdata = xbt_new(s_smpi_host_data_t, 1);
60   SIMIX_host_set_data(host, hdata);
61   SIMIX_process_set_data(SIMIX_process_self(),hdata);
62
63   for (i = 0; i < smpi_global->host_count && host != smpi_global->hosts[i]; i++);
64
65   hdata->index = i;
66   hdata->mutex = SIMIX_mutex_init();
67   hdata->cond = SIMIX_cond_init();
68   hdata->finalize = 0;
69
70   hdata->pending_recv_request_queue = xbt_fifo_new();
71   hdata->pending_send_request_queue = xbt_fifo_new();
72   hdata->received_message_queue = xbt_fifo_new();
73
74   hdata->main = SIMIX_process_self();
75   hdata->sender = SIMIX_process_create("smpi_sender",
76           smpi_sender, hdata,
77           SIMIX_host_get_name(SIMIX_host_self()), 0, NULL,
78           /*props */ NULL);
79   hdata->receiver = SIMIX_process_create("smpi_receiver",
80           smpi_receiver, hdata,
81           SIMIX_host_get_name(SIMIX_host_self()), 0, NULL,
82           /*props */ NULL);
83
84   smpi_global->main_processes[hdata->index] = SIMIX_process_self();
85   return;
86 }
87
88 void smpi_process_finalize()
89 {
90   smpi_host_data_t hdata =  SIMIX_host_get_data(SIMIX_host_self());
91
92   hdata->finalize = 2; /* Tell sender and receiver to quit */
93   SIMIX_process_resume(hdata->sender);
94   SIMIX_process_resume(hdata->receiver);
95   while (hdata->finalize>0) { /* wait until it's done */
96           SIMIX_cond_wait(hdata->cond,hdata->mutex);
97   }
98
99   SIMIX_mutex_destroy(hdata->mutex);
100   SIMIX_cond_destroy(hdata->cond);
101   xbt_fifo_free(hdata->pending_recv_request_queue);
102   xbt_fifo_free(hdata->pending_send_request_queue);
103   xbt_fifo_free(hdata->received_message_queue);
104 }
105
106 int smpi_mpi_barrier(smpi_mpi_communicator_t comm)
107 {
108
109   SIMIX_mutex_lock(comm->barrier_mutex);
110   ++comm->barrier_count;
111   if (comm->barrier_count > comm->size) {       // only happens on second barrier...
112     comm->barrier_count = 0;
113   } else if (comm->barrier_count == comm->size) {
114     SIMIX_cond_broadcast(comm->barrier_cond);
115   }
116   while (comm->barrier_count < comm->size) {
117     SIMIX_cond_wait(comm->barrier_cond, comm->barrier_mutex);
118   }
119   SIMIX_mutex_unlock(comm->barrier_mutex);
120
121   return MPI_SUCCESS;
122 }
123
124 int smpi_mpi_isend(smpi_mpi_request_t request)
125 {
126         smpi_host_data_t hdata =  SIMIX_host_get_data(SIMIX_host_self());
127   int retval = MPI_SUCCESS;
128
129   if (NULL == request) {
130     retval = MPI_ERR_INTERN;
131   } else {
132     xbt_fifo_push(hdata->pending_send_request_queue, request);
133     SIMIX_process_resume(hdata->sender);
134   }
135
136   return retval;
137 }
138
139 int smpi_mpi_irecv(smpi_mpi_request_t request)
140 {
141   int retval = MPI_SUCCESS;
142   smpi_host_data_t hdata =  SIMIX_host_get_data(SIMIX_host_self());
143
144   if (NULL == request) {
145     retval = MPI_ERR_INTERN;
146   } else {
147     xbt_fifo_push(hdata->pending_recv_request_queue, request);
148
149     if (SIMIX_process_is_suspended(hdata->receiver)) {
150       SIMIX_process_resume(hdata->receiver);
151     }
152   }
153
154   return retval;
155 }
156
157 int smpi_mpi_wait(smpi_mpi_request_t request, smpi_mpi_status_t * status)
158 {
159   int retval = MPI_SUCCESS;
160
161   if (NULL == request) {
162     retval = MPI_ERR_INTERN;
163   } else {
164     SIMIX_mutex_lock(request->mutex);
165     while (!request->completed) {
166       SIMIX_cond_wait(request->cond, request->mutex);
167     }
168     if (NULL != status) {
169       status->MPI_SOURCE = request->src;
170       status->MPI_TAG = request->tag;
171       status->MPI_ERROR = MPI_SUCCESS;
172     }
173     SIMIX_mutex_unlock(request->mutex);
174   }
175
176   return retval;
177 }