Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Some symbols were renamed by last flexml update.
[simgrid.git] / examples / msg / pmm / msg_pmm.c
1 /* pmm - parallel matrix multiplication "double diffusion"                  */
2
3 /* Copyright (c) 2006, 2007, 2008, 2009, 2010, 2011. The SimGrid Team.
4  * All rights reserved.                                                     */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8 #include "msg/msg.h"
9 #include "xbt/matrix.h"
10 #include "xbt/log.h"
11
12 // #define BENCH_THIS_CODE /* Will only work from within the source tree as we require xbt/xbt_os_time.h, that is not public yet) */
13 #ifdef BENCH_THIS_CODE
14 #include "xbt/xbt_os_time.h"
15 #endif
16
17 /** @addtogroup MSG_examples
18  * 
19  * - <b>pmm/msg_pmm.c</b>: Parallel Matrix Multiplication is a little
20  *   application. This is something that most MPI developper have
21  *   written during their class, here implemented using MSG instead
22  *   of MPI. 
23  */
24
25 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_pmm,
26                              "Messages specific for this msg example");
27
28 /* This example should always be executed using a deployment of
29  * GRID_SIZE * GRID_SIZE nodes. */
30 #define GRID_SIZE 3    /* Modify to adjust the grid's size */
31 #define NODE_MATRIX_SIZE 300  /* Ammount of work done by each node*/
32
33 #define GRID_NUM_NODES GRID_SIZE * GRID_SIZE
34 #define MATRIX_SIZE NODE_MATRIX_SIZE * GRID_SIZE
35 #define MAILBOX_NAME_SIZE 10
36 #define NEIGHBOURS_COUNT GRID_SIZE - 1
37
38 /*
39  * The job sent to every node
40  */
41 typedef struct s_node_job{
42   int row;
43   int col;
44   int nodes_in_row[NEIGHBOURS_COUNT];
45   int nodes_in_col[NEIGHBOURS_COUNT];
46   xbt_matrix_t A;
47   xbt_matrix_t B;
48 } s_node_job_t, *node_job_t;
49
50 /*
51  * Structure for recovering results
52  */
53 typedef struct s_result {
54   int row;
55   int col;
56   xbt_matrix_t sC;
57 } s_result_t, *result_t;
58
59 int node(int argc, char **argv);
60 static void create_jobs(xbt_matrix_t A, xbt_matrix_t B, node_job_t *jobs);
61 static void broadcast_jobs(node_job_t *jobs);
62 static node_job_t wait_job(int selfid);
63 static void broadcast_matrix(xbt_matrix_t M, int num_nodes, int *nodes);
64 static void get_sub_matrix(xbt_matrix_t *sM, int selfid);
65 static void receive_results(result_t *results);
66 static void task_cleanup(void *arg);
67
68 int node(int argc, char **argv)
69 {
70   int k, myid;
71   char my_mbox[MAILBOX_NAME_SIZE];
72   node_job_t myjob, jobs[GRID_NUM_NODES];
73   xbt_matrix_t A, B, C, sA, sB, sC;
74   result_t result;
75
76   xbt_assert(argc != 1, "Wrong number of arguments for this node");
77
78   /* Initialize the node's data-structures */
79   myid = atoi(argv[1]);
80   snprintf(my_mbox, MAILBOX_NAME_SIZE - 1, "%d", myid);
81   sC = xbt_matrix_double_new_zeros(NODE_MATRIX_SIZE, NODE_MATRIX_SIZE);
82
83   if(myid == 0){
84     /* Create the matrices to multiply and one to store the result */
85     A = xbt_matrix_double_new_id(MATRIX_SIZE, MATRIX_SIZE);
86     B = xbt_matrix_double_new_seq(MATRIX_SIZE, MATRIX_SIZE);
87     C = xbt_matrix_double_new_zeros(MATRIX_SIZE, MATRIX_SIZE);
88
89     /* Create the nodes' jobs */
90     create_jobs(A, B, jobs);
91
92     /* Get own job first */
93     myjob = jobs[0];
94
95     /* Broadcast the rest of the jobs to the other nodes */
96     broadcast_jobs(jobs + 1);
97
98   }else{
99     A = B = C = NULL;           /* Avoid warning at compilation */
100     myjob = wait_job(myid);
101   }
102
103   /* Multiplication main-loop */
104   XBT_VERB("Start Multiplication's Main-loop");
105   for(k=0; k < GRID_SIZE; k++){
106     if(k == myjob->col){
107       XBT_VERB("Broadcast sA(%d,%d) to row %d", myjob->row, k, myjob->row);
108       broadcast_matrix(myjob->A, NEIGHBOURS_COUNT, myjob->nodes_in_row);
109     }
110
111     if(k == myjob->row){
112       XBT_VERB("Broadcast sB(%d,%d) to col %d", k, myjob->col, myjob->col);
113       broadcast_matrix(myjob->B, NEIGHBOURS_COUNT, myjob->nodes_in_col);
114     }
115
116     if(myjob->row == k && myjob->col == k){
117       xbt_matrix_double_addmult(myjob->A, myjob->B, sC);
118     }else if(myjob->row == k){
119       get_sub_matrix(&sA, myid);
120       xbt_matrix_double_addmult(sA, myjob->B, sC);
121       xbt_matrix_free(sA);
122     }else if(myjob->col == k){
123       get_sub_matrix(&sB, myid);
124       xbt_matrix_double_addmult(myjob->A, sB, sC);
125       xbt_matrix_free(sB);
126     }else{
127       get_sub_matrix(&sA, myid);
128       get_sub_matrix(&sB, myid);
129       xbt_matrix_double_addmult(sA, sB, sC);
130       xbt_matrix_free(sA);
131       xbt_matrix_free(sB);
132     }
133   }
134
135   /* Node 0: gather the results and reconstruct the final matrix */
136   if(myid == 0){
137     int node;
138     result_t results[GRID_NUM_NODES] = {0};
139
140     XBT_VERB("Multiplication done.");
141
142     /* Get the result from the nodes in the GRID */
143     receive_results(results);
144
145     /* First add our results */
146     xbt_matrix_copy_values(C, sC, NODE_MATRIX_SIZE, NODE_MATRIX_SIZE,
147                            0, 0, 0, 0, NULL);
148
149     /* Reconstruct the rest of the result matrix */
150     for (node = 1; node < GRID_NUM_NODES; node++){
151       xbt_matrix_copy_values(C, results[node]->sC,
152                              NODE_MATRIX_SIZE, NODE_MATRIX_SIZE,
153                              NODE_MATRIX_SIZE * results[node]->row,
154                              NODE_MATRIX_SIZE * results[node]->col,
155                              0, 0, NULL);
156       xbt_matrix_free(results[node]->sC);
157       xbt_free(results[node]);
158     }
159
160     //xbt_matrix_dump(C, "C:res", 0, xbt_matrix_dump_display_double);
161
162     xbt_matrix_free(A);
163     xbt_matrix_free(B);
164     xbt_matrix_free(C);
165
166   /* The rest: return the result to node 0 */
167   }else{
168     msg_task_t task;
169
170     XBT_VERB("Multiplication done. Send the sub-result.");
171
172     result = xbt_new0(s_result_t, 1);
173     result->row = myjob->row;
174     result->col = myjob->col;
175     result->sC =
176       xbt_matrix_new_sub(sC, NODE_MATRIX_SIZE, NODE_MATRIX_SIZE, 0, 0, NULL);
177     task = MSG_task_create("result",100,100,result);
178     MSG_task_send(task, "0");
179   }
180
181   /* Clean up and finish*/
182   xbt_matrix_free(sC);
183   xbt_matrix_free(myjob->A);
184   xbt_matrix_free(myjob->B);
185   xbt_free(myjob);
186   return 0;
187 }
188
189 /*
190  * Broadcast the jobs to the nodes of the grid (except to node 0)
191  */
192 static void broadcast_jobs(node_job_t *jobs)
193 {
194   int node;
195   char node_mbox[MAILBOX_NAME_SIZE];
196   msg_task_t task;
197   xbt_dynar_t comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
198
199   XBT_VERB("Broadcast Jobs");
200   for (node = 1; node < GRID_NUM_NODES; node++){
201     task = MSG_task_create("Job", 100, 100, jobs[node-1]);
202     snprintf(node_mbox, MAILBOX_NAME_SIZE - 1, "%d", node);
203      xbt_dynar_push_as(comms, msg_comm_t, MSG_task_isend(task, node_mbox));
204   }
205
206   MSG_comm_waitall(comms, -1);
207
208   msg_comm_t comm;
209   unsigned int cursor;
210   xbt_dynar_foreach(comms, cursor, comm) {
211     MSG_comm_destroy(comm);
212   }
213
214   xbt_dynar_free(&comms);
215 }
216
217 static node_job_t wait_job(int selfid)
218 {
219   msg_task_t task = NULL;
220   char self_mbox[MAILBOX_NAME_SIZE];
221   node_job_t job;
222   msg_error_t err;
223   snprintf(self_mbox, MAILBOX_NAME_SIZE - 1, "%d", selfid);
224   err = MSG_task_receive(&task, self_mbox);
225   if (err != MSG_OK)
226     xbt_die("Error while receiving from %s (%d)", self_mbox, (int)err);
227   job = (node_job_t)MSG_task_get_data(task);
228   MSG_task_destroy(task);
229   XBT_VERB("Got Job (%d,%d)", job->row, job->col);
230
231   return job;
232 }
233
234 static void broadcast_matrix(xbt_matrix_t M, int num_nodes, int *nodes)
235 {
236   int node;
237   char node_mbox[MAILBOX_NAME_SIZE];
238   msg_task_t task;
239   xbt_matrix_t sM;
240
241   for(node=0; node < num_nodes; node++){
242     snprintf(node_mbox, MAILBOX_NAME_SIZE - 1, "%d", nodes[node]);
243     sM = xbt_matrix_new_sub(M, NODE_MATRIX_SIZE, NODE_MATRIX_SIZE, 0, 0, NULL);
244     task = MSG_task_create("sub-matrix", 100, 100, sM);
245     MSG_task_dsend(task, node_mbox, task_cleanup);
246     XBT_DEBUG("sub-matrix sent to %s", node_mbox);
247   }
248
249 }
250
251 static void get_sub_matrix(xbt_matrix_t *sM, int selfid)
252 {
253   msg_task_t task = NULL;
254   char node_mbox[MAILBOX_NAME_SIZE];
255   msg_error_t err;
256
257   XBT_VERB("Get sub-matrix");
258
259   snprintf(node_mbox, MAILBOX_NAME_SIZE - 1, "%d", selfid);
260   err = MSG_task_receive(&task, node_mbox);
261   if (err != MSG_OK)
262     xbt_die("Error while receiving from %s (%d)", node_mbox, (int)err);
263   *sM = (xbt_matrix_t)MSG_task_get_data(task);
264   MSG_task_destroy(task);
265 }
266
267 static void task_cleanup(void *arg){
268   msg_task_t task = (msg_task_t)arg;
269   xbt_matrix_t m = (xbt_matrix_t)MSG_task_get_data(task);
270   xbt_matrix_free(m);
271   MSG_task_destroy(task);
272 }
273
274 /**
275  * \brief Main function.
276  */
277 int main(int argc, char *argv[])
278 {
279 #ifdef BENCH_THIS_CODE
280   xbt_os_timer_t timer = xbt_os_timer_new();
281 #endif
282
283   MSG_init(&argc, argv);
284
285   char **options = &argv[1];
286   const char* platform_file = options[0];
287   const char* application_file = options[1];
288
289   MSG_create_environment(platform_file);
290
291   MSG_function_register("node", node);
292   MSG_launch_application(application_file);
293
294 #ifdef BENCH_THIS_CODE
295   xbt_os_timer_start(timer);
296 #endif
297   msg_error_t res = MSG_main();
298 #ifdef BENCH_THIS_CODE
299   xbt_os_timer_stop(timer);
300 #endif
301   XBT_CRITICAL("Simulated time: %g", MSG_get_clock());
302
303   if (res == MSG_OK)
304     return 0;
305   else
306     return 1;
307 }
308
309 static void create_jobs(xbt_matrix_t A, xbt_matrix_t B, node_job_t *jobs)
310 {
311   int node, j, k, row = 0, col = 0;
312
313   for (node = 0; node < GRID_NUM_NODES; node++){
314     XBT_VERB("Create job %d", node);
315     jobs[node] = xbt_new0(s_node_job_t, 1);
316     jobs[node]->row = row;
317     jobs[node]->col = col;
318
319     /* Compute who are the nodes in the same row and column */
320     /* than the node receiving this job */
321     for (j = 0, k = 0; j < GRID_SIZE; j++) {
322       if (node != (GRID_SIZE * row) + j) {
323         jobs[node]->nodes_in_row[k] = (GRID_SIZE * row) + j;
324         k++;
325       }
326     }
327
328     for (j = 0, k = 0; j < GRID_SIZE; j++) {
329       if (node != (GRID_SIZE * j) + col) {
330         jobs[node]->nodes_in_col[k] = (GRID_SIZE * j) + col;
331         k++;
332       }
333     }
334
335     /* Assign a sub matrix of A and B to the job */
336     jobs[node]->A =
337       xbt_matrix_new_sub(A, NODE_MATRIX_SIZE, NODE_MATRIX_SIZE,
338                          NODE_MATRIX_SIZE * row, NODE_MATRIX_SIZE * col,
339                          NULL);
340     jobs[node]->B =
341       xbt_matrix_new_sub(B, NODE_MATRIX_SIZE, NODE_MATRIX_SIZE,
342                          NODE_MATRIX_SIZE * row, NODE_MATRIX_SIZE * col,
343                          NULL);
344
345     if (++col >= GRID_SIZE){
346       col = 0;
347       row++;
348     }
349   }
350 }
351
352 static void receive_results(result_t *results){
353   int node;
354   xbt_dynar_t comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
355   msg_task_t tasks[GRID_NUM_NODES-1] = {0};
356
357   XBT_VERB("Receive Results.");
358
359   /* Get the result from the nodes in the GRID */
360   for (node = 1; node < GRID_NUM_NODES; node++){
361    xbt_dynar_push_as(comms, msg_comm_t, MSG_task_irecv(&tasks[node-1], "0"));
362   }
363
364   MSG_comm_waitall(comms, -1);
365   
366   msg_comm_t comm;
367   unsigned int cursor;
368   xbt_dynar_foreach(comms, cursor, comm) {
369     MSG_comm_destroy(comm);
370   }
371   xbt_dynar_free(&comms);
372
373   /* Reconstruct the result matrix */
374   for (node = 1; node < GRID_NUM_NODES; node++){
375     results[node] = (result_t)MSG_task_get_data(tasks[node-1]);
376     MSG_task_destroy(tasks[node-1]);
377   }
378 }