Skip to content

Commit

Permalink
Factor out mr test utilities.
Browse files Browse the repository at this point in the history
  • Loading branch information
harrism committed Jan 18, 2024
1 parent fa140ae commit aafa18a
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 54 deletions.
28 changes: 1 addition & 27 deletions tests/mr/device/mr_ref_test.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#pragma once

#include "../../byte_literals.hpp"
#include "test_utils.hpp"

#include <rmm/aligned.hpp>
#include <rmm/cuda_device.hpp>
Expand All @@ -35,8 +36,6 @@

#include <gtest/gtest.h>

#include <cuda_runtime_api.h>

#include <cuda/memory_resource>

#include <cstddef>
Expand All @@ -50,31 +49,6 @@ using async_resource_ref = cuda::mr::async_resource_ref<cuda::mr::device_accessi

namespace rmm::test {

/**
* @brief Returns if a pointer points to a device memory or managed memory
* allocation.
*/
inline bool is_device_accessible_memory(void* ptr)
{
cudaPointerAttributes attributes{};
if (cudaSuccess != cudaPointerGetAttributes(&attributes, ptr)) { return false; }
return (attributes.type == cudaMemoryTypeDevice) or (attributes.type == cudaMemoryTypeManaged) or
((attributes.type == cudaMemoryTypeHost) and (attributes.devicePointer != nullptr));
}

inline bool is_host_memory(void* ptr)
{
cudaPointerAttributes attributes{};
if (cudaSuccess != cudaPointerGetAttributes(&attributes, ptr)) { return false; }
return attributes.type == cudaMemoryTypeHost;
}

inline bool is_properly_aligned(void* ptr)
{
if (is_host_memory(ptr)) { return rmm::is_pointer_aligned(ptr, rmm::RMM_DEFAULT_HOST_ALIGNMENT); }
return rmm::is_pointer_aligned(ptr, rmm::CUDA_ALLOCATION_ALIGNMENT);
}

enum size_in_bytes : size_t {};

constexpr auto default_num_allocations{100};
Expand Down
28 changes: 1 addition & 27 deletions tests/mr/device/mr_test.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#pragma once

#include "../../byte_literals.hpp"
#include "test_utils.hpp"

#include <rmm/aligned.hpp>
#include <rmm/cuda_device.hpp>
Expand All @@ -36,8 +37,6 @@

#include <gtest/gtest.h>

#include <cuda_runtime_api.h>

#include <cstddef>
#include <cstdint>
#include <functional>
Expand All @@ -46,31 +45,6 @@

namespace rmm::test {

/**
* @brief Returns if a pointer points to a device memory or managed memory
* allocation.
*/
inline bool is_device_accessible_memory(void* ptr)
{
cudaPointerAttributes attributes{};
if (cudaSuccess != cudaPointerGetAttributes(&attributes, ptr)) { return false; }
return (attributes.type == cudaMemoryTypeDevice) or (attributes.type == cudaMemoryTypeManaged) or
((attributes.type == cudaMemoryTypeHost) and (attributes.devicePointer != nullptr));
}

inline bool is_host_memory(void* ptr)
{
cudaPointerAttributes attributes{};
if (cudaSuccess != cudaPointerGetAttributes(&attributes, ptr)) { return false; }
return attributes.type == cudaMemoryTypeHost;
}

inline bool is_properly_aligned(void* ptr)
{
if (is_host_memory(ptr)) { return rmm::is_pointer_aligned(ptr, rmm::RMM_DEFAULT_HOST_ALIGNMENT); }
return rmm::is_pointer_aligned(ptr, rmm::CUDA_ALLOCATION_ALIGNMENT);
}

enum size_in_bytes : size_t {};

constexpr auto default_num_allocations{100};
Expand Down
50 changes: 50 additions & 0 deletions tests/mr/device/test_utils.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright (c) 2024, NVIDIA 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.
*/

#pragma once

#include <rmm/aligned.hpp>

#include <cuda_runtime_api.h>

namespace rmm::test {

/**
* @brief Returns if a pointer points to a device memory or managed memory
* allocation.
*/
inline bool is_device_accessible_memory(void* ptr)
{
cudaPointerAttributes attributes{};
if (cudaSuccess != cudaPointerGetAttributes(&attributes, ptr)) { return false; }
return (attributes.type == cudaMemoryTypeDevice) or (attributes.type == cudaMemoryTypeManaged) or
((attributes.type == cudaMemoryTypeHost) and (attributes.devicePointer != nullptr));
}

inline bool is_host_memory(void* ptr)
{
cudaPointerAttributes attributes{};
if (cudaSuccess != cudaPointerGetAttributes(&attributes, ptr)) { return false; }
return attributes.type == cudaMemoryTypeHost;
}

inline bool is_properly_aligned(void* ptr)
{
if (is_host_memory(ptr)) { return rmm::is_pointer_aligned(ptr, rmm::RMM_DEFAULT_HOST_ALIGNMENT); }
return rmm::is_pointer_aligned(ptr, rmm::CUDA_ALLOCATION_ALIGNMENT);
}

} // namespace rmm::test

0 comments on commit aafa18a

Please sign in to comment.