1 /* pmm - double broadcast parallel matrix multiplication */
3 /* Copyright (c) 2006-2016. The SimGrid Team. All rights reserved. */
5 /* This program is free software; you can redistribute it and/or modify it
6 * under the terms of the license (GNU LGPL) which comes with this package. */
8 #include "simgrid/msg.h"
9 #include "xbt/matrix.h"
10 #include "xbt/xbt_os_time.h"
12 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_pmm, "Messages specific for this msg example");
14 /* This example should always be executed using a deployment of GRID_SIZE * GRID_SIZE nodes. */
15 #define GRID_SIZE 3 /* Modify to adjust the grid's size */
16 #define NODE_MATRIX_SIZE 300 /* Amount of work done by each node*/
18 #define GRID_NUM_NODES GRID_SIZE * GRID_SIZE
19 #define MATRIX_SIZE NODE_MATRIX_SIZE * GRID_SIZE
20 #define MAILBOX_NAME_SIZE 10
21 #define NEIGHBOURS_COUNT GRID_SIZE - 1
24 * The job sent to every node
26 typedef struct s_node_job{
29 int nodes_in_row[NEIGHBOURS_COUNT];
30 int nodes_in_col[NEIGHBOURS_COUNT];
33 } s_node_job_t, *node_job_t;
36 * Structure for recovering results
38 typedef struct s_result {
42 } s_result_t, *result_t;
44 int node(int argc, char **argv);
45 static void create_jobs(xbt_matrix_t A, xbt_matrix_t B, node_job_t *jobs);
46 static void broadcast_jobs(node_job_t *jobs);
47 static node_job_t wait_job(int selfid);
48 static void broadcast_matrix(xbt_matrix_t M, int num_nodes, int *nodes);
49 static void get_sub_matrix(xbt_matrix_t *sM, int selfid);
50 static void receive_results(result_t *results);
51 static void task_cleanup(void *arg);
53 int node(int argc, char **argv)
55 char my_mbox[MAILBOX_NAME_SIZE];
56 node_job_t myjob, jobs[GRID_NUM_NODES];
57 xbt_matrix_t A, B, C, sA, sB, sC;
60 xbt_assert(argc != 1, "Wrong number of arguments for this node");
62 /* Initialize the node's data-structures */
63 int myid = xbt_str_parse_int(argv[1], "Invalid ID received as first node parameter: %s");
64 snprintf(my_mbox, MAILBOX_NAME_SIZE - 1, "%d", myid);
65 sC = xbt_matrix_double_new_zeros(NODE_MATRIX_SIZE, NODE_MATRIX_SIZE);
68 /* Create the matrices to multiply and one to store the result */
69 A = xbt_matrix_double_new_id(MATRIX_SIZE, MATRIX_SIZE);
70 B = xbt_matrix_double_new_seq(MATRIX_SIZE, MATRIX_SIZE);
71 C = xbt_matrix_double_new_zeros(MATRIX_SIZE, MATRIX_SIZE);
73 /* Create the nodes' jobs */
74 create_jobs(A, B, jobs);
76 /* Get own job first */
79 /* Broadcast the rest of the jobs to the other nodes */
80 broadcast_jobs(jobs + 1);
83 A = B = C = NULL; /* Avoid warning at compilation */
84 myjob = wait_job(myid);
87 /* Multiplication main-loop */
88 XBT_VERB("Start Multiplication's Main-loop");
89 for (int k=0; k < GRID_SIZE; k++){
91 XBT_VERB("Broadcast sA(%d,%d) to row %d", myjob->row, k, myjob->row);
92 broadcast_matrix(myjob->A, NEIGHBOURS_COUNT, myjob->nodes_in_row);
96 XBT_VERB("Broadcast sB(%d,%d) to col %d", k, myjob->col, myjob->col);
97 broadcast_matrix(myjob->B, NEIGHBOURS_COUNT, myjob->nodes_in_col);
100 if(myjob->row == k && myjob->col == k){
101 xbt_matrix_double_addmult(myjob->A, myjob->B, sC);
102 }else if(myjob->row == k){
103 get_sub_matrix(&sA, myid);
104 xbt_matrix_double_addmult(sA, myjob->B, sC);
106 }else if(myjob->col == k){
107 get_sub_matrix(&sB, myid);
108 xbt_matrix_double_addmult(myjob->A, sB, sC);
111 get_sub_matrix(&sA, myid);
112 get_sub_matrix(&sB, myid);
113 xbt_matrix_double_addmult(sA, sB, sC);
119 /* Node 0: gather the results and reconstruct the final matrix */
122 result_t results[GRID_NUM_NODES] = {0};
124 XBT_VERB("Multiplication done.");
126 /* Get the result from the nodes in the GRID */
127 receive_results(results);
129 /* First add our results */
130 xbt_matrix_copy_values(C, sC, NODE_MATRIX_SIZE, NODE_MATRIX_SIZE, 0, 0, 0, 0, NULL);
132 /* Reconstruct the rest of the result matrix */
133 for (node = 1; node < GRID_NUM_NODES; node++){
134 xbt_matrix_copy_values(C, results[node]->sC, NODE_MATRIX_SIZE, NODE_MATRIX_SIZE,
135 NODE_MATRIX_SIZE * results[node]->row, NODE_MATRIX_SIZE * results[node]->col,
137 xbt_matrix_free(results[node]->sC);
138 xbt_free(results[node]);
141 //xbt_matrix_dump(C, "C:res", 0, xbt_matrix_dump_display_double);
147 /* The rest: return the result to node 0 */
151 XBT_VERB("Multiplication done. Send the sub-result.");
153 result = xbt_new0(s_result_t, 1);
154 result->row = myjob->row;
155 result->col = myjob->col;
156 result->sC = xbt_matrix_new_sub(sC, NODE_MATRIX_SIZE, NODE_MATRIX_SIZE, 0, 0, NULL);
157 task = MSG_task_create("result",100,100,result);
158 MSG_task_send(task, "0");
161 /* Clean up and finish*/
163 xbt_matrix_free(myjob->A);
164 xbt_matrix_free(myjob->B);
170 * Broadcast the jobs to the nodes of the grid (except to node 0)
172 static void broadcast_jobs(node_job_t *jobs)
174 char node_mbox[MAILBOX_NAME_SIZE];
175 msg_comm_t comms[GRID_NUM_NODES - 1] = {0};
177 XBT_VERB("Broadcast Jobs");
178 for (int node = 1; node < GRID_NUM_NODES; node++){
179 msg_task_t task = MSG_task_create("Job", 100, 100, jobs[node-1]);
180 snprintf(node_mbox, MAILBOX_NAME_SIZE - 1, "%d", node);
181 comms[node-1] = MSG_task_isend(task, node_mbox);
184 MSG_comm_waitall(comms, GRID_NUM_NODES-1, -1);
185 for (int node = 1; node < GRID_NUM_NODES; node++)
186 MSG_comm_destroy(comms[node - 1]);
189 static node_job_t wait_job(int selfid)
191 msg_task_t task = NULL;
192 char self_mbox[MAILBOX_NAME_SIZE];
193 snprintf(self_mbox, MAILBOX_NAME_SIZE - 1, "%d", selfid);
194 msg_error_t err = MSG_task_receive(&task, self_mbox);
195 xbt_assert(err == MSG_OK, "Error while receiving from %s (%d)", self_mbox, (int)err);
196 node_job_t job = (node_job_t)MSG_task_get_data(task);
197 MSG_task_destroy(task);
198 XBT_VERB("Got Job (%d,%d)", job->row, job->col);
203 static void broadcast_matrix(xbt_matrix_t M, int num_nodes, int *nodes)
205 char node_mbox[MAILBOX_NAME_SIZE];
207 for(int node=0; node < num_nodes; node++){
208 snprintf(node_mbox, MAILBOX_NAME_SIZE - 1, "%d", nodes[node]);
209 xbt_matrix_t sM = xbt_matrix_new_sub(M, NODE_MATRIX_SIZE, NODE_MATRIX_SIZE, 0, 0, NULL);
210 msg_task_t task = MSG_task_create("sub-matrix", 100, 100, sM);
211 MSG_task_dsend(task, node_mbox, task_cleanup);
212 XBT_DEBUG("sub-matrix sent to %s", node_mbox);
216 static void get_sub_matrix(xbt_matrix_t *sM, int selfid)
218 msg_task_t task = NULL;
219 char node_mbox[MAILBOX_NAME_SIZE];
221 XBT_VERB("Get sub-matrix");
223 snprintf(node_mbox, MAILBOX_NAME_SIZE - 1, "%d", selfid);
224 msg_error_t err = MSG_task_receive(&task, node_mbox);
225 xbt_assert(err == MSG_OK, "Error while receiving from %s (%d)", node_mbox, (int)err);
226 *sM = (xbt_matrix_t)MSG_task_get_data(task);
227 MSG_task_destroy(task);
230 static void task_cleanup(void *arg){
231 msg_task_t task = (msg_task_t)arg;
232 xbt_matrix_t m = (xbt_matrix_t)MSG_task_get_data(task);
234 MSG_task_destroy(task);
237 int main(int argc, char *argv[])
239 xbt_os_timer_t timer = xbt_os_timer_new();
241 MSG_init(&argc, argv);
242 MSG_create_environment(argv[1]);
244 MSG_function_register("node", node);
245 for(int i = 0 ; i< 9; i++) {
246 char *hostname = bprintf("node-%d.acme.org", i);
247 char **argvF = xbt_new(char *, 3);
248 argvF[0] = xbt_strdup("node");
249 argvF[1] = bprintf("%d", i);
251 MSG_process_create_with_arguments("node", node, NULL, MSG_host_by_name(hostname), 2, argvF);
255 xbt_os_cputimer_start(timer);
256 msg_error_t res = MSG_main();
257 xbt_os_cputimer_stop(timer);
258 XBT_CRITICAL("Simulated time: %g", MSG_get_clock());
260 return res != MSG_OK;
263 static void create_jobs(xbt_matrix_t A, xbt_matrix_t B, node_job_t *jobs)
265 int row = 0, col = 0;
267 for (int node = 0; node < GRID_NUM_NODES; node++){
268 XBT_VERB("Create job %d", node);
269 jobs[node] = xbt_new0(s_node_job_t, 1);
270 jobs[node]->row = row;
271 jobs[node]->col = col;
273 /* Compute who are the nodes in the same row and column */
274 /* than the node receiving this job */
275 for (int j = 0, k = 0; j < GRID_SIZE; j++) {
276 if (node != (GRID_SIZE * row) + j) {
277 jobs[node]->nodes_in_row[k] = (GRID_SIZE * row) + j;
282 for (int j = 0, k = 0; j < GRID_SIZE; j++) {
283 if (node != (GRID_SIZE * j) + col) {
284 jobs[node]->nodes_in_col[k] = (GRID_SIZE * j) + col;
289 /* Assign a sub matrix of A and B to the job */
291 xbt_matrix_new_sub(A, NODE_MATRIX_SIZE, NODE_MATRIX_SIZE, NODE_MATRIX_SIZE * row, NODE_MATRIX_SIZE * col, NULL);
293 xbt_matrix_new_sub(B, NODE_MATRIX_SIZE, NODE_MATRIX_SIZE, NODE_MATRIX_SIZE * row, NODE_MATRIX_SIZE * col, NULL);
295 if (++col >= GRID_SIZE){
302 static void receive_results(result_t *results) {
303 msg_comm_t comms[GRID_NUM_NODES-1] = {0};
304 msg_task_t tasks[GRID_NUM_NODES-1] = {0};
306 XBT_VERB("Receive Results.");
308 /* Get the result from the nodes in the GRID */
309 for (int node = 1; node < GRID_NUM_NODES; node++)
310 comms[node-1] = MSG_task_irecv(&tasks[node-1], "0");
312 MSG_comm_waitall(comms, GRID_NUM_NODES - 1, -1);
313 for (int node = 1; node < GRID_NUM_NODES; node++)
314 MSG_comm_destroy(comms[node - 1]);
316 /* Reconstruct the result matrix */
317 for (int node = 1; node < GRID_NUM_NODES; node++){
318 results[node] = (result_t)MSG_task_get_data(tasks[node-1]);
319 MSG_task_destroy(tasks[node-1]);