@@ -503,17 +503,18 @@ class Runtime():
503
503
OpenCL devices found on the system.
504
504
'''
505
505
506
- _runtime = None
507
- _cpu_device = None
508
- _gpu_device = None
509
- _curr_device = None
506
+ _singleton = None
510
507
511
508
def __new__ (cls ):
512
- obj = cls ._runtime
509
+ obj = cls ._singleton
513
510
if obj is not None :
514
511
return obj
515
512
else :
516
513
obj = object .__new__ (cls )
514
+
515
+ cls ._lib = lib
516
+ cls ._ffi = ffi
517
+
517
518
ffiobj = ffi .new ("runtime_t *" )
518
519
retval = (lib .create_dp_runtime (ffiobj ))
519
520
if (retval ):
@@ -533,67 +534,68 @@ def __new__(cls):
533
534
_logger .warning ("No GPU device" )
534
535
535
536
cls ._curr_device = DeviceEnv (cls ._runtime [0 ][0 ].curr_env )
537
+ cls ._singleton = obj
536
538
537
539
return obj
538
540
539
541
def __init__ (self ):
540
542
pass
541
543
542
544
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 ))
545
547
if (retval ):
546
548
_raise_driver_error ("destroy_dp_runtime" , - 1 )
547
549
548
550
def has_cpu_device (self ):
549
551
''' Returns True is the system has an OpenCL driver for the CPU.'''
550
552
551
- return Runtime ._cpu_device is not None
553
+ return self ._cpu_device is not None
552
554
553
555
def has_gpu_device (self ):
554
556
''' Returns True is the system has an OpenCL driver for the GPU.'''
555
557
556
- return Runtime ._gpu_device is not None
558
+ return self ._gpu_device is not None
557
559
558
560
559
561
def get_cpu_device (self ):
560
562
''' Returns a cdata wrapper for the first available OpenCL
561
563
CPU context.
562
564
'''
563
565
564
- if (Runtime ._cpu_device is None ):
566
+ if (self ._cpu_device is None ):
565
567
_raise_device_not_found_error ("get_cpu_device" )
566
568
567
- return Runtime ._cpu_device
569
+ return self ._cpu_device
568
570
569
571
def get_gpu_device (self ):
570
572
''' Returns a cdata wrapper for the first available OpenCL
571
573
GPU context.
572
574
'''
573
575
574
- if (Runtime ._gpu_device is None ):
576
+ if (self ._gpu_device is None ):
575
577
_raise_device_not_found_error ("get_gpu_device" )
576
578
577
- return Runtime ._gpu_device
579
+ return self ._gpu_device
578
580
579
581
def get_current_device (self ):
580
582
''' Returns a cdata wrapper for the first available OpenCL
581
583
CPU context.
582
584
'''
583
585
584
- return Runtime ._curr_device
586
+ return self ._curr_device
585
587
586
588
def get_runtime_ptr (self ):
587
589
''' Returns a reference to the runtime object.
588
590
'''
589
591
590
- return Runtime ._runtime [0 ]
592
+ return self ._runtime [0 ]
591
593
592
594
def dump (self ):
593
595
''' Prints OpenCL metadata about the available devices and contexts.
594
596
'''
595
597
596
- retval = Runtime ._runtime [0 ].dump_fn (Runtime ._runtime [0 ])
598
+ retval = self ._runtime [0 ].dump_fn (Runtime ._runtime [0 ])
597
599
if retval == - 1 :
598
600
_raise_driver_error ("runtime dump_fn" , - 1 )
599
601
return retval
0 commit comments