Logo AND Algorithmique Numérique Distribuée

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