TrioCFD 1.9.8
TrioCFD documentation
Loading...
Searching...
No Matches
My_Comm_Group.cpp
1/****************************************************************************
2* Copyright (c) 2026, CEA
3* All rights reserved.
4*
5* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8* 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
9*
10* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
11* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
12* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
13*
14*****************************************************************************/
15
16#include <Comm_Group_MPI.h>
17#include <My_Comm_Group.h>
18#include <Comm_Group.h>
19#include <PE_Groups.h>
20#include <Motcle.h>
21#include <Param.h>
22//#include <Probleme_base.h>
23
24Implemente_instanciable(My_Comm_Group, "My_Comm_Group", Interprete);
25// XD My_Comm_Group interprete My_Comm_Group BRACE This keyword allows to create a user MPI Comm Group of size N using
26// XD_CONT the processors allocated to TRUST. The set of processors is split in N subsets.
27
28static OWN_PTR(Comm_Group) my_comm_group;
29
31
33
34//void test()
35//{
36// Cerr << "TESTING CLASS My_Comm_Group" << finl;
37// Cerr << finl;
38// Process::barrier();
39//
40// // get pb
41// if (Interprete::objet_existant("pb"))
42// {
43// const Probleme_base& pb=ref_cast(Probleme_base, Interprete::objet("pb"));
44// const int nb_elem = pb.domaine().nb_elem();
45// const int nb_procs = Process::nproc();
46//
47// if (PE_Groups::enter_group(PE_Groups::get_user_defined_group()))
48// {
49// Cerr << "Process::mp_sum(nb_elem) on group = " << Process::mp_sum(nb_elem) << finl;
50// Process::barrier();
51// std::cerr << "Proc local : " << Process::me() << " , proc global : " << PE_Groups::groupe_TRUST().rank() << " , nb_elem : " << nb_elem << std::endl;
52//
53// PE_Groups::exit_group();
54// }
55//
56// Cerr << "@@@@@@@@@@ GLOBAL : Process::mp_sum(nb_elem) = " << Process::mp_sum(nb_elem) << finl;
57// Cerr << finl;
58// Process::barrier();
59//
60// std::vector<int> global_nb_elem;
61// global_nb_elem.assign(nb_procs, -123 /* default */);
62// const Comm_Group_MPI& comm = ref_cast(Comm_Group_MPI, PE_Groups::groupe_TRUST());
63//
64// MPI_Allgather(&nb_elem, 1, MPI_ENTIER, global_nb_elem.data(), 1, MPI_ENTIER, comm.get_mpi_comm());
65//
66// Cerr << "@@@@@@@@@@ GLOBAL : gather all nb_elem in global_nb_elem : "<< finl;
67// for (auto& itr : global_nb_elem)
68// Cerr << " - " << itr << finl;
69//
70// Cerr << finl;
71// Process::barrier();
72//
73// std::vector<int> global_nb_elem2;
74//
75// Cerr << "Testing local gather all nb_elem on group " << finl;
76// const auto& grp = PE_Groups::get_user_defined_group();
77// if (PE_Groups::enter_group(grp))
78// {
79// global_nb_elem2.assign(Process::nproc(), -123 /* default */);
80// const Comm_Group_MPI& comm_loc = ref_cast(Comm_Group_MPI, grp);
81//
82// MPI_Allgather(&nb_elem, 1, MPI_ENTIER, global_nb_elem2.data(), 1, MPI_ENTIER, comm_loc.get_mpi_comm());
83//
84// for (auto& itr : global_nb_elem2)
85// Cerr << " - " << itr << finl;
86//
87// PE_Groups::exit_group();
88// }
89// Cerr << finl;
90//
91// Process::barrier();
92// Process::exit();
93// }
94//}
95
97{
98 int nb_groups = -123;
99 Param param(que_suis_je());
100 param.ajouter("Group_nb", &nb_groups, Param::REQUIRED); // XD_ADD_P entier
101 // XD_CONT Number of groups to define in your Comm Group.
103
105 return is; /* rien */
106
107#ifndef MPI_
108 Process::exit("What !!! You need an MPI TRUST version to use My_Comm_Group !!!");
109#endif
110
111 assert(!my_comm_group);
112
113 const int nb_procs = Process::nproc();
114 const int rank = Process::me();
115
116 if (nb_groups > nb_procs)
117 nb_groups = nb_procs; // sinon
118
119 const int base_size = nb_procs / nb_groups;
120 const int extra = nb_procs % nb_groups; // nombre des procs avec 1 de plus !
121
122 // ex : 0,1,2,3 4,5,6,7 8,9,10
123 // Total = 11 procs
124
125 int count = 0;
126 for (int i = 0; i < nb_groups; i++)
127 {
128 const int group_size = base_size + (i < extra ? 1 : 0); // le 1 de plus envoyer aux premiers !!!
129
130 if (rank >= count && rank < count + group_size)
131 {
132 ArrOfInt tab(group_size);
133 for (int j = 0; j < group_size; j++)
134 tab[j] = count + j;
135
136 PE_Groups::create_group(tab, my_comm_group);
137 PE_Groups::initialize_user_defined_group(my_comm_group.valeur());
138 break; // le groupe a été trouvé, pas besoin de continuer
139 }
140
141 count += group_size;
142 }
143
144// test();
145
146 return is;
147}
148
150{
151 assert(my_comm_group);
152 return my_comm_group.valeur();
153}
: Cette classe decrit un groupe de processeurs sur lesquels
Definition Comm_Group.h:40
Class defining operators and methods for all reading operation in an input flow (file,...
Definition Entree.h:42
Classe de base des objets "interprete".
Definition Interprete.h:38
Entree & interpreter(Entree &) override
static const Comm_Group & get_my_comm_group()
friend class Entree
Definition Objet_U.h:76
const Nom & que_suis_je() const
renvoie la chaine identifiant la classe.
Definition Objet_U.cpp:104
virtual Entree & readOn(Entree &)
Lecture d'un Objet_U sur un flot d'entree Methode a surcharger.
Definition Objet_U.cpp:293
virtual Sortie & printOn(Sortie &) const
Ecriture de l'objet sur un flot de sortie Methode a surcharger.
Definition Objet_U.cpp:282
static void initialize_user_defined_group(const Comm_Group &ngrp)
static void create_group(const ArrOfInt &liste_pe, OWN_PTR(Comm_Group) &group, int force_Comm_Group_NoParallel=0)
Creation d'un nouveau groupe de processeurs (utilisation possible n'importe ou dans le code).
Definition PE_Groups.cpp:53
Helper class to factorize the readOn method of Objet_U classes.
Definition Param.h:112
void ajouter(const char *keyword, const int *value, Param::Nature nat=Param::OPTIONAL)
Register an integer parameter.
Definition Param.cpp:364
@ REQUIRED
Definition Param.h:115
int lire_avec_accolades_depuis(Entree &is)
Parse the parameter block { ... } from is.
Definition Param.cpp:32
static int nproc()
renvoie le nombre de processeurs dans le groupe courant Voir Comm_Group::nproc() et PE_Groups::curren...
Definition Process.cpp:104
static int me()
renvoie mon rang dans le groupe de communication courant.
Definition Process.cpp:125
static void exit(int exit_code=-1)
Routine de sortie de TRUST dans une region Kokkos.
Definition Process.cpp:455
static bool is_sequential()
Definition Process.cpp:115
Classe de base des flux de sortie.
Definition Sortie.h:52