1 /* Copyright (c) 2004-2020. 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"
13 XBT_LOG_EXTERNAL_CATEGORY(smpi);
19 static int universe_size = 0;
23 Instance(int max_no_processes, MPI_Comm comm) : size_(max_no_processes), comm_world_(comm)
25 auto* group = new simgrid::smpi::Group(size_);
26 comm_world_ = new simgrid::smpi::Comm(group, nullptr, false, -1);
27 // FIXME : using MPI_Attr_put with MPI_UNIVERSE_SIZE is forbidden and we make it a no-op (which triggers a warning
28 // as MPI_ERR_ARG is returned). Directly calling Comm::attr_put breaks for now, as MPI_UNIVERSE_SIZE,is <0
29 // instance.comm_world->attr_put<simgrid::smpi::Comm>(MPI_UNIVERSE_SIZE, reinterpret_cast<void*>(instance.size));
31 universe_size += max_no_processes;
35 unsigned int finalized_ranks_ = 0;
42 using simgrid::smpi::app::Instance;
44 static std::map<std::string, Instance> smpi_instances;
46 /** @ingroup smpi_simulation
47 * @brief Registers a running instance of a MPI program.
49 * @param name the reference name of the function.
50 * @param code either the main mpi function
51 * (must have a int ..(int argc, char *argv[]) prototype) or nullptr
52 * (if the function deployment is managed somewhere else —
53 * e.g., when deploying manually or using smpirun)
54 * @param num_processes the size of the instance we want to deploy
56 void SMPI_app_instance_register(const char *name, xbt_main_func_t code, int num_processes)
58 if (code != nullptr) // When started with smpirun, we will not execute a function
59 simgrid::s4u::Engine::get_instance()->register_function(name, code);
61 Instance instance(num_processes, MPI_COMM_NULL);
63 smpi_instances.insert(std::pair<std::string, Instance>(name, instance));
66 void smpi_deployment_register_process(const std::string& instance_id, int rank, simgrid::s4u::Actor* actor)
68 const Instance& instance = smpi_instances.at(instance_id);
69 instance.comm_world_->group()->set_mapping(actor, rank);
72 void smpi_deployment_unregister_process(const std::string& instance_id)
74 Instance& instance = smpi_instances.at(instance_id);
75 instance.finalized_ranks_++;
77 if (instance.finalized_ranks_ == instance.size_) {
78 simgrid::smpi::Comm::destroy(instance.comm_world_);
79 smpi_instances.erase(instance_id);
83 MPI_Comm* smpi_deployment_comm_world(const std::string& instance_id)
86 .empty()) { // no instance registered, we probably used smpirun. (FIXME: I guess this never happens for real)
89 Instance& instance = smpi_instances.at(instance_id);
90 return &instance.comm_world_;
93 void smpi_deployment_cleanup_instances(){
94 for (auto const& item : smpi_instances) {
95 XBT_CINFO(smpi, "Stalling SMPI instance: %s. Do all your MPI ranks call MPI_Finalize()?", item.first.c_str());
96 Instance instance = item.second;
97 simgrid::smpi::Comm::destroy(instance.comm_world_);
99 smpi_instances.clear();
102 int smpi_get_universe_size()
104 return simgrid::smpi::app::universe_size;