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/MTLHeap.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 "MTLAllocation.hpp"25#include "MTLDefines.hpp"26#include "MTLHeaderBridge.hpp"27#include "MTLPrivate.hpp"28#include "MTLResource.hpp"2930namespace MTL31{32class AccelerationStructure;33class AccelerationStructureDescriptor;34class Buffer;35class Device;36class HeapDescriptor;37class Texture;38class TextureDescriptor;39_MTL_ENUM(NS::Integer, HeapType) {40 HeapTypeAutomatic = 0,41 HeapTypePlacement = 1,42 HeapTypeSparse = 2,43};4445class HeapDescriptor : public NS::Copying<HeapDescriptor>46{47public:48 static HeapDescriptor* alloc();4950 CPUCacheMode cpuCacheMode() const;5152 HazardTrackingMode hazardTrackingMode() const;5354 HeapDescriptor* init();5556 SparsePageSize maxCompatiblePlacementSparsePageSize() const;5758 ResourceOptions resourceOptions() const;5960 void setCpuCacheMode(MTL::CPUCacheMode cpuCacheMode);6162 void setHazardTrackingMode(MTL::HazardTrackingMode hazardTrackingMode);6364 void setMaxCompatiblePlacementSparsePageSize(MTL::SparsePageSize maxCompatiblePlacementSparsePageSize);6566 void setResourceOptions(MTL::ResourceOptions resourceOptions);6768 void setSize(NS::UInteger size);6970 void setSparsePageSize(MTL::SparsePageSize sparsePageSize);7172 void setStorageMode(MTL::StorageMode storageMode);7374 void setType(MTL::HeapType type);7576 NS::UInteger size() const;77 SparsePageSize sparsePageSize() const;7879 StorageMode storageMode() const;8081 HeapType type() const;82};83class Heap : public NS::Referencing<Heap, Allocation>84{85public:86 CPUCacheMode cpuCacheMode() const;8788 NS::UInteger currentAllocatedSize() const;8990 Device* device() const;9192 HazardTrackingMode hazardTrackingMode() const;9394 NS::String* label() const;9596 NS::UInteger maxAvailableSize(NS::UInteger alignment);9798 AccelerationStructure* newAccelerationStructure(NS::UInteger size);99 AccelerationStructure* newAccelerationStructure(const MTL::AccelerationStructureDescriptor* descriptor);100 AccelerationStructure* newAccelerationStructure(NS::UInteger size, NS::UInteger offset);101 AccelerationStructure* newAccelerationStructure(const MTL::AccelerationStructureDescriptor* descriptor, NS::UInteger offset);102103 Buffer* newBuffer(NS::UInteger length, MTL::ResourceOptions options);104 Buffer* newBuffer(NS::UInteger length, MTL::ResourceOptions options, NS::UInteger offset);105106 Texture* newTexture(const MTL::TextureDescriptor* descriptor);107 Texture* newTexture(const MTL::TextureDescriptor* descriptor, NS::UInteger offset);108109 ResourceOptions resourceOptions() const;110111 void setLabel(const NS::String* label);112113 PurgeableState setPurgeableState(MTL::PurgeableState state);114115 NS::UInteger size() const;116117 StorageMode storageMode() const;118119 HeapType type() const;120121 NS::UInteger usedSize() const;122};123124}125_MTL_INLINE MTL::HeapDescriptor* MTL::HeapDescriptor::alloc()126{127 return NS::Object::alloc<MTL::HeapDescriptor>(_MTL_PRIVATE_CLS(MTLHeapDescriptor));128}129130_MTL_INLINE MTL::CPUCacheMode MTL::HeapDescriptor::cpuCacheMode() const131{132 return Object::sendMessage<MTL::CPUCacheMode>(this, _MTL_PRIVATE_SEL(cpuCacheMode));133}134135_MTL_INLINE MTL::HazardTrackingMode MTL::HeapDescriptor::hazardTrackingMode() const136{137 return Object::sendMessage<MTL::HazardTrackingMode>(this, _MTL_PRIVATE_SEL(hazardTrackingMode));138}139140_MTL_INLINE MTL::HeapDescriptor* MTL::HeapDescriptor::init()141{142 return NS::Object::init<MTL::HeapDescriptor>();143}144145_MTL_INLINE MTL::SparsePageSize MTL::HeapDescriptor::maxCompatiblePlacementSparsePageSize() const146{147 return Object::sendMessage<MTL::SparsePageSize>(this, _MTL_PRIVATE_SEL(maxCompatiblePlacementSparsePageSize));148}149150_MTL_INLINE MTL::ResourceOptions MTL::HeapDescriptor::resourceOptions() const151{152 return Object::sendMessage<MTL::ResourceOptions>(this, _MTL_PRIVATE_SEL(resourceOptions));153}154155_MTL_INLINE void MTL::HeapDescriptor::setCpuCacheMode(MTL::CPUCacheMode cpuCacheMode)156{157 Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setCpuCacheMode_), cpuCacheMode);158}159160_MTL_INLINE void MTL::HeapDescriptor::setHazardTrackingMode(MTL::HazardTrackingMode hazardTrackingMode)161{162 Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setHazardTrackingMode_), hazardTrackingMode);163}164165_MTL_INLINE void MTL::HeapDescriptor::setMaxCompatiblePlacementSparsePageSize(MTL::SparsePageSize maxCompatiblePlacementSparsePageSize)166{167 Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setMaxCompatiblePlacementSparsePageSize_), maxCompatiblePlacementSparsePageSize);168}169170_MTL_INLINE void MTL::HeapDescriptor::setResourceOptions(MTL::ResourceOptions resourceOptions)171{172 Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setResourceOptions_), resourceOptions);173}174175_MTL_INLINE void MTL::HeapDescriptor::setSize(NS::UInteger size)176{177 Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setSize_), size);178}179180_MTL_INLINE void MTL::HeapDescriptor::setSparsePageSize(MTL::SparsePageSize sparsePageSize)181{182 Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setSparsePageSize_), sparsePageSize);183}184185_MTL_INLINE void MTL::HeapDescriptor::setStorageMode(MTL::StorageMode storageMode)186{187 Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setStorageMode_), storageMode);188}189190_MTL_INLINE void MTL::HeapDescriptor::setType(MTL::HeapType type)191{192 Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setType_), type);193}194195_MTL_INLINE NS::UInteger MTL::HeapDescriptor::size() const196{197 return Object::sendMessage<NS::UInteger>(this, _MTL_PRIVATE_SEL(size));198}199200_MTL_INLINE MTL::SparsePageSize MTL::HeapDescriptor::sparsePageSize() const201{202 return Object::sendMessage<MTL::SparsePageSize>(this, _MTL_PRIVATE_SEL(sparsePageSize));203}204205_MTL_INLINE MTL::StorageMode MTL::HeapDescriptor::storageMode() const206{207 return Object::sendMessage<MTL::StorageMode>(this, _MTL_PRIVATE_SEL(storageMode));208}209210_MTL_INLINE MTL::HeapType MTL::HeapDescriptor::type() const211{212 return Object::sendMessage<MTL::HeapType>(this, _MTL_PRIVATE_SEL(type));213}214215_MTL_INLINE MTL::CPUCacheMode MTL::Heap::cpuCacheMode() const216{217 return Object::sendMessage<MTL::CPUCacheMode>(this, _MTL_PRIVATE_SEL(cpuCacheMode));218}219220_MTL_INLINE NS::UInteger MTL::Heap::currentAllocatedSize() const221{222 return Object::sendMessage<NS::UInteger>(this, _MTL_PRIVATE_SEL(currentAllocatedSize));223}224225_MTL_INLINE MTL::Device* MTL::Heap::device() const226{227 return Object::sendMessage<MTL::Device*>(this, _MTL_PRIVATE_SEL(device));228}229230_MTL_INLINE MTL::HazardTrackingMode MTL::Heap::hazardTrackingMode() const231{232 return Object::sendMessage<MTL::HazardTrackingMode>(this, _MTL_PRIVATE_SEL(hazardTrackingMode));233}234235_MTL_INLINE NS::String* MTL::Heap::label() const236{237 return Object::sendMessage<NS::String*>(this, _MTL_PRIVATE_SEL(label));238}239240_MTL_INLINE NS::UInteger MTL::Heap::maxAvailableSize(NS::UInteger alignment)241{242 return Object::sendMessage<NS::UInteger>(this, _MTL_PRIVATE_SEL(maxAvailableSizeWithAlignment_), alignment);243}244245_MTL_INLINE MTL::AccelerationStructure* MTL::Heap::newAccelerationStructure(NS::UInteger size)246{247 return Object::sendMessage<MTL::AccelerationStructure*>(this, _MTL_PRIVATE_SEL(newAccelerationStructureWithSize_), size);248}249250_MTL_INLINE MTL::AccelerationStructure* MTL::Heap::newAccelerationStructure(const MTL::AccelerationStructureDescriptor* descriptor)251{252 return Object::sendMessage<MTL::AccelerationStructure*>(this, _MTL_PRIVATE_SEL(newAccelerationStructureWithDescriptor_), descriptor);253}254255_MTL_INLINE MTL::AccelerationStructure* MTL::Heap::newAccelerationStructure(NS::UInteger size, NS::UInteger offset)256{257 return Object::sendMessage<MTL::AccelerationStructure*>(this, _MTL_PRIVATE_SEL(newAccelerationStructureWithSize_offset_), size, offset);258}259260_MTL_INLINE MTL::AccelerationStructure* MTL::Heap::newAccelerationStructure(const MTL::AccelerationStructureDescriptor* descriptor, NS::UInteger offset)261{262 return Object::sendMessage<MTL::AccelerationStructure*>(this, _MTL_PRIVATE_SEL(newAccelerationStructureWithDescriptor_offset_), descriptor, offset);263}264265_MTL_INLINE MTL::Buffer* MTL::Heap::newBuffer(NS::UInteger length, MTL::ResourceOptions options)266{267 return Object::sendMessage<MTL::Buffer*>(this, _MTL_PRIVATE_SEL(newBufferWithLength_options_), length, options);268}269270_MTL_INLINE MTL::Buffer* MTL::Heap::newBuffer(NS::UInteger length, MTL::ResourceOptions options, NS::UInteger offset)271{272 return Object::sendMessage<MTL::Buffer*>(this, _MTL_PRIVATE_SEL(newBufferWithLength_options_offset_), length, options, offset);273}274275_MTL_INLINE MTL::Texture* MTL::Heap::newTexture(const MTL::TextureDescriptor* descriptor)276{277 return Object::sendMessage<MTL::Texture*>(this, _MTL_PRIVATE_SEL(newTextureWithDescriptor_), descriptor);278}279280_MTL_INLINE MTL::Texture* MTL::Heap::newTexture(const MTL::TextureDescriptor* descriptor, NS::UInteger offset)281{282 return Object::sendMessage<MTL::Texture*>(this, _MTL_PRIVATE_SEL(newTextureWithDescriptor_offset_), descriptor, offset);283}284285_MTL_INLINE MTL::ResourceOptions MTL::Heap::resourceOptions() const286{287 return Object::sendMessage<MTL::ResourceOptions>(this, _MTL_PRIVATE_SEL(resourceOptions));288}289290_MTL_INLINE void MTL::Heap::setLabel(const NS::String* label)291{292 Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setLabel_), label);293}294295_MTL_INLINE MTL::PurgeableState MTL::Heap::setPurgeableState(MTL::PurgeableState state)296{297 return Object::sendMessage<MTL::PurgeableState>(this, _MTL_PRIVATE_SEL(setPurgeableState_), state);298}299300_MTL_INLINE NS::UInteger MTL::Heap::size() const301{302 return Object::sendMessage<NS::UInteger>(this, _MTL_PRIVATE_SEL(size));303}304305_MTL_INLINE MTL::StorageMode MTL::Heap::storageMode() const306{307 return Object::sendMessage<MTL::StorageMode>(this, _MTL_PRIVATE_SEL(storageMode));308}309310_MTL_INLINE MTL::HeapType MTL::Heap::type() const311{312 return Object::sendMessage<MTL::HeapType>(this, _MTL_PRIVATE_SEL(type));313}314315_MTL_INLINE NS::UInteger MTL::Heap::usedSize() const316{317 return Object::sendMessage<NS::UInteger>(this, _MTL_PRIVATE_SEL(usedSize));318}319