29
29
namespace runtime {
30
30
namespace metal {
31
31
32
+ AutoReleasePoolWrapper& AutoReleasePoolWrapper::GetInstance () {
33
+ static AutoReleasePoolWrapper instance;
34
+ return instance;
35
+ }
36
+
32
37
MetalWorkspace* MetalWorkspace::Global () {
33
- @autoreleasepool {
34
- // NOTE: explicitly use new to avoid exit-time destruction of global state
35
- // Global state will be recycled by OS as the process exits.
36
- static MetalWorkspace* inst = new MetalWorkspace ();
37
- return inst;
38
- }
38
+ // NOTE: explicitly use new to avoid exit-time destruction of global state
39
+ // Global state will be recycled by OS as the process exits.
40
+ static MetalWorkspace* inst = new MetalWorkspace ();
41
+ return inst;
39
42
}
40
43
41
44
void MetalWorkspace::GetAttr (Device dev, DeviceAttrKind kind, TVMRetValue* rv) {
42
- @autoreleasepool {
45
+ AUTORELEASEPOOL {
43
46
this ->Init ();
44
47
size_t index = static_cast <size_t >(dev.device_id );
45
48
if (kind == kExist ) {
80
83
case kDriverVersion :
81
84
return ;
82
85
}
83
- }
86
+ };
84
87
}
85
88
86
89
static const char * kDummyKernel = R"A0B0(
@@ -161,7 +164,8 @@ int GetWarpSize(id<MTLDevice> dev) {
161
164
162
165
void * MetalWorkspace::AllocDataSpace (Device device, size_t nbytes, size_t alignment,
163
166
DLDataType type_hint) {
164
- @autoreleasepool {
167
+ id <MTLBuffer > buf;
168
+ AUTORELEASEPOOL {
165
169
this ->Init ();
166
170
id <MTLDevice > dev = GetDevice (device);
167
171
// GPU memory only
@@ -173,20 +177,20 @@ int GetWarpSize(id<MTLDevice> dev) {
173
177
storage_mode = MTLResourceStorageModeManaged;
174
178
#endif
175
179
*/
176
- id < MTLBuffer > buf = [dev newBufferWithLength: nbytes options: storage_mode];
180
+ buf = [dev newBufferWithLength: nbytes options: storage_mode];
177
181
ICHECK (buf != nil );
178
- return ( void *)(buf) ;
179
- }
182
+ } ;
183
+ return ( void *)(buf);
180
184
}
181
185
182
186
void MetalWorkspace::FreeDataSpace (Device dev, void * ptr) {
183
- @autoreleasepool {
187
+ AUTORELEASEPOOL {
184
188
// MTLBuffer PurgeableState should be set to empty before manual
185
189
// release in order to prevent memory leak
186
190
[(id <MTLBuffer >)ptr setPurgeableState: MTLPurgeableStateEmpty ];
187
191
// release the ptr.
188
192
CFRelease (ptr);
189
- }
193
+ };
190
194
}
191
195
192
196
Stream* GetStream (TVMStreamHandle stream, int device_id) {
@@ -199,7 +203,7 @@ int GetWarpSize(id<MTLDevice> dev) {
199
203
void MetalWorkspace::CopyDataFromTo (const void * from, size_t from_offset, void * to,
200
204
size_t to_offset, size_t size, Device dev_from, Device dev_to,
201
205
DLDataType type_hint, TVMStreamHandle stream) {
202
- @autoreleasepool {
206
+ AUTORELEASEPOOL {
203
207
this ->Init ();
204
208
Device dev = dev_from;
205
209
Stream* s = GetStream (stream, dev.device_id );
@@ -261,7 +265,7 @@ int GetWarpSize(id<MTLDevice> dev) {
261
265
LOG (FATAL) << " Expect copy from/to Metal or between Metal"
262
266
<< " , from=" << from_dev_type << " , to=" << to_dev_type;
263
267
}
264
- }
268
+ };
265
269
}
266
270
267
271
TVMStreamHandle MetalWorkspace::CreateStream (Device dev) {
@@ -276,7 +280,7 @@ int GetWarpSize(id<MTLDevice> dev) {
276
280
}
277
281
278
282
void MetalWorkspace::StreamSync (Device dev, TVMStreamHandle stream) {
279
- @autoreleasepool {
283
+ AUTORELEASEPOOL {
280
284
Stream* s = GetStream (stream, dev.device_id );
281
285
// commit an empty command buffer and wait until it completes.
282
286
id <MTLCommandBuffer > cb = s->GetCommandBuffer ();
@@ -285,7 +289,7 @@ int GetWarpSize(id<MTLDevice> dev) {
285
289
if (s->HasErrorHappened ()) {
286
290
LOG (FATAL) << " Error! Some problems on GPU happaned!" ;
287
291
}
288
- }
292
+ };
289
293
}
290
294
291
295
void MetalWorkspace::SetStream (Device dev, TVMStreamHandle stream) {
0 commit comments