cyclic_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  * cyclic_pmap.h
22  * May 1, 2012
23  *
24  */
25 
26 #ifndef TILEDARRAY_PMAP_CYCLIC_PMAP_H__INCLUDED
27 #define TILEDARRAY_PMAP_CYCLIC_PMAP_H__INCLUDED
28 
29 #include <TiledArray/pmap/pmap.h>
30 
31 namespace TiledArray {
32 namespace detail {
33 
35 
48 class CyclicPmap : public Pmap {
49  protected:
50  // Import Pmap protected variables
51  using Pmap::procs_;
52  using Pmap::rank_;
53  using Pmap::size_;
54 
55  private:
56  const size_type rows_;
57  const size_type cols_;
58  const size_type proc_cols_;
59  const size_type proc_rows_;
60  size_type rank_row_ = 0;
61  size_type rank_col_ = 0;
62  size_type local_rows_ = 0;
63  size_type local_cols_ =
64  0;
65 
66  public:
68 
70 
80  CyclicPmap(World& world, size_type rows, size_type cols, size_type proc_rows,
81  size_type proc_cols)
82  : Pmap(world, rows * cols),
83  rows_(rows),
84  cols_(cols),
85  proc_cols_(proc_cols),
86  proc_rows_(proc_rows) {
87  // Check that the size is non-zero
88  TA_ASSERT(rows_ >= 1ul);
89  TA_ASSERT(cols_ >= 1ul);
90 
91  // Check limits of process rows and columns
92  TA_ASSERT(proc_rows_ >= 1ul);
93  TA_ASSERT(proc_cols_ >= 1ul);
94  TA_ASSERT((proc_rows_ * proc_cols_) <= procs_);
95 
96  // Compute local size_, if have any
97  if (rank_ < (proc_rows_ * proc_cols_)) {
98  // Compute rank coordinates
99  rank_row_ = rank_ / proc_cols_;
100  rank_col_ = rank_ % proc_cols_;
101 
102  local_rows_ =
103  (rows_ / proc_rows_) + ((rows_ % proc_rows_) > rank_row_ ? 1ul : 0ul);
104  local_cols_ =
105  (cols_ / proc_cols_) + ((cols_ % proc_cols_) > rank_col_ ? 1ul : 0ul);
106 
107  // Allocate memory for the local tile list
108  this->local_size_ = local_rows_ * local_cols_;
109  }
110  }
111 
112  virtual ~CyclicPmap() {}
113 
115  size_type nrows() const { return rows_; }
117  size_type ncols() const { return cols_; }
119  size_type nrows_proc() const { return proc_rows_; }
121  size_type ncols_proc() const { return proc_cols_; }
122 
124 
127  virtual size_type owner(const size_type tile) const {
128  TA_ASSERT(tile < size_);
129  // Compute tile coordinate in tile grid
130  const size_type tile_row = tile / cols_;
131  const size_type tile_col = tile % cols_;
132  // Compute process coordinate of tile in the process grid
133  const size_type proc_row = tile_row % proc_rows_;
134  const size_type proc_col = tile_col % proc_cols_;
135  // Compute the process that owns tile
136  const size_type proc = proc_row * proc_cols_ + proc_col;
137 
138  TA_ASSERT(proc < procs_);
139 
140  return proc;
141  }
142 
144 
147  virtual bool is_local(const size_type tile) const {
148  return (CyclicPmap::owner(tile) == rank_);
149  }
150 
151  private:
152  virtual void advance(size_type& value, bool increment) const {
153  if (increment) {
154  auto row = value / cols_;
155  const auto row_end = (row + 1) * cols_;
156  value += proc_cols_;
157  if (value >= row_end) { // if past the end of row ...
158  row += proc_rows_;
159  if (row < rows_) { // still have tiles
160  value = row * cols_ + rank_col_; // first tile in this row
161  } else // done
162  value = size_;
163  }
164  } else { // decrement
165  auto row = value / cols_;
166  const auto row_begin = row * cols_;
167  if (value < proc_cols_) { // protect against unsigned wraparound
168  return;
169  }
170  value -= proc_cols_;
171  if (value < row_begin) { // if past the beginning of row ...
172  if (row < proc_rows_) // protect against unsigned wraparound
173  return;
174  row -= proc_rows_;
175  value = row * cols_ + rank_col_ +
176  (local_cols_ - 1) * proc_cols_; // last tile in this row
177  }
178  }
179  }
180 
181  public:
182  virtual const_iterator begin() const {
183  return this->local_size_ > 0
184  ? Iterator(*this, rank_row_ * cols_ + rank_col_, this->size_,
185  rank_row_ * cols_ + rank_col_, false, true)
186  : end(); // make end() if empty
187  }
188  virtual const_iterator end() const {
189  return this->local_size_ > 0
190  ? Iterator(*this, rank_row_ * cols_ + rank_col_, this->size_,
191  this->size_, false, true)
192  : Iterator(*this, 0, this->size_, this->size_, false, true);
193  }
194 
195 }; // class CyclicPmap
196 
197 } // namespace detail
198 } // namespace TiledArray
199 
200 #endif // TILEDARRAY_PMAP_CYCLIC_PMAP_H__INCLUDED
size_type ncols_proc() const
Access number of columns in the process matrix.
Definition: cyclic_pmap.h:121
const size_type procs_
The number of processes.
Definition: pmap.h:61
virtual size_type owner(const size_type tile) const
Maps tile to the processor that owns it.
Definition: cyclic_pmap.h:127
const size_type rank_
< The number of processes
Definition: pmap.h:60
virtual const_iterator begin() const
Begin local element iterator.
Definition: cyclic_pmap.h:182
virtual bool is_local(const size_type tile) const
Check that the tile is owned by this process.
Definition: cyclic_pmap.h:147
CyclicPmap(World &world, size_type rows, size_type cols, size_type proc_rows, size_type proc_cols)
Construct process map.
Definition: cyclic_pmap.h:80
Pmap::size_type size_type
Size type.
Definition: cyclic_pmap.h:67
#define TA_ASSERT(EXPR,...)
Definition: error.h:39
const size_type procs_
The number of processes.
Definition: pmap.h:61
virtual const_iterator end() const
End local element iterator.
Definition: cyclic_pmap.h:188
Maps cyclically a sequence of indices onto a 2-d matrix of processes.
Definition: cyclic_pmap.h:48
Pmap iterator type.
Definition: pmap.h:161
size_type local_size_
Definition: pmap.h:65
size_type nrows_proc() const
Access number of rows in the process matrix.
Definition: cyclic_pmap.h:119
const size_type rank_
The rank of this process.
Definition: pmap.h:60
size_type nrows() const
Access number of rows in the tile index matrix.
Definition: cyclic_pmap.h:115
std::size_t size_type
Size type.
Definition: pmap.h:57
const size_type size_
< The rank of this process
Definition: pmap.h:62
friend class Iterator
Iterator type.
Definition: pmap.h:280
Process map.
Definition: pmap.h:55
const size_type size_
The number of tiles mapped among all processes.
Definition: pmap.h:62
size_type ncols() const
Access number of columns in the tile index matrix.
Definition: cyclic_pmap.h:117