Logo AND Algorithmique Numérique Distribuée

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