forked from arrayfire/arrayfire
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSparseArray.cpp
More file actions
262 lines (235 loc) · 11.8 KB
/
SparseArray.cpp
File metadata and controls
262 lines (235 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*******************************************************
* Copyright (c) 2014, ArrayFire
* All rights reserved.
*
* This file is distributed under 3-clause BSD license.
* The complete license agreement can be obtained at:
* http://arrayfire.com/licenses/BSD-3-Clause
********************************************************/
#include <backend.hpp>
#include <common/SparseArray.hpp>
#include <copy.hpp>
#include <math.hpp>
#include <platform.hpp>
#include <af/traits.hpp>
using af::dim4;
using af::dtype_traits;
using detail::Array;
using detail::cdouble;
using detail::cfloat;
using detail::copyArray;
using detail::createDeviceDataArray;
using detail::createHostDataArray;
using detail::createValueArray;
using detail::getActiveDeviceId;
using detail::scalar;
using detail::writeDeviceDataArray;
namespace common {
////////////////////////////////////////////////////////////////////////////
// Sparse Array Base Implementations
////////////////////////////////////////////////////////////////////////////
// ROW_LENGTH and column length expect standard variable names of
// SparseArrayBase::stype
// _nNZ -> Constructor Argument
// _dims -> Constructor Argument
#define ROW_LENGTH \
((stype == AF_STORAGE_COO || stype == AF_STORAGE_CSC) ? _nNZ \
: (_dims[0] + 1))
#define COL_LENGTH \
((stype == AF_STORAGE_COO || stype == AF_STORAGE_CSR) ? _nNZ \
: (_dims[1] + 1))
SparseArrayBase::SparseArrayBase(const af::dim4 &_dims, dim_t _nNZ,
af::storage _storage, af_dtype _type)
: info(getActiveDeviceId(), _dims, 0, calcStrides(_dims), _type, true)
, stype(_storage)
, rowIdx(createValueArray<int>(dim4(ROW_LENGTH), 0))
, colIdx(createValueArray<int>(dim4(COL_LENGTH), 0)) {
static_assert(offsetof(SparseArrayBase, info) == 0,
"SparseArrayBase::info must be the first member variable of "
"SparseArrayBase.");
static_assert(std::is_nothrow_move_assignable<SparseArrayBase>::value,
"SparseArrayBase is not move assignable");
static_assert(std::is_nothrow_move_constructible<SparseArrayBase>::value,
"SparseArrayBase is not move constructible");
}
SparseArrayBase::SparseArrayBase(const af::dim4 &_dims, dim_t _nNZ,
int *const _rowIdx, int *const _colIdx,
const af::storage _storage, af_dtype _type,
bool _is_device, bool _copy_device)
: info(getActiveDeviceId(), _dims, 0, calcStrides(_dims), _type, true)
, stype(_storage)
, rowIdx(_is_device
? (!_copy_device
? createDeviceDataArray<int>(dim4(ROW_LENGTH), _rowIdx)
: createValueArray<int>(dim4(ROW_LENGTH), 0))
: createHostDataArray<int>(dim4(ROW_LENGTH), _rowIdx))
, colIdx(_is_device
? (!_copy_device
? createDeviceDataArray<int>(dim4(COL_LENGTH), _colIdx)
: createValueArray<int>(dim4(COL_LENGTH), 0))
: createHostDataArray<int>(dim4(COL_LENGTH), _colIdx)) {
static_assert(offsetof(SparseArrayBase, info) == 0,
"SparseArrayBase::info must be the first member variable of "
"SparseArrayBase.");
if (_is_device && _copy_device) {
writeDeviceDataArray<int>(rowIdx, _rowIdx, ROW_LENGTH * sizeof(int));
writeDeviceDataArray<int>(colIdx, _colIdx, COL_LENGTH * sizeof(int));
}
}
SparseArrayBase::SparseArrayBase(const af::dim4 &_dims,
const Array<int> &_rowIdx,
const Array<int> &_colIdx,
const af::storage _storage, af_dtype _type,
bool _copy)
: info(getActiveDeviceId(), _dims, 0, calcStrides(_dims), _type, true)
, stype(_storage)
, rowIdx(_copy ? copyArray<int>(_rowIdx) : _rowIdx)
, colIdx(_copy ? copyArray<int>(_colIdx) : _colIdx) {
static_assert(offsetof(SparseArrayBase, info) == 0,
"SparseArrayBase::info must be the first member variable of "
"SparseArrayBase.");
}
SparseArrayBase::SparseArrayBase(const SparseArrayBase &base, bool copy)
: info(base.info)
, stype(base.stype)
, rowIdx(copy ? copyArray<int>(base.rowIdx) : base.rowIdx)
, colIdx(copy ? copyArray<int>(base.colIdx) : base.colIdx) {}
SparseArrayBase::~SparseArrayBase() = default;
dim_t SparseArrayBase::getNNZ() const {
if (stype == AF_STORAGE_COO || stype == AF_STORAGE_CSC) {
return rowIdx.elements();
}
if (stype == AF_STORAGE_CSR) { return colIdx.elements(); }
// This is to ensure future storages are properly configured
return 0;
}
#undef ROW_LENGTH
#undef COL_LENGTH
////////////////////////////////////////////////////////////////////////////
// Friend functions for Sparse Array Creation Implementations
////////////////////////////////////////////////////////////////////////////
template<typename T>
SparseArray<T> createEmptySparseArray(const af::dim4 &_dims, dim_t _nNZ,
const af::storage _storage) {
return SparseArray<T>(_dims, _nNZ, _storage);
}
template<typename T>
SparseArray<T> createHostDataSparseArray(const af::dim4 &_dims, const dim_t nNZ,
const T *const _values,
const int *const _rowIdx,
const int *const _colIdx,
const af::storage _storage) {
return SparseArray<T>(_dims, nNZ, const_cast<T *>(_values),
const_cast<int *>(_rowIdx),
const_cast<int *>(_colIdx), _storage, false);
}
template<typename T>
SparseArray<T> createDeviceDataSparseArray(
const af::dim4 &_dims, const dim_t nNZ, T *const _values,
int *const _rowIdx, // NOLINT(readability-non-const-parameter)
int *const _colIdx, // NOLINT(readability-non-const-parameter)
const af::storage _storage, const bool _copy) {
return SparseArray<T>(_dims, nNZ, _values, _rowIdx, _colIdx, _storage, true,
_copy);
}
template<typename T>
SparseArray<T> createArrayDataSparseArray(
const af::dim4 &_dims, const Array<T> &_values, const Array<int> &_rowIdx,
const Array<int> &_colIdx, const af::storage _storage, const bool _copy) {
return SparseArray<T>(_dims, _values, _rowIdx, _colIdx, _storage, _copy);
}
template<typename T>
SparseArray<T> copySparseArray(const SparseArray<T> &other) {
return SparseArray<T>(other, true);
}
template<typename T>
SparseArray<T> *initSparseArray() {
return new SparseArray<T>(dim4(), 0, (af::storage)0);
}
template<typename T>
void destroySparseArray(SparseArray<T> *sparse) {
delete sparse;
}
////////////////////////////////////////////////////////////////////////////
// Sparse Array Class Implementations
////////////////////////////////////////////////////////////////////////////
template<typename T>
SparseArray<T>::SparseArray(const dim4 &_dims, dim_t _nNZ, af::storage _storage)
: base(_dims, _nNZ, _storage,
static_cast<af_dtype>(dtype_traits<T>::af_type))
, values(createValueArray<T>(dim4(_nNZ), scalar<T>(0))) {
static_assert(std::is_standard_layout<SparseArray<T>>::value,
"SparseArray<T> must be a standard layout type");
static_assert(std::is_nothrow_move_assignable<SparseArray<T>>::value,
"SparseArray<T> is not move assignable");
static_assert(std::is_nothrow_move_constructible<SparseArray<T>>::value,
"SparseArray<T> is not move constructible");
static_assert(offsetof(SparseArray<T>, base) == 0,
"SparseArray<T>::base must be the first member variable of "
"SparseArray<T>");
}
template<typename T>
SparseArray<T>::SparseArray(const af::dim4 &_dims, dim_t _nNZ, T *const _values,
int *const _rowIdx, int *const _colIdx,
const af::storage _storage, bool _is_device,
bool _copy_device)
: base(_dims, _nNZ, _rowIdx, _colIdx, _storage,
static_cast<af_dtype>(dtype_traits<T>::af_type), _is_device,
_copy_device)
, values(_is_device ? (!_copy_device
? createDeviceDataArray<T>(dim4(_nNZ), _values)
: createValueArray<T>(dim4(_nNZ), scalar<T>(0)))
: createHostDataArray<T>(dim4(_nNZ), _values)) {
if (_is_device && _copy_device) {
writeDeviceDataArray<T>(values, _values, _nNZ * sizeof(T));
}
}
template<typename T>
SparseArray<T>::SparseArray(const af::dim4 &_dims, const Array<T> &_values,
const Array<int> &_rowIdx,
const Array<int> &_colIdx,
const af::storage _storage, bool _copy)
: base(_dims, _rowIdx, _colIdx, _storage,
static_cast<af_dtype>(dtype_traits<T>::af_type), _copy)
, values(_copy ? copyArray<T>(_values) : _values) {}
template<typename T>
SparseArray<T>::SparseArray(const SparseArray<T> &other, bool copy)
: base(other.base, copy)
, values(copy ? copyArray<T>(other.values) : other.values) {}
#define INSTANTIATE(T) \
template SparseArray<T> createEmptySparseArray<T>( \
const af::dim4 &_dims, dim_t _nNZ, const af::storage _storage); \
template SparseArray<T> createHostDataSparseArray<T>( \
const af::dim4 &_dims, const dim_t _nNZ, const T *const _values, \
const int *const _rowIdx, const int *const _colIdx, \
const af::storage _storage); \
template SparseArray<T> createDeviceDataSparseArray<T>( \
const af::dim4 &_dims, const dim_t _nNZ, \
T *const _values, /* NOLINT */ \
int *const _rowIdx, int *const _colIdx, const af::storage _storage, \
const bool _copy); \
template SparseArray<T> createArrayDataSparseArray<T>( \
const af::dim4 &_dims, const Array<T> &_values, \
const Array<int> &_rowIdx, const Array<int> &_colIdx, \
const af::storage _storage, const bool _copy); \
template SparseArray<T> *initSparseArray<T>(); \
template SparseArray<T> copySparseArray<T>(const SparseArray<T> &other); \
template void destroySparseArray<T>(SparseArray<T> * sparse); \
\
template SparseArray<T>::SparseArray(const af::dim4 &_dims, dim_t _nNZ, \
af::storage _storage); \
template SparseArray<T>::SparseArray( \
const af::dim4 &_dims, dim_t _nNZ, T *const _values, /* NOLINT */ \
int *const _rowIdx, int *const _colIdx, const af::storage _storage, \
bool _is_device, bool _copy_device); \
template SparseArray<T>::SparseArray( \
const af::dim4 &_dims, const Array<T> &_values, \
const Array<int> &_rowIdx, const Array<int> &_colIdx, \
const af::storage _storage, bool _copy)
// Instantiate only floating types
INSTANTIATE(float);
INSTANTIATE(double);
INSTANTIATE(cfloat);
INSTANTIATE(cdouble);
#undef INSTANTIATE
} // namespace common