Vector_eigen.hpp 10.9 KB
 Pietro Incardona committed Nov 30, 2015 1 2 3 4 5 6 7 8 9 10 ``````/* * Vector_eigen.hpp * * Created on: Nov 27, 2015 * Author: i-bird */ #ifndef OPENFPM_NUMERICS_SRC_VECTOR_VECTOR_EIGEN_HPP_ #define OPENFPM_NUMERICS_SRC_VECTOR_VECTOR_EIGEN_HPP_ `````` Pietro Incardona committed Dec 09, 2015 11 12 13 14 15 ``````#include #include "util/mul_array_extents.hpp" #include #include "Vector_eigen_util.hpp" #include "Grid/staggered_dist_grid_util.hpp" `````` Pietro Incardona committed Apr 07, 2016 16 17 ``````#include "Space/Ghost.hpp" #include "FiniteDifference/util/common.hpp" `````` Pietro Incardona committed Dec 09, 2015 18 ``````#include `````` Pietro Incardona committed Apr 07, 2016 19 ``````#include `````` Pietro Incardona committed Nov 30, 2015 20 `````` `````` Pietro Incardona committed Apr 07, 2016 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 ``````#define EIGEN_RVAL 1 /*! \brief It store one row value of a vector * * Given a row, store a value * * */ template class rval { // row long int r; // value T val; public: // Get the row long int & row() { return r; } // Get the value T & value() { return val; } /*! \brief Default constructor * */ rval() {} /*! \brief Constructor from row, colum and value * * \param i row * \param val value * */ rval(long int i, T val) { row() = i; value() = val; } }; `````` Pietro Incardona committed Nov 30, 2015 69 `````` `````` Pietro Incardona committed Dec 09, 2015 70 71 ``````template class Vector> `````` Pietro Incardona committed Nov 30, 2015 72 ``````{ `````` Pietro Incardona committed Apr 07, 2016 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 `````` mutable Eigen::Matrix v; // row value vector mutable openfpm::vector> row_val; mutable openfpm::vector> row_val_recv; // global to local map mutable std::unordered_map map; // invalid T invalid; // Processors from where we gather mutable openfpm::vector prc; //size of each chunk mutable openfpm::vector sz; `````` Pietro Incardona committed Dec 09, 2015 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 `````` /*! \brief Check that the size of the iterators match * * It check the the boxes that the sub iterator defines has same dimensions, for example * if the first sub-iterator, iterate from (1,1) to (5,3) and the second from (2,2) to (6,4) * they match (2,2) to (4,6) they do not match * * \tparam Grid_map type of the map grid * \tparam Grid_dst type of the destination grid * * \param it1 Iterator1 * \param it2 Iterator2 * * \return true if they match * */ template bool checkIterator(const it1_type & it1, const it2_type & it2) `````` Pietro Incardona committed Nov 30, 2015 107 `````` { `````` Pietro Incardona committed Dec 09, 2015 108 109 110 111 112 113 ``````#ifdef SE_CLASS1 grid_key_dx it1_k = it1.getStop() - it1.getStart(); grid_key_dx it2_k = it2.getStop() - it2.getStart(); for (size_t i = 0 ; i < Eqs_sys::dims ; i++) `````` Pietro Incardona committed Nov 30, 2015 114 `````` { `````` Pietro Incardona committed Dec 09, 2015 115 116 117 118 119 `````` if (it1_k.get(i) != it2_k.get(i)) { std::cerr << __FILE__ << ":" << __LINE__ << " error src iterator and destination iterator does not match in size\n"; return false; } `````` Pietro Incardona committed Nov 30, 2015 120 121 `````` } `````` Pietro Incardona committed Dec 09, 2015 122 123 `````` return true; #else `````` Pietro Incardona committed Nov 30, 2015 124 `````` `````` Pietro Incardona committed Dec 09, 2015 125 `````` return true; `````` Pietro Incardona committed Nov 30, 2015 126 `````` `````` Pietro Incardona committed Dec 09, 2015 127 128 ``````#endif } `````` Pietro Incardona committed Nov 30, 2015 129 130 `````` `````` Pietro Incardona committed Dec 09, 2015 131 `````` /*! \brief Copy in a staggered grid `````` Pietro Incardona committed Nov 30, 2015 132 133 134 `````` * * */ `````` Pietro Incardona committed Dec 09, 2015 135 136 137 138 `````` template void copy_staggered_to_staggered(scheme & sc,const long int (& start)[scheme::Sys_eqs_typ::dims], const long int (& stop)[scheme::Sys_eqs_typ::dims], Grid_dst & g_dst) { // get the map const auto & g_map = sc.getMap(); `````` Pietro Incardona committed Nov 30, 2015 139 `````` `````` Pietro Incardona committed Dec 09, 2015 140 141 `````` // get the grid padding const Padding & pd = sc.getPadding(); `````` Pietro Incardona committed Nov 30, 2015 142 `````` `````` Pietro Incardona committed Dec 09, 2015 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 `````` // shift the start and stop by the padding grid_key_dx start_k = grid_key_dx(start) + pd.getKP1(); grid_key_dx stop_k = grid_key_dx(stop) + pd.getKP1(); // sub-grid iterator over the grid map auto g_map_it = g_map.getSubDomainIterator(start_k,stop_k); // Iterator over the destination grid auto g_dst_it = g_dst.getDomainIterator(); // Check that the 2 iterator has the same size checkIterator(g_map_it,g_dst_it); while (g_map_it.isNext() == true) { typedef typename to_boost_vmpl::type vid; typedef boost::mpl::size v_size; auto key_src = g_map_it.get(); size_t lin_id = g_map.template get<0>(key_src); // destination point auto key_dst = g_dst_it.get(); // Transform this id into an id for the Eigen vector copy_ele> cp(key_dst,g_dst,v,lin_id,g_map.size()); boost::mpl::for_each_ref>(cp); ++g_map_it; ++g_dst_it; } } /*! \brief Copy in a normal grid `````` Pietro Incardona committed Nov 30, 2015 179 180 181 `````` * * */ `````` Pietro Incardona committed Dec 09, 2015 182 `````` template void copy_staggered_to_normal(scheme & sc,const long int (& start)[scheme::Sys_eqs_typ::dims], const long int (& stop)[scheme::Sys_eqs_typ::dims], Grid_dst & g_dst) `````` Pietro Incardona committed Nov 30, 2015 183 `````` { `````` Pietro Incardona committed Dec 09, 2015 184 185 186 187 188 189 190 191 `````` // get the map const auto & g_map = sc.getMap(); // get the grid padding const Padding & pd = sc.getPadding(); // set the staggered position of the property openfpm::vector> stag_pos[sizeof...(pos)]; `````` Pietro Incardona committed Nov 30, 2015 192 `````` `````` Pietro Incardona committed Dec 11, 2015 193 194 195 `````` // interpolation points for each property openfpm::vector>> interp_pos[sizeof...(pos)]; `````` Pietro Incardona committed Dec 09, 2015 196 197 198 199 200 201 `````` // fill the staggered position vector for each property stag_set_position ssp(stag_pos); typedef boost::mpl::vector_c v_pos_type; boost::mpl::for_each_ref(ssp); `````` Pietro Incardona committed Dec 11, 2015 202 203 204 `````` interp_points itp(interp_pos,stag_pos); boost::mpl::for_each_ref(itp); `````` Pietro Incardona committed Dec 09, 2015 205 206 207 208 209 210 211 212 213 214 215 216 217 218 `````` // shift the start and stop by the padding grid_key_dx start_k = grid_key_dx(start) + pd.getKP1(); grid_key_dx stop_k = grid_key_dx(stop) + pd.getKP1(); // sub-grid iterator over the grid map auto g_map_it = g_map.getSubDomainIterator(start_k,stop_k); // Iterator over the destination grid auto g_dst_it = g_dst.getDomainIterator(); // Check that the 2 iterator has the same size checkIterator(g_map_it,g_dst_it); while (g_map_it.isNext() == true) `````` Pietro Incardona committed Nov 30, 2015 219 `````` { `````` Pietro Incardona committed Dec 09, 2015 220 221 `````` typedef typename to_boost_vmpl::type vid; typedef boost::mpl::size v_size; `````` Pietro Incardona committed Nov 30, 2015 222 `````` `````` Pietro Incardona committed Dec 09, 2015 223 `````` auto key_src = g_map_it.get(); `````` Pietro Incardona committed Nov 30, 2015 224 `````` `````` Pietro Incardona committed Dec 09, 2015 225 226 `````` // destination point auto key_dst = g_dst_it.get(); `````` Pietro Incardona committed Nov 30, 2015 227 `````` `````` Pietro Incardona committed Dec 09, 2015 228 229 `````` // Transform this id into an id for the Eigen vector `````` Pietro Incardona committed Dec 11, 2015 230 `````` interp_ele,T,sizeof...(pos)> cp(key_dst,g_dst,v,key_src,g_map,interp_pos); `````` Pietro Incardona committed Dec 09, 2015 231 232 233 234 235 236 237 `````` boost::mpl::for_each_ref>(cp); ++g_map_it; ++g_dst_it; } } `````` Pietro Incardona committed Nov 30, 2015 238 `````` `````` Pietro Incardona committed Apr 07, 2016 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 `````` /*! \brief Here we collect the full matrix on master * */ void collect() const { Vcluster & vcl = *global_v_cluster; row_val_recv.clear(); // here we collect all the triplet in one array on the root node vcl.SGather(row_val,row_val_recv,prc,sz,0); if (vcl.getProcessUnitID() != 0) row_val.resize(0); else row_val.swap(row_val_recv); build_map(); } /*! \brief Set the Eigen internal vector * * */ void setEigen() const { // set the vector for (size_t i = 0 ; i < row_val.size() ; i++) v[row_val.get(i).row()] = row_val.get(i).value(); } /*! \brief Build the map * * */ void build_map() const { map.clear(); for (size_t i = 0 ; i < row_val.size() ; i++) map[row_val.get(i).row()] = i; } `````` Pietro Incardona committed Nov 30, 2015 284 285 ``````public: `````` Pietro Incardona committed Apr 07, 2016 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 `````` /*! \brief Copy the vector * * \param v vector to copy * */ Vector(const Vector & v) { this->operator=(v); } /*! \brief Copy the vector * * \param v vector to copy * */ Vector(const Vector && v) { this->operator=(v); } /*! \brief Create a vector with n elements * * \param n number of elements in the vector * */ Vector(size_t n) { resize(n); } /*! \brief Create a vector with 0 elements * */ Vector() { } `````` Pietro Incardona committed Nov 30, 2015 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 `````` /*! \brief Resize the Vector * * \param row numbers of row * */ void resize(size_t row) { v.resize(row); } /*! \brief Return a reference to the vector element * * \param i element * * \return reference to the element vector * */ `````` Pietro Incardona committed Apr 07, 2016 340 `````` void insert(size_t i, T val) `````` Pietro Incardona committed Nov 30, 2015 341 `````` { `````` Pietro Incardona committed Apr 07, 2016 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 `````` row_val.add(); // Map map[i] = row_val.size()-1; row_val.last().row() = i; row_val.last().value() = val; } /*! \brief Return a reference to the vector element * * \warning The element must exist * * \param i element * * \return reference to the element vector * */ T & operator()(size_t i) { // Search if exist std::unordered_map::iterator it = map.find(i); if ( it == map.end() ) { // Does not exist std::cerr << __FILE__ << ":" << __LINE__ << " value does not exist " << std::endl; return invalid; } else return row_val.get(it->second).value(); return invalid; `````` Pietro Incardona committed Nov 30, 2015 378 379 380 381 382 383 384 385 386 `````` } /*! \brief Get the Eigen Vector object * * \return the Eigen Vector * */ const Eigen::Matrix & getVec() const { `````` Pietro Incardona committed Apr 07, 2016 387 388 389 `````` collect(); setEigen(); `````` Pietro Incardona committed Nov 30, 2015 390 391 392 393 394 395 396 397 398 399 `````` return v; } /*! \brief Get the Eigen Vector object * * \return the Eigen Vector * */ Eigen::Matrix & getVec() { `````` Pietro Incardona committed Apr 07, 2016 400 401 402 `````` collect(); setEigen(); `````` Pietro Incardona committed Nov 30, 2015 403 404 405 406 407 408 409 `````` return v; } /*! \brief Copy the vector into the grid * * */ `````` Pietro Incardona committed Dec 09, 2015 410 `````` template void copy(scheme & sc,const long int (& start)[scheme::Sys_eqs_typ::dims], const long int (& stop)[scheme::Sys_eqs_typ::dims], Grid_dst & g_dst) `````` Pietro Incardona committed Nov 30, 2015 411 `````` { `````` Pietro Incardona committed Dec 09, 2015 412 `````` if (is_grid_staggered::value()) `````` Pietro Incardona committed Nov 30, 2015 413 `````` { `````` Pietro Incardona committed Dec 09, 2015 414 415 416 417 418 419 `````` if (g_dst.is_staggered() == true) copy_staggered_to_staggered(sc,start,stop,g_dst); else copy_staggered_to_normal(sc,start,stop,g_dst); } } `````` Pietro Incardona committed Nov 30, 2015 420 `````` `````` Pietro Incardona committed Feb 08, 2016 421 422 423 424 425 426 427 428 429 `````` /*! \brief Scatter the vector information to the other processors * * Eigen does not have a real parallel vector, so in order to work we have to scatter * the vector from one processor to the other * * */ void scatter() { `````` Pietro Incardona committed Apr 07, 2016 430 431 432 433 `````` row_val_recv.clear(); Vcluster & vcl = *global_v_cluster; vcl.SScatter(row_val,row_val_recv,prc,sz,0); `````` Pietro Incardona committed Feb 08, 2016 434 `````` `````` Pietro Incardona committed Apr 07, 2016 435 436 437 438 439 440 441 442 `````` // if we do not receive anything a previous collect has not been performed // and so nothing is scattered if (row_val_recv.size() != 0) { row_val.clear(); row_val.add(row_val_recv); build_map(); } `````` Pietro Incardona committed Feb 08, 2016 443 444 `````` } `````` Pietro Incardona committed Dec 09, 2015 445 446 447 448 449 450 451 452 453 `````` /*! \brief Load from file * * * */ void fromFile(std::string file) { std::ifstream inputf; inputf.open(file); `````` Pietro Incardona committed Nov 30, 2015 454 `````` `````` Pietro Incardona committed Dec 09, 2015 455 456 457 458 `````` for (size_t i = 0 ; i < v.size() ; i++) inputf >> v(i); inputf.close(); `````` Pietro Incardona committed Nov 30, 2015 459 460 `````` } `````` Pietro Incardona committed Apr 07, 2016 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 `````` /*! \brief Copy the vector * * \param v vector to copy * */ Vector & operator=(const Vector & v) { prc = v.prc; sz = v.sz; map = v.map; row_val = v.row_val; return *this; } /*! \brief Copy the vector * * \param v vector to copy * */ Vector & operator=(const Vector && v) { prc = v.prc; sz = v.sz; map = v.map; row_val = v.row_val; return *this; } /*! \brief Copy the vector (it is used for special purpose) * * \warning v MUST contain at least all the elements of the vector * * \param v base eigen vector to copy * */ Vector & operator=(Eigen::Matrix & v) { for (size_t i = 0 ; i < row_val.size() ; i++) row_val.get(i).value() = v(row_val.get(i).row()); return *this; } `````` Pietro Incardona committed Nov 30, 2015 506 507 508 509 ``````}; #endif /* OPENFPM_NUMERICS_SRC_VECTOR_VECTOR_EIGEN_HPP_ */``````