Reference documentation for deal.II version 8.1.0
partitioner.h
1 // ---------------------------------------------------------------------
2 // @f$Id: partitioner.h 30177 2013-07-28 18:32:49Z bangerth @f$
3 //
4 // Copyright (C) 2011 - 2013 by the deal.II authors
5 //
6 // This file is part of the deal.II library.
7 //
8 // The deal.II library is free software; you can use it, redistribute
9 // it, and/or modify it under the terms of the GNU Lesser General
10 // Public License as published by the Free Software Foundation; either
11 // version 2.1 of the License, or (at your option) any later version.
12 // The full text of the license can be found in the file LICENSE at
13 // the top level of the deal.II distribution.
14 //
15 // ---------------------------------------------------------------------
16 
17 #ifndef __deal2__partitioner_h
18 #define __deal2__partitioner_h
19 
20 #include <deal.II/base/config.h>
21 #include <deal.II/base/index_set.h>
22 #include <deal.II/base/mpi.h>
23 #include <deal.II/base/types.h>
24 #include <deal.II/base/utilities.h>
25 #include <deal.II/base/memory_consumption.h>
26 
27 #include <limits>
28 
29 
31 
32 namespace Utilities
33 {
34  namespace MPI
35  {
67  {
68  public:
72  Partitioner ();
73 
79  Partitioner (const unsigned int size);
80 
91  Partitioner (const IndexSet &locally_owned_indices,
92  const IndexSet &ghost_indices_in,
93  const MPI_Comm communicator_in);
94 
104  Partitioner (const IndexSet &locally_owned_indices,
105  const MPI_Comm communicator_in);
106 
111  void set_owned_indices (const IndexSet &locally_owned_indices);
112 
118 
123 
129  unsigned int local_size() const;
130 
139  const IndexSet &locally_owned_range() const;
140 
147  std::pair<types::global_dof_index,types::global_dof_index>
148  local_range() const;
149 
154  bool in_local_range (const types::global_dof_index global_index) const;
155 
168  unsigned int
169  global_to_local (const types::global_dof_index global_index) const;
170 
182  local_to_global (const unsigned int local_index) const;
183 
191  bool is_ghost_entry (const types::global_dof_index global_index) const;
192 
197  const IndexSet &ghost_indices() const;
198 
204  unsigned int n_ghost_indices() const;
205 
212  const std::vector<std::pair<unsigned int, types::global_dof_index> > &
213  ghost_targets() const;
214 
223  const std::vector<std::pair<types::global_dof_index, types::global_dof_index> > &
224  import_indices() const;
225 
231  unsigned int n_import_indices() const;
232 
241  const std::vector<std::pair<unsigned int, types::global_dof_index> > &
242  import_targets() const;
243 
261  bool is_compatible (const Partitioner &part) const;
262 
267  unsigned int this_mpi_process () const;
268 
274  unsigned int n_mpi_processes () const;
275 
280  const MPI_Comm &get_communicator() const;
281 
286  std::size_t memory_consumption() const;
287 
291  DeclException2 (ExcIndexNotPresent,
293  unsigned int,
294  << "Global index " << arg1
295  << " neither owned nor ghost on proc " << arg2);
296 
297  private:
302 
308 
314  std::pair<types::global_dof_index,types::global_dof_index> local_range_data;
315 
321 
327  unsigned int n_ghost_indices_data;
328 
334  std::vector<std::pair<unsigned int, types::global_dof_index> > ghost_targets_data;
335 
344  std::vector<std::pair<types::global_dof_index, types::global_dof_index> > import_indices_data;
345 
352  unsigned int n_import_indices_data;
353 
358  std::vector<std::pair<unsigned int,types::global_dof_index> > import_targets_data;
359 
364  unsigned int my_pid;
365 
370  unsigned int n_procs;
371 
376  const MPI_Comm communicator;
377  };
378 
379 
380 
381  /*----------------------- Inline functions ----------------------------------*/
382 
383 #ifndef DOXYGEN
384 
385  inline
387  {
388  return global_size;
389  }
390 
391 
392 
393  inline
395  {
397  }
398 
399 
400 
401  inline
402  std::pair<types::global_dof_index,types::global_dof_index>
404  {
405  return local_range_data;
406  }
407 
408 
409 
410  inline
411  unsigned int
412  Partitioner::local_size () const
413  {
415  Assert(size<=std::numeric_limits<unsigned int>::max(),
417  return static_cast<unsigned int>(size);
418  }
419 
420 
421 
422  inline
423  bool
424  Partitioner::in_local_range (const types::global_dof_index global_index) const
425  {
426  return (local_range_data.first <= global_index &&
427  global_index < local_range_data.second);
428  }
429 
430 
431 
432  inline
433  bool
434  Partitioner::is_ghost_entry (const types::global_dof_index global_index) const
435  {
436  // if the index is in the global range, it is
437  // trivially not a ghost
438  if (in_local_range(global_index) == true)
439  return false;
440  else
441  return ghost_indices().is_element(global_index);
442  }
443 
444 
445 
446  inline
447  unsigned int
448  Partitioner::global_to_local (const types::global_dof_index global_index) const
449  {
450  Assert(in_local_range(global_index) || is_ghost_entry (global_index),
451  ExcIndexNotPresent(global_index, my_pid));
452  if (in_local_range(global_index))
453  return static_cast<unsigned int>(global_index - local_range_data.first);
454  else if (is_ghost_entry (global_index))
455  return (local_size() +
456  static_cast<unsigned int>(ghost_indices_data.index_within_set (global_index)));
457  else
458  // should only end up here in
459  // optimized mode, when we use this
460  // large number to trigger a segfault
461  // when using this method for array
462  // access
464  }
465 
466 
467 
468  inline
470  Partitioner::local_to_global (const unsigned int local_index) const
471  {
473  if (local_index < local_size())
474  return local_range_data.first + local_index;
475  else
476  return ghost_indices_data.nth_index_in_set (local_index-local_size());
477  }
478 
479 
480 
481  inline
482  const IndexSet &Partitioner::ghost_indices() const
483  {
484  return ghost_indices_data;
485  }
486 
487 
488 
489  inline
490  unsigned int
492  {
493  return n_ghost_indices_data;
494  }
495 
496 
497 
498  inline
499  const std::vector<std::pair<unsigned int, types::global_dof_index> > &
501  {
502  return ghost_targets_data;
503  }
504 
505 
506  inline
507  const std::vector<std::pair<types::global_dof_index, types::global_dof_index> > &
509  {
510  return import_indices_data;
511  }
512 
513 
514 
515  inline
516  unsigned int
518  {
519  return n_import_indices_data;
520  }
521 
522 
523 
524  inline
525  const std::vector<std::pair<unsigned int,types::global_dof_index> > &
527  {
528  return import_targets_data;
529  }
530 
531 
532 
533  inline
534  bool
535  Partitioner::is_compatible (const Partitioner &part) const
536  {
537  // is the partitioner points to the same
538  // memory location as the calling processor
539  if (&part == this)
540  return true;
541  else
542  return (global_size == part.global_size &&
543  local_range_data == part.local_range_data &&
544  ghost_indices_data == part.ghost_indices_data);
545  }
546 
547 
548  inline
549  unsigned int
551  {
552  // return the id from the variable stored in
553  // this class instead of
554  // Utilities::MPI::this_mpi_process() in order
555  // to make this query also work when MPI is
556  // not initialized.
557  return my_pid;
558  }
559 
560 
561 
562  inline
563  unsigned int
565  {
566  // return the number of MPI processes from the
567  // variable stored in this class instead of
568  // Utilities::MPI::n_mpi_processes() in order
569  // to make this query also work when MPI is
570  // not initialized.
571  return n_procs;
572  }
573 
574 
575 
576  inline
577  const MPI_Comm &
579  {
580  return communicator;
581  }
582 
583 #endif // ifndef DOXYGEN
584 
585  } // end of namespace MPI
586 
587 } // end of namespace Utilities
588 
589 
590 DEAL_II_NAMESPACE_CLOSE
591 
592 #endif
unsigned int n_ghost_indices() const
static const unsigned int invalid_unsigned_int
Definition: types.h:191
types::global_dof_index index_within_set(const types::global_dof_index global_index) const
Definition: index_set.h:970
const std::vector< std::pair< types::global_dof_index, types::global_dof_index > > & import_indices() const
unsigned int this_mpi_process() const
bool is_ghost_entry(const types::global_dof_index global_index) const
types::global_dof_index size() const
std::vector< std::pair< types::global_dof_index, types::global_dof_index > > import_indices_data
Definition: partitioner.h:344
#define AssertIndexRange(index, range)
Definition: exceptions.h:888
unsigned int n_ghost_indices_data
Definition: partitioner.h:327
const MPI_Comm & get_communicator() const
const IndexSet & locally_owned_range() const
void set_owned_indices(const IndexSet &locally_owned_indices)
const std::vector< std::pair< unsigned int, types::global_dof_index > > & ghost_targets() const
DeclException2(ExcIndexNotPresent, types::global_dof_index, unsigned int,<< "Global index "<< arg1<< " neither owned nor ghost on proc "<< arg2)
const std::vector< std::pair< unsigned int, types::global_dof_index > > & import_targets() const
unsigned int global_dof_index
Definition: types.h:100
const MPI_Comm communicator
Definition: partitioner.h:376
#define Assert(cond, exc)
Definition: exceptions.h:299
std::size_t memory_consumption() const
types::global_dof_index nth_index_in_set(const unsigned int local_index) const
Definition: index_set.h:919
types::global_dof_index local_to_global(const unsigned int local_index) const
const types::global_dof_index global_size
Definition: partitioner.h:301
std::vector< std::pair< unsigned int, types::global_dof_index > > ghost_targets_data
Definition: partitioner.h:334
unsigned int n_import_indices() const
unsigned int global_to_local(const types::global_dof_index global_index) const
const IndexSet & ghost_indices() const
bool is_compatible(const Partitioner &part) const
bool in_local_range(const types::global_dof_index global_index) const
std::pair< types::global_dof_index, types::global_dof_index > local_range() const
std::vector< std::pair< unsigned int, types::global_dof_index > > import_targets_data
Definition: partitioner.h:358
unsigned int n_mpi_processes() const
::ExceptionBase & ExcNotImplemented()
std::pair< types::global_dof_index, types::global_dof_index > local_range_data
Definition: partitioner.h:314
unsigned int n_import_indices_data
Definition: partitioner.h:352
bool is_element(const types::global_dof_index index) const
Definition: index_set.h:811
void set_ghost_indices(const IndexSet &ghost_indices)
unsigned int local_size() const