| 1 |
// @HEADER |
| 2 |
// ***************************************************************************** |
| 3 |
// Stokhos Package |
| 4 |
// |
| 5 |
// Copyright 2009 NTESS and the Stokhos contributors. |
| 6 |
// SPDX-License-Identifier: BSD-3-Clause |
| 7 |
// ***************************************************************************** |
| 8 |
// @HEADER |
| 9 |
|
| 10 |
#ifndef STOKHOS_DYNAMIC_STORAGE_HPP |
| 11 |
#define STOKHOS_DYNAMIC_STORAGE_HPP |
| 12 |
|
| 13 |
#include "Stokhos_DynArrayTraits.hpp" |
| 14 |
|
| 15 |
#include "Kokkos_Macros.hpp" |
| 16 |
|
| 17 |
#include "Sacado_Traits.hpp" |
| 18 |
#include "Stokhos_KokkosTraits.hpp" |
| 19 |
#include <sstream> |
| 20 |
|
| 21 |
namespace Stokhos { |
| 22 |
|
| 23 |
template <typename ordinal_t, typename value_t, typename device_t> |
| 24 |
class DynamicStorage { |
| 25 |
public: |
| 26 |
|
| 27 |
static const bool is_static = false; |
| 28 |
static const int static_size = 0; |
| 29 |
static const bool supports_reset = false; |
| 30 |
|
| 31 |
typedef ordinal_t ordinal_type; |
| 32 |
typedef value_t value_type; |
| 33 |
typedef typename device_t::execution_space execution_space; |
| 34 |
typedef typename device_t::memory_space memory_space; |
| 35 |
typedef value_type& reference; |
| 36 |
typedef volatile value_type& volatile_reference; |
| 37 |
typedef const value_type& const_reference; |
| 38 |
typedef const volatile value_type& const_volatile_reference; |
| 39 |
typedef value_type* pointer; |
| 40 |
typedef volatile value_type* volatile_pointer; |
| 41 |
typedef const value_type* const_pointer; |
| 42 |
typedef const volatile value_type* const_volatile_pointer; |
| 43 |
typedef Stokhos::DynArrayTraits<value_type,execution_space> ds; |
| 44 |
|
| 45 |
//! Turn DynamicStorage into a meta-function class usable with mpl::apply |
| 46 |
template <typename ord_t, typename val_t = value_t , typename dev_t = device_t > |
| 47 |
struct apply { |
| 48 |
typedef DynamicStorage<ord_t,val_t,dev_t> type; |
| 49 |
}; |
| 50 |
|
| 51 |
template <int N> |
| 52 |
struct apply_N { |
| 53 |
typedef DynamicStorage<ordinal_type,value_type,device_t> type; |
| 54 |
}; |
| 55 |
|
| 56 |
//! Constructor |
| 57 |
KOKKOS_INLINE_FUNCTION |
| 58 |
DynamicStorage(const ordinal_type& sz = 1, |
| 59 |
const value_type& x = value_type(0.0)) : |
| 60 |
sz_(sz), is_view_(false) { |
| 61 |
if (sz_ == 0) { |
| 62 |
sz_ = 1; |
| 63 |
coeff_0_ = x; |
| 64 |
coeff_ = &coeff_0_; |
| 65 |
is_constant_ = true; |
| 66 |
} |
| 67 |
else if (sz_ == 1) { |
| 68 |
coeff_0_ = x; |
| 69 |
coeff_ = &coeff_0_; |
| 70 |
is_constant_ = true; |
| 71 |
} |
| 72 |
else { |
| 73 |
coeff_ = ds::get_and_fill(sz_, x); |
| 74 |
is_constant_ = false; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
//! Constructor from array |
| 79 |
KOKKOS_INLINE_FUNCTION |
| 80 |
DynamicStorage(const ordinal_type& sz, const value_type* x) : |
| 81 |
sz_(sz), is_view_(false) { |
| 82 |
if (sz_ == 0) { |
| 83 |
sz_ = 1; |
| 84 |
coeff_0_ = 0.0; |
| 85 |
coeff_ = &coeff_0_; |
| 86 |
is_constant_ = true; |
| 87 |
} |
| 88 |
else if (sz_ == 1) { |
| 89 |
coeff_0_ = *x; |
| 90 |
coeff_ = &coeff_0_; |
| 91 |
is_constant_ = true; |
| 92 |
} |
| 93 |
else { |
| 94 |
coeff_ = ds::get_and_fill(x, sz_); |
| 95 |
is_constant_ = false; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
//! Constructor for creating a view |
| 100 |
// This might be problematic if sz == 1 and owned == true |
| 101 |
KOKKOS_INLINE_FUNCTION |
| 102 |
DynamicStorage(const ordinal_type& sz, pointer v, bool owned) : |
| 103 |
coeff_(v), sz_(sz), is_view_(!owned), is_constant_(false) {} |
| 104 |
|
| 105 |
//! Constructor |
| 106 |
KOKKOS_INLINE_FUNCTION |
| 107 |
DynamicStorage(const DynamicStorage& s) : |
| 108 |
sz_(s.sz_), is_view_(false) { |
| 109 |
if (sz_ > 1) { |
| 110 |
coeff_ = ds::get_and_fill(s.coeff_, sz_); |
| 111 |
is_constant_ = false; |
| 112 |
} |
| 113 |
else { |
| 114 |
coeff_0_ = s.coeff_[0]; |
| 115 |
coeff_ = &coeff_0_; |
| 116 |
is_constant_ = true; |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
//! Constructor |
| 121 |
KOKKOS_INLINE_FUNCTION |
| 122 |
DynamicStorage(const volatile DynamicStorage& s) : |
| 123 |
sz_(s.sz_), is_view_(false) { |
| 124 |
if (sz_ > 1) { |
| 125 |
coeff_ = ds::get_and_fill(s.coeff_, sz_); |
| 126 |
is_constant_ = false; |
| 127 |
} |
| 128 |
else { |
| 129 |
coeff_0_ = s.coeff_[0]; |
| 130 |
coeff_ = &coeff_0_; |
| 131 |
is_constant_ = true; |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
//! Destructor |
| 136 |
KOKKOS_INLINE_FUNCTION |
| 137 |
~DynamicStorage() { |
| 138 |
if (!is_view_ && !is_constant_) ds::destroy_and_release(coeff_, sz_); |
| 139 |
} |
| 140 |
|
| 141 |
//! Assignment operator |
| 142 |
// To do: add error check if is_view_ == true && s.sz_ > sz_ |
| 143 |
KOKKOS_INLINE_FUNCTION |
| 144 |
DynamicStorage& operator=(const DynamicStorage& s) { |
| 145 |
if (&s != this) { |
| 146 |
// Only reallocate if we own the array and the sizes |
| 147 |
// differ |
| 148 |
if (!is_view_ && s.sz_ != sz_) { |
| 149 |
if (!is_constant_) |
| 150 |
ds::destroy_and_release(coeff_, sz_); |
| 151 |
if (s.sz_ > 1) { |
| 152 |
coeff_ = ds::get_and_fill(s.coeff_, s.sz_); |
| 153 |
is_constant_ = false; |
| 154 |
} |
| 155 |
else { |
| 156 |
coeff_0_ = s.coeff_[0]; |
| 157 |
coeff_ = &coeff_0_; |
| 158 |
is_constant_ = true; |
| 159 |
} |
| 160 |
sz_ = s.sz_; |
| 161 |
} |
| 162 |
else { |
| 163 |
// This should work if is_view_ or s.sz_ == sz_, regardless |
| 164 |
// of whether s.sz_ or sz_ is 1 |
| 165 |
ds::copy(s.coeff_, coeff_, s.sz_); |
| 166 |
} |
| 167 |
} |
| 168 |
return *this; |
| 169 |
} |
| 170 |
|
| 171 |
//! Assignment operator |
| 172 |
// To do: add error check if is_view_ == true && s.sz_ > sz_ |
| 173 |
KOKKOS_INLINE_FUNCTION |
| 174 |
DynamicStorage& operator=(const volatile DynamicStorage& s) { |
| 175 |
if (&s != this) { |
| 176 |
// Only reallocate if we own the array and the sizes |
| 177 |
// differ |
| 178 |
if (!is_view_ && s.sz_ != sz_) { |
| 179 |
if (!is_constant_) |
| 180 |
ds::destroy_and_release(coeff_, sz_); |
| 181 |
if (s.sz_ > 1) { |
| 182 |
coeff_ = ds::get_and_fill(s.coeff_, s.sz_); |
| 183 |
is_constant_ = false; |
| 184 |
} |
| 185 |
else { |
| 186 |
coeff_0_ = s.coeff_[0]; |
| 187 |
coeff_ = &coeff_0_; |
| 188 |
is_constant_ = true; |
| 189 |
} |
| 190 |
sz_ = s.sz_; |
| 191 |
} |
| 192 |
else { |
| 193 |
// This should work if is_view_ or s.sz_ == sz_, regardless |
| 194 |
// of whether s.sz_ or sz_ is 1 |
| 195 |
ds::copy(s.coeff_, coeff_, s.sz_); |
| 196 |
} |
| 197 |
} |
| 198 |
return *this; |
| 199 |
} |
| 200 |
|
| 201 |
//! Assignment operator |
| 202 |
// To do: add error check if is_view_ == true && s.sz_ > sz_ |
| 203 |
KOKKOS_INLINE_FUNCTION |
| 204 |
/*volatile*/ DynamicStorage& operator=(const DynamicStorage& s) volatile { |
| 205 |
if (&s != this) { |
| 206 |
// Only reallocate if we own the array and the sizes |
| 207 |
// differ |
| 208 |
if (!is_view_ && s.sz_ != sz_) { |
| 209 |
if (!is_constant_) |
| 210 |
ds::destroy_and_release(coeff_, sz_); |
| 211 |
if (s.sz_ > 1) { |
| 212 |
coeff_ = ds::get_and_fill(s.coeff_, s.sz_); |
| 213 |
is_constant_ = false; |
| 214 |
} |
| 215 |
else { |
| 216 |
coeff_0_ = s.coeff_[0]; |
| 217 |
coeff_ = const_cast<value_type*>(&coeff_0_); |
| 218 |
is_constant_ = true; |
| 219 |
} |
| 220 |
sz_ = s.sz_; |
| 221 |
} |
| 222 |
else { |
| 223 |
// This should work if is_view_ or s.sz_ == sz_, regardless |
| 224 |
// of whether s.sz_ or sz_ is 1 |
| 225 |
ds::copy(s.coeff_, coeff_, s.sz_); |
| 226 |
} |
| 227 |
} |
| 228 |
return const_cast<DynamicStorage&>(*this); |
| 229 |
} |
| 230 |
|
| 231 |
//! Assignment operator |
| 232 |
// To do: add error check if is_view_ == true && s.sz_ > sz_ |
| 233 |
KOKKOS_INLINE_FUNCTION |
| 234 |
/*volatile*/ DynamicStorage& |
| 235 |
operator=(const volatile DynamicStorage& s) volatile { |
| 236 |
if (&s != this) { |
| 237 |
// Only reallocate if we own the array and the sizes |
| 238 |
// differ |
| 239 |
if (!is_view_ && s.sz_ != sz_) { |
| 240 |
if (!is_constant_) |
| 241 |
ds::destroy_and_release(coeff_, sz_); |
| 242 |
if (s.sz_ > 1) { |
| 243 |
coeff_ = ds::get_and_fill(s.coeff_, s.sz_); |
| 244 |
is_constant_ = false; |
| 245 |
} |
| 246 |
else { |
| 247 |
coeff_0_ = s.coeff_[0]; |
| 248 |
coeff_ = &coeff_0_; |
| 249 |
is_constant_ = true; |
| 250 |
} |
| 251 |
sz_ = s.sz_; |
| 252 |
} |
| 253 |
else { |
| 254 |
// This should work if is_view_ or s.sz_ == sz_, regardless |
| 255 |
// of whether s.sz_ or sz_ is 1 |
| 256 |
ds::copy(s.coeff_, coeff_, s.sz_); |
| 257 |
} |
| 258 |
} |
| 259 |
return const_cast<DynamicStorage&>(*this); |
| 260 |
} |
| 261 |
|
| 262 |
//! Initialize values to a constant value |
| 263 |
KOKKOS_INLINE_FUNCTION |
| 264 |
void init(const_reference v) { |
| 265 |
ds::fill(coeff_, sz_, v); |
| 266 |
} |
| 267 |
|
| 268 |
//! Initialize values to a constant value |
| 269 |
KOKKOS_INLINE_FUNCTION |
| 270 |
void init(const_reference v) volatile { |
| 271 |
ds::fill(coeff_, sz_, v); |
| 272 |
} |
| 273 |
|
| 274 |
//! Initialize values to an array of values |
| 275 |
KOKKOS_INLINE_FUNCTION |
| 276 |
void init(const_pointer v, const ordinal_type& sz = 0) { |
| 277 |
if (sz == 0) |
| 278 |
ds::copy(v, coeff_, sz_); |
| 279 |
else |
| 280 |
ds::copy(v, coeff_, sz); |
| 281 |
} |
| 282 |
|
| 283 |
//! Initialize values to an array of values |
| 284 |
KOKKOS_INLINE_FUNCTION |
| 285 |
void init(const_pointer v, const ordinal_type& sz = 0) volatile { |
| 286 |
if (sz == 0) |
| 287 |
ds::copy(v, coeff_, sz_); |
| 288 |
else |
| 289 |
ds::copy(v, coeff_, sz); |
| 290 |
} |
| 291 |
|
| 292 |
//! Load values to an array of values |
| 293 |
KOKKOS_INLINE_FUNCTION |
| 294 |
void load(pointer v) { |
| 295 |
ds::copy(v, coeff_, sz_); |
| 296 |
} |
| 297 |
|
| 298 |
//! Load values to an array of values |
| 299 |
KOKKOS_INLINE_FUNCTION |
| 300 |
void load(pointer v) volatile { |
| 301 |
ds::copy(v, coeff_, sz_); |
| 302 |
} |
| 303 |
|
| 304 |
//! Resize to new size (values are preserved) |
| 305 |
KOKKOS_INLINE_FUNCTION |
| 306 |
void resize(const ordinal_type& sz) { |
| 307 |
if (!is_view_ && sz != sz_) { |
| 308 |
if (sz > 1) { |
| 309 |
value_type *coeff_new = ds::get_and_fill(sz); |
| 310 |
if (sz > sz_) |
| 311 |
ds::copy(coeff_, coeff_new, sz_); |
| 312 |
else |
| 313 |
ds::copy(coeff_, coeff_new, sz); |
| 314 |
if (!is_view_ && !is_constant_) |
| 315 |
ds::destroy_and_release(coeff_, sz_); |
| 316 |
coeff_ = coeff_new; |
| 317 |
is_constant_ = false; |
| 318 |
} |
| 319 |
else { |
| 320 |
coeff_0_ = coeff_[0]; |
| 321 |
if (!is_constant_) |
| 322 |
ds::destroy_and_release(coeff_, sz_); |
| 323 |
coeff_ = &coeff_0_; |
| 324 |
is_constant_ = true; |
| 325 |
} |
| 326 |
sz_ = sz; |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
//! Resize to new size (values are preserved) |
| 331 |
KOKKOS_INLINE_FUNCTION |
| 332 |
void resize(const ordinal_type& sz) volatile { |
| 333 |
if (!is_view_ && sz != sz_) { |
| 334 |
if (sz > 1) { |
| 335 |
value_type *coeff_new = ds::get_and_fill(sz); |
| 336 |
if (sz > sz_) |
| 337 |
ds::copy(coeff_, coeff_new, sz_); |
| 338 |
else |
| 339 |
ds::copy(coeff_, coeff_new, sz); |
| 340 |
if (!is_view_ && !is_constant_) |
| 341 |
ds::destroy_and_release(coeff_, sz_); |
| 342 |
coeff_ = coeff_new; |
| 343 |
is_constant_ = false; |
| 344 |
} |
| 345 |
else { |
| 346 |
coeff_0_ = coeff_[0]; |
| 347 |
if (!is_constant_) |
| 348 |
ds::destroy_and_release(coeff_, sz_); |
| 349 |
coeff_ = const_cast<value_type*>(&coeff_0_); |
| 350 |
is_constant_ = true; |
| 351 |
} |
| 352 |
sz_ = sz; |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
//! Reset storage to given array, size, and stride |
| 357 |
KOKKOS_INLINE_FUNCTION |
| 358 |
void shallowReset(pointer v, const ordinal_type& sz, |
| 359 |
const ordinal_type& stride, bool owned) { |
| 360 |
if (!is_view_ && !is_constant_) |
| 361 |
ds::destroy_and_release(coeff_, sz_); |
| 362 |
coeff_ = v; |
| 363 |
sz_ = sz; |
| 364 |
is_view_ = !owned; |
| 365 |
} |
| 366 |
|
| 367 |
//! Reset storage to given array, size, and stride |
| 368 |
KOKKOS_INLINE_FUNCTION |
| 369 |
void shallowReset(pointer v, const ordinal_type& sz, |
| 370 |
const ordinal_type& stride, bool owned) volatile { |
| 371 |
if (!is_view_ && !is_constant_) |
| 372 |
ds::destroy_and_release(coeff_, sz_); |
| 373 |
coeff_ = v; |
| 374 |
sz_ = sz; |
| 375 |
is_view_ = !owned; |
| 376 |
} |
| 377 |
|
| 378 |
//! Return size |
| 379 |
KOKKOS_INLINE_FUNCTION |
| 380 |
ordinal_type size() const { return sz_; } |
| 381 |
|
| 382 |
//! Return size |
| 383 |
KOKKOS_INLINE_FUNCTION |
| 384 |
ordinal_type size() const volatile { return sz_; } |
| 385 |
|
| 386 |
//! Return whether storage is a view |
| 387 |
KOKKOS_INLINE_FUNCTION |
| 388 |
bool is_view() const { return is_view_; } |
| 389 |
|
| 390 |
//! Return whether storage is a view |
| 391 |
KOKKOS_INLINE_FUNCTION |
| 392 |
bool is_view() const volatile { return is_view_; } |
| 393 |
|
| 394 |
//! Coefficient access (avoid if possible) |
| 395 |
KOKKOS_INLINE_FUNCTION |
| 396 |
const_reference operator[] (const ordinal_type& i) const { |
| 397 |
return coeff_[i]; |
| 398 |
} |
| 399 |
|
| 400 |
//! Coefficient access (avoid if possible) |
| 401 |
KOKKOS_INLINE_FUNCTION |
| 402 |
const_volatile_reference operator[] (const ordinal_type& i) const volatile { |
| 403 |
return coeff_[i]; |
| 404 |
} |
| 405 |
|
| 406 |
//! Coefficient access (avoid if possible) |
| 407 |
KOKKOS_INLINE_FUNCTION |
| 408 |
reference operator[] (const ordinal_type& i) { return coeff_[i]; } |
| 409 |
|
| 410 |
//! Coefficient access (avoid if possible) |
| 411 |
KOKKOS_INLINE_FUNCTION |
| 412 |
volatile_reference operator[] (const ordinal_type& i) volatile { |
| 413 |
return coeff_[i]; } |
| 414 |
|
| 415 |
template <int i> |
| 416 |
KOKKOS_INLINE_FUNCTION |
| 417 |
reference getCoeff() { return coeff_[i]; } |
| 418 |
|
| 419 |
template <int i> |
| 420 |
KOKKOS_INLINE_FUNCTION |
| 421 |
volatile_reference getCoeff() volatile { return coeff_[i]; } |
| 422 |
|
| 423 |
template <int i> |
| 424 |
KOKKOS_INLINE_FUNCTION |
| 425 |
const_volatile_reference getCoeff() const volatile { return coeff_[i]; } |
| 426 |
|
| 427 |
template <int i> |
| 428 |
KOKKOS_INLINE_FUNCTION |
| 429 |
const_reference getCoeff() const { return coeff_[i]; } |
| 430 |
|
| 431 |
//! Get coefficients |
| 432 |
KOKKOS_INLINE_FUNCTION |
| 433 |
const_volatile_pointer coeff() const volatile { return coeff_; } |
| 434 |
|
| 435 |
//! Get coefficients |
| 436 |
KOKKOS_INLINE_FUNCTION |
| 437 |
const_pointer coeff() const { return coeff_; } |
| 438 |
|
| 439 |
//! Get coefficients |
| 440 |
KOKKOS_INLINE_FUNCTION |
| 441 |
volatile_pointer coeff() volatile { return coeff_; } |
| 442 |
|
| 443 |
//! Get coefficients |
| 444 |
KOKKOS_INLINE_FUNCTION |
| 445 |
pointer coeff() { return coeff_; } |
| 446 |
|
| 447 |
private: |
| 448 |
|
| 449 |
//! Coefficient values |
| 450 |
pointer coeff_; |
| 451 |
|
| 452 |
//! Coefficient value when sz_ == 1 (i.e., a constant) |
| 453 |
/*! Eliminates dynamic memory allocation for this common use-case */ |
| 454 |
value_type coeff_0_; |
| 455 |
|
| 456 |
//! Size of array used |
| 457 |
ordinal_type sz_; |
| 458 |
|
| 459 |
//! Do we own the array |
| 460 |
bool is_view_; |
| 461 |
|
| 462 |
//! Is the coefficient array length-1 |
| 463 |
bool is_constant_; |
| 464 |
|
| 465 |
}; |
| 466 |
|
| 467 |
} |
| 468 |
|
| 469 |
#include "Stokhos_StorageHelpers.hpp" |
| 470 |
STOKHOS_STORAGE_HELPER_STRINGNAME_DYNAMIC(DynamicStorage) |
| 471 |
|
| 472 |
#endif // STOKHOS_DYNAMIC_STORAGE_HPP |