Permalink
Browse files

cleanup

  • Loading branch information...
1 parent cc46a20 commit 67ebb67e99641066d723be1ee5e7fa31cdbb7e82 @jbooth committed Jan 20, 2011
Showing with 79 additions and 57 deletions.
  1. +1 −1 cublas/cublas1.go
  2. +78 −56 cublas/memory.go
View
@@ -5,7 +5,7 @@ import "C"
import "os"
func floatPtr(d DevicePointer) *_Ctype_float {
- return ((*_Ctype_float)(d.data()))
+ return ((*_Ctype_float)(d.ptr))
}
// BLAS 1 operations, vector-vector and vector-scalar
View
@@ -25,86 +25,108 @@ func Init() {
//fmt.Printf("sync: " + CudaErrT(C.cudaThreadSynchronize()).String())
}
-type Vector interface {
- data() unsafe.Pointer
- Len() int
- elemSize() int
+// represents a region of memory
+type Memory struct {
+ ptr unsafe.Pointer
+ Length int
+ elemSize _Ctype_int
}
-// Vector of floats in host memory. Pinned for optimal transfer speed with GPU.
-type VectorFH struct {
- Ptr unsafe.Pointer // ptr to pinned memory
- Slice []float // Slice representation of _Ctype_ints, could be broken on your platform
- Length int // length of pinned memory section
+// functions to allocate/manage pinned memory for fast transfer with GPU
+// Vector and Matrix types expose slices
+type HostMemory struct {
+ Memory
+}
+func (v HostMemory) Free() {
+ err := CudaErr(C.cuMemFreeHost(v.ptr))
+ if (err != SUCCESS) { panic(err.String()) }
}
-func (v VectorFH) elemSize() int { return int(floatSize) }
-func (v VectorFH) data() unsafe.Pointer { return v.Ptr }
-func (v VectorFH) Len() int { return v.Length }
-
-func (v VectorFH) Free() os.Error {
- return CudaErr(C.cuMemFreeHost(v.Ptr))
+func (v HostMemory) sliceFloat() []float {
+ h := reflect.SliceHeader{uintptr(v.ptr), v.Length, v.Length}
+ return unsafe.Unreflect(floatSliceType, unsafe.Pointer(&h)).([]float)
}
-// allocates a vector of host pinned memory. Must be Free()d
-func MallocVecFH(length int) (VectorFH,os.Error) {
+func mallocHostPinned(length int, elemSize _Ctype_int) HostMemory {
// indirect pointer we'll copy alloc'd address to
var ptrptr *unsafe.Pointer = new(unsafe.Pointer)
- var err = CudaErr(C.cuMemAllocHost(ptrptr,_Ctypedef_size_t(length)))
- // dereference - this is our memory now
- var ptr = (*ptrptr)
- // wrap in Slice
- var h = reflect.SliceHeader{uintptr(ptr), length, length}
- var Slice []float =unsafe.Unreflect(floatSliceType, unsafe.Pointer(&h)).([]float)
-
- return VectorFH { ptr, Slice, length }, err
+ // malloc length * elemSize bytes
+ var err = CudaErr(C.cuMemAllocHost(ptrptr,_Ctypedef_size_t(length * int(elemSize))))
+ if (err != SUCCESS) { panic (err.String()) }
+ // dereference - this is our memory now
+ ptr := (*ptrptr)
+ // wrap in HostMemory
+ return HostMemory { Memory {ptr, length,elemSize }}
}
+type VectorFH struct {
+ HostMemory
+ Slice []float // Slice representation of _Ctype_floats, could be broken on your platform
+}
-type DevicePointer struct {
- Ptr unsafe.Pointer
- Length int
+// allocates a vector of host pinned memory. Must be Free()d
+func MallocVecFH(length int) (VectorFH) {
+ hp := mallocHostPinned ( length , floatSize )
+ return VectorFH { hp, hp.sliceFloat() }
+}
+
+type MatrixFH struct {
+ HostMemory
+ Slice [][]float
+ Width int
+ Height int
}
-func (d DevicePointer) data() unsafe.Pointer { return d.Ptr }
-func (d DevicePointer) Len() int { return d.Length }
+//func MallocMatFH(width int, height int) {
+ //length int64 := width*height
+ //HostMemory mem := mallocHostPinned(length, floatSize)
+ //Slice [][]float :=make([][]float,10
+//}
+
+type DevicePointer struct {
+ Memory
+}
-func (d DevicePointer) Free() os.Error {
- return CublasErr(C.cublasFree(d.data()))
+func (d DevicePointer) Free() {
+ err := CublasErr(C.cublasFree(d.ptr))
+ if (err != SUCCESS) { panic ( err.String()) }
}
-func (d DevicePointer) CopyFrom(host Vector) os.Error {
- if (host.Len() != d.Length) {
- return os.NewError("Host and device length not equal")
+func (d DevicePointer) CopyVectorFrom(host Memory) {
+ if (host.Length != d.Length) {
+ panic("Host and device length not equal")
}
- var stat = C.cublasSetVector(
- _Ctype_int(host.Len()),
- _Ctype_int(host.elemSize()),
- host.data(),
+ err := CublasErr(C.cublasSetVector(
+ _Ctype_int(host.Length),
+ _Ctype_int(host.elemSize),
+ host.ptr,
_Ctype_int(1),
- d.data(),
- _Ctype_int(1))
- return CublasErr(stat)
+ d.ptr,
+ _Ctype_int(1)))
+ if (err != SUCCESS) { panic(err.String()) }
}
-func (d DevicePointer) CopyTo(host Vector) os.Error {
- if (host.Len() != d.Length) {
- return os.NewError("Host and device length not equal")
+func (d DevicePointer) CopyVectorTo(host Memory) {
+ if (host.Length != d.Length) {
+ panic("Host and device length not equal")
}
- var stat = C.cublasGetVector(
- _Ctype_int(host.Len()),
- _Ctype_int(host.elemSize()),
- d.data(),
+ err := CublasErr(C.cublasGetVector(
+ _Ctype_int(host.Length),
+ _Ctype_int(host.elemSize),
+ d.ptr,
_Ctype_int(1),
- host.data(),
- _Ctype_int(1))
- return CublasErr(stat)
+ host.ptr,
+ _Ctype_int(1)))
+ if (err != SUCCESS) { panic(err.String()) }
}
-// mallocs a vector of floats on the device
-func MallocDevicePointerVF(length int) (DevicePointer, os.Error) {
+// mallocs a vector of length elements at elemBytes each on the device
+func mallocDevice(length int, elemSize int) (DevicePointer, os.Error) {
+ // indirect pointer
var ptrptr *unsafe.Pointer = new(unsafe.Pointer)
- var err = CublasErr(C.cublasAlloc(_Ctype_int(length), floatSize, ptrptr,))
+ // malloc
+ var err = CublasErr(C.cublasAlloc(_Ctype_int(length), _Ctype_int(elemSize), ptrptr))
+ // dereference indirect pointer for direct pointer to our new memory
var ptr = (*ptrptr)
- return DevicePointer { ptr, length }, err
+ return DevicePointer { Memory { ptr, length, _Ctype_int(elemSize) }}, err
}

0 comments on commit 67ebb67

Please sign in to comment.