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

4
#include "libpaddle_pserver_cclient.h"
5

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

13 14 15 16 17 18 19 20 21 22 23
void print_parameter(paddle_gradient* param) {
  if (param == NULL) {
    printf("param is NULL!!\n");
  } else {
    printf("==== parameter ====\n");
    printf("name: %s\n", param->name);
    printf("content_len: %d\n", param->content_len);
    printf("content_type: %d\n", param->element_type);
    for (int i = 0; i < param->content_len; ++i) {
      printf("0x%x ", param->content[i]);
    }
24
    printf("\n\n");
25 26 27
  }
}

28
int main() {
H
Helin Wang 已提交
29
  char addr[] = "localhost:3000";
H
Helin Wang 已提交
30
  client c = paddle_new_pserver_client(addr, 1);
H
Helin Wang 已提交
31
retry:
H
Helin Wang 已提交
32
  if (paddle_begin_init_params(c)) {
H
Helin Wang 已提交
33 34 35
    paddle_parameter param;
    char name_a[] = "param_a";
    char name_b[] = "param_b";
36
    unsigned char content1[] = {0x01, 0x02, 0x03};
H
Helin Wang 已提交
37 38
    param.element_type = PADDLE_ELEMENT_TYPE_FLOAT32;
    param.name = name_a;
39
    param.content = content1;
H
Helin Wang 已提交
40 41 42 43
    param.content_len = 3;
    if (paddle_init_param(c, param, NULL, 0) != 0) {
      goto retry;
    }
44
    unsigned char content2[] = {0x04, 0x05, 0x06};
H
Helin Wang 已提交
45 46
    param.element_type = PADDLE_ELEMENT_TYPE_INT32;
    param.name = name_b;
47
    param.content = content2;
H
Helin Wang 已提交
48 49 50 51
    param.content_len = 3;
    if (paddle_init_param(c, param, NULL, 0) != 0) {
      goto retry;
    }
52

H
Helin Wang 已提交
53 54 55 56
    if (paddle_finish_init_params(c) != 0) {
      goto retry;
    }
  } else {
H
Helin Wang 已提交
57
    fail();
H
Helin Wang 已提交
58 59
  }

60 61 62 63 64 65 66 67 68 69
  unsigned char content1[] = {0x12, 0x23, 0x34};
  unsigned char content2[] = {0x45, 0x56, 0x67};

  paddle_gradient** new_params =
      (paddle_gradient**)malloc(sizeof(paddle_gradient*) * 2);
  new_params[0] = (paddle_gradient*)malloc(sizeof(paddle_gradient));
  new_params[0]->name = "param_a";
  new_params[0]->content = content1;
  new_params[0]->content_len = 3;
  new_params[0]->element_type = PADDLE_ELEMENT_TYPE_FLOAT32;
H
Helin Wang 已提交
70

71 72 73 74 75 76 77 78 79 80
  new_params[1] = (paddle_gradient*)malloc(sizeof(paddle_gradient));
  new_params[1]->name = "param_b";
  new_params[1]->content = content2;
  new_params[1]->content_len = 3;
  new_params[1]->element_type = PADDLE_ELEMENT_TYPE_INT32;

  print_parameter(new_params[0]);
  print_parameter(new_params[1]);

  if (paddle_send_grads(c, new_params, 2) != 0) {
H
Helin Wang 已提交
81
    fail();
H
Helin Wang 已提交
82 83
  }

H
Helin Wang 已提交
84
  paddle_parameter* params[2] = {NULL, NULL};
H
Helin Wang 已提交
85
  char* names[] = {"param_a", "param_b"};
86
  if (paddle_get_params(c, names, params, 2) != 0) {
H
Helin Wang 已提交
87
    fail();
H
Helin Wang 已提交
88 89
  }

90 91 92 93 94 95 96 97 98
  print_parameter(params[0]);
  print_parameter(params[1]);

  /// change name of parameter.
  char* names2[] = {"param_1", "param_2"};
  if (paddle_get_params(c, names2, params, 2) == 0) {
    fail();
  }

H
Helin Wang 已提交
99
  // get parameters again by reusing the allocated parameter buffers.
100
  if (paddle_get_params(c, names, params, 2) != 0) {
H
Helin Wang 已提交
101
    fail();
H
Helin Wang 已提交
102 103
  }

H
Helin Wang 已提交
104 105
  paddle_release_param(params[0]);
  paddle_release_param(params[1]);
H
Helin Wang 已提交
106

107
  if (paddle_save_model(c, "/tmp/") != 0) {
H
Helin Wang 已提交
108
    fail();
H
Helin Wang 已提交
109 110
  }

111
  printf("test success!\n");
112 113
  return 0;
}