Skip to content

Commit 9c6760a

Browse files
authored
Add a set of macros to help create opaque vector classes for opaque SYCL types. (#297)
1 parent 8d47967 commit 9c6760a

File tree

5 files changed

+218
-0
lines changed

5 files changed

+218
-0
lines changed

dpctl-capi/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ endif()
6464
file(GLOB_RECURSE sources
6565
${CMAKE_CURRENT_SOURCE_DIR}/source/*.cpp
6666
)
67+
68+
# Exclude from sources
69+
list(REMOVE_ITEM
70+
sources
71+
"${CMAKE_CURRENT_SOURCE_DIR}/source/dpctl_vector_templ.cpp"
72+
)
73+
6774
file(GLOB_RECURSE helper_sources
6875
${CMAKE_CURRENT_SOURCE_DIR}/helper/source/*.cpp
6976
)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//=== dpctl_vector_macros.h - Macros to help build function sig. -*-C++-*- //
2+
//
3+
// Data Parallel Control (dpCtl)
4+
//
5+
// Copyright 2020-2021 Intel Corporation
6+
//
7+
// Licensed under the Apache License, Version 2.0 (the "License");
8+
// you may not use this file except in compliance with the License.
9+
// You may obtain a copy of the License at
10+
//
11+
// http://www.apache.org/licenses/LICENSE-2.0
12+
//
13+
// Unless required by applicable law or agreed to in writing, software
14+
// distributed under the License is distributed on an "AS IS" BASIS,
15+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
// See the License for the specific language governing permissions and
17+
// limitations under the License.
18+
//
19+
//===----------------------------------------------------------------------===//
20+
///
21+
/// \file These macros are used in dpctl_vector_templ.cpp. They help build the
22+
/// function signatures for the functions defined in dpctl_vector_templ.cpp.
23+
///
24+
//===----------------------------------------------------------------------===//
25+
26+
#pragma once
27+
28+
#define xFN(TYPE, NAME) DPCTL##TYPE##Vector##_##NAME
29+
#define FN(TYPE, NAME) xFN(TYPE, NAME)
30+
#define xVECTOR(EL) DPCTL##EL##VectorRef
31+
#define VECTOR(EL) xVECTOR(EL)
32+
#define xSYCLREF(EL) DPCTLSycl##EL##Ref
33+
#define SYCLREF(EL) xSYCLREF(EL)

dpctl-capi/include/dpctl_vector.h

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
//===-- dpctl_vector.h - Defines macros for opaque vector types -*-C++-*- =//
2+
//
3+
// Data Parallel Control (dpCtl)
4+
//
5+
// Copyright 2020-2021 Intel Corporation
6+
//
7+
// Licensed under the Apache License, Version 2.0 (the "License");
8+
// you may not use this file except in compliance with the License.
9+
// You may obtain a copy of the License at
10+
//
11+
// http://www.apache.org/licenses/LICENSE-2.0
12+
//
13+
// Unless required by applicable law or agreed to in writing, software
14+
// distributed under the License is distributed on an "AS IS" BASIS,
15+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
// See the License for the specific language governing permissions and
17+
// limitations under the License.
18+
//
19+
//===----------------------------------------------------------------------===//
20+
///
21+
/// \file
22+
/// A set of helper macros are defined here to create opaque lists (implemented
23+
/// using std::vector) and helper functions of any DPCTL type.
24+
///
25+
//===----------------------------------------------------------------------===//
26+
27+
#pragma once
28+
29+
#include "Support/DllExport.h"
30+
#include "Support/ExternC.h"
31+
#include "Support/MemOwnershipAttrs.h"
32+
#include "dpctl_data_types.h"
33+
34+
DPCTL_C_EXTERN_C_BEGIN
35+
36+
#define DPCTL_DECLARE_VECTOR_TYPE(EL) \
37+
typedef struct DPCTL##EL##Vector *DPCTL##EL##VectorRef;
38+
39+
#define DPCTL_DECLARE_VECTOR_FN(EL) \
40+
DPCTL_API \
41+
__dpctl_give DPCTL##EL##VectorRef DPCTL##EL##Vector_Create(); \
42+
\
43+
DPCTL_API \
44+
void DPCTL##EL##Vector_Delete(__dpctl_take DPCTL##EL##VectorRef Ref); \
45+
\
46+
DPCTL_API \
47+
void DPCTL##EL##Vector_Clear(__dpctl_keep DPCTL##EL##VectorRef Ref); \
48+
\
49+
DPCTL_API \
50+
size_t DPCTL##EL##Vector_Size(__dpctl_keep DPCTL##EL##VectorRef Ref); \
51+
\
52+
DPCTL_API \
53+
__dpctl_give DPCTLSycl##EL##Ref DPCTL##EL##Vector_GetAt( \
54+
__dpctl_keep DPCTL##EL##VectorRef Ref, size_t index);
55+
56+
#define DPCTL_DECLARE_VECTOR(EL) \
57+
DPCTL_DECLARE_VECTOR_TYPE(EL) \
58+
DPCTL_DECLARE_VECTOR_FN(EL)
59+
60+
DPCTL_C_EXTERN_C_END
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
//===-- dpctl_vector_templ.cpp - Wrapper functions for opaque vector types ===//
2+
//
3+
// Data Parallel Control (dpCtl)
4+
//
5+
// Copyright 2020-2021 Intel Corporation
6+
//
7+
// Licensed under the Apache License, Version 2.0 (the "License");
8+
// you may not use this file except in compliance with the License.
9+
// You may obtain a copy of the License at
10+
//
11+
// http://www.apache.org/licenses/LICENSE-2.0
12+
//
13+
// Unless required by applicable law or agreed to in writing, software
14+
// distributed under the License is distributed on an "AS IS" BASIS,
15+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
// See the License for the specific language governing permissions and
17+
// limitations under the License.
18+
//
19+
//===----------------------------------------------------------------------===//
20+
///
21+
/// \file
22+
/// This file is meant to be directly included inside other source files to add
23+
/// the wrapper functions for vector operations.
24+
///
25+
//===----------------------------------------------------------------------===//
26+
#include "../helper/include/dpctl_vector_macros.h"
27+
#include "Support/MemOwnershipAttrs.h"
28+
#include <type_traits>
29+
30+
namespace
31+
{
32+
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(vector_class<SYCLREF(EL)>, VECTOR(EL))
33+
}
34+
35+
/*!
36+
* @brief Creates a new std::vector of the opaque SYCL pointer types.
37+
*
38+
* @return A new dynamically allocated std::vector of opaque pointer types.
39+
*/
40+
__dpctl_give VECTOR(EL) FN(EL, Create)()
41+
{
42+
try {
43+
auto Vec = new vector_class<SYCLREF(EL)>();
44+
return wrap(Vec);
45+
} catch (std::bad_alloc const &ba) {
46+
return nullptr;
47+
}
48+
}
49+
50+
/*!
51+
* @brief Frees all the elements of the passed in std::vector and then frees the
52+
* std::vector pointer.
53+
*
54+
*/
55+
void FN(EL, Delete)(__dpctl_take VECTOR(EL) VRef)
56+
{
57+
auto Vec = unwrap(VRef);
58+
59+
for (auto i = 0ul; i < Vec->size(); ++i) {
60+
auto D = unwrap((*Vec)[i]);
61+
delete D;
62+
}
63+
delete Vec;
64+
}
65+
66+
/*!
67+
* @brief Frees all the elements of the vector and then calls clear().
68+
*
69+
*/
70+
void FN(EL, Clear)(__dpctl_keep VECTOR(EL) VRef)
71+
{
72+
auto Vec = unwrap(VRef);
73+
74+
for (auto i = 0ul; i < Vec->size(); ++i) {
75+
auto D = unwrap((*Vec)[i]);
76+
delete D;
77+
}
78+
Vec->clear();
79+
}
80+
81+
/*!
82+
* @brief Returns the number of elements in the vector.
83+
*
84+
*/
85+
size_t FN(EL, Size)(__dpctl_keep VECTOR(EL) VRef)
86+
{
87+
return unwrap(VRef)->size();
88+
}
89+
90+
/*!
91+
* @brief Returns a copy of the opaque pointer at specified index, and throws
92+
* an out_of_range exception if the index is incorrect.
93+
*
94+
*/
95+
SYCLREF(EL) FN(EL, GetAt)(__dpctl_keep VECTOR(EL) VRef, size_t index)
96+
{
97+
auto Vec = unwrap(VRef);
98+
SYCLREF(EL) ret, copy = nullptr;
99+
try {
100+
ret = Vec->at(index);
101+
auto Ref = unwrap(ret);
102+
copy = wrap(new std::remove_pointer<decltype(Ref)>::type(*Ref));
103+
} catch (std::out_of_range const &oor) {
104+
std::cerr << oor.what() << '\n';
105+
} catch (std::bad_alloc const &ba) {
106+
// \todo log error
107+
std::cerr << ba.what() << '\n';
108+
return nullptr;
109+
}
110+
111+
return copy;
112+
}

dpctl-capi/tests/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ if(DPCTL_GENERATE_COVERAGE)
2222
file(GLOB_RECURSE sources ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp)
2323
file(GLOB_RECURSE dpctl_sources ${CMAKE_CURRENT_SOURCE_DIR}/../source/*.cpp)
2424

25+
# Exclude from sources
26+
list(REMOVE_ITEM
27+
dpctl_sources
28+
"${CMAKE_CURRENT_SOURCE_DIR}/../source/dpctl_vector_templ.cpp"
29+
)
30+
2531
# Add profiling flags
2632
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-instr-generate -fcoverage-mapping")
2733

0 commit comments

Comments
 (0)