From 4413510122c5bee6f9f2979bfe99d6da617ab751 Mon Sep 17 00:00:00 2001 From: Diptorup Deb Date: Fri, 26 Feb 2021 13:58:33 -0600 Subject: [PATCH] Add a set of macros to help create opaque vector classes for opaque SYCL types. --- dpctl-capi/CMakeLists.txt | 7 ++ .../helper/include/dpctl_vector_macros.h | 33 ++++++ dpctl-capi/include/dpctl_vector.h | 60 ++++++++++ dpctl-capi/source/dpctl_vector_templ.cpp | 112 ++++++++++++++++++ dpctl-capi/tests/CMakeLists.txt | 6 + 5 files changed, 218 insertions(+) create mode 100644 dpctl-capi/helper/include/dpctl_vector_macros.h create mode 100644 dpctl-capi/include/dpctl_vector.h create mode 100644 dpctl-capi/source/dpctl_vector_templ.cpp diff --git a/dpctl-capi/CMakeLists.txt b/dpctl-capi/CMakeLists.txt index 93cae6ab11..2d3dec6287 100644 --- a/dpctl-capi/CMakeLists.txt +++ b/dpctl-capi/CMakeLists.txt @@ -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 ) diff --git a/dpctl-capi/helper/include/dpctl_vector_macros.h b/dpctl-capi/helper/include/dpctl_vector_macros.h new file mode 100644 index 0000000000..b8702a5965 --- /dev/null +++ b/dpctl-capi/helper/include/dpctl_vector_macros.h @@ -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) diff --git a/dpctl-capi/include/dpctl_vector.h b/dpctl-capi/include/dpctl_vector.h new file mode 100644 index 0000000000..590ccdf387 --- /dev/null +++ b/dpctl-capi/include/dpctl_vector.h @@ -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 diff --git a/dpctl-capi/source/dpctl_vector_templ.cpp b/dpctl-capi/source/dpctl_vector_templ.cpp new file mode 100644 index 0000000000..1a83769871 --- /dev/null +++ b/dpctl-capi/source/dpctl_vector_templ.cpp @@ -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 + +namespace +{ +DEFINE_SIMPLE_CONVERSION_FUNCTIONS(vector_class, 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(); + 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::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; +} diff --git a/dpctl-capi/tests/CMakeLists.txt b/dpctl-capi/tests/CMakeLists.txt index 87dd681743..68d3db10bd 100644 --- a/dpctl-capi/tests/CMakeLists.txt +++ b/dpctl-capi/tests/CMakeLists.txt @@ -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")