1 /* Copyright (c) 2010-2014. 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 #include "smpi/private.h"
15 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
16 "Messages specific for this msg example");
18 int master(int argc, char *argv[]);
19 int slave(int argc, char *argv[]);
20 int master_mpi(int argc, char *argv[]);
21 int alltoall_mpi(int argc, char *argv[]);
23 /** Emitter function */
24 int master(int argc, char *argv[])
26 long number_of_tasks = atol(argv[1]);
27 double task_comp_size = atof(argv[2]);
28 double task_comm_size = atof(argv[3]);
29 long slaves_count = atol(argv[4]);
33 XBT_INFO("Got %ld slaves and %ld tasks to process", slaves_count,
36 for (i = 0; i < number_of_tasks; i++) {
38 char sprintf_buffer[256];
39 msg_task_t task = NULL;
41 sprintf(mailbox, "slave-%ld", i % slaves_count);
42 sprintf(sprintf_buffer, "Task_%d", i);
44 MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size,
46 if (number_of_tasks < 10000 || i % 10000 == 0)
47 XBT_INFO("Sending \"%s\" (of %ld) to mailbox \"%s\"", task->name,
48 number_of_tasks, mailbox);
50 MSG_task_send(task, mailbox);
54 ("All tasks have been dispatched. Let's tell everybody the computation is over.");
55 for (i = 0; i < slaves_count; i++) {
58 sprintf(mailbox, "slave-%ld", i % slaves_count);
59 msg_task_t finalize = MSG_task_create("finalize", 0, 0, 0);
60 MSG_task_send(finalize, mailbox);
63 // XBT_INFO("Goodbye now!");
68 int master_mpi(int argc, char *argv[])
70 MPI_Init(&argc, &argv);
73 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
74 XBT_INFO("here for rank %d", rank);
75 int test[1000]={rank};
78 MPI_INT, 1, 1, MPI_COMM_WORLD);
81 MPI_INT, 0, 1, MPI_COMM_WORLD, MPI_STATUSES_IGNORE);
83 XBT_INFO("After comm %d", rank);
86 XBT_INFO("After finalize %d %d", rank, test[0]);
92 int alltoall_mpi(int argc, char *argv[])
94 MPI_Init(&argc, &argv);
97 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
98 MPI_Comm_size(MPI_COMM_WORLD, &size);
99 XBT_INFO("alltoall for rank %d", rank);
100 int* out=malloc(1000*size*sizeof(int));
101 int* in=malloc(1000*size*sizeof(int));
102 MPI_Alltoall(out, 1000, MPI_INT,in, 1000, MPI_INT, MPI_COMM_WORLD);
104 XBT_INFO("after alltoall %d", rank);
109 } /* end_of_master */
112 /** Receiver function */
113 int slave(int argc, char *argv[])
115 msg_task_t task = NULL;
116 _XBT_GNUC_UNUSED int res;
119 _XBT_GNUC_UNUSED int read;
121 read = sscanf(argv[1], "%d", &id);
122 xbt_assert(read, "Invalid argument %s\n", argv[1]);
124 sprintf(mailbox, "slave-%d", id);
127 res = MSG_task_receive(&(task), mailbox);
128 xbt_assert(res == MSG_OK, "MSG_task_get failed");
130 // XBT_INFO("Received \"%s\"", MSG_task_get_name(task));
131 if (!strcmp(MSG_task_get_name(task), "finalize")) {
132 MSG_task_destroy(task);
135 // XBT_INFO("Processing \"%s\"", MSG_task_get_name(task));
136 MSG_task_execute(task);
137 // XBT_INFO("\"%s\" done", MSG_task_get_name(task));
138 MSG_task_destroy(task);
141 XBT_INFO("I'm done. See you!");
147 int main(int argc, char *argv[])
150 const char *platform_file;
151 const char *application_file;
153 MSG_init(&argc, argv);
156 printf("Usage: %s platform_file deployment_file\n", argv[0]);
157 printf("example: %s msg_platform.xml msg_deployment.xml\n", argv[0]);
160 platform_file = argv[1];
161 application_file = argv[2];
163 /* MSG_config("workstation/model","KCCFLN05"); */
164 { /* Simulation setting */
165 MSG_create_environment(platform_file);
167 { /* Application deployment */
168 MSG_function_register("master", master);
169 MSG_function_register("slave", slave);
170 // launch two MPI applications as well, one using master_mpi function as main on 2 nodes
171 SMPI_app_instance_register("master_mpi", master_mpi,2);
172 // the second performing an alltoall on 4 nodes
173 SMPI_app_instance_register("alltoall_mpi", alltoall_mpi,4);
174 MSG_launch_application(application_file);
179 XBT_INFO("Simulation time %g", MSG_get_clock());