MetalCompute 1.0
An API to make GPU compute calls easier
Loading...
Searching...
No Matches
MTLComputeGPU.hpp
Go to the documentation of this file.
6
7#pragma once
8
9#ifdef USING_MULTIPLE_CLASSES
10#error "Cannot include both MTLCompute.hpp and MTLComputeGPU.hpp"
11#endif
12
13#define USING_SINGLE_CLASS
14
15namespace MTLCompute {
16
17 template <typename T>
18 class GPU {
19
20 private:
21 MTL::Device *gpu;
23 bool kernelLoaded = false;
25
32 void checkloaded() {
33 if (!this->kernelLoaded) {
34 throw std::runtime_error("MTLCompute error: Kernel not loaded");
35 }
36 }
37
38 public:
39
50 GPU(std::string libname, std::string functionName) : kernel(), commandManager() {
51 this->gpu = MTL::CreateSystemDefaultDevice();
52 this->kernel = MTLCompute::Kernel(this->gpu, libname+".metallib");
53 this->kernel.useFunction(functionName);
54 this->commandManager = MTLCompute::CommandManager<T>(this->gpu, &this->kernel);
55 this->kernelLoaded = true;
56 }
57
65 this->gpu = MTL::CreateSystemDefaultDevice();
66 };
67
74 ~GPU() {
75 this->gpu->autorelease();
76 }
77
88 void loadKernel(std::string libname, std::string functionName) {
89 this->kernel = MTLCompute::Kernel(this->gpu, libname+".metallib");
90 this->kernel.useFunction(functionName);
91 this->commandManager = MTLCompute::CommandManager<T>(this->gpu, &this->kernel);
92 this->kernelLoaded = true;
93 }
94
105 void loadArray(std::vector<T> &array, int index) {
106 this->checkloaded();
108 buffer = array;
109 this->commandManager.loadBuffer(buffer, index);
110 }
111
122 void loadMatrix(std::vector<std::vector<T>> &matrix, int index) {
123 this->checkloaded();
124 MTLCompute::Texture<T> texture(gpu, matrix[0].size(), matrix.size());
125 texture = matrix;
126 this->commandManager.loadTexture(texture, index);
127 }
128
135 void runKernel() {
136 this->checkloaded();
137 this->commandManager.dispatch();
138 }
139
150 std::vector<T> getArray(int index) {
151 this->checkloaded();
152 MTLCompute::Buffer<T> buffer = commandManager.getBuffers()[index];
153 return buffer.getData();
154 }
155
166 std::vector<std::vector<T>> getMatrix(int index) {
167 this->checkloaded();
168 MTLCompute::Texture<T> texture = commandManager.getTextures()[index];
169 return texture.getData();
170 }
171
178 void reset() {
179 this->checkloaded();
180 this->commandManager.reset();
181 }
182
189 MTL::Device *getGPU() {
190 return this->gpu;
191 }
192
193 };
194
195}
Definition MTLComputeBuffer.hpp:10
std::vector< T > getData()
Get the data from the buffer as a vector.
Definition MTLComputeBuffer.hpp:181
Definition MTLComputeCommandManager.hpp:13
void loadBuffer(Buffer< T > buffer, int index)
Load a buffer into the CommandManager.
Definition MTLComputeCommandManager.hpp:75
void loadTexture(Texture< T > texture, int index)
Load a texture into the CommandManager.
Definition MTLComputeCommandManager.hpp:95
void reset()
reset the buffers and textures
Definition MTLComputeCommandManager.hpp:211
void dispatch()
Dispatch the kernel.
Definition MTLComputeCommandManager.hpp:118
Definition MTLComputeGPU.hpp:18
void runKernel()
Run the kernel.
Definition MTLComputeGPU.hpp:135
void checkloaded()
Check if the kernel has been loaded.
Definition MTLComputeGPU.hpp:32
GPU()
Default constructor for the GPU class.
Definition MTLComputeGPU.hpp:64
std::vector< std::vector< T > > getMatrix(int index)
Get a matrix from the GPU.
Definition MTLComputeGPU.hpp:166
void loadArray(std::vector< T > &array, int index)
Load an array into the GPU.
Definition MTLComputeGPU.hpp:105
void reset()
Reset the GPU.
Definition MTLComputeGPU.hpp:178
MTL::Device * gpu
The Metal device object.
Definition MTLComputeGPU.hpp:21
MTLCompute::CommandManager< T > commandManager
The MetalCompute command manager object.
Definition MTLComputeGPU.hpp:24
GPU(std::string libname, std::string functionName)
Constructor for the GPU class.
Definition MTLComputeGPU.hpp:50
MTLCompute::Kernel kernel
The MetalCompute kernel object.
Definition MTLComputeGPU.hpp:22
void loadMatrix(std::vector< std::vector< T > > &matrix, int index)
Load a matrix into the GPU.
Definition MTLComputeGPU.hpp:122
std::vector< T > getArray(int index)
Get an array from the GPU.
Definition MTLComputeGPU.hpp:150
void loadKernel(std::string libname, std::string functionName)
Load a metal library and function.
Definition MTLComputeGPU.hpp:88
~GPU()
Destructor for the GPU class.
Definition MTLComputeGPU.hpp:74
MTL::Device * getGPU()
Get the GPU device.
Definition MTLComputeGPU.hpp:189
bool kernelLoaded
Whether the kernel has been loaded.
Definition MTLComputeGPU.hpp:23
Definition MTLComputeKernel.hpp:7
void useFunction(const std::string &funcname)
Use a function in the library.
Definition MTLComputeKernel.hpp:101
Definition MTLComputeTexture.hpp:10
std::vector< std::vector< T > > getData()
Get the data from the texture as a vector.
Definition MTLComputeTexture.hpp:252
Definition MTLComputeBuffer.hpp:7