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.9 KB · 212 lines cpp
Raw Blame History
1//-------------------------------------------------------------------------------------------------------------------------------------------------------------2//3// Metal/MTLIOCommandQueue.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"2728namespace MTL29{30class Buffer;31class IOCommandBuffer;32class IOCommandQueueDescriptor;33class IOScratchBuffer;34class IOScratchBufferAllocator;35_MTL_ENUM(NS::Integer, IOPriority) {36    IOPriorityHigh = 0,37    IOPriorityNormal = 1,38    IOPriorityLow = 2,39};4041_MTL_ENUM(NS::Integer, IOCommandQueueType) {42    IOCommandQueueTypeConcurrent = 0,43    IOCommandQueueTypeSerial = 1,44};4546_MTL_ENUM(NS::Integer, IOError) {47    IOErrorURLInvalid = 1,48    IOErrorInternal = 2,49};5051_MTL_CONST(NS::ErrorDomain, IOErrorDomain);52class IOCommandQueue : public NS::Referencing<IOCommandQueue>53{54public:55    IOCommandBuffer* commandBuffer();56    IOCommandBuffer* commandBufferWithUnretainedReferences();5758    void             enqueueBarrier();5960    NS::String*      label() const;61    void             setLabel(const NS::String* label);62};63class IOScratchBuffer : public NS::Referencing<IOScratchBuffer>64{65public:66    Buffer* buffer() const;67};68class IOScratchBufferAllocator : public NS::Referencing<IOScratchBufferAllocator>69{70public:71    IOScratchBuffer* newScratchBuffer(NS::UInteger minimumSize);72};73class IOCommandQueueDescriptor : public NS::Copying<IOCommandQueueDescriptor>74{75public:76    static IOCommandQueueDescriptor* alloc();7778    IOCommandQueueDescriptor*        init();7980    NS::UInteger                     maxCommandBufferCount() const;8182    NS::UInteger                     maxCommandsInFlight() const;8384    IOPriority                       priority() const;8586    IOScratchBufferAllocator*        scratchBufferAllocator() const;8788    void                             setMaxCommandBufferCount(NS::UInteger maxCommandBufferCount);8990    void                             setMaxCommandsInFlight(NS::UInteger maxCommandsInFlight);9192    void                             setPriority(MTL::IOPriority priority);9394    void                             setScratchBufferAllocator(const MTL::IOScratchBufferAllocator* scratchBufferAllocator);9596    void                             setType(MTL::IOCommandQueueType type);97    IOCommandQueueType               type() const;98};99class IOFileHandle : public NS::Referencing<IOFileHandle>100{101public:102    NS::String* label() const;103    void        setLabel(const NS::String* label);104};105106}107_MTL_PRIVATE_DEF_CONST(NS::ErrorDomain, IOErrorDomain);108_MTL_INLINE MTL::IOCommandBuffer* MTL::IOCommandQueue::commandBuffer()109{110    return Object::sendMessage<MTL::IOCommandBuffer*>(this, _MTL_PRIVATE_SEL(commandBuffer));111}112113_MTL_INLINE MTL::IOCommandBuffer* MTL::IOCommandQueue::commandBufferWithUnretainedReferences()114{115    return Object::sendMessage<MTL::IOCommandBuffer*>(this, _MTL_PRIVATE_SEL(commandBufferWithUnretainedReferences));116}117118_MTL_INLINE void MTL::IOCommandQueue::enqueueBarrier()119{120    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(enqueueBarrier));121}122123_MTL_INLINE NS::String* MTL::IOCommandQueue::label() const124{125    return Object::sendMessage<NS::String*>(this, _MTL_PRIVATE_SEL(label));126}127128_MTL_INLINE void MTL::IOCommandQueue::setLabel(const NS::String* label)129{130    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setLabel_), label);131}132133_MTL_INLINE MTL::Buffer* MTL::IOScratchBuffer::buffer() const134{135    return Object::sendMessage<MTL::Buffer*>(this, _MTL_PRIVATE_SEL(buffer));136}137138_MTL_INLINE MTL::IOScratchBuffer* MTL::IOScratchBufferAllocator::newScratchBuffer(NS::UInteger minimumSize)139{140    return Object::sendMessage<MTL::IOScratchBuffer*>(this, _MTL_PRIVATE_SEL(newScratchBufferWithMinimumSize_), minimumSize);141}142143_MTL_INLINE MTL::IOCommandQueueDescriptor* MTL::IOCommandQueueDescriptor::alloc()144{145    return NS::Object::alloc<MTL::IOCommandQueueDescriptor>(_MTL_PRIVATE_CLS(MTLIOCommandQueueDescriptor));146}147148_MTL_INLINE MTL::IOCommandQueueDescriptor* MTL::IOCommandQueueDescriptor::init()149{150    return NS::Object::init<MTL::IOCommandQueueDescriptor>();151}152153_MTL_INLINE NS::UInteger MTL::IOCommandQueueDescriptor::maxCommandBufferCount() const154{155    return Object::sendMessage<NS::UInteger>(this, _MTL_PRIVATE_SEL(maxCommandBufferCount));156}157158_MTL_INLINE NS::UInteger MTL::IOCommandQueueDescriptor::maxCommandsInFlight() const159{160    return Object::sendMessage<NS::UInteger>(this, _MTL_PRIVATE_SEL(maxCommandsInFlight));161}162163_MTL_INLINE MTL::IOPriority MTL::IOCommandQueueDescriptor::priority() const164{165    return Object::sendMessage<MTL::IOPriority>(this, _MTL_PRIVATE_SEL(priority));166}167168_MTL_INLINE MTL::IOScratchBufferAllocator* MTL::IOCommandQueueDescriptor::scratchBufferAllocator() const169{170    return Object::sendMessage<MTL::IOScratchBufferAllocator*>(this, _MTL_PRIVATE_SEL(scratchBufferAllocator));171}172173_MTL_INLINE void MTL::IOCommandQueueDescriptor::setMaxCommandBufferCount(NS::UInteger maxCommandBufferCount)174{175    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setMaxCommandBufferCount_), maxCommandBufferCount);176}177178_MTL_INLINE void MTL::IOCommandQueueDescriptor::setMaxCommandsInFlight(NS::UInteger maxCommandsInFlight)179{180    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setMaxCommandsInFlight_), maxCommandsInFlight);181}182183_MTL_INLINE void MTL::IOCommandQueueDescriptor::setPriority(MTL::IOPriority priority)184{185    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setPriority_), priority);186}187188_MTL_INLINE void MTL::IOCommandQueueDescriptor::setScratchBufferAllocator(const MTL::IOScratchBufferAllocator* scratchBufferAllocator)189{190    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setScratchBufferAllocator_), scratchBufferAllocator);191}192193_MTL_INLINE void MTL::IOCommandQueueDescriptor::setType(MTL::IOCommandQueueType type)194{195    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setType_), type);196}197198_MTL_INLINE MTL::IOCommandQueueType MTL::IOCommandQueueDescriptor::type() const199{200    return Object::sendMessage<MTL::IOCommandQueueType>(this, _MTL_PRIVATE_SEL(type));201}202203_MTL_INLINE NS::String* MTL::IOFileHandle::label() const204{205    return Object::sendMessage<NS::String*>(this, _MTL_PRIVATE_SEL(label));206}207208_MTL_INLINE void MTL::IOFileHandle::setLabel(const NS::String* label)209{210    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setLabel_), label);211}212