Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Make xbt_free a function-like macro.
[simgrid.git] / src / smpi / smpi_deployment.c
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 "xbt/sysdep.h"
9 #include "xbt/synchro_core.h"
10 #include "xbt/log.h"
11 #include "xbt/dict.h"
12
13 static xbt_dict_t smpi_instances = NULL;
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   xbt_bar_t finalization_barrier;
24 } s_smpi_mpi_instance_t;
25
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 the same prototype than the main function of any C program: int ..(int argc, char *argv[]))
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 =
40       (s_smpi_mpi_instance_t*)xbt_malloc(sizeof(s_smpi_mpi_instance_t));
41
42   instance->name = name;
43   instance->size = num_processes;
44   instance->present_processes = 0;
45   instance->index = process_count;
46   instance->comm_world = MPI_COMM_NULL;
47   instance->finalization_barrier=xbt_barrier_init(num_processes);
48
49   process_count+=num_processes;
50
51   if(!smpi_instances){
52     smpi_instances = xbt_dict_new_homogeneous(xbt_free_f);
53   }
54
55   xbt_dict_set(smpi_instances, name, (void*)instance, NULL);
56   return;
57 }
58
59
60 //get the index of the process in the process_data array
61 void smpi_deployment_register_process(const char* instance_id, int rank, int index,MPI_Comm** comm, xbt_bar_t* bar){
62
63   if(!smpi_instances){//no instance registered, we probably used smpirun.
64     index_to_process_data[index]=index;
65     *bar = NULL;
66     *comm = NULL;
67     return;
68   }
69
70   s_smpi_mpi_instance_t* instance = xbt_dict_get_or_null(smpi_instances, instance_id);
71   if (!instance)
72     xbt_die("Error, unknown instance %s", instance_id);
73
74   if(instance->comm_world == MPI_COMM_NULL){
75     MPI_Group group = smpi_group_new(instance->size);
76     instance->comm_world = smpi_comm_new(group, NULL);
77   }
78   instance->present_processes++;
79   index_to_process_data[index]=instance->index+rank;
80   smpi_group_set_mapping(smpi_comm_group(instance->comm_world), index, rank);
81   *bar = instance->finalization_barrier;
82   *comm = &instance->comm_world;
83   return;
84 }
85
86 void smpi_deployment_cleanup_instances(){
87   xbt_dict_cursor_t cursor = NULL;
88   s_smpi_mpi_instance_t* instance = NULL;
89   char *name = NULL;
90   xbt_dict_foreach(smpi_instances, cursor, name, instance) {
91     while (smpi_group_unuse(smpi_comm_group(instance->comm_world)) > 0);
92     xbt_free(instance->comm_world);
93     xbt_barrier_destroy(instance->finalization_barrier);
94   }
95   xbt_dict_free(&smpi_instances);
96 }