1 /* Copyright (c) 2004-2014. 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. */
8 #include "simgrid/msg.h" /* barrier */
11 #include "xbt/sysdep.h"
13 static xbt_dict_t smpi_instances = nullptr;
14 extern int process_count;
15 extern int* index_to_process_data;
17 typedef struct s_smpi_mpi_instance{
20 int present_processes;
23 msg_bar_t finalization_barrier;
24 } s_smpi_mpi_instance_t;
26 /** \ingroup smpi_simulation
27 * \brief Registers a running instance of a MPI program.
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
34 void SMPI_app_instance_register(const char *name, xbt_main_func_t code, int num_processes)
36 SIMIX_function_register(name, code);
38 s_smpi_mpi_instance_t* instance = (s_smpi_mpi_instance_t*)xbt_malloc(sizeof(s_smpi_mpi_instance_t));
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);
47 process_count+=num_processes;
49 if(smpi_instances==nullptr){
50 smpi_instances = xbt_dict_new_homogeneous(xbt_free_f);
53 xbt_dict_set(smpi_instances, name, (void*)instance, nullptr);
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, MPI_Comm** comm, msg_bar_t* bar)
60 if(smpi_instances==nullptr){//no instance registered, we probably used smpirun.
61 index_to_process_data[index]=index;
67 s_smpi_mpi_instance_t* instance =
68 static_cast<s_smpi_mpi_instance_t*>(xbt_dict_get_or_null(smpi_instances, instance_id));
69 xbt_assert(instance, "Error, unknown instance %s", instance_id);
71 if(instance->comm_world == MPI_COMM_NULL){
72 MPI_Group group = smpi_group_new(instance->size);
73 instance->comm_world = smpi_comm_new(group, nullptr);
75 instance->present_processes++;
76 index_to_process_data[index]=instance->index+rank;
77 smpi_group_set_mapping(smpi_comm_group(instance->comm_world), index, rank);
78 *bar = instance->finalization_barrier;
79 *comm = &instance->comm_world;
82 void smpi_deployment_cleanup_instances(){
83 xbt_dict_cursor_t cursor = nullptr;
84 s_smpi_mpi_instance_t* instance = nullptr;
86 xbt_dict_foreach(smpi_instances, cursor, name, instance) {
87 if(instance->comm_world!=MPI_COMM_NULL)
88 while (smpi_group_unuse(smpi_comm_group(instance->comm_world)) > 0);
89 xbt_free(instance->comm_world);
90 MSG_barrier_destroy(instance->finalization_barrier);
92 xbt_dict_free(&smpi_instances);