spb/forge Public MIT
Forge — LLM training from scratch in pure C++20 + Metal on Apple Silicon.
C++ 61.2%
C 23%
Python 7.6%
TeX 7.2%
CMake 1.1%
1//-------------------------------------------------------------------------------------------------------------------------------------------------------------2//3// Metal/MTL4CommandQueue.hpp4//5// Copyright 2020-2025 Apple Inc.6//7// Licensed under the Apache License, Version 2.0 (the "License");8// you may not use this file except in compliance with the License.9// You may obtain a copy of the License at10//11// http://www.apache.org/licenses/LICENSE-2.012//13// Unless required by applicable law or agreed to in writing, software14// distributed under the License is distributed on an "AS IS" BASIS,15// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.16// See the License for the specific language governing permissions and17// limitations under the License.18//19//-------------------------------------------------------------------------------------------------------------------------------------------------------------2021#pragma once2223#include "../Foundation/Foundation.hpp"24#include "MTL4CommitFeedback.hpp"25#include "MTLDefines.hpp"26#include "MTLHeaderBridge.hpp"27#include "MTLPrivate.hpp"28#include "MTLResourceStateCommandEncoder.hpp"29#include "MTLTypes.hpp"30#include <cstdint>31#include <dispatch/dispatch.h>3233namespace MTL34{35class Buffer;36class Device;37class Drawable;38class Event;39class Heap;40class ResidencySet;41class Texture;42}4344namespace MTL445{46class CommandBuffer;47class CommandQueueDescriptor;48class CommitOptions;49struct CopySparseBufferMappingOperation;50struct CopySparseTextureMappingOperation;51struct UpdateSparseBufferMappingOperation;52struct UpdateSparseTextureMappingOperation;53_MTL_ENUM(NS::Integer, CommandQueueError) {54 CommandQueueErrorNone = 0,55 CommandQueueErrorTimeout = 1,56 CommandQueueErrorNotPermitted = 2,57 CommandQueueErrorOutOfMemory = 3,58 CommandQueueErrorDeviceRemoved = 4,59 CommandQueueErrorAccessRevoked = 5,60 CommandQueueErrorInternal = 6,61};6263struct UpdateSparseTextureMappingOperation64{65 MTL::SparseTextureMappingMode mode;66 MTL::Region textureRegion;67 NS::UInteger textureLevel;68 NS::UInteger textureSlice;69 NS::UInteger heapOffset;70} _MTL_PACKED;7172struct CopySparseTextureMappingOperation73{74 MTL::Region sourceRegion;75 NS::UInteger sourceLevel;76 NS::UInteger sourceSlice;77 MTL::Origin destinationOrigin;78 NS::UInteger destinationLevel;79 NS::UInteger destinationSlice;80} _MTL_PACKED;8182struct UpdateSparseBufferMappingOperation83{84 MTL::SparseTextureMappingMode mode;85 NS::Range bufferRange;86 NS::UInteger heapOffset;87} _MTL_PACKED;8889struct CopySparseBufferMappingOperation90{91 NS::Range sourceRange;92 NS::UInteger destinationOffset;93} _MTL_PACKED;9495class CommitOptions : public NS::Referencing<CommitOptions>96{97public:98 void addFeedbackHandler(const MTL4::CommitFeedbackHandler block);99 void addFeedbackHandler(const MTL4::CommitFeedbackHandlerFunction& function);100101 static CommitOptions* alloc();102103 CommitOptions* init();104};105class CommandQueueDescriptor : public NS::Copying<CommandQueueDescriptor>106{107public:108 static CommandQueueDescriptor* alloc();109110 dispatch_queue_t feedbackQueue() const;111112 CommandQueueDescriptor* init();113114 NS::String* label() const;115116 void setFeedbackQueue(const dispatch_queue_t feedbackQueue);117118 void setLabel(const NS::String* label);119};120class CommandQueue : public NS::Referencing<CommandQueue>121{122public:123 void addResidencySet(const MTL::ResidencySet* residencySet);124 void addResidencySets(const MTL::ResidencySet* const residencySets[], NS::UInteger count);125126 void commit(const MTL4::CommandBuffer* const commandBuffers[], NS::UInteger count);127 void commit(const MTL4::CommandBuffer* const commandBuffers[], NS::UInteger count, const MTL4::CommitOptions* options);128129 void copyBufferMappingsFromBuffer(const MTL::Buffer* sourceBuffer, const MTL::Buffer* destinationBuffer, const MTL4::CopySparseBufferMappingOperation* operations, NS::UInteger count);130131 void copyTextureMappingsFromTexture(const MTL::Texture* sourceTexture, const MTL::Texture* destinationTexture, const MTL4::CopySparseTextureMappingOperation* operations, NS::UInteger count);132133 MTL::Device* device() const;134135 NS::String* label() const;136137 void removeResidencySet(const MTL::ResidencySet* residencySet);138 void removeResidencySets(const MTL::ResidencySet* const residencySets[], NS::UInteger count);139140 void signalDrawable(const MTL::Drawable* drawable);141142 void signalEvent(const MTL::Event* event, uint64_t value);143144 void updateBufferMappings(const MTL::Buffer* buffer, const MTL::Heap* heap, const MTL4::UpdateSparseBufferMappingOperation* operations, NS::UInteger count);145146 void updateTextureMappings(const MTL::Texture* texture, const MTL::Heap* heap, const MTL4::UpdateSparseTextureMappingOperation* operations, NS::UInteger count);147148 void wait(const MTL::Event* event, uint64_t value);149 void wait(const MTL::Drawable* drawable);150};151152}153154_MTL_INLINE void MTL4::CommitOptions::addFeedbackHandler(const MTL4::CommitFeedbackHandler block)155{156 Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(addFeedbackHandler_), block);157}158159_MTL_INLINE void MTL4::CommitOptions::addFeedbackHandler(const MTL4::CommitFeedbackHandlerFunction& function)160{161 __block MTL4::CommitFeedbackHandlerFunction blockFunction = function;162 addFeedbackHandler(^(MTL4::CommitFeedback* pFeedback) { blockFunction(pFeedback); });163}164165_MTL_INLINE MTL4::CommitOptions* MTL4::CommitOptions::alloc()166{167 return NS::Object::alloc<MTL4::CommitOptions>(_MTL_PRIVATE_CLS(MTL4CommitOptions));168}169170_MTL_INLINE MTL4::CommitOptions* MTL4::CommitOptions::init()171{172 return NS::Object::init<MTL4::CommitOptions>();173}174175_MTL_INLINE MTL4::CommandQueueDescriptor* MTL4::CommandQueueDescriptor::alloc()176{177 return NS::Object::alloc<MTL4::CommandQueueDescriptor>(_MTL_PRIVATE_CLS(MTL4CommandQueueDescriptor));178}179180_MTL_INLINE dispatch_queue_t MTL4::CommandQueueDescriptor::feedbackQueue() const181{182 return Object::sendMessage<dispatch_queue_t>(this, _MTL_PRIVATE_SEL(feedbackQueue));183}184185_MTL_INLINE MTL4::CommandQueueDescriptor* MTL4::CommandQueueDescriptor::init()186{187 return NS::Object::init<MTL4::CommandQueueDescriptor>();188}189190_MTL_INLINE NS::String* MTL4::CommandQueueDescriptor::label() const191{192 return Object::sendMessage<NS::String*>(this, _MTL_PRIVATE_SEL(label));193}194195_MTL_INLINE void MTL4::CommandQueueDescriptor::setFeedbackQueue(const dispatch_queue_t feedbackQueue)196{197 Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setFeedbackQueue_), feedbackQueue);198}199200_MTL_INLINE void MTL4::CommandQueueDescriptor::setLabel(const NS::String* label)201{202 Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setLabel_), label);203}204205_MTL_INLINE void MTL4::CommandQueue::addResidencySet(const MTL::ResidencySet* residencySet)206{207 Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(addResidencySet_), residencySet);208}209210_MTL_INLINE void MTL4::CommandQueue::addResidencySets(const MTL::ResidencySet* const residencySets[], NS::UInteger count)211{212 Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(addResidencySets_count_), residencySets, count);213}214215_MTL_INLINE void MTL4::CommandQueue::commit(const MTL4::CommandBuffer* const commandBuffers[], NS::UInteger count)216{217 Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(commit_count_), commandBuffers, count);218}219220_MTL_INLINE void MTL4::CommandQueue::commit(const MTL4::CommandBuffer* const commandBuffers[], NS::UInteger count, const MTL4::CommitOptions* options)221{222 Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(commit_count_options_), commandBuffers, count, options);223}224225_MTL_INLINE void MTL4::CommandQueue::copyBufferMappingsFromBuffer(const MTL::Buffer* sourceBuffer, const MTL::Buffer* destinationBuffer, const MTL4::CopySparseBufferMappingOperation* operations, NS::UInteger count)226{227 Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(copyBufferMappingsFromBuffer_toBuffer_operations_count_), sourceBuffer, destinationBuffer, operations, count);228}229230_MTL_INLINE void MTL4::CommandQueue::copyTextureMappingsFromTexture(const MTL::Texture* sourceTexture, const MTL::Texture* destinationTexture, const MTL4::CopySparseTextureMappingOperation* operations, NS::UInteger count)231{232 Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(copyTextureMappingsFromTexture_toTexture_operations_count_), sourceTexture, destinationTexture, operations, count);233}234235_MTL_INLINE MTL::Device* MTL4::CommandQueue::device() const236{237 return Object::sendMessage<MTL::Device*>(this, _MTL_PRIVATE_SEL(device));238}239240_MTL_INLINE NS::String* MTL4::CommandQueue::label() const241{242 return Object::sendMessage<NS::String*>(this, _MTL_PRIVATE_SEL(label));243}244245_MTL_INLINE void MTL4::CommandQueue::removeResidencySet(const MTL::ResidencySet* residencySet)246{247 Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(removeResidencySet_), residencySet);248}249250_MTL_INLINE void MTL4::CommandQueue::removeResidencySets(const MTL::ResidencySet* const residencySets[], NS::UInteger count)251{252 Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(removeResidencySets_count_), residencySets, count);253}254255_MTL_INLINE void MTL4::CommandQueue::signalDrawable(const MTL::Drawable* drawable)256{257 Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(signalDrawable_), drawable);258}259260_MTL_INLINE void MTL4::CommandQueue::signalEvent(const MTL::Event* event, uint64_t value)261{262 Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(signalEvent_value_), event, value);263}264265_MTL_INLINE void MTL4::CommandQueue::updateBufferMappings(const MTL::Buffer* buffer, const MTL::Heap* heap, const MTL4::UpdateSparseBufferMappingOperation* operations, NS::UInteger count)266{267 Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(updateBufferMappings_heap_operations_count_), buffer, heap, operations, count);268}269270_MTL_INLINE void MTL4::CommandQueue::updateTextureMappings(const MTL::Texture* texture, const MTL::Heap* heap, const MTL4::UpdateSparseTextureMappingOperation* operations, NS::UInteger count)271{272 Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(updateTextureMappings_heap_operations_count_), texture, heap, operations, count);273}274275_MTL_INLINE void MTL4::CommandQueue::wait(const MTL::Event* event, uint64_t value)276{277 Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(waitForEvent_value_), event, value);278}279280_MTL_INLINE void MTL4::CommandQueue::wait(const MTL::Drawable* drawable)281{282 Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(waitForDrawable_), drawable);283}284