TiledArray  0.7.0
blocked_pmap.h
Go to the documentation of this file.
1 /*
2  * This file is a part of TiledArray.
3  * Copyright (C) 2013 Virginia Tech
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU 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  * This program 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 General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * Justus Calvin
19  * Department of Chemistry, Virginia Tech
20  *
21  * add.h
22  * April 24, 2012
23  *
24  */
25 
26 #ifndef TILEDARRAY_PMAP_BLOCKED_PMAP_H__INCLUDED
27 #define TILEDARRAY_PMAP_BLOCKED_PMAP_H__INCLUDED
28 
29 #include <TiledArray/pmap/pmap.h>
30 
31 namespace TiledArray {
32  namespace detail {
33 
35 
38  class BlockedPmap : public Pmap {
39  protected:
40 
41  // Import Pmap protected variables
42  using Pmap::rank_;
43  using Pmap::procs_;
44  using Pmap::size_;
45  using Pmap::local_;
46 
47  private:
48 
49  const size_type block_size_;
50  const size_type remainder_;
51  const size_type block_size_plus_1_;
52  const size_type block_size_plus_1_times_remainder_;
53  const size_type local_first_;
54  const size_type local_last_;
55 
56  public:
58 
60 
63  BlockedPmap(World& world, size_type size) :
64  Pmap(world, size),
65  block_size_(size_ / procs_),
66  remainder_(size_ % procs_),
67  block_size_plus_1_(block_size_ + 1),
68  block_size_plus_1_times_remainder_(remainder_ * block_size_plus_1_),
69  local_first_(rank_ * block_size_ + std::min<size_type>(rank_, remainder_)),
70  local_last_((rank_ + 1) * block_size_ + std::min<size_type>((rank_ + 1), remainder_))
71  {
72  local_.reserve(local_last_ - local_first_);
73 
74 
75  // Construct a map of all local processes
76  for(size_type first = local_first_; first < local_last_; ++first) {
78  local_.push_back(first);
79  }
80  }
81 
82  virtual ~BlockedPmap() { }
83 
85 
88  virtual size_type owner(const size_type tile) const {
89  TA_ASSERT(tile < size_);
90  return (tile < block_size_plus_1_times_remainder_ ?
91  tile / block_size_plus_1_ :
92  ((tile - block_size_plus_1_times_remainder_) / block_size_) + remainder_);
93  }
94 
95 
97 
100  virtual bool is_local(const size_type tile) const {
101  return ((tile >= local_first_) && (tile < local_last_));
102  }
103  }; // class BlockedPmap
104 
105  } // namespace detail
106 } // namespace TiledArray
107 
108 
109 #endif // TILEDARRAY_PMAP_BLOCKED_PMAP_H__INCLUDED
size_type size() const
Size accessor.
Definition: pmap.h:91
decltype(auto) min(const Tile< Arg > &arg)
Minimum element of a tile.
Definition: tile.h:948
const size_type procs_
The number of processes.
Definition: pmap.h:53
const size_type rank_
The rank of this process.
Definition: pmap.h:52
Process map.
Definition: pmap.h:46
STL namespace.
virtual bool is_local(const size_type tile) const
Check that the tile is owned by this process.
Definition: blocked_pmap.h:100
#define TA_ASSERT(a)
Definition: error.h:107
A blocked process map.
Definition: blocked_pmap.h:38
virtual size_type owner(const size_type tile) const
Maps tile to the processor that owns it.
Definition: blocked_pmap.h:88
Pmap::size_type size_type
Key type.
Definition: blocked_pmap.h:57
const size_type size_
The number of tiles mapped among all processes.
Definition: pmap.h:54
std::size_t size_type
Size type.
Definition: pmap.h:48
std::vector< size_type > local_
A list of local tiles.
Definition: pmap.h:55
BlockedPmap(World &world, size_type size)
Construct Blocked map.
Definition: blocked_pmap.h:63