cclient.go 7.4 KB
Newer Older
D
dongzhihong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
package main

/*
#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;
31
  unsigned char*      content;
32 33 34
  int                 content_len;
} paddle_parameter, paddle_gradient;

35
typedef int paddle_pserver_client;
H
Helin Wang 已提交
36 37
#define PSERVER_ERROR -1
#define PSERVER_OK 0
38 39 40 41
*/
import "C"

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

46
	"github.com/PaddlePaddle/Paddle/go/pserver"
Q
Qiao Longfei 已提交
47
	"github.com/PaddlePaddle/Paddle/go/pserver/client"
H
Helin Wang 已提交
48
	log "github.com/sirupsen/logrus"
49 50 51
)

var mu sync.Mutex
Q
Qiao Longfei 已提交
52
var handleMap = make(map[C.paddle_pserver_client]*client.Client)
53
var curHandle C.paddle_pserver_client
54

Q
Qiao Longfei 已提交
55
func add(c *client.Client) C.paddle_pserver_client {
56 57 58 59 60 61 62 63
	mu.Lock()
	defer mu.Unlock()
	client := curHandle
	curHandle++
	handleMap[client] = c
	return client
}

Q
Qiao Longfei 已提交
64
func get(client C.paddle_pserver_client) *client.Client {
65 66 67 68 69
	mu.Lock()
	defer mu.Unlock()
	return handleMap[client]
}

Q
Qiao Longfei 已提交
70
func remove(client C.paddle_pserver_client) *client.Client {
71 72 73 74 75 76 77 78
	mu.Lock()
	defer mu.Unlock()
	h := handleMap[client]
	delete(handleMap, client)
	return h
}

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

83 84 85 86
	// 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 已提交
87
	// to be freed properly.
88 89 90
	return (*[1 << 30]byte)(p)[:len:len]
}

91 92 93 94 95 96
type selector bool

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

Q
Qiao Longfei 已提交
97
type lister []client.Server
98

Q
Qiao Longfei 已提交
99
func (l lister) List() []client.Server {
100 101 102
	return l
}

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

116
//export paddle_new_etcd_pserver_client
H
Helin Wang 已提交
117
func paddle_new_etcd_pserver_client(etcdEndpoints *C.char, selected int) C.paddle_pserver_client {
Q
Qiao Longfei 已提交
118
	// TODO(Longfei: use etcd lock to decide which trainer to initialize the parameters)
H
Helin Wang 已提交
119 120 121
	addr := C.GoString(etcdEndpoints)
	etcdClient := client.NewEtcd(addr)
	c := client.NewClient(etcdClient, etcdClient.Desired(), selector(selected != 0))
Q
Qiao Longfei 已提交
122
	return add(c)
123 124
}

125
//export paddle_pserver_client_release
126
func paddle_pserver_client_release(client C.paddle_pserver_client) {
127
	remove(client)
128 129 130
}

//export paddle_begin_init_params
131
func paddle_begin_init_params(client C.paddle_pserver_client) C.int {
132
	c := get(client)
133
	if selected := c.BeginInitParams(); selected {
134 135
		return 1
	}
H
Helin Wang 已提交
136
	return C.PSERVER_OK
137 138 139
}

//export paddle_init_param
H
Helin Wang 已提交
140
func paddle_init_param(client C.paddle_pserver_client, param C.paddle_parameter, paramConfig unsafe.Pointer, configLen C.int) C.int {
141 142 143 144 145
	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},
H
Helin Wang 已提交
146
		Config: cArrayToSlice(paramConfig, int(configLen)),
147 148 149
	}
	c := get(client)
	err := c.InitParam(pc)
150

151
	if err != nil {
152
		if err.Error() == pserver.AlreadyInitialized {
153
			log.Warningf("parameter %s already initialized, treat paddle_init_param as successful.", name)
H
Helin Wang 已提交
154
			return C.PSERVER_OK
155
		}
H
Helin Wang 已提交
156
		log.Errorln(err)
H
Helin Wang 已提交
157
		return C.PSERVER_ERROR
158 159
	}

H
Helin Wang 已提交
160
	return C.PSERVER_OK
161 162 163
}

//export paddle_finish_init_params
164
func paddle_finish_init_params(client C.paddle_pserver_client) C.int {
165 166 167
	c := get(client)
	err := c.FinishInitParams()
	if err != nil {
168
		if err.Error() == pserver.AlreadyInitialized {
169
			log.Warningln("parameters already initialized, treat paddle_finish_init_params as successful.")
H
Helin Wang 已提交
170
			return C.PSERVER_OK
171 172
		}

H
Helin Wang 已提交
173
		log.Errorln(err)
H
Helin Wang 已提交
174
		return C.PSERVER_ERROR
175 176
	}

H
Helin Wang 已提交
177
	return C.PSERVER_OK
178 179 180
}

