test_cclient.c 3.3 KB
Newer Older
D
dongzhihong 已提交
1 2
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

L
Luo Tao 已提交
3 4 5
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
D
dongzhihong 已提交
6

L
Luo Tao 已提交
7
    http://www.apache.org/licenses/LICENSE-2.0
D
dongzhihong 已提交
8

L
Luo Tao 已提交
9 10 11 12 13
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
D
dongzhihong 已提交
14

15 16 17 18 19
#include <stdio.h>
#include <stdlib.h>

#include "libpaddle_pserver_cclient.h"

D
dzhwinter 已提交
20 21 22 23
// TODO(helin): Fix: gtest using cmake is not working, using this
// hacky way for now.
#define fail()                                          \
  fprintf(stderr, "info: %s:%d: ", __FILE__, __LINE__); \
24
  exit(-1);
D
dzhwinter 已提交
25 26 27 28 29 30 31 32

void sendGrads(paddle_pserver_client c) {
  unsigned char grad_a[2000] = {2};
  unsigned char grad_b[3000] = {3};
  paddle_gradient grad1 = {
      "param_a", PADDLE_ELEMENT_TYPE_FLOAT32, grad_a, 2000};
  paddle_gradient grad2 = {
      "param_b", PADDLE_ELEMENT_TYPE_FLOAT32, grad_b, 3000};
D
dongzhihong 已提交
33
  paddle_gradient *grads[2] = {&grad1, &grad2};
D
dzhwinter 已提交
34 35 36
  if (paddle_send_grads(c, grads, 2)) {
    fail();
  }
37 38
}

D
dzhwinter 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
void getParams(paddle_pserver_client c) {
  paddle_parameter param_a;
  paddle_parameter param_b;
  char name_a[] = "param_a";
  char name_b[] = "param_b";
  // Must pre-allocate the prameter content before calling paddle_get_params.
  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;

D
dongzhihong 已提交
56
  paddle_parameter *params[2] = {&param_a, &param_b};
D
dzhwinter 已提交
57 58
  if (paddle_get_params(c, params, 2)) {
    fail();
59 60 61 62 63
  }
}

int main() {
  char addr[] = "localhost:3000";
Q
qiaolongfei 已提交
64
  paddle_pserver_client c = paddle_new_pserver_client(addr, 1);
D
dongzhihong 已提交
65
  char *config_proto;
D
dzhwinter 已提交
66 67
  size_t config_proto_len = 0;
  ssize_t nread;
D
dongzhihong 已提交
68
  FILE *fp = fopen("testdata/optimizer.pb", "r");
D
dongzhihong 已提交
69 70 71 72
  if (!fp) {
    fail();
  }
  while ((nread = getline(&config_proto, &config_proto_len, fp)) != -1) {
D
dzhwinter 已提交
73 74 75
    printf("%s", config_proto);
  }
  fclose(fp);
76 77
retry:
  if (paddle_begin_init_params(c)) {
D
dzhwinter 已提交
78 79 80 81 82 83 84 85 86
    paddle_parameter param;
    char name_a[] = "param_a";
    char name_b[] = "param_b";
    unsigned char content_a[2000] = {1};
    unsigned char content_b[3000] = {0};
    param.element_type = PADDLE_ELEMENT_TYPE_FLOAT32;
    param.name = name_a;
    param.content = content_a;
    param.content_len = 2000;
D
dongzhihong 已提交
87 88
    int error =
        paddle_init_param(c, param, (void *)config_proto, config_proto_len);
D
dzhwinter 已提交
89
    if (error != 0) {
90 91
      goto retry;
    }
D
dzhwinter 已提交
92 93 94 95 96

    param.element_type = PADDLE_ELEMENT_TYPE_FLOAT32;
    param.name = name_b;
    param.content = content_b;
    param.content_len = 3000;
D
dongzhihong 已提交
97
    error = paddle_init_param(c, param, (void *)config_proto, config_proto_len);
D
dzhwinter 已提交
98
    if (error != 0) {
99 100
      goto retry;
    }
D
dzhwinter 已提交
101 102 103

    error = paddle_finish_init_params(c);
    if (error != 0) {
104 105 106 107
      goto retry;
    }
  }

D
dzhwinter 已提交
108 109 110 111
  int i;
  for (i = 0; i < 100; i++) {
    sendGrads(c);
    getParams(c);
112 113 114 115
  }

  return 0;
}