@@ -131,7 +131,7 @@ struct Kernel {
131
131
size_t numInputs;
132
132
WGPUCommandBuffer commandBuffer;
133
133
WGPUBuffer readbackBuffer;
134
- CallbackDataDyn callbackData;
134
+ CallbackDataDyn callbackData;
135
135
std::promise<void > promise;
136
136
std::future<void > future;
137
137
};
@@ -174,26 +174,27 @@ bool operator<(const Kernel &lhs, const Kernel &rhs) {
174
174
return lhs.commandBuffer < rhs.commandBuffer ;
175
175
}
176
176
177
- void FreeKernel (Kernel* op) {
177
+ void FreeKernel (Kernel * op) {
178
178
log (kDefLog , kInfo , " Freeing kernel" );
179
+ // TODO(avh): nullptr is insufficient check for freeable resources
179
180
if (op->commandBuffer != nullptr ) {
180
- // wgpuCommandBufferRelease(op->commandBuffer);
181
+ wgpuCommandBufferRelease (op->commandBuffer );
181
182
}
182
183
if (op->readbackBuffer != nullptr ) {
183
- // wgpuBufferRelease(op->readbackBuffer);
184
+ wgpuBufferRelease (op->readbackBuffer );
184
185
}
185
186
if (op->callbackData .buffer != nullptr ) {
186
- // wgpuBufferRelease(op->callbackData.buffer);
187
+ wgpuBufferRelease (op->callbackData .buffer );
187
188
}
188
189
}
189
190
190
- void FreeMultiKernel (MultiKernel & pipeline) {
191
+ void FreeMultiKernel (MultiKernel * pipeline) {
191
192
log (kDefLog , kInfo , " Freeing multi kernel" );
192
- if (pipeline. commandBuffer ) {
193
- wgpuCommandBufferRelease (pipeline. commandBuffer );
193
+ if (pipeline-> commandBuffer ) {
194
+ // wgpuCommandBufferRelease(pipeline-> commandBuffer);
194
195
}
195
- if (pipeline. readbackBuffer ) {
196
- wgpuBufferRelease (pipeline. readbackBuffer );
196
+ if (pipeline-> readbackBuffer ) {
197
+ // wgpuBufferRelease(pipeline-> readbackBuffer);
197
198
}
198
199
}
199
200
@@ -202,7 +203,16 @@ struct KernelPool {
202
203
GPUContext *ctx;
203
204
std::set<Kernel *> data;
204
205
std::set<MultiKernel *> multiData;
205
- ~KernelPool ();
206
+ ~KernelPool () {
207
+ for (auto kernelPtr : data) {
208
+ FreeKernel (kernelPtr);
209
+ }
210
+ data.clear ();
211
+ for (MultiKernel *multiKernelPtr : multiData) {
212
+ FreeMultiKernel (multiKernelPtr);
213
+ }
214
+ multiData.clear ();
215
+ }
206
216
};
207
217
208
218
struct GPUContext {
@@ -212,11 +222,8 @@ struct GPUContext {
212
222
WGPUQueue queue;
213
223
TensorPool pool = TensorPool(this );
214
224
KernelPool kernelPool = KernelPool(this );
215
- /*
216
225
~GPUContext () {
217
226
log (kDefLog , kInfo , " Destroying context" );
218
- pool.~TensorPool();
219
- kernelPool.~KernelPool();
220
227
if (queue) {
221
228
wgpuQueueRelease (queue);
222
229
wgpuInstanceProcessEvents (instance);
@@ -240,29 +247,10 @@ struct GPUContext {
240
247
} else {
241
248
log (kDefLog , kWarn , " Instance is null" );
242
249
}
250
+ log (kDefLog , kInfo , " Destroyed context" );
243
251
}
244
- */
245
252
};
246
253
247
- KernelPool::~KernelPool () {
248
- for (auto kernelPtr : data) {
249
- FreeKernel (kernelPtr);
250
- // data.erase(kernelPtr);
251
- }
252
- /*
253
- for (MultiKernel *multiKernelPtr : multiData) {
254
- while (multiKernelPtr->future.wait_for(std::chrono::seconds(0)) !=
255
- std::future_status::ready) {
256
- log(kDefLog, kWarn,
257
- "MultiKernel future not ready, waiting before freeing");
258
- wgpuInstanceProcessEvents(ctx->instance);
259
- }
260
- FreeMultiKernel(*multiKernelPtr);
261
- multiData.erase(multiKernelPtr);
262
- }
263
- */
264
- }
265
-
266
254
/* Tensor factory function */
267
255
GPUTensor CreateTensor (TensorPool &pool, WGPUDevice &device, const Shape &shape,
268
256
NumType dtype,
@@ -380,9 +368,9 @@ void showDeviceInfo(WGPUAdapter &adapter) {
380
368
}
381
369
382
370
GPUContext CreateContext (bool quietLogging = true ,
383
- const WGPUInstanceDescriptor &desc = {},
384
- const WGPURequestAdapterOptions &adapterOpts = {},
385
- WGPUDeviceDescriptor devDescriptor = {}) {
371
+ const WGPUInstanceDescriptor &desc = {},
372
+ const WGPURequestAdapterOptions &adapterOpts = {},
373
+ WGPUDeviceDescriptor devDescriptor = {}) {
386
374
if (quietLogging) {
387
375
kDefLog .level = kError ;
388
376
}
@@ -732,8 +720,7 @@ Kernel CreateKernel(GPUContext &ctx, const ShaderCode &shader,
732
720
}
733
721
734
722
log (kDefLog , kInfo , " Initializing callbackData" );
735
- op.callbackData =
736
- {op.readbackBuffer , op.outputSize , nullptr , &op.promise };
723
+ op.callbackData = {op.readbackBuffer , op.outputSize , nullptr , &op.promise };
737
724
738
725
ctx.kernelPool .data .insert (&op);
739
726
0 commit comments