SPB Git

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%
6.7 KB · 183 lines cpp
Raw Blame History
1//-------------------------------------------------------------------------------------------------------------------------------------------------------------2//3// Metal/MTLIOCommandBuffer.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 "MTLDefines.hpp"25#include "MTLHeaderBridge.hpp"26#include "MTLPrivate.hpp"27#include "MTLTypes.hpp"28#include <cstdint>2930namespace MTL31{32class Buffer;33class IOCommandBuffer;34class IOFileHandle;35class SharedEvent;36class Texture;37_MTL_ENUM(NS::Integer, IOStatus) {38    IOStatusPending = 0,39    IOStatusCancelled = 1,40    IOStatusError = 2,41    IOStatusComplete = 3,42};4344using IOCommandBufferHandler = void (^)(MTL::IOCommandBuffer*);45using IOCommandBufferHandlerFunction = std::function<void(MTL::IOCommandBuffer*)>;4647class IOCommandBuffer : public NS::Referencing<IOCommandBuffer>48{49public:50    void        addBarrier();5152    void        addCompletedHandler(const MTL::IOCommandBufferHandler block);53    void        addCompletedHandler(const MTL::IOCommandBufferHandlerFunction& function);5455    void        commit();5657    void        copyStatusToBuffer(const MTL::Buffer* buffer, NS::UInteger offset);5859    void        enqueue();6061    NS::Error*  error() const;6263    NS::String* label() const;6465    void        loadBuffer(const MTL::Buffer* buffer, NS::UInteger offset, NS::UInteger size, const MTL::IOFileHandle* sourceHandle, NS::UInteger sourceHandleOffset);6667    void        loadBytes(const void* pointer, NS::UInteger size, const MTL::IOFileHandle* sourceHandle, NS::UInteger sourceHandleOffset);6869    void        loadTexture(const MTL::Texture* texture, NS::UInteger slice, NS::UInteger level, MTL::Size size, NS::UInteger sourceBytesPerRow, NS::UInteger sourceBytesPerImage, MTL::Origin destinationOrigin, const MTL::IOFileHandle* sourceHandle, NS::UInteger sourceHandleOffset);7071    void        popDebugGroup();7273    void        pushDebugGroup(const NS::String* string);7475    void        setLabel(const NS::String* label);7677    void        signalEvent(const MTL::SharedEvent* event, uint64_t value);7879    IOStatus    status() const;8081    void        tryCancel();8283    void        wait(const MTL::SharedEvent* event, uint64_t value);84    void        waitUntilCompleted();85};8687}88_MTL_INLINE void MTL::IOCommandBuffer::addBarrier()89{90    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(addBarrier));91}9293_MTL_INLINE void MTL::IOCommandBuffer::addCompletedHandler(const MTL::IOCommandBufferHandler block)94{95    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(addCompletedHandler_), block);96}9798_MTL_INLINE void MTL::IOCommandBuffer::addCompletedHandler(const MTL::IOCommandBufferHandlerFunction& function)99{100    __block MTL::IOCommandBufferHandlerFunction blockFunction = function;101    addCompletedHandler(^(MTL::IOCommandBuffer* pCommandBuffer) { blockFunction(pCommandBuffer); });102}103104_MTL_INLINE void MTL::IOCommandBuffer::commit()105{106    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(commit));107}108109_MTL_INLINE void MTL::IOCommandBuffer::copyStatusToBuffer(const MTL::Buffer* buffer, NS::UInteger offset)110{111    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(copyStatusToBuffer_offset_), buffer, offset);112}113114_MTL_INLINE void MTL::IOCommandBuffer::enqueue()115{116    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(enqueue));117}118119_MTL_INLINE NS::Error* MTL::IOCommandBuffer::error() const120{121    return Object::sendMessage<NS::Error*>(this, _MTL_PRIVATE_SEL(error));122}123124_MTL_INLINE NS::String* MTL::IOCommandBuffer::label() const125{126    return Object::sendMessage<NS::String*>(this, _MTL_PRIVATE_SEL(label));127}128129_MTL_INLINE void MTL::IOCommandBuffer::loadBuffer(const MTL::Buffer* buffer, NS::UInteger offset, NS::UInteger size, const MTL::IOFileHandle* sourceHandle, NS::UInteger sourceHandleOffset)130{131    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(loadBuffer_offset_size_sourceHandle_sourceHandleOffset_), buffer, offset, size, sourceHandle, sourceHandleOffset);132}133134_MTL_INLINE void MTL::IOCommandBuffer::loadBytes(const void* pointer, NS::UInteger size, const MTL::IOFileHandle* sourceHandle, NS::UInteger sourceHandleOffset)135{136    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(loadBytes_size_sourceHandle_sourceHandleOffset_), pointer, size, sourceHandle, sourceHandleOffset);137}138139_MTL_INLINE void MTL::IOCommandBuffer::loadTexture(const MTL::Texture* texture, NS::UInteger slice, NS::UInteger level, MTL::Size size, NS::UInteger sourceBytesPerRow, NS::UInteger sourceBytesPerImage, MTL::Origin destinationOrigin, const MTL::IOFileHandle* sourceHandle, NS::UInteger sourceHandleOffset)140{141    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(loadTexture_slice_level_size_sourceBytesPerRow_sourceBytesPerImage_destinationOrigin_sourceHandle_sourceHandleOffset_), texture, slice, level, size, sourceBytesPerRow, sourceBytesPerImage, destinationOrigin, sourceHandle, sourceHandleOffset);142}143144_MTL_INLINE void MTL::IOCommandBuffer::popDebugGroup()145{146    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(popDebugGroup));147}148149_MTL_INLINE void MTL::IOCommandBuffer::pushDebugGroup(const NS::String* string)150{151    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(pushDebugGroup_), string);152}153154_MTL_INLINE void MTL::IOCommandBuffer::setLabel(const NS::String* label)155{156    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setLabel_), label);157}158159_MTL_INLINE void MTL::IOCommandBuffer::signalEvent(const MTL::SharedEvent* event, uint64_t value)160{161    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(signalEvent_value_), event, value);162}163164_MTL_INLINE MTL::IOStatus MTL::IOCommandBuffer::status() const165{166    return Object::sendMessage<MTL::IOStatus>(this, _MTL_PRIVATE_SEL(status));167}168169_MTL_INLINE void MTL::IOCommandBuffer::tryCancel()170{171    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(tryCancel));172}173174_MTL_INLINE void MTL::IOCommandBuffer::wait(const MTL::SharedEvent* event, uint64_t value)175{176    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(waitForEvent_value_), event, value);177}178179_MTL_INLINE void MTL::IOCommandBuffer::waitUntilCompleted()180{181    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(waitUntilCompleted));182}183