Skip to content

Commit 3d8f8c5

Browse files
committed
Take better care of heap allocation.
1 parent 5d6dae4 commit 3d8f8c5

File tree

3 files changed

+27
-11
lines changed

3 files changed

+27
-11
lines changed

01-MetalAdder/MetalAdder.cpp

+17-5
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ MetalAdder::MetalAdder(MTL::Device *device)
2929

3030
auto str = NS::String::string("add_arrays", NS::ASCIIStringEncoding);
3131
MTL::Function *addFunction = defaultLibrary->newFunction(str);
32+
defaultLibrary->release();
3233

3334
if (addFunction == nullptr)
3435
{
@@ -38,6 +39,7 @@ MetalAdder::MetalAdder(MTL::Device *device)
3839

3940
// Create a compute pipeline state object.
4041
_mAddFunctionPSO = _mDevice->newComputePipelineState(addFunction, &error);
42+
addFunction->release();
4143

4244
if (_mAddFunctionPSO == nullptr)
4345
{
@@ -55,16 +57,16 @@ MetalAdder::MetalAdder(MTL::Device *device)
5557
return;
5658
}
5759

60+
// Allocate three buffers to hold our initial data and the result.
61+
_mBufferA = _mDevice->newBuffer(bufferSize, MTL::ResourceStorageModeShared);
62+
_mBufferB = _mDevice->newBuffer(bufferSize, MTL::ResourceStorageModeShared);
63+
_mBufferResult = _mDevice->newBuffer(bufferSize, MTL::ResourceStorageModeShared);
64+
5865
prepareData();
5966
}
6067

6168
void MetalAdder::prepareData()
6269
{
63-
// Allocate three buffers to hold our initial data and the result.
64-
65-
_mBufferA = _mDevice->newBuffer(bufferSize, MTL::ResourceStorageModeShared);
66-
_mBufferB = _mDevice->newBuffer(bufferSize, MTL::ResourceStorageModeShared);
67-
_mBufferResult = _mDevice->newBuffer(bufferSize, MTL::ResourceStorageModeShared);
6870

6971
generateRandomFloatData(_mBufferA);
7072
generateRandomFloatData(_mBufferB);
@@ -143,3 +145,13 @@ void MetalAdder::verifyResults()
143145
}
144146
}
145147
}
148+
149+
MetalAdder::~MetalAdder()
150+
{
151+
_mBufferA->release();
152+
_mBufferB->release();
153+
_mBufferResult->release();
154+
155+
_mAddFunctionPSO->release();
156+
_mCommandQueue->release();
157+
}

01-MetalAdder/MetalAdder.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ class MetalAdder
3434
MTL::Buffer *_mBufferResult;
3535

3636
MetalAdder(MTL::Device *device);
37+
~MetalAdder();
38+
3739
void prepareData();
3840
void sendComputeCommand();
3941
void verifyResults();

01-MetalAdder/main.cpp

+8-6
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,15 @@ int main(int argc, char *argv[])
5353
<< std::endl;
5454

5555
// Verify serial code --------------------------------------------------------------
56-
// Let's randomize the data again, making sure that the result buffer starts out
57-
// incorrect
58-
adder->prepareData();
5956
// Get buffers pointers for CPU code. Using MTL::ResourceStorageModeShared should
6057
// make them accessible to both GPU and CPU, perfect!
6158
auto array_a = ((float *)adder->_mBufferA->contents());
6259
auto array_b = ((float *)adder->_mBufferB->contents());
6360
auto array_c = ((float *)adder->_mBufferResult->contents());
61+
62+
// Let's randomize the data again, making sure that the result buffer starts out
63+
// incorrect
64+
adder->prepareData();
6465
add_array_serial(array_a, array_b, array_c, arrayLength);
6566
adder->verifyResults();
6667

@@ -85,9 +86,6 @@ int main(int argc, char *argv[])
8586
// Verify OpenMP code --------------------------------------------------------------
8687
omp_set_num_threads(threads);
8788
adder->prepareData();
88-
array_a = ((float *)adder->_mBufferA->contents());
89-
array_b = ((float *)adder->_mBufferB->contents());
90-
array_c = ((float *)adder->_mBufferResult->contents());
9189
add_array_openmp(array_a, array_b, array_c, arrayLength);
9290
adder->verifyResults();
9391

@@ -105,6 +103,10 @@ int main(int argc, char *argv[])
105103
std::cout << array_mean << unit_name << " \t +/- " << array_std << unit_name << std::endl
106104
<< std::endl;
107105
}
106+
107+
delete[] durations;
108+
delete adder;
109+
device->release();
108110
}
109111

110112
template <class T>

0 commit comments

Comments
 (0)