Skip to content
This repository has been archived by the owner on Mar 21, 2024. It is now read-only.

Commit

Permalink
Move properties out of namespace mr and add forward_property helper
Browse files Browse the repository at this point in the history
  • Loading branch information
miscco committed Jan 11, 2023
1 parent 9cc136e commit f419473
Show file tree
Hide file tree
Showing 15 changed files with 195 additions and 96 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// SPDX-FileCopyrightText: Copyright (c) 2022 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: c++03, c++11

// cuda::forward_property
#include <cuda/std/cassert>
#include <cuda/memory_resource>

struct prop_with_value {
using value_type = int;
};
struct prop {};

template<class Upstream>
struct derived_plain : public cuda::forward_property<derived_plain<Upstream>, Upstream>
{
__host__ __device__ constexpr Upstream upstream_resource() const noexcept { return Upstream{}; }
};

struct upstream_with_valueless_property {
__host__ __device__ friend constexpr void get_property(const upstream_with_valueless_property&, prop) {}
};
static_assert( cuda::has_property<derived_plain<upstream_with_valueless_property>, prop>, "");
static_assert(!cuda::has_property<derived_plain<upstream_with_valueless_property>, prop_with_value>, "");

struct upstream_with_stateful_property {
__host__ __device__ friend constexpr int get_property(const upstream_with_stateful_property&, prop_with_value) {
return 42;
}
};
static_assert(!cuda::has_property<derived_plain<upstream_with_stateful_property>, prop>, "");
static_assert( cuda::has_property<derived_plain<upstream_with_stateful_property>, prop_with_value>, "");

struct upstream_with_both_properties {
__host__ __device__ friend constexpr void get_property(const upstream_with_both_properties&, prop) {}
__host__ __device__ friend constexpr int get_property(const upstream_with_both_properties&, prop_with_value) {
return 42;
}
};
static_assert( cuda::has_property<derived_plain<upstream_with_both_properties>, prop>, "");
static_assert( cuda::has_property<derived_plain<upstream_with_both_properties>, prop_with_value>, "");

struct derived_override : public cuda::forward_property<derived_override, upstream_with_both_properties>
{
__host__ __device__ constexpr upstream_with_both_properties upstream_resource() const noexcept {
return upstream_with_both_properties{};
}
__host__ __device__ friend constexpr int get_property(const derived_override&, prop_with_value) {
return 1337;
}
};

__host__ __device__ constexpr bool test_stateful() {
using derived_no_override = derived_plain<upstream_with_stateful_property>;
const derived_no_override without_override{};
assert(get_property(without_override, prop_with_value{}) == 42);

const derived_override with_override{};
assert(get_property(with_override, prop_with_value{}) == 1337);

return true;
}

