1
/*------------------------------------------------------------------------*/
2
/* phdMesh : Parallel Heterogneous Dynamic unstructured Mesh */
3
/* Copyright (2008) Sandia Corporation */
5
/* Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive */
6
/* license for use of this work by or on behalf of the U.S. Government. */
8
/* This library is free software; you can redistribute it and/or modify */
9
/* it under the terms of the GNU Lesser General Public License as */
10
/* published by the Free Software Foundation; either version 2.1 of the */
11
/* License, or (at your option) any later version. */
13
/* This library is distributed in the hope that it will be useful, */
14
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
15
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU */
16
/* Lesser General Public License for more details. */
18
/* You should have received a copy of the GNU Lesser General Public */
19
/* License along with this library; if not, write to the Free Software */
20
/* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 */
22
/*------------------------------------------------------------------------*/
24
* @author H. Carter Edwards
27
#ifndef phdmesh_FieldParallel_hpp
28
#define phdmesh_FieldParallel_hpp
30
//----------------------------------------------------------------------
32
#include <util/SimpleArrayOps.hpp>
33
#include <util/Parallel.hpp>
34
#include <util/ParallelComm.hpp>
36
#include <mesh/Types.hpp>
37
#include <mesh/Field.hpp>
38
#include <mesh/Entity.hpp>
39
#include <mesh/BulkData.hpp>
43
/** Communicate field data from domain to range.
44
* The fields array must be identical on all processors.
45
* All fields and mesh entities must belong to the same mesh.
46
* If symmetric ( & domain == & range) then from owned to not owned.
48
bool communicate_field_data(
49
const BulkData & mesh ,
50
const std::vector<EntityProc> & domain ,
51
const std::vector<EntityProc> & range ,
52
const std::vector< const FieldBase *> & fields ,
53
bool local_flag = false );
55
/** Communicate field data given symmetric communication plan. */
56
void communicate_field_data(
58
const std::vector<EntityProc> & ,
59
const unsigned field_count ,
60
const FieldBase * fields[] ,
63
//----------------------------------------------------------------------
67
//----------------------------------------------------------------------
68
/** Parallel reduction of shared entities' field data.
69
* In the anonymous namespace to avoid redundant link symbols.
72
* parallel_reduce( mesh , sum( field ) );
74
* where the operations are: sum, max, min
76
template< class OpField >
77
void parallel_reduce( const BulkData & mesh ,
80
const FieldBase * fields[1] = { & op.field };
82
const std::vector<EntityProc> & shared = mesh.shared_entities();
86
communicate_field_data( mesh.parallel(), shared, 1, fields, sparse );
88
op( shared , sparse );
91
/** Parallel reduction of shared entities' field data.
92
* In the anonymous namespace to avoid redundant link symbols.
95
* parallel_reduce( mesh , sum( fieldA ) , max( fieldB ) );
97
template< class OpField1 , class OpField2 >
98
void parallel_reduce( const BulkData & mesh ,
99
const OpField1 & op1 ,
100
const OpField2 & op2 )
102
const FieldBase * fields[2] = { & op1.field , & op2.field };
104
const std::vector<EntityProc> & shared = mesh.shared_entities();
108
communicate_field_data( mesh.parallel(), shared, 2, fields, sparse );
110
op1( shared , sparse );
111
op2( shared , sparse );
114
//----------------------------------------------------------------------
116
template< class ReduceOp ,
117
class Type , class Tag1, class Tag2, class Tag3 ,
118
class Tag4 , class Tag5, class Tag6, class Tag7 >
119
struct ParallelReduceField {
120
typedef Field<Type,Tag1,Tag2,Tag3,Tag4,Tag5,Tag6,Tag7> field_type ;
122
const field_type & field ;
124
ParallelReduceField( const field_type & f ) : field(f) {}
125
ParallelReduceField( const ParallelReduceField & p ) : field(p.field) {}
127
void operator()( const std::vector<EntityProc> & shared ,
128
CommAll & sparse ) const ;
131
ParallelReduceField & operator = ( const ParallelReduceField & );
134
template< class ReduceOp ,
135
class Type , class Tag1, class Tag2, class Tag3 ,
136
class Tag4 , class Tag5, class Tag6, class Tag7 >
137
void ParallelReduceField< ReduceOp , Type , Tag1, Tag2, Tag3 ,
138
Tag4 , Tag5, Tag6, Tag7 >::
139
operator()( const std::vector<EntityProc> & shared , CommAll & sparse ) const
141
typedef EntityArray< field_type > array_type ;
143
for ( std::vector<EntityProc>::const_iterator
144
i = shared.begin(); i != shared.end() ; ++i ) {
146
array_type array( field , * i->first );
147
Type * ptr = array.contiguous_data();
148
Type * const ptr_end = ptr + array.size();
150
CommBuffer & b = sparse.recv_buffer( i->second );
152
for ( ; ptr < ptr_end ; ++ptr ) {
154
b.template unpack<unsigned char>( (unsigned char *)(&tmp), sizeof(Type) );
155
ReduceOp::op( ptr , & tmp );
162
//----------------------------------------------------------------------
164
template< class Type , class Tag1, class Tag2, class Tag3 ,
165
class Tag4 , class Tag5, class Tag6, class Tag7 >
166
ParallelReduceField<impl::Sum<Type,1>,Type,Tag1,Tag2,Tag3,Tag4,Tag5,Tag6,Tag7>
168
sum( const Field<Type,Tag1,Tag2,Tag3,Tag4,Tag5,Tag6,Tag7> & f )
170
return ParallelReduceField<impl::Sum<Type,1>,
171
Type,Tag1,Tag2,Tag3,Tag4,Tag5,Tag6,Tag7>( f );
174
template< class Type , class Tag1, class Tag2, class Tag3 ,
175
class Tag4 , class Tag5, class Tag6, class Tag7 >
176
ParallelReduceField<impl::Max<Type,1>,Type,Tag1,Tag2,Tag3,Tag4,Tag5,Tag6,Tag7>
178
max( const Field<Type,Tag1,Tag2,Tag3,Tag4,Tag5,Tag6,Tag7> & f )
180
return ParallelReduceField<impl::Max<Type,1>,
181
Type,Tag1,Tag2,Tag3,Tag4,Tag5,Tag6,Tag7>( f );
184
template< class Type , class Tag1, class Tag2, class Tag3 ,
185
class Tag4 , class Tag5, class Tag6, class Tag7 >
186
ParallelReduceField<impl::Min<Type,1>,Type,Tag1,Tag2,Tag3,Tag4,Tag5,Tag6,Tag7>
188
min( const Field<Type,Tag1,Tag2,Tag3,Tag4,Tag5,Tag6,Tag7> & f )
190
return ParallelReduceField<impl::Min<Type,1>,
191
Type,Tag1,Tag2,Tag3,Tag4,Tag5,Tag6,Tag7>( f );