From bd2469f21c663c192a7ab73b2b2fdfafffdf5edb Mon Sep 17 00:00:00 2001 From: Helin Wang Date: Wed, 17 May 2017 20:01:29 -0400 Subject: [PATCH] correct optimizer release, add test --- paddle/go/pserver/optimizer.c | 18 ++++++++++++++---- paddle/go/pserver/optimizer_test.go | 8 ++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) create mode 100644 paddle/go/pserver/optimizer_test.go diff --git a/paddle/go/pserver/optimizer.c b/paddle/go/pserver/optimizer.c index 123684970f9..36a612a56f4 100644 --- a/paddle/go/pserver/optimizer.c +++ b/paddle/go/pserver/optimizer.c @@ -3,34 +3,44 @@ #include "optimizer.h" typedef int (*update_func)(void*, void *, paddle_element_type, const void*, int); +typedef void (*release_func)(void*); typedef struct paddle_optimizer{ - update_func func; + update_func update; + release_func release; void* optimizer; } paddle_optimizer; void paddle_release_optimizer(paddle_optimizer* o) { + o->release(o->optimizer); free(o); } int paddle_update_parameter(paddle_optimizer* o, void *buffer, paddle_element_type element_type, const void* gradient, int num_bytes) { - return o->func(o->optimizer, buffer, element_type, gradient, num_bytes); + return o->update(o->optimizer, buffer, element_type, gradient, num_bytes); } typedef struct { double learning_rate; } SGD_optimizer; -int paddle_SGD_update_parameter(void* optimizer, void *buffer, paddle_element_type element_type, const void* gradient, int num_bytes) { +int update_SGD(void* optimizer, void *buffer, paddle_element_type element_type, const void* gradient, int num_bytes) { + SGD_optimizer* o = (SGD_optimizer*)optimizer; // TODO return 0; } +void release_SGD(void *optimizer) { + SGD_optimizer* o = (SGD_optimizer*)optimizer; + // nothing allocated on heap +} + paddle_optimizer* paddle_create_SGD_optimizer(double learning_rate) { SGD_optimizer* o = (SGD_optimizer*)malloc(sizeof(SGD_optimizer)); o->learning_rate = learning_rate; paddle_optimizer* container = (paddle_optimizer*)malloc(sizeof(paddle_optimizer)); - container->func = paddle_SGD_update_parameter; + container->update = update_SGD; + container->release = release_SGD; container->optimizer = o; return container; } diff --git a/paddle/go/pserver/optimizer_test.go b/paddle/go/pserver/optimizer_test.go new file mode 100644 index 00000000000..64d6d092aa1 --- /dev/null +++ b/paddle/go/pserver/optimizer_test.go @@ -0,0 +1,8 @@ +package pserver + +import "testing" + +func TestSGDCreateRelease(t *testing.T) { + o := newOptimizer(sgd, 1) + o.Cleanup() +} -- GitLab