| 1 |
/* |
| 2 |
* (C) Copyright 2024- 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 "SparseMatrixStorage.h" |
| 12 |
|
| 13 |
namespace atlas::linalg { |
| 14 |
|
| 15 |
|
| 16 |
SparseMatrixStorage::SparseMatrixStorage(SparseMatrixStorage&& other) { |
| 17 |
nnz_ = other.nnz_; |
| 18 |
rows_ = other.rows_; |
| 19 |
cols_ = other.cols_; |
| 20 |
outer_ = std::move(other.outer_); |
| 21 |
inner_ = std::move(other.inner_); |
| 22 |
value_ = std::move(other.value_); |
| 23 |
storage_ = std::move(other.storage_); |
| 24 |
|
| 25 |
// Invalidate other, just in case it is being used (IT SHOULDNT) |
| 26 |
other.nnz_ = 0; |
| 27 |
other.rows_ = 0; |
| 28 |
other.cols_ = 0; |
| 29 |
} |
| 30 |
|
| 31 |
SparseMatrixStorage::SparseMatrixStorage(const SparseMatrixStorage& other) { |
| 32 |
nnz_ = other.nnz_; |
| 33 |
rows_ = other.rows_; |
| 34 |
cols_ = other.cols_; |
| 35 |
{ |
| 36 |
array::label label{"sparse_matrix.outer"}; |
| 37 |
outer_.reset(atlas::array::Array::create(other.outer_->datatype(), atlas::array::make_shape(other.outer_->size()))); |
| 38 |
} |
| 39 |
{ |
| 40 |
array::label label{"sparse_matrix.inner"}; |
| 41 |
inner_.reset(atlas::array::Array::create(other.inner_->datatype(), atlas::array::make_shape(other.inner_->size()))); |
| 42 |
} |
| 43 |
{ |
| 44 |
array::label label{"sparse_matrix.value"}; |
| 45 |
value_.reset(atlas::array::Array::create(other.value_->datatype(), atlas::array::make_shape(other.value_->size()))); |
| 46 |
} |
| 47 |
outer_->copy(*other.outer_); |
| 48 |
inner_->copy(*other.inner_); |
| 49 |
value_->copy(*other.value_); |
| 50 |
} |
| 51 |
|
| 52 |
SparseMatrixStorage& SparseMatrixStorage::operator=(SparseMatrixStorage&& other) { |
| 53 |
nnz_ = other.nnz_; |
| 54 |
rows_ = other.rows_; |
| 55 |
cols_ = other.cols_; |
| 56 |
outer_ = std::move(other.outer_); |
| 57 |
inner_ = std::move(other.inner_); |
| 58 |
value_ = std::move(other.value_); |
| 59 |
storage_ = std::move(other.storage_); |
| 60 |
|
| 61 |
// Invalidate other, just in case it is being used (IT SHOULDNT) |
| 62 |
other.nnz_ = 0; |
| 63 |
other.rows_ = 0; |
| 64 |
other.cols_ = 0; |
| 65 |
return *this; |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
SparseMatrixStorage& SparseMatrixStorage::operator=(const SparseMatrixStorage& other) { |
| 70 |
nnz_ = other.nnz_; |
| 71 |
rows_ = other.rows_; |
| 72 |
cols_ = other.cols_; |
| 73 |
{ |
| 74 |
array::label label{"sparse_matrix.outer"}; |
| 75 |
outer_.reset(atlas::array::Array::create(other.outer_->datatype(), atlas::array::make_shape(other.outer_->size()))); |
| 76 |
} |
| 77 |
{ |
| 78 |
array::label label{"sparse_matrix.inner"}; |
| 79 |
inner_.reset(atlas::array::Array::create(other.inner_->datatype(), atlas::array::make_shape(other.inner_->size()))); |
| 80 |
} |
| 81 |
{ |
| 82 |
array::label label{"sparse_matrix.value"}; |
| 83 |
value_.reset(atlas::array::Array::create(other.value_->datatype(), atlas::array::make_shape(other.value_->size()))); |
| 84 |
} |
| 85 |
outer_->copy(*other.outer_); |
| 86 |
inner_->copy(*other.inner_); |
| 87 |
value_->copy(*other.value_); |
| 88 |
return *this; |
| 89 |
} |
| 90 |
|
| 91 |
void SparseMatrixStorage::clear() { |
| 92 |
nnz_ = 0; |
| 93 |
rows_ = 0; |
| 94 |
cols_ = 0; |
| 95 |
storage_.reset(); |
| 96 |
outer_.reset(); |
| 97 |
inner_.reset(); |
| 98 |
value_.reset(); |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
void SparseMatrixStorage::updateDevice() const { |
| 104 |
outer_->updateDevice(); |
| 105 |
inner_->updateDevice(); |
| 106 |
value_->updateDevice(); |
| 107 |
} |
| 108 |
|
| 109 |
void SparseMatrixStorage::updateHost() const { |
| 110 |
outer_->updateHost(); |
| 111 |
inner_->updateHost(); |
| 112 |
value_->updateHost(); |
| 113 |
} |
| 114 |
|
| 115 |
void SparseMatrixStorage::syncDevice() const { |
| 116 |
outer_->syncDevice(); |
| 117 |
inner_->syncDevice(); |
| 118 |
value_->syncDevice(); |
| 119 |
} |
| 120 |
|
| 121 |
void SparseMatrixStorage::syncHost() const { |
| 122 |
outer_->syncHost(); |
| 123 |
inner_->syncHost(); |
| 124 |
value_->syncHost(); |
| 125 |
} |
| 126 |
|
| 127 |
bool SparseMatrixStorage::hostNeedsUpdate() const { |
| 128 |
return outer_->hostNeedsUpdate() || |
| 129 |
inner_->hostNeedsUpdate() || |
| 130 |
value_->hostNeedsUpdate(); |
| 131 |
} |
| 132 |
|
| 133 |
bool SparseMatrixStorage::deviceNeedsUpdate() const { |
| 134 |
return outer_->deviceNeedsUpdate() || |
| 135 |
inner_->deviceNeedsUpdate() || |
| 136 |
value_->deviceNeedsUpdate(); |
| 137 |
} |
| 138 |
|
| 139 |
void SparseMatrixStorage::setHostNeedsUpdate(bool v) const { |
| 140 |
outer_->setHostNeedsUpdate(v); |
| 141 |
inner_->setHostNeedsUpdate(v); |
| 142 |
value_->setHostNeedsUpdate(v); |
| 143 |
} |
| 144 |
|
| 145 |
void SparseMatrixStorage::setDeviceNeedsUpdate(bool v) const { |
| 146 |
outer_->setDeviceNeedsUpdate(v); |
| 147 |
inner_->setDeviceNeedsUpdate(v); |
| 148 |
value_->setDeviceNeedsUpdate(v); |
| 149 |
} |
| 150 |
|
| 151 |
bool SparseMatrixStorage::deviceAllocated() const { |
| 152 |
return outer_->deviceAllocated() && |
| 153 |
inner_->deviceAllocated() && |
| 154 |
value_->deviceAllocated(); |
| 155 |
} |
| 156 |
|
| 157 |
void SparseMatrixStorage::allocateDevice() const { |
| 158 |
outer_->allocateDevice(); |
| 159 |
inner_->allocateDevice(); |
| 160 |
value_->allocateDevice(); |
| 161 |
} |
| 162 |
|
| 163 |
void SparseMatrixStorage::deallocateDevice() const { |
| 164 |
outer_->deallocateDevice(); |
| 165 |
inner_->deallocateDevice(); |
| 166 |
value_->deallocateDevice(); |
| 167 |
} |
| 168 |
|
| 169 |
namespace detail { |
| 170 |
template<typename OutputT, typename InputT> |
| 171 |
void host_copy(const InputT* input_data, array::Array& output) { |
| 172 |
auto size = output.size(); |
| 173 |
OutputT* output_data = output.host_data<OutputT>(); |
| 174 |
std::copy( input_data, input_data + size, output_data ); |
| 175 |
} |
| 176 |
|
| 177 |
template<typename InputT, typename OutputT> |
| 178 |
void host_copy(const array::Array& input, array::Array& output) { |
| 179 |
host_copy<OutputT>( input.host_data<InputT>(), output ); |
| 180 |
} |
| 181 |
|
| 182 |
template<typename OutputT> |
| 183 |
void host_copy(const array::Array& input, array::Array& output) { |
| 184 |
switch(input.datatype().kind()) { |
| 185 |
case DataType::kind<int>(): return host_copy<int,OutputT>( input, output ); |
| 186 |
case DataType::kind<long>(): return host_copy<long,OutputT>( input, output ); |
| 187 |
case DataType::kind<float>(): return host_copy<float,OutputT>( input, output ); |
| 188 |
case DataType::kind<double>(): return host_copy<double,OutputT>( input, output ); |
| 189 |
case DataType::kind<unsigned int>(): return host_copy<unsigned int,OutputT>( input, output ); |
| 190 |
case DataType::kind<unsigned long>(): return host_copy<unsigned long,OutputT>( input, output ); |
| 191 |
default: ATLAS_NOTIMPLEMENTED; |
| 192 |
} |
| 193 |
} |
| 194 |
void host_copy(const array::Array& input, array::Array& output) { |
| 195 |
switch(output.datatype().kind()) { |
| 196 |
case DataType::kind<int>(): return host_copy<int>( input, output ); |
| 197 |
case DataType::kind<long>(): return host_copy<long>( input, output ); |
| 198 |
case DataType::kind<float>(): return host_copy<float>( input, output ); |
| 199 |
case DataType::kind<double>(): return host_copy<double>( input, output ); |
| 200 |
case DataType::kind<unsigned int>(): return host_copy<unsigned int>( input, output ); |
| 201 |
case DataType::kind<unsigned long>(): return host_copy<unsigned long>( input, output ); |
| 202 |
default: ATLAS_NOTIMPLEMENTED; |
| 203 |
} |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
} |