| 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 Table.h |
| 12 |
/// @author Willem Deconinck |
| 13 |
/// @date October 2015 |
| 14 |
/// This file contains the classes |
| 15 |
/// - Table |
| 16 |
|
| 17 |
#pragma once |
| 18 |
|
| 19 |
#include <array> |
| 20 |
#include <type_traits> |
| 21 |
|
| 22 |
#include "atlas/util/Object.h" |
| 23 |
|
| 24 |
#include "atlas/array.h" |
| 25 |
#include "atlas/library/config.h" |
| 26 |
|
| 27 |
namespace atlas { |
| 28 |
namespace array { |
| 29 |
|
| 30 |
// -------------------------------------------------------------------------- |
| 31 |
|
| 32 |
/// @brief Table |
| 33 |
/// @author Willem Deconinck |
| 34 |
/// |
| 35 |
/// Container for tables. This is e.g. |
| 36 |
/// for a node-to-X connectivity. |
| 37 |
/// connectivity = [ |
| 38 |
/// 1 2 3 4 5 6 # node 1 |
| 39 |
/// 7 8 # node 2 |
| 40 |
/// 9 10 11 12 # node 3 |
| 41 |
/// 13 14 15 # node 4 |
| 42 |
/// 16 17 18 19 20 21 # node 5 |
| 43 |
/// ] |
| 44 |
/// There are 2 modes of construction: |
| 45 |
/// - It wraps existing raw data |
| 46 |
/// - It owns the connectivity data |
| 47 |
/// |
| 48 |
/// In case ATLAS_HAVE_FORTRAN is defined (which is usually the case), |
| 49 |
/// the raw data will be stored with base 1 for Fortran interoperability. |
| 50 |
/// The operator(row,col) in C++ will then do the conversion to base 0. |
| 51 |
/// |
| 52 |
/// In the first mode of construction, the connectivity table cannot be resized. |
| 53 |
/// In the second mode of construction, resizing is possible |
| 54 |
|
| 55 |
class Table : public util::Object { |
| 56 |
private: |
| 57 |
static constexpr unsigned short _values_ = 0; |
| 58 |
static constexpr unsigned short _displs_ = 1; |
| 59 |
static constexpr unsigned short _counts_ = 2; |
| 60 |
|
| 61 |
public: |
| 62 |
//-- Constructors |
| 63 |
|
| 64 |
/// @brief Construct connectivity table that needs resizing a-posteriori |
| 65 |
/// Data is owned |
| 66 |
Table(const std::string& name = ""); |
| 67 |
|
| 68 |
~Table(); |
| 69 |
|
| 70 |
private: |
| 71 |
/// @brief Construct connectivity table wrapping existing raw data. |
| 72 |
/// No resizing can be performed as data is not owned. |
| 73 |
Table(idx_t values[], size_t rows, size_t displs[], size_t counts[]); |
| 74 |
|
| 75 |
public: |
| 76 |
//-- Accessors |
| 77 |
|
| 78 |
/// @brief Name associated to this Connetivity |
| 79 |
const std::string& name() const { return name_; } |
| 80 |
|
| 81 |
/// @brief Rename this Connectivity |
| 82 |
void rename(const std::string& name) { name_ = name; } |
| 83 |
|
| 84 |
/// @brief Number of rows in the connectivity table |
| 85 |
size_t rows() const { return rows_; } |
| 86 |
|
| 87 |
/// @brief Number of columns for specified row in the connectivity table |
| 88 |
size_t cols(size_t row_idx) const { return counts_(row_idx); } |
| 89 |
|
| 90 |
/// @brief Maximum value for number of columns over all rows |
| 91 |
size_t maxcols() const { return maxcols_; } |
| 92 |
|
| 93 |
/// @brief Minimum value for number of columns over all rows |
| 94 |
size_t mincols() const { return mincols_; } |
| 95 |
|
| 96 |
/// @brief Value that is given to unassigned entries |
| 97 |
idx_t missing_value() const { return missing_value_; } |
| 98 |
|
| 99 |
/// @brief Number of values stored in the table |
| 100 |
size_t size() const { return data_[_values_]->size(); } |
| 101 |
|
| 102 |
/// @brief Return memory footprint of table |
| 103 |
virtual size_t footprint() const; |
| 104 |
|
| 105 |
/// @brief Clone data to device |
| 106 |
virtual void updateDevice() const; |
| 107 |
|
| 108 |
/// @brief Clone data from device |
| 109 |
virtual void updateHost() const; |
| 110 |
|
| 111 |
/// @brief Synchronise data between host and device |
| 112 |
virtual void syncHostDevice() const; |
| 113 |
|
| 114 |
/// @brief Check if data is valid |
| 115 |
virtual bool valid() const; |
| 116 |
|
| 117 |
/// @brief Check if data is present on host |
| 118 |
virtual bool hostNeedsUpdate() const; |
| 119 |
|
| 120 |
/// @brief Check if data is present on device |
| 121 |
virtual bool deviceNeedsUpdate() const; |
| 122 |
|
| 123 |
/// @brief Print all values unformatted to output stream |
| 124 |
void dump(std::ostream&) const; |
| 125 |
|
| 126 |
/// @brief Check if data is owned or wrapped |
| 127 |
bool owns() { return owns_; } |
| 128 |
|
| 129 |
///-- Modifiers |
| 130 |
|
| 131 |
/// @brief Resize connectivity, and add given rows |
| 132 |
/// @note Can only be used when data is owned. |
| 133 |
virtual void add(size_t rows, size_t cols, const idx_t values[], bool fortran_array = false); |
| 134 |
|
| 135 |
/// @brief Resize connectivity, and add given rows with missing values |
| 136 |
/// @note Can only be used when data is owned. |
| 137 |
virtual void add(size_t rows, size_t cols); |
| 138 |
|
| 139 |
/// @brief Resize connectivity, and add given rows with missing values |
| 140 |
/// @note Can only be used when data is owned. |
| 141 |
virtual void add(size_t rows, const size_t cols[]); |
| 142 |
|
| 143 |
/// @brief Resize connectivity, and insert given rows |
| 144 |
/// @note Can only be used when data is owned. |
| 145 |
virtual void insert(size_t position, size_t rows, size_t cols, const idx_t values[], bool fortran_array = false); |
| 146 |
|
| 147 |
/// @brief Resize connectivity, and insert given rows with missing values |
| 148 |
/// @note Can only be used when data is owned. |
| 149 |
virtual void insert(size_t position, size_t rows, size_t cols); |
| 150 |
|
| 151 |
/// @brief Resize connectivity, and insert given rows with missing values |
| 152 |
/// @note Can only be used when data is owned. |
| 153 |
virtual void insert(size_t position, size_t rows, const size_t cols[]); |
| 154 |
|
| 155 |
/// @brief Resize connectivity, and insert given rows with missing values |
| 156 |
/// @note Invalidates non-owned Table |
| 157 |
virtual void clear(); |
| 158 |
|
| 159 |
private: |
| 160 |
///-- Internal helper functions |
| 161 |
|
| 162 |
void resize_values(size_t old_size, size_t size, bool initialize, const idx_t values[], bool fortran_array); |
| 163 |
|
| 164 |
void resize_counts_and_displs(size_t size); |
| 165 |
|
| 166 |
void insert_counts_and_displs(size_t position, size_t rows); |
| 167 |
|
| 168 |
void resize_values(size_t size); |
| 169 |
|
| 170 |
void insert_values(size_t position, size_t size); |
| 171 |
|
| 172 |
private: |
| 173 |
template <bool ReadOnly> |
| 174 |
friend class TableView; |
| 175 |
|
| 176 |
std::string name_; |
| 177 |
bool owns_; |
| 178 |
std::array<array::Array*, 3> data_; |
| 179 |
idx_t missing_value_; |
| 180 |
size_t rows_; |
| 181 |
size_t maxcols_; |
| 182 |
size_t mincols_; |
| 183 |
ArrayView<size_t, 1> displs_; |
| 184 |
ArrayView<size_t, 1> counts_; |
| 185 |
ArrayView<idx_t, 1> values_; |
| 186 |
}; |
| 187 |
|
| 188 |
//------------------------------------------------------------------------------------------------------ |
| 189 |
|
| 190 |
} // namespace array |
| 191 |
} // namespace atlas |