int main(int, char**) {
test_stateful();
static_assert(test_stateful(), "");
return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,36 @@

// UNSUPPORTED: c++03, c++11

// cuda::mr::has_property, cuda::mr::has_property_with
// cuda::has_property, cuda::has_property_with
#include <cuda/memory_resource>

struct prop_with_value {
using value_type = int;
};
struct prop {};

static_assert(cuda::mr::property_with_value<prop_with_value>);
static_assert(!cuda::mr::property_with_value<prop>);
static_assert(cuda::property_with_value<prop_with_value>);
static_assert(!cuda::property_with_value<prop>);

struct valid_property {
friend void get_property(const valid_property&, prop) {}
};
static_assert(!cuda::mr::has_property<valid_property, prop_with_value>, "");
static_assert(cuda::mr::has_property<valid_property, prop>, "");
static_assert(!cuda::mr::has_property_with<valid_property, prop, int>, "");
static_assert(!cuda::has_property<valid_property, prop_with_value>, "");
static_assert(cuda::has_property<valid_property, prop>, "");
static_assert(!cuda::has_property_with<valid_property, prop, int>, "");

struct valid_property_with_value {
friend int get_property(const valid_property_with_value&, prop_with_value) {
return 42;
}
};
static_assert(
cuda::mr::has_property<valid_property_with_value, prop_with_value>, "");
static_assert(!cuda::mr::has_property<valid_property_with_value, prop>, "");
static_assert(cuda::mr::has_property_with<valid_property_with_value,
cuda::has_property<valid_property_with_value, prop_with_value>, "");
static_assert(!cuda::has_property<valid_property_with_value, prop>, "");
static_assert(cuda::has_property_with<valid_property_with_value,
prop_with_value, int>,
"");
static_assert(!cuda::mr::has_property_with<valid_property_with_value,
static_assert(!cuda::has_property_with<valid_property_with_value,
prop_with_value, double>,
"");

Expand All @@ -47,13 +47,13 @@ struct derived_from_property : public valid_property {
return 42;
}
};
static_assert(cuda::mr::has_property<derived_from_property, prop_with_value>,
static_assert(cuda::has_property<derived_from_property, prop_with_value>,
"");
static_assert(cuda::mr::has_property<derived_from_property, prop>, "");
static_assert(cuda::has_property<derived_from_property, prop>, "");
static_assert(
cuda::mr::has_property_with<derived_from_property, prop_with_value, int>,
cuda::has_property_with<derived_from_property, prop_with_value, int>,
"");
static_assert(!cuda::mr::has_property_with<derived_from_property,
static_assert(!cuda::has_property_with<derived_from_property,
prop_with_value, double>,
"");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ struct async_resource {
int _val = 0;

_LIBCUDACXX_TEMPLATE(class Property)
(requires !cuda::mr::property_with_value<Property> &&
(requires !cuda::property_with_value<Property> &&
_CUDA_VSTD::_One_of<Property, Properties...>) //
friend void get_property(const async_resource&, Property) noexcept {}

_LIBCUDACXX_TEMPLATE(class Property)
(requires cuda::mr::property_with_value<Property>&&
(requires cuda::property_with_value<Property>&&
_CUDA_VSTD::_One_of<Property, Properties...>) //
friend typename Property::value_type
get_property(const async_resource& res, Property) noexcept {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ struct resource {
int _val = 0;

_LIBCUDACXX_TEMPLATE(class Property)
(requires !cuda::mr::property_with_value<Property> &&
(requires !cuda::property_with_value<Property> &&
_CUDA_VSTD::_One_of<Property, Properties...>) //
friend void get_property(const resource&, Property) noexcept {}

_LIBCUDACXX_TEMPLATE(class Property)
(requires cuda::mr::property_with_value<Property>&&
(requires cuda::property_with_value<Property>&&
_CUDA_VSTD::_One_of<Property, Properties...>) //
friend typename Property::value_type
get_property(const resource& res, Property) noexcept {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ struct async_resource {
int _val = 0;

_LIBCUDACXX_TEMPLATE(class Property)
(requires !cuda::mr::property_with_value<Property> &&
(requires !cuda::property_with_value<Property> &&
_CUDA_VSTD::_One_of<Property, Properties...>) //
friend void get_property(const async_resource&, Property) noexcept {}

_LIBCUDACXX_TEMPLATE(class Property)
(requires cuda::mr::property_with_value<Property>&&
(requires cuda::property_with_value<Property>&&
_CUDA_VSTD::_One_of<Property, Properties...>) //
friend typename Property::value_type
get_property(const async_resource& res, Property) noexcept {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ struct async_resource {
int _val = 0;

_LIBCUDACXX_TEMPLATE(class Property)
(requires !cuda::mr::property_with_value<Property> &&
(requires !cuda::property_with_value<Property> &&
_CUDA_VSTD::_One_of<Property, Properties...>) //
friend void get_property(const async_resource&, Property) noexcept {}

_LIBCUDACXX_TEMPLATE(class Property)
(requires cuda::mr::property_with_value<Property>&&
(requires cuda::property_with_value<Property>&&
_CUDA_VSTD::_One_of<Property, Properties...>) //
friend typename Property::value_type
get_property(const async_resource& res, Property) noexcept {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ struct async_resource_base {
bool operator!=(const async_resource_base& other) const { return false; }

_LIBCUDACXX_TEMPLATE(class Property)
(requires !cuda::mr::property_with_value<Property> &&
(requires !cuda::property_with_value<Property> &&
_CUDA_VSTD::_One_of<Property, Properties...>) //
friend void get_property(const async_resource_base&, Property) noexcept {}

_LIBCUDACXX_TEMPLATE(class Property)
(requires cuda::mr::property_with_value<Property>&&
(requires cuda::property_with_value<Property>&&
_CUDA_VSTD::_One_of<Property, Properties...>) //
friend typename Property::value_type
get_property(const async_resource_base& res, Property) noexcept {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ template <class T>
struct property_without_value {};

namespace properties_test {
static_assert(cuda::mr::property_with_value<property_with_value<int> >, "");
static_assert(cuda::property_with_value<property_with_value<int> >, "");
static_assert(
cuda::mr::property_with_value<property_with_value<struct someStruct> >, "");
cuda::property_with_value<property_with_value<struct someStruct> >, "");

static_assert(!cuda::mr::property_with_value<property_without_value<int> >, "");
static_assert(!cuda::property_with_value<property_without_value<int> >, "");
static_assert(
!cuda::mr::property_with_value<property_without_value<struct otherStruct> >,
!cuda::property_with_value<property_without_value<struct otherStruct> >,
"");
} // namespace properties_test

Expand All @@ -56,12 +56,12 @@ struct async_resource {
int _val = 0;

_LIBCUDACXX_TEMPLATE(class Property)
(requires !cuda::mr::property_with_value<Property> &&
(requires !cuda::property_with_value<Property> &&
_CUDA_VSTD::_One_of<Property, Properties...>) //
friend void get_property(const async_resource&, Property) noexcept {}

_LIBCUDACXX_TEMPLATE(class Property)
(requires cuda::mr::property_with_value<Property>&&
(requires cuda::property_with_value<Property>&&
_CUDA_VSTD::_One_of<Property, Properties...>) //
friend typename Property::value_type
get_property(const async_resource& res, Property) noexcept {
Expand All @@ -86,25 +86,25 @@ static_assert(
(2 * sizeof(void*)));

_LIBCUDACXX_TEMPLATE(class Property, class Ref)
(requires !cuda::mr::property_with_value<Property>) //
(requires !cuda::property_with_value<Property>) //
int InvokeIfWithValue(const Ref& ref) {
return -1;
}

_LIBCUDACXX_TEMPLATE(class Property, class Ref)
(requires cuda::mr::property_with_value<Property>) //
(requires cuda::property_with_value<Property>) //
typename Property::value_type InvokeIfWithValue(const Ref& ref) {
return get_property(ref, Property{});
}

_LIBCUDACXX_TEMPLATE(class Property, class Ref)
(requires cuda::mr::property_with_value<Property>) //
(requires cuda::property_with_value<Property>) //
int InvokeIfWithoutValue(const Ref& ref) {
return -1;
}

_LIBCUDACXX_TEMPLATE(class Property, class Ref)
(requires !cuda::mr::property_with_value<Property>) //
(requires !cuda::property_with_value<Property>) //
int InvokeIfWithoutValue(const Ref& ref) {
get_property(ref, Property{});
return 1;
Expand All @@ -119,7 +119,7 @@ void test_async_resource_ref() {
// Check all the potentially stateful properties
const int properties_with_value[] = {InvokeIfWithValue<Properties>(ref)...};
const int expected_with_value[] = {
((cuda::mr::property_with_value<Properties>) ? expected_initially
((cuda::property_with_value<Properties>) ? expected_initially
: -1)...};
for (std::size_t i = 0; i < sizeof...(Properties); ++i) {
assert(properties_with_value[i] == expected_with_value[i]);
Expand All @@ -128,7 +128,7 @@ void test_async_resource_ref() {
const int properties_without_value[] = {
InvokeIfWithoutValue<Properties>(ref)...};
const int expected_without_value[] = {
((cuda::mr::property_with_value<Properties>) ? -1 : 1)...};
((cuda::property_with_value<Properties>) ? -1 : 1)...};
for (std::size_t i = 0; i < sizeof...(Properties); ++i) {
assert(properties_without_value[i] == expected_without_value[i]);
}
Expand All @@ -139,7 +139,7 @@ void test_async_resource_ref() {
// Check whether we truly get the right value
const int properties_with_value2[] = {InvokeIfWithValue<Properties>(ref)...};
const int expected_with_value2[] = {
((cuda::mr::property_with_value<Properties>) ? expected_after_change
((cuda::property_with_value<Properties>) ? expected_after_change
: -1)...};
for (std::size_t i = 0; i < sizeof...(Properties); ++i) {
assert(properties_with_value2[i] == expected_with_value2[i]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ struct resource {
int _val = 0;

_LIBCUDACXX_TEMPLATE(class Property)
(requires !cuda::mr::property_with_value<Property> &&
(requires !cuda::property_with_value<Property> &&
_CUDA_VSTD::_One_of<Property, Properties...>) //
friend void get_property(const resource&, Property) noexcept {}

_LIBCUDACXX_TEMPLATE(class Property)
(requires cuda::mr::property_with_value<Property>&&
(requires cuda::property_with_value<Property>&&
_CUDA_VSTD::_One_of<Property, Properties...>) //
friend typename Property::value_type
get_property(const resource& res, Property) noexcept {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ struct resource {
int _val = 0;

_LIBCUDACXX_TEMPLATE(class Property)
(requires !cuda::mr::property_with_value<Property> &&
(requires !cuda::property_with_value<Property> &&
_CUDA_VSTD::_One_of<Property, Properties...>) //
friend void get_property(const resource&, Property) noexcept {}

_LIBCUDACXX_TEMPLATE(class Property)
(requires cuda::mr::property_with_value<Property>&&
(requires cuda::property_with_value<Property>&&
_CUDA_VSTD::_One_of<Property, Properties...>) //
friend typename Property::value_type
get_property(const resource& res, Property) noexcept {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ struct resource {
int _val = 0;

_LIBCUDACXX_TEMPLATE(class Property)
(requires !cuda::mr::property_with_value<Property> &&
(requires !cuda::property_with_value<Property> &&
_CUDA_VSTD::_One_of<Property, Properties...>) //
friend void get_property(const resource&, Property) noexcept {}

_LIBCUDACXX_TEMPLATE(class Property)
(requires cuda::mr::property_with_value<Property>&&
(requires cuda::property_with_value<Property>&&
_CUDA_VSTD::_One_of<Property, Properties...>) //
friend typename Property::value_type
get_property(const resource& res, Property) noexcept {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ struct resource {
int _val = 0;

_LIBCUDACXX_TEMPLATE(class Property)
(requires !cuda::mr::property_with_value<Property> &&
(requires !cuda::property_with_value<Property> &&
_CUDA_VSTD::_One_of<Property, Properties...>) //
friend void get_property(const resource&, Property) noexcept {}

_LIBCUDACXX_TEMPLATE(class Property)
(requires cuda::mr::property_with_value<Property>&&
(requires cuda::property_with_value<Property>&&
_CUDA_VSTD::_One_of<Property, Properties...>) //
friend typename Property::value_type
get_property(const resource& res, Property) noexcept {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ struct resource_base {
bool operator!=(const resource_base& other) const { return false; }

_LIBCUDACXX_TEMPLATE(class Property)
(requires !cuda::mr::property_with_value<Property> &&
(requires !cuda::property_with_value<Property> &&
_CUDA_VSTD::_One_of<Property, Properties...>) //
friend void get_property(const resource_base&, Property) noexcept {}

_LIBCUDACXX_TEMPLATE(class Property)
(requires cuda::mr::property_with_value<Property>&&
(requires cuda::property_with_value<Property>&&
_CUDA_VSTD::_One_of<Property, Properties...>) //
friend typename Property::value_type
get_property(const resource_base& res, Property) noexcept {
Expand Down
Loading

0 comments on commit f419473

Please sign in to comment.