Repository navigation
Expand file tree
/
Copy pathpycsparse.h
More file actions
248 lines (228 loc) · 8.93 KB
/
Copy pathpycsparse.h
File metadata and controls
248 lines (228 loc) · 8.93 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
#ifndef PYCSPARSEMAT_H
#define PYCSPARSEMAT_H
#include "pybind11/numpy.h"
#include "pybind11/pytypes.h"
#include "blaze/util/Serialization.h"
#include "minicore/util/csc.h"
#include "pyhelpers.h"
#define ENABLE_CONST_FUNCS 1
#define ENABLE_NONCONST_FUNCS 0
#ifndef ENABLE_8BITINT_DATA
#define ENABLE_8BITINT_DATA 0
#endif
#ifndef ENABLE_16BITINT_DATA
#define ENABLE_16BITINT_DATA 1
#endif
#ifndef ENABLE_64BITINT_DATA
#define ENABLE_64BITINT_DATA 0
#endif
#ifndef ENABLE_8BITINT_INDICES
#define ENABLE_8BITINT_INDICES 0
#endif
#ifndef ENABLE_16BITINT_INDICES
#define ENABLE_16BITINT_INDICES 1
#endif
using namespace minicore;
namespace py = pybind11;
struct PyCSparseMatrix {
void *datap_;
void *indicesp_;
void *indptrp_;
std::string data_t_;
std::string indices_t_;
std::string indptr_t_;
size_t nr_, nc_, nnz_;
template<typename DataT, typename IndicesT, typename IndPtrT>
PyCSparseMatrix(DataT *data, const IndicesT *indices, const IndPtrT *indptr, Py_ssize_t nr, Py_ssize_t nc, Py_ssize_t nnz):
datap_((void *)data),
indicesp_((void *)indices),
indptrp_((void *)indptr),
data_t_(py::format_descriptor<DataT>::format()),
indices_t_(py::format_descriptor<IndicesT>::format()),
indptr_t_(py::format_descriptor<IndPtrT>::format()),
nr_(nr), nc_(nc), nnz_(nnz)
{
}
PyCSparseMatrix(): datap_(nullptr), indicesp_(nullptr), indptrp_(nullptr), nr_(0), nc_(0), nnz_(0) {}
PyCSparseMatrix(const PyCSparseMatrix &) = default;
PyCSparseMatrix(PyCSparseMatrix &&) = default;
PyCSparseMatrix &operator=(const PyCSparseMatrix &) = default;
PyCSparseMatrix &operator=(PyCSparseMatrix &&) = default;
PyCSparseMatrix(py::object obj): PyCSparseMatrix(py::cast<py::array>(obj.attr("data")), py::cast<py::array>(obj.attr("indices")), py::cast<py::array>(obj.attr("indptr")), py::int_(py::cast<py::sequence>(obj.attr("shape"))[0]).cast<Py_ssize_t>(),
py::int_(py::cast<py::sequence>(obj.attr("shape"))[1]).cast<Py_ssize_t>(), obj.attr("nnz").cast<Py_ssize_t>()) {}
PyCSparseMatrix(py::array data, py::array indices, py::array indptr, Py_ssize_t nr, Py_ssize_t nc, Py_ssize_t nnz): nr_(nr), nc_(nc), nnz_(nnz)
{
auto datainf = data.request();
auto indicesinf = indices.request();
auto indptrinf = indptr.request();
datap_ = datainf.ptr;
indicesp_ = indicesinf.ptr;
indptrp_ = indptrinf.ptr;
data_t_ = standardize_dtype(datainf.format);
indices_t_ = standardize_dtype(indicesinf.format);
indptr_t_ = standardize_dtype(indptrinf.format);
}
#if ENABLE_CONST_FUNCS
template<typename Func> void perform(const Func &func) const {
switch(data_t_.front()) {
#if ENABLE_8BITINT_DATA
case 'b': case 'B': _perform<uint8_t, Func>(func); break;
#endif
#if ENABLE_16BITINT_DATA
case 'h': case 'H': _perform<uint16_t, Func>(func); break;
#endif
#if ENABLE_64BITINT_DATA
case 'q': case 'l': case 'u': case 'L': _perform<uint64_t, Func>(func); break;
#endif
//case 'i': case 'I': _perform<unsigned, Func>(func); break;
case 'f': _perform<float, Func>(func); break;
case 'd': _perform<double, Func>(func); break;
default: throw std::invalid_argument(std::string("Unsupported type for data: ") + data_t_);
}
}
#endif
#if ENABLE_NONCONST_FUNCS
template<typename Func> void perform(const Func &func) {
switch(data_t_.front()) {
#if ENABLE_8BITINT_DATA
case 'b': case 'B': _perform<uint8_t, Func>(func); break;
#endif
#if ENABLE_16BITINT_DATA
case 'h': case 'H': _perform<uint16_t, Func>(func); break;
#endif
#if ENABLE_64BITINT_DATA
case 'q': case 'l': case 'u': case 'L': _perform<uint64_t, Func>(func); break;
#endif
//case 'i': case 'I': _perform<unsigned, Func>(func); break;
case 'f': _perform<float, Func>(func); break;
case 'd': _perform<double, Func>(func); break;
default: throw std::invalid_argument(std::string("Unsupported type for data: ") + data_t_);
}
}
#endif
#if ENABLE_CONST_FUNCS
template<typename Func> void perform(const PyCSparseMatrix &rhs, const Func &func) const {
switch(data_t_.front()) {
#if ENABLE_8BITINT_DATA
case 'b': case 'B': _perform<uint8_t, Func>(rhs, func); break;
#endif
#if ENABLE_16BITINT_DATA
case 'h': case 'H': _perform<uint16_t, Func>(rhs, func); break;
#endif
#if ENABLE_64BITINT_DATA
case 'q': case 'l': case 'u': case 'L': _perform<uint64_t, Func>(rhs, func); break;
#endif
//case 'i': case 'I': _perform<unsigned, Func>(func); break;
case 'f': _perform<float, Func>(rhs, func); break;
case 'd': _perform<double, Func>(rhs, func); break;
default: throw std::invalid_argument(std::string("Unsupported type for data: ") + data_t_);
}
}
#endif
#if ENABLE_NONCONST_FUNCS
template<typename Func> void perform(PyCSparseMatrix &rhs, const Func &func) {
switch(data_t_.front()) {
#if ENABLE_8BITINT_DATA
case 'b': case 'B': _perform<uint8_t, Func>(rhs, func); break;
#endif
#if ENABLE_16BITINT_DATA
case 'h': case 'H': _perform<uint16_t, Func>(rhs, func); break;
#endif
#if ENABLE_64BITINT_DATA
case 'q': case 'l': case 'u': case 'L': _perform<uint64_t, Func>(rhs, func); break;
#endif
//case 'i': case 'I': _perform<unsigned, Func>(rhs, func); break;
case 'f': _perform<float, Func>(rhs, func); break;
case 'd': _perform<double, Func>(rhs, func); break;
default: throw std::invalid_argument(std::string("Unsupported type for data: ") + data_t_);
}
}
#endif
#define PERF3(c, c2, IPtr, Indices) \
case c: case c2: {auto smat = util::make_csparse_matrix((DataT *)datap_, (Indices *)indicesp_, (IPtr *)indptrp_, nr_, nc_, nnz_); func(smat);} break
#define PERF2(c, c2, Indices) \
case c: case c2: { \
switch(indptr_t_[0]) { \
PERF3('L', 'l', uint64_t, Indices);\
PERF3('I', 'i', uint32_t, Indices);\
}\
} break
#if ENABLE_CONST_FUNCS
template<typename DataT, typename Func> void _perform(const Func &func) const {
switch(indices_t_[0]) {
#if ENABLE_8BITINT_INDICES
PERF2('B', 'b', uint8_t);
#endif
#if ENABLE_16BITINT_INDICES
PERF2('H', 'h', uint16_t);
#endif
PERF2('I', 'i', uint32_t);
//PERF2('L', 'l', uint64_t);
default: throw std::invalid_argument(std::string("Unsupported type for indices: ") + indices_t_);
}
}
#endif
#if ENABLE_NONCONST_FUNCS
template<typename DataT, typename Func> void _perform(const Func &func) {
switch(indices_t_.front()) {
#if ENABLE_8BITINT_INDICES
PERF2('B', 'b', uint8_t);
#endif
#if ENABLE_16BITINT_INDICES
PERF2('H', 'h', uint16_t);
#endif
PERF2('I', 'i', uint32_t);
//PERF2('L', 'l', uint64_t);
default: throw std::invalid_argument(std::string("Unsupported type for indices: ") + indices_t_);
}
}
#endif
#undef PERF3
#undef PERF2
#define PERF3(c, c2, IPtr, Indices) \
case c: case c2: {auto lhsmat = util::make_csparse_matrix((DataT *)datap_, (Indices *)indicesp_, (IPtr *)indptrp_, nr_, nc_, nnz_); \
auto rhsmat = util::make_csparse_matrix((DataT *)rhs.datap_, (Indices *)rhs.indicesp_, (IPtr *)rhs.indptrp_, rhs.nr_, rhs.nc_, rhs.nnz_); \
func(lhsmat, rhsmat);\
} break
#define PERF2(c, c2, Indices) \
case c: case c2: { \
switch(indptr_t_[0]) { \
PERF3('L', 'l', uint64_t, Indices);\
PERF3('I', 'i', uint32_t, Indices);\
}\
} break
#if ENABLE_CONST_FUNCS
template<typename DataT, typename Func> void _perform(const PyCSparseMatrix &rhs, const Func &func) const {
switch(indices_t_[0]) {
#if ENABLE_8BITINT_INDICES
PERF2('B', 'b', uint8_t);
#endif
#if ENABLE_16BITINT_INDICES
PERF2('H', 'h', uint16_t);
#endif
PERF2('I', 'i', uint32_t);
default: throw std::invalid_argument(std::string("Unsupported type for indices: ") + indices_t_);
}
}
#endif
#if ENABLE_NONCONST_FUNCS
template<typename DataT, typename Func> void _perform(PyCSparseMatrix &rhs, const Func &func) {
switch(indices_t_.front()) {
#if ENABLE_8BITINT_INDICES
PERF2('B', 'b', uint8_t);
#endif
#if ENABLE_16BITINT_INDICES
PERF2('H', 'h', uint16_t);
#endif
PERF2('I', 'i', uint32_t);
default: throw std::invalid_argument(std::string("Unsupported type for indices: ") + indices_t_);
}
}
#endif
size_t rows() const {return nr_;}
size_t columns() const {return nc_;}
size_t nnz() const {return nnz_;}
};
#undef PERF3
#undef PERF2
#endif