cuda_cffi/_cusolver_cffi_autogen.py
def build_func_body(func_name, arg_dict, return_type):
    """ generate python_wrapper function body

    Note: this code is highly specific to the particulars of the cuSOLVER
    library

    """
    body = ""
    arg_list = ""

    # the following are pointers to scalar outputs
    # Note: pBufferSize was renamed pBufferSizeInBytes in v6.5
    scalar_ptr_outputs = ['nnzTotalDevHostPtr',
                          'pBufferSize',
                          'pBufferSizeInBytes',
                          'resultDevHostPtr']

    is_creator = 'Create' in func_name
    is_getter = 'Get' in func_name

    if return_type == 'cusolverStatus_t' and not (is_creator or is_getter):
        is_return = False
    else:
        is_return = True

    # else:
    return_str = ''
    for k, v in arg_dict.items():

        """
        set some flags based on the name/type of the argument
        will use these flags to determine whether and how to call ffi.new or
        ffi.cast on each variable
        """
        is_ptr = '*' in v
        is_cusolver_type = '_t' in v
        is_cusolver_ptr = is_ptr and is_cusolver_type
        is_output_scalar = k in scalar_ptr_outputs
        if k in ['alpha', 'beta']:
            is_scalar = True
        else:
            is_scalar = False
        if is_getter:
            is_gpu_array = False
        else:
            is_gpu_array = is_ptr and (not is_cusolver_ptr) and (not is_scalar)
        if 'Complex' in v:
            is_complex = True
        else:
            is_complex = False

        # convert variable to appropriate type for the FFI
        if is_output_scalar:
            # for scalar outputs make a new pointer
            body += "%s = ffi.cast('%s', %s)\n" % (k, v, k)
        elif is_getter and is_ptr and (return_type == 'cusolverStatus_t'):
            # any pointers in cusolverGet* are new outputs to be created
            body += "%s = ffi.new('%s')\n" % (k, v)
        elif is_gpu_array:
            # pass pointer to GPU array data (use either .ptr or .gpudata)
            body += "%s = ffi.cast('%s', %s.ptr)\n" % (k, v, k)
        elif is_cusolver_ptr:
            if is_creator:
                # generate custom cusolver type
                body += "%s = ffi.new('%s')\n" % (k, v)
            else:
                # cast to the custom cusolver type
                body += "%s = ffi.cast('%s', %s)\n" % (k, v, k)
        elif is_ptr and is_scalar:
            # create new pointer, with value initialized to scalar
            if is_complex:
                # complex case is a bit tricky.  requires ffi.buffer
                body += "%sffi = ffi.new('%s')\n" % (k, v)
                if 'cusolverC' in func_name:
                    body += "ffi.buffer(%sffi)[:] = \
                        np.complex64(%s).tostring()\n" % (k, k)
                elif 'cusolverZ' in func_name:
                    body += "ffi.buffer(%sffi)[:] = \
                        np.complex128(%s).tostring()\n" % (k, k)
            else:
                body += "%s = ffi.new('%s', %s)\n" % (k, v, k)
        elif is_ptr or v == 'cudaStream_t':
            # case non-scalar pointer to appropriate type
            body += "%s = ffi.cast('%s', %s)\n" % (k, v, k)
        else:
            # don't need explicit cast for plain int, float, etc
            pass

        # build the list of arguments to pass to the API
        if is_ptr and is_scalar and is_complex:
            # take into account modified argument name for complex scalars
            arg_list += "%sffi, " % k
        else:
            arg_list += "%s, " % k

    # add the function call and optionally return the result
    last_key = k
    arg_list = arg_list[:-2]  # remove trailing ", "
    if is_getter and return_type != 'cusolverStatus_t':
        body += "return ffi_lib.%s(%s)\n" % (func_name, arg_list)
    else:
        # check cusolverStatus_t state before returning
        call_str = "status = ffi_lib.%s(%s)\n" % (func_name, arg_list)
        body += split_line(call_str, break_pattern=', ', nmax=76)
        body += "cusolverCheckStatus(status)\n"
        if is_return:
            # len(arg_dict) == 2) is to avoid return for cusolverGetLevelInfo
            if is_creator or (is_getter and (len(arg_dict) == 2)):
                body += "return %s[0]\n" % last_key
            else:
                body += "#TODO: return the appropriate result"
    body += '\n\n'
    return reindent(body, numSpaces=4, lstrip=False)
 generate python_wrapper function body

    Note: this code is highly specific to the particulars of the cuSOLVER
    library

    
