TRUST 1.9.8
HPC thermohydraulic platform
Loading...
Searching...
No Matches
communications_array.h
1/****************************************************************************
2* Copyright (c) 2025, 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#ifndef communications_array_included
17#define communications_array_included
18
19#include <type_traits>
20#include <Comm_Group.h>
21#include <PE_Groups.h>
22#include <arch.h>
23
24// Ces methodes sont volontairement separees de communications.h car plus dangereuses a l'usage.
25// Les tableaux passes en parametres doivent absolument avoir au moins n cases et n doit etre
26// identique sur tous les processeurs qui communiquent entre eux.
27
28// Attention: le template ne marche que pour les types simples (pas Objet_U !)
29// On ne passe pas par un buffer Entree / Sortie mais on envoie directement le tableau sous sa forme binaire.
30// Un seul message envoye, sauf en mode check() ou on envoie aussi la taille pour verifier.
31template<typename _TYPE_> std::enable_if_t<std::is_arithmetic<_TYPE_>::value,bool >
32inline envoyer_array(const _TYPE_ *objet, int n, int source, int cible, int canal)
33{
35 const int moi = grp.rank();
36 if (moi == cible)
37 return false;
38
39 if (source != moi && source != -1)
40 return false;
41
42 const char *data = (const char*) objet;
43 const int sz = (int)sizeof(_TYPE_) * n;
44 if (cible < 0)
45 {
46 const int nbproc = grp.nproc();
47 for (int i = 0; i < nbproc; i++)
48 {
49 if (i != moi)
50 {
51 // En mode check, on verifie que n est bien le meme sur l'expediteur et le recepteur
52 if (grp.check_enabled())
53 grp.send(i, &sz, (int)sizeof(int), canal);
54 if (sz > 0)
55 grp.send(i, data, sz, canal);
56 }
57 }
58 }
59 else
60 {
61 if (grp.check_enabled())
62 grp.send(cible, &sz, (int)sizeof(int), canal);
63 if (sz > 0)
64 grp.send(cible, data, sz, canal);
65 }
66 return true;
67}
68
69#ifndef INT_is_64_
70inline bool envoyer_array(const long *t, int n, int source, int cible, int canal)
71{
72 return envoyer_array<long>(t,n,source,cible,canal);
73}
74#endif
75
76template<typename _TYPE_> std::enable_if_t<std::is_arithmetic<_TYPE_>::value,bool >
77inline recevoir_array(const _TYPE_ *objet, int n, int source, int cible, int canal)
78{
80 const int moi = grp.rank();
81 if (moi == source)
82 return false;
83
84 if (cible != moi && cible != -1)
85 return false;
86
87 const int sz = (int)sizeof(_TYPE_) * n;
88 if (grp.check_enabled())
89 {
90 int sz_check;
91 grp.recv(source, &sz_check, (int)sizeof(int), canal);
92 if (sz_check != sz)
93 {
94 Cerr << "Fatal error in template<typename _TYPE_> int recevoir_array : incorrect size\n" << " sent=" << sz_check << " expected=" << sz << finl;
96 }
97 }
98 char *data = (char*) objet;
99 if (sz > 0)
100 grp.recv(source, data, sz, canal);
101 return true;
102}
103
104#ifndef INT_is_64_
105inline bool recevoir_array(const long *t, int n, int source, int cible, int canal)
106{
107 return recevoir_array<long>(t, n, source, cible, canal);
108}
109#endif
110
111template<typename _TYPE_> std::enable_if_t<std::is_arithmetic<_TYPE_>::value,bool >
112inline envoyer_broadcast_array(_TYPE_ *objet, int n, int source)
113{
115 if (grp.check_enabled())
116 {
117 int copie_n = n;
118 grp.broadcast(&copie_n, (int)sizeof(int), source);
119 if (copie_n != n)
120 {
121 Cerr << "Error in template<typename _TYPE_> envoyer_broadcast_array !" << finl;
123 }
124 }
125 grp.broadcast(objet, (int)sizeof(_TYPE_) * n, source);
126 return true;
127}
128
129#ifndef INT_is_64_
130inline bool envoyer_broadcast_array(long *t, int n, int source)
131{
132 return envoyer_broadcast_array<long>(t, n, source);
133}
134#endif
135
136#endif /* communications_array_included */
: Cette classe decrit un groupe de processeurs sur lesquels
Definition Comm_Group.h:40
static int check_enabled()
Definition Comm_Group.h:159
virtual void send(int pe, const void *buffer, int size, int tag) const =0
int nproc() const
Renvoie le nombre de processeurs dans le groupe *this.
Definition Comm_Group.h:190
int rank() const
Renvoie le rang du processeur local dans le groupe *this.
Definition Comm_Group.h:182
virtual void broadcast(void *buffer, int size, int pe_source) const =0
virtual void recv(int pe, void *buffer, int size, int tag) const =0
static const Comm_Group & current_group()
renvoie une reference au groupe de processeurs actif courant
Definition PE_Groups.h:65
static void exit(int exit_code=-1)
Routine de sortie de TRUST dans une region Kokkos.
Definition Process.cpp:455