Source

PyCULA / PyCULA / my_init.py

# -----------------------------------------------------------------------
# PyCULA
# by Louis Theran and Garrett Wright
# Mathematics Department, Temple University
# All rights reserved
# Free for non-commercial use with attribution to:
#       "Louis Theran and Garrett Wright, Temple University"
# -----------------------------------------------------------------------

import atexit, ctypes
import cula
import pycuda.driver as cuda
from pycuda.tools import make_default_context
import PyCUsp



#----------------------------------------------------------
# PyCUDA init stuff
#----------------------------------------------------------

_PyCUDAContext = None
_PyCUDADevice = None

def pycuda_init():
    """Initializes PyCUDA and handles context."""
    global _PyCUDAContext
    global _PyCUDADevice
    
    cuda.init()
    _PyCUDAContext = make_default_context()
    _PyCUDADevice = _PyCUDAContext.get_device()
    atexit.register(_PyCUDAContext.detach)
    
# ------------------------------

def pycuda_init_once():
    """Checks if PyCUDA is initiliazed, and initilizes if needed."""
    if _PyCUDAContext is None:
        pycuda_init()


#---end-pycuda-init-stuff-----------------------------------




#-----------------------------------------------------------
# CULA init stuff
# -----------------------------------------------------------

# CULA init/shutdown -------------------------
#_CULAInitialized = False # moved to cula.py
_dev=None
def cula_init_check_on_Dev():
    """Checks whether CULA is initialized on devive number 'dev', and if not, initializes"""
    global _CULAInitialized
    global _dev
    if not _CULAInitialized:
        if not _dev==None:
    	    devNumber=_dev
        else:
	        devNumber=0
        # set cula to use cuda device given by dev
        cula.libcula.culaSelectDevice(ctypes.c_int(devNumber))
        cula.culaInitialize()
#---end-CULA-init-stuff



def mixed_init(scikits_cuda_cublas=False,cusp=False):
    """
    
    Inits driver and runtime libs for transperant mixed mode cuda enviornment.
    
    mixed_init(scikits_cuda_cublas=False,cusp=False)
    
    if scikits_cuda_cublas=True:
        initializes scikits.cuda.cublas,
        and shutsdown upon exit
    
    if cusp=True:
        initializes cusp,
        returns cusp handle,
        and shutsdown upon exit
        
    Note: This is a change from earlier PyCULA versions where all libraries are loaded by default,
        this is intended to make PyCULA easier to install, more robust, and reduce unneeded module loading.
    
    """
    import pycuda.autoinit #init driver bound to this host thread
    if scikits_cuda_cublas:
        import scikits.cuda.cublas as cublas
        cublas.cublasInit() #init cublas bound to this driver context
    cula.culaInitialize() # init cula bount to this driver context
    atexit.register(cula.culaShutdown) #shutdown on exit
    if scikits_cuda_cublas:
        atexit.register(cublas.cublasInit) #shutdown on exit
    handle=None
    if cusp:
        handle = PyCUsp.cusp_init()
    global _MixedInitialized
    _MixedInitialized=True
    return handle
    
def lazy_init():
    if not cula.is_cula_initialized():
        cula.culaInitialize()
    atexit.register(cula.culaShutdown)

# execute
if __name__ == "__main__":
    mixed_init()