-
Notifications
You must be signed in to change notification settings - Fork 23
Add experimental HostArrayManager #351
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
8db9c2d
Add HostArrayManager
adayton1 0527bff
Add tests for HostArrayManager
adayton1 cb5f940
Add more tests
adayton1 399a14d
Test fixes
adayton1 9a30562
Add HostArrayPointerTests
adayton1 f260e21
Fix a couple of bugs
adayton1 2b886b6
Clean up
adayton1 12eaaab
Test updates
adayton1 ac9c776
Add docs
adayton1 edef37b
Add a factory function and tests
adayton1 d2a2277
Clean up
adayton1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,130 @@ | ||
| ////////////////////////////////////////////////////////////////////////////// | ||
| // Copyright (c) 2016-25, Lawrence Livermore National Security, LLC and CHAI | ||
| // project contributors. See the CHAI LICENSE file for details. | ||
| // | ||
| // SPDX-License-Identifier: BSD-3-Clause | ||
| ////////////////////////////////////////////////////////////////////////////// | ||
| #ifndef CHAI_HOST_ARRAY_MANAGER_HPP | ||
| #define CHAI_HOST_ARRAY_MANAGER_HPP | ||
|
|
||
| #include "umpire/ResourceManager.hpp" | ||
| #include "umpire/TypedAllocator.hpp" | ||
| #include <cstddef> | ||
| #include <vector> | ||
|
|
||
| namespace chai::expt | ||
| { | ||
| /*! | ||
| * \brief This class manages a host array. It is designed for use with | ||
| * ManagedArrayPointer. | ||
| * | ||
| * \tparam ElementType The type of elements contained in this array. | ||
| * | ||
| * \note HostArrayManager performs value initialization of each array element. | ||
| * That is to say, numeric types will be initialized to zero and nontrivial | ||
| * types will be default constructed. In the future, this behavior may | ||
| * change to default initialization for performance reasons, such that | ||
| * numeric types will be left in an indeterminate state and nontrivial | ||
| * types will be default constructed. | ||
| * | ||
| * \note HostArrayManager does not rely on ContextManager, so it will behave | ||
| * differently than other array managers. The major difference is that | ||
| * when used with ManagedArrayPointer, the ManagedArrayPointer does not | ||
| * need the update method called or to be copy constructed before it can | ||
| * be used on the host. Since it will not respect the current Context, | ||
| * be extra careful to avoid using it on the device. | ||
| */ | ||
| template <typename ElementType> | ||
| class HostArrayManager { | ||
| private: | ||
| /*! | ||
| * \brief Allocator used by the managed host storage. | ||
| */ | ||
| using AllocatorType = ::umpire::TypedAllocator<ElementType>; | ||
|
|
||
| /*! | ||
| * \brief Underlying contiguous host storage type for managed elements. | ||
| */ | ||
| using StorageType = std::vector<ElementType, AllocatorType>; | ||
|
|
||
| public: | ||
| /*! | ||
| * \brief Default-constructs a HostArrayManager with zero elements | ||
| * and a default allocator for host memory allocations. | ||
| */ | ||
| HostArrayManager() = default; | ||
|
|
||
| /*! | ||
| * \brief Constructs a HostArrayManager with zero elements | ||
| * and \p allocator for host memory allocations. | ||
| * | ||
| * \param allocator Allocator used for host memory allocations. | ||
| */ | ||
| explicit HostArrayManager(const umpire::Allocator& allocator) | ||
| : m_storage{StorageType(AllocatorType(allocator))} | ||
| { | ||
| } | ||
|
|
||
| /*! | ||
| * \brief Constructs a HostArrayManager with \p size elements | ||
| * using the default allocator for host memory allocations. | ||
| * | ||
| * \param size Number of elements to allocate. | ||
| */ | ||
| explicit HostArrayManager(std::size_t size) | ||
| : m_storage{StorageType(size, AllocatorType(::umpire::ResourceManager::getInstance().getAllocator("HOST")))} | ||
| { | ||
| } | ||
|
|
||
| /*! | ||
| * \brief Constructs a HostArrayManager with \p size elements | ||
| * using \p allocator for host memory allocations. | ||
| * | ||
| * \param size Number of elements to allocate. | ||
| * \param allocator Allocator used for host memory allocations. | ||
| */ | ||
| HostArrayManager(std::size_t size, | ||
| const umpire::Allocator& allocator) | ||
| : m_storage{StorageType(size, AllocatorType(allocator))} | ||
| { | ||
| } | ||
|
|
||
| /*! | ||
| * \brief Resizes the managed storage to \p new_size elements. | ||
| * | ||
| * \param new_size New number of elements. | ||
| */ | ||
| void resize(std::size_t new_size) | ||
| { | ||
| m_storage.resize(new_size); | ||
| } | ||
|
|
||
| /*! | ||
| * \brief Returns the number of elements currently managed. | ||
| * | ||
| * \return Number of elements in the managed storage. | ||
| */ | ||
| std::size_t size() const | ||
| { | ||
| return m_storage.size(); | ||
| } | ||
|
|
||
| /*! | ||
| * \brief Returns a pointer to the underlying contiguous storage. | ||
| * | ||
| * \return Pointer to the first element, or nullptr if the storage is empty. | ||
| */ | ||
| ElementType* data() | ||
| { | ||
| return m_storage.empty() ? nullptr : m_storage.data(); | ||
| } | ||
|
|
||
| private: | ||
| /*! | ||
| * \brief Underlying host storage for the managed elements. | ||
| */ | ||
| StorageType m_storage{AllocatorType(::umpire::ResourceManager::getInstance().getAllocator("HOST"))}; | ||
| }; // class HostArrayManager | ||
| } // namespace chai::expt | ||
|
|
||
| #endif // CHAI_HOST_ARRAY_MANAGER_HPP | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.