Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
8378556b14f0d0fe195ab515f00c4960e1bc99f7
[simgrid.git] / src / smpi / smpi_base.c
1 #include "private.h"
2
3 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_base, smpi,
4                                 "Logging specific to SMPI (base)");
5 XBT_LOG_EXTERNAL_CATEGORY(smpi_base);
6 XBT_LOG_EXTERNAL_CATEGORY(smpi_bench);
7 XBT_LOG_EXTERNAL_CATEGORY(smpi_kernel);
8 XBT_LOG_EXTERNAL_CATEGORY(smpi_mpi);
9 XBT_LOG_EXTERNAL_CATEGORY(smpi_receiver);
10 XBT_LOG_EXTERNAL_CATEGORY(smpi_sender);
11 XBT_LOG_EXTERNAL_CATEGORY(smpi_util);
12
13 smpi_mpi_global_t smpi_mpi_global = NULL;
14
15 void smpi_mpi_land_func(void *a, void *b, int *length,
16                         MPI_Datatype * datatype);
17
18 void smpi_mpi_land_func(void *a, void *b, int *length,
19                         MPI_Datatype * datatype)
20 {
21   int i;
22   if (*datatype == smpi_mpi_global->mpi_int) {
23     int *x = a, *y = b;
24     for (i = 0; i < *length; i++) {
25       y[i] = x[i] && y[i];
26     }
27   }
28 }
29
30 void smpi_mpi_sum_func(void *a, void *b, int *length,
31                        MPI_Datatype * datatype);
32
33 void smpi_mpi_sum_func(void *a, void *b, int *length, MPI_Datatype * datatype)
34 {
35   int i;
36   if (*datatype == smpi_mpi_global->mpi_int) {
37     int *x = a, *y = b;
38     for (i = 0; i < *length; i++) {
39       y[i] = x[i] + y[i];
40     }
41   }
42 }
43
44 int smpi_mpi_comm_rank(smpi_mpi_communicator_t comm)
45 {
46   return comm->index_to_rank_map[smpi_host_index()];
47 }
48
49 void smpi_init_process()
50 {
51   smx_host_t host;
52   int i;
53   smpi_host_data_t hdata;
54
55   SIMIX_mutex_lock(smpi_global->running_hosts_count_mutex);
56   smpi_global->running_hosts_count++;
57   SIMIX_mutex_unlock(smpi_global->running_hosts_count_mutex);
58
59   // initialize some local variables
60   host = SIMIX_host_self();
61
62   hdata = xbt_new(s_smpi_host_data_t, 1);
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   SIMIX_host_set_data(host, hdata);
71
72   return;
73 }
74
75 void smpi_mpi_finalize()
76 {
77   int i;
78
79   SIMIX_mutex_lock(smpi_global->running_hosts_count_mutex);
80   i = --smpi_global->running_hosts_count;
81   SIMIX_mutex_unlock(smpi_global->running_hosts_count_mutex);
82
83   SIMIX_mutex_destroy(smpi_host_mutex());
84   SIMIX_cond_destroy(smpi_host_cond());
85
86   if (0 >= i) {
87
88     // wake up senders/receivers
89     for (i = 0; i < smpi_global->host_count; i++) {
90       if (SIMIX_process_is_suspended(smpi_global->sender_processes[i])) {
91         SIMIX_process_resume(smpi_global->sender_processes[i]);
92       }
93       if (SIMIX_process_is_suspended(smpi_global->receiver_processes[i])) {
94         SIMIX_process_resume(smpi_global->receiver_processes[i]);
95       }
96     }
97
98     SIMIX_mutex_destroy(smpi_mpi_global->mpi_comm_world->barrier_mutex);
99     SIMIX_cond_destroy(smpi_mpi_global->mpi_comm_world->barrier_cond);
100     xbt_free(smpi_mpi_global->mpi_comm_world);
101
102     xbt_free(smpi_mpi_global->mpi_byte);
103     xbt_free(smpi_mpi_global->mpi_int);
104     xbt_free(smpi_mpi_global->mpi_double);
105
106     xbt_free(smpi_mpi_global->mpi_land);
107     xbt_free(smpi_mpi_global->mpi_sum);
108
109     xbt_free(smpi_mpi_global);
110
111   }
112
113 }
114
115 int smpi_mpi_barrier(smpi_mpi_communicator_t comm)
116 {
117
118   SIMIX_mutex_lock(comm->barrier_mutex);
119   ++comm->barrier_count;
120   if (comm->barrier_count > comm->size) {       // only happens on second barrier...
121     comm->barrier_count = 0;
122   } else if (comm->barrier_count == comm->size) {
123     SIMIX_cond_broadcast(comm->barrier_cond);
124   }
125   while (comm->barrier_count < comm->size) {
126     SIMIX_cond_wait(comm->barrier_cond, comm->barrier_mutex);
127   }
128   SIMIX_mutex_unlock(comm->barrier_mutex);
129
130   return MPI_SUCCESS;
131 }
132
133 int smpi_mpi_isend(smpi_mpi_request_t request)
134 {
135   int retval = MPI_SUCCESS;
136   int index = smpi_host_index();
137
138   if (NULL == request) {
139     retval = MPI_ERR_INTERN;
140   } else {
141     xbt_fifo_push(smpi_global->pending_send_request_queues[index], request);
142
143     if (SIMIX_process_is_suspended(smpi_global->sender_processes[index])) {
144       SIMIX_process_resume(smpi_global->sender_processes[index]);
145     }
146   }
147
148   return retval;
149 }
150
151 int smpi_mpi_irecv(smpi_mpi_request_t request)
152 {
153   int retval = MPI_SUCCESS;
154   int index = smpi_host_index();
155
156   if (NULL == request) {
157     retval = MPI_ERR_INTERN;
158   } else {
159     xbt_fifo_push(smpi_global->pending_recv_request_queues[index], request);
160
161     if (SIMIX_process_is_suspended(smpi_global->receiver_processes[index])) {
162       SIMIX_process_resume(smpi_global->receiver_processes[index]);
163     }
164   }
165
166   return retval;
167 }
168
169 int smpi_mpi_wait(smpi_mpi_request_t request, smpi_mpi_status_t * status)
170 {
171   int retval = MPI_SUCCESS;
172
173   if (NULL == request) {
174     retval = MPI_ERR_INTERN;
175   } else {
176     SIMIX_mutex_lock(request->mutex);
177     while (!request->completed) {
178       SIMIX_cond_wait(request->cond, request->mutex);
179     }
180     if (NULL != status) {
181       status->MPI_SOURCE = request->src;
182       status->MPI_TAG = request->tag;
183       status->MPI_ERROR = MPI_SUCCESS;
184     }
185     SIMIX_mutex_unlock(request->mutex);
186   }
187
188   return retval;
189 }