| 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 |
#include "Table.h" |
| 12 |
|
| 13 |
#include <algorithm> |
| 14 |
#include <limits> |
| 15 |
|
| 16 |
#include "atlas/array.h" |
| 17 |
#include "atlas/array/DataType.h" |
| 18 |
#include "atlas/library/defines.h" |
| 19 |
#include "atlas/runtime/Exception.h" |
| 20 |
|
| 21 |
#if ATLAS_HAVE_FORTRAN |
| 22 |
#define FORTRAN_BASE 1 |
| 23 |
#define TO_FORTRAN +1 |
| 24 |
#else |
| 25 |
#define FORTRAN_BASE 0 |
| 26 |
#define TO_FORTRAN |
| 27 |
#endif |
| 28 |
|
| 29 |
namespace atlas { |
| 30 |
namespace array { |
| 31 |
|
| 32 |
// ---------------------------------------------------------------------------- |
| 33 |
|
| 34 |
Table::Table(const std::string& name): |
| 35 |
name_(name), |
| 36 |
owns_(true), |
| 37 |
data_{Array::create<idx_t>(0), // values |
| 38 |
Array::create<size_t>(1), // displs |
| 39 |
Array::create<size_t>(1)}, // counts |
| 40 |
missing_value_(std::numeric_limits<idx_t>::is_signed ? -1 : std::numeric_limits<idx_t>::max()), |
| 41 |
rows_(0), |
| 42 |
maxcols_(0), |
| 43 |
mincols_(std::numeric_limits<size_t>::max()), |
| 44 |
displs_(make_host_view<size_t, 1>(*(data_[_displs_]))), |
| 45 |
counts_(make_host_view<size_t, 1>(*(data_[_counts_]))), |
| 46 |
values_(make_host_view<idx_t, 1>(*(data_[_values_]))) { |
| 47 |
displs_(0) = 0; |
| 48 |
counts_(0) = 0; |
| 49 |
} |
| 50 |
|
| 51 |
// ---------------------------------------------------------------------------- |
| 52 |
|
| 53 |
namespace { |
| 54 |
static size_t get_total_size_counts(size_t rows, size_t counts[]) { |
| 55 |
size_t total_size = 0; |
| 56 |
for (size_t j = 0; j < rows; ++j) { |
| 57 |
total_size += counts[j]; |
| 58 |
} |
| 59 |
return total_size; |
| 60 |
} |
| 61 |
} // namespace |
| 62 |
|
| 63 |
// ---------------------------------------------------------------------------- |
| 64 |
|
| 65 |
Table::Table(idx_t values[], size_t rows, size_t displs[], size_t counts[]): |
| 66 |
name_(), |
| 67 |
owns_(false), |
| 68 |
data_{Array::wrap<idx_t>(values, array::ArrayShape{get_total_size_counts(rows, counts)}), |
| 69 |
Array::wrap<size_t>(displs, array::ArrayShape{rows}), Array::wrap<size_t>(counts, array::ArrayShape{rows})}, |
| 70 |
missing_value_(std::numeric_limits<idx_t>::is_signed ? -1 : std::numeric_limits<idx_t>::max()), |
| 71 |
rows_(rows), |
| 72 |
maxcols_(0), |
| 73 |
mincols_(std::numeric_limits<size_t>::max()), |
| 74 |
displs_(make_host_view<size_t, 1>(*(data_[_displs_]))), |
| 75 |
counts_(make_host_view<size_t, 1>(*(data_[_counts_]))), |
| 76 |
values_(make_host_view<idx_t, 1>(*(data_[_values_]))) { |
| 77 |
for (size_t j = 0; j < rows; ++j) { |
| 78 |
maxcols_ = std::max(maxcols_, counts[j]); |
| 79 |
mincols_ = std::min(mincols_, counts[j]); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
// ---------------------------------------------------------------------------- |
| 84 |
|
| 85 |
Table::~Table() { |
| 86 |
if (owns_) { |
| 87 |
std::for_each(data_.begin(), data_.end(), [](array::Array* a) { |
| 88 |
assert(a); |
| 89 |
delete a; |
| 90 |
a = 0; |
| 91 |
}); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
// ---------------------------------------------------------------------------- |
| 96 |
|
| 97 |
void Table::clear() { |
| 98 |
if (owns()) { |
| 99 |
resize_values(0); |
| 100 |
resize_counts_and_displs(1); |
| 101 |
displs_(0) = 0; |
| 102 |
counts_(0) = 0; |
| 103 |
} |
| 104 |
else { |
| 105 |
std::for_each(data_.begin(), data_.end(), [](array::Array* a) { a = 0; }); |
| 106 |
// data_ and host_views will be invalid now! |
| 107 |
} |
| 108 |
maxcols_ = 0; |
| 109 |
mincols_ = std::numeric_limits<size_t>::max(); |
| 110 |
} |
| 111 |
|
| 112 |
// ---------------------------------------------------------------------------- |
| 113 |
|
| 114 |
void Table::resize_values(size_t old_size, size_t new_size, bool initialize, const idx_t values[], bool fortran_array) { |
| 115 |
resize_values(new_size); |
| 116 |
idx_t add_base = fortran_array ? 0 : FORTRAN_BASE; |
| 117 |
if (initialize) { |
| 118 |
for (size_t j = 0, c = old_size; c < new_size; ++c, ++j) { |
| 119 |
values_(c) = values[j] + add_base; |
| 120 |
} |
| 121 |
} |
| 122 |
else { |
| 123 |
for (size_t j = old_size; j < new_size; ++j) { |
| 124 |
values_(j) = missing_value() TO_FORTRAN; |
| 125 |
} |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
// ---------------------------------------------------------------------------- |
| 130 |
|
| 131 |
void Table::resize_counts_and_displs(size_t size) { |
| 132 |
ATLAS_ASSERT(data_[_displs_] != 0); |
| 133 |
ATLAS_ASSERT(data_[_counts_] != 0); |
| 134 |
data_[_displs_]->resize(size); |
| 135 |
data_[_counts_]->resize(size); |
| 136 |
displs_ = make_host_view<size_t, 1>(*(data_[_displs_])); |
| 137 |
counts_ = make_host_view<size_t, 1>(*(data_[_counts_])); |
| 138 |
} |
| 139 |
|
| 140 |
// ---------------------------------------------------------------------------- |
| 141 |
|
| 142 |
void Table::insert_counts_and_displs(size_t position, size_t rows) { |
| 143 |
ATLAS_ASSERT(data_[_displs_] != 0); |
| 144 |
ATLAS_ASSERT(data_[_counts_] != 0); |
| 145 |
data_[_displs_]->insert(position, rows); |
| 146 |
data_[_counts_]->insert(position, rows); |
| 147 |
displs_ = make_host_view<size_t, 1>(*(data_[_displs_])); |
| 148 |
counts_ = make_host_view<size_t, 1>(*(data_[_counts_])); |
| 149 |
} |
| 150 |
|
| 151 |
// ---------------------------------------------------------------------------- |
| 152 |
|
| 153 |
void Table::resize_values(size_t size) { |
| 154 |
ATLAS_ASSERT(data_[_values_] != 0); |
| 155 |
data_[_values_]->resize(size); |
| 156 |
values_ = make_host_view<idx_t, 1>(*(data_[_values_])); |
| 157 |
} |
| 158 |
|
| 159 |
// ---------------------------------------------------------------------------- |
| 160 |
|
| 161 |
void Table::insert_values(size_t position, size_t size) { |
| 162 |
ATLAS_ASSERT(data_[_values_] != 0); |
| 163 |
data_[_values_]->insert(position, size); |
| 164 |
values_ = make_host_view<idx_t, 1>(*(data_[_values_])); |
| 165 |
} |
| 166 |
|
| 167 |
// ---------------------------------------------------------------------------- |
| 168 |
|
| 169 |
void Table::add(size_t rows, size_t cols, const idx_t values[], bool fortran_array) { |
| 170 |
ATLAS_ASSERT(owns_, "HybridConnectivity must be owned to be resized directly"); |
| 171 |
size_t old_size = size(); |
| 172 |
|
| 173 |
if (rows_ == 0) |
| 174 |
old_size = 0; |
| 175 |
|
| 176 |
size_t new_size = old_size + rows * cols; |
| 177 |
size_t new_rows = rows_ + rows; |
| 178 |
|
| 179 |
resize_counts_and_displs(new_rows + 1); |
| 180 |
|
| 181 |
for (size_t j = 0; rows_ < new_rows; ++rows_, ++j) { |
| 182 |
displs_(rows_ + 1) = displs_(rows_) + cols; |
| 183 |
counts_(rows_) = cols; |
| 184 |
} |
| 185 |
|
| 186 |
maxcols_ = std::max(maxcols_, cols); |
| 187 |
mincols_ = std::min(mincols_, cols); |
| 188 |
|
| 189 |
resize_values(old_size, new_size, true, values, fortran_array); |
| 190 |
} |
| 191 |
|
| 192 |
// ---------------------------------------------------------------------------- |
| 193 |
|
| 194 |
void Table::add(size_t rows, const size_t cols[]) { |
| 195 |
ATLAS_ASSERT(owns_, "HybridConnectivity must be owned to be resized directly"); |
| 196 |
size_t old_size = size(); |
| 197 |
size_t new_size = old_size; |
| 198 |
for (size_t j = 0; j < rows; ++j) |
| 199 |
new_size += cols[j]; |
| 200 |
size_t new_rows = rows_ + rows; |
| 201 |
resize_counts_and_displs(new_rows + 1); |
| 202 |
|
| 203 |
for (size_t j = 0; rows_ < new_rows; ++rows_, ++j) { |
| 204 |
displs_(rows_ + 1) = displs_(rows_) + cols[j]; |
| 205 |
counts_(rows_) = cols[j]; |
| 206 |
maxcols_ = std::max(maxcols_, cols[j]); |
| 207 |
mincols_ = std::min(mincols_, cols[j]); |
| 208 |
} |
| 209 |
|
| 210 |
resize_values(old_size, new_size, false, NULL, false); |
| 211 |
} |
| 212 |
|
| 213 |
// ---------------------------------------------------------------------------- |
| 214 |
|
| 215 |
void Table::add(size_t rows, size_t cols) { |
| 216 |
ATLAS_ASSERT(owns_, "HybridConnectivity must be owned to be resized directly"); |
| 217 |
size_t old_size = size(); |
| 218 |
|
| 219 |
if (rows_ == 0) |
| 220 |
old_size = 0; |
| 221 |
|
| 222 |
size_t new_size = old_size + rows * cols; |
| 223 |
size_t new_rows = rows_ + rows; |
| 224 |
resize_counts_and_displs(new_rows + 1); |
| 225 |
for (size_t j = 0; rows_ < new_rows; ++rows_, ++j) { |
| 226 |
displs_(rows_ + 1) = displs_(rows_) + cols; |
| 227 |
counts_(rows_) = cols; |
| 228 |
} |
| 229 |
|
| 230 |
maxcols_ = std::max(maxcols_, cols); |
| 231 |
mincols_ = std::min(mincols_, cols); |
| 232 |
|
| 233 |
const bool dummy_arg_fortran_array = false; |
| 234 |
const idx_t* dummy_arg_values = nullptr; |
| 235 |
resize_values(old_size, new_size, false, dummy_arg_values, dummy_arg_fortran_array); |
| 236 |
} |
| 237 |
|
| 238 |
// ---------------------------------------------------------------------------- |
| 239 |
|
| 240 |
void Table::insert(size_t position, size_t rows, size_t cols, const idx_t values[], bool fortran_array) { |
| 241 |
ATLAS_ASSERT(owns_, "HybridConnectivity must be owned to be resized directly"); |
| 242 |
size_t position_displs = displs_(position); |
| 243 |
insert_counts_and_displs(position, rows); |
| 244 |
|
| 245 |
displs_(position) = position_displs; |
| 246 |
for (size_t jrow = position; jrow < position + rows; ++jrow) { |
| 247 |
counts_(jrow) = cols; |
| 248 |
} |
| 249 |
for (size_t jrow = position; jrow < displs_.size() - 1; ++jrow) { |
| 250 |
displs_(jrow + 1) = displs_(jrow) + counts_(jrow); |
| 251 |
} |
| 252 |
maxcols_ = std::max(maxcols_, cols); |
| 253 |
mincols_ = std::min(mincols_, cols); |
| 254 |
|
| 255 |
insert_values(position_displs, rows * cols); |
| 256 |
|
| 257 |
if (values == nullptr) { |
| 258 |
for (size_t c = position_displs; c < position_displs + rows * cols; ++c) { |
| 259 |
values_(c) = missing_value() TO_FORTRAN; |
| 260 |
} |
| 261 |
} |
| 262 |
else { |
| 263 |
idx_t add_base = fortran_array ? 0 : FORTRAN_BASE; |
| 264 |
for (size_t c = position_displs; c < position_displs + rows * cols; ++c) { |
| 265 |
values_(c) = values[c - position_displs] + add_base; |
| 266 |
} |
| 267 |
} |
| 268 |
rows_ += rows; |
| 269 |
} |
| 270 |
|
| 271 |
// ---------------------------------------------------------------------------- |
| 272 |
|
| 273 |
void Table::insert(size_t position, size_t rows, size_t cols) { |
| 274 |
Table::insert(position, rows, cols, nullptr, false); |
| 275 |
} |
| 276 |
|
| 277 |
// ---------------------------------------------------------------------------- |
| 278 |
|
| 279 |
void Table::insert(size_t position, size_t rows, const size_t cols[]) { |
| 280 |
ATLAS_ASSERT(owns_, "HybridConnectivity must be owned to be resized directly"); |
| 281 |
size_t position_displs = displs_(position); |
| 282 |
|
| 283 |
if (rows_ == 0) { |
| 284 |
if (position > 1) { |
| 285 |
insert_counts_and_displs(position - 1, rows); |
| 286 |
} |
| 287 |
} |
| 288 |
else { |
| 289 |
insert_counts_and_displs(position, rows); |
| 290 |
} |
| 291 |
displs_(position) = position_displs; |
| 292 |
for (size_t jrow = position; jrow < position + rows; ++jrow) { |
| 293 |
counts_(jrow) = cols[jrow - position]; |
| 294 |
maxcols_ = std::max(maxcols_, counts_(jrow)); |
| 295 |
mincols_ = std::min(mincols_, counts_(jrow)); |
| 296 |
} |
| 297 |
for (size_t jrow = position; jrow < displs_.size() - 1; ++jrow) { |
| 298 |
displs_(jrow + 1) = displs_(jrow) + counts_(jrow); |
| 299 |
} |
| 300 |
|
| 301 |
size_t insert_size(0); |
| 302 |
for (size_t j = 0; j < rows; ++j) |
| 303 |
insert_size += cols[j]; |
| 304 |
|
| 305 |
insert_values(position_displs, insert_size); |
| 306 |
|
| 307 |
for (size_t c = position_displs; c < position_displs + insert_size; ++c) { |
| 308 |
values_(c) = missing_value() TO_FORTRAN; |
| 309 |
} |
| 310 |
|
| 311 |
rows_ += rows; |
| 312 |
} |
| 313 |
|
| 314 |
// ---------------------------------------------------------------------------- |
| 315 |
|
| 316 |
void Table::updateDevice() const { |
| 317 |
std::for_each(data_.begin(), data_.end(), [](array::Array* a) { a->updateDevice(); }); |
| 318 |
} |
| 319 |
|
| 320 |
// ---------------------------------------------------------------------------- |
| 321 |
|
| 322 |
void Table::updateHost() const { |
| 323 |
std::for_each(data_.begin(), data_.end(), [](array::Array* a) { a->updateHost(); }); |
| 324 |
} |
| 325 |
|
| 326 |
// ---------------------------------------------------------------------------- |
| 327 |
|
| 328 |
void Table::syncHostDevice() const { |
| 329 |
std::for_each(data_.begin(), data_.end(), [](array::Array* a) { a->syncHostDevice(); }); |
| 330 |
} |
| 331 |
|
| 332 |
// ---------------------------------------------------------------------------- |
| 333 |
|
| 334 |
bool Table::valid() const { |
| 335 |
bool res = true; |
| 336 |
std::for_each(data_.begin(), data_.end(), [&](array::Array* a) { res &= a->valid(); }); |
| 337 |
return res; |
| 338 |
} |
| 339 |
|
| 340 |
// ---------------------------------------------------------------------------- |
| 341 |
|
| 342 |
bool Table::hostNeedsUpdate() const { |
| 343 |
bool res = true; |
| 344 |
std::for_each(data_.begin(), data_.end(), [&](array::Array* a) { res &= a->hostNeedsUpdate(); }); |
| 345 |
return res; |
| 346 |
} |
| 347 |
|
| 348 |
// ---------------------------------------------------------------------------- |
| 349 |
|
| 350 |
bool Table::deviceNeedsUpdate() const { |
| 351 |
bool res = true; |
| 352 |
std::for_each(data_.begin(), data_.end(), [&](array::Array* a) { res &= a->deviceNeedsUpdate(); }); |
| 353 |
return res; |
| 354 |
} |
| 355 |
|
| 356 |
// ---------------------------------------------------------------------------- |
| 357 |
|
| 358 |
size_t Table::footprint() const { |
| 359 |
size_t size = sizeof(*this); |
| 360 |
if (owns_) { |
| 361 |
std::for_each(data_.begin(), data_.end(), [&](array::Array* a) { size += a->footprint(); }); |
| 362 |
} |
| 363 |
return size; |
| 364 |
} |
| 365 |
|
| 366 |
// ---------------------------------------------------------------------------- |
| 367 |
|
| 368 |
void Table::dump(std::ostream& os) const { |
| 369 |
values_.dump(os); |
| 370 |
} |
| 371 |
|
| 372 |
// ---------------------------------------------------------------------------- |
| 373 |
|
| 374 |
} // namespace array |
| 375 |
} // namespace atlas |