提交 242df4f0 编写于 作者: D dongzhihong

"copy parameter content with memcpy"

上级 864386d5
...@@ -39,22 +39,20 @@ func newOptimizer(paramWithConfigs ParameterWithConfig) *optimizer { ...@@ -39,22 +39,20 @@ func newOptimizer(paramWithConfigs ParameterWithConfig) *optimizer {
p := paramWithConfigs.Param p := paramWithConfigs.Param
c := paramWithConfigs.Config c := paramWithConfigs.Config
var cbuffer unsafe.Pointer var cbuffer unsafe.Pointer
cbuffer = unsafe.Pointer(&p.Content[0]) cbuffer_len := int(unsafe.Sizeof(p.Content[0])) * len(p.Content)
cbuffer = C.malloc(C.size_t(cbuffer_len))
C.memcpy(cbuffer, unsafe.Pointer(&p.Content[0]), C.size_t(cbuffer_len))
o.opt = C.paddle_create_optimizer((*C.uchar)(&c[0]), C.int(len(c)), o.opt = C.paddle_create_optimizer((*C.uchar)(&c[0]), C.int(len(c)),
C.paddle_element_type(p.ElementType), cbuffer, C.int(len(p.Content)), C.paddle_element_type(p.ElementType), cbuffer, C.int(len(p.Content)),
(*C.char)(nullPtr), 0) (*C.char)(nullPtr), 0)
return o return o
} }
func (o *optimizer) GetWeights(p *Parameter) error { func (o *optimizer) GetWeights() []byte {
// FIXME: get weigths from optimizer has bug // FIXME: get weigths from optimizer has bug
var buffer unsafe.Pointer var buffer unsafe.Pointer
buffer_len := C.paddle_optimizer_get_weights(o.opt, &buffer) buffer_len := C.paddle_optimizer_get_weights(o.opt, &buffer)
if buffer_len == 0 || buffer == nullPtr { return cArrayToSlice(buffer, int(buffer_len))
return fmt.Errorf("parameter optimizer error : %s get failed", p.Name)
}
p.Content = cArrayToSlice(buffer, int(buffer_len))
return nil
} }
func (o *optimizer) UpdateParameter(g Gradient) error { func (o *optimizer) UpdateParameter(g Gradient) error {
...@@ -62,10 +60,7 @@ func (o *optimizer) UpdateParameter(g Gradient) error { ...@@ -62,10 +60,7 @@ func (o *optimizer) UpdateParameter(g Gradient) error {
return fmt.Errorf("Name: %s, parameter and gradient element type not match, parameter: %v, gradient: %v", g.Name, o.ElementType, g.ElementType) return fmt.Errorf("Name: %s, parameter and gradient element type not match, parameter: %v, gradient: %v", g.Name, o.ElementType, g.ElementType)
} }
// FIXME: do we need a copy? discard g.Content by GC ok r := C.paddle_update_parameter(o.opt, C.paddle_element_type(g.ElementType), unsafe.Pointer(&g.Content[0]), C.int(len(g.Content)))
var cbuffer unsafe.Pointer
cbuffer = unsafe.Pointer(&g.Content[0])
r := C.paddle_update_parameter(o.opt, C.paddle_element_type(g.ElementType), cbuffer, C.int(len(g.Content)))
if r != 0 { if r != 0 {
return fmt.Errorf("optimizer update returned error code: %d", r) return fmt.Errorf("optimizer update returned error code: %d", r)
} }
......
...@@ -45,7 +45,6 @@ type Service struct { ...@@ -45,7 +45,6 @@ type Service struct {
initialized chan struct{} initialized chan struct{}
mu sync.Mutex mu sync.Mutex
// injection from parameter to optimizer
optMap map[string]*optimizer optMap map[string]*optimizer
} }
...@@ -130,9 +129,8 @@ func (s *Service) GetParam(name string, parameter *Parameter) error { ...@@ -130,9 +129,8 @@ func (s *Service) GetParam(name string, parameter *Parameter) error {
// paramter content. // paramter content.
parameter.Name = name parameter.Name = name
parameter.ElementType = opt.ElementType parameter.ElementType = opt.ElementType
parameter.Content = opt.GetWeights()
err := opt.GetWeights(parameter) return nil
return err
} }
// Save tells the parameter server to save parameters. // Save tells the parameter server to save parameters.
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册