test_cclient.c 3.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#include <stdio.h>
#include <stdlib.h>

#include "libpaddle_pserver_cclient.h"

typedef float real;

void fail() {
  // TODO(helin): fix: gtest using cmake is not working, using this
  // hacky way for now.
  printf("test failed.\n");
  exit(-1);
}

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);
Q
qiaolongfei 已提交
23 24
    int i;
    for (i = 0; i < param->content_len / sizeof(real); ++i) {
Q
qiaolongfei 已提交
25
      printf("%f ", ((float*)param->content)[i]);
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
    }
    printf("\n\n");
  }
}

int main() {
  char addr[] = "localhost:3000";
  client c = paddle_new_pserver_client(addr, 1);

  char* names[] = {"param_a", "param_b"};
retry:

  if (paddle_begin_init_params(c)) {
    paddle_parameter param;
    real param_content1[] = {0.1, 0.2, 0.3};
    param.element_type = PADDLE_ELEMENT_TYPE_FLOAT32;
    param.name = names[0];
    param.content = (unsigned char*)param_content1;
    param.content_len = 3 * sizeof(real);
    if (paddle_init_param(c, param, NULL, 0) != 0) {
      goto retry;
    }
    real param_content2[] = {0.4, 0.5, 0.6};
    param.element_type = PADDLE_ELEMENT_TYPE_INT32;
    param.name = names[1];
    param.content = (unsigned char*)param_content2;
    param.content_len = 3 * sizeof(real);
    if (paddle_init_param(c, param, NULL, 0) != 0) {
      goto retry;
    }

    if (paddle_finish_init_params(c) != 0) {
      goto retry;
    }
  } else {
    fail();
  }

  printf("get initialized parameters from pserver:\n");
  paddle_parameter* param_ptrs[2] = {NULL, NULL};
  if (paddle_get_params(c, names, param_ptrs, 2) != 0) {
    fail();
  }
  print_parameter(param_ptrs[0]);
  print_parameter(param_ptrs[1]);

  printf("send gradient to pserver:\n");
  real gradient_content1[] = {0.01, 0.02, 0.03};
  real gradinet_content2[] = {0.04, 0.05, 0.06};

  paddle_gradient** grads =
      (paddle_gradient**)malloc(sizeof(paddle_gradient*) * 2);
  grads[0] = (paddle_gradient*)malloc(sizeof(paddle_gradient));
  grads[0]->name = names[0];
  grads[0]->content = (unsigned char*)gradient_content1;
  grads[0]->content_len = 3 * sizeof(real);
  grads[0]->element_type = PADDLE_ELEMENT_TYPE_FLOAT32;

  grads[1] = (paddle_gradient*)malloc(sizeof(paddle_gradient));
  grads[1]->name = names[1];
  grads[1]->content = (unsigned char*)gradinet_content2;
  grads[1]->content_len = 3 * sizeof(real);
  grads[1]->element_type = PADDLE_ELEMENT_TYPE_INT32;

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

  if (paddle_send_grads(c, grads, 2) != 0) {
    fail();
  }

  printf("get updated parameters from pserver:\n");
  // get parameters again by reusing the allocated parameter buffers.
  if (paddle_get_params(c, names, param_ptrs, 2) != 0) {
    fail();
  }

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

  paddle_release_param(param_ptrs[0]);
  paddle_release_param(param_ptrs[1]);

  if (paddle_save_model(c, "/tmp/") != 0) {
    fail();
  }

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