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

Commit

Permalink
Implement concepts for <memory_resource>
Browse files Browse the repository at this point in the history
This includes:
* cuda::mr::has_property
* cuda::mr::has_property_with
* cuda::mr::resource
* cuda::mr::resource_with
* cuda::mr::async_resource
* cuda::mr::async_resource_with
  • Loading branch information
miscco committed Sep 14, 2022
1 parent d479363 commit 2d22f33
Show file tree
Hide file tree
Showing 6 changed files with 619 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
//===----------------------------------------------------------------------===//
//
// 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::mr::async_resource
#include <cuda/memory_resource>

#include <cuda/std/cstdint>

#include "test_macros.h"

struct invalid_argument {};

struct valid_resource {
void* allocate(std::size_t, std::size_t) { return nullptr; }
void deallocate(void*, std::size_t, std::size_t) {}
void* allocate_async(std::size_t, std::size_t, cuda::stream_ref) {
return nullptr;
}
void deallocate_async(void*, std::size_t, std::size_t, cuda::stream_ref) {}
bool operator==(const valid_resource&) const { return true; }
bool operator!=(const valid_resource&) const { return false; }
};
static_assert(cuda::mr::async_resource<valid_resource>, "");

struct invalid_allocate_missing {
void deallocate(void*, std::size_t, std::size_t) {}
void* allocate_async(std::size_t, std::size_t, cuda::stream_ref) {
return nullptr;
}
void deallocate_async(void*, std::size_t, std::size_t, cuda::stream_ref) {}
bool operator==(const invalid_allocate_missing&) const { return true; }
bool operator!=(const invalid_allocate_missing&) const { return false; }
};
static_assert(!cuda::mr::async_resource<invalid_allocate_missing>, "");

struct invalid_deallocate_missing {
void* allocate(std::size_t, std::size_t) { return nullptr; }
void* allocate_async(std::size_t, std::size_t, cuda::stream_ref) {
return nullptr;
}
void deallocate_async(void*, std::size_t, std::size_t, cuda::stream_ref) {}
bool operator==(const invalid_allocate_missing&) const { return true; }
bool operator!=(const invalid_allocate_missing&) const { return false; }
};
static_assert(!cuda::mr::async_resource<invalid_deallocate_missing>, "");

struct invalid_allocate_async_argument {
void* allocate(std::size_t, std::size_t) { return nullptr; }
void deallocate(void*, std::size_t, std::size_t) {}
void* allocate_async(invalid_argument, std::size_t) { return nullptr; }
void deallocate_async(void*, std::size_t, std::size_t, cuda::stream_ref) {}
bool operator==(const invalid_allocate_async_argument&) const { return true; }
bool operator!=(const invalid_allocate_async_argument&) const { return false; }
};
static_assert(!cuda::mr::async_resource<invalid_allocate_async_argument>, "");

struct invalid_allocate_async_return {
void* allocate(std::size_t, std::size_t) { return nullptr; }
void deallocate(void*, std::size_t, std::size_t) {}
int allocate_async(std::size_t, std::size_t, cuda::stream_ref) { return 42; }
void deallocate_async(void*, std::size_t, std::size_t, cuda::stream_ref) {}
bool operator==(const invalid_allocate_async_return&) const { return true; }
bool operator!=(const invalid_allocate_async_return&) const { return false; }
};
static_assert(!cuda::mr::async_resource<invalid_allocate_async_return>, "");

struct invalid_deallocate_async_argument {
void* allocate(std::size_t, std::size_t) { return nullptr; }
void deallocate(void*, std::size_t, std::size_t) {}
void* allocate_async(std::size_t, std::size_t, cuda::stream_ref) {
return nullptr;
}
void deallocate_async(void*, invalid_argument, std::size_t) {}
bool operator==(const invalid_deallocate_async_argument&) const { return true; }
bool operator!=(const invalid_deallocate_async_argument&) const { return false; }
};
static_assert(!cuda::mr::async_resource<invalid_deallocate_async_argument>, "");

struct non_comparable {
void* allocate(std::size_t, std::size_t) { return nullptr; }
void deallocate(void*, std::size_t, std::size_t) {}
void* allocate_async(std::size_t, std::size_t, cuda::stream_ref) {
return nullptr;
}
void deallocate_async(void*, std::size_t, std::size_t, cuda::stream_ref) {}
};
static_assert(!cuda::mr::async_resource<non_comparable>, "");

struct non_eq_comparable {
void* allocate(std::size_t, std::size_t) { return nullptr; }
void deallocate(void*, std::size_t, std::size_t) {}
void* allocate_async(std::size_t, std::size_t, cuda::stream_ref) {
return nullptr;
}
void deallocate_async(void*, std::size_t, std::size_t, cuda::stream_ref) {}
bool operator!=(const non_eq_comparable&) const { return false; }
};
static_assert(!cuda::mr::async_resource<non_eq_comparable>, "");

#if TEST_STD_VER < 20
struct non_neq_comparable {
void* allocate(std::size_t, std::size_t) { return nullptr; }
void deallocate(void*, std::size_t, std::size_t) {}
void* allocate_async(std::size_t, std::size_t, cuda::stream_ref) {
return nullptr;
}
void deallocate_async(void*, std::size_t, std::size_t, cuda::stream_ref) {}
bool operator==(const non_neq_comparable&) const { return true; }
};
static_assert(!cuda::mr::async_resource<non_neq_comparable>, "");
#endif // TEST_STD_VER < 20

int main(int, char**) { return 0; }
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
//===----------------------------------------------------------------------===//
//
// 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::mr::async_resource_with
#include <cuda/memory_resource>

