Skip to content

Commit f372028

Browse files
authored
Fixes the implementation of the oclrv.Runtime singleton and ensures lib, ffi stay around till Runtime is alive. (#68)
1 parent 8c0a9d0 commit f372028

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

dpctl/ocldrv.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -503,17 +503,18 @@ class Runtime():
503503
OpenCL devices found on the system.
504504
'''
505505

506-
_runtime = None
507-
_cpu_device = None
508-
_gpu_device = None
509-
_curr_device = None
506+
_singleton = None
510507

511508
def __new__(cls):
512-
obj = cls._runtime
509+
obj = cls._singleton
513510
if obj is not None:
514511
return obj
515512
else:
516513
obj = object.__new__(cls)
514+
515+
cls._lib = lib
516+
cls._ffi = ffi
517+
517518
ffiobj = ffi.new("runtime_t *")
518519
retval = (lib.create_dp_runtime(ffiobj))
519520
if(retval):
@@ -533,67 +534,68 @@ def __new__(cls):
533534
_logger.warning("No GPU device")
534535

535536
cls._curr_device = DeviceEnv(cls._runtime[0][0].curr_env)
537+
cls._singleton = obj
536538

537539
return obj
538540

539541
def __init__(self):
540542
pass
541543

542544
def __del__(self):
543-
if Runtime._runtime:
544-
retval = (lib.destroy_dp_runtime(Runtime._runtime))
545+
if self._runtime:
546+
retval = (self._lib.destroy_dp_runtime(self._runtime))
545547
if(retval):
546548
_raise_driver_error("destroy_dp_runtime", -1)
547549

548550
def has_cpu_device(self):
549551
''' Returns True is the system has an OpenCL driver for the CPU.'''
550552

551-
return Runtime._cpu_device is not None
553+
return self._cpu_device is not None
552554

553555
def has_gpu_device(self):
554556
''' Returns True is the system has an OpenCL driver for the GPU.'''
555557

556-
return Runtime._gpu_device is not None
558+
return self._gpu_device is not None
557559

558560

559561
def get_cpu_device(self):
560562
''' Returns a cdata wrapper for the first available OpenCL
561563
CPU context.
562564
'''
563565

564-
if(Runtime._cpu_device is None):
566+
if(self._cpu_device is None):
565567
_raise_device_not_found_error("get_cpu_device")
566568

567-
return Runtime._cpu_device
569+
return self._cpu_device
568570

569571
def get_gpu_device(self):
570572
''' Returns a cdata wrapper for the first available OpenCL
571573
GPU context.
572574
'''
573575

574-
if(Runtime._gpu_device is None):
576+
if(self._gpu_device is None):
575577
_raise_device_not_found_error("get_gpu_device")
576578

577-
return Runtime._gpu_device
579+
return self._gpu_device
578580

579581
def get_current_device(self):
580582
''' Returns a cdata wrapper for the first available OpenCL
581583
CPU context.
582584
'''
583585

584-
return Runtime._curr_device
586+
return self._curr_device
585587

586588
def get_runtime_ptr(self):
587589
''' Returns a reference to the runtime object.
588590
'''
589591

590-
return Runtime._runtime[0]
592+
return self._runtime[0]
591593

592594
def dump(self):
593595
''' Prints OpenCL metadata about the available devices and contexts.
594596
'''
595597

596-
retval = Runtime._runtime[0].dump_fn(Runtime._runtime[0])
598+
retval = self._runtime[0].dump_fn(Runtime._runtime[0])
597599
if retval == -1:
598600
_raise_driver_error("runtime dump_fn", -1)
599601
return retval

0 commit comments

Comments
 (0)