generate python_wrapper function body
Note: this code is highly specific to the particulars of the cuSOLVER
library
generate python_wrapper function body Note : this code is highly specific to the particulars of the cuSOLVER library
def build_func_body(func_name, arg_dict, return_type):
    body = ""
    arg_list = ""
    scalar_ptr_outputs = ['nnzTotalDevHostPtr',
                          'pBufferSize',
                          'pBufferSizeInBytes',
                          'resultDevHostPtr']
    is_creator = 'Create' in func_name
    is_getter = 'Get' in func_name
    if return_type == 'cusolverStatus_t' and not (is_creator or is_getter):
        is_return = False
    else:
        is_return = True
    return_str = ''
    for k, v in arg_dict.items():
        is_ptr = '*' in v
        is_cusolver_type = '_t' in v
        is_cusolver_ptr = is_ptr and is_cusolver_type
        is_output_scalar = k in scalar_ptr_outputs
        if k in ['alpha', 'beta']:
            is_scalar = True
        else:
            is_scalar = False
        if is_getter:
            is_gpu_array = False
        else:
            is_gpu_array = is_ptr and (not is_cusolver_ptr) and (not is_scalar)
        if 'Complex' in v:
            is_complex = True
        else:
            is_complex = False
        if is_output_scalar:
            body += "%s = ffi.cast('%s', %s)\n" % (k, v, k)
        elif is_getter and is_ptr and (return_type == 'cusolverStatus_t'):
            body += "%s = ffi.new('%s')\n" % (k, v)
        elif is_gpu_array:
            body += "%s = ffi.cast('%s', %s.ptr)\n" % (k, v, k)
        elif is_cusolver_ptr:
            if is_creator:
                body += "%s = ffi.new('%s')\n" % (k, v)
            else:
                body += "%s = ffi.cast('%s', %s)\n" % (k, v, k)
        elif is_ptr and is_scalar:
            if is_complex:
                body += "%sffi = ffi.new('%s')\n" % (k, v)
                if 'cusolverC' in func_name:
                    body += "ffi.buffer(%sffi)[:] = \
                        np.complex64(%s).tostring()\n" % (k, k)
                elif 'cusolverZ' in func_name:
                    body += "ffi.buffer(%sffi)[:] = \
                        np.complex128(%s).tostring()\n" % (k, k)
            else:
                body += "%s = ffi.new('%s', %s)\n" % (k, v, k)
        elif is_ptr or v == 'cudaStream_t':
            body += "%s = ffi.cast('%s', %s)\n" % (k, v, k)
        else:
            pass
        if is_ptr and is_scalar and is_complex:
            arg_list += "%sffi, " % k
        else:
            arg_list += "%s, " % k
    last_key = k
    arg_list = arg_list[:-2]  
    if is_getter and return_type != 'cusolverStatus_t':
        body += "return ffi_lib.%s(%s)\n" % (func_name, arg_list)
    else:
        call_str = "status = ffi_lib.%s(%s)\n" % (func_name, arg_list)
        body += split_line(call_str, break_pattern=', ', nmax=76)
        body += "cusolverCheckStatus(status)\n"
        if is_return:
            if is_creator or (is_getter and (len(arg_dict) == 2)):
                body += "return %s[0]\n" % last_key
            else:
                body += "#TODO: return the appropriate result"
    body += '\n\n'
    return reindent(body, numSpaces=4, lstrip=False)