#include <cuda/std/cstdint>

struct prop_with_value {};
struct prop {};

struct valid_resource_with_property {
void* allocate(std::size_t, std::size_t) { return nullptr; }
void deallocate(void*, std::size_t, std::size_t) {}
void* allocate_async(std::size_t, std::size_t, cuda::stream_ref) {
return nullptr;
}
void deallocate_async(void*, std::size_t, std::size_t, cuda::stream_ref) {}
bool operator==(const valid_resource_with_property&) const { return true; }
bool operator!=(const valid_resource_with_property&) const { return false; }
friend void get_property(const valid_resource_with_property&,
prop_with_value) {}
};
static_assert(cuda::mr::async_resource_with<valid_resource_with_property,
prop_with_value>,
"");

struct valid_resource_without_property {
void* allocate(std::size_t, std::size_t) { return nullptr; }
void deallocate(void*, std::size_t, std::size_t) {}
void* allocate_async(std::size_t, std::size_t, cuda::stream_ref) {
return nullptr;
}
void deallocate_async(void*, std::size_t, std::size_t, cuda::stream_ref) {}
bool operator==(const valid_resource_without_property&) const { return true; }
bool operator!=(const valid_resource_without_property&) const { return false; }
};
static_assert(!cuda::mr::async_resource_with<valid_resource_without_property,
prop_with_value>,
"");

struct invalid_resource_with_property {
friend void get_property(const invalid_resource_with_property&,
prop_with_value) {}
};
static_assert(!cuda::mr::async_resource_with<invalid_resource_with_property,
prop_with_value>,
"");

struct resource_with_many_properties {
void* allocate(std::size_t, std::size_t) { return nullptr; }
void deallocate(void*, std::size_t, std::size_t) {}
void* allocate_async(std::size_t, std::size_t, cuda::stream_ref) {
return nullptr;
}
void deallocate_async(void*, std::size_t, std::size_t, cuda::stream_ref) {}
bool operator==(const resource_with_many_properties&) const { return true; }
bool operator!=(const resource_with_many_properties&) const { return false; }
friend void get_property(const resource_with_many_properties&,
prop_with_value) {}
friend void get_property(const resource_with_many_properties&, prop) {}
};
static_assert(cuda::mr::async_resource_with<resource_with_many_properties,
prop_with_value, prop>,
"");
static_assert(!cuda::mr::async_resource_with<resource_with_many_properties,
prop_with_value, int, prop>,
"");

struct derived_with_property : public valid_resource_without_property {
friend void get_property(const derived_with_property&, prop_with_value) {}
};
static_assert(
cuda::mr::async_resource_with<derived_with_property, prop_with_value>, "");

int main(int, char**) { return 0; }
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//===----------------------------------------------------------------------===//
//
// 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::mr::has_property, cuda::mr::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>);

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>, "");

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,
prop_with_value, int>,
"");
static_assert(!cuda::mr::has_property_with<valid_property_with_value,
prop_with_value, double>,
"");

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

int main(int, char**) { return 0; }
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::mr::resource
#include <cuda/memory_resource>

#include <cuda/std/cstdint>

#include "test_macros.h"

struct invalid_argument {};

struct valid_resource {
void* allocate(std::size_t, std::size_t) { return nullptr; }
void deallocate(void*, std::size_t, std::size_t) {}
bool operator==(const valid_resource&) const { return true; }
bool operator!=(const valid_resource&) const { return false; }
};
static_assert(cuda::mr::resource<valid_resource>, "");

struct invalid_allocate_argument {
void* allocate(invalid_argument, std::size_t) { return nullptr; }
void deallocate(void*, std::size_t, std::size_t) {}
bool operator==(const invalid_allocate_argument&) { return true; }
bool operator!=(const invalid_allocate_argument&) { return false; }
};
static_assert(!cuda::mr::resource<invalid_allocate_argument>, "");

struct invalid_allocate_return {
int allocate(std::size_t, std::size_t) { return 42; }
void deallocate(void*, std::size_t, std::size_t) {}
bool operator==(const invalid_allocate_return&) { return true; }
bool operator!=(const invalid_allocate_return&) { return false; }
};
static_assert(!cuda::mr::resource<invalid_allocate_return>, "");

struct invalid_deallocate_argument {
void* allocate(std::size_t, std::size_t) { return nullptr; }
void deallocate(void*, invalid_argument, std::size_t) {}
bool operator==(const invalid_deallocate_argument&) { return true; }
bool operator!=(const invalid_deallocate_argument&) { return false; }
};
static_assert(!cuda::mr::resource<invalid_deallocate_argument>, "");

struct non_comparable {
void* allocate(std::size_t, std::size_t) { return nullptr; }
void deallocate(void*, std::size_t, std::size_t) {}
};
static_assert(!cuda::mr::resource<non_comparable>, "");

struct non_eq_comparable {
void* allocate(std::size_t, std::size_t) { return nullptr; }
void deallocate(void*, std::size_t, std::size_t) {}
bool operator!=(const non_eq_comparable&) { return false; }
};
static_assert(!cuda::mr::resource<non_eq_comparable>, "");

#if TEST_STD_VER < 20
struct non_neq_comparable {
void* allocate(std::size_t, std::size_t) { return nullptr; }
void deallocate(void*, std::size_t, std::size_t) {}
bool operator==(const non_neq_comparable&) { return true; }
};
static_assert(!cuda::mr::resource<non_neq_comparable>, "");
#endif // TEST_STD_VER <20

int main(int, char**) { return 0; }
Loading

0 comments on commit 2d22f33

Please sign in to comment.