DOLFIN
DOLFIN C++ interface
LocalMeshValueCollection.h
1 // Copyright (C) 2008-2012 Garth N. Wells
2 //
3 // This file is part of DOLFIN.
4 //
5 // DOLFIN is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // DOLFIN is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
17 //
18 // Modified by Anders Logg, 2008-2009.
19 //
20 // First added: 2008-11-28
21 // Last changed: 2011-03-25
22 //
23 // Modified by Anders Logg, 2008-2009.
24 // Modified by Kent-Andre Mardal, 2011.
25 
26 
27 #ifndef __LOCAL_MESH_VALUE_COLLECTION_H
28 #define __LOCAL_MESH_VALUE_COLLECTION_H
29 
30 #include <map>
31 #include <utility>
32 #include <vector>
33 #include <dolfin/common/MPI.h>
34 #include <dolfin/log/log.h>
35 
36 namespace dolfin
37 {
38 
39  template <typename T> class MeshValueCollection;
40 
43 
44  template <typename T>
46  {
47  public:
48 
51  std::size_t dim);
52 
55 
57  std::size_t dim () const
58  { return _dim; }
59 
61  const std::vector<std::pair<std::pair<std::size_t,
62  std::size_t>, T> >& values() const
63  { return _values; }
64 
65  private:
66 
67  // Topological dimension
68  const std::size_t _dim;
69 
70  // MeshValueCollection values (cell_index, local_index), value))
71  std::vector<std::pair<std::pair<std::size_t, std::size_t>, T> > _values;
72 
73  // MPI communicator
74  dolfin::MPI::Comm _mpi_comm;
75 
76  };
77 
78  //---------------------------------------------------------------------------
79  // Implementation of LocalMeshValueCollection
80  //---------------------------------------------------------------------------
81  template <typename T>
84  std::size_t dim)
85  : _dim(dim), _mpi_comm(comm)
86  {
87  // Prepare data
88  std::vector<std::vector<std::size_t> > send_indices;
89  std::vector<std::vector<T> > send_v;
90 
91  // Extract data on main process and split among processes
92  if (MPI::is_broadcaster(_mpi_comm.comm()))
93  {
94  // Get number of processes
95  const std::size_t num_processes = MPI::size(_mpi_comm.comm());
96  send_indices.resize(num_processes);
97  send_v.resize(num_processes);
98 
99  const std::map<std::pair<std::size_t, std::size_t>, T>& vals
100  = values.values();
101  for (std::size_t p = 0; p < num_processes; p++)
102  {
103  const std::pair<std::size_t, std::size_t> local_range
104  = MPI::local_range(_mpi_comm.comm(), p, vals.size());
105  typename std::map<std::pair<std::size_t,
106  std::size_t>, T>::const_iterator it = vals.begin();
107  std::advance(it, local_range.first);
108  for (std::size_t i = local_range.first; i < local_range.second; ++i)
109  {
110  send_indices[p].push_back(it->first.first);
111  send_indices[p].push_back(it->first.second);
112  send_v[p].push_back(it->second);
113  std::advance(it, 1);
114  }
115  }
116  }
117 
118  // Scatter data
119  std::vector<std::size_t> indices;
120  std::vector<T> v;
121  MPI::scatter(_mpi_comm.comm(), send_indices, indices);
122  MPI::scatter(_mpi_comm.comm(), send_v, v);
123  dolfin_assert(2*v.size() == indices.size());
124 
125  // Unpack
126  for (std::size_t i = 0; i < v.size(); ++i)
127  {
128  const std::size_t cell_index = indices[2*i];
129  const std::size_t local_entity_index = indices[2*i + 1];
130  const T value = v[i];
131  _values.push_back({{cell_index, local_entity_index}, value});
132  }
133  }
134  //---------------------------------------------------------------------------
135 
136 }
137 
138 #endif
LocalMeshValueCollection(MPI_Comm comm, const MeshValueCollection< T > &values, std::size_t dim)
Create local mesh data for given LocalMeshValueCollection.
Definition: LocalMeshValueCollection.h:82
static void scatter(MPI_Comm comm, const std::vector< std::vector< T >> &in_values, std::vector< T > &out_value, unsigned int sending_process=0)
Scatter vector in_values[i] to process i.
Definition: MPI.h:499
std::map< std::pair< std::size_t, std::size_t >, T > & values()
Definition: MeshValueCollection.h:521
Definition: adapt.h:29
static unsigned int size(MPI_Comm comm)
Definition: MPI.cpp:154
std::size_t dim() const
Return dimension of cell entity.
Definition: LocalMeshValueCollection.h:57
MPI_Comm comm() const
Return the underlying MPI_Comm object.
Definition: MPI.cpp:117
const std::vector< std::pair< std::pair< std::size_t, std::size_t >, T > > & values() const
Return data.
Definition: LocalMeshValueCollection.h:62
static bool is_broadcaster(MPI_Comm comm)
Definition: MPI.cpp:166
~LocalMeshValueCollection()
Destructor.
Definition: LocalMeshValueCollection.h:54
Definition: GenericFile.h:38
static std::pair< std::int64_t, std::int64_t > local_range(MPI_Comm comm, std::int64_t N)
Definition: MPI.cpp:201
Definition: MPI.h:76
Definition: LocalMeshValueCollection.h:45