def build_func_body ( func_name , arg_dict , return_type ) : body = "" arg_list = "" scalar_ptr_outputs = [ 'nnzTotalDevHostPtr' , 'pBufferSize' , 'pBufferSizeInBytes' , 'resultDevHostPtr' ] is_creator = 'Create' in func_name is_getter = 'Get' in func_name if return_type == 'cusolverStatus_t' and not ( is_creator or is_getter ) : is_return = False else : is_return = True return_str = '' for k , v in arg_dict . items ( ) : """
        set some flags based on the name/type of the argument
        will use these flags to determine whether and how to call ffi.new or
        ffi.cast on each variable
        """ is_ptr = '*' in v is_cusolver_type = '_t' in v is_cusolver_ptr = is_ptr and is_cusolver_type is_output_scalar = k in scalar_ptr_outputs if k in [ 'alpha' , 'beta' ] : is_scalar = True else : is_scalar = False if is_getter : is_gpu_array = False else : is_gpu_array = is_ptr and ( not is_cusolver_ptr ) and ( not is_scalar ) if 'Complex' in v : is_complex = True else : is_complex = False if is_output_scalar : body += "%s = ffi.cast('%s', %s)\n" % ( k , v , k ) elif is_getter and is_ptr and ( return_type == 'cusolverStatus_t' ) : body += "%s = ffi.new('%s')\n" % ( k , v ) elif is_gpu_array : body += "%s = ffi.cast('%s', %s.ptr)\n" % ( k , v , k ) elif is_cusolver_ptr : if is_creator : body += "%s = ffi.new('%s')\n" % ( k , v ) else : body += "%s = ffi.cast('%s', %s)\n" % ( k , v , k ) elif is_ptr and is_scalar : if is_complex : body += "%sffi = ffi.new('%s')\n" % ( k , v ) if 'cusolverC' in func_name : body += "ffi.buffer(%sffi)[:] = \
                        np.complex64(%s).tostring()\n" % ( k , k ) elif 'cusolverZ' in func_name : body += "ffi.buffer(%sffi)[:] = \
                        np.complex128(%s).tostring()\n" % ( k , k ) else : body += "%s = ffi.new('%s', %s)\n" % ( k , v , k ) elif is_ptr or v == 'cudaStream_t' : body += "%s = ffi.cast('%s', %s)\n" % ( k , v , k ) else : pass if is_ptr and is_scalar and is_complex : arg_list += "%sffi, " % k else : arg_list += "%s, " % k last_key = k arg_list = arg_list [ : - 2 ] if is_getter and return_type != 'cusolverStatus_t' : body += "return ffi_lib.%s(%s)\n" % ( func_name , arg_list ) else : call_str = "status = ffi_lib.%s(%s)\n" % ( func_name , arg_list ) body += split_line ( call_str , break_pattern = ', ' , nmax = 76 ) body += "cusolverCheckStatus(status)\n" if is_return : if is_creator or ( is_getter and ( len ( arg_dict ) == 2 ) ) : body += "return %s[0]\n" % last_key else : body += "#TODO: return the appropriate result" body += '\n\n' return reindent ( body , numSpaces = 4 , lstrip = False )
generate python_wrapper function body
Note: this code is highly specific to the particulars of the cuSOLVER
library
generate python_wrapper function body Note : this code is highly specific to the particulars of the cuSOLVER library
""" generate python_wrapper function body

    Note: this code is highly specific to the particulars of the cuSOLVER
    library

    """ # the following are pointers to scalar outputs # Note: pBufferSize was renamed pBufferSizeInBytes in v6.5 # else: """
        set some flags based on the name/type of the argument
        will use these flags to determine whether and how to call ffi.new or
        ffi.cast on each variable
        """ # convert variable to appropriate type for the FFI # for scalar outputs make a new pointer # any pointers in cusolverGet* are new outputs to be created # pass pointer to GPU array data (use either .ptr or .gpudata) # generate custom cusolver type # cast to the custom cusolver type # create new pointer, with value initialized to scalar # complex case is a bit tricky.  requires ffi.buffer # case non-scalar pointer to appropriate type # don't need explicit cast for plain int, float, etc # build the list of arguments to pass to the API # take into account modified argument name for complex scalars # add the function call and optionally return the result # remove trailing ", " # check cusolverStatus_t state before returning # len(arg_dict) == 2) is to avoid return for cusolverGetLevelInfo
func_name
arg_dict
return_type