Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
67a0cea8baaa46ced09efe3a3ab23510ff10206c
[simgrid.git] / src / smpi / smpi_deployment.cpp
1 /* Copyright (c) 2004-2014. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #include "private.h"
8 #include "simgrid/msg.h" /* barrier */
9 #include "xbt/dict.h"
10 #include "xbt/log.h"
11 #include "xbt/sysdep.h"
12 #include <src/smpi/smpi_group.hpp>
13
14 static xbt_dict_t smpi_instances = nullptr;
15 extern int process_count;
16 extern int* index_to_process_data;
17
18 typedef struct s_smpi_mpi_instance{
19   const char* name;
20   int size;
21   int present_processes;
22   int index;
23   MPI_Comm comm_world;
24   msg_bar_t finalization_barrier;
25 } s_smpi_mpi_instance_t;
26
27 /** \ingroup smpi_simulation
28  * \brief Registers a running instance of a MPI program.
29  *
30  * FIXME : remove MSG from the loop at some point.
31  * \param name the reference name of the function.
32  * \param code the main mpi function (must have a int ..(int argc, char *argv[])) prototype
33  * \param num_processes the size of the instance we want to deploy
34  */
35 void SMPI_app_instance_register(const char *name, xbt_main_func_t code, int num_processes)
36 {
37   SIMIX_function_register(name, code);
38
39   s_smpi_mpi_instance_t* instance = (s_smpi_mpi_instance_t*)xbt_malloc(sizeof(s_smpi_mpi_instance_t));
40
41   instance->name = name;
42   instance->size = num_processes;
43   instance->present_processes = 0;
44   instance->index = process_count;
45   instance->comm_world = MPI_COMM_NULL;
46   instance->finalization_barrier = MSG_barrier_init(num_processes);
47
48   process_count+=num_processes;
49
50   if(smpi_instances==nullptr){
51     smpi_instances = xbt_dict_new_homogeneous(xbt_free_f);
52   }
53
54   xbt_dict_set(smpi_instances, name, (void*)instance, nullptr);
55 }
56
57 //get the index of the process in the process_data array
58 void smpi_deployment_register_process(const char* instance_id, int rank, int index, MPI_Comm** comm, msg_bar_t* bar)
59 {
60
61   if(smpi_instances==nullptr){//no instance registered, we probably used smpirun.
62     index_to_process_data[index]=index;
63     *bar = nullptr;
64     *comm = nullptr;
65     return;
66   }
67
68   s_smpi_mpi_instance_t* instance =
69      static_cast<s_smpi_mpi_instance_t*>(xbt_dict_get_or_null(smpi_instances, instance_id));
70   xbt_assert(instance, "Error, unknown instance %s", instance_id);
71
72   if(instance->comm_world == MPI_COMM_NULL){
73     MPI_Group group = new simgrid::SMPI::Group(instance->size);
74     instance->comm_world = new simgrid::SMPI::Comm(group, nullptr);
75   }
76   instance->present_processes++;
77   index_to_process_data[index]=instance->index+rank;
78   instance->comm_world->group()->set_mapping(index, rank);
79   *bar = instance->finalization_barrier;
80   *comm = &instance->comm_world;
81 }
82
83 void smpi_deployment_cleanup_instances(){
84   xbt_dict_cursor_t cursor = nullptr;
85   s_smpi_mpi_instance_t* instance = nullptr;
86   char *name = nullptr;
87   xbt_dict_foreach(smpi_instances, cursor, name, instance) {
88     if(instance->comm_world!=MPI_COMM_NULL)
89       while (instance->comm_world->group()->unuse() > 0);
90     xbt_free(instance->comm_world);
91     MSG_barrier_destroy(instance->finalization_barrier);
92   }
93   xbt_dict_free(&smpi_instances);
94 }