cclient.go 7.3 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
	mu.Lock()
	defer mu.Unlock()
58
	cli := curHandle
59
	curHandle++
60 61
	handleMap[cli] = c
	return cli
62 63
}

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
type selector bool

93 94 95 96 97 98
func (s selector) Select() (bool, error) {
	return bool(s), nil
}

func (s selector) Done() error {
	return nil
99 100
}

Q
Qiao Longfei 已提交
101
type lister []client.Server
102

Q
Qiao Longfei 已提交
103
func (l lister) List() []client.Server {
104 105 106
	return l
}

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

120
//export paddle_new_etcd_pserver_client
121
func paddle_new_etcd_pserver_client(etcdEndpoints *C.char) C.paddle_pserver_client {
H
Helin Wang 已提交
122 123
	addr := C.GoString(etcdEndpoints)
	etcdClient := client.NewEtcd(addr)
124
	c := client.NewClient(etcdClient, etcdClient.Desired(), etcdClient)
Q
Qiao Longfei 已提交
125
	return add(c)
126 127
}

128
//export paddle_pserver_client_release
129
func paddle_pserver_client_release(client C.paddle_pserver_client) {
130
	remove(client)
131 132
}

133 134 135 136 137 138
// paddle_begin_init_params tells trainer if it needs to init the
// parameters.
//
// returns 1 if the trainer needs to init the parameters. 0 if the
// trainer does not need to init the parameters.
//
139
//export paddle_begin_init_params
140
func paddle_begin_init_params(client C.paddle_pserver_client) C.int {
141
	c := get(client)
142 143 144 145 146 147
	selected, err := c.BeginInitParams()
	if err != nil {
		panic(err)
	}

	if selected {
148 149
		return 1
	}
150
	return 0
151 152 153
}

//export paddle_init_param
H
Helin Wang 已提交
154
func paddle_init_param(client C.paddle_pserver_client, param C.paddle_parameter, paramConfig unsafe.Pointer, configLen C.int) C.int {
155 156 157 158 159
	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 已提交
160
		Config: cArrayToSlice(paramConfig, int(configLen)),
161 162 163
	}
	c := get(client)
	err := c.InitParam(pc)
164

165
	if err != nil {
166
		if err.Error() == pserver.AlreadyInitialized {
167
			log.Warningf("parameter %s already initialized, treat paddle_init_param as successful.", name)
H
Helin Wang 已提交
168
			return C.PSERVER_OK
169
		}
H
Helin Wang 已提交
170
		log.Errorln(err)
H
Helin Wang 已提交
171
		return C.PSERVER_ERROR
172 173
	}

H
Helin Wang 已提交
174
	return C.PSERVER_OK
175 176 177
}

//export paddle_finish_init_params
178
func paddle_finish_init_params(client C.paddle_pserver_client) C.int {
179 180 181
	c := get(client)
	err := c.FinishInitParams()
	if err != nil {
182
		if err.Error() == pserver.AlreadyInitialized {
183
			log.Warningln("parameters already initialized, treat paddle_finish_init_params as successful.")
H
Helin Wang 已提交
184
			return C.PSERVER_OK
185 186
		}

H
Helin Wang 已提交
187
		log.Errorln(err)
H
Helin Wang 已提交
188
		return C.PSERVER_ERROR
189 190
	}

H
Helin Wang 已提交
191
	return C.PSERVER_OK
192 193 194
}

//export paddle_send_grads
195
func paddle_send_grads(client C.paddle_pserver_client, grads **C.paddle_gradient, total C.int) C.int {
196 197
	var gs []pserver.Gradient
	for i := 0; i < int(total); i++ {
198
		grad := *(**C.paddle_gradient)(unsafe.Pointer((uintptr(unsafe.Pointer(grads)) + uintptr(i)*unsafe.Sizeof(*grads))))
199 200 201 202 203 204 205 206 207
		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 已提交
208
		log.Errorln(err)
H
Helin Wang 已提交
209
		return C.PSERVER_ERROR
210 211
	}

H
Helin Wang 已提交
212
	return C.PSERVER_OK
213 214 215
}

//export paddle_get_params
216
func paddle_get_params(client C.paddle_pserver_client, dst **C.paddle_parameter, total C.int) C.int {
217 218
	var ns []string
	for i := 0; i < int(total); i++ {
219 220
		param := *(**C.paddle_parameter)(unsafe.Pointer((uintptr(unsafe.Pointer(dst)) + uintptr(i)*unsafe.Sizeof(*dst))))
		ns = append(ns, C.GoString(param.name))
221 222 223 224
	}
	c := get(client)
	ps, err := c.GetParams(ns)
	if err != nil {
H
Helin Wang 已提交
225
		log.Errorln(err)
H
Helin Wang 已提交
226
		return C.PSERVER_ERROR
227 228
	}

229
	if len(ps) != len(ns) {
H
Helin Wang 已提交
230 231 232 233
		pn := make([]string, len(ps))
		for i, p := range ps {
			pn[i] = p.Name
		}
234
		log.Errorf("pserver returned wrong number of parameters. Requested: %s, returned: %s.", strings.Join(pn, ", "), strings.Join(ns, ", "))
H
Helin Wang 已提交
235
		return C.PSERVER_ERROR
236 237 238 239
	}

	for i := range ps {
		if ns[i] != ps[i].Name {
H
Helin Wang 已提交
240 241 242 243
			pn := make([]string, len(ps))
			for i, p := range ps {
				pn[i] = p.Name
			}
244
			log.Errorf("pserver returned wrong parameters, or not in requested order. Requested: %s, returned: %s.", strings.Join(pn, ", "), strings.Join(ns, ", "))
H
Helin Wang 已提交
245
			return C.PSERVER_ERROR
246
		}
247
	}
248

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

253
		if unsafe.Pointer(param) == nil {
H
Helin Wang 已提交
254
			log.Errorln("must pre-allocate parameter.")
H
Helin Wang 已提交
255
			return C.PSERVER_ERROR
256 257
		}

258
		if unsafe.Pointer(param.content) != nil {
259
			if int(param.content_len) != len(p.Content) {
H
Helin Wang 已提交
260
				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))
261
				return C.PSERVER_ERROR
262 263 264
			}
		}

H
Helin Wang 已提交
265
		C.memcpy(unsafe.Pointer(param.content), unsafe.Pointer(&p.Content[0]), C.size_t(len(p.Content)))
266 267 268 269
		param.content_len = C.int(len(p.Content))
		param.element_type = C.paddle_element_type(p.ElementType)
	}

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

func main() {} // Required but ignored