| 1 | #ifndef GRID_DIST_KEY_DX_HPP | 
| 2 | #define GRID_DIST_KEY_DX_HPP | 
| 3 |  | 
| 4 | #include "Grid/map_grid.hpp" | 
| 5 | template<unsigned int dim, typename base_key = grid_key_dx<dim>> | 
| 6 | class grid_dist_key_dx; | 
| 7 |  | 
| 8 | template<bool impl, typename grid_key_base, unsigned int dim> | 
| 9 | class move_impl | 
| 10 | { | 
| 11 | public: | 
| 12 | 	static grid_dist_key_dx<dim,grid_key_base> move(grid_key_base & key, size_t sub, size_t i, int s) | 
| 13 | 	{ | 
| 14 | 		key.set_d(i,key.get(i) + s); | 
| 15 | 		return grid_dist_key_dx<dim,grid_key_base>(sub,key); | 
| 16 | 	} | 
| 17 | }; | 
| 18 |  | 
| 19 | template<typename grid_key_base, unsigned int dim> | 
| 20 | class move_impl<false,grid_key_base,dim> | 
| 21 | { | 
| 22 | public: | 
| 23 | 	static grid_dist_key_dx<dim> move(grid_key_base & key, size_t sub, size_t i, int s) | 
| 24 | 	{ | 
| 25 | 		std::cout << __FILE__ << ":"  << __LINE__ << " Error move a key is not supported"  | 
| 26 | 				" directly acting on the grid key, please use the move function from the grid method"  << std::endl; | 
| 27 |  | 
| 28 | 		grid_key_dx<dim> zero; | 
| 29 | 		zero.zero(); | 
| 30 |  | 
| 31 | 		return grid_dist_key_dx<dim>(0,zero); | 
| 32 | 	} | 
| 33 | }; | 
| 34 |  | 
| 35 |  | 
| 36 | /*! \brief Grid key for a distributed grid | 
| 37 |  * | 
| 38 |  * It contain from which local sub-domain grid come from, and the local grid_key_dx | 
| 39 |  * | 
| 40 |  */ | 
| 41 |  | 
| 42 | template<unsigned int dim, typename base_key> | 
| 43 | class grid_dist_key_dx | 
| 44 | { | 
| 45 | 	//! grid list counter | 
| 46 |  | 
| 47 | 	size_t g_c; | 
| 48 |  | 
| 49 | 	//! Local grid iterator | 
| 50 |  | 
| 51 | 	base_key key; | 
| 52 |  | 
| 53 | public: | 
| 54 |  | 
| 55 | 	/*! \brief Set the local grid | 
| 56 | 	 * | 
| 57 | 	 * \param sub local grid | 
| 58 | 	 * | 
| 59 | 	 */ | 
| 60 | 	inline void setSub(size_t sub) | 
| 61 | 	{ | 
| 62 | 		g_c = sub; | 
| 63 | 	} | 
| 64 |  | 
| 65 | 	/*! \brief Get the local grid | 
| 66 | 	 * | 
| 67 | 	 * \return the id of the local grid | 
| 68 | 	 * | 
| 69 | 	 */ | 
| 70 | 	inline size_t getSub() const | 
| 71 | 	{ | 
| 72 | 		return g_c; | 
| 73 | 	} | 
| 74 |  | 
| 75 | 	/*! \brief Get the key | 
| 76 | 	 * | 
| 77 | 	 * \return the local key | 
| 78 | 	 * | 
| 79 | 	 */ | 
| 80 | 	inline base_key getKey() const | 
| 81 | 	{ | 
| 82 | 		return key; | 
| 83 | 	} | 
| 84 |  | 
| 85 |  | 
| 86 | 	/*! \brief Get the reference key | 
| 87 | 	 * | 
| 88 | 	 * \return the local key | 
| 89 | 	 * | 
| 90 | 	 */ | 
| 91 | 	inline base_key & getKeyRef() | 
| 92 | 	{ | 
| 93 | 		return key; | 
| 94 | 	} | 
| 95 |  | 
| 96 | 	/*! \brief Get the reference key | 
| 97 | 	 * | 
| 98 | 	 * \return the local key | 
| 99 | 	 * | 
| 100 | 	 */ | 
| 101 | 	inline const base_key & getKeyRef() const | 
| 102 | 	{ | 
| 103 | 		return key; | 
| 104 | 	} | 
| 105 |  | 
| 106 | 	/* \brief Check if two key are the same | 
| 107 | 	 * | 
| 108 | 	 * \param key_t key to check | 
| 109 | 	 * | 
| 110 | 	 * \return true if the two key are equal | 
| 111 | 	 * | 
| 112 | 	 */ | 
| 113 |  | 
| 114 | 	inline bool operator==(const grid_dist_key_dx<dim> & key_t) | 
| 115 | 	{ | 
| 116 | 		if (g_c != key_t.g_c) | 
| 117 | 			return false; | 
| 118 |  | 
| 119 | 		// Check the two key index by index | 
| 120 |  | 
| 121 | 		return getKey() == key_t.getKey(); | 
| 122 | 	} | 
| 123 |  | 
| 124 | 	/*! \brief Create a new key moving the old one | 
| 125 | 	 * | 
| 126 | 	 * \param i dimension id | 
| 127 | 	 * \param s number of steps | 
| 128 | 	 * | 
| 129 | 	 * \return new key | 
| 130 | 	 * | 
| 131 | 	 */ | 
| 132 | 	inline grid_dist_key_dx<dim,base_key> move(size_t i,int s) const | 
| 133 | 	{ | 
| 134 | 		auto key = getKey(); | 
| 135 |  | 
| 136 | 		return move_impl<has_set_d<base_key>::value, | 
| 137 | 				 decltype(this->getKey()), | 
| 138 | 				 dim>::move(key,getSub(),i,s); | 
| 139 | 	} | 
| 140 |  | 
| 141 | 	/*! \brief Create a new key moving the old one | 
| 142 | 	 * | 
| 143 | 	 * \param c where to move for each component | 
| 144 | 	 * | 
| 145 | 	 * \return new key | 
| 146 | 	 * | 
| 147 | 	 */ | 
| 148 | 	inline grid_dist_key_dx<dim> move(const comb<dim> & c) const | 
| 149 | 	{ | 
| 150 | 		grid_key_dx<dim> key = getKey(); | 
| 151 | 		for (size_t i = 0 ; i < dim ; i++) | 
| 152 | 			key.set_d(i,key.get(i) + c[i]); | 
| 153 | 		return grid_dist_key_dx<dim>(getSub(),key); | 
| 154 | 	} | 
| 155 |  | 
| 156 | 	/*! \brief Constructor set the sub-domain grid and the position in local coordinates | 
| 157 | 	 * | 
| 158 | 	 * \param g_c sub-domain | 
| 159 | 	 * \param key key | 
| 160 | 	 * | 
| 161 | 	 */ | 
| 162 | 	inline grid_dist_key_dx(int g_c, const base_key & key) | 
| 163 | 	:g_c(g_c),key(key) | 
| 164 | 	{ | 
| 165 | 	} | 
| 166 |  | 
| 167 | 	//! Constructor | 
| 168 | 	inline grid_dist_key_dx(){} | 
| 169 |  | 
| 170 | 	/*! \brief convert the key to string | 
| 171 | 	 * | 
| 172 | 	 * | 
| 173 | 	 */ | 
| 174 | 	std::string to_string() | 
| 175 | 	{ | 
| 176 | 		std::stringstream str; | 
| 177 |  | 
| 178 | 		str << "sub_domain="  << g_c << " " ; | 
| 179 |  | 
| 180 | 		for (size_t i = 0 ; i < dim ; i++) | 
| 181 | 			str << "x["  << i << "]="  << key.get(i) << " " ; | 
| 182 |  | 
| 183 | 		str << "\n" ; | 
| 184 |  | 
| 185 | 		return str.str(); | 
| 186 | 	} | 
| 187 | }; | 
| 188 |  | 
| 189 | /*! \brief Distributed linearized key | 
| 190 |  * | 
| 191 |  * | 
| 192 |  * | 
| 193 |  */ | 
| 194 | class grid_dist_lin_dx | 
| 195 | { | 
| 196 | 	//! grid list counter | 
| 197 | 	size_t g_c; | 
| 198 |  | 
| 199 | 	//! Local grid iterator | 
| 200 | 	size_t key; | 
| 201 |  | 
| 202 | public: | 
| 203 |  | 
| 204 | 	/*! \brief Set the local grid | 
| 205 | 	 * | 
| 206 | 	 * \param sub local grid | 
| 207 | 	 * | 
| 208 | 	 */ | 
| 209 | 	inline void setSub(size_t sub) | 
| 210 | 	{ | 
| 211 | 		g_c = sub; | 
| 212 | 	} | 
| 213 |  | 
| 214 | 	/*! \brief Get the local grid | 
| 215 | 	 * | 
| 216 | 	 * \return the id of the local grid | 
| 217 | 	 * | 
| 218 | 	 */ | 
| 219 | 	inline size_t getSub() const | 
| 220 | 	{ | 
| 221 | 		return g_c; | 
| 222 | 	} | 
| 223 |  | 
| 224 | 	/*! \brief Get the key | 
| 225 | 	 * | 
| 226 | 	 * \return the local key | 
| 227 | 	 * | 
| 228 | 	 */ | 
| 229 | 	inline size_t getKey() const | 
| 230 | 	{ | 
| 231 | 		return key; | 
| 232 | 	} | 
| 233 |  | 
| 234 |  | 
| 235 | 	/*! \brief Get the reference key | 
| 236 | 	 * | 
| 237 | 	 * \return the local key | 
| 238 | 	 * | 
| 239 | 	 */ | 
| 240 | 	inline size_t & getKeyRef() | 
| 241 | 	{ | 
| 242 | 		return key; | 
| 243 | 	} | 
| 244 |  | 
| 245 | 	/* \brief Check if two key are the same | 
| 246 | 	 * | 
| 247 | 	 * \param key_t key to check | 
| 248 | 	 * | 
| 249 | 	 * \return true if the two key are equal | 
| 250 | 	 * | 
| 251 | 	 */ | 
| 252 |  | 
| 253 | 	inline bool operator==(const grid_dist_lin_dx & key_t) | 
| 254 | 	{ | 
| 255 | 		if (g_c != key_t.g_c) | 
| 256 | 			return false; | 
| 257 |  | 
| 258 | 		// Check the two key index by index | 
| 259 |  | 
| 260 | 		return getKey() == key_t.getKey(); | 
| 261 | 	} | 
| 262 |  | 
| 263 |  | 
| 264 | 	/*! \brief Constructor set the sub-domain grid and the position in local coordinates | 
| 265 | 	 * | 
| 266 | 	 * \param g_c sub-domain | 
| 267 | 	 * \param key key | 
| 268 | 	 * | 
| 269 | 	 */ | 
| 270 | 	inline grid_dist_lin_dx(int g_c, size_t key) | 
| 271 | 	:g_c(g_c),key(key) | 
| 272 | 	{ | 
| 273 | 	} | 
| 274 |  | 
| 275 | 	//! Constructor | 
| 276 | 	inline grid_dist_lin_dx(){} | 
| 277 |  | 
| 278 | 	/*! \brief convert the key to string | 
| 279 | 	 * | 
| 280 | 	 * | 
| 281 | 	 */ | 
| 282 | 	std::string to_string() | 
| 283 | 	{ | 
| 284 | 		std::stringstream str; | 
| 285 |  | 
| 286 | 		str << "sub_domain="  << g_c << " " ; | 
| 287 | 		str << "lin_id="  << key << " " ; | 
| 288 |  | 
| 289 | 		str << "\n" ; | 
| 290 |  | 
| 291 | 		return str.str(); | 
| 292 | 	} | 
| 293 | }; | 
| 294 |  | 
| 295 | /*! \brief Distributed linearized key | 
| 296 |  * | 
| 297 |  * instead of having the sub-subdomain index it store directly a pointer to the grid | 
| 298 |  * | 
| 299 |  */ | 
| 300 | template<typename device_grid> | 
| 301 | class grid_dist_g_dx | 
| 302 | { | 
| 303 | 	//! grid list counter | 
| 304 | 	device_grid * dg; | 
| 305 |  | 
| 306 | 	//! Local grid iterator | 
| 307 | 	size_t key; | 
| 308 |  | 
| 309 | public: | 
| 310 |  | 
| 311 | 	/*! \brief return the sub-domain grid | 
| 312 | 	 * | 
| 313 | 	 * | 
| 314 | 	 */ | 
| 315 | 	inline device_grid * getSub() const | 
| 316 | 	{ | 
| 317 | 		return dg; | 
| 318 | 	} | 
| 319 |  | 
| 320 |  | 
| 321 | 	/*! \brief Get the key | 
| 322 | 	 * | 
| 323 | 	 * \return the local key | 
| 324 | 	 * | 
| 325 | 	 */ | 
| 326 | 	inline size_t getKey() const | 
| 327 | 	{ | 
| 328 | 		return key; | 
| 329 | 	} | 
| 330 |  | 
| 331 |  | 
| 332 | 	/*! \brief Get the reference key | 
| 333 | 	 * | 
| 334 | 	 * \return the local key | 
| 335 | 	 * | 
| 336 | 	 */ | 
| 337 | 	inline size_t & getKeyRef() | 
| 338 | 	{ | 
| 339 | 		return key; | 
| 340 | 	} | 
| 341 |  | 
| 342 | 	/* \brief Check if two key are the same | 
| 343 | 	 * | 
| 344 | 	 * \param key_t key to check | 
| 345 | 	 * | 
| 346 | 	 * \return true if the two key are equal | 
| 347 | 	 * | 
| 348 | 	 */ | 
| 349 |  | 
| 350 | 	inline bool operator==(const grid_dist_g_dx & key_t) | 
| 351 | 	{ | 
| 352 | 		if (dg != key_t.dg) | 
| 353 | 			return false; | 
| 354 |  | 
| 355 | 		// Check the two key index by index | 
| 356 |  | 
| 357 | 		return getKey() == key_t.getKey(); | 
| 358 | 	} | 
| 359 |  | 
| 360 |  | 
| 361 | 	/*! \brief Constructor | 
| 362 | 	 * | 
| 363 | 	 * \param dg array of local grid | 
| 364 | 	 * \param key actual position linearized | 
| 365 | 	 * | 
| 366 | 	 */ | 
| 367 | 	inline grid_dist_g_dx(device_grid * dg, size_t key) | 
| 368 | 	:dg(dg),key(key) | 
| 369 | 	{ | 
| 370 | 	} | 
| 371 |  | 
| 372 | 	//! Constructor | 
| 373 | 	inline grid_dist_g_dx(){} | 
| 374 |  | 
| 375 | 	/*! \brief convert the key to string | 
| 376 | 	 * | 
| 377 | 	 * \return a string representing the position | 
| 378 | 	 * | 
| 379 | 	 */ | 
| 380 | 	std::string to_string() | 
| 381 | 	{ | 
| 382 | 		std::stringstream str; | 
| 383 |  | 
| 384 | 		str << "sub_domain="  << dg << " " ; | 
| 385 | 		str << "lin_id="  << key << " " ; | 
| 386 |  | 
| 387 | 		str << "\n" ; | 
| 388 |  | 
| 389 | 		return str.str(); | 
| 390 | 	} | 
| 391 | }; | 
| 392 |  | 
| 393 | #endif | 
| 394 |  |