GeometricStrategy.h 6.5 KB
 adelmann committed Jun 16, 2017 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 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 ``````#ifndef __GEOMETRIC_STRATEGY_H__ #define __GEOMETRIC_STRATEGY_H__ #include #include #include "Comm/Splitter/SplitStrategy.h" typedef std::vector coordinates_t; typedef struct { coordinates_t extensions; coordinates_t origin; MPI_Comm comm; int master_pid; std::vector worker_pids; } region_t; /** * The geometric strategy partitions the network graph in num_masters equally * sized regions and then places a master at the centroid of each region. * Workers inside a region are assigned to the master at its centroid. */ class GeometricStrategy : protected SplitStrategy { public: GeometricStrategy(size_t num_masters, boost::shared_ptr topology, MPI_Comm comm) : Strategy(num_masters, topology, comm) {} virtual ~GeometricStrategy() {} void split() { // 1. Initialize the region list with the initial topology region_t initial_region; for(int dim=0; dim < topology_.getNumDimensions(); dim++) { initial_region.origin.push_back(0); initial_region.extensions.push_back(dims_[dim]); } regions_.push_back(initial_region); // 2. Build num_masters_ partitions of the topology while(regions_.size() < num_masters_) { splitInLargestDim(); } // 3. Compute centroids of partition and assign master `````` snuverink_j committed Aug 02, 2018 52 `````` for(region_t & region : regions_) { `````` adelmann committed Jun 16, 2017 53 54 55 56 `````` computeCentroid(region); } // 4. Create communicator groups `````` snuverink_j committed Aug 02, 2018 57 `````` for(region_t & region : regions_) { `````` adelmann committed Jun 16, 2017 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 `````` bool is_worker = false; coordinates_t iter = region.origin; for(int x = 0; x < region.extensions[0]; x++) { iter[0] = region.origin[0] + x; for(int y = 0; y < region.extensions[1]; y++) { iter[1] = region.origin[1] + y; for(int z = 0; z < region.extensions[2]; z++) { iter[2] = region.origin[2] + z; std::vector worker_pids; coordinatesToPID(iter, worker_pids); if(worker_pids[0] != region.master_pid) region.worker_pids.push_back(worker_pids[0]); region.worker_pids.push_back(worker_pids[1]); region.worker_pids.push_back(worker_pids[2]); region.worker_pids.push_back(worker_pids[3]); if(rank_ == worker_pids[0] || rank_ == worker_pids[1] || rank_ == worker_pids[2] || rank_ == worker_pids[3] ) { is_worker = true; } } } } if(rank_ == region.master_pid || is_worker) addCommGroup(region.master_pid, region.worker_pids); MPI_Barrier(comm_); } } protected: std::vector regions_; void splitInLargestDim() { // first we determine the largest extension of all remaining regions int origin = 0; int split_in_direction = 0; int max_ext_dir = 0; `````` snuverink_j committed Aug 02, 2018 107 `````` for(region_t region : regions_) { `````` adelmann committed Jun 16, 2017 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 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 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 `````` for(int dim = 0; dim < topology_.getNumDimensions(); dim++) { if(region.extensions[dim] > max_ext_dir) { split_in_direction = dim; origin = region.origin[dim]; max_ext_dir = region.extensions[dim]; } } } // now we can perform splits as long as the number of requested // regions is not reached int split_at_coordinate = origin; split_at_coordinate += (int)(max_ext_dir / 2.0); splitRegionAt(split_at_coordinate, split_in_direction); } void splitRegionAt(int split_at_coordinate, int split_in_direction) { int removed = 0; bool continue_splitting = true; std::vector new_regions; std::vector::iterator itr; for(itr = regions_.begin(); itr != regions_.end(); itr++) { if(regions_.size() + new_regions.size() - removed == num_masters_) continue_splitting = false; double origin = itr->origin[split_in_direction]; double extension = itr->extensions[split_in_direction]; if( continue_splitting && isSplittable(origin, split_at_coordinate, extension) ) { // clone region region_t second = *itr; // and split second.origin[split_in_direction] = split_at_coordinate; second.extensions[split_in_direction] = extension - (split_at_coordinate - origin); itr->extensions[split_in_direction] = split_at_coordinate - origin; new_regions.push_back(*itr); new_regions.push_back(second); removed++; } else { new_regions.push_back(*itr); removed++; } } regions_ = new_regions; } bool isSplittable(double origin, double split_at_coordinate, double extension) { return (origin < split_at_coordinate && split_at_coordinate < origin + extension); } void computeCentroid(region_t ®ion) { coordinates_t centroid; centroid.resize(3, 0); for(unsigned int pos=0; pos < region.extensions.size(); pos++) { int centroid_coordinate = (int)(region.origin[pos]*1.0 + region.extensions[pos]/2.0); centroid[pos] = centroid_coordinate; } std::vector pids; coordinatesToPID(centroid, pids); region.master_pid = pids[0]; } void coordinatesToPID(coordinates_t coordinate, std::vector &pid) { pid.resize(4, 0); if(coords_[0] == coordinate[0] && coords_[1] == coordinate[1] && coords_[2] == coordinate[2] ) { pid[my_core_] = rank_; } MPI_Allreduce(MPI_IN_PLACE, &pid[0], 4, MPI_INT, MPI_SUM, comm_); } void printRegion(region_t region) { if(rank_ == 0) { std::cout << "region: "; std::cout << "[" << region.origin[0] << ", " << region.origin[1] << ", " << region.origin[2] << "], "; std::cout << "[" << region.extensions[0] << ", " << region.extensions[1] << ", " << region.extensions[2] << "]"; std::cout << std::endl; } } }; #endif``````