//export paddle_send_grads
181
func paddle_send_grads(client C.paddle_pserver_client, grads **C.paddle_gradient, total C.int) C.int {
182 183
	var gs []pserver.Gradient
	for i := 0; i < int(total); i++ {
184
		grad := *(**C.paddle_gradient)(unsafe.Pointer((uintptr(unsafe.Pointer(grads)) + uintptr(i)*unsafe.Sizeof(*grads))))
185 186 187 188 189 190 191 192 193
		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 {
H
Helin Wang 已提交
194
		log.Errorln(err)
H
Helin Wang 已提交
195
		return C.PSERVER_ERROR
196 197
	}

H
Helin Wang 已提交
198
	return C.PSERVER_OK
199 200 201
}

//export paddle_get_params
202
func paddle_get_params(client C.paddle_pserver_client, dst **C.paddle_parameter, total C.int) C.int {
203 204
	var ns []string
	for i := 0; i < int(total); i++ {
205 206
		param := *(**C.paddle_parameter)(unsafe.Pointer((uintptr(unsafe.Pointer(dst)) + uintptr(i)*unsafe.Sizeof(*dst))))
		ns = append(ns, C.GoString(param.name))
207 208 209 210
	}
	c := get(client)
	ps, err := c.GetParams(ns)
	if err != nil {
H
Helin Wang 已提交
211
		log.Errorln(err)
H
Helin Wang 已提交
212
		return C.PSERVER_ERROR
213 214
	}

215
	if len(ps) != len(ns) {
H
Helin Wang 已提交
216 217 218 219
		pn := make([]string, len(ps))
		for i, p := range ps {
			pn[i] = p.Name
		}
220
		log.Errorf("pserver returned wrong number of parameters. Requested: %s, returned: %s.", strings.Join(pn, ", "), strings.Join(ns, ", "))
H
Helin Wang 已提交
221
		return C.PSERVER_ERROR
222 223 224 225
	}

	for i := range ps {
		if ns[i] != ps[i].Name {
H
Helin Wang 已提交
226 227 228 229
			pn := make([]string, len(ps))
			for i, p := range ps {
				pn[i] = p.Name
			}
230
			log.Errorf("pserver returned wrong parameters, or not in requested order. Requested: %s, returned: %s.", strings.Join(pn, ", "), strings.Join(ns, ", "))
H
Helin Wang 已提交
231
			return C.PSERVER_ERROR
232
		}
233
	}
234

235
	for i := 0; i < int(total); i++ {
236
		p := ps[i]
H
Helin Wang 已提交
237 238
		param := *(**C.paddle_parameter)(unsafe.Pointer((uintptr(unsafe.Pointer(dst)) + uintptr(i)*unsafe.Sizeof(*dst))))

239
		if unsafe.Pointer(param) == nil {
H
Helin Wang 已提交
240
			log.Errorln("must pre-allocate parameter.")
H
Helin Wang 已提交
241
			return C.PSERVER_ERROR
242 243
		}

244
		if unsafe.Pointer(param.content) != nil {
245
			if int(param.content_len) != len(p.Content) {
H
Helin Wang 已提交
246
				log.Errorf("the pre-allocated content len does not match parameter content len. Pre-allocated len: %d, returned len: %d", param.content_len, len(p.Content))
247
				return C.PSERVER_ERROR
248 249 250
			}
		}

H
Helin Wang 已提交
251
		C.memcpy(unsafe.Pointer(param.content), unsafe.Pointer(&p.Content[0]), C.size_t(len(p.Content)))
252 253 254 255
		param.content_len = C.int(len(p.Content))
		param.element_type = C.paddle_element_type(p.ElementType)
	}

H
Helin Wang 已提交
256
	return C.PSERVER_OK
257 258 259
}

//export paddle_save_model
260
func paddle_save_model(client C.paddle_pserver_client, path *C.char) C.int {
261 262
	p := C.GoString(path)
	c := get(client)
263
	err := c.Save(p)
264
	if err != nil {
H
Helin Wang 已提交
265
		log.Errorln(err)
H
Helin Wang 已提交
266
		return C.PSERVER_ERROR
267 268
	}

H
Helin Wang 已提交
269
	return C.PSERVER_OK
270 271 272
}

func main() {} // Required but ignored