1 /* Copyright (c) 2004-2018. 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. */
7 #include "smpi_host.hpp"
9 #include "simgrid/s4u/Engine.hpp"
10 #include "smpi_comm.hpp"
19 Instance(const std::string name, int max_no_processes, int process_count, MPI_Comm comm,
20 simgrid::s4u::Barrier* finalization_barrier)
22 , size(max_no_processes)
23 , present_processes(0)
25 , finalization_barrier(finalization_barrier)
28 const std::string name;
30 int present_processes;
32 simgrid::s4u::Barrier* finalization_barrier;
38 using simgrid::smpi::app::Instance;
40 static std::map<std::string, Instance> smpi_instances;
41 extern int process_count; // How many processes have been allocated over all instances?
43 /** @ingroup smpi_simulation
44 * @brief Registers a running instance of a MPI program.
46 * @param name the reference name of the function.
47 * @param code either the main mpi function
48 * (must have a int ..(int argc, char *argv[]) prototype) or nullptr
49 * (if the function deployment is managed somewhere else —
50 * e.g., when deploying manually or using smpirun)
51 * @param num_processes the size of the instance we want to deploy
53 void SMPI_app_instance_register(const char *name, xbt_main_func_t code, int num_processes)
55 if (code != nullptr) { // When started with smpirun, we will not execute a function
56 simgrid::s4u::Engine::get_instance()->register_function(name, code);
59 static int already_called = 0;
60 if (not already_called) {
62 std::vector<simgrid::s4u::Host*> list = simgrid::s4u::Engine::get_instance()->get_all_hosts();
63 for (auto const& host : list) {
64 host->extension_set(new simgrid::smpi::Host(host));
68 Instance instance(std::string(name), num_processes, process_count, MPI_COMM_NULL,
69 new simgrid::s4u::Barrier(num_processes));
70 MPI_Group group = new simgrid::smpi::Group(instance.size);
71 instance.comm_world = new simgrid::smpi::Comm(group, nullptr);
72 // FIXME : using MPI_Attr_put with MPI_UNIVERSE_SIZE is forbidden and we make it a no-op (which triggers a warning as MPI_ERR_ARG is returned).
73 // Directly calling Comm::attr_put breaks for now, as MPI_UNIVERSE_SIZE,is <0
74 // instance.comm_world->attr_put<simgrid::smpi::Comm>(MPI_UNIVERSE_SIZE, reinterpret_cast<void*>(instance.size));
76 process_count+=num_processes;
78 smpi_instances.insert(std::pair<std::string, Instance>(name, instance));
81 void smpi_deployment_register_process(const std::string instance_id, int rank, simgrid::s4u::ActorPtr actor)
83 Instance& instance = smpi_instances.at(instance_id);
85 instance.present_processes++;
86 instance.comm_world->group()->set_mapping(actor, rank);
89 MPI_Comm* smpi_deployment_comm_world(const std::string instance_id)
91 if (smpi_instances.empty()) { // no instance registered, we probably used smpirun.
94 Instance& instance = smpi_instances.at(instance_id);
95 return &instance.comm_world;
98 simgrid::s4u::Barrier* smpi_deployment_finalization_barrier(const std::string instance_id)
100 if (smpi_instances.empty()) { // no instance registered, we probably used smpirun.
103 Instance& instance = smpi_instances.at(instance_id);
104 return instance.finalization_barrier;
107 void smpi_deployment_cleanup_instances(){
108 for (auto const& item : smpi_instances) {
109 Instance instance = item.second;
110 delete instance.finalization_barrier;
111 simgrid::smpi::Comm::destroy(instance.comm_world);
113 smpi_instances.clear();