forked from scikit-hep/awkward
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Identities.cpp
397 lines (358 loc) · 12.1 KB
/
Identities.cpp
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
// BSD 3-Clause License; see https://github.com/scikit-hep/awkward-1.0/blob/main/LICENSE
#define FILENAME(line) FILENAME_FOR_EXCEPTIONS("src/libawkward/Identities.cpp", line)
#define FILENAME_C(line) FILENAME_FOR_EXCEPTIONS_C("src/libawkward/Identities.cpp", line)
#include <cstring>
#include <atomic>
#include <iomanip>
#include <sstream>
#include <type_traits>
#include "awkward/kernels.h"
#include "awkward/kernel-utils.h"
#include "awkward/Slice.h"
#define AWKWARD_IDENTITIES_NO_EXTERN_TEMPLATE
#include "awkward/Identities.h"
namespace awkward {
std::atomic<Identities::Ref> numrefs{0};
Identities::Ref
Identities::newref() {
return numrefs++;
}
IdentitiesPtr
Identities::none() {
return IdentitiesPtr(nullptr);
}
Identities::Identities(const Ref ref,
const FieldLoc& fieldloc,
int64_t offset,
int64_t width,
int64_t length)
: ref_(ref)
, fieldloc_(fieldloc)
, offset_(offset)
, width_(width)
, length_(length) { }
Identities::~Identities() = default;
const Identities::Ref
Identities::ref() const {
return ref_;
}
const Identities::FieldLoc
Identities::fieldloc() const {
return fieldloc_;
}
const int64_t
Identities::offset() const {
return offset_;
}
const int64_t
Identities::width() const {
return width_;
}
const int64_t
Identities::length() const {
return length_;
}
template <typename T>
IdentitiesOf<T>::IdentitiesOf(const Ref ref,
const FieldLoc& fieldloc,
int64_t width,
int64_t length,
kernel::lib ptr_lib)
: Identities(ref, fieldloc, 0, width, length)
, ptr_(kernel::malloc<T>(ptr_lib, width * length * (int64_t)sizeof(T)))
, ptr_lib_(ptr_lib) { }
template <typename T>
IdentitiesOf<T>::IdentitiesOf(const Ref ref,
const FieldLoc& fieldloc,
int64_t offset,
int64_t width,
int64_t length,
const std::shared_ptr<T> ptr,
kernel::lib ptr_lib)
: Identities(ref, fieldloc, offset, width, length)
, ptr_(ptr)
, ptr_lib_(ptr_lib) { }
template <typename T>
const std::shared_ptr<T>
IdentitiesOf<T>::ptr() const {
return ptr_;
}
template<typename T>
kernel::lib IdentitiesOf<T>::ptr_lib() const {
return ptr_lib_;
}
template <typename T>
T*
IdentitiesOf<T>::data() const {
return ptr_.get() + offset_;
}
template <typename T>
const std::string
IdentitiesOf<T>::classname() const {
if (std::is_same<T, int32_t>::value) {
return "Identities32";
}
else if (std::is_same<T, int64_t>::value) {
return "Identities64";
}
else {
return "UnrecognizedIdentities";
}
}
template <typename T>
const std::string
IdentitiesOf<T>::identity_at(int64_t at) const {
std::stringstream out;
for (int64_t i = 0; i < width_; i++) {
if (i != 0) {
out << ", ";
}
out << ptr_.get()[offset_ + at*width_ + i];
for (auto pair : fieldloc_) {
if (pair.first == i) {
out << ", " << util::quote(pair.second);
}
}
}
return out.str();
}
template <typename T>
const IdentitiesPtr
IdentitiesOf<T>::to64() const {
if (std::is_same<T, int64_t>::value) {
return shallow_copy();
}
else if (std::is_same<T, int32_t>::value) {
IdentitiesPtr out = std::make_shared<Identities64>(ref_,
fieldloc_,
width_,
length_);
Identities64* raw = reinterpret_cast<Identities64*>(out.get());
kernel::Identities_to_Identities64<int32_t>(
kernel::lib::cpu, // DERIVE
raw->data(),
reinterpret_cast<int32_t*>(data()),
length_,
width_);
return out;
}
}
template <typename T>
const std::string
IdentitiesOf<T>::tostring_part(const std::string& indent,
const std::string& pre,
const std::string& post) const {
std::stringstream out;
std::string name = "Unrecognized Identities";
if (std::is_same<T, int32_t>::value) {
name = "Identities32";
}
else if (std::is_same<T, int64_t>::value) {
name = "Identities64";
}
out << indent << pre << "<" << name << " ref=\"" << ref_
<< "\" fieldloc=\"";
for (size_t i = 0; i < fieldloc_.size(); i++) {
if (i != 0) {
out << " ";
}
out << fieldloc_[i].first << ": " << fieldloc_[i].second;
}
out << "\" width=\"" << width_ << "\" offset=\"" << offset_
<< "\" length=\"" << length_ << "\" at=\"0x";
out << std::hex << std::setw(12) << std::setfill('0')
<< reinterpret_cast<ssize_t>(ptr_.get()) << "\"/>" << post;
return out.str();
}
template <typename T>
const IdentitiesPtr
IdentitiesOf<T>:: getitem_range_nowrap(int64_t start, int64_t stop) const {
if (!(0 <= start && start < length_ && 0 <= stop && stop <= length_)
&& start != stop) {
throw std::runtime_error(
std::string("Identities::getitem_range_nowrap with illegal start:stop "
"for this length") + FILENAME(__LINE__));
}
return std::make_shared<IdentitiesOf<T>>(
ref_,
fieldloc_,
offset_ + width_*start*(start != stop),
width_,
(stop - start),
ptr_);
}
template <typename T>
void
IdentitiesOf<T>::nbytes_part(std::map<size_t, int64_t>& largest) const {
size_t x = (size_t)ptr_.get();
auto it = largest.find(x);
if (it == largest.end() ||
it->second < (int64_t)sizeof(T)*length_*width_) {
largest[x] = (int64_t)sizeof(T)*length_*width_;
}
}
template <typename T>
const IdentitiesPtr
IdentitiesOf<T>::shallow_copy() const {
return std::make_shared<IdentitiesOf<T>>(ref_,
fieldloc_,
offset_,
width_,
length_,
ptr_);
}
template <typename T>
const IdentitiesPtr
IdentitiesOf<T>::deep_copy() const {
std::shared_ptr<T> ptr = kernel::malloc<T>(kernel::lib::cpu, // DERIVE
length_*(int64_t)sizeof(T));
if (length_ != 0) {
// DERIVE Yikes! It's a memcpy!
memcpy(ptr.get(),
&ptr_.get()[(size_t)offset_],
sizeof(T)*((size_t)length_));
}
return std::make_shared<IdentitiesOf<T>>(ref_,
fieldloc_,
0,
width_,
length_,
ptr);
}
template <typename T>
const IdentitiesPtr
IdentitiesOf<T>::getitem_carry_64(const Index64& carry) const {
IdentitiesPtr out = std::make_shared<IdentitiesOf<T>>(ref_,
fieldloc_,
width_,
carry.length());
IdentitiesOf<T>* rawout = reinterpret_cast<IdentitiesOf<T>*>(out.get());
if (std::is_same<T, int32_t>::value) {
struct Error err = kernel::Identities_getitem_carry_64<int32_t>(
kernel::lib::cpu, // DERIVE
reinterpret_cast<int32_t*>(rawout->data()),
reinterpret_cast<int32_t*>(data()),
carry.data(),
carry.length(),
width_,
length_);
util::handle_error(err, classname(), nullptr);
}
else if (std::is_same<T, int64_t>::value) {
struct Error err = kernel::Identities_getitem_carry_64<int64_t>(
kernel::lib::cpu, // DERIVE
reinterpret_cast<int64_t*>(rawout->data()),
reinterpret_cast<int64_t*>(data()),
carry.data(),
carry.length(),
width_,
length_);
util::handle_error(err, classname(), nullptr);
}
else {
throw std::runtime_error(
std::string("unrecognized Identities specialization") + FILENAME(__LINE__));
}
return out;
}
const std::string
Identities::tostring() const {
return tostring_part("", "", "");
}
template <typename T>
const IdentitiesPtr
IdentitiesOf<T>::withfieldloc(const FieldLoc& fieldloc) const {
return std::make_shared<IdentitiesOf<T>>(ref_,
fieldloc,
offset_,
width_,
length_,
ptr_);
}
template <typename T>
int64_t
IdentitiesOf<T>::value(int64_t row, int64_t col) const {
return (int64_t)ptr_.get()[offset_ + row*width_ + col];
}
template <typename T>
const std::vector<T>
IdentitiesOf<T>::getitem_at(int64_t at) const {
int64_t regular_at = at;
if (regular_at < 0) {
regular_at += length_;
}
if (!(0 <= regular_at && regular_at < length_)) {
util::handle_error(failure("index out of range", kSliceNone, at, FILENAME_C(__LINE__)),
classname(),
nullptr);
}
return getitem_at_nowrap(regular_at);
}
template <typename T>
const std::vector<T>
IdentitiesOf<T>::getitem_at_nowrap(int64_t at) const {
if (!(0 <= at && at < length_)) {
throw std::runtime_error(
std::string("Identities::getitem_at_nowrap with illegal index for this length")
+ FILENAME(__LINE__));
}
std::vector<T> out;
for (size_t i = (size_t)(offset_ + at);
i < (size_t)(offset_ + at + width_);
i++) {
out.push_back(ptr_.get()[i]);
}
return out;
}
template <typename T>
const IdentitiesPtr
IdentitiesOf<T>::getitem_range(int64_t start, int64_t stop) const {
int64_t regular_start = start;
int64_t regular_stop = stop;
kernel::regularize_rangeslice(®ular_start, ®ular_stop,
true, start != Slice::none(), stop != Slice::none(), length_);
return getitem_range_nowrap(regular_start, regular_stop);
}
template <typename T>
const IdentitiesPtr
IdentitiesOf<T>::copy_to(kernel::lib ptr_lib) const {
if (ptr_lib == ptr_lib_) {
return shallow_copy();
}
else {
int64_t bytelength = (offset_ + (width_ * length_)) * (int64_t)sizeof(T);
std::shared_ptr<T> ptr = kernel::malloc<T>(ptr_lib, bytelength);
Error err = kernel::copy_to(ptr_lib,
ptr_lib_,
ptr.get(),
ptr_.get(),
bytelength);
util::handle_error(err);
return std::make_shared<IdentitiesOf<T>>(ref_,
fieldloc_,
offset_,
width_,
length_,
ptr,
ptr_lib);
}
}
template <typename T>
bool
IdentitiesOf<T>::referentially_equal(const IdentitiesPtr& other) const {
if (IdentitiesOf<T>* raw = dynamic_cast<IdentitiesOf<T>*>(other.get())) {
return ref_ == raw->ref() &&
fieldloc_ == raw->fieldloc() &&
offset_ == raw->offset() &&
width_ == raw->width() &&
length_ == raw->length() &&
ptr_ == raw->ptr() &&
ptr_lib_ == raw->ptr_lib();
}
else {
return false;
}
}
template class EXPORT_TEMPLATE_INST IdentitiesOf<int32_t>;
template class EXPORT_TEMPLATE_INST IdentitiesOf<int64_t>;
}