cutil

package module
v0.0.0-...-cd61820 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 1, 2020 License: MIT Imports: 5 Imported by: 38

README

cutil

Helpful interfaces for interfacing go packages that use cgo.

What this does

This is a small package that contains types, methods and interfaces I seemed to use over and over again to allow go packages that contained c libraries interface with each other easier.

This is not a fix all to go's unexported use of c types. Each package that uses cgo will still have to do type casting where it is needed.

Example of use case from GoCudnn

A brief cudnnActivationForward takes a void pointer for alpha and beta, but they need to be the same type(for the most part) as the tensors x and y. xD and yD are both tensor descriptors that contain information on the memory of x and y. The function cscalarbydatatype takes a float64 (alpha and beta) and converts it to a CScalar (a1 and b) of the type that is compatable with cudnnActivationForward(). x and y where allocated by another package, but where able to be used by GoCudnn through the Mem interface.

func (a *ActivationD) Forward(handle *Handle,
                              alpha float64,
                              xD *TensorD, x cutil.Mem,
                              beta float64,
                              yD *TensorD, y cutil.Mem),
                              error {

                              a1 := cscalarbydatatype(yD.dtype, alpha)
                              b := cscalarbydatatype(yD.dtype, beta)

        return Status(
               C.cudnnActivationForward(handle.x,
                                       a.descriptor,
                                       a1.CPtr(),
                                       xD.descriptor,
                                       x.Ptr(),
                                       b.CPtr(),
                                       yD.descriptor,
                                       y.Ptr(),
                                       )).error("ActivationForward")
}

func cscalarbydatatype(dtype DataType, num float64) ctype.CScalar {
    var x DataType
    switch dtype {
    case x.Double():
        return cutil.CDouble(num)
    case x.Float():
        return cutil.CFloat(num)
    case x.Half():
        y := float32(num)
        return cutil.CFloat(y)
    default:
        return nil
    }

}

Note

I will add to this library where it is needed.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CScalartoFloat64

func CScalartoFloat64(x CScalar) float64

CScalartoFloat64 changes a CScalar to a float64 value so it could be read or debugging.

Types

type CBool

type CBool C.bool

CBool is a wrapper for C.bool it is in the stdbool.h header

func (CBool) CPtr

func (c CBool) CPtr() unsafe.Pointer

CPtr retunrs an unsafe pointer for CBool

func (CBool) SIB

func (c CBool) SIB() uint

SIB returns the number of bytes the CScalar has

type CChar

type CChar C.char

CChar is a signed char

func (CChar) CPtr

func (c CChar) CPtr() unsafe.Pointer

CPtr retunrs an unsafe pointer for CInt8

func (CChar) SIB

func (c CChar) SIB() uint

SIB returns the number of bytes the CScalar has

type CDouble

type CDouble C.double

CDouble is a double in C

func (CDouble) CPtr

func (d CDouble) CPtr() unsafe.Pointer

CPtr returns an unsafe pointer of the double

func (CDouble) SIB

func (d CDouble) SIB() uint

SIB returns the number of bytes the CScalar has

type CFloat

type CFloat C.float

CFloat is a float in C

func (CFloat) CPtr

func (f CFloat) CPtr() unsafe.Pointer

CPtr returns an unsafe pointer of the float

func (CFloat) SIB

func (f CFloat) SIB() uint

SIB returns the number of bytes the CScalar has

type CHalf

type CHalf C.ushort

CHalf is a half precision

func (CHalf) CPtr

func (f CHalf) CPtr() unsafe.Pointer

CPtr returns an unsafe pointer of the half

func (CHalf) SIB

func (f CHalf) SIB() uint

SIB returns the number of bytes the CScalar has as an sizeT

type CInt

type CInt C.int

CInt is a int in C

func (CInt) CPtr

func (i CInt) CPtr() unsafe.Pointer

CPtr returns an unsafe pointer of the int

func (CInt) SIB

func (i CInt) SIB() uint

SIB returns the number of bytes the CScalar has

type CScalar

type CScalar interface {
	CPtr() unsafe.Pointer
	SIB() uint
}

CScalar is used for scalar multiplications with cudnn. They have to be Ctypes. It could have easily been called voider

func CScalarConversion

func CScalarConversion(gotype interface{}) CScalar

CScalarConversion takes a go type and converts it to a CScalar interface. golang type int and int32 will both be converted to a CInt type. If a go type is not supported then it will return a nil. Current support is float64,float32,int, int32, int8,uint32, uint, uint8(byte)

type CSizet

type CSizet C.size_t

CSizet is a wrapper for C.size_t

func (CSizet) CPtr

func (c CSizet) CPtr() unsafe.Pointer

CPtr retunrs an unsafe pointer for CSizet

func (CSizet) SIB

func (c CSizet) SIB() uint

SIB returns the number of bytes the CScalar has

type CUChar

type CUChar C.uchar

CUChar is a C.uchar

func (CUChar) CPtr

func (c CUChar) CPtr() unsafe.Pointer

CPtr retunrs an unsafe pointer for CUInt8

func (CUChar) SIB

func (c CUChar) SIB() uint

SIB returns the number of bytes the CScalar has

type CUInt

type CUInt C.uint

CUInt is an unsigned int in C

func (CUInt) CPtr

func (i CUInt) CPtr() unsafe.Pointer

CPtr returns an unsafe pointer of the Unsigned Int

func (CUInt) SIB

func (i CUInt) SIB() uint

SIB returns the number of bytes the CScalar has

type DPointer

type DPointer interface {
	DPtr() *unsafe.Pointer
}

DPointer interface returns an *unsafe.Pointer

type Mem

type Mem interface {
	Pointer
	DPointer
}

Mem is mem that is shared between cuda packages

type Pointer

type Pointer interface {
	Ptr() unsafe.Pointer
}

Pointer interface returns an unsafe.Pointer

func Offset

func Offset(p Pointer, bybytes uint) Pointer

Offset returns a gocu.Mem with the unsafe.Pointer stored in it at the offset bybytes

type Wrapper

type Wrapper struct {
	// contains filtered or unexported fields
}

Wrapper is a wrapper around some golang memory

func CreateUnsafeWrapper

func CreateUnsafeWrapper(p unsafe.Pointer, sib uint) *Wrapper

CreateUnsafeWrapper creates Wrapper from an unsafe.Pointer

func WrapGoMem

func WrapGoMem(input interface{}) (*Wrapper, error)

WrapGoMem returns a GoMem considering the input type. Will only support slices and pointers to go types

func (*Wrapper) DPtr

func (g *Wrapper) DPtr() *unsafe.Pointer

DPtr satisfies DPointer interface

func (*Wrapper) Ptr

func (g *Wrapper) Ptr() unsafe.Pointer

Ptr satisfies Pointer interface

func (*Wrapper) SIB

func (g *Wrapper) SIB() (sib uint)

SIB returns the size in bytes the wrapper has.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL