Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'oldstyle_element_set'
[simgrid.git] / examples / smpi / smpi_msg_masterslave / masterslave_mailbox_smpi.c
1 /* Copyright (c) 2010-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #include <stdio.h>
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 */
10 #include "mpi.h"
11 /* Create a log channel to have nice outputs. */
12 #include "xbt/log.h"
13 #include "xbt/asserts.h"
14 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
15                              "Messages specific for this msg example");
16
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[]);
21
22 /** sender function  */
23 int master(int argc, char *argv[])
24 {
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");
29
30   int i;
31
32   XBT_INFO("Got %ld slaves and %ld tasks to process", slaves_count, number_of_tasks);
33
34   for (i = 0; i < number_of_tasks; i++) {
35     char mailbox[256];
36     char sprintf_buffer[256];
37     msg_task_t task = NULL;
38
39     sprintf(mailbox, "slave-%ld", i % slaves_count);
40     sprintf(sprintf_buffer, "Task_%d", i);
41     task =
42         MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size,
43                         NULL);
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);
47
48     MSG_task_send(task, mailbox);
49   }
50
51   XBT_INFO
52       ("All tasks have been dispatched. Let's tell everybody the computation is over.");
53   for (i = 0; i < slaves_count; i++) {
54     char mailbox[80];
55
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);
59   }
60
61 //  XBT_INFO("Goodbye now!");
62   return 0;
63 }                               /* end_of_master */
64
65
66 int master_mpi(int argc, char *argv[])
67 {
68   MPI_Init(&argc, &argv);
69
70   int rank;
71   MPI_Comm_rank(MPI_COMM_WORLD, &rank);
72   XBT_INFO("here for rank %d", rank);
73   int test[1000]={rank};
74   if(rank==0)
75         MPI_Send(&test, 1000, 
76                  MPI_INT, 1, 1, MPI_COMM_WORLD); 
77   else
78         MPI_Recv(&test, 1000, 
79                  MPI_INT, 0, 1, MPI_COMM_WORLD, MPI_STATUSES_IGNORE); 
80
81   XBT_INFO("After comm %d", rank);
82   MPI_Finalize();
83
84   XBT_INFO("After finalize %d %d", rank, test[0]);
85   return 0;
86 }                               /* end_of_master */
87
88
89
90 int alltoall_mpi(int argc, char *argv[])
91 {
92   MPI_Init(&argc, &argv);
93
94   int rank, size;
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); 
101
102   XBT_INFO("after alltoall %d", rank);
103   free(out);
104   free(in);
105   MPI_Finalize();
106   return 0;
107 }                               /* end_of_master */
108
109
110 /** Receiver function  */
111 int slave(int argc, char *argv[])
112 {
113   msg_task_t task = NULL;
114   XBT_ATTRIB_UNUSED int res;
115   int id = -1;
116   char mailbox[80];
117   XBT_ATTRIB_UNUSED int read;
118
119   read = sscanf(argv[1], "%d", &id);
120   xbt_assert(read, "Invalid argument %s\n", argv[1]);
121
122   sprintf(mailbox, "slave-%d", id);
123
124   while (1) {
125     res = MSG_task_receive(&(task), mailbox);
126     xbt_assert(res == MSG_OK, "MSG_task_get failed");
127
128 //  XBT_INFO("Received \"%s\"", MSG_task_get_name(task));
129     if (!strcmp(MSG_task_get_name(task), "finalize")) {
130       MSG_task_destroy(task);
131       break;
132     }
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);
137     task = NULL;
138   }
139   XBT_INFO("I'm done. See you!");
140
141   return 0;
142 }                               /* end_of_slave */
143
144 /** Main function */
145 int main(int argc, char *argv[])
146 {
147   msg_error_t res;
148   const char *platform_file;
149   const char *application_file;
150
151   MSG_init(&argc, argv);
152
153   if (argc < 3) {
154     printf("Usage: %s platform_file deployment_file\n", argv[0]);
155     printf("example: %s msg_platform.xml msg_deployment.xml\n", argv[0]);
156     exit(1);
157   }
158   platform_file = argv[1];
159   application_file = argv[2];
160
161   {                             /*  Simulation setting */
162     MSG_create_environment(platform_file);
163   }
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);
172     SMPI_init();
173   }
174   res = MSG_main();
175
176   XBT_INFO("Simulation time %g", MSG_get_clock());
177
178
179   SMPI_finalize();
180   if (res == MSG_OK)
181     return 0;
182   else
183     return 1;
184 }                               /* end_of_main */