|
| 1 | +//***************************************************************************** |
| 2 | +// Copyright (c) 2016-2020, Intel Corporation |
| 3 | +// All rights reserved. |
| 4 | +// |
| 5 | +// Redistribution and use in source and binary forms, with or without |
| 6 | +// modification, are permitted provided that the following conditions are met: |
| 7 | +// - Redistributions of source code must retain the above copyright notice, |
| 8 | +// this list of conditions and the following disclaimer. |
| 9 | +// - Redistributions in binary form must reproduce the above copyright notice, |
| 10 | +// this list of conditions and the following disclaimer in the documentation |
| 11 | +// and/or other materials provided with the distribution. |
| 12 | +// |
| 13 | +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 14 | +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 | +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 16 | +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 17 | +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 18 | +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 19 | +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 20 | +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 21 | +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 22 | +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 23 | +// THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | +//***************************************************************************** |
| 25 | + |
| 26 | +#include <cmath> |
| 27 | +#include <iostream> |
| 28 | +#include <mkl_blas_sycl.hpp> |
| 29 | +#include <vector> |
| 30 | + |
| 31 | +#include <backend_iface.hpp> |
| 32 | +#include "backend_utils.hpp" |
| 33 | +#include "queue_sycl.hpp" |
| 34 | + |
| 35 | +template <typename _KernelNameSpecialization> |
| 36 | +class custom_elemwise_absolute_c_kernel; |
| 37 | + |
| 38 | +template <typename _DataType> |
| 39 | +void custom_elemwise_absolute_c(void* array1_in, |
| 40 | + const std::vector<long>& input_shape, |
| 41 | + void* result1, |
| 42 | + size_t size) |
| 43 | +{ |
| 44 | + if (!size) |
| 45 | + { |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + cl::sycl::event event; |
| 50 | + _DataType* array1 = reinterpret_cast<_DataType*>(array1_in); |
| 51 | + _DataType* result = reinterpret_cast<_DataType*>(result1); |
| 52 | + |
| 53 | + const size_t input_shape_size = input_shape.size(); |
| 54 | + size_t* input_offset_shape = reinterpret_cast<size_t*>(dpnp_memory_alloc_c(input_shape_size * sizeof(long))); |
| 55 | + size_t* result_offset_shape = reinterpret_cast<size_t*>(dpnp_memory_alloc_c(input_shape_size * sizeof(long))); |
| 56 | + |
| 57 | + cl::sycl::range<1> gws(size); |
| 58 | + event = DPNP_QUEUE.submit([&](cl::sycl::handler& cgh) { |
| 59 | + cgh.parallel_for<class custom_elemwise_absolute_c_kernel<_DataType> >( |
| 60 | + gws, |
| 61 | + [=](cl::sycl::id<1> global_id) |
| 62 | + { |
| 63 | + const size_t idx = global_id[0]; |
| 64 | + |
| 65 | + if (array1[idx] >= 0) { |
| 66 | + result[idx] = array1[idx]; |
| 67 | + } |
| 68 | + else{ |
| 69 | + result[idx] = -1 * array1[idx]; |
| 70 | + } |
| 71 | + |
| 72 | + }); // parallel_for |
| 73 | + }); // queue.submit |
| 74 | + |
| 75 | + event.wait(); |
| 76 | + |
| 77 | + free(input_offset_shape, DPNP_QUEUE); |
| 78 | + free(result_offset_shape, DPNP_QUEUE); |
| 79 | + |
| 80 | +} |
| 81 | + |
| 82 | +template void custom_elemwise_absolute_c<double>(void* array1_in, |
| 83 | + const std::vector<long>& input_shape, |
| 84 | + void* result1, |
| 85 | + size_t size); |
| 86 | +template void custom_elemwise_absolute_c<float>(void* array1_in, |
| 87 | + const std::vector<long>& input_shape, |
| 88 | + void* result1, |
| 89 | + size_t size); |
| 90 | +template void custom_elemwise_absolute_c<long>(void* array1_in, |
| 91 | + const std::vector<long>& input_shape, |
| 92 | + void* result1, |
| 93 | + size_t size); |
| 94 | +template void custom_elemwise_absolute_c<int>(void* array1_in, |
| 95 | + const std::vector<long>& input_shape, |
| 96 | + void* result1, |
| 97 | + size_t size); |
0 commit comments