Skip to content

A set of macros to help create opaque vector classes out of SYCL types #297

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions dpctl-capi/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ endif()
file(GLOB_RECURSE sources
${CMAKE_CURRENT_SOURCE_DIR}/source/*.cpp
)

# Exclude from sources
list(REMOVE_ITEM
sources
"${CMAKE_CURRENT_SOURCE_DIR}/source/dpctl_vector_templ.cpp"
)

file(GLOB_RECURSE helper_sources
${CMAKE_CURRENT_SOURCE_DIR}/helper/source/*.cpp
)
Expand Down
33 changes: 33 additions & 0 deletions dpctl-capi/helper/include/dpctl_vector_macros.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//=== dpctl_vector_macros.h - Macros to help build function sig. -*-C++-*- //
//
// Data Parallel Control (dpCtl)
//
// Copyright 2020-2021 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 These macros are used in dpctl_vector_templ.cpp. They help build the
/// function signatures for the functions defined in dpctl_vector_templ.cpp.
///
//===----------------------------------------------------------------------===//

#pragma once

#define xFN(TYPE, NAME) DPCTL##TYPE##Vector##_##NAME
#define FN(TYPE, NAME) xFN(TYPE, NAME)
#define xVECTOR(EL) DPCTL##EL##VectorRef
#define VECTOR(EL) xVECTOR(EL)
#define xSYCLREF(EL) DPCTLSycl##EL##Ref
#define SYCLREF(EL) xSYCLREF(EL)
60 changes: 60 additions & 0 deletions dpctl-capi/include/dpctl_vector.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//===-- dpctl_vector.h - Defines macros for opaque vector types -*-C++-*- =//
//
// Data Parallel Control (dpCtl)
//
// Copyright 2020-2021 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
/// A set of helper macros are defined here to create opaque lists (implemented
/// using std::vector) and helper functions of any DPCTL type.
///
//===----------------------------------------------------------------------===//

#pragma once

#include "Support/DllExport.h"
#include "Support/ExternC.h"
#include "Support/MemOwnershipAttrs.h"
#include "dpctl_data_types.h"

DPCTL_C_EXTERN_C_BEGIN

#define DPCTL_DECLARE_VECTOR_TYPE(EL) \
typedef struct DPCTL##EL##Vector *DPCTL##EL##VectorRef;

#define DPCTL_DECLARE_VECTOR_FN(EL) \
DPCTL_API \
__dpctl_give DPCTL##EL##VectorRef DPCTL##EL##Vector_Create(); \
\
DPCTL_API \
void DPCTL##EL##Vector_Delete(__dpctl_take DPCTL##EL##VectorRef Ref); \
\
DPCTL_API \
void DPCTL##EL##Vector_Clear(__dpctl_keep DPCTL##EL##VectorRef Ref); \
\
DPCTL_API \
size_t DPCTL##EL##Vector_Size(__dpctl_keep DPCTL##EL##VectorRef Ref); \
\
DPCTL_API \
__dpctl_give DPCTLSycl##EL##Ref DPCTL##EL##Vector_GetAt( \
__dpctl_keep DPCTL##EL##VectorRef Ref, size_t index);

#define DPCTL_DECLARE_VECTOR(EL) \
DPCTL_DECLARE_VECTOR_TYPE(EL) \
DPCTL_DECLARE_VECTOR_FN(EL)

DPCTL_C_EXTERN_C_END
112 changes: 112 additions & 0 deletions dpctl-capi/source/dpctl_vector_templ.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
//===-- dpctl_vector_templ.cpp - Wrapper functions for opaque vector types ===//
//
// Data Parallel Control (dpCtl)
//
// Copyright 2020-2021 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 "../helper/include/dpctl_vector_macros.h"
#include "Support/MemOwnershipAttrs.h"
#include <type_traits>

namespace
{
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(vector_class<SYCLREF(EL)>, VECTOR(EL))
}

/*!
* @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)()
{
try {
auto Vec = new vector_class<SYCLREF(EL)>();
return wrap(Vec);
} catch (std::bad_alloc const &ba) {
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)
{
auto Vec = unwrap(VRef);

for (auto i = 0ul; i < Vec->size(); ++i) {
auto D = unwrap((*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)
{
auto Vec = unwrap(VRef);

for (auto i = 0ul; i < Vec->size(); ++i) {
auto D = unwrap((*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)
{
return unwrap(VRef)->size();
}

/*!
* @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)
{
auto Vec = unwrap(VRef);
SYCLREF(EL) ret, copy = nullptr;
try {
ret = Vec->at(index);
auto Ref = unwrap(ret);
copy = wrap(new std::remove_pointer<decltype(Ref)>::type(*Ref));
} catch (std::out_of_range const &oor) {
std::cerr << oor.what() << '\n';
} catch (std::bad_alloc const &ba) {
// \todo log error
std::cerr << ba.what() << '\n';
return nullptr;
}

return copy;
}
6 changes: 6 additions & 0 deletions dpctl-capi/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ if(DPCTL_GENERATE_COVERAGE)
file(GLOB_RECURSE sources ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp)
file(GLOB_RECURSE dpctl_sources ${CMAKE_CURRENT_SOURCE_DIR}/../source/*.cpp)

# Exclude from sources
list(REMOVE_ITEM
dpctl_sources
"${CMAKE_CURRENT_SOURCE_DIR}/../source/dpctl_vector_templ.cpp"
)

# Add profiling flags
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-instr-generate -fcoverage-mapping")

Expand Down