WIP(wrapper/error): some fixed

This commit is contained in:
sugarme 2020-06-04 14:01:10 +10:00
parent ae5f26d567
commit 2ef7f06e4a
3 changed files with 11 additions and 38 deletions

View File

@ -12,8 +12,8 @@ func main() {
// Try to compare 2 tensor with incompatible dimensions
// and check this returns an error
dx := []int32{1, 2, 3}
// dy := []int32{1, 2, 3, 4}
dy := []int32{1, 2, 5}
dy := []int32{1, 2, 3, 4}
// dy := []int32{1, 2, 5}
xs, err := wrapper.OfSlice(dx)
if err != nil {

View File

@ -22,9 +22,8 @@ func ptrToString(cptr *C.char) string {
if cptr != nil {
// strPtr := (*string)(unsafe.Pointer(cptr))
// fmt.Printf("Error: string at err pointer: %v\n", cptr)
// TODO: get error string from pointer
fmt.Printf("Err Msg from C (TODO: Will show err message here)...\n")
str = fmt.Sprintf("TODO: will show more detail here.")
C.free(unsafe.Pointer(cptr))
}

View File

@ -251,47 +251,21 @@ func (ts Tensor) Eq1(other Tensor) {
// ctensorsPtr := C.malloc(C.size_t(1) * C.size_t(unsafe.Sizeof(C.tensor)))
// C null pointer C.tensor * = null
ctensorPtr := lib.NewTensor()
fmt.Printf("Out tensor BEFORE: %v\n", &ctensorPtr)
fmt.Printf("Out tensor address: %v\n", *(*int)(unsafe.Pointer(&ctensorPtr)))
// ctensorPtr := lib.NewTensor()
// nbytes := C.size_t(1) * C.size_t(unsafe.Sizeof(C.tensor))
ctensorAddr := *(*int64)(unsafe.Pointer(&ctensorPtr))
var data []int64
data = append(data, ctensorAddr)
// Get a pointer in C memory
ctensorPtr := C.malloc(0)
fmt.Printf("ctensorPtr: %v\n", ctensorPtr)
// lib.AtPrint((*lib.C_tensor)(unsafe.Pointer(ctensorPtr)))
// nullPtr := (*C.tensor)(unsafe.Pointer(uintptr(0)))
// fmt.Printf("Null pointer: %v\n", &nullPtr)
//
// data := []*C.tensor{nullPtr}
// fmt.Printf("data: %v\n", data)
// // Calculate number of bytes for a slice of one element of C null pointer
// nbytes := 1 * unsafe.Sizeof(uintptr(0))
// fmt.Printf("Nbytes: %v\n", nbytes)
//
// cptr := C.malloc(C.size_t(nbytes))
// ctensorsPtr := (*[1 << 30]byte)(cptr)[:nbytes:nbytes]
// buf := bytes.NewBuffer(ctensorsPtr[:0:nbytes])
// // ctensorsPtr := (*[1 << 30]C.tensor)(unsafe.Pointer(uintptr(0)))[:nbytes:nbytes]
// fmt.Printf("ctensorsPtr 1: %v\n", &ctensorsPtr[0])
// fmt.Printf("Type of ctensorsPtr: %v\n", reflect.TypeOf(ctensorsPtr))
// // buff := bytes.NewBuffer(dataSlice[:0:nbytes])
// // Write to memory
// err := binary.Write(buf, nativeEndian, data)
// if err != nil {
// log.Fatal(err)
// }
// lib.Atg_eq1(unsafe.Pointer(cptr), ts.ctensor, other.ctensor)
lib.Atg_eq1(unsafe.Pointer(&ctensorPtr), ts.ctensor, other.ctensor)
lib.Atg_eq1(unsafe.Pointer(ctensorPtr), ts.ctensor, other.ctensor)
if err := TorchErr(); err != nil {
log.Fatal(err)
}
fmt.Printf("Out tensor AFTER: %v\n", &ctensorPtr)
lib.AtPrint((*lib.C_tensor)(unsafe.Pointer(ctensorPtr)))
lib.AtPrint((*lib.C_tensor)(unsafe.Pointer(&ctensorPtr)))
// fmt.Printf("Out tensor AFTER: %v\n", &ctensorPtr)
}