cclient.go 6.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
package main

/*
#include <stdlib.h>
#include <string.h>
typedef enum {
  PADDLE_ELEMENT_TYPE_INT32   = 0,
  PADDLE_ELEMENT_TYPE_UINT32  = 1,
  PADDLE_ELEMENT_TYPE_INT64   = 2,
  PADDLE_ELEMENT_TYPE_UINT64  = 3,
  PADDLE_ELEMENT_TYPE_FLOAT32 = 4,
  PADDLE_ELEMENT_TYPE_FLOAT64 = 5,
} paddle_element_type;

typedef struct {
  char*               name;
  paddle_element_type element_type;
18
  unsigned char*      content;
19 20 21
  int                 content_len;
} paddle_parameter, paddle_gradient;

H
Helin Wang 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34 35
static inline void paddle_release_param(paddle_parameter* param) {
  if (param != NULL) {
    if (param->name != NULL) {
      free(param->name);
    }

    if (param->content != NULL) {
      free(param->content);
    }

    free(param);
  }
}

36 37 38 39 40 41
typedef int client;
*/
import "C"

import (
	"log"
42
	"strings"
43 44 45
	"sync"
	"unsafe"

46
	"github.com/PaddlePaddle/Paddle/go/pserver"
47 48
)

H
Helin Wang 已提交
49
var nullPtr = unsafe.Pointer(uintptr(0))
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
var mu sync.Mutex
var handleMap = make(map[C.client]*pserver.Client)
var curHandle C.client

func add(c *pserver.Client) C.client {
	mu.Lock()
	defer mu.Unlock()
	client := curHandle
	curHandle++
	handleMap[client] = c
	return client
}

func get(client C.client) *pserver.Client {
	mu.Lock()
	defer mu.Unlock()
	return handleMap[client]
}

func remove(client C.client) *pserver.Client {
	mu.Lock()
	defer mu.Unlock()
	h := handleMap[client]
	delete(handleMap, client)
	return h
}

func cArrayToSlice(p unsafe.Pointer, len int) []byte {
H
Helin Wang 已提交
78 79 80 81
	if p == nullPtr {
		return nil
	}

82 83 84 85
	// create a Go clice backed by a C array, reference:
	// https://github.com/golang/go/wiki/cgo#turning-c-arrays-into-go-slices
	//
	// Go garbage collector will not interact with this data, need
H
Helin Wang 已提交
86
	// to be freed properly.
87 88 89
	return (*[1 << 30]byte)(p)[:len:len]
}

90 91 92 93 94 95 96 97 98 99 100 101
type selector bool

func (s selector) Select() bool {
	return bool(s)
}

type lister []pserver.Server

func (l lister) List() []pserver.Server {
	return l
}

102
//export paddle_new_pserver_client
H
Helin Wang 已提交
103
func paddle_new_pserver_client(addrs *C.char, selected int) C.client {
104 105 106 107 108 109 110
	a := C.GoString(addrs)
	as := strings.Split(a, ",")
	servers := make([]pserver.Server, len(as))
	for i := range as {
		servers[i].Index = i
		servers[i].Addr = as[i]
	}
H
Helin Wang 已提交
111
	c := pserver.NewClient(lister(servers), len(as), selector(selected != 0))
112 113 114
	return add(c)
}

115 116 117 118 119 120
//export paddle_new_etcd_pserver_client
func paddle_new_etcd_pserver_client(etcd_addr *C.char) C.client {
	// TODO(helin): fault tolerant pserver client using etcd.
	panic("not implemented.")
}

121 122
//export paddle_pserver_client_release
func paddle_pserver_client_release(client C.client) {
123
	remove(client)
124 125 126
}

//export paddle_begin_init_params
127
func paddle_begin_init_params(client C.client) C.int {
128
	c := get(client)
129
	if selected := c.BeginInitParams(); selected {
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
		return 1
	}
	return 0
}

//export paddle_init_param
func paddle_init_param(client C.client, param C.paddle_parameter, param_config unsafe.Pointer, config_len C.int) C.int {
	et := pserver.ElementType(param.element_type)
	name := C.GoString(param.name)
	content := cArrayToSlice(unsafe.Pointer(param.content), int(param.content_len))
	pc := pserver.ParameterWithConfig{
		Param:  pserver.Parameter{Name: name, ElementType: et, Content: content},
		Config: cArrayToSlice(param_config, int(config_len)),
	}
	c := get(client)
	err := c.InitParam(pc)
	if err != nil {
		log.Println(err)
		return -1
	}

	return 0
}

