Logo AND Algorithmique Numérique Distribuée

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