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 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 131 132 133 134 135
// 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.
//
136
//export paddle_begin_init_params
137
func paddle_begin_init_params(client C.paddle_pserver_client) C.int {
138
	c := get(client)
139
	if selected := c.BeginInitParams(); selected {
140 141
		return 1
	}
142
	return 0
143 144 145
}

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

157
	if err != nil {
158
		if err.Error() == pserver.AlreadyInitialized {
159
			log.Warningf("parameter %s already initialized, treat paddle_init_param as successful.", name)
H
Helin Wang 已提交
160
			return C.PSERVER_OK
161
		}
H
Helin Wang 已提交
162
		log.Errorln(err)
H
Helin Wang 已提交
163
		return C.PSERVER_ERROR
164 165
	}

H
Helin Wang 已提交
166
	return C.PSERVER_OK
167 168 169
}

//export paddle_finish_init_params
170
func paddle_finish_init_params(client C.paddle_pserver_client) C.int {
171 172 173
	c := get(client)
	err := c.FinishInitParams()
	if err != nil {
174
		if err.Error() == pserver.AlreadyInitialized {
175
			log.Warningln("parameters already initialized, treat paddle_finish_init_params as successful.")
H
Helin Wang 已提交
176
			return C.PSERVER_OK
177 178
		}

H
Helin Wang 已提交
179
		log.Errorln(err)
H
Helin Wang 已提交
180
		return C.PSERVER_ERROR
181 182
	}

H
Helin Wang 已提交
183
	return C.PSERVER_OK
184 185 186
}

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

H
Helin Wang 已提交
204
	return C.PSERVER_OK
205 206 207
}

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

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

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

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

245
		if unsafe.Pointer(param) == nil {
H
Helin Wang 已提交
246
			log.Errorln("must pre-allocate parameter.")
H
Helin Wang 已提交
247
			return C.PSERVER_ERROR
248 249
		}

250
		if unsafe.Pointer(param.content) != nil {
251
			if int(param.content_len) != len(p.Content) {
H
Helin Wang 已提交
252
				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))
253
				return C.PSERVER_ERROR
254 255 256
			}
		}

H
Helin Wang 已提交
257
		C.memcpy(unsafe.Pointer(param.content), unsafe.Pointer(&p.Content[0]), C.size_t(len(p.Content)))
258 259 260 261
		param.content_len = C.int(len(p.Content))
		param.element_type = C.paddle_element_type(p.ElementType)
	}

H
Helin Wang 已提交
262
	return C.PSERVER_OK
263 264 265
}

func main() {} // Required but ignored