-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HIP updates: managed_memory_resource and missing header (#272)
* Add missing include * Add hip version of managed_memory_resource * Update copyright * Extra includes and copyrights, formatting * Minor fixes for the HIP code's benefit. --------- Co-authored-by: Stewart Martin-Haugh [email protected] <[email protected]> Co-authored-by: Attila Krasznahorkay <[email protected]>
- Loading branch information
1 parent
e8a9614
commit 5f2d20e
Showing
9 changed files
with
147 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* VecMem project, part of the ACTS project (R&D line) | ||
* | ||
* (c) 2024 CERN for the benefit of the ACTS project | ||
* | ||
* Mozilla Public License Version 2.0 | ||
*/ | ||
|
||
#pragma once | ||
|
||
// Local include(s). | ||
#include "vecmem/memory/memory_resource.hpp" | ||
#include "vecmem/vecmem_hip_export.hpp" | ||
|
||
namespace vecmem::hip { | ||
|
||
/** | ||
* @brief Memory resource that wraps managed HIP allocation. | ||
* | ||
* This is an allocator-type memory resource that allocates managed HIP | ||
* memory, which is accessible directly to devices as well as to the host. | ||
*/ | ||
class managed_memory_resource final : public memory_resource { | ||
|
||
public: | ||
/// Default constructor | ||
VECMEM_HIP_EXPORT | ||
managed_memory_resource(); | ||
/// Destructor | ||
VECMEM_HIP_EXPORT | ||
~managed_memory_resource(); | ||
|
||
private: | ||
/// @name Function(s) implementing @c vecmem::memory_resource | ||
/// @{ | ||
|
||
/// Allocate HIP managed memory | ||
VECMEM_HIP_EXPORT | ||
virtual void* do_allocate(std::size_t, std::size_t) override final; | ||
/// De-allocate a previously allocated managed memory block | ||
VECMEM_HIP_EXPORT | ||
virtual void do_deallocate(void* p, std::size_t, | ||
std::size_t) override final; | ||
/// Compares @c *this for equality with @c other | ||
VECMEM_HIP_EXPORT | ||
virtual bool do_is_equal( | ||
const memory_resource& other) const noexcept override final; | ||
|
||
/// @} | ||
|
||
}; // class managed_memory_resource | ||
|
||
} // namespace vecmem::hip |
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,55 @@ | ||
/* | ||
* VecMem project, part of the ACTS project (R&D line) | ||
* | ||
* (c) 2024 CERN for the benefit of the ACTS project | ||
* | ||
* Mozilla Public License Version 2.0 | ||
*/ | ||
|
||
// Local include(s). | ||
#include "vecmem/memory/hip/managed_memory_resource.hpp" | ||
|
||
#include "../utils/hip_error_handling.hpp" | ||
#include "vecmem/utils/debug.hpp" | ||
|
||
// HIP include(s). | ||
#include <hip/hip_runtime_api.h> | ||
|
||
namespace vecmem::hip { | ||
|
||
managed_memory_resource::managed_memory_resource() = default; | ||
|
||
managed_memory_resource::~managed_memory_resource() = default; | ||
|
||
void *managed_memory_resource::do_allocate(std::size_t bytes, std::size_t) { | ||
|
||
if (bytes == 0) { | ||
return nullptr; | ||
} | ||
|
||
// Allocate the memory. | ||
void *res = nullptr; | ||
VECMEM_HIP_ERROR_CHECK(hipMallocManaged(&res, bytes)); | ||
VECMEM_DEBUG_MSG(2, "Allocated %ld bytes at %p", bytes, res); | ||
return res; | ||
} | ||
|
||
void managed_memory_resource::do_deallocate(void *p, std::size_t, std::size_t) { | ||
|
||
if (p == nullptr) { | ||
return; | ||
} | ||
|
||
// Free the memory. | ||
VECMEM_DEBUG_MSG(2, "De-allocating memory at %p", p); | ||
VECMEM_HIP_ERROR_CHECK(hipFree(p)); | ||
} | ||
|
||
bool managed_memory_resource::do_is_equal( | ||
const memory_resource &other) const noexcept { | ||
|
||
// The two are equal if they are of the same type. | ||
return (dynamic_cast<const managed_memory_resource *>(&other) != nullptr); | ||
} | ||
|
||
} // namespace vecmem::hip |
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