forked from rapidsai/raft
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide memory_type enum (rapidsai#984)
This PR introduces an enum to specify a memory type (e.g. host, device, managed...). This allows us to provide a template parameter which always indicates a valid memory type, which is extensible for possible future memory types, and which is less verbose than alternatives. The most serious shortcoming of existing alternatives is the possibility of indicating invalid memory states. E.g. by templating on `is_device` and `is_host`, we introduce the possible state `is_host=false, is_device=false`. Authors: - William Hicks (https://github.com/wphicks) Approvers: - Rajesh Gandham (https://github.com/rg20) - Divye Gala (https://github.com/divyegala) URL: rapidsai#984
- Loading branch information
Showing
8 changed files
with
152 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright (c) 2022, 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 | ||
|
||
namespace raft { | ||
enum class memory_type { host, device, managed, pinned }; | ||
|
||
auto constexpr is_device_accessible(memory_type mem_type) | ||
{ | ||
return (mem_type == memory_type::device || mem_type == memory_type::managed); | ||
} | ||
auto constexpr is_host_accessible(memory_type mem_type) | ||
{ | ||
return (mem_type == memory_type::host || mem_type == memory_type::managed || | ||
mem_type == memory_type::pinned); | ||
} | ||
auto constexpr is_host_device_accessible(memory_type mem_type) | ||
{ | ||
return is_device_accessible(mem_type) && is_host_accessible(mem_type); | ||
} | ||
|
||
namespace detail { | ||
|
||
template <bool is_host_accessible, bool is_device_accessible> | ||
auto constexpr memory_type_from_access() | ||
{ | ||
if constexpr (is_host_accessible && is_device_accessible) { | ||
return memory_type::managed; | ||
} else if constexpr (is_host_accessible) { | ||
return memory_type::host; | ||
} else if constexpr (is_device_accessible) { | ||
return memory_type::device; | ||
} | ||
static_assert(is_host_accessible || is_device_accessible, | ||
"Must be either host or device accessible to return a valid memory type"); | ||
} | ||
|
||
} // end namespace detail | ||
} // end namespace raft |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright (c) 2022, 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. | ||
*/ | ||
#include <gtest/gtest.h> | ||
#include <raft/core/memory_type.hpp> | ||
|
||
namespace raft { | ||
TEST(MemoryType, IsDeviceAccessible) | ||
{ | ||
static_assert(!is_device_accessible(memory_type::host)); | ||
static_assert(is_device_accessible(memory_type::device)); | ||
static_assert(is_device_accessible(memory_type::managed)); | ||
static_assert(!is_device_accessible(memory_type::pinned)); | ||
} | ||
|
||
TEST(MemoryType, IsHostAccessible) | ||
{ | ||
static_assert(is_host_accessible(memory_type::host)); | ||
static_assert(!is_host_accessible(memory_type::device)); | ||
static_assert(is_host_accessible(memory_type::managed)); | ||
static_assert(is_host_accessible(memory_type::pinned)); | ||
} | ||
|
||
TEST(MemoryType, IsHostDeviceAccessible) | ||
{ | ||
static_assert(!is_host_device_accessible(memory_type::host)); | ||
static_assert(!is_host_device_accessible(memory_type::device)); | ||
static_assert(is_host_device_accessible(memory_type::managed)); | ||
static_assert(!is_host_device_accessible(memory_type::pinned)); | ||
} | ||
} // namespace raft |