1 /* Copyright (c) 2004-2017. 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 "SmpiHost.hpp"
9 #include "simgrid/msg.h" /* barrier */
10 #include "simgrid/s4u/Engine.hpp"
11 #include "smpi_comm.hpp"
20 Instance(const char* name, int max_no_processes, int process_count, MPI_Comm comm, msg_bar_t finalization_barrier)
22 , size(max_no_processes)
23 , present_processes(0)
25 , finalization_barrier(finalization_barrier)
30 int present_processes;
32 msg_bar_t 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 * FIXME : remove MSG from the loop at some point.
47 * \param name the reference name of the function.
48 * \param code the main mpi function (must have a int ..(int argc, char *argv[])) prototype
49 * \param num_processes the size of the instance we want to deploy
51 void SMPI_app_instance_register(const char *name, xbt_main_func_t code, int num_processes)
53 if (code != nullptr) { // When started with smpirun, we will not execute a function
54 SIMIX_function_register(name, code);
57 static int already_called = 0;
58 if (not already_called) {
60 std::vector<simgrid::s4u::Host*> list;
61 simgrid::s4u::Engine::getInstance()->getHostList(&list);
62 for (auto const& host : list) {
63 host->extension_set(new simgrid::smpi::SmpiHost(host));
67 Instance instance(name, num_processes, process_count, MPI_COMM_NULL, MSG_barrier_init(num_processes));
68 MPI_Group group = new simgrid::smpi::Group(instance.size);
69 instance.comm_world = new simgrid::smpi::Comm(group, nullptr);
70 MPI_Attr_put(instance.comm_world, MPI_UNIVERSE_SIZE, reinterpret_cast<void*>(instance.size));
72 process_count+=num_processes;
74 smpi_instances.insert(std::pair<std::string, Instance>(name, instance));
77 void smpi_deployment_register_process(const char* instance_id, int rank, simgrid::s4u::ActorPtr actor)
79 if (smpi_instances.empty()) // no instance registered, we probably used smpirun.
82 Instance& instance = smpi_instances.at(instance_id);
84 instance.present_processes++;
85 instance.comm_world->group()->set_mapping(actor, rank);
88 MPI_Comm* smpi_deployment_comm_world(const char* instance_id)
90 if (smpi_instances.empty()) { // no instance registered, we probably used smpirun.
93 Instance& instance = smpi_instances.at(instance_id);
94 return &instance.comm_world;
97 msg_bar_t smpi_deployment_finalization_barrier(const char* instance_id)
99 if (smpi_instances.empty()) { // no instance registered, we probably used smpirun.
102 Instance& instance = smpi_instances.at(instance_id);
103 return instance.finalization_barrier;
106 void smpi_deployment_cleanup_instances(){
107 for (auto const& item : smpi_instances) {
108 Instance instance = item.second;
109 MSG_barrier_destroy(instance.finalization_barrier);
110 simgrid::smpi::Comm::destroy(instance.comm_world);
112 smpi_instances.clear();