Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot/simgrid/simgrid
[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/log.h"
10 #include "xbt/dict.h"
11
12 static xbt_dict_t smpi_instances = NULL;
13 extern int process_count;
14 extern int* index_to_process_data;
15
16 typedef struct s_smpi_mpi_instance{
17   const char* name;
18   int size;
19   int present_processes;
20   int index;
21   MPI_Comm comm_world;
22 } s_smpi_mpi_instance_t;
23
24
25 /** \ingroup smpi_simulation
26  * \brief Registers a running instance of a MPI program.
27  *
28  * FIXME : remove MSG from the loop at some point.
29  * \param name the reference name of the function.
30  * \param code the main mpi function (must have the same prototype than the main function of any C program: int ..(int argc, char *argv[]))
31  * \param num_processes the size of the instance we want to deploy
32  */
33 void SMPI_app_instance_register(const char *name, xbt_main_func_t code, int num_processes)
34 {
35   SIMIX_function_register(name, code);
36
37   s_smpi_mpi_instance_t* instance =
38       (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   process_count+=num_processes;
46
47   if(!smpi_instances){
48       smpi_instances=xbt_dict_new_homogeneous(xbt_free);
49   }
50
51   xbt_dict_set(smpi_instances, name, (void*)instance, NULL);
52   return;
53 }
54
55
56 //get the index of the process in the process_data array
57 MPI_Comm* smpi_deployment_register_process(const char* instance_id, int rank, int index){
58
59   if(!smpi_instances){//no instance registered, we probably used smpirun.
60       index_to_process_data[index]=index;
61       return NULL;
62   }
63
64   s_smpi_mpi_instance_t* instance = xbt_dict_get_or_null(smpi_instances, instance_id);
65   if (!instance)
66     xbt_die("Error, unknown instance %s", instance_id);
67
68   if(instance->comm_world == MPI_COMM_NULL){
69     MPI_Group group = smpi_group_new(instance->size);
70     instance->comm_world = smpi_comm_new(group, NULL);
71   }
72   instance->present_processes++;
73   index_to_process_data[index]=instance->index+rank;
74   smpi_group_set_mapping(smpi_comm_group(instance->comm_world), index, rank);
75   return & instance->comm_world;
76 }