Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Simple MPI_Win_lock and MPI_Win_unlock implementation.
[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
13 static xbt_dict_t smpi_instances = nullptr;
14 extern int process_count;
15 extern int* index_to_process_data;
16
17 typedef struct s_smpi_mpi_instance{
18   const char* name;
19   int size;
20   int present_processes;
21   int index;
22   MPI_Comm comm_world;
23   msg_bar_t finalization_barrier;
24 } s_smpi_mpi_instance_t;
25
26 /** \ingroup smpi_simulation
27  * \brief Registers a running instance of a MPI program.
28  *
29  * FIXME : remove MSG from the loop at some point.
30  * \param name the reference name of the function.
31  * \param code the main mpi function (must have a int ..(int argc, char *argv[])) prototype
32  * \param num_processes the size of the instance we want to deploy
33  */
34 void SMPI_app_instance_register(const char *name, xbt_main_func_t code, int num_processes)
35 {
36   SIMIX_function_register(name, code);
37
38   s_smpi_mpi_instance_t* instance = (s_smpi_mpi_instance_t*)xbt_malloc(sizeof(s_smpi_mpi_instance_t));
39
40   instance->name = name;
41   instance->size = num_processes;
42   instance->present_processes = 0;
43   instance->index = process_count;
44   instance->comm_world = MPI_COMM_NULL;
45   instance->finalization_barrier = MSG_barrier_init(num_processes);
46
47   process_count+=num_processes;
48
49   if(smpi_instances==nullptr){
50     smpi_instances = xbt_dict_new_homogeneous(xbt_free_f);
51   }
52
53   xbt_dict_set(smpi_instances, name, (void*)instance, nullptr);
54 }
55
56 //get the index of the process in the process_data array
57 void smpi_deployment_register_process(const char* instance_id, int rank, int index)
58 {
59
60   if(smpi_instances==nullptr){//no instance registered, we probably used smpirun.
61     index_to_process_data[index]=index;
62     return;
63   }
64
65   s_smpi_mpi_instance_t* instance =
66      static_cast<s_smpi_mpi_instance_t*>(xbt_dict_get_or_null(smpi_instances, instance_id));
67   xbt_assert(instance, "Error, unknown instance %s", instance_id);
68
69   if(instance->comm_world == MPI_COMM_NULL){
70     MPI_Group group = new  Group(instance->size);
71     instance->comm_world = new  Comm(group, nullptr);
72   }
73   instance->present_processes++;
74   index_to_process_data[index]=instance->index+rank;
75   instance->comm_world->group()->set_mapping(index, rank);
76 }
77
78 //get the index of the process in the process_data array
79 MPI_Comm* smpi_deployment_comm_world(const char* instance_id)
80 {
81   if(smpi_instances==nullptr){//no instance registered, we probably used smpirun.
82     return nullptr;
83   }
84   s_smpi_mpi_instance_t* instance =
85      static_cast<s_smpi_mpi_instance_t*>(xbt_dict_get_or_null(smpi_instances, instance_id));
86   xbt_assert(instance, "Error, unknown instance %s", instance_id);
87   return &instance->comm_world;
88 }
89
90 msg_bar_t smpi_deployment_finalization_barrier(const char* instance_id)
91 {
92   if(smpi_instances==nullptr){//no instance registered, we probably used smpirun.
93     return nullptr;
94   }
95   s_smpi_mpi_instance_t* instance =
96      static_cast<s_smpi_mpi_instance_t*>(xbt_dict_get_or_null(smpi_instances, instance_id));
97   xbt_assert(instance, "Error, unknown instance %s", instance_id);
98   return instance->finalization_barrier;
99 }
100
101 void smpi_deployment_cleanup_instances(){
102   xbt_dict_cursor_t cursor = nullptr;
103   s_smpi_mpi_instance_t* instance = nullptr;
104   char *name = nullptr;
105   xbt_dict_foreach(smpi_instances, cursor, name, instance) {
106     if(instance->comm_world!=MPI_COMM_NULL)
107       delete instance->comm_world->group();
108     delete instance->comm_world;
109     MSG_barrier_destroy(instance->finalization_barrier);
110   }
111   xbt_dict_free(&smpi_instances);
112 }