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

3
#include "libpaddle_pserver_cclient.h"
4

H
Helin Wang 已提交
5
void fail() {
H
Helin Wang 已提交
6 7
  // TODO(helin): fix: gtest using cmake is not working, using this
  // hacky way for now.
H
Helin Wang 已提交
8 9
  printf("test failed.\n");
  exit(-1);
H
Helin Wang 已提交
10 11
}

12
int main() {
H
Helin Wang 已提交
13
  char addr[] = "localhost:3000";
H
Helin Wang 已提交
14
  client c = paddle_new_pserver_client(addr, 1);
H
Helin Wang 已提交
15
retry:
H
Helin Wang 已提交
16
  if (paddle_begin_init_params(c)) {
H
Helin Wang 已提交
17 18 19
    paddle_parameter param;
    char name_a[] = "param_a";
    char name_b[] = "param_b";
20
    unsigned char content[] = {0x00, 0x11, 0x22};
H
Helin Wang 已提交
21 22
    param.element_type = PADDLE_ELEMENT_TYPE_FLOAT32;
    param.name = name_a;
23
    param.content = content;
H
Helin Wang 已提交
24 25 26 27 28 29
    param.content_len = 3;
    if (paddle_init_param(c, param, NULL, 0) != 0) {
      goto retry;
    }
    param.element_type = PADDLE_ELEMENT_TYPE_INT32;
    param.name = name_b;
30
    param.content = content;
H
Helin Wang 已提交
31 32 33 34 35 36 37 38
    param.content_len = 3;
    if (paddle_init_param(c, param, NULL, 0) != 0) {
      goto retry;
    }
    if (paddle_finish_init_params(c) != 0) {
      goto retry;
    }
  } else {
H
Helin Wang 已提交
39
    fail();
H
Helin Wang 已提交
40 41
  }

42 43
  unsigned char content[] = {0x00, 0x11, 0x22};
  paddle_gradient** grads =
Q
qiaolongfei 已提交
44
      (paddle_gradient**)malloc(sizeof(paddle_gradient*) * 2);
45 46 47 48 49 50 51 52 53 54 55 56 57
  grads[0] = (paddle_gradient*)malloc(sizeof(paddle_gradient));
  grads[0]->name = "param_a";
  grads[0]->content = content;
  grads[0]->content_len = 3;
  grads[0]->element_type = PADDLE_ELEMENT_TYPE_FLOAT32;

  grads[1] = (paddle_gradient*)malloc(sizeof(paddle_gradient));
  grads[1]->name = "param_b";
  grads[1]->content = content;
  grads[1]->content_len = 3;
  grads[1]->element_type = PADDLE_ELEMENT_TYPE_INT32;

  if (paddle_send_grads(c, grads, 2) != 0) {
H
Helin Wang 已提交
58
    fail();
H
Helin Wang 已提交
59 60
  }

H
Helin Wang 已提交
61
  paddle_parameter* params[2] = {NULL, NULL};
H
Helin Wang 已提交
62
  char* names[] = {"param_a", "param_b"};
63
  if (paddle_get_params(c, names, params, 2) != 0) {
H
Helin Wang 已提交
64
    fail();
H
Helin Wang 已提交
65 66
  }

H
Helin Wang 已提交
67
  // get parameters again by reusing the allocated parameter buffers.
68
  if (paddle_get_params(c, names, params, 2) != 0) {
H
Helin Wang 已提交
69
    fail();
H
Helin Wang 已提交
70 71
  }

H
Helin Wang 已提交
72 73
  paddle_release_param(params[0]);
  paddle_release_param(params[1]);
H
Helin Wang 已提交
74

75
  if (paddle_save_model(c, "/tmp/") != 0) {
H
Helin Wang 已提交
76
    fail();
H
Helin Wang 已提交
77 78
  }

79 80
  return 0;
}