-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathdpctl_vector_templ.cpp
More file actions
158 lines (152 loc) · 4.75 KB
/
dpctl_vector_templ.cpp
File metadata and controls
158 lines (152 loc) · 4.75 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
//===-- dpctl_vector_templ.cpp - Wrapper functions for opaque vector types ===//
//
// Data Parallel Control (dpctl)
//
// Copyright 2020-2025 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.
//
//===----------------------------------------------------------------------===//
///
/// \file
/// This file is meant to be directly included inside other source files to add
/// the wrapper functions for vector operations.
///
//===----------------------------------------------------------------------===//
#include "Support/MemOwnershipAttrs.h"
#include "dpctl_error_handlers.h"
#include "dpctl_sycl_type_casters.hpp"
#include "dpctl_vector_macros.h"
#include <stddef.h>
#include <type_traits>
#include <vector>
/*!
* @brief Creates a new std::vector of the opaque SYCL pointer types.
*
* @return A new dynamically allocated std::vector of opaque pointer types.
*/
__dpctl_give VECTOR(EL) FN(EL, Create)()
{
using vecTy = std::vector<SYCLREF(EL)>;
vecTy *Vec = nullptr;
try {
Vec = new std::vector<SYCLREF(EL)>();
return ::dpctl::syclinterface::wrap<vecTy>(Vec);
} catch (std::exception const &e) {
delete Vec;
error_handler(e, __FILE__, __func__, __LINE__);
return nullptr;
}
}
/*!
* @brief Creates a new std::vector of the opaque SYCL pointer types from given
* C array with deep copy.
*
* @return A new dynamically allocated std::vector of opaque pointer types.
*/
__dpctl_give VECTOR(EL)
FN(EL, CreateFromArray)(size_t n, __dpctl_keep SYCLREF(EL) * elems)
{
using vecTy = std::vector<SYCLREF(EL)>;
vecTy *Vec = nullptr;
try {
Vec = new vecTy();
for (size_t i = 0; i < n; ++i) {
auto Ref = ::dpctl::syclinterface::unwrap<EL_SYCL_TYPE>(elems[i]);
Vec->emplace_back(::dpctl::syclinterface::wrap<EL_SYCL_TYPE>(
new EL_SYCL_TYPE(*Ref)));
}
return ::dpctl::syclinterface::wrap<vecTy>(Vec);
} catch (std::exception const &e) {
delete Vec;
error_handler(e, __FILE__, __func__, __LINE__);
return nullptr;
}
}
/*!
* @brief Frees all the elements of the passed in std::vector and then frees the
* std::vector pointer.
*
*/
void FN(EL, Delete)(__dpctl_take VECTOR(EL) VRef)
{
using vecTy = std::vector<SYCLREF(EL)>;
auto Vec = ::dpctl::syclinterface::unwrap<vecTy>(VRef);
if (Vec) {
for (auto i = 0ul; i < Vec->size(); ++i) {
auto D = ::dpctl::syclinterface::unwrap<EL_SYCL_TYPE>((*Vec)[i]);
delete D;
}
}
delete Vec;
}
/*!
* @brief Frees all the elements of the vector and then calls clear().
*
*/
void FN(EL, Clear)(__dpctl_keep VECTOR(EL) VRef)
{
using vecTy = std::vector<SYCLREF(EL)>;
auto Vec = ::dpctl::syclinterface::unwrap<vecTy>(VRef);
if (Vec) {
for (auto i = 0ul; i < Vec->size(); ++i) {
auto D = ::dpctl::syclinterface::unwrap<EL_SYCL_TYPE>((*Vec)[i]);
delete D;
}
Vec->clear();
}
}
/*!
* @brief Returns the number of elements in the vector.
*
*/
size_t FN(EL, Size)(__dpctl_keep VECTOR(EL) VRef)
{
using vecTy = std::vector<SYCLREF(EL)>;
auto V = ::dpctl::syclinterface::unwrap<vecTy>(VRef);
if (V)
return V->size();
else
return 0;
}
/*!
* @brief Returns a copy of the opaque pointer at specified index, and throws
* an out_of_range exception if the index is incorrect.
*
*/
SYCLREF(EL) FN(EL, GetAt)(__dpctl_keep VECTOR(EL) VRef, size_t index)
{
using vecTy = std::vector<SYCLREF(EL)>;
auto Vec = ::dpctl::syclinterface::unwrap<vecTy>(VRef);
SYCLREF(EL) copy = nullptr;
if (Vec) {
SYCLREF(EL) ret;
try {
ret = Vec->at(index);
} catch (std::exception const &e) {
error_handler(e, __FILE__, __func__, __LINE__);
return nullptr;
}
auto Ref = ::dpctl::syclinterface::unwrap<EL_SYCL_TYPE>(ret);
EL_SYCL_TYPE *elPtr = nullptr;
try {
elPtr = new EL_SYCL_TYPE(*Ref);
copy = ::dpctl::syclinterface::wrap<EL_SYCL_TYPE>(elPtr);
} catch (std::exception const &e) {
delete elPtr;
error_handler(e, __FILE__, __func__, __LINE__);
return nullptr;
}
}
return copy;
}