28
28
#include < dpnp_iface.hpp>
29
29
#include " dpnp_fptr.hpp"
30
30
#include " dpnp_utils.hpp"
31
+ #include " dpnpc_memory_adapter.hpp"
31
32
#include " queue_sycl.hpp"
32
33
33
34
namespace mkl_blas = oneapi::mkl::blas::row_major;
@@ -68,7 +69,8 @@ class dpnp_cov_c_kernel;
68
69
template <typename _DataType>
69
70
void dpnp_cov_c (void * array1_in, void * result1, size_t nrows, size_t ncols)
70
71
{
71
- _DataType* array_1 = reinterpret_cast <_DataType*>(array1_in);
72
+ DPNPC_ptr_adapter<_DataType> input1_ptr (array1_in, nrows * ncols);
73
+ _DataType* array_1 = input1_ptr.get_ptr ();
72
74
_DataType* result = reinterpret_cast <_DataType*>(result1);
73
75
74
76
if (!nrows || !ncols)
@@ -144,15 +146,23 @@ template <typename _DataType>
144
146
class dpnp_max_c_kernel ;
145
147
146
148
template <typename _DataType>
147
- void dpnp_max_c (void * array1_in, void * result1, const size_t * shape, size_t ndim, const size_t * axis, size_t naxis)
149
+ void dpnp_max_c (void * array1_in, void * result1, const size_t result_size, const size_t * shape, size_t ndim, const size_t * axis, size_t naxis)
148
150
{
151
+ const size_t size_input = std::accumulate (shape, shape + ndim, 1 , std::multiplies<size_t >());
152
+ if (!size_input)
153
+ {
154
+ return ;
155
+ }
156
+
157
+ DPNPC_ptr_adapter<_DataType> input1_ptr (array1_in, size_input, true );
158
+ DPNPC_ptr_adapter<_DataType> result_ptr (result1, result_size, true , true );
159
+ _DataType* array_1 = input1_ptr.get_ptr ();
160
+ _DataType* result = result_ptr.get_ptr ();
161
+
149
162
if (naxis == 0 )
150
163
{
151
164
__attribute__ ((unused)) void * tmp = (void *)(axis + naxis);
152
165
153
- _DataType* array_1 = reinterpret_cast <_DataType*>(array1_in);
154
- _DataType* result = reinterpret_cast <_DataType*>(result1);
155
-
156
166
size_t size = 1 ;
157
167
for (size_t i = 0 ; i < ndim; ++i)
158
168
{
@@ -182,9 +192,6 @@ void dpnp_max_c(void* array1_in, void* result1, const size_t* shape, size_t ndim
182
192
}
183
193
else
184
194
{
185
- _DataType* array_1 = reinterpret_cast <_DataType*>(array1_in);
186
- _DataType* result = reinterpret_cast <_DataType*>(result1);
187
-
188
195
size_t res_ndim = ndim - naxis;
189
196
size_t res_shape[res_ndim];
190
197
int ind = 0 ;
@@ -206,12 +213,6 @@ void dpnp_max_c(void* array1_in, void* result1, const size_t* shape, size_t ndim
206
213
}
207
214
}
208
215
209
- size_t size_input = 1 ;
210
- for (size_t i = 0 ; i < ndim; ++i)
211
- {
212
- size_input *= shape[i];
213
- }
214
-
215
216
size_t input_shape_offsets[ndim];
216
217
size_t acc = 1 ;
217
218
for (size_t i = ndim - 1 ; i > 0 ; --i)
@@ -338,24 +339,20 @@ void dpnp_mean_c(void* array1_in, void* result1, const size_t* shape, size_t ndi
338
339
{
339
340
__attribute__ ((unused)) void * tmp = (void *)(axis + naxis);
340
341
341
- _ResultType* result = reinterpret_cast <_ResultType*>(result1);
342
-
343
- size_t size = 1 ;
344
- for (size_t i = 0 ; i < ndim; ++i)
345
- {
346
- size *= shape[i];
347
- }
348
-
342
+ const size_t size = std::accumulate (shape, shape + ndim, 1 , std::multiplies<size_t >());
349
343
if (!size)
350
344
{
351
345
return ;
352
346
}
353
347
348
+ DPNPC_ptr_adapter<_DataType> input1_ptr (array1_in, size, true );
349
+ DPNPC_ptr_adapter<_ResultType> result_ptr (result1, 1 , true , true );
350
+ _DataType* array = input1_ptr.get_ptr ();
351
+ _ResultType* result = result_ptr.get_ptr ();
352
+
354
353
if constexpr (std::is_same<_DataType, double >::value || std::is_same<_DataType, float >::value)
355
354
{
356
- _ResultType* array = reinterpret_cast <_DataType*>(array1_in);
357
-
358
- auto dataset = mkl_stats::make_dataset<mkl_stats::layout::row_major>(1 , size, array);
355
+ auto dataset = mkl_stats::make_dataset<mkl_stats::layout::row_major /* , _ResultType*/ >(1 , size, array);
359
356
360
357
cl::sycl::event event = mkl_stats::mean (DPNP_QUEUE, dataset, result);
361
358
@@ -366,7 +363,7 @@ void dpnp_mean_c(void* array1_in, void* result1, const size_t* shape, size_t ndi
366
363
_ResultType* sum = reinterpret_cast <_ResultType*>(dpnp_memory_alloc_c (1 * sizeof (_ResultType)));
367
364
368
365
dpnp_sum_c<_ResultType, _DataType>(
369
- sum, array1_in , shape, ndim, reinterpret_cast <const long *>(axis), naxis, nullptr , nullptr );
366
+ sum, array , shape, ndim, reinterpret_cast <const long *>(axis), naxis, nullptr , nullptr );
370
367
371
368
result[0 ] = sum[0 ] / static_cast <_ResultType>(size);
372
369
@@ -381,14 +378,15 @@ void dpnp_median_c(void* array1_in, void* result1, const size_t* shape, size_t n
381
378
{
382
379
__attribute__ ((unused)) void * tmp = (void *)(axis + naxis);
383
380
384
- _ResultType* result = reinterpret_cast <_ResultType*>(result1);
385
-
386
- size_t size = 1 ;
387
- for (size_t i = 0 ; i < ndim; ++i)
381
+ const size_t size = std::accumulate (shape, shape + ndim, 1 , std::multiplies<size_t >());
382
+ if (!size)
388
383
{
389
- size *= shape[i] ;
384
+ return ;
390
385
}
391
386
387
+ DPNPC_ptr_adapter<_ResultType> result_ptr (result1, 1 , true , true );
388
+ _ResultType* result = result_ptr.get_ptr ();
389
+
392
390
_DataType* sorted = reinterpret_cast <_DataType*>(dpnp_memory_alloc_c (size * sizeof (_DataType)));
393
391
394
392
dpnp_sort_c<_DataType>(array1_in, sorted, size);
@@ -411,26 +409,29 @@ template <typename _DataType>
411
409
class dpnp_min_c_kernel ;
412
410
413
411
template <typename _DataType>
414
- void dpnp_min_c (void * array1_in, void * result1, const size_t * shape, size_t ndim, const size_t * axis, size_t naxis)
412
+ void dpnp_min_c (void * array1_in, void * result1, const size_t result_size, const size_t * shape, size_t ndim, const size_t * axis, size_t naxis)
415
413
{
416
- if (naxis == 0 )
414
+ __attribute__ ((unused)) void * tmp = (void *)(axis + naxis);
415
+
416
+ const size_t size_input = std::accumulate (shape, shape + ndim, 1 , std::multiplies<size_t >());
417
+ if (!size_input)
417
418
{
418
- __attribute__ ((unused)) void * tmp = (void *)(axis + naxis);
419
+ return ;
420
+ }
419
421
420
- _DataType* array_1 = reinterpret_cast <_DataType*>(array1_in);
421
- _DataType* result = reinterpret_cast <_DataType*>(result1);
422
+ DPNPC_ptr_adapter<_DataType> input1_ptr (array1_in, size_input, true );
423
+ DPNPC_ptr_adapter<_DataType> result_ptr (result1, result_size, true , true );
424
+ _DataType* array_1 = input1_ptr.get_ptr ();
425
+ _DataType* result = result_ptr.get_ptr ();
422
426
423
- size_t size = 1 ;
424
- for (size_t i = 0 ; i < ndim; ++i)
425
- {
426
- size *= shape[i];
427
- }
427
+ if (naxis == 0 )
428
+ {
428
429
if constexpr (std::is_same<_DataType, double >::value || std::is_same<_DataType, float >::value)
429
430
{
430
431
// Required initializing the result before call the function
431
432
result[0 ] = array_1[0 ];
432
433
433
- auto dataset = mkl_stats::make_dataset<mkl_stats::layout::row_major>(1 , size , array_1);
434
+ auto dataset = mkl_stats::make_dataset<mkl_stats::layout::row_major>(1 , size_input , array_1);
434
435
435
436
cl::sycl::event event = mkl_stats::min (DPNP_QUEUE, dataset, result);
436
437
@@ -440,17 +441,14 @@ void dpnp_min_c(void* array1_in, void* result1, const size_t* shape, size_t ndim
440
441
{
441
442
auto policy = oneapi::dpl::execution::make_device_policy<class dpnp_min_c_kernel <_DataType>>(DPNP_QUEUE);
442
443
443
- _DataType* res = std::min_element (policy, array_1, array_1 + size );
444
+ _DataType* res = std::min_element (policy, array_1, array_1 + size_input );
444
445
policy.queue ().wait ();
445
446
446
447
result[0 ] = *res;
447
448
}
448
449
}
449
450
else
450
451
{
451
- _DataType* array_1 = reinterpret_cast <_DataType*>(array1_in);
452
- _DataType* result = reinterpret_cast <_DataType*>(result1);
453
-
454
452
size_t res_ndim = ndim - naxis;
455
453
size_t res_shape[res_ndim];
456
454
int ind = 0 ;
@@ -472,12 +470,6 @@ void dpnp_min_c(void* array1_in, void* result1, const size_t* shape, size_t ndim
472
470
}
473
471
}
474
472
475
- size_t size_input = 1 ;
476
- for (size_t i = 0 ; i < ndim; ++i)
477
- {
478
- size_input *= shape[i];
479
- }
480
-
481
473
size_t input_shape_offsets[ndim];
482
474
size_t acc = 1 ;
483
475
for (size_t i = ndim - 1 ; i > 0 ; --i)
@@ -600,13 +592,9 @@ void dpnp_min_c(void* array1_in, void* result1, const size_t* shape, size_t ndim
600
592
}
601
593
602
594
template <typename _DataType>
603
- void dpnp_nanvar_c (void * array1_in, void * mask_arr1, void * result1, size_t arr_size)
595
+ void dpnp_nanvar_c (void * array1_in, void * mask_arr1, void * result1, const size_t result_size, size_t arr_size)
604
596
{
605
- _DataType* array1 = reinterpret_cast <_DataType*>(array1_in);
606
- bool * mask_arr = reinterpret_cast <bool *>(mask_arr1);
607
- _DataType* result = reinterpret_cast <_DataType*>(result1);
608
-
609
- if ((array1 == nullptr ) || (mask_arr == nullptr ) || (result == nullptr ))
597
+ if ((array1_in == nullptr ) || (mask_arr1 == nullptr ) || (result1 == nullptr ))
610
598
{
611
599
return ;
612
600
}
@@ -616,6 +604,13 @@ void dpnp_nanvar_c(void* array1_in, void* mask_arr1, void* result1, size_t arr_s
616
604
return ;
617
605
}
618
606
607
+ DPNPC_ptr_adapter<_DataType> input1_ptr (array1_in, arr_size, true );
608
+ DPNPC_ptr_adapter<bool > input2_ptr (mask_arr1, arr_size, true );
609
+ DPNPC_ptr_adapter<_DataType> result_ptr (result1, result_size, true , true );
610
+ _DataType* array1 = input1_ptr.get_ptr ();
611
+ bool * mask_arr = input2_ptr.get_ptr ();
612
+ _DataType* result = result_ptr.get_ptr ();
613
+
619
614
size_t ind = 0 ;
620
615
for (size_t i = 0 ; i < arr_size; ++i)
621
616
{
@@ -633,13 +628,10 @@ template <typename _DataType, typename _ResultType>
633
628
void dpnp_std_c (
634
629
void * array1_in, void * result1, const size_t * shape, size_t ndim, const size_t * axis, size_t naxis, size_t ddof)
635
630
{
636
- _DataType* array1 = reinterpret_cast <_DataType*>(array1_in);
637
- _ResultType* result = reinterpret_cast <_ResultType*>(result1);
638
-
639
631
_ResultType* var = reinterpret_cast <_ResultType*>(dpnp_memory_alloc_c (1 * sizeof (_ResultType)));
640
- dpnp_var_c<_DataType, _ResultType>(array1, var, shape, ndim, axis, naxis, ddof);
641
632
642
- dpnp_sqrt_c<_ResultType, _ResultType>(var, result, 1 );
633
+ dpnp_var_c<_DataType, _ResultType>(array1_in, var, shape, ndim, axis, naxis, ddof);
634
+ dpnp_sqrt_c<_ResultType, _ResultType>(var, result1, 1 );
643
635
644
636
dpnp_memory_free_c (var);
645
637
@@ -653,20 +645,22 @@ template <typename _DataType, typename _ResultType>
653
645
void dpnp_var_c (
654
646
void * array1_in, void * result1, const size_t * shape, size_t ndim, const size_t * axis, size_t naxis, size_t ddof)
655
647
{
648
+ const size_t size = std::accumulate (shape, shape + ndim, 1 , std::multiplies<size_t >());
649
+ if (!size)
650
+ {
651
+ return ;
652
+ }
653
+
656
654
cl::sycl::event event;
657
- _DataType* array1 = reinterpret_cast <_DataType*>(array1_in);
658
- _ResultType* result = reinterpret_cast <_ResultType*>(result1);
655
+ DPNPC_ptr_adapter<_DataType> input1_ptr (array1_in, size);
656
+ DPNPC_ptr_adapter<_ResultType> result_ptr (result1, 1 , true , true );
657
+ _DataType* array1 = input1_ptr.get_ptr ();
658
+ _ResultType* result = result_ptr.get_ptr ();
659
659
660
660
_ResultType* mean = reinterpret_cast <_ResultType*>(dpnp_memory_alloc_c (1 * sizeof (_ResultType)));
661
661
dpnp_mean_c<_DataType, _ResultType>(array1, mean, shape, ndim, axis, naxis);
662
662
_ResultType mean_val = mean[0 ];
663
663
664
- size_t size = 1 ;
665
- for (size_t i = 0 ; i < ndim; ++i)
666
- {
667
- size *= shape[i];
668
- }
669
-
670
664
_ResultType* squared_deviations = reinterpret_cast <_ResultType*>(dpnp_memory_alloc_c (size * sizeof (_ResultType)));
671
665
672
666
cl::sycl::range<1 > gws (size);
0 commit comments