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 24 25 26 27
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]);
    }
    printf("\n");
  }
}

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 content[] = {0x00, 0x00, 0x00};
H
Helin Wang 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49 50
    param.element_type = PADDLE_ELEMENT_TYPE_FLOAT32;
    param.name = name_a;
    param.content = content;
    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;
    param.content = content;
    param.content_len = 3;
    if (paddle_init_param(c, param, NULL, 0) != 0) {
      goto retry;
    }
51

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

59 60 61 62 63 64 65 66 67 68
  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 已提交
69

70 71 72 73 74 75 76 77 78 79
  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 已提交
80
    fail();
H
Helin Wang 已提交
81 82
  }

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

89 90 91 92 93 94 95 96 97
  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 已提交
98
  // get parameters again by reusing the allocated parameter buffers.
99
  if (paddle_get_params(c, names, params, 2) != 0) {
H
Helin Wang 已提交
100
    fail();
H
Helin Wang 已提交
101 102
  }

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

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

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