comparison src/Function.cc @ 256:8fe68d94ab76

Add parallel assembly of matrices and vectors
author Eugenio Gianniti <eugenio.gianniti@mail.polimi.it>
date Wed, 30 Jul 2014 20:59:37 +0200
parents 5e9b5bbdc56b
children 61830a4f9ab9
comparison
equal deleted inserted replaced
255:072aee55bb75 256:8fe68d94ab76
17 17
18 #include <dolfin.h> 18 #include <dolfin.h>
19 #include "functionspace.h" 19 #include "functionspace.h"
20 #include "function.h" 20 #include "function.h"
21 #include "dolfin_compat.h" 21 #include "dolfin_compat.h"
22 #ifdef LATEST_DOLFIN
23 #include <boost/mpi.hpp>
24 #include <boost/serialization/vector.hpp>
25 #include <boost/serialization/utility.hpp>
26 #endif
27
28 void copy_vector (Array <double> const&, SHARED_PTR <dolfin::GenericVector>);
22 29
23 DEFUN_DLD (Function, args, , "-*- texinfo -*-\n\ 30 DEFUN_DLD (Function, args, , "-*- texinfo -*-\n\
24 @deftypefn {Function File} {[@var{func}]} = \ 31 @deftypefn {Function File} {[@var{func}]} = \
25 Function (@var{name}, @var{FunctionSpace} (or @var{Function}),\ 32 Function (@var{name}, @var{FunctionSpace} (or @var{Function}),\
26 @var{Vector} (or @var{index}))\n\ 33 @var{Vector} (or @var{index}))\n\
88 if (V->dim () != myu.length ()) 95 if (V->dim () != myu.length ())
89 error("The size of the functional space \ 96 error("The size of the functional space \
90 and of the vector must agree"); 97 and of the vector must agree");
91 else 98 else
92 { 99 {
93 #ifdef LATEST_DOLFIN 100 dolfin::Function aux (V);
94 dolfin::Vector du (MPI_COMM_WORLD, myu.length ()); 101 copy_vector (myu, aux.vector ());
95 #else 102
96 dolfin::Vector du(myu.length ()); 103 SHARED_PTR <dolfin::Function const>
97 #endif 104 u (new dolfin::Function (aux));
98 for (std::size_t i = 0; i < myu.length (); ++i)
99 du.setitem (i, myu(i));
100
101 SHARED_PTR <dolfin::Vector>
102 uu (new dolfin::Vector(du));
103
104 SHARED_PTR <const dolfin::Function>
105 u (new dolfin::Function(V, uu));
106
107 retval = new function (str, u); 105 retval = new function (str, u);
108 } 106 }
109 } 107 }
110 } 108 }
111 else if (args(1).type_id () == function::static_type_id ()) 109 else if (args(1).type_id () == function::static_type_id ())
138 } 136 }
139 } 137 }
140 } 138 }
141 return retval; 139 return retval;
142 } 140 }
141
142 void
143 copy_vector (Array <double> const& arr, SHARED_PTR <dolfin::GenericVector> vec)
144 {
145 unsigned const size =
146 #ifdef LATEST_DOLFIN
147 dolfin::MPI::size (MPI_COMM_WORLD);
148 #else
149 dolfin::MPI::num_processes ();
150 #endif
151
152 if (size > 1)
153 {
154 std::vector <double> locvals;
155 boost::mpi::communicator world;
156 unsigned const rank = world.rank ();
157
158 std::pair <std::size_t, std::size_t> const loc_range =
159 vec->local_range ();
160
161 for (unsigned p = 1; p < size; ++p)
162 if (rank == p)
163 world.send (0, p, loc_range);
164
165 if (rank == 0)
166 {
167 for (unsigned p = 0; p < size; ++p)
168 {
169 std::pair <std::size_t, std::size_t> range;
170 if (p == 0)
171 range = loc_range;
172 else
173 world.recv (p, p, range);
174
175 std::vector <double> entries;
176 for (std::size_t i = range.first; i < range.second; ++i)
177 entries.push_back (arr(i));
178
179 if (p == 0)
180 locvals = entries;
181 else
182 world.send (p, p, entries);
183 }
184 }
185
186 for (unsigned p = 1; p < size; ++p)
187 {
188 if (rank == p)
189 world.recv (0, p, locvals);
190 }
191
192 vec->set_local (locvals);
193 }
194 else
195 for (std::size_t i = 0; i < arr.length (); ++i)
196 vec->setitem (i, arr(i));
197
198 vec->apply ("insert");
199
200 return;
201 }