-
Notifications
You must be signed in to change notification settings - Fork 13
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
Introduce vecmem::metal, main branch (2024.10.27.) #300
Draft
krasznaa
wants to merge
5
commits into
acts-project:main
Choose a base branch
from
krasznaa:MetalSupport-main-20241027
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a9d0298
Added support for Objective-C(++) and Metal source files.
krasznaa a3debde
Introduced the vecmem::metal library.
krasznaa f953351
Added basic tests for vecmem::metal::shared_memory_resource.
krasznaa 3676522
Tweaked the vecmem::metal code a little.
krasznaa f8d3042
Taught the code what to do without a Metal device.
krasznaa 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 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,34 @@ | ||
# 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 | ||
|
||
# OBJCXX support requires CMake 3.16. | ||
cmake_minimum_required( VERSION 3.16 ) | ||
|
||
# Enable the OBJCXX language. | ||
enable_language( OBJCXX ) | ||
|
||
# Set up the build of the VecMem Metal library. | ||
vecmem_add_library( vecmem_metal metal | ||
# Memory management. | ||
"include/vecmem/memory/metal/shared_memory_resource.hpp" | ||
"src/memory/shared_memory_resource.mm" | ||
# Utilities. | ||
"include/vecmem/utils/metal/device_wrapper.hpp" | ||
"src/utils/device_wrapper.mm" | ||
"src/utils/opaque_device.h" | ||
"src/utils/get_device.h" | ||
"src/utils/get_device.mm" ) | ||
target_link_libraries( vecmem_metal PUBLIC vecmem::core ) | ||
|
||
# Hide the library's symbols by default. | ||
set_target_properties( vecmem_metal PROPERTIES | ||
OBJCXX_VISIBILITY_PRESET "hidden" ) | ||
|
||
# Link against the necessary macOS framework(s). | ||
find_library( VECMEM_FOUNDATION_FRAMEWORK "Foundation" REQUIRED ) | ||
find_library( VECMEM_METAL_FRAMEWORK "Metal" REQUIRED ) | ||
target_link_libraries( vecmem_metal | ||
PRIVATE ${VECMEM_FOUNDATION_FRAMEWORK} ${VECMEM_METAL_FRAMEWORK} ) |
67 changes: 67 additions & 0 deletions
67
metal/include/vecmem/memory/metal/shared_memory_resource.hpp
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,67 @@ | ||
/* | ||
* 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/utils/metal/device_wrapper.hpp" | ||
#include "vecmem/vecmem_metal_export.hpp" | ||
|
||
// System include(s). | ||
#include <memory> | ||
|
||
/// @brief Namespace holding types that work with Metal | ||
namespace vecmem::metal { | ||
|
||
// Forward declaration(s). | ||
namespace details { | ||
struct shared_memory_resource_data; | ||
} | ||
|
||
/** | ||
* @brief Memory resource that exposes Metal shared memory allocations. | ||
* | ||
* This is an allocator-type memory resource (that is to say, it only | ||
* allocates, it does not try to manage memory in a smart way) that works | ||
* for Metal shared memory. Each instance is bound to a specific device. | ||
*/ | ||
class shared_memory_resource final : public memory_resource { | ||
|
||
public: | ||
/// Default constructor | ||
VECMEM_METAL_EXPORT | ||
shared_memory_resource(const device_wrapper& device = {}); | ||
/// Destructor | ||
VECMEM_METAL_EXPORT | ||
~shared_memory_resource(); | ||
|
||
private: | ||
/// @name Function(s) implementing @c vecmem::memory_resource | ||
/// @{ | ||
|
||
/// Allocate memory on the selected device | ||
VECMEM_METAL_EXPORT | ||
virtual void* do_allocate(std::size_t, std::size_t) override final; | ||
/// De-allocate a previously allocated memory block on the selected device | ||
VECMEM_METAL_EXPORT | ||
virtual void do_deallocate(void* p, std::size_t, | ||
std::size_t) override final; | ||
/// Compares @c *this for equality with @c other | ||
VECMEM_METAL_EXPORT | ||
virtual bool do_is_equal( | ||
const memory_resource& other) const noexcept override final; | ||
|
||
/// @} | ||
|
||
/// Internal state of the memory resource | ||
std::unique_ptr<details::shared_memory_resource_data> m_data; | ||
|
||
}; // class shared_memory_resource | ||
|
||
} // namespace vecmem::metal |
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,74 @@ | ||
/* 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/vecmem_metal_export.hpp" | ||
|
||
// System include(s). | ||
#include <memory> | ||
|
||
namespace vecmem::metal { | ||
|
||
// Forward declaration(s). | ||
namespace details { | ||
class opaque_device; | ||
} | ||
|
||
/// Wrapper class for @c MTLDevice | ||
/// | ||
/// It is necessary for passing around Metal device objects in | ||
/// non-Objective-C(++) code. | ||
/// | ||
class device_wrapper { | ||
|
||
public: | ||
/// Construct a default device | ||
VECMEM_METAL_EXPORT | ||
device_wrapper(); | ||
/// Wrap an existing @c MTLDevice object | ||
/// | ||
/// Without taking ownership of it! | ||
/// | ||
VECMEM_METAL_EXPORT | ||
device_wrapper(void* device); | ||
|
||
/// Copy constructor | ||
VECMEM_METAL_EXPORT | ||
device_wrapper(const device_wrapper& parent); | ||
/// Move constructor | ||
VECMEM_METAL_EXPORT | ||
device_wrapper(device_wrapper&& parent); | ||
|
||
/// Destructor | ||
VECMEM_METAL_EXPORT | ||
~device_wrapper(); | ||
|
||
/// Copy assignment | ||
VECMEM_METAL_EXPORT | ||
device_wrapper& operator=(const device_wrapper& rhs); | ||
/// Move assignment | ||
VECMEM_METAL_EXPORT | ||
device_wrapper& operator=(device_wrapper&& rhs); | ||
|
||
/// Access a typeless pointer to the managed @c MTLDevice object | ||
VECMEM_METAL_EXPORT | ||
void* device(); | ||
/// Access a typeless pointer to the managed @c MTLDevice object | ||
VECMEM_METAL_EXPORT | ||
const void* device() const; | ||
|
||
private: | ||
/// Bare pointer to the wrapped @c MTLDevice object | ||
void* m_device; | ||
|
||
/// Smart pointer to the managed @c MTLDevice object | ||
std::unique_ptr<details::opaque_device> m_managedDevice; | ||
|
||
}; // class device_wrapper | ||
|
||
} // namespace vecmem::metal |
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,80 @@ | ||
/* | ||
* 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/metal/shared_memory_resource.hpp" | ||
#include "../utils/get_device.h" | ||
|
||
// System import(s). | ||
#import <Metal/Metal.h> | ||
|
||
// System include(s). | ||
#include <cassert> | ||
#include <map> | ||
#include <stdexcept> | ||
|
||
namespace vecmem::metal { | ||
namespace details { | ||
|
||
/// Internal state for the shared memory resource. | ||
struct shared_memory_resource_data { | ||
/// The Metal device. | ||
device_wrapper m_device; | ||
/// Memory buffers allocated on the device. | ||
std::map<void*, id<MTLBuffer> > m_buffers; | ||
}; | ||
|
||
} // namespace details | ||
|
||
shared_memory_resource::shared_memory_resource(const device_wrapper& device) | ||
: m_data(std::make_unique<details::shared_memory_resource_data>()) { | ||
|
||
// Save the device wrapper object. | ||
m_data->m_device = device; | ||
} | ||
|
||
shared_memory_resource::~shared_memory_resource() {} | ||
|
||
void* shared_memory_resource::do_allocate(std::size_t bytes, std::size_t) { | ||
|
||
// Create a new shared buffer. | ||
id<MTLBuffer> buffer = [details::get_device(m_data->m_device) | ||
newBufferWithLength:bytes | ||
options:MTLResourceStorageModeShared]; | ||
// If the buffer was created successfully, store it in the map, and return | ||
// its pointer. | ||
if (buffer != nil) { | ||
void* ptr = buffer.contents; | ||
assert(m_data); | ||
m_data->m_buffers[ptr] = buffer; | ||
return ptr; | ||
} | ||
|
||
// If the buffer could not be created, throw an exception. | ||
throw std::bad_alloc(); | ||
} | ||
|
||
void shared_memory_resource::do_deallocate(void* p, std::size_t, std::size_t) { | ||
|
||
// Find the buffer, and release it. It's undefined behaviour to deallocate | ||
// a buffer that was not allocated by this memory resource. So we might as | ||
// well crash when that happens. | ||
assert(m_data); | ||
auto it = m_data->m_buffers.find(p); | ||
assert(it != m_data->m_buffers.end()); | ||
m_data->m_buffers.erase(it); | ||
} | ||
|
||
bool shared_memory_resource::do_is_equal( | ||
const memory_resource& other) const noexcept { | ||
|
||
// The memory resource manages buffers itself. It is only equal to itself. | ||
return this == &other; | ||
} | ||
|
||
} // namespace vecmem::metal |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
m_buffers
array just contains the pointer right? Doesid<MTLBuffer>
support RAII in this way, or do you need to do any other cleanup?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My own experience with this is limited, but from what I understand, Apple uses ARC these days.
But I've indeed not quite made sure that it would be active in this case. 🤔 I think ARC is in effect all the time unless it's turned off, but at the same time I've also seen @autoreleasepool in some of the examples. But I think that's not needed for the garbage collection... 🤔 (I believe that's a slightly different thing.)
I'll need to check this in action, but I think one just needs to make
id
objects go out of scope, and the runtime should take care of deleting them eventually.