Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 190
Expand file tree
/
Copy pathdaal4py.cpp
More file actions
Latest commit
executable file
·900 lines (823 loc) · 37.4 KB
/
Copy pathdaal4py.cpp
File metadata and controls
executable file
·900 lines (823 loc) · 37.4 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
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
/*******************************************************************************
* Copyright 2014 Intel Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
#defineNO_IMPORT_ARRAY
#include<cstdint>
#include<cstring>
#include<limits>
#include<memory>
#include<Python.h>
#include"daal4py.h"
#include"npy4daal.h"
#include"daal4py_defines.h"
#if NPY_ABI_VERSION < 0x02000000
#definePyDataType_NAMES(descr) ((descr)->names)
#endif
// ************************************************************************************
// ************************************************************************************
// Numpy type conversion code, taken from numpy.i (SWIG typemap-code)
// ************************************************************************************
// ************************************************************************************
#defineis_array(a) ((a) && PyArray_Check(a))
#definearray_type(a) PyArray_TYPE((PyArrayObject *)a)
#definearray_is_behaved_C(a) (PyArray_ISCARRAY_RO((PyArrayObject *)a) && array_type(a) < NPY_OBJECT)
#definearray_is_behaved_F(a) (PyArray_ISFARRAY_RO((PyArrayObject *)a) && array_type(a) < NPY_OBJECT)
#definearray_is_native(a) (PyArray_ISNOTSWAPPED((PyArrayObject *)a))
#definearray_numdims(a) PyArray_NDIM((PyArrayObject *)a)
#definearray_data(a) PyArray_DATA((PyArrayObject *)a)
#definearray_size(a, i) PyArray_DIM((PyArrayObject *)a, i)
// ************************************************************************************
// ************************************************************************************
classNumpyDeleter : publicdaal::services::DeleterIface
{
public:
// Keep a strong owner before exposing the raw NumPy buffer to oneDAL. Copies of
// this deleter share the owner, so constructor failure cannot run a callback
// against a borrowed reference.
explicitNumpyDeleter(PyArrayObject * a) : _ndarray(make_owner(a)) {}
NumpyDeleter(const NumpyDeleter &) = default;
// oneDAL owns only the buffer lifetime; the shared owner releases the Python
// reference when the final deleter copy is destroyed.
voidoperator()(constvoid *) override {}
NumpyDeleter & operator=(const NumpyDeleter &) = delete;
private:
static std::shared_ptr<PyArrayObject> make_owner(PyArrayObject * a)
{
// Symmetric with the deleter below. Every current caller reaches this from
// a Python-facing conversion and so already holds the GIL, which makes the
// Ensure a cheap no-op recursive acquisition; the deleter genuinely needs
// it, because oneDAL can drop the last table reference on a worker thread.
// Guarding both sides means neither depends on the other's caller.
PyGILState_STATE state = PyGILState_Ensure();
Py_INCREF(a);
PyGILState_Release(state);
return std::shared_ptr<PyArrayObject>(a, [](PyArrayObject * array) {
if (!can_decref_python_object()) return;
PyGILState_STATE state = PyGILState_Ensure();
Py_DECREF(array);
PyGILState_Release(state);
});
}
std::shared_ptr<PyArrayObject> _ndarray;
};
inlinevoidpy_err_check()
{
if (PyErr_Occurred())
{
PyErr_Print();
throwstd::runtime_error("Python error");
}
}
classPyOwnedRef
{
public:
explicitPyOwnedRef(PyObject * obj = nullptr) : _obj(obj) {}
~PyOwnedRef() { Py_XDECREF(_obj); }
PyOwnedRef(const PyOwnedRef &) = delete;
PyOwnedRef & operator=(const PyOwnedRef &) = delete;
PyObject * get() const { return _obj; }
private:
PyObject * _obj;
};
static PyObject * list_get_item_ref(PyObject * list, Py_ssize_t index)
{
#if PY_VERSION_HEX >= 0x030D0000
returnPyList_GetItemRef(list, index);
#else
PyObject * item = PyList_GetItem(list, index);
Py_XINCREF(item);
return item;
#endif
}
// *****************************************************************************
// Uses a shared pointer to a raw array (T*) for creating a nd-array
template <typename T, intNPTYPE>
static PyObject * _sp_to_nda(daal::services::SharedPtr<T> & sp, size_t nr, size_t nc)
{
DAAL4PY_CHECK_BAD_CAST(nc <= std::numeric_limits<int>::max());
DAAL4PY_CHECK_BAD_CAST(nr <= std::numeric_limits<int>::max());
npy_intp dims[2] = { static_cast<npy_intp>(nr), static_cast<npy_intp>(nc) };
PyObject * obj = PyArray_SimpleNewFromData(2, dims, NPTYPE, static_cast<void *>(sp.get()));
if (!obj) throwstd::invalid_argument("conversion to numpy array failed");
set_sp_base(reinterpret_cast<PyArrayObject *>(obj), sp);
return obj;
}
// get a block of Rows from NT and then create nd-array from it
// oneDAL potentially makes a copy when creating the BlockDesriptor
template <typename T, intNPTYPE>
static PyObject * _make_nda_from_bd(daal::data_management::NumericTablePtr * ptr)
{
daal::data_management::BlockDescriptor<T> block;
(*ptr)->getBlockOfRows(0, (*ptr)->getNumberOfRows(), daal::data_management::readOnly, block);
if (block.getNumberOfRows() != (*ptr)->getNumberOfRows() || block.getNumberOfColumns() != (*ptr)->getNumberOfColumns())
{
std::cerr << "Getting data ptr as block-of-rows failed.\nExpected shape: " << (*ptr)->getNumberOfRows() << "x" << (*ptr)->getNumberOfColumns() << "\nBlock shape:" << block.getNumberOfRows() << "x" << block.getNumberOfColumns() << std::endl;
returnNULL;
}
daal::services::SharedPtr<T> data_tmp = block.getBlockSharedPtr();
if (!data_tmp)
{
std::cerr << "Unexpected null pointer from block descriptor.";
returnNULL;
}
return _sp_to_nda<T, NPTYPE>(data_tmp, block.getNumberOfRows(), block.getNumberOfColumns());
}
// Most efficient conversion if NT is a HomogenNumericTable
// We do not need to make a copy and can use the raw data pointer directly.
template <typename T, intNPTYPE>
static PyObject * _make_nda_from_homogen(daal::data_management::NumericTablePtr * ptr)
{
auto dptr = dynamic_cast<daal::data_management::HomogenNumericTable<T> *>((*ptr).get());
if (dptr)
{
daal::services::SharedPtr<T> data_tmp(dptr->getArraySharedPtr());
return _sp_to_nda<T, NPTYPE>(data_tmp, (*ptr)->getNumberOfRows(), (*ptr)->getNumberOfColumns());
}
returnNULL;
}
template <typename T, intNPTYPE>
static PyObject * _make_npy_from_data(T * data, size_t n)
{
DAAL4PY_CHECK_BAD_CAST(n <= std::numeric_limits<int>::max());
npy_intp dims[1] = { static_cast<npy_intp>(n) };
PyObject * obj = PyArray_SimpleNewFromData(1, dims, NPTYPE, static_cast<void *>(data));
if (!obj) throwstd::invalid_argument("conversion to numpy array failed");
return obj;
}
template <typename T, intNPTYPE>
static PyObject * _make_nda_from_csr(daal::data_management::NumericTablePtr * ptr)
{
daal::data_management::CSRNumericTable * csr_ptr = dynamic_cast<daal::data_management::CSRNumericTable *>(const_cast<daal::data_management::NumericTable *>((*ptr).get()));
if (csr_ptr)
{
T * data_ptr;
size_t * col_indices_ptr;
size_t * row_offsets_ptr;
csr_ptr->getArrays<T>(&data_ptr, &col_indices_ptr, &row_offsets_ptr);
size_t n = csr_ptr->getDataSize();
T * data_copy = static_cast<T *>(daal::services::daal_malloc(n * sizeof(T)));
DAAL4PY_CHECK_MALLOC(data_copy);
daal::services::internal::daal_memcpy_s(data_ptr, sizeof(T) * n, data_copy, sizeof(T) * n);
PyObject * py_data = _make_npy_from_data<T, NPTYPE>(data_copy, n);
n = csr_ptr->getNumberOfColumns();
size_t * col_indices_copy = static_cast<size_t *>(daal::services::daal_malloc(n * sizeof(size_t)));
DAAL4PY_CHECK_MALLOC(col_indices_copy);
for (size_t i = 0; i < n; ++i)
{
col_indices_copy[i] = col_indices_ptr[i] - 1;
}
PyObject * py_col = _make_npy_from_data<size_t, NPTYPE>(col_indices_copy, n);
n = csr_ptr->getNumberOfRows();
size_t * row_offsets_copy = static_cast<size_t *>(daal::services::daal_malloc(n * sizeof(size_t)));
DAAL4PY_CHECK_MALLOC(row_offsets_copy);
for (size_t i = 0; i < n; ++i)
{
row_offsets_copy[i] = row_offsets_ptr[i] - 1;
}
PyObject * py_row = _make_npy_from_data<size_t, NPTYPE>(row_offsets_copy, n);
PyObject * result = PyTuple_New(3);
PyTuple_SetItem(result, 0, py_data);
PyTuple_SetItem(result, 1, py_col);
PyTuple_SetItem(result, 2, py_row);
return result;
}
returnNULL;
}
// Convert a oneDAL NT to a numpy nd-array
// tries to avoid copying the data, instead we try to share the memory with DAAL
PyObject * make_nda(daal::data_management::NumericTablePtr * ptr)
{
if (!ptr || !(*ptr).get() || (*ptr)->getNumberOfRows() == 0 || (*ptr)->getNumberOfRows() == 0) return Py_None;
PyObject * res = NULL;
// Try to convert from homogen/dense type as given in first column of NT
// first try HomogenNT, then via BlockDescriptor. The latter requires a copy.
switch ((*(*ptr)->getDictionary())[0].indexType)
{
case daal::data_management::data_feature_utils::DAAL_FLOAT64:
if ((res = _make_nda_from_homogen<double, NPY_FLOAT64>(ptr)) != NULL) return res;
if ((res = _make_nda_from_bd<double, NPY_FLOAT64>(ptr)) != NULL) return res;
if ((res = _make_nda_from_csr<double, NPY_FLOAT64>(ptr)) != NULL) return res;
break;
case daal::data_management::data_feature_utils::DAAL_FLOAT32:
if ((res = _make_nda_from_homogen<float, NPY_FLOAT32>(ptr)) != NULL) return res;
if ((res = _make_nda_from_bd<float, NPY_FLOAT32>(ptr)) != NULL) return res;
if ((res = _make_nda_from_csr<float, NPY_FLOAT32>(ptr)) != NULL) return res;
break;
case daal::data_management::data_feature_utils::DAAL_INT32_S:
if ((res = _make_nda_from_homogen<std::int32_t, NPY_INT32>(ptr)) != NULL) return res;
if ((res = _make_nda_from_csr<std::int32_t, NPY_INT32>(ptr)) != NULL) return res;
break;
case daal::data_management::data_feature_utils::DAAL_INT32_U:
if ((res = _make_nda_from_homogen<std::uint32_t, NPY_UINT32>(ptr)) != NULL) return res;
if ((res = _make_nda_from_csr<std::uint32_t, NPY_UINT32>(ptr)) != NULL) return res;
break;
case daal::data_management::data_feature_utils::DAAL_INT64_S:
if ((res = _make_nda_from_homogen<std::int64_t, NPY_INT64>(ptr)) != NULL) return res;
if ((res = _make_nda_from_csr<std::int64_t, NPY_INT64>(ptr)) != NULL) return res;
break;
case daal::data_management::data_feature_utils::DAAL_INT64_U:
if ((res = _make_nda_from_homogen<std::uint64_t, NPY_UINT64>(ptr)) != NULL) return res;
if ((res = _make_nda_from_csr<std::uint64_t, NPY_UINT64>(ptr)) != NULL) return res;
break;
}
// Falling back to using block-desriptors and converting to double
if ((res = _make_nda_from_bd<double, NPY_FLOAT64>(ptr)) != NULL) return res;
throwstd::invalid_argument("Got unsupported table type.");
}
// Create a list of numpy arrays
extern PyObject * make_nda(daal::data_management::DataCollectionPtr * coll)
{
if (PyErr_Occurred())
{
PyErr_Print();
PyErr_Clear();
}
if (!coll) return Py_None;
auto n = (*coll)->size();
PyObject * list = PyList_New(n);
for (auto i = 0; i < n; ++i)
{
daal::data_management::NumericTablePtr nt = daal::services::dynamicPointerCast<daal::data_management::NumericTable>((*coll)->get(i));
PyList_SetItem(list, i, make_nda(&nt));
py_err_check();
}
return list;
}
extern PyObject * make_nda(daal::data_management::KeyValueDataCollectionPtr * dict, consti2str_map_t & id2str)
{
PyObject * pydict = PyDict_New();
for (size_t i = 0; i < (*dict)->size(); ++i)
{
auto elem = (*dict)->getValueByIndex(i);
auto tbl = daal::services::dynamicPointerCast<daal::data_management::NumericTable>(elem);
// There can be NULL elements in collection
if (tbl || !elem)
{
PyObject * obj = tbl ? make_nda(&tbl) : Py_None;
size_t key = (*dict)->getKeyByIndex(i);
auto strkey = id2str.find(key);
if (strkey != id2str.end())
{
PyObject * keyobj = PyString_FromString(strkey->second.c_str());
PyDict_SetItem(pydict, keyobj, obj);
Py_DECREF(keyobj);
}
else
{
Py_DECREF(pydict);
throwstd::invalid_argument(std::string("Unexpected key '") + std::to_string(key) + "' found in KeyValueDataCollectionPtr\n");
}
}
else
{
Py_DECREF(pydict);
throwstd::invalid_argument("Unexpected object found in KeyValueDataCollectionPtr, expected NULL or NumericTable\n");
}
}
return pydict;
}
template <typename T>
static daal::data_management::NumericTablePtr _make_hnt(PyObject * nda)
{
daal::data_management::NumericTablePtr ptr;
PyArrayObject * array = reinterpret_cast<PyArrayObject *>(nda);
assert(is_array(nda) && array_is_behaved_C(array));
if (array_numdims(array) == 2)
{
// we provide the SharedPtr with a deleter which decrements the pyref
ptr = daal::data_management::HomogenNumericTable<T>::create(daal::services::SharedPtr<T>(reinterpret_cast<T *>(array_data(array)), NumpyDeleter(array)), static_cast<size_t>(array_size(array, 1)), static_cast<size_t>(array_size(array, 0)));
}
else
{
throwstd::invalid_argument("Input array has wrong dimensionality (must be 2d).\n");
}
return ptr;
}
static daal::data_management::NumericTablePtr _make_npynt(PyObject * nda)
{
daal::data_management::NumericTable * ptr = NULL;
assert(is_array(nda));
PyArrayObject * array = reinterpret_cast<PyArrayObject *>(nda);
if (array_numdims(array) == 2)
{
// the given numpy array is not well behaved C array but has right dimensionality
try
{
ptr = new NpyNumericTable<NpyNonContigHandler>(array);
}
catch (...)
{
ptr = NULL;
}
}
elseif (array_numdims(nda) == 1)
{
PyArray_Descr * descr = PyArray_DESCR(array);
if (PyDataType_NAMES(descr))
{
// the given array is a structured numpy array.
ptr = new NpyNumericTable<NpyStructHandler>(array);
}
else
{
throwstd::invalid_argument("Input array is neither well behaved and nor a structured array.\n");
}
}
else
{
throwstd::invalid_argument("Input array has wrong dimensionality (must be 2d).\n");
}
returndaal::data_management::NumericTablePtr(ptr);
}
// Try to convert given object to oneDAL Table without copying. Currently supports
// * numpy contiguous, homogeneous -> oneDAL HomogenNumericTable
// * numpy non-contiguous, homogeneous -> NpyNumericTable
// * numpy structured, heterogeneous -> NpyNumericTable
// * list of arrays, heterogen -> oneDAL SOANumericTable
// * scipy csr_matrix -> oneDAL CSRNumericTable
// As long as oneDAL CSR is only 0-based we need to copy indices/offsets
staticconstexprconstchar * numeric_table_capsule_name = "daal4py.NumericTablePtr";
staticvoiddelete_numeric_table_capsule(PyObject * capsule)
{
constchar * name = PyCapsule_GetName(capsule);
if (PyErr_Occurred())
{
PyErr_Clear();
return;
}
auto ptr = reinterpret_cast<daal::data_management::NumericTablePtr *>(PyCapsule_GetPointer(capsule, name));
if (ptr)
delete ptr;
else
PyErr_Clear();
}
daal::data_management::NumericTablePtr make_nt(PyObject * obj)
{
if (PyErr_Occurred())
{
PyErr_Print();
PyErr_Clear();
}
if (obj && obj != Py_None)
{
constint has_protocol = PyObject_HasAttrString(obj, "__2daalnt__");
if (has_protocol < 0) py_err_check();
if (has_protocol)
{
// __2daalnt__ transfers one heap-allocated NumericTablePtr through a
// typed capsule. Disable its destructor only after validation, then
// consume and delete the transferred pointer exactly once.
PyOwnedRef capsule(PyObject_CallMethod(obj, "__2daalnt__", NULL));
py_err_check();
if (!PyCapsule_CheckExact(capsule.get())) throwstd::invalid_argument("__2daalnt__ must return a capsule");
constchar * capsule_name = PyCapsule_GetName(capsule.get());
py_err_check();
if (capsule_name && std::strcmp(capsule_name, numeric_table_capsule_name) != 0) throwstd::invalid_argument("__2daalnt__ returned a capsule with an unsupported name");
// Keep accepting the legacy unnamed capsule while producers migrate
// to the typed protocol.
auto nt = reinterpret_cast<daal::data_management::NumericTablePtr *>(PyCapsule_GetPointer(capsule.get(), capsule_name));
py_err_check();
auto ntptr = *nt;
if (PyCapsule_SetDestructor(capsule.get(), nullptr) < 0) py_err_check();
delete nt;
return ntptr;
}
daal::data_management::NumericTablePtr ptr;
if (is_array(obj))
{ // we got a numpy array
PyArrayObject * ary = reinterpret_cast<PyArrayObject *>(obj);
if (array_is_behaved_C(ary))
{
#defineMAKENT_(_T) ptr = _make_hnt<_T>(obj)
SET_NPY_FEATURE(PyArray_DESCR(ary)->type, MAKENT_, throwstd::invalid_argument("Found unsupported array type"));
#undef MAKENT_
}
else
{
if (array_is_behaved_F(ary) && (PyArray_NDIM(ary) == 2))
{
int _axes = 0;
npy_intp N = PyArray_DIM(ary, 1); // number of columns
npy_intp column_len = PyArray_DIM(ary, 0);
int ary_numtype = PyArray_TYPE(ary);
/*
* Input is 2D F-contiguous array: represent it as SOA numeric table
*/
daal::data_management::SOANumericTablePtr soatbl;
// iterate over columns
PyObject * it = PyArray_IterAllButAxis(obj, &_axes);
if (it == NULL)
{
Py_XDECREF(it);
throwstd::runtime_error("Creating oneDAL SOA table from F-contigous NumPy array failed: iterator could not be created");
}
soatbl = daal::data_management::SOANumericTable::create(N, column_len);
for (npy_intp i = 0; PyArray_ITER_NOTDONE(it); ++i)
{
PyArrayObject * slice = reinterpret_cast<PyArrayObject *>(PyArray_SimpleNewFromData(1, &column_len, ary_numtype, static_cast<void *>(PyArray_ITER_DATA(it))));
if (!slice) throwstd::runtime_error("Creating NumPy column view failed");
Py_INCREF(ary);
if (PyArray_SetBaseObject(slice, reinterpret_cast<PyObject *>(ary)) < 0)
{
Py_DECREF(slice);
throwstd::runtime_error("Setting NumPy column owner failed");
}
#defineSETARRAY_(_T) \
{ \
daal::services::SharedPtr<_T> _tmp(reinterpret_cast<_T *>(PyArray_DATA(slice)), NumpyDeleter(slice)); \
soatbl->setArray(_tmp, i); \
}
SET_NPY_FEATURE(PyArray_DESCR(ary)->type, SETARRAY_, throwstd::invalid_argument("Found unsupported array type"));
#undef SETARRAY_
Py_DECREF(slice);
PyArray_ITER_NEXT(it);
}
Py_DECREF(it);
if (soatbl->getNumberOfColumns() != N)
{
throwstd::runtime_error("Creating oneDAL SOA table from F-contigous NumPy array failed.");
}
ptr = soatbl;
}
else
ptr = _make_npynt(obj);
}
if (!ptr) throwstd::runtime_error("Could not convert Python object to oneDAL table.\n");
}
elseif (PyList_Check(obj) && PyList_Size(obj) > 0)
{ // a list of arrays for SOA?
PyOwnedRef first(list_get_item_ref(obj, 0));
py_err_check();
if (is_array(first.get()))
{ // can handle only list of 1d arrays
auto N = PyList_Size(obj);
daal::data_management::SOANumericTablePtr soatbl;
for (auto i = 0; i < N; i++)
{
PyOwnedRef item(list_get_item_ref(obj, i));
py_err_check();
PyArrayObject * ary = reinterpret_cast<PyArrayObject *>(item.get());
if (i == 0) soatbl = daal::data_management::SOANumericTable::create(N, PyArray_DIM(ary, 0));
if (PyArray_NDIM(ary) != 1)
{
throwstd::runtime_error(std::string("Found wrong dimensionality (") + std::to_string(PyArray_NDIM(ary)) + ") of array in list when constructing SOA table (must be 1d)");
}
if (!array_is_behaved_C(ary))
{
throwstd::runtime_error(std::string("Cannot operate on column: ") + std::to_string(i) + " because it is non-contiguous. Please make it contiguous before passing it to daal4py\n");
}
#defineSETARRAY_(_T) \
{ \
daal::services::SharedPtr<_T> _tmp(reinterpret_cast<_T *>(PyArray_DATA(ary)), NumpyDeleter(ary)); \
soatbl->setArray(_tmp, i); \
}
SET_NPY_FEATURE(PyArray_DESCR(ary)->type, SETARRAY_, throwstd::invalid_argument("Found unsupported array type"));
#undef SETARRAY_
}
if (soatbl->getNumberOfColumns() != N)
{
throwstd::runtime_error("Creating oneDAL SOA table from list failed.");
}
ptr = soatbl;
} // else not a list of 1d arrays
} // else not a list of 1d arrays
if (!ptr && ((strcmp(Py_TYPE(obj)->tp_name, "csr_matrix") == 0) || (strcmp(Py_TYPE(obj)->tp_name, "csr_array") == 0)))
{
daal::services::SharedPtr<daal::data_management::CSRNumericTable> ret;
PyOwnedRef vals(PyObject_GetAttrString(obj, "data"));
py_err_check();
PyOwnedRef indcs(PyObject_GetAttrString(obj, "indices"));
py_err_check();
PyOwnedRef roffs(PyObject_GetAttrString(obj, "indptr"));
py_err_check();
PyOwnedRef shape(PyObject_GetAttrString(obj, "shape"));
py_err_check();
if (shape.get() && PyTuple_Check(shape.get()) && is_array(vals.get()) && is_array(indcs.get()) && is_array(roffs.get()) && array_numdims(vals.get()) == 1 && array_numdims(indcs.get()) == 1 && array_numdims(roffs.get()) == 1)
{
py_err_check();
// As long as oneDAL does not support 0-based indexing we have to copy the indices and add 1 to each
PyOwnedRef np_indcs(PyArray_FROMANY(indcs.get(), NPY_UINT64, 0, 0, NPY_ARRAY_CARRAY | NPY_ARRAY_ENSURECOPY | NPY_ARRAY_FORCECAST));
py_err_check();
PyOwnedRef np_roffs(PyArray_FROMANY(roffs.get(), NPY_UINT64, 0, 0, NPY_ARRAY_CARRAY | NPY_ARRAY_ENSURECOPY | NPY_ARRAY_FORCECAST));
py_err_check();
PyOwnedRef np_vals(PyArray_FROMANY(vals.get(), array_type(vals.get()), 0, 0, NPY_ARRAY_CARRAY));
py_err_check();
PyObject * nr = PyTuple_GetItem(shape.get(), 0);
py_err_check();
PyObject * nc = PyTuple_GetItem(shape.get(), 1);
py_err_check();
if (np_indcs.get() && np_roffs.get() && np_vals.get() && nr && nc)
{
size_t * c_indcs = static_cast<size_t *>(array_data(np_indcs.get()));
size_t n = array_size(np_indcs.get(), 0);
size_t * c_indcs_one_based = static_cast<size_t *>(daal::services::daal_malloc(n * sizeof(size_t)));
DAAL4PY_CHECK_MALLOC(c_indcs_one_based);
for (size_t i = 0; i < n; ++i) c_indcs_one_based[i] = c_indcs[i] + 1;
size_t * c_roffs = static_cast<size_t *>(array_data(np_roffs.get()));
n = array_size(np_roffs.get(), 0);
size_t * c_roffs_one_based = static_cast<size_t *>(daal::services::daal_malloc((n + 1) * sizeof(size_t)));
DAAL4PY_CHECK_MALLOC(c_roffs_one_based);
for (size_t i = 0; i < n; ++i) c_roffs_one_based[i] = c_roffs[i] + 1;
size_t c_nc = static_cast<size_t>(PyInt_AsSsize_t(nc));
py_err_check();
size_t c_nr = static_cast<size_t>(PyInt_AsSsize_t(nr));
py_err_check();
#defineMKCSR_(_T) ret = daal::data_management::CSRNumericTable::create(daal::services::SharedPtr<_T>(reinterpret_cast<_T *>(array_data(np_vals.get())), NumpyDeleter(reinterpret_cast<PyArrayObject *>(np_vals.get()))), daal::services::SharedPtr<size_t>(c_indcs_one_based, daal::services::ServiceDeleter()), daal::services::SharedPtr<size_t>(c_roffs_one_based, daal::services::ServiceDeleter()), c_nc, c_nr)
SET_NPY_FEATURE(array_type(np_vals.get()), MKCSR_, throwstd::invalid_argument(std::string("Found unsupported data type in ") + Py_TYPE(obj)->tp_name + "\n"));
#undef MKCSR_
}
else
throwstd::invalid_argument(std::string("Failed accessing csr data when converting ") + Py_TYPE(obj)->tp_name + "\n");
}
else
throwstd::invalid_argument("Got invalid csr_matrix or csr_array.\n");
returndaal::data_management::NumericTablePtr(ret);
}
return ptr;
}
returndaal::data_management::NumericTablePtr();
}
PyObject * make_nt_capsule_for_testing(PyObject * obj, bool legacy)
{
auto ptr = std::make_unique<daal::data_management::NumericTablePtr>(make_nt(obj));
PyObject * capsule = PyCapsule_New(ptr.get(), legacy ? nullptr : numeric_table_capsule_name, delete_numeric_table_capsule);
if (!capsule) throwstd::runtime_error("Creating NumericTablePtr capsule failed");
ptr.release();
return capsule;
}
PyObject * roundtrip_nt_for_testing(PyObject * obj)
{
auto ptr = make_nt(obj);
returnmake_nda(&ptr);
}
extern daal::data_management::KeyValueDataCollectionPtr make_dnt(PyObject * dict, str2i_map_t & str2id)
{
daal::data_management::KeyValueDataCollectionPtr dc(new daal::data_management::KeyValueDataCollection);
if (dict && dict != Py_None)
{
if (PyDict_Check(dict))
{
PyObject *key, *value;
Py_ssize_t pos = 0;
while (PyDict_Next(dict, &pos, &key, &value))
{
constchar * strkey = PyString_AsString(key);
auto keyid = str2id.find(strkey);
if (keyid != str2id.end())
{
daal::data_management::NumericTablePtr tbl = make_nt(value);
if (tbl)
{
(*dc)[keyid->second] = daal::services::staticPointerCast<daal::data_management::SerializationIface>(tbl);
}
else
{
throwstd::invalid_argument(std::string("Unexpected object '") + Py_TYPE(value)->tp_name + "' found in dict, expected an array\n");
}
}
else
{
throwstd::invalid_argument(std::string("Unexpected key '") + Py_TYPE(key)->tp_name + "' found in dict, expected a string\n");
}
}
}
else
{
throwstd::invalid_argument(std::string("Unexpected object '") + Py_TYPE(dict)->tp_name + "' found, expected dict\n");
}
}
return dc;
}
data_or_file::data_or_file(PyObject * input)
{
this->table.reset();
this->file.resize(0);
if (input == Py_None)
{
;
}
elseif (PyUnicode_Check(input))
{
// this->file = PyUnicode_AsUTF8AndSize(input, &size);
this->file = PyUnicode_AsUTF8(input);
}
else
{
auto tmp = make_nt(input);
if (tmp)
{
this->table = tmp;
}
if (!this->table)
{
throwstd::invalid_argument(std::string("Got type '") + Py_TYPE(input)->tp_name + "' when expecting string, array, or list of 1d-arrays.");
}
}
}
const daal::data_management::NumericTablePtr get_table(const data_or_file & t)
{
if (t.table) return t.table;
if (t.file.size()) returnreadCSV(t.file);
throwstd::invalid_argument("one and only one input per process allowed");
returndaal::data_management::NumericTablePtr();
}
const daal::data_management::NumericTablePtr readCSV(const std::string & fname)
{
daal::data_management::FileDataSource<daal::data_management::CSVFeatureManager> dataSource(fname, daal::data_management::DataSource::doAllocateNumericTable, daal::data_management::DataSource::doDictionaryFromContext);
dataSource.loadDataBlock();
return dataSource.getNumericTable();
}
extern"C"voidto_c_array(const daal::data_management::NumericTablePtr * ptr, void ** data, size_t * dims, char dtype)
{
*data = NULL;
if (ptr && ptr->get())
{
dims[0] = (*ptr)->getNumberOfRows();
dims[1] = (*ptr)->getNumberOfColumns();
switch (dtype)
{
case0: *data = get_nt_data_ptr<double>(ptr); break;
case1: *data = get_nt_data_ptr<float>(ptr); break;
case2: *data = get_nt_data_ptr<int>(ptr); break;
default: throwstd::invalid_argument("Invalid data type specified.");
}
if (*data) return;
throwstd::invalid_argument("Data type and table type are incompatible.");
}
// ptr==NULL: no input data
dims[0] = dims[1] = 0;
return;
}
daal::data_management::DataCollectionPtr make_datacoll(PyObject * input)
{
if (PyErr_Occurred())
{
PyErr_Print();
PyErr_Clear();
}
if (input && input != Py_None && PyList_Check(input) && PyList_Size(input) > 0)
{
auto n = PyList_Size(input);
daal::data_management::DataCollection * res = new daal::data_management::DataCollection;
res->resize(n);
for (auto i = 0; i < n; i++)
{
PyOwnedRef obj(list_get_item_ref(input, i));
py_err_check();
auto tmp = make_nt(obj.get());
if (tmp)
res->push_back(tmp);
else
throwstd::runtime_error(std::string("Unexpected object '") + Py_TYPE(obj.get())->tp_name + "' found in list, expected an array");
}
returndaal::data_management::DataCollectionPtr(res);
}
returndaal::data_management::DataCollectionPtr();
}
static std::int64_tgetval_(const std::string & str, conststr2i_map_t & strmap)
{
auto i = strmap.find(str);
if (i == strmap.end()) throwstd::invalid_argument(std::string("Encountered unexpected string-identifier '") + str + std::string("'"));
return i->second;
}
std::int64_tstring2enum(const std::string & str, str2i_map_t & strmap)
{
std::int64_t r = 0;
std::size_t current, previous = 0;
while ((current = str.find('|', previous)) != std::string::npos)
{
r |= getval_(str.substr(previous, current - previous), strmap);
previous = current + 1;
}
return (r | getval_(str.substr(previous, current - previous), strmap));
}
#ifdef _DIST_
#include"transceiver.h"
#endif
extern"C"
{
voidc_daalinit(int nthreads)
{
if (nthreads > 0) daal::services::Environment::getInstance()->setNumberOfThreads(nthreads);
}
voidc_daalfini()
{
#ifdef _DIST_
del_transceiver();
#endif
}
size_tc_num_threads()
{
returndaal::services::Environment::getInstance()->getNumberOfThreads();
}
size_tc_num_procs()
{
#ifdef _DIST_
returnget_transceiver()->nMembers();
#else
return1;
#endif
}
size_tc_my_procid()
{
#ifdef _DIST_
returnget_transceiver()->me();
#else
return0;
#endif
}
voidc_enable_thread_pinning(bool enabled)
{
daal::services::Environment::getInstance()->enableThreadPinning(enabled);
}
} // extern "C"
boolc_assert_all_finite(const data_or_file & t, bool allowNaN, char dtype)
{
bool result = false;
auto tab = get_table(t);
switch (dtype)
{
case0: result = daal::data_management::internal::allValuesAreFinite<double>(*tab, allowNaN); break;
case1: result = daal::data_management::internal::allValuesAreFinite<float>(*tab, allowNaN); break;
default: throwstd::invalid_argument("Invalid data type specified.");
}
return result;
}
voidc_train_test_split(data_or_file & orig, data_or_file & train, data_or_file & test, data_or_file & train_idx, data_or_file & test_idx)
{
auto origTable = get_table(orig);
auto trainTable = get_table(train);
auto testTable = get_table(test);
auto trainIdxTable = get_table(train_idx);
auto testIdxTable = get_table(test_idx);
daal::data_management::internal::trainTestSplit<int>(origTable, trainTable, testTable, trainIdxTable, testIdxTable);
}
doublec_roc_auc_score(data_or_file & y_true, data_or_file & y_test)
{
#if __INTEL_DAAL__ >= 2021 && INTEL_DAAL_VERSION >= 20210200
constsize_t col_true = y_true.table->getNumberOfColumns();
constsize_t row_true = y_true.table->getNumberOfRows();
constsize_t col_test = y_test.table->getNumberOfColumns();
constsize_t row_test = y_test.table->getNumberOfRows();
if (col_true != 1 || col_test != 1 || row_true != row_test)
{
PyErr_SetString(PyExc_RuntimeError, "Unknown shape data");
returnNULL;
}
auto table_true = get_table(y_true);
auto table_test = get_table(y_test);
auto type = (*table_test->getDictionary())[0].indexType;
if (type == daal::data_management::data_feature_utils::DAAL_FLOAT64 || type == daal::data_management::data_feature_utils::DAAL_INT64_S || type == daal::data_management::data_feature_utils::DAAL_INT64_U || type == daal::data_management::data_feature_utils::DAAL_FLOAT32 || type == daal::data_management::data_feature_utils::DAAL_INT32_S || type == daal::data_management::data_feature_utils::DAAL_INT32_U)
{
returndaal::data_management::internal::rocAucScore(table_true, table_test);
}
PyErr_SetString(PyExc_RuntimeError, "Unknown shape data");
return0.0;
#else
return -1.0;
#endif
}
voidc_generate_shuffled_indices(data_or_file & idx, data_or_file & random_state)
{
#if __INTEL_DAAL__ == 2020 && INTEL_DAAL_VERSION >= 20200003 || __INTEL_DAAL__ >= 2021
auto idxTable = get_table(idx);
auto randomStateTable = get_table(random_state);
daal::data_management::internal::generateShuffledIndices<int>(idxTable, randomStateTable);
#else
#endif
}
voidc_tsne_gradient_descent(data_or_file & init, data_or_file & p, data_or_file & size_iter, data_or_file & params, data_or_file & results, char dtype)
{
#if __INTEL_DAAL__ >= 2021 && INTEL_DAAL_VERSION >= 20210600
auto initTable = get_table(init);
auto pTable = get_table(p);
auto sizeIterTable = get_table(size_iter);
auto paramTable = get_table(params);
auto resultTable = get_table(results);
daal::data_management::CSRNumericTablePtr csrTable = daal::services::dynamicPointerCast<daal::data_management::CSRNumericTable, daal::data_management::NumericTable>(pTable);
if (csrTable)
{
switch (dtype)
{
case0: daal::algorithms::internal::tsneGradientDescent<int, double>(initTable, csrTable, sizeIterTable, paramTable, resultTable); break;
case1: daal::algorithms::internal::tsneGradientDescent<int, float>(initTable, csrTable, sizeIterTable, paramTable, resultTable); break;
default: throwstd::invalid_argument("Invalid data type specified.");
}
}
else
PyErr_SetString(PyExc_RuntimeError, "Unexpected table type");
#else
#endif
}