1 /* Copyright (c) 2010-2015. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
8 #include "simgrid/msg.h" /* Yeah! If you want to use msg, you need to include simgrid/msg.h */
9 #include "xbt/sysdep.h" /* calloc, printf */
11 /* Create a log channel to have nice outputs. */
13 #include "xbt/asserts.h"
14 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
15 "Messages specific for this msg example");
17 int master(int argc, char *argv[]);
18 int slave(int argc, char *argv[]);
19 int master_mpi(int argc, char *argv[]);
20 int alltoall_mpi(int argc, char *argv[]);
22 /** sender function */
23 int master(int argc, char *argv[])
25 long number_of_tasks = xbt_str_parse_int(argv[1], "Invalid amount of tasks: %s");
26 double task_comp_size = xbt_str_parse_double(argv[2], "Invalid computational size: %s");
27 double task_comm_size = xbt_str_parse_double(argv[3], "Invalid communication size: %s");
28 long slaves_count = xbt_str_parse_int(argv[4], "Invalid amount of slaves: %s");
32 XBT_INFO("Got %ld slaves and %ld tasks to process", slaves_count, number_of_tasks);
34 for (i = 0; i < number_of_tasks; i++) {
36 char sprintf_buffer[256];
37 msg_task_t task = NULL;
39 sprintf(mailbox, "slave-%ld", i % slaves_count);
40 sprintf(sprintf_buffer, "Task_%d", i);
42 MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size,
44 if (number_of_tasks < 10000 || i % 10000 == 0)
45 XBT_INFO("Sending \"%s\" (of %ld) to mailbox \"%s\"", task->name,
46 number_of_tasks, mailbox);
48 MSG_task_send(task, mailbox);
52 ("All tasks have been dispatched. Let's tell everybody the computation is over.");
53 for (i = 0; i < slaves_count; i++) {
56 sprintf(mailbox, "slave-%ld", i % slaves_count);
57 msg_task_t finalize = MSG_task_create("finalize", 0, 0, 0);
58 MSG_task_send(finalize, mailbox);
61 // XBT_INFO("Goodbye now!");
66 int master_mpi(int argc, char *argv[])
68 MPI_Init(&argc, &argv);
71 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
72 XBT_INFO("here for rank %d", rank);
73 int test[1000]={rank};
76 MPI_INT, 1, 1, MPI_COMM_WORLD);
79 MPI_INT, 0, 1, MPI_COMM_WORLD, MPI_STATUSES_IGNORE);
81 XBT_INFO("After comm %d", rank);
84 XBT_INFO("After finalize %d %d", rank, test[0]);
90 int alltoall_mpi(int argc, char *argv[])
92 MPI_Init(&argc, &argv);
95 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
96 MPI_Comm_size(MPI_COMM_WORLD, &size);
97 XBT_INFO("alltoall for rank %d", rank);
98 int* out=malloc(1000*size*sizeof(int));
99 int* in=malloc(1000*size*sizeof(int));
100 MPI_Alltoall(out, 1000, MPI_INT,in, 1000, MPI_INT, MPI_COMM_WORLD);
102 XBT_INFO("after alltoall %d", rank);
107 } /* end_of_master */
110 /** Receiver function */
111 int slave(int argc, char *argv[])
113 msg_task_t task = NULL;
114 XBT_ATTRIB_UNUSED int res;
117 XBT_ATTRIB_UNUSED int read;
119 read = sscanf(argv[1], "%d", &id);
120 xbt_assert(read, "Invalid argument %s\n", argv[1]);
122 sprintf(mailbox, "slave-%d", id);
125 res = MSG_task_receive(&(task), mailbox);
126 xbt_assert(res == MSG_OK, "MSG_task_get failed");
128 // XBT_INFO("Received \"%s\"", MSG_task_get_name(task));
129 if (!strcmp(MSG_task_get_name(task), "finalize")) {
130 MSG_task_destroy(task);
133 // XBT_INFO("Processing \"%s\"", MSG_task_get_name(task));
134 MSG_task_execute(task);
135 // XBT_INFO("\"%s\" done", MSG_task_get_name(task));
136 MSG_task_destroy(task);
139 XBT_INFO("I'm done. See you!");
145 int main(int argc, char *argv[])
148 const char *platform_file;
149 const char *application_file;
151 MSG_init(&argc, argv);
154 printf("Usage: %s platform_file deployment_file\n", argv[0]);
155 printf("example: %s msg_platform.xml msg_deployment.xml\n", argv[0]);
158 platform_file = argv[1];
159 application_file = argv[2];
161 { /* Simulation setting */
162 MSG_create_environment(platform_file);
164 { /* Application deployment */
165 MSG_function_register("master", master);
166 MSG_function_register("slave", slave);
167 // launch two MPI applications as well, one using master_mpi function as main on 2 nodes
168 SMPI_app_instance_register("master_mpi", master_mpi,2);
169 // the second performing an alltoall on 4 nodes
170 SMPI_app_instance_register("alltoall_mpi", alltoall_mpi,4);
171 MSG_launch_application(application_file);
176 XBT_INFO("Simulation time %g", MSG_get_clock());