TiledArray  0.7.0
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 
46  class CyclicPmap : public Pmap {
47  protected:
48 
49  // Import Pmap protected variables
50  using Pmap::rank_;
51  using Pmap::procs_;
52  using Pmap::size_;
53  using Pmap::local_;
54 
55  private:
56 
57  const size_type rows_;
58  const size_type cols_;
59  const size_type proc_cols_;
60  const size_type proc_rows_;
61 
62  public:
64 
66 
75  CyclicPmap(World& world, size_type rows, size_type cols,
76  size_type proc_rows, size_type proc_cols) :
77  Pmap(world, rows * cols), rows_(rows), cols_(cols),
78  proc_cols_(proc_cols), proc_rows_(proc_rows)
79  {
80  // Check that the size is non-zero
81  TA_ASSERT(rows_ >= 1ul);
82  TA_ASSERT(cols_ >= 1ul);
83 
84  // Check limits of process rows and columns
85  TA_ASSERT(proc_rows_ >= 1ul);
86  TA_ASSERT(proc_cols_ >= 1ul);
87  TA_ASSERT((proc_rows_ * proc_cols_) <= procs_);
88 
89  // Initialize local tile list
90  if(rank_ < (proc_rows_ * proc_cols_)) {
91  // Compute rank coordinates
92  const size_type rank_row = rank_ / proc_cols_;
93  const size_type rank_col = rank_ % proc_cols_;
94 
95  const size_type local_rows =
96  (rows_ / proc_rows_) + ((rows_ % proc_rows_) < rank_row ? 1ul : 0ul);
97  const size_type local_cols =
98  (cols_ / proc_cols_) + ((cols_ % proc_cols_) < rank_col ? 1ul : 0ul);
99 
100  // Allocate memory for the local tile list
101  local_.reserve(local_rows * local_cols);
102 
103  // Iterate over local tiles
104  for(size_type i = rank_row; i < rows_; i += proc_rows_) {
105  const size_type row_end = (i + 1) * cols_;
106  for(size_type tile = i * cols_ + rank_col; tile < row_end; tile += proc_cols_) {
108  local_.push_back(tile);
109  }
110  }
111  }
112  }
113 
114  virtual ~CyclicPmap() { }
115 
117  size_type nrows() const { return rows_; }
119  size_type ncols() const { return cols_; }
121  size_type nrows_proc() const { return proc_rows_; }
123  size_type ncols_proc() const { return proc_cols_; }
124 
126 
129  virtual size_type owner(const size_type tile) const {
130  TA_ASSERT(tile < size_);
131  // Compute tile coordinate in tile grid
132  const size_type tile_row = tile / cols_;
133  const size_type tile_col = tile % cols_;
134  // Compute process coordinate of tile in the process grid
135  const size_type proc_row = tile_row % proc_rows_;
136  const size_type proc_col = tile_col % proc_cols_;
137  // Compute the process that owns tile
138  const size_type proc = proc_row * proc_cols_ + proc_col;
139 
140  TA_ASSERT(proc < procs_);
141 
142  return proc;
143  }
144 
145 
147 
150  virtual bool is_local(const size_type tile) const {
151  return (CyclicPmap::owner(tile) == rank_);
152  }
153 
154  }; // class CyclicPmap
155 
156  } // namespace detail
157 } // namespace TiledArray
158 
159 
160 #endif // TILEDARRAY_PMAP_CYCLIC_PMAP_H__INCLUDED
size_type ncols() const
Access number of columns in the tile index matrix.
Definition: cyclic_pmap.h:119
const size_type procs_
The number of processes.
Definition: pmap.h:53
CyclicPmap(World &world, size_type rows, size_type cols, size_type proc_rows, size_type proc_cols)
Construct process map.
Definition: cyclic_pmap.h:75
size_type nrows() const
Access number of rows in the tile index matrix.
Definition: cyclic_pmap.h:117
const size_type rank_
The rank of this process.
Definition: pmap.h:52
Maps cyclically a sequence of indices onto a 2-d matrix of processes.
Definition: cyclic_pmap.h:46
Process map.
Definition: pmap.h:46
Pmap::size_type size_type
Size type.
Definition: cyclic_pmap.h:63
#define TA_ASSERT(a)
Definition: error.h:107
virtual bool is_local(const size_type tile) const
Check that the tile is owned by this process.
Definition: cyclic_pmap.h:150
virtual size_type owner(const size_type tile) const
Maps tile to the processor that owns it.
Definition: cyclic_pmap.h:129
const size_type size_
The number of tiles mapped among all processes.
Definition: pmap.h:54
size_type nrows_proc() const
Access number of rows in the process matrix.
Definition: cyclic_pmap.h:121
size_type ncols_proc() const
Access number of columns in the process matrix.
Definition: cyclic_pmap.h:123
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