Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use C++ types instead of c-style arrays.
[simgrid.git] / examples / smpi / smpi_s4u_masterworker / masterworker_mailbox_smpi.cpp
1 /* Copyright (c) 2010-2020. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "mpi.h"
7 #include "simgrid/s4u.hpp"
8
9 #include <array>
10 #include <cstdio> /* snprintf */
11
12 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, "Messages specific for this msg example");
13
14 static void master(std::vector<std::string> args)
15 {
16   xbt_assert(args.size() > 4, "The master function expects at least 3 arguments");
17
18   long tasks_count        = std::stol(args[1]);
19   double compute_cost     = std::stod(args[2]);
20   long communication_cost = std::stol(args[3]);
21   std::vector<simgrid::s4u::Mailbox*> workers;
22   for (unsigned int i = 4; i < args.size(); i++)
23     workers.push_back(simgrid::s4u::Mailbox::by_name(args[i]));
24
25   XBT_INFO("Got %zu workers and %ld tasks to process", workers.size(), tasks_count);
26
27   for (int i = 0; i < tasks_count; i++) { /* For each task to be executed: */
28     /* - Select a worker in a round-robin way */
29     simgrid::s4u::Mailbox* mailbox = workers[i % workers.size()];
30
31     /* - Send the computation cost to that worker */
32     XBT_INFO("Sending task %d of %ld to mailbox '%s'", i, tasks_count, mailbox->get_cname());
33     mailbox->put(new double(compute_cost), communication_cost);
34   }
35
36   XBT_INFO("All tasks have been dispatched. Request all workers to stop.");
37   for (unsigned int i = 0; i < workers.size(); i++) {
38     /* The workers stop when receiving a negative compute_cost */
39     simgrid::s4u::Mailbox* mailbox = workers[i % workers.size()];
40
41     mailbox->put(new double(-1.0), 0);
42   }
43 }
44
45 static void worker(std::vector<std::string> args)
46 {
47   xbt_assert(args.size() == 1, "The worker expects no argument");
48
49   const simgrid::s4u::Host* my_host = simgrid::s4u::this_actor::get_host();
50   simgrid::s4u::Mailbox* mailbox    = simgrid::s4u::Mailbox::by_name(my_host->get_name());
51
52   double compute_cost;
53   do {
54     const auto* msg   = static_cast<double*>(mailbox->get());
55     compute_cost      = *msg;
56     delete msg;
57
58     if (compute_cost > 0) /* If compute_cost is valid, execute a computation of that cost */
59       simgrid::s4u::this_actor::execute(compute_cost);
60   } while (compute_cost > 0); /* Stop when receiving an invalid compute_cost */
61
62   XBT_INFO("Exiting now.");
63 }
64
65 static void master_mpi(int argc, char* argv[])
66 {
67   MPI_Init(&argc, &argv);
68
69   int rank;
70   MPI_Comm_rank(MPI_COMM_WORLD, &rank);
71   XBT_INFO("here for rank %d", rank);
72   std::array<int, 1000> test{{rank}};
73   if (rank == 0)
74     MPI_Send(test.data(), 1000, MPI_INT, 1, 1, MPI_COMM_WORLD);
75   else
76     MPI_Recv(test.data(), 1000, MPI_INT, 0, 1, MPI_COMM_WORLD, MPI_STATUSES_IGNORE);
77
78   XBT_INFO("After comm %d", rank);
79   MPI_Finalize();
80
81   XBT_INFO("After finalize %d %d", rank, test[0]);
82 }
83
84 static void alltoall_mpi(int argc, char* argv[])
85 {
86   MPI_Init(&argc, &argv);
87
88   int rank;
89   int size;
90   MPI_Comm_rank(MPI_COMM_WORLD, &rank);
91   MPI_Comm_size(MPI_COMM_WORLD, &size);
92   XBT_INFO("alltoall for rank %d", rank);
93   std::vector<int> out(1000 * size);
94   std::vector<int> in(1000 * size);
95   MPI_Alltoall(out.data(), 1000, MPI_INT, in.data(), 1000, MPI_INT, MPI_COMM_WORLD);
96
97   XBT_INFO("after alltoall %d", rank);
98   MPI_Finalize();
99 }
100
101 int main(int argc, char* argv[])
102 {
103   simgrid::s4u::Engine e(&argc, argv);
104
105   SMPI_init();
106
107   xbt_assert(argc > 2,
108              "Usage: %s platform_file deployment_file\n"
109              "\nexample: %s msg_platform.xml msg_deployment.xml\n",
110              argv[0], argv[0]);
111
112   e.load_platform(argv[1]);
113
114   e.register_function("master", master);
115   e.register_function("worker", worker);
116   // launch two MPI applications as well, one using master_mpi function as main on 2 nodes
117   SMPI_app_instance_register("master_mpi", master_mpi, 2);
118   // the second performing an alltoall on 4 nodes
119   SMPI_app_instance_register("alltoall_mpi", alltoall_mpi, 4);
120   e.load_deployment(argv[2]);
121
122   e.run();
123
124   XBT_INFO("Simulation time %g", e.get_clock());
125
126   SMPI_finalize();
127   return 0;
128 }