From dde1c6243ee808704fc965d9a08f1488d129f8d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Zientkiewicz?= Date: Tue, 27 Apr 2021 20:20:12 +0200 Subject: [PATCH] Add a test checking that the interface of the resource pointer is properly propagated to the resource view via operator->. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: MichaƂ Zientkiewicz --- ...source_view_interface_propagation.pass.cpp | 199 ++++++++++++++++++ 1 file changed, 199 insertions(+) create mode 100644 .upstream-tests/test/cuda/memory_resource/resource_view/basic_resource_view_interface_propagation.pass.cpp diff --git a/.upstream-tests/test/cuda/memory_resource/resource_view/basic_resource_view_interface_propagation.pass.cpp b/.upstream-tests/test/cuda/memory_resource/resource_view/basic_resource_view_interface_propagation.pass.cpp new file mode 100644 index 0000000000..8dc781a7cd --- /dev/null +++ b/.upstream-tests/test/cuda/memory_resource/resource_view/basic_resource_view_interface_propagation.pass.cpp @@ -0,0 +1,199 @@ +//===----------------------------------------------------------------------===// +// +// Part of libcu++, the C++ Standard Library for your entire system, +// 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 +// +//===----------------------------------------------------------------------===// + + +#include +#include +#include +#include +#include +#include +#include +#include +#include "resource_hierarchy.h" + + +class sync_resource : public cuda::memory_resource { +public: + int extra_sync() { + return 42; + } + + size_t allocated_size = 0, allocated_alignment = 0; + void *allocated_pointer = nullptr; + mutable const cuda::memory_resource *compared_resource = nullptr; +private: + void *do_allocate(size_t size, size_t alignment) override { + allocated_size = size; + allocated_alignment = alignment; + return allocated_pointer = reinterpret_cast(0x123400); + } + + void do_deallocate(void *mem, size_t size, size_t alignment) { + assert(mem == allocated_pointer); + assert(size == allocated_size); + assert(alignment == allocated_alignment); + allocated_pointer = 0; + allocated_size = 0; + allocated_alignment = 0; + } + + bool do_is_equal(const cuda::memory_resource &other) const noexcept override { + compared_resource = &other; + return this == &other; + } +}; + +class async_resource : public cuda::stream_ordered_memory_resource { +public: + int extra_async() { + return 42; + } + + size_t allocated_size = 0, allocated_alignment = 0; + void *allocated_pointer = nullptr; + cuda::stream_view allocation_stream = {}; + mutable const cuda::memory_resource *compared_resource = nullptr; +private: + void *do_allocate(size_t size, size_t alignment) override { + allocated_size = size; + allocated_alignment = alignment; + return allocated_pointer = reinterpret_cast(0x123400); + } + + void *do_allocate_async(size_t size, size_t alignment, cuda::stream_view stream) override { + allocated_size = size; + allocated_alignment = alignment; + allocation_stream = stream; + return allocated_pointer = reinterpret_cast(0x123400); + } + + void do_deallocate(void *mem, size_t size, size_t alignment) { + assert(mem == allocated_pointer); + assert(size == allocated_size); + assert(alignment == allocated_alignment); + allocated_pointer = 0; + allocated_size = 0; + allocated_alignment = 0; + } + + void do_deallocate_async(void *mem, size_t size, size_t alignment, cuda::stream_view stream) override { + assert(mem == allocated_pointer); + assert(size == allocated_size); + assert(stream == allocation_stream); + assert(alignment == allocated_alignment); + assert(stream == allocation_stream); + allocated_pointer = 0; + allocated_size = 0; + allocated_alignment = 0; + allocation_stream = {}; + } + + bool do_is_equal(const cuda::memory_resource &other) const noexcept override { + compared_resource = &other; + return this == &other; + } +}; + +int main(int argc, char **argv) { +#ifndef __CUDA_ARCH__ + // syncrhonous resource + { + sync_resource rsrc; + auto view = cuda::view_resource(&rsrc); + assert(view->extra_sync() == 42); + void *ptr = view->allocate(23, 32); + assert(ptr == view->allocated_pointer); + assert(23 == view->allocated_size); + assert(32 == view->allocated_alignment); + view->deallocate(ptr, 23, 32); + assert(nullptr == view->allocated_pointer); + assert(0 == view->allocated_size); + assert(0 == view->allocated_alignment); + + assert(view->is_equal(rsrc)); + assert(view->compared_resource == &rsrc); + } + { + sync_resource rsrc; + cuda::resource_view view = &rsrc; + void *ptr = view->allocate(23, 32); + assert(ptr == rsrc.allocated_pointer); + assert(23 == rsrc.allocated_size); + assert(32 == rsrc.allocated_alignment); + view->deallocate(ptr, 23, 32); + assert(nullptr == rsrc.allocated_pointer); + assert(0 == rsrc.allocated_size); + assert(0 == rsrc.allocated_alignment); + } + // stream-ordered resource + { + cuda::stream_view stream((cudaStream_t)0x1234); + async_resource rsrc; + auto view = cuda::view_resource(&rsrc); + + assert(view->extra_async() == 42); + + void *ptr = view->allocate(23, 32); + assert(ptr == view->allocated_pointer); + assert(23 == view->allocated_size); + assert(32 == view->allocated_alignment); + + view->deallocate(ptr, 23, 32); + assert(nullptr == view->allocated_pointer); + assert(0 == view->allocated_size); + assert(0 == view->allocated_alignment); + assert(0 == view->allocated_alignment); + + ptr = view->allocate_async(42, 64, stream); + assert(ptr == view->allocated_pointer); + assert(42 == view->allocated_size); + assert(64 == view->allocated_alignment); + assert(stream.get() == view->allocation_stream.get()); + + view->deallocate_async(ptr, 42, 64, stream); + assert(nullptr == view->allocated_pointer); + assert(0 == view->allocated_size); + assert(0 == view->allocated_alignment); + assert(0 == view->allocation_stream.get()); + + assert(view->is_equal(rsrc)); + assert(view->compared_resource == &rsrc); + } + { + cuda::stream_view stream((cudaStream_t)0x1234); + async_resource rsrc; + cuda::stream_ordered_resource_view view = &rsrc; + + void *ptr = view->allocate(23, 32); + assert(ptr == rsrc.allocated_pointer); + assert(23 == rsrc.allocated_size); + assert(32 == rsrc.allocated_alignment); + + view->deallocate(ptr, 23, 32); + assert(nullptr == rsrc.allocated_pointer); + assert(0 == rsrc.allocated_size); + assert(0 == rsrc.allocated_alignment); + assert(0 == rsrc.allocated_alignment); + + ptr = view->allocate_async(42, 64, stream); + assert(ptr == rsrc.allocated_pointer); + assert(42 == rsrc.allocated_size); + assert(64 == rsrc.allocated_alignment); + assert(stream.get() == rsrc.allocation_stream.get()); + + view->deallocate_async(ptr, 42, 64, stream); + assert(nullptr == rsrc.allocated_pointer); + assert(0 == rsrc.allocated_size); + assert(0 == rsrc.allocated_alignment); + assert(0 == rsrc.allocation_stream.get()); + } +#endif + return 0; +}