Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Check for errors from MSG_task_receive.
[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 = NULL, 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     myjob = wait_job(myid);
100   }
101
102   /* Multiplication main-loop */
103   XBT_VERB("Start Multiplication's Main-loop");
104   for(k=0; k < GRID_SIZE; k++){
105     if(k == myjob->col){
106       XBT_VERB("Broadcast sA(%d,%d) to row %d", myjob->row, k, myjob->row);
107       broadcast_matrix(myjob->A, NEIGHBOURS_COUNT, myjob->nodes_in_row);
108     }
109
110     if(k == myjob->row){
111       XBT_VERB("Broadcast sB(%d,%d) to col %d", k, myjob->col, myjob->col);
112       broadcast_matrix(myjob->B, NEIGHBOURS_COUNT, myjob->nodes_in_col);
113     }
114
115     if(myjob->row == k && myjob->col == k){
116       xbt_matrix_double_addmult(myjob->A, myjob->B, sC);
117     }else if(myjob->row == k){
118       get_sub_matrix(&sA, myid);
119       xbt_matrix_double_addmult(sA, myjob->B, sC);
120       xbt_matrix_free(sA);
121     }else if(myjob->col == k){
122       get_sub_matrix(&sB, myid);
123       xbt_matrix_double_addmult(myjob->A, sB, sC);
124       xbt_matrix_free(sB);
125     }else{
126       get_sub_matrix(&sA, myid);
127       get_sub_matrix(&sB, myid);
128       xbt_matrix_double_addmult(sA, sB, sC);
129       xbt_matrix_free(sA);
130       xbt_matrix_free(sB);
131     }
132   }
133
134   /* Node 0: gather the results and reconstruct the final matrix */
135   if(myid == 0){
136     int node;
137     result_t results[GRID_NUM_NODES] = {0};
138
139     XBT_VERB("Multiplication done.");
140
141     /* Get the result from the nodes in the GRID */
142     receive_results(results);
143
144     /* First add our results */
145     xbt_matrix_copy_values(C, sC, NODE_MATRIX_SIZE, NODE_MATRIX_SIZE,
146                            0, 0, 0, 0, NULL);
147
148     /* Reconstruct the rest of the result matrix */
149     for (node = 1; node < GRID_NUM_NODES; node++){
150       xbt_matrix_copy_values(C, results[node]->sC,
151                              NODE_MATRIX_SIZE, NODE_MATRIX_SIZE,
152                              NODE_MATRIX_SIZE * results[node]->row,
153                              NODE_MATRIX_SIZE * results[node]->col,
154                              0, 0, NULL);
155       xbt_matrix_free(results[node]->sC);
156       xbt_free(results[node]);
157     }
158
159     //xbt_matrix_dump(C, "C:res", 0, xbt_matrix_dump_display_double);
160
161   /* The rest: return the result to node 0 */
162   }else{
163     msg_task_t task;
164
165     XBT_VERB("Multiplication done. Send the sub-result.");
166
167     result = xbt_new0(s_result_t, 1);
168     result->row = myjob->row;
169     result->col = myjob->col;
170     result->sC =
171       xbt_matrix_new_sub(sC, NODE_MATRIX_SIZE, NODE_MATRIX_SIZE, 0, 0, NULL);
172     task = MSG_task_create("result",100,100,result);
173     MSG_task_dsend(task, "0", (void_f_pvoid_t) MSG_task_destroy);
174   }
175
176   /* Clean up and finish*/
177   xbt_matrix_free(myjob->A);
178   xbt_matrix_free(myjob->B);
179   xbt_free(myjob);
180   return 0;
181 }
182
183 /*
184  * Broadcast the jobs to the nodes of the grid (except to node 0)
185  */
186 static void broadcast_jobs(node_job_t *jobs)
187 {
188   int node;
189   char node_mbox[MAILBOX_NAME_SIZE];
190   msg_task_t task;
191   msg_comm_t comms[GRID_NUM_NODES - 1] = {0};
192
193   XBT_VERB("Broadcast Jobs");
194   for (node = 1; node < GRID_NUM_NODES; node++){
195     task = MSG_task_create("Job", 100, 100, jobs[node-1]);
196     snprintf(node_mbox, MAILBOX_NAME_SIZE - 1, "%d", node);
197     comms[node-1] = MSG_task_isend(task, node_mbox);
198   }
199
200   MSG_comm_waitall(comms, GRID_NUM_NODES-1, -1);
201 }
202
203 static node_job_t wait_job(int selfid)
204 {
205   msg_task_t task = NULL;
206   char self_mbox[MAILBOX_NAME_SIZE];
207   node_job_t job;
208   msg_error_t err;
209   snprintf(self_mbox, MAILBOX_NAME_SIZE - 1, "%d", selfid);
210   err = MSG_task_receive(&task, self_mbox);
211   if (err != MSG_OK)
212     xbt_die("Error while receiving from %s (%d)", self_mbox, (int)err);
213   job = (node_job_t)MSG_task_get_data(task);
214   MSG_task_destroy(task);
215   XBT_VERB("Got Job (%d,%d)", job->row, job->col);
216
217   return job;
218 }
219
220 static void broadcast_matrix(xbt_matrix_t M, int num_nodes, int *nodes)
221 {
222   int node;
223   char node_mbox[MAILBOX_NAME_SIZE];
224   msg_task_t task;
225   xbt_matrix_t sM;
226
227   for(node=0; node < num_nodes; node++){
228     snprintf(node_mbox, MAILBOX_NAME_SIZE - 1, "%d", nodes[node]);
229     sM = xbt_matrix_new_sub(M, NODE_MATRIX_SIZE, NODE_MATRIX_SIZE, 0, 0, NULL);
230     task = MSG_task_create("sub-matrix", 100, 100, sM);
231     MSG_task_dsend(task, node_mbox, task_cleanup);
232     XBT_DEBUG("sub-matrix sent to %s", node_mbox);
233   }
234
235 }
236
237 static void get_sub_matrix(xbt_matrix_t *sM, int selfid)
238 {
239   msg_task_t task = NULL;
240   char node_mbox[MAILBOX_NAME_SIZE];
241   msg_error_t err;
242
243   XBT_VERB("Get sub-matrix");
244
245   snprintf(node_mbox, MAILBOX_NAME_SIZE - 1, "%d", selfid);
246   err = MSG_task_receive(&task, node_mbox);
247   if (err != MSG_OK)
248     xbt_die("Error while receiving from %s (%d)", node_mbox, (int)err);
249   *sM = (xbt_matrix_t)MSG_task_get_data(task);
250   MSG_task_destroy(task);
251 }
252
253 static void task_cleanup(void *arg){
254   msg_task_t task = (msg_task_t)arg;
255   xbt_matrix_t m = (xbt_matrix_t)MSG_task_get_data(task);
256   xbt_matrix_free(m);
257   MSG_task_destroy(task);
258 }
259
260 /**
261  * \brief Main function.
262  */
263 int main(int argc, char *argv[])
264 {
265 #ifdef BENCH_THIS_CODE
266   xbt_os_timer_t timer = xbt_os_timer_new();
267 #endif
268
269   MSG_init(&argc, argv);
270
271   char **options = &argv[1];
272   const char* platform_file = options[0];
273   const char* application_file = options[1];
274
275   MSG_create_environment(platform_file);
276
277   MSG_function_register("node", node);
278   MSG_launch_application(application_file);
279
280 #ifdef BENCH_THIS_CODE
281   xbt_os_timer_start(timer);
282 #endif
283   msg_error_t res = MSG_main();
284 #ifdef BENCH_THIS_CODE
285   xbt_os_timer_stop(timer);
286 #endif
287   XBT_CRITICAL("Simulated time: %g", MSG_get_clock());
288
289   if (res == MSG_OK)
290     return 0;
291   else
292     return 1;
293 }
294
295 static void create_jobs(xbt_matrix_t A, xbt_matrix_t B, node_job_t *jobs)
296 {
297   int node, j, k, row = 0, col = 0;
298
299   for (node = 0; node < GRID_NUM_NODES; node++){
300     XBT_VERB("Create job %d", node);
301     jobs[node] = xbt_new0(s_node_job_t, 1);
302     jobs[node]->row = row;
303     jobs[node]->col = col;
304
305     /* Compute who are the nodes in the same row and column */
306     /* than the node receiving this job */
307     for (j = 0, k = 0; j < GRID_SIZE; j++) {
308       if (node != (GRID_SIZE * row) + j) {
309         jobs[node]->nodes_in_row[k] = (GRID_SIZE * row) + j;
310         k++;
311       }
312     }
313
314     for (j = 0, k = 0; j < GRID_SIZE; j++) {
315       if (node != (GRID_SIZE * j) + col) {
316         jobs[node]->nodes_in_col[k] = (GRID_SIZE * j) + col;
317         k++;
318       }
319     }
320
321     /* Assign a sub matrix of A and B to the job */
322     jobs[node]->A =
323       xbt_matrix_new_sub(A, NODE_MATRIX_SIZE, NODE_MATRIX_SIZE,
324                          NODE_MATRIX_SIZE * row, NODE_MATRIX_SIZE * col,
325                          NULL);
326     jobs[node]->B =
327       xbt_matrix_new_sub(B, NODE_MATRIX_SIZE, NODE_MATRIX_SIZE,
328                          NODE_MATRIX_SIZE * row, NODE_MATRIX_SIZE * col,
329                          NULL);
330
331     if (++col >= GRID_SIZE){
332       col = 0;
333       row++;
334     }
335   }
336 }
337
338 static void receive_results(result_t *results){
339   int node;
340   msg_comm_t comms[GRID_NUM_NODES-1] = {0};
341   msg_task_t tasks[GRID_NUM_NODES-1] = {0};
342
343   XBT_VERB("Receive Results.");
344
345   /* Get the result from the nodes in the GRID */
346   for (node = 1; node < GRID_NUM_NODES; node++){
347    comms[node-1] = MSG_task_irecv(&tasks[node-1], "0");
348   }
349
350   MSG_comm_waitall(comms, GRID_NUM_NODES - 1, -1);
351
352   /* Reconstruct the result matrix */
353   for (node = 1; node < GRID_NUM_NODES; node++){
354     results[node] = (result_t)MSG_task_get_data(tasks[node-1]);
355     MSG_task_destroy(tasks[node-1]);
356   }
357 }