Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid
[simgrid.git] / examples / msg / app-pmm / app-pmm.c
1 /* pmm - parallel matrix multiplication "double diffusion"                  */
2
3 /* Copyright (c) 2006-2015. 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
9 #include "simgrid/msg.h"
10 #include "xbt/matrix.h"
11 #include "xbt/xbt_os_time.h"
12
13 /** @addtogroup MSG_examples
14  *  @section MSG_ex_apps Examples of full applications
15  *
16  * - <b>pmm/msg_pmm.c</b>: Parallel Matrix Multiplication is a little application. This is something that most MPI
17  *   developers have written during their class, here implemented using MSG instead of MPI.
18  */
19
20 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_pmm, "Messages specific for this msg example");
21
22 /* This example should always be executed using a deployment of GRID_SIZE * GRID_SIZE nodes. */
23 #define GRID_SIZE 3           /* Modify to adjust the grid's size */
24 #define NODE_MATRIX_SIZE 300  /* Amount of work done by each node*/
25
26 #define GRID_NUM_NODES GRID_SIZE * GRID_SIZE
27 #define MATRIX_SIZE NODE_MATRIX_SIZE * GRID_SIZE
28 #define MAILBOX_NAME_SIZE 10
29 #define NEIGHBOURS_COUNT GRID_SIZE - 1
30
31 /*
32  * The job sent to every node
33  */
34 typedef struct s_node_job{
35   int row;
36   int col;
37   int nodes_in_row[NEIGHBOURS_COUNT];
38   int nodes_in_col[NEIGHBOURS_COUNT];
39   xbt_matrix_t A;
40   xbt_matrix_t B;
41 } s_node_job_t, *node_job_t;
42
43 /*
44  * Structure for recovering results
45  */
46 typedef struct s_result {
47   int row;
48   int col;
49   xbt_matrix_t sC;
50 } s_result_t, *result_t;
51
52 int node(int argc, char **argv);
53 static void create_jobs(xbt_matrix_t A, xbt_matrix_t B, node_job_t *jobs);
54 static void broadcast_jobs(node_job_t *jobs);
55 static node_job_t wait_job(int selfid);
56 static void broadcast_matrix(xbt_matrix_t M, int num_nodes, int *nodes);
57 static void get_sub_matrix(xbt_matrix_t *sM, int selfid);
58 static void receive_results(result_t *results);
59 static void task_cleanup(void *arg);
60
61 int node(int argc, char **argv)
62 {
63   char my_mbox[MAILBOX_NAME_SIZE];
64   node_job_t myjob, jobs[GRID_NUM_NODES];
65   xbt_matrix_t A, B, C, sA, sB, sC;
66   result_t result;
67
68   xbt_assert(argc != 1, "Wrong number of arguments for this node");
69
70   /* Initialize the node's data-structures */
71   int myid = xbt_str_parse_int(argv[1], "Invalid ID received as first node parameter: %s");
72   snprintf(my_mbox, MAILBOX_NAME_SIZE - 1, "%d", myid);
73   sC = xbt_matrix_double_new_zeros(NODE_MATRIX_SIZE, NODE_MATRIX_SIZE);
74
75   if(myid == 0){
76     /* Create the matrices to multiply and one to store the result */
77     A = xbt_matrix_double_new_id(MATRIX_SIZE, MATRIX_SIZE);
78     B = xbt_matrix_double_new_seq(MATRIX_SIZE, MATRIX_SIZE);
79     C = xbt_matrix_double_new_zeros(MATRIX_SIZE, MATRIX_SIZE);
80
81     /* Create the nodes' jobs */
82     create_jobs(A, B, jobs);
83
84     /* Get own job first */
85     myjob = jobs[0];
86
87     /* Broadcast the rest of the jobs to the other nodes */
88     broadcast_jobs(jobs + 1);
89
90   } else {
91     A = B = C = NULL;           /* Avoid warning at compilation */
92     myjob = wait_job(myid);
93   }
94
95   /* Multiplication main-loop */
96   XBT_VERB("Start Multiplication's Main-loop");
97   for (int k=0; k < GRID_SIZE; k++){
98     if(k == myjob->col){
99       XBT_VERB("Broadcast sA(%d,%d) to row %d", myjob->row, k, myjob->row);
100       broadcast_matrix(myjob->A, NEIGHBOURS_COUNT, myjob->nodes_in_row);
101     }
102
103     if(k == myjob->row){
104       XBT_VERB("Broadcast sB(%d,%d) to col %d", k, myjob->col, myjob->col);
105       broadcast_matrix(myjob->B, NEIGHBOURS_COUNT, myjob->nodes_in_col);
106     }
107
108     if(myjob->row == k && myjob->col == k){
109       xbt_matrix_double_addmult(myjob->A, myjob->B, sC);
110     }else if(myjob->row == k){
111       get_sub_matrix(&sA, myid);
112       xbt_matrix_double_addmult(sA, myjob->B, sC);
113       xbt_matrix_free(sA);
114     }else if(myjob->col == k){
115       get_sub_matrix(&sB, myid);
116       xbt_matrix_double_addmult(myjob->A, sB, sC);
117       xbt_matrix_free(sB);
118     }else{
119       get_sub_matrix(&sA, myid);
120       get_sub_matrix(&sB, myid);
121       xbt_matrix_double_addmult(sA, sB, sC);
122       xbt_matrix_free(sA);
123       xbt_matrix_free(sB);
124     }
125   }
126
127   /* Node 0: gather the results and reconstruct the final matrix */
128   if(myid == 0){
129     int node;
130     result_t results[GRID_NUM_NODES] = {0};
131
132     XBT_VERB("Multiplication done.");
133
134     /* Get the result from the nodes in the GRID */
135     receive_results(results);
136
137     /* First add our results */
138     xbt_matrix_copy_values(C, sC, NODE_MATRIX_SIZE, NODE_MATRIX_SIZE, 0, 0, 0, 0, NULL);
139
140     /* Reconstruct the rest of the result matrix */
141     for (node = 1; node < GRID_NUM_NODES; node++){
142       xbt_matrix_copy_values(C, results[node]->sC, NODE_MATRIX_SIZE, NODE_MATRIX_SIZE,
143                              NODE_MATRIX_SIZE * results[node]->row, NODE_MATRIX_SIZE * results[node]->col,
144                              0, 0, NULL);
145       xbt_matrix_free(results[node]->sC);
146       xbt_free(results[node]);
147     }
148
149     //xbt_matrix_dump(C, "C:res", 0, xbt_matrix_dump_display_double);
150
151     xbt_matrix_free(A);
152     xbt_matrix_free(B);
153     xbt_matrix_free(C);
154
155   /* The rest: return the result to node 0 */
156   }else{
157     msg_task_t task;
158
159     XBT_VERB("Multiplication done. Send the sub-result.");
160
161     result = xbt_new0(s_result_t, 1);
162     result->row = myjob->row;
163     result->col = myjob->col;
164     result->sC = xbt_matrix_new_sub(sC, NODE_MATRIX_SIZE, NODE_MATRIX_SIZE, 0, 0, NULL);
165     task = MSG_task_create("result",100,100,result);
166     MSG_task_send(task, "0");
167   }
168
169   /* Clean up and finish*/
170   xbt_matrix_free(sC);
171   xbt_matrix_free(myjob->A);
172   xbt_matrix_free(myjob->B);
173   xbt_free(myjob);
174   return 0;
175 }
176
177 /*
178  * Broadcast the jobs to the nodes of the grid (except to node 0)
179  */
180 static void broadcast_jobs(node_job_t *jobs)
181 {
182   char node_mbox[MAILBOX_NAME_SIZE];
183   msg_comm_t comms[GRID_NUM_NODES - 1] = {0};
184
185   XBT_VERB("Broadcast Jobs");
186   for (int node = 1; node < GRID_NUM_NODES; node++){
187     msg_task_t task  = MSG_task_create("Job", 100, 100, jobs[node-1]);
188     snprintf(node_mbox, MAILBOX_NAME_SIZE - 1, "%d", node);
189     comms[node-1] = MSG_task_isend(task, node_mbox);
190   }
191
192   MSG_comm_waitall(comms, GRID_NUM_NODES-1, -1);
193   for (int node = 1; node < GRID_NUM_NODES; node++)
194     MSG_comm_destroy(comms[node - 1]);
195 }
196
197 static node_job_t wait_job(int selfid)
198 {
199   msg_task_t task = NULL;
200   char self_mbox[MAILBOX_NAME_SIZE];
201   snprintf(self_mbox, MAILBOX_NAME_SIZE - 1, "%d", selfid);
202   msg_error_t err = MSG_task_receive(&task, self_mbox);
203   xbt_assert(err == MSG_OK, "Error while receiving from %s (%d)", self_mbox, (int)err);
204   node_job_t job  = (node_job_t)MSG_task_get_data(task);
205   MSG_task_destroy(task);
206   XBT_VERB("Got Job (%d,%d)", job->row, job->col);
207
208   return job;
209 }
210
211 static void broadcast_matrix(xbt_matrix_t M, int num_nodes, int *nodes)
212 {
213   char node_mbox[MAILBOX_NAME_SIZE];
214
215   for(int node=0; node < num_nodes; node++){
216     snprintf(node_mbox, MAILBOX_NAME_SIZE - 1, "%d", nodes[node]);
217     xbt_matrix_t sM  = xbt_matrix_new_sub(M, NODE_MATRIX_SIZE, NODE_MATRIX_SIZE, 0, 0, NULL);
218     msg_task_t task = MSG_task_create("sub-matrix", 100, 100, sM);
219     MSG_task_dsend(task, node_mbox, task_cleanup);
220     XBT_DEBUG("sub-matrix sent to %s", node_mbox);
221   }
222 }
223
224 static void get_sub_matrix(xbt_matrix_t *sM, int selfid)
225 {
226   msg_task_t task = NULL;
227   char node_mbox[MAILBOX_NAME_SIZE];
228
229   XBT_VERB("Get sub-matrix");
230
231   snprintf(node_mbox, MAILBOX_NAME_SIZE - 1, "%d", selfid);
232   msg_error_t err = MSG_task_receive(&task, node_mbox);
233   xbt_assert(err == MSG_OK, "Error while receiving from %s (%d)", node_mbox, (int)err);
234   *sM = (xbt_matrix_t)MSG_task_get_data(task);
235   MSG_task_destroy(task);
236 }
237
238 static void task_cleanup(void *arg){
239   msg_task_t task = (msg_task_t)arg;
240   xbt_matrix_t m = (xbt_matrix_t)MSG_task_get_data(task);
241   xbt_matrix_free(m);
242   MSG_task_destroy(task);
243 }
244
245 int main(int argc, char *argv[])
246 {
247   xbt_os_timer_t timer = xbt_os_timer_new();
248
249   MSG_init(&argc, argv);
250   MSG_create_environment(argv[1]);
251
252   MSG_function_register("node", node);
253   for(int i = 0 ; i< 9; i++) {
254     char *hostname = bprintf("node-%d.acme.org", i);
255     char **argvF = xbt_new(char *, 3);
256     argvF[0] = xbt_strdup("node");
257     argvF[1] = bprintf("%d", i);
258     argvF[2] = NULL;
259     MSG_process_create_with_arguments("node", node, NULL, MSG_host_by_name(hostname), 2, argvF);
260     xbt_free(hostname);
261   }
262
263   xbt_os_cputimer_start(timer);
264   msg_error_t res = MSG_main();
265   xbt_os_cputimer_stop(timer);
266   XBT_CRITICAL("Simulated time: %g", MSG_get_clock());
267
268   return res != MSG_OK;
269 }
270
271 static void create_jobs(xbt_matrix_t A, xbt_matrix_t B, node_job_t *jobs)
272 {
273   int row = 0, col = 0;
274
275   for (int node = 0; node < GRID_NUM_NODES; node++){
276     XBT_VERB("Create job %d", node);
277     jobs[node] = xbt_new0(s_node_job_t, 1);
278     jobs[node]->row = row;
279     jobs[node]->col = col;
280
281     /* Compute who are the nodes in the same row and column */
282     /* than the node receiving this job */
283     for (int j = 0, k = 0; j < GRID_SIZE; j++) {
284       if (node != (GRID_SIZE * row) + j) {
285         jobs[node]->nodes_in_row[k] = (GRID_SIZE * row) + j;
286         k++;
287       }
288     }
289
290     for (int j = 0, k = 0; j < GRID_SIZE; j++) {
291       if (node != (GRID_SIZE * j) + col) {
292         jobs[node]->nodes_in_col[k] = (GRID_SIZE * j) + col;
293         k++;
294       }
295     }
296
297     /* Assign a sub matrix of A and B to the job */
298     jobs[node]->A =
299       xbt_matrix_new_sub(A, NODE_MATRIX_SIZE, NODE_MATRIX_SIZE, NODE_MATRIX_SIZE * row, NODE_MATRIX_SIZE * col, NULL);
300     jobs[node]->B =
301       xbt_matrix_new_sub(B, NODE_MATRIX_SIZE, NODE_MATRIX_SIZE, NODE_MATRIX_SIZE * row, NODE_MATRIX_SIZE * col, NULL);
302
303     if (++col >= GRID_SIZE){
304       col = 0;
305       row++;
306     }
307   }
308 }
309
310 static void receive_results(result_t *results) {
311   msg_comm_t comms[GRID_NUM_NODES-1] = {0};
312   msg_task_t tasks[GRID_NUM_NODES-1] = {0};
313
314   XBT_VERB("Receive Results.");
315
316   /* Get the result from the nodes in the GRID */
317   for (int node = 1; node < GRID_NUM_NODES; node++)
318    comms[node-1] = MSG_task_irecv(&tasks[node-1], "0");
319
320   MSG_comm_waitall(comms, GRID_NUM_NODES - 1, -1);
321   for (int node = 1; node < GRID_NUM_NODES; node++)
322     MSG_comm_destroy(comms[node - 1]);
323
324   /* Reconstruct the result matrix */
325   for (int node = 1; node < GRID_NUM_NODES; node++){
326     results[node] = (result_t)MSG_task_get_data(tasks[node-1]);
327     MSG_task_destroy(tasks[node-1]);
328   }
329 }