| 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 nor |
| 8 |
* does it submit to any jurisdiction. |
| 9 |
*/ |
| 10 |
|
| 11 |
#pragma once |
| 12 |
|
| 13 |
#include <cassert> |
| 14 |
#include <cstddef> |
| 15 |
|
| 16 |
#include "atlas/library/config.h" |
| 17 |
#include "atlas/runtime/Exception.h" |
| 18 |
|
| 19 |
#include "hic/hic.h" |
| 20 |
|
| 21 |
namespace atlas { |
| 22 |
namespace array { |
| 23 |
|
| 24 |
//------------------------------------------------------------------------------ |
| 25 |
|
| 26 |
template <typename T> |
| 27 |
class Vector { |
| 28 |
public: |
| 29 |
Vector(idx_t N = 0): data_(N ? new T[N] : nullptr), data_gpu_(nullptr), size_(N) {} |
| 30 |
|
| 31 |
void resize_impl(idx_t N) { |
| 32 |
ATLAS_ASSERT(not data_gpu_, "we can not resize a vector after has been cloned to device"); |
| 33 |
ATLAS_ASSERT(N >= size_); |
| 34 |
if (N == size_) |
| 35 |
return; |
| 36 |
|
| 37 |
T* d_ = new T[N]; |
| 38 |
for (idx_t c = 0; c < size_; ++c) { |
| 39 |
d_[c] = data_[c]; |
| 40 |
} |
| 41 |
if (data_) |
| 42 |
delete[] data_; |
| 43 |
data_ = d_; |
| 44 |
} |
| 45 |
|
| 46 |
void resize(idx_t N) { |
| 47 |
resize_impl(N); |
| 48 |
size_ = N; |
| 49 |
} |
| 50 |
|
| 51 |
void resize(idx_t N, T val) { |
| 52 |
resize_impl(N); |
| 53 |
for (idx_t c = size_; c < N; ++c) { |
| 54 |
data_[c] = val; |
| 55 |
} |
| 56 |
|
| 57 |
size_ = N; |
| 58 |
} |
| 59 |
|
| 60 |
void allocateDevice() { |
| 61 |
if constexpr (ATLAS_HAVE_GPU) { |
| 62 |
HIC_CALL(hicMalloc((void**)(&data_gpu_), sizeof(T*) * size_)); |
| 63 |
|
| 64 |
T* buff = new T[size_]; |
| 65 |
|
| 66 |
for (idx_t i = 0; i < size(); ++i) { |
| 67 |
data_[i]->updateDevice(); |
| 68 |
buff[i] = data_[i]->gpu_object_ptr(); |
| 69 |
} |
| 70 |
HIC_CALL(hicMemcpy(data_gpu_, buff, sizeof(T*) * size_, hicMemcpyHostToDevice)); |
| 71 |
delete[] buff; |
| 72 |
} |
| 73 |
else { |
| 74 |
data_gpu_ = data_; |
| 75 |
} |
| 76 |
size_gpu_ = size_; |
| 77 |
} |
| 78 |
|
| 79 |
void updateDevice() { |
| 80 |
if (!data_gpu_) { |
| 81 |
allocateDevice(); |
| 82 |
} |
| 83 |
else { |
| 84 |
ATLAS_ASSERT(size_gpu_ == size_); |
| 85 |
if constexpr (ATLAS_HAVE_GPU) { |
| 86 |
for (idx_t i = 0; i < size(); ++i) { |
| 87 |
data_[i]->updateDevice(); |
| 88 |
assert(data_gpu_[i] == data_[i]->gpu_object_ptr()); |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
void updateHost() { |
| 94 |
ATLAS_ASSERT(data_gpu_ != nullptr); |
| 95 |
if constexpr (ATLAS_HAVE_GPU) { |
| 96 |
for (idx_t i = 0; i < size(); ++i) { |
| 97 |
data_[i]->updateHost(); |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
T* gpu_object_ptr() { return data_gpu_; } |
| 103 |
|
| 104 |
T* data() { return data_; } |
| 105 |
T* data_gpu() { return data_gpu_; } |
| 106 |
|
| 107 |
idx_t size() const { return size_; } |
| 108 |
|
| 109 |
private: |
| 110 |
T* data_; |
| 111 |
T* data_gpu_; |
| 112 |
idx_t size_; |
| 113 |
idx_t size_gpu_; |
| 114 |
}; |
| 115 |
|
| 116 |
template <typename T> |
| 117 |
class VectorView { |
| 118 |
public: |
| 119 |
VectorView(): vector_(nullptr), data_(nullptr), size_(0) {} |
| 120 |
VectorView(Vector<T> const& vector, T* data): vector_(&vector), data_(data), size_(vector.size()) {} |
| 121 |
|
| 122 |
ATLAS_HOST_DEVICE |
| 123 |
T& operator[](idx_t idx) { |
| 124 |
assert(idx < size_); |
| 125 |
|
| 126 |
return data_[idx]; |
| 127 |
} |
| 128 |
|
| 129 |
ATLAS_HOST_DEVICE |
| 130 |
T const& operator[](idx_t idx) const { |
| 131 |
assert(idx < size_); |
| 132 |
return data_[idx]; |
| 133 |
} |
| 134 |
ATLAS_HOST_DEVICE |
| 135 |
T base() { return *data_; } |
| 136 |
|
| 137 |
ATLAS_HOST_DEVICE |
| 138 |
idx_t size() const { return size_; } |
| 139 |
|
| 140 |
bool is_valid(Vector<T>& vector) { return (&vector) == vector_ && (data_ != nullptr); } |
| 141 |
|
| 142 |
public: |
| 143 |
Vector<T> const* vector_; |
| 144 |
T* data_; |
| 145 |
idx_t size_; |
| 146 |
}; |
| 147 |
|
| 148 |
template <typename T> |
| 149 |
VectorView<T> make_host_vector_view(Vector<T> vector_) { |
| 150 |
return VectorView<T>(vector_, vector_.data()); |
| 151 |
} |
| 152 |
|
| 153 |
template <typename T> |
| 154 |
VectorView<T> make_device_vector_view(Vector<T> vector_) { |
| 155 |
return VectorView<T>(vector_, vector_.data_gpu()); |
| 156 |
} |
| 157 |
|
| 158 |
//------------------------------------------------------------------------------ |
| 159 |
|
| 160 |
} // namespace array |
| 161 |
} // namespace atlas |