| 1 |
/* |
| 2 |
* (C) Copyright 2013 ECMWF. |
| 3 |
* |
| 4 |
* This software is licensed under the terms of the Apache Licence Version 2.0 |
| 5 |
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. |
| 6 |
* In applying this licence, ECMWF does not waive the privileges and immunities |
| 7 |
* granted to it by virtue of its status as an intergovernmental organisation |
| 8 |
* nor does it submit to any jurisdiction. |
| 9 |
*/ |
| 10 |
|
| 11 |
/// @file Connectivity.h |
| 12 |
/// @author Willem Deconinck |
| 13 |
/// @date October 2015 |
| 14 |
/// @details |
| 15 |
/// This file contains Connectivity classes |
| 16 |
/// - IrregularConnectivity |
| 17 |
/// - BlockConnectivity |
| 18 |
/// - MultiBlockConnectivity |
| 19 |
/// |
| 20 |
/// It is important to note that connectivity access functions are |
| 21 |
/// inlined for optimal performance. The connectivity itself is internally |
| 22 |
/// stored with base 1, to be compatible with Fortran access. |
| 23 |
/// C++ access operators however convert the resulting connectivity to base 0. |
| 24 |
|
| 25 |
#pragma once |
| 26 |
|
| 27 |
#include <array> |
| 28 |
#include <cstring> |
| 29 |
#include <initializer_list> |
| 30 |
|
| 31 |
#include "atlas/array.h" |
| 32 |
#include "atlas/array/ArrayView.h" |
| 33 |
#include "atlas/array/DataType.h" |
| 34 |
#include "atlas/array/IndexView.h" |
| 35 |
#include "atlas/array/SVector.h" |
| 36 |
#include "atlas/array_fwd.h" |
| 37 |
#include "atlas/library/config.h" |
| 38 |
#include "atlas/util/Object.h" |
| 39 |
|
| 40 |
namespace eckit { |
| 41 |
class Stream; |
| 42 |
} |
| 43 |
|
| 44 |
namespace atlas { |
| 45 |
namespace mesh { |
| 46 |
|
| 47 |
constexpr size_t MAX_STRING_SIZE() { |
| 48 |
return 60; |
| 49 |
} |
| 50 |
|
| 51 |
template <typename ConnectivityImpl> |
| 52 |
class ConnectivityInterface : public util::Object, DOXYGEN_HIDE(public ConnectivityImpl) { |
| 53 |
using ConnectivityImpl::ConnectivityImpl; |
| 54 |
using util::Object::Object; |
| 55 |
}; |
| 56 |
|
| 57 |
// Classes defined in this file: |
| 58 |
class IrregularConnectivityImpl; |
| 59 |
class BlockConnectivityImpl; |
| 60 |
class MultiBlockConnectivityImpl; |
| 61 |
|
| 62 |
// -------------------------------------------------------------------------- |
| 63 |
|
| 64 |
#ifndef DOXYGEN_SHOULD_SKIP_THIS |
| 65 |
#if ATLAS_HAVE_FORTRAN |
| 66 |
#define INDEX_DEREF Index |
| 67 |
#define INDEX_REF Index |
| 68 |
#define FROM_FORTRAN -1 |
| 69 |
#define TO_FORTRAN +1 |
| 70 |
#else |
| 71 |
#define INDEX_DEREF * |
| 72 |
#define INDEX_REF idx_t& |
| 73 |
#define FROM_FORTRAN |
| 74 |
#define TO_FORTRAN |
| 75 |
#endif |
| 76 |
#endif |
| 77 |
|
| 78 |
/// @brief Irregular Connectivity Table |
| 79 |
/// @author Willem Deconinck |
| 80 |
/// |
| 81 |
/// Container for irregular connectivity tables. This is e.g. the case |
| 82 |
/// for a node-to-X connectivity. |
| 83 |
/// connectivity = [ |
| 84 |
/// 1 2 3 4 5 6 # node 1 |
| 85 |
/// 7 8 # node 2 |
| 86 |
/// 9 10 11 12 # node 3 |
| 87 |
/// 13 14 15 # node 4 |
| 88 |
/// 16 17 18 19 20 21 # node 5 |
| 89 |
/// ] |
| 90 |
/// There are 2 modes of construction: |
| 91 |
/// - It wraps existing raw data |
| 92 |
/// - It owns the connectivity data |
| 93 |
/// |
| 94 |
/// In case ATLAS_HAVE_FORTRAN is defined (which is usually the case), |
| 95 |
/// the raw data will be stored with base 1 for Fortran interoperability. |
| 96 |
/// The operator(row,col) will then do the conversion to base 0. |
| 97 |
/// |
| 98 |
/// In the first mode of construction, the connectivity table cannot be resized. |
| 99 |
/// In the second mode of construction, resizing is possible |
| 100 |
|
| 101 |
namespace detail { |
| 102 |
// FortranIndex: |
| 103 |
// Helper class that does +1 and -1 operations on stored values |
| 104 |
|
| 105 |
class ConnectivityIndex { |
| 106 |
public: |
| 107 |
enum |
| 108 |
{ |
| 109 |
BASE = 1 |
| 110 |
}; |
| 111 |
|
| 112 |
public: |
| 113 |
ATLAS_HOST_DEVICE ConnectivityIndex(const ConnectivityIndex& other) { set(other.get()); } |
| 114 |
ATLAS_HOST_DEVICE ConnectivityIndex(idx_t* idx): idx_(idx) {} |
| 115 |
ATLAS_HOST_DEVICE void set(const idx_t& value) { *(idx_) = value + BASE; } |
| 116 |
ATLAS_HOST_DEVICE idx_t get() const { return *(idx_)-BASE; } |
| 117 |
ATLAS_HOST_DEVICE void operator=(const idx_t& value) { set(value); } |
| 118 |
ATLAS_HOST_DEVICE ConnectivityIndex& operator+(const idx_t& value) { |
| 119 |
*(idx_) += value; |
| 120 |
return *this; |
| 121 |
} |
| 122 |
ATLAS_HOST_DEVICE ConnectivityIndex& operator-(const idx_t& value) { |
| 123 |
*(idx_) -= value; |
| 124 |
return *this; |
| 125 |
} |
| 126 |
ATLAS_HOST_DEVICE ConnectivityIndex& operator--() { |
| 127 |
--(*(idx_)); |
| 128 |
return *this; |
| 129 |
} |
| 130 |
ATLAS_HOST_DEVICE ConnectivityIndex& operator++() { |
| 131 |
++(*(idx_)); |
| 132 |
return *this; |
| 133 |
} |
| 134 |
|
| 135 |
// implicit conversion |
| 136 |
ATLAS_HOST_DEVICE operator idx_t() const { return get(); } |
| 137 |
|
| 138 |
private: |
| 139 |
idx_t* idx_; |
| 140 |
}; |
| 141 |
} // namespace detail |
| 142 |
|
| 143 |
class ConnectivityRow { |
| 144 |
#if ATLAS_HAVE_FORTRAN |
| 145 |
using Index = detail::ConnectivityIndex; |
| 146 |
#else |
| 147 |
using Index = idx_t&; |
| 148 |
#endif |
| 149 |
public: |
| 150 |
ATLAS_HOST_DEVICE |
| 151 |
ConnectivityRow(idx_t* data, idx_t size): data_(data), size_(size) {} |
| 152 |
|
| 153 |
template <typename Int> |
| 154 |
ATLAS_HOST_DEVICE idx_t operator()(Int i) const { |
| 155 |
return data_[i] FROM_FORTRAN; |
| 156 |
} |
| 157 |
|
| 158 |
template <typename Int> |
| 159 |
ATLAS_HOST_DEVICE INDEX_REF operator()(Int i) { |
| 160 |
return INDEX_DEREF(data_ + i); |
| 161 |
} |
| 162 |
|
| 163 |
ATLAS_HOST_DEVICE |
| 164 |
idx_t size() const { return size_; } |
| 165 |
|
| 166 |
private: |
| 167 |
idx_t* data_; |
| 168 |
idx_t size_; |
| 169 |
}; |
| 170 |
|
| 171 |
class IrregularConnectivityImpl { |
| 172 |
public: |
| 173 |
typedef ConnectivityRow Row; |
| 174 |
|
| 175 |
public: |
| 176 |
//-- Constructors |
| 177 |
|
| 178 |
/// @brief Construct connectivity table that needs resizing a-posteriori |
| 179 |
/// Data is owned |
| 180 |
IrregularConnectivityImpl(const std::string& name = ""); |
| 181 |
|
| 182 |
/// @brief Construct connectivity table wrapping existing raw data. |
| 183 |
/// No resizing can be performed as data is not owned. |
| 184 |
IrregularConnectivityImpl(idx_t values[], idx_t rows, idx_t displs[], idx_t counts[]); |
| 185 |
|
| 186 |
#if ATLAS_HIC_COMPILER |
| 187 |
/// @brief Copy ctr (only to be used when calling a cuda kernel) |
| 188 |
// This ctr has to be defined in the header, since __CUDACC__ will identify |
| 189 |
// whether |
| 190 |
// it is compiled it for a GPU kernel |
| 191 |
IrregularConnectivityImpl(const IrregularConnectivityImpl& other): |
| 192 |
owns_(false), |
| 193 |
values_(other.values_), |
| 194 |
displs_(other.displs_), |
| 195 |
counts_(other.counts_), |
| 196 |
missing_value_(other.missing_value_), |
| 197 |
rows_(other.rows_), |
| 198 |
maxcols_(other.maxcols_), |
| 199 |
mincols_(other.mincols_), |
| 200 |
ctxt_(nullptr) {} |
| 201 |
#endif |
| 202 |
|
| 203 |
/// @brief Construct a mesh from a Stream (serialization) |
| 204 |
explicit IrregularConnectivityImpl(eckit::Stream&); |
| 205 |
|
| 206 |
virtual ~IrregularConnectivityImpl(); |
| 207 |
|
| 208 |
//-- Accessors |
| 209 |
|
| 210 |
/// @brief Name associated to this Connetivity |
| 211 |
const std::string name() const { return std::string(name_); } |
| 212 |
|
| 213 |
/// @brief Rename this Connectivity |
| 214 |
void rename(const std::string& name) { |
| 215 |
std::strncpy(name_, name.c_str(), std::max(name.size(), MAX_STRING_SIZE())); |
| 216 |
} |
| 217 |
|
| 218 |
/// @brief Number of rows in the connectivity table |
| 219 |
ATLAS_HOST_DEVICE |
| 220 |
idx_t rows() const { return rows_; } |
| 221 |
|
| 222 |
/// @brief Number of columns for specified row in the connectivity table |
| 223 |
ATLAS_HOST_DEVICE |
| 224 |
idx_t cols(idx_t row_idx) const { return counts_[row_idx]; } |
| 225 |
|
| 226 |
/// @brief Maximum value for number of columns over all rows |
| 227 |
ATLAS_HOST_DEVICE |
| 228 |
idx_t maxcols() const { return maxcols_; } |
| 229 |
|
| 230 |
/// @brief Minimum value for number of columns over all rows |
| 231 |
ATLAS_HOST_DEVICE |
| 232 |
idx_t mincols() const { return mincols_; } |
| 233 |
|
| 234 |
/// @brief Access to connectivity table elements for given row and column |
| 235 |
/// The returned index has base 0 regardless if ATLAS_HAVE_FORTRAN is defined. |
| 236 |
ATLAS_HOST_DEVICE |
| 237 |
idx_t operator()(idx_t row_idx, idx_t col_idx) const; |
| 238 |
|
| 239 |
idx_t size() const { return values_.size(); } |
| 240 |
|
| 241 |
ATLAS_HOST_DEVICE |
| 242 |
idx_t missing_value() const { return missing_value_; } |
| 243 |
|
| 244 |
ATLAS_HOST_DEVICE |
| 245 |
Row row(idx_t row_idx) const; |
| 246 |
|
| 247 |
// -- Modifiers |
| 248 |
|
| 249 |
/// @brief Modify row with given values. Values must be given with base 0 |
| 250 |
void set(idx_t row_idx, const idx_t column_values[]); |
| 251 |
|
| 252 |
/// @brief Modify (row,col) with given value. Value must be given with base 0 |
| 253 |
void set(idx_t row_idx, idx_t col_idx, const idx_t value); |
| 254 |
|
| 255 |
/// @brief Resize connectivity |
| 256 |
/// @note Can only be used when data is owned. |
| 257 |
virtual void resize(idx_t old_size, idx_t size, bool initialize, const idx_t values[], bool fortran_array); |
| 258 |
|
| 259 |
/// @brief Resize connectivity, and add given rows |
| 260 |
/// @note Can only be used when data is owned. |
| 261 |
virtual void add(idx_t rows, idx_t cols, const idx_t values[], bool fortran_array = false); |
| 262 |
|
| 263 |
/// @brief Resize connectivity, and add given rows with missing values |
| 264 |
/// @note Can only be used when data is owned. |
| 265 |
virtual void add(idx_t rows, idx_t cols); |
| 266 |
|
| 267 |
/// @brief Resize connectivity, and add given rows with missing values |
| 268 |
/// @note Can only be used when data is owned. |
| 269 |
virtual void add(idx_t rows, const idx_t cols[]); |
| 270 |
|
| 271 |
/// @brief Resize connectivity, and copy from a BlockConnectivity |
| 272 |
virtual void add(const BlockConnectivityImpl& block); |
| 273 |
|
| 274 |
/// @brief Resize connectivity, and insert given rows |
| 275 |
/// @note Can only be used when data is owned. |
| 276 |
virtual void insert(idx_t position, idx_t rows, idx_t cols, const idx_t values[], bool fortran_array = false); |
| 277 |
|
| 278 |
/// @brief Resize connectivity, and insert given rows with missing values |
| 279 |
/// @note Can only be used when data is owned. |
| 280 |
virtual void insert(idx_t position, idx_t rows, idx_t cols); |
| 281 |
|
| 282 |
/// @brief Resize connectivity, and insert given rows with missing values |
| 283 |
/// @note Can only be used when data is owned. |
| 284 |
virtual void insert(idx_t position, idx_t rows, const idx_t cols[]); |
| 285 |
|
| 286 |
virtual void clear(); |
| 287 |
|
| 288 |
virtual size_t footprint() const; |
| 289 |
|
| 290 |
idx_t displs(const idx_t row) const { return displs_[row]; } |
| 291 |
|
| 292 |
void dump(std::ostream& os) const; |
| 293 |
friend std::ostream& operator<<(std::ostream& os, const IrregularConnectivityImpl& p) { |
| 294 |
p.dump(os); |
| 295 |
return os; |
| 296 |
} |
| 297 |
|
| 298 |
virtual void encode(eckit::Stream& s) const { encode_(s); } |
| 299 |
virtual void decode(eckit::Stream& s) { decode_(s); } |
| 300 |
|
| 301 |
protected: |
| 302 |
bool owns() { return owns_; } |
| 303 |
const idx_t* displs() const { return displs_.data(); } |
| 304 |
const idx_t* counts() const { return counts_.data(); } |
| 305 |
|
| 306 |
/// @brief Serialization to Stream |
| 307 |
void encode_(eckit::Stream&) const; |
| 308 |
|
| 309 |
/// @brief Serialization from Stream |
| 310 |
void decode_(eckit::Stream&); |
| 311 |
|
| 312 |
friend eckit::Stream& operator<<(eckit::Stream& s, const IrregularConnectivityImpl& x) { |
| 313 |
x.encode(s); |
| 314 |
return s; |
| 315 |
} |
| 316 |
|
| 317 |
friend eckit::Stream& operator>>(eckit::Stream& s, IrregularConnectivityImpl& x) { |
| 318 |
x.decode(s); |
| 319 |
return s; |
| 320 |
} |
| 321 |
|
| 322 |
private: |
| 323 |
void on_delete(); |
| 324 |
void on_update(); |
| 325 |
|
| 326 |
private: |
| 327 |
char name_[MAX_STRING_SIZE()]; |
| 328 |
bool owns_; |
| 329 |
|
| 330 |
protected: |
| 331 |
array::SVector<idx_t> values_; |
| 332 |
array::SVector<idx_t> displs_; |
| 333 |
array::SVector<idx_t> counts_; |
| 334 |
|
| 335 |
idx_t missing_value_; |
| 336 |
idx_t rows_; |
| 337 |
idx_t maxcols_; |
| 338 |
idx_t mincols_; |
| 339 |
|
| 340 |
public: |
| 341 |
typedef void* ctxt_t; |
| 342 |
typedef void (*callback_t)(ctxt_t); |
| 343 |
|
| 344 |
private: |
| 345 |
friend class ConnectivityPrivateAccess; |
| 346 |
ctxt_t ctxt_; |
| 347 |
callback_t callback_update_; |
| 348 |
callback_t callback_delete_; |
| 349 |
}; |
| 350 |
|
| 351 |
// ---------------------------------------------------------------------------------------------- |
| 352 |
|
| 353 |
/// @brief Connectivity contiguously composed of multiple BlockConnectivityImpl |
| 354 |
/// @author Willem Deconinck |
| 355 |
/// |
| 356 |
/// Container for connectivity tables that are layed out in memory as multiple |
| 357 |
/// BlockConnectivities stitched together. |
| 358 |
/// This is e.g. the case for a element-node connectivity, with element-types |
| 359 |
/// grouped together: |
| 360 |
/// @code{.sh} |
| 361 |
/// connectivity = [ |
| 362 |
/// # triangles (block 0) |
| 363 |
/// 1 2 3 |
| 364 |
/// 4 5 6 |
| 365 |
/// 7 8 9 |
| 366 |
/// 10 11 12 |
| 367 |
/// # quadrilaterals (block 1) |
| 368 |
/// 13 14 15 16 |
| 369 |
/// 17 18 19 20 |
| 370 |
/// 21 22 23 24 |
| 371 |
/// ] |
| 372 |
/// @endcode |
| 373 |
/// |
| 374 |
/// This class can also be interpreted as a atlas::IrregularConnectivity without |
| 375 |
/// distinction between blocks |
| 376 |
/// |
| 377 |
/// There are 2 modes of construction: |
| 378 |
/// - It wraps existing raw data |
| 379 |
/// - It owns the connectivity data |
| 380 |
/// |
| 381 |
/// In case ATLAS_HAVE_FORTRAN is defined (which is usually the case), |
| 382 |
/// the raw data will be stored with base 1 for Fortran interoperability. |
| 383 |
/// The operator(row,col) will then do the conversion to base 0. |
| 384 |
/// |
| 385 |
/// In the first mode of construction, the connectivity table cannot be resized. |
| 386 |
/// In the second mode of construction, resizing is possible |
| 387 |
class MultiBlockConnectivityImpl : public IrregularConnectivityImpl { |
| 388 |
public: |
| 389 |
//-- Constructors |
| 390 |
|
| 391 |
/// @brief Construct connectivity table that needs resizing a-posteriori |
| 392 |
/// Data is owned |
| 393 |
MultiBlockConnectivityImpl(const std::string& name = ""); |
| 394 |
|
| 395 |
/// @brief Construct a mesh from a Stream (serialization) |
| 396 |
explicit MultiBlockConnectivityImpl(eckit::Stream&); |
| 397 |
|
| 398 |
virtual ~MultiBlockConnectivityImpl(); |
| 399 |
|
| 400 |
//-- Accessors |
| 401 |
|
| 402 |
/// @brief Number of blocks |
| 403 |
ATLAS_HOST_DEVICE |
| 404 |
idx_t blocks() const { return blocks_; } |
| 405 |
|
| 406 |
/// @brief Access to a block connectivity |
| 407 |
ATLAS_HOST_DEVICE |
| 408 |
const BlockConnectivityImpl& block(idx_t block_idx) const { return block_[block_idx]; } |
| 409 |
ATLAS_HOST_DEVICE |
| 410 |
BlockConnectivityImpl& block(idx_t block_idx) { return block_[block_idx]; } |
| 411 |
|
| 412 |
/// @brief Access to connectivity table elements for given row and column |
| 413 |
/// The row_idx counts up from 0, from block 0, as in IrregularConnectivity |
| 414 |
/// The returned index has base 0 regardless if ATLAS_HAVE_FORTRAN is defined. |
| 415 |
ATLAS_HOST_DEVICE |
| 416 |
idx_t operator()(idx_t row_idx, idx_t col_idx) const; |
| 417 |
|
| 418 |
/// @brief Access to connectivity table elements for given row and column |
| 419 |
/// The block_row_idx counts up from zero for every block_idx. |
| 420 |
/// The returned index has base 0 regardless if ATLAS_HAVE_FORTRAN is defined. |
| 421 |
ATLAS_HOST_DEVICE |
| 422 |
idx_t operator()(idx_t block_idx, idx_t block_row_idx, idx_t block_col_idx) const; |
| 423 |
|
| 424 |
//-- Modifiers |
| 425 |
|
| 426 |
/// @brief Resize connectivity, and add given rows as a new block |
| 427 |
/// @note Can only be used when data is owned. |
| 428 |
virtual void add(idx_t rows, idx_t cols, const idx_t values[], bool fortran_array = false); |
| 429 |
|
| 430 |
/// @brief Resize connectivity, and copy from a BlockConnectivity to a new |
| 431 |
/// block |
| 432 |
/// @note Can only be used when data is owned. |
| 433 |
virtual void add(const BlockConnectivityImpl&); |
| 434 |
|
| 435 |
/// @brief Resize connectivity, and add given rows with missing values |
| 436 |
/// @note Can only be used when data is owned. |
| 437 |
virtual void add(idx_t rows, idx_t cols); |
| 438 |
|
| 439 |
/// @brief Resize connectivity, and add given rows with missing values |
| 440 |
/// @note Can only be used when data is owned. |
| 441 |
virtual void add(idx_t rows, const idx_t cols[]); |
| 442 |
|
| 443 |
/// @brief Resize connectivity, and insert given rows |
| 444 |
/// @note Can only be used when data is owned. |
| 445 |
virtual void insert(idx_t position, idx_t rows, idx_t cols, const idx_t values[], bool fortran_array = false); |
| 446 |
|
| 447 |
/// @brief Resize connectivity, and insert given rows with missing values |
| 448 |
/// @note Can only be used when data is owned. |
| 449 |
virtual void insert(idx_t position, idx_t rows, idx_t cols); |
| 450 |
|
| 451 |
/// @brief Resize connectivity, and insert given rows with missing values |
| 452 |
/// @note Can only be used when data is owned. |
| 453 |
virtual void insert(idx_t position, idx_t rows, const idx_t cols[]); |
| 454 |
|
| 455 |
virtual void clear(); |
| 456 |
|
| 457 |
virtual size_t footprint() const; |
| 458 |
|
| 459 |
virtual void encode(eckit::Stream& s) const { |
| 460 |
IrregularConnectivityImpl::encode_(s); |
| 461 |
encode_(s); |
| 462 |
} |
| 463 |
virtual void decode(eckit::Stream& s) { |
| 464 |
IrregularConnectivityImpl::decode_(s); |
| 465 |
decode_(s); |
| 466 |
} |
| 467 |
|
| 468 |
protected: |
| 469 |
/// @brief Serialization to Stream |
| 470 |
void encode_(eckit::Stream&) const; |
| 471 |
|
| 472 |
/// @brief Serialization from Stream |
| 473 |
void decode_(eckit::Stream&); |
| 474 |
|
| 475 |
friend eckit::Stream& operator<<(eckit::Stream& s, const MultiBlockConnectivityImpl& x) { |
| 476 |
x.encode(s); |
| 477 |
return s; |
| 478 |
} |
| 479 |
|
| 480 |
friend eckit::Stream& operator>>(eckit::Stream& s, MultiBlockConnectivityImpl& x) { |
| 481 |
x.decode(s); |
| 482 |
return s; |
| 483 |
} |
| 484 |
|
| 485 |
private: |
| 486 |
void rebuild_block_connectivity(); |
| 487 |
|
| 488 |
private: |
| 489 |
idx_t blocks_; |
| 490 |
array::SVector<idx_t> block_displs_; |
| 491 |
array::SVector<idx_t> block_cols_; |
| 492 |
|
| 493 |
array::SVector<BlockConnectivityImpl> block_; |
| 494 |
}; |
| 495 |
|
| 496 |
// ----------------------------------------------------------------------------------------------------- |
| 497 |
|
| 498 |
/// @brief Block Connectivity table |
| 499 |
/// @author Willem Deconinck |
| 500 |
/// Container for connectivity tables that are layed out in memory as a block. |
| 501 |
/// Every row has the same |
| 502 |
/// number of columns. |
| 503 |
/// |
| 504 |
/// There are 2 modes of construction: |
| 505 |
/// - It wraps existing raw data |
| 506 |
/// - It owns the connectivity data |
| 507 |
/// |
| 508 |
/// In case ATLAS_HAVE_FORTRAN is defined (which is usually the case), |
| 509 |
/// the raw data will be stored with base 1 for Fortran interoperability. |
| 510 |
/// The operator(row,col) will then do the conversion to base 0. |
| 511 |
/// |
| 512 |
/// In the first mode of construction, the connectivity table cannot be resized. |
| 513 |
/// In the second mode of construction, resizing is possible |
| 514 |
class BlockConnectivityImpl { |
| 515 |
private: |
| 516 |
friend class IrregularConnectivityImpl; |
| 517 |
friend class MultiBlockConnectivityImpl; |
| 518 |
BlockConnectivityImpl(idx_t rows, idx_t cols, idx_t values[], bool dummy); |
| 519 |
|
| 520 |
public: |
| 521 |
//-- Constructors |
| 522 |
|
| 523 |
/// @brief Construct connectivity table that needs resizing a-posteriori |
| 524 |
/// Data is owned |
| 525 |
BlockConnectivityImpl(); |
| 526 |
BlockConnectivityImpl(idx_t rows, idx_t cols, const std::initializer_list<idx_t>&); |
| 527 |
|
| 528 |
/// @brief Construct connectivity table wrapping existing raw data. |
| 529 |
/// No resizing can be performed as data is not owned. |
| 530 |
BlockConnectivityImpl(idx_t rows, idx_t cols, idx_t values[]); |
| 531 |
|
| 532 |
#if ATLAS_HIC_COMPILER |
| 533 |
/// @brief Copy ctr (only to be used when calling a cuda kernel) |
| 534 |
// This ctr has to be defined in the header, since __CUDACC__ will identify |
| 535 |
// whether it is compiled it for a GPU kernel |
| 536 |
BlockConnectivityImpl(const BlockConnectivityImpl& other): |
| 537 |
owns_(false), |
| 538 |
values_(other.values_), |
| 539 |
rows_(other.rows_), |
| 540 |
cols_(other.cols_), |
| 541 |
missing_value_(other.missing_value_) {} |
| 542 |
#endif |
| 543 |
|
| 544 |
BlockConnectivityImpl(BlockConnectivityImpl&& other); |
| 545 |
|
| 546 |
BlockConnectivityImpl& operator=(const BlockConnectivityImpl& other); |
| 547 |
BlockConnectivityImpl& operator=(BlockConnectivityImpl&& other); |
| 548 |
|
| 549 |
/// @brief Construct a mesh from a Stream (serialization) |
| 550 |
explicit BlockConnectivityImpl(eckit::Stream&); |
| 551 |
|
| 552 |
/// @brief Destructor |
| 553 |
~BlockConnectivityImpl(); |
| 554 |
|
| 555 |
ATLAS_HOST_DEVICE |
| 556 |
idx_t index(idx_t i, idx_t j) const; |
| 557 |
|
| 558 |
//-- Accessors |
| 559 |
|
| 560 |
/// @brief Access to connectivity table elements for given row and column |
| 561 |
/// The returned index has base 0 regardless if ATLAS_HAVE_FORTRAN is defined. |
| 562 |
ATLAS_HOST_DEVICE |
| 563 |
idx_t operator()(idx_t row_idx, idx_t col_idx) const; |
| 564 |
|
| 565 |
/// @brief Number of rows |
| 566 |
ATLAS_HOST_DEVICE |
| 567 |
idx_t rows() const { return rows_; } |
| 568 |
|
| 569 |
/// @brief Number of columns |
| 570 |
ATLAS_HOST_DEVICE |
| 571 |
idx_t cols() const { return cols_; } |
| 572 |
|
| 573 |
/// @brief Access to raw data. |
| 574 |
/// Note that the connectivity base is 1 in case ATLAS_HAVE_FORTRAN is |
| 575 |
/// defined. |
| 576 |
ATLAS_HOST_DEVICE |
| 577 |
const idx_t* data() const { return values_.data(); } |
| 578 |
ATLAS_HOST_DEVICE |
| 579 |
idx_t* data() { return values_.data(); } |
| 580 |
|
| 581 |
ATLAS_HOST_DEVICE |
| 582 |
idx_t missing_value() const { return missing_value_; } |
| 583 |
|
| 584 |
size_t footprint() const; |
| 585 |
|
| 586 |
//-- Modifiers |
| 587 |
|
| 588 |
/// @brief Modify row with given values. Values must be given with base 0 |
| 589 |
ATLAS_HOST_DEVICE |
| 590 |
void set(idx_t row_idx, const idx_t column_values[]); |
| 591 |
|
| 592 |
/// @brief Modify (row,col) with given value. Value must be given with base 0 |
| 593 |
ATLAS_HOST_DEVICE |
| 594 |
void set(idx_t row_idx, idx_t col_idx, const idx_t value); |
| 595 |
|
| 596 |
/// @brief Resize connectivity, and add given rows |
| 597 |
/// @note Can only be used when data is owned. |
| 598 |
void add(idx_t rows, idx_t cols, const idx_t values[], bool fortran_array = false); |
| 599 |
|
| 600 |
bool owns() const { return owns_; } |
| 601 |
|
| 602 |
|
| 603 |
friend std::ostream& operator<<(std::ostream& out, const BlockConnectivityImpl& x) { |
| 604 |
x.print(out); |
| 605 |
return out; |
| 606 |
} |
| 607 |
|
| 608 |
|
| 609 |
protected: |
| 610 |
/// @brief Wrap existing and set owns_ = false |
| 611 |
void rebuild(idx_t rows, idx_t cols, idx_t values[]); |
| 612 |
|
| 613 |
|
| 614 |
/// @brief Serialization to Stream |
| 615 |
void encode(eckit::Stream&) const; |
| 616 |
|
| 617 |
/// @brief Serialization from Stream |
| 618 |
void decode(eckit::Stream&); |
| 619 |
|
| 620 |
friend eckit::Stream& operator<<(eckit::Stream& s, const BlockConnectivityImpl& x) { |
| 621 |
x.encode(s); |
| 622 |
return s; |
| 623 |
} |
| 624 |
|
| 625 |
friend eckit::Stream& operator>>(eckit::Stream& s, BlockConnectivityImpl& x) { |
| 626 |
x.decode(s); |
| 627 |
return s; |
| 628 |
} |
| 629 |
|
| 630 |
void print(std::ostream&) const; |
| 631 |
|
| 632 |
private: |
| 633 |
bool owns_; |
| 634 |
array::SVector<idx_t> values_; |
| 635 |
|
| 636 |
idx_t rows_; |
| 637 |
idx_t cols_; |
| 638 |
idx_t missing_value_; |
| 639 |
}; |
| 640 |
|
| 641 |
using IrregularConnectivity = ConnectivityInterface<IrregularConnectivityImpl>; |
| 642 |
using MultiBlockConnectivity = ConnectivityInterface<MultiBlockConnectivityImpl>; |
| 643 |
using BlockConnectivity = BlockConnectivityImpl; |
| 644 |
using Connectivity = IrregularConnectivity; |
| 645 |
|
| 646 |
// ----------------------------------------------------------------------------------------------------- |
| 647 |
|
| 648 |
ATLAS_HOST_DEVICE |
| 649 |
inline idx_t IrregularConnectivityImpl::operator()(idx_t row_idx, idx_t col_idx) const { |
| 650 |
assert(counts_[row_idx] > (col_idx)); |
| 651 |
return values_[displs_[row_idx] + col_idx] FROM_FORTRAN; |
| 652 |
} |
| 653 |
|
| 654 |
inline void IrregularConnectivityImpl::set(idx_t row_idx, const idx_t column_values[]) { |
| 655 |
const idx_t N = counts_[row_idx]; |
| 656 |
for (idx_t n = 0; n < N; ++n) { |
| 657 |
values_[displs_[row_idx] + n] = column_values[n] TO_FORTRAN; |
| 658 |
} |
| 659 |
} |
| 660 |
|
| 661 |
inline void IrregularConnectivityImpl::set(idx_t row_idx, idx_t col_idx, const idx_t value) { |
| 662 |
assert(col_idx < counts_[row_idx]); |
| 663 |
values_[displs_[row_idx] + col_idx] = value TO_FORTRAN; |
| 664 |
} |
| 665 |
|
| 666 |
ATLAS_HOST_DEVICE |
| 667 |
inline IrregularConnectivityImpl::Row IrregularConnectivityImpl::row(idx_t row_idx) const { |
| 668 |
return IrregularConnectivityImpl::Row(const_cast<idx_t*>(values_.data()) + displs_(row_idx), counts_(row_idx)); |
| 669 |
} |
| 670 |
|
| 671 |
// ----------------------------------------------------------------------------------------------------- |
| 672 |
|
| 673 |
ATLAS_HOST_DEVICE |
| 674 |
inline idx_t MultiBlockConnectivityImpl::operator()(idx_t row_idx, idx_t col_idx) const { |
| 675 |
return IrregularConnectivityImpl::operator()(row_idx, col_idx); |
| 676 |
} |
| 677 |
|
| 678 |
ATLAS_HOST_DEVICE |
| 679 |
inline idx_t MultiBlockConnectivityImpl::operator()(idx_t block_idx, idx_t block_row_idx, idx_t block_col_idx) const { |
| 680 |
return block(block_idx)(block_row_idx, block_col_idx); |
| 681 |
} |
| 682 |
|
| 683 |
// ----------------------------------------------------------------------------------------------------- |
| 684 |
|
| 685 |
ATLAS_HOST_DEVICE |
| 686 |
inline idx_t BlockConnectivityImpl::operator()(idx_t row_idx, idx_t col_idx) const { |
| 687 |
return values_[index(row_idx, col_idx)] FROM_FORTRAN; |
| 688 |
} |
| 689 |
|
| 690 |
ATLAS_HOST_DEVICE |
| 691 |
inline void BlockConnectivityImpl::set(idx_t row_idx, const idx_t column_values[]) { |
| 692 |
for (idx_t n = 0; n < cols_; ++n) { |
| 693 |
values_[index(row_idx, n)] = column_values[n] TO_FORTRAN; |
| 694 |
} |
| 695 |
} |
| 696 |
|
| 697 |
ATLAS_HOST_DEVICE |
| 698 |
inline void BlockConnectivityImpl::set(idx_t row_idx, idx_t col_idx, const idx_t value) { |
| 699 |
values_[index(row_idx, col_idx)] = value TO_FORTRAN; |
| 700 |
} |
| 701 |
|
| 702 |
ATLAS_HOST_DEVICE |
| 703 |
inline idx_t BlockConnectivityImpl::index(idx_t i, idx_t j) const { |
| 704 |
return i * cols_ + j; |
| 705 |
} |
| 706 |
|
| 707 |
// ------------------------------------------------------------------------------------------------------ |
| 708 |
|
| 709 |
extern "C" { |
| 710 |
Connectivity* atlas__Connectivity__create(); |
| 711 |
MultiBlockConnectivity* atlas__MultiBlockConnectivity__create(); |
| 712 |
const char* atlas__Connectivity__name(Connectivity* This); |
| 713 |
void atlas__Connectivity__rename(Connectivity* This, const char* name); |
| 714 |
void atlas__Connectivity__delete(Connectivity* This); |
| 715 |
void atlas__Connectivity__displs(Connectivity* This, idx_t*& displs, idx_t& size); |
| 716 |
void atlas__Connectivity__counts(Connectivity* This, idx_t*& counts, idx_t& size); |
| 717 |
void atlas__Connectivity__values(Connectivity* This, idx_t*& values, idx_t& size); |
| 718 |
idx_t atlas__Connectivity__rows(const Connectivity* This); |
| 719 |
void atlas__Connectivity__add_values(Connectivity* This, idx_t rows, idx_t cols, idx_t values[]); |
| 720 |
void atlas__Connectivity__add_missing(Connectivity* This, idx_t rows, idx_t cols); |
| 721 |
idx_t atlas__Connectivity__missing_value(const Connectivity* This); |
| 722 |
|
| 723 |
idx_t atlas__MultiBlockConnectivity__blocks(const MultiBlockConnectivity* This); |
| 724 |
BlockConnectivityImpl* atlas__MultiBlockConnectivity__block(MultiBlockConnectivity* This, idx_t block_idx); |
| 725 |
|
| 726 |
idx_t atlas__BlockConnectivity__rows(const BlockConnectivityImpl* This); |
| 727 |
idx_t atlas__BlockConnectivity__cols(const BlockConnectivityImpl* This); |
| 728 |
idx_t atlas__BlockConnectivity__missing_value(const BlockConnectivityImpl* This); |
| 729 |
void atlas__BlockConnectivity__data(BlockConnectivityImpl* This, idx_t*& data, idx_t& rows, idx_t& cols); |
| 730 |
void atlas__BlockConnectivity__delete(BlockConnectivityImpl* This); |
| 731 |
} |
| 732 |
|
| 733 |
#undef FROM_FORTRAN |
| 734 |
#undef TO_FORTRAN |
| 735 |
#undef INDEX_DEREF |
| 736 |
|
| 737 |
//------------------------------------------------------------------------------------------------------ |
| 738 |
|
| 739 |
} // namespace mesh |
| 740 |
|
| 741 |
namespace array { |
| 742 |
|
| 743 |
// TODO HACK |
| 744 |
// template<> inline DataType::kind_t |
| 745 |
// DataType::kind<mesh::BlockConnectivityImpl*>() { return KIND_INT32; } |
| 746 |
} |
| 747 |
|
| 748 |
} // namespace atlas |