TrioCFD 1.9.9_beta
TrioCFD documentation
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// These methods are intentionally separated from communications.h as they are more dangerous to use.
25// The arrays passed as parameters must have at least n elements, and n must be
26// identical on all processors communicating with each other.
27
28// Warning: the template only works for simple types (not Objet_U!)
29// We do not go through an Entree/Sortie buffer but send the array directly in binary form.
30// A single message is sent, except in check() mode where the size is also sent for verification.
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 // In check mode, we verify that n is the same on both sender and receiver
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 */
: This class describes a group of processors on which
Definition Comm_Group.h:37
static int check_enabled()
Definition Comm_Group.h:154
virtual void send(int pe, const void *buffer, int size, int tag) const =0
int nproc() const
Returns the number of processors in the group *this.
Definition Comm_Group.h:185
int rank() const
Returns the rank of the local processor in the group *this.
Definition Comm_Group.h:177
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()
Returns a reference to the current active processor group.
Definition PE_Groups.h:64
static void exit(int exit_code=-1)
Exit routine for TRUST within a Kokkos region.
Definition Process.cpp:466