|
| 1 | +package main |
| 2 | + |
| 3 | +/* |
| 4 | +#include <stdio.h> |
| 5 | +#include <stdlib.h> |
| 6 | +
|
| 7 | +enum CVMStatus { |
| 8 | + SUCCEED = 0, |
| 9 | + ERROR_LOGIC = -1, |
| 10 | + ERROR_RUNTIME = -2 |
| 11 | +}; |
| 12 | +
|
| 13 | +void myprint_int(long long *num) { |
| 14 | + printf("%lld\n", *num); |
| 15 | + *num = 4; |
| 16 | +} |
| 17 | +void myprint(char *s) { |
| 18 | + printf("%s\n", s); |
| 19 | +} |
| 20 | +
|
| 21 | +void new_arr(void **arr) { |
| 22 | + void *mal = malloc(sizeof(int) * 1); |
| 23 | + int *nums = (int*)mal; |
| 24 | + *nums = 1000; |
| 25 | + printf("%p %d\n", mal, *nums); |
| 26 | + *arr = mal; |
| 27 | +} |
| 28 | +
|
| 29 | +void myprint_void(void *arr) { |
| 30 | + int *nums = (int*)arr; |
| 31 | + printf("%p %d\n", arr, *nums); |
| 32 | +} |
| 33 | +
|
| 34 | +*/ |
| 35 | +import "C" |
| 36 | +import ( |
| 37 | + "fmt" |
| 38 | + "io/ioutil" |
| 39 | + _ "io/ioutil" |
| 40 | + "os" |
| 41 | + _ "reflect" |
| 42 | + _ "runtime" |
| 43 | + "unsafe" |
| 44 | + |
| 45 | + "github.com/CortexFoundation/CortexTheseus/cvm-runtime/kernel" |
| 46 | + // "github.com/CortexFoundation/CortexTheseus/inference/synapse" |
| 47 | + "github.com/CortexFoundation/CortexTheseus/log" |
| 48 | +) |
| 49 | + |
| 50 | +func test() { |
| 51 | + cs := C.CString("Hello from stdio") |
| 52 | + C.myprint(cs) |
| 53 | + C.free(unsafe.Pointer(cs)) |
| 54 | + |
| 55 | + var num C.longlong |
| 56 | + num = 3 |
| 57 | + C.myprint_int(&num) |
| 58 | + fmt.Println(int64(num)) |
| 59 | + |
| 60 | + var s1 C.enum_CVMStatus |
| 61 | + s1 = C.ERROR_LOGIC |
| 62 | + s2 := C.ERROR_LOGIC |
| 63 | + fmt.Println(int(s1) == int(s2)) |
| 64 | + |
| 65 | + var arr unsafe.Pointer |
| 66 | + C.new_arr(&arr) |
| 67 | + C.myprint_void(arr) |
| 68 | +} |
| 69 | + |
| 70 | +func main() { |
| 71 | + // Set log |
| 72 | + log.Root().SetHandler(log.LvlFilterHandler(log.Lvl(5), log.StreamHandler(os.Stdout, log.TerminalFormat(true)))) |
| 73 | + |
| 74 | + var ( |
| 75 | + lib *kernel.LibCVM |
| 76 | + net *kernel.Model |
| 77 | + res []byte |
| 78 | + status int |
| 79 | + ) |
| 80 | + |
| 81 | + device := "cpu" |
| 82 | + deviceType := 0 |
| 83 | + if device == "cuda" { |
| 84 | + deviceType = 1 |
| 85 | + } |
| 86 | + // lib, status = kernel.LibOpen("./libcvm_runtime_" + device + ".so") |
| 87 | + // lib, status = kernel.LibOpen("./libcvm_runtime_cpu.so") |
| 88 | + lib, status = kernel.LibOpen("./build/libcvm_runtime.so") |
| 89 | + if status != kernel.SUCCEED { |
| 90 | + fmt.Printf("open library error: %d\n", status) |
| 91 | + return |
| 92 | + } |
| 93 | + |
| 94 | + root := "/data/std_out/log2" |
| 95 | + // root := "/home/serving/ctxc_data/cpu/3145ad19228c1cd2d051314e72f26c1ce77b7f02/data" |
| 96 | + modelCfg, sErr := ioutil.ReadFile(root + "/symbol") |
| 97 | + if sErr != nil { |
| 98 | + fmt.Println(sErr) |
| 99 | + return |
| 100 | + } |
| 101 | + modelBin, pErr := ioutil.ReadFile(root + "/params") |
| 102 | + if pErr != nil { |
| 103 | + fmt.Println(pErr) |
| 104 | + return |
| 105 | + } |
| 106 | + // modelCfg := []byte("{}") |
| 107 | + // modelBin := []byte("dkjflsiejflsdkj") |
| 108 | + net, status = kernel.New(lib, modelCfg, modelBin, deviceType, 0) |
| 109 | + if status != kernel.SUCCEED { |
| 110 | + fmt.Printf("Failed LoadModel: %d\n", status) |
| 111 | + return |
| 112 | + } |
| 113 | + input_size := net.GetInputLength() |
| 114 | + fmt.Printf("Succeed LoadModel: %p ops=%s size=%s input_size=%s\n", |
| 115 | + &net, net.Ops(), net.Size(), input_size) |
| 116 | + |
| 117 | + var data []byte = make([]byte, input_size) |
| 118 | + // cvmVersion := synapse.CVMVersion(cvm.chainConfig, cvm.BlockNumber) |
| 119 | + var cvmVersion int = 1 |
| 120 | + res, status = net.Predict(data, cvmVersion) |
| 121 | + if status != kernel.SUCCEED { |
| 122 | + fmt.Printf("Failed Predict: %d\n", status) |
| 123 | + return |
| 124 | + } |
| 125 | + fmt.Printf("Succeed Predict: %v\n", res) |
| 126 | + |
| 127 | + status = net.Free() |
| 128 | + if status != kernel.SUCCEED { |
| 129 | + fmt.Printf("Failed Free model: %d\n", status) |
| 130 | + return |
| 131 | + } |
| 132 | + fmt.Printf("Succeed Free model\n") |
| 133 | + |
| 134 | + var gas uint64 |
| 135 | + gas, status = kernel.GetModelGasFromGraphFile(lib, modelCfg) |
| 136 | + if status != kernel.SUCCEED { |
| 137 | + fmt.Printf("Failed get model gas from file: %s\n", status) |
| 138 | + return |
| 139 | + } |
| 140 | + fmt.Printf("Succeed get model gas from file: %s\n", int(gas)) |
| 141 | +} |
0 commit comments