main.c 2.4 KB
Newer Older
H
Helin Wang 已提交
1
#include <stdio.h>
2

3
#include "libpaddle_pserver_cclient.h"
4

H
Helin Wang 已提交
5
// TODO(helin): Fix: gtest using cmake is not working, using this
6 7 8
// hacky way for now.
#define fail()                                          \
  fprintf(stderr, "info: %s:%d: ", __FILE__, __LINE__); \
H
Helin Wang 已提交
9
  exit(-1);
10 11 12 13

void sendGrads(paddle_pserver_client c) {
  unsigned char grad_a[2000] = {2};
  unsigned char grad_b[3000] = {3};
Q
qiaolongfei 已提交
14 15 16
  paddle_gradient grad1 = {"param_a", PADDLE_ELEMENT_TYPE_FLOAT32, grad_a, 2000};
  paddle_gradient grad2 = {"param_b", PADDLE_ELEMENT_TYPE_FLOAT32, grad_b, 3000};
  paddle_gradient* grads[2] = {&grad1, &grad2};
17 18 19 20 21 22 23 24 25 26
  if (paddle_send_grads(c, grads, 2)) {
    fail();
  }
}

void getParams(paddle_pserver_client c) {
  paddle_parameter param_a;
  paddle_parameter param_b;
  char name_a[] = "param_a";
  char name_b[] = "param_b";
H
Helin Wang 已提交
27
  // Must pre-allocate the prameter content before calling paddle_get_params.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
  unsigned char content_a[2000] = {};
  unsigned char content_b[3000] = {};
  param_a.element_type = PADDLE_ELEMENT_TYPE_FLOAT32;
  param_a.name = name_a;
  param_a.content = content_a;
  param_a.content_len = 2000;
  param_b.element_type = PADDLE_ELEMENT_TYPE_FLOAT32;
  param_b.name = name_b;
  param_b.content = content_b;
  param_b.content_len = 3000;

  paddle_parameter* params[2] = {&param_a, &param_b};
  if (paddle_get_params(c, params, 2)) {
    fail();
  }
H
Helin Wang 已提交
43 44
}

45
int main() {
H
Helin Wang 已提交
46
  char addr[] = "localhost:3000";
47
  paddle_pserver_client c = paddle_new_pserver_client(addr, 1);
H
Helin Wang 已提交
48
retry:
H
Helin Wang 已提交
49
  if (paddle_begin_init_params(c)) {
H
Helin Wang 已提交
50 51 52
    paddle_parameter param;
    char name_a[] = "param_a";
    char name_b[] = "param_b";
53 54
    unsigned char content_a[2000] = {1};
    unsigned char content_b[3000] = {0};
H
Helin Wang 已提交
55 56
    param.element_type = PADDLE_ELEMENT_TYPE_FLOAT32;
    param.name = name_a;
57 58 59 60
    param.content = content_a;
    param.content_len = 2000;
    int error = paddle_init_param(c, param, NULL, 0);
    if (error != 0) {
H
Helin Wang 已提交
61 62
      goto retry;
    }
63 64

    param.element_type = PADDLE_ELEMENT_TYPE_FLOAT32;
H
Helin Wang 已提交
65
    param.name = name_b;
66 67 68 69
    param.content = content_b;
    param.content_len = 3000;
    error = paddle_init_param(c, param, NULL, 0);
    if (error != 0) {
H
Helin Wang 已提交
70 71
      goto retry;
    }
72 73 74

    error = paddle_finish_init_params(c);
    if (error != 0) {
H
Helin Wang 已提交
75 76 77 78
      goto retry;
    }
  }

79 80 81
  for (int i = 0; i < 100; i++) {
    sendGrads(c);
    getParams(c);
H
Helin Wang 已提交
82 83
  }

84
  if (paddle_save_model(c, "/tmp/")) {
H
Helin Wang 已提交
85
    fail();
H
Helin Wang 已提交
86 87
  }

88 89
  return 0;
}