2020-06-13 02:19:01 +01:00
|
|
|
package tensor
|
2020-06-02 10:29:24 +01:00
|
|
|
|
2020-06-03 02:03:38 +01:00
|
|
|
// #include <stdlib.h>
|
|
|
|
import "C"
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"unsafe"
|
|
|
|
|
|
|
|
lib "github.com/sugarme/gotch/libtch"
|
|
|
|
)
|
|
|
|
|
2020-06-03 03:07:08 +01:00
|
|
|
// ptrToString check C pointer for null. If not null, get value
|
|
|
|
// the pointer points to and frees up C memory. It is used for
|
|
|
|
// getting error message C pointer points to and clean up C memory.
|
|
|
|
//
|
2020-06-03 02:03:38 +01:00
|
|
|
// NOTE: C does not have exception design. C++ throws exception
|
|
|
|
// to stderr. This code to check stderr for any err message,
|
2020-06-03 03:07:08 +01:00
|
|
|
// if it exists, takes it and frees up C memory.
|
2020-06-03 02:03:38 +01:00
|
|
|
func ptrToString(cptr *C.char) string {
|
2020-06-03 03:07:08 +01:00
|
|
|
var str string = ""
|
2020-06-03 02:03:38 +01:00
|
|
|
|
2020-06-03 03:07:08 +01:00
|
|
|
if cptr != nil {
|
2020-06-05 20:44:54 +01:00
|
|
|
str = C.GoString(cptr)
|
2020-06-03 02:03:38 +01:00
|
|
|
C.free(unsafe.Pointer(cptr))
|
|
|
|
}
|
2020-06-03 03:07:08 +01:00
|
|
|
|
|
|
|
return str
|
2020-06-03 02:03:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// TorchErr checks and retrieves last error message from
|
|
|
|
// C `thread_local` if existing and frees up C memory the C pointer
|
|
|
|
// points to.
|
|
|
|
//
|
|
|
|
// NOTE: Go language atm does not have generic function something
|
|
|
|
// similar to `macro` in Rust language, does it? So we have to
|
|
|
|
// wrap this function to any Libtorch C function call to check error
|
|
|
|
// instead of doing the other way around.
|
|
|
|
// See Go2 proposal: https://github.com/golang/go/issues/32620
|
|
|
|
func TorchErr() error {
|
|
|
|
cptr := (*C.char)(lib.GetAndResetLastErr())
|
|
|
|
errStr := ptrToString(cptr)
|
|
|
|
if errStr != "" {
|
2020-06-05 20:44:54 +01:00
|
|
|
return fmt.Errorf("Libtorch API Error: %v\n", errStr)
|
2020-06-03 02:03:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|