//export paddle_finish_init_params
func paddle_finish_init_params(client C.client) C.int {
	c := get(client)
	err := c.FinishInitParams()
	if err != nil {
		log.Println(err)
		return -1
	}

	return 0
}

//export paddle_send_grads
167
func paddle_send_grads(client C.client, grads **C.paddle_gradient, total C.int) C.int {
168 169
	var gs []pserver.Gradient
	for i := 0; i < int(total); i++ {
170
		grad := *(**C.paddle_gradient)(unsafe.Pointer((uintptr(unsafe.Pointer(grads)) + uintptr(i)*unsafe.Sizeof(*grads))))
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
		et := pserver.ElementType(grad.element_type)
		name := C.GoString(grad.name)
		content := cArrayToSlice(unsafe.Pointer(grad.content), int(grad.content_len))
		gs = append(gs, pserver.Gradient{Name: name, ElementType: et, Content: content})
	}

	c := get(client)
	err := c.SendGrads(gs)
	if err != nil {
		log.Println(err)
		return -1
	}

	return 0
}

//export paddle_get_params
H
Helin Wang 已提交
188
func paddle_get_params(client C.client, names **C.char, dst **C.paddle_parameter, total C.int) C.int {
189 190
	var ns []string
	for i := 0; i < int(total); i++ {
H
Helin Wang 已提交
191
		name := *(**C.char)(unsafe.Pointer((uintptr(unsafe.Pointer(names)) + uintptr(i)*unsafe.Sizeof(*names))))
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
		ns = append(ns, C.GoString(name))
	}
	c := get(client)
	ps, err := c.GetParams(ns)
	if err != nil {
		log.Println(err)
		return -1
	}

	for i := 0; i < int(total); i++ {
		if i >= len(ps) {
			break
		}

		p := ps[i]
H
Helin Wang 已提交
207 208 209 210 211 212
		param := *(**C.paddle_parameter)(unsafe.Pointer((uintptr(unsafe.Pointer(dst)) + uintptr(i)*unsafe.Sizeof(*dst))))
		nameReady := false
		contentAllocated := false

		if unsafe.Pointer(param) == nullPtr {
			param = (*C.paddle_parameter)(C.calloc(1, C.size_t(unsafe.Sizeof(*param))))
H
Helin Wang 已提交
213
		} else {
H
Helin Wang 已提交
214 215
			if unsafe.Pointer(param.name) != nullPtr {
				if n := C.GoString(param.name); n != p.Name {
H
Helin Wang 已提交
216
					log.Println("Warning: the pre-allocated parameter name does not match the parameter name, it will be freed.", n, p.Name)
H
Helin Wang 已提交
217 218 219 220 221
					C.free(unsafe.Pointer(param.name))
				} else {
					nameReady = true
				}
			}
222

H
Helin Wang 已提交
223
			if unsafe.Pointer(param.content) != nullPtr {
H
Helin Wang 已提交
224 225 226
				if int(param.content_len) == len(p.Content) {
					contentAllocated = true
				} else {
H
Helin Wang 已提交
227
					log.Println("Warning: the pre-allocated content len does not match parameter content len, the pre-allocated content will be freed.", param.content_len, len(p.Content))
H
Helin Wang 已提交
228
					C.free(unsafe.Pointer(param.content))
H
Helin Wang 已提交
229
				}
230 231 232
			}
		}

H
Helin Wang 已提交
233 234 235 236
		if !nameReady {
			param.name = C.CString(p.Name)
		}
		if !contentAllocated {
237
			param.content = (*C.uchar)(C.malloc(C.size_t(len(p.Content))))
238
		}
H
Helin Wang 已提交
239
		C.memcpy(unsafe.Pointer(param.content), unsafe.Pointer(&p.Content[0]), C.size_t(len(p.Content)))
240 241 242 243 244 245 246 247 248 249 250
		param.content_len = C.int(len(p.Content))
		param.element_type = C.paddle_element_type(p.ElementType)
	}

	return 0
}

//export paddle_save_model
func paddle_save_model(client C.client, path *C.char) C.int {
	p := C.GoString(path)
	c := get(client)
251
	err := c.Save(p)
252 253 254 255 256 257 258 259 260
	if err != nil {
		log.Println(err)
		return -1
	}

	return 0
}

func main() {} // Required but ignored