Arguments.cpp 4.6 KB
Newer Older
Y
Yu Yang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

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

    http://www.apache.org/licenses/LICENSE-2.0

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. */

Y
Yu Yang 已提交
15 16 17
#include "PaddleCAPI.h"
#include "PaddleCAPIPrivate.h"

Y
Yu Yang 已提交
18 19 20 21
using paddle::capi::cast;

#define castArg(v) cast<paddle::capi::CArguments>(v)
#define castIVec(v) cast<paddle::capi::CIVector>(v)
Y
Yu Yang 已提交
22 23 24 25 26

extern "C" {
int PDArgsCreateNone(PD_Arguments* args) {
  auto ptr = new paddle::capi::CArguments();
  *args = ptr;
Y
Yu Yang 已提交
27
  return kPD_NO_ERROR;
Y
Yu Yang 已提交
28 29 30
}

int PDArgsDestroy(PD_Arguments args) {
Y
Yu Yang 已提交
31 32 33
  if (args == nullptr) return kPD_NULLPTR;
  delete castArg(args);
  return kPD_NO_ERROR;
Y
Yu Yang 已提交
34 35 36
}

int PDArgsGetSize(PD_Arguments args, uint64_t* size) {
Y
Yu Yang 已提交
37 38 39
  if (args == nullptr || size == nullptr) return kPD_NULLPTR;
  *size = castArg(args)->args.size();
  return kPD_NO_ERROR;
Y
Yu Yang 已提交
40 41 42
}

int PDArgsResize(PD_Arguments args, uint64_t size) {
Y
Yu Yang 已提交
43 44 45
  if (args == nullptr) return kPD_NULLPTR;
  castArg(args)->args.resize(size);
  return kPD_NO_ERROR;
Y
Yu Yang 已提交
46 47 48
}

int PDArgsSetValue(PD_Arguments args, uint64_t ID, PD_Matrix mat) {
Y
Yu Yang 已提交
49
  if (args == nullptr || mat == nullptr) return kPD_NULLPTR;
Y
Yu Yang 已提交
50
  auto m = paddle::capi::cast<paddle::capi::CMatrix>(mat);
Y
Yu Yang 已提交
51 52 53
  if (m->mat == nullptr) return kPD_NULLPTR;
  auto a = castArg(args);
  if (ID >= a->args.size()) return kPD_OUT_OF_RANGE;
Y
Yu Yang 已提交
54
  a->args[ID].value = m->mat;
Y
Yu Yang 已提交
55
  return kPD_NO_ERROR;
Y
Yu Yang 已提交
56 57 58
}

int PDArgsGetValue(PD_Arguments args, uint64_t ID, PD_Matrix mat) {
Y
Yu Yang 已提交
59
  if (args == nullptr || mat == nullptr) return kPD_NULLPTR;
Y
Yu Yang 已提交
60
  auto m = paddle::capi::cast<paddle::capi::CMatrix>(mat);
Y
Yu Yang 已提交
61 62
  auto a = castArg(args);
  if (ID >= a->args.size()) return kPD_OUT_OF_RANGE;
Y
Yu Yang 已提交
63
  m->mat = a->args[ID].value;
Y
Yu Yang 已提交
64 65 66 67 68 69 70 71 72 73
  return kPD_NO_ERROR;
}

int PDArgsGetIds(PD_Arguments args, uint64_t ID, PD_IVector ids) {
  if (args == nullptr || ids == nullptr) return kPD_NULLPTR;
  auto iv = castIVec(ids);
  auto a = castArg(args);
  if (ID >= a->args.size()) return kPD_OUT_OF_RANGE;
  iv->vec = a->args[ID].ids;
  return kPD_NO_ERROR;
Y
Yu Yang 已提交
74
}
Y
Yu Yang 已提交
75 76 77

int PDArgsSetIds(PD_Arguments args, uint64_t ID, PD_IVector ids) {
  //! TODO(lizhao): Complete this method.
L
livc 已提交
78 79 80 81 82 83 84
  if (args == nullptr || ids == nullptr) return kPD_NULLPTR;
  auto iv = paddle::capi::cast<paddle::capi::CIVector>(ids);
  if (iv->vec == nullptr) return kPD_NULLPTR;
  auto a = castArg(args);
  if (ID >= a->args.size()) return kPD_OUT_OF_RANGE;
  a->args[ID].ids = iv->vec;
  return kPD_NO_ERROR;
Y
Yu Yang 已提交
85 86 87 88 89
}

int PDArgsSetSequenceStartPos(PD_Arguments args,
                              uint64_t ID,
                              PD_IVector seqPos) {
L
livc 已提交
90 91 92 93 94 95 96 97
  if (args == nullptr || seqPos == nullptr) return kPD_NULLPTR;
  auto iv = paddle::capi::cast<paddle::capi::CIVector>(seqPos);
  if (iv->vec == nullptr) return kPD_NULLPTR;
  auto a = castArg(args);
  if (ID >= a->args.size()) return kPD_OUT_OF_RANGE;
  a->args[ID].sequenceStartPositions =
      std::make_shared<paddle::ICpuGpuVector>(iv->vec);
  return kPD_NO_ERROR;
Y
Yu Yang 已提交
98 99 100 101 102
}

int PDArgsSetSubSequenceStartPos(PD_Arguments args,
                                 uint64_t ID,
                                 PD_IVector subSeqPos) {
L
livc 已提交
103 104 105 106 107
  if (args == nullptr || subSeqPos == nullptr) return kPD_NULLPTR;
  auto iv = paddle::capi::cast<paddle::capi::CIVector>(subSeqPos);
  if (iv->vec == nullptr) return kPD_NULLPTR;
  auto a = castArg(args);
  if (ID >= a->args.size()) return kPD_OUT_OF_RANGE;
Y
Yu Yang 已提交
108
  a->args[ID].subSequenceStartPositions =
L
livc 已提交
109 110
      std::make_shared<paddle::ICpuGpuVector>(iv->vec);
  return kPD_NO_ERROR;
Y
Yu Yang 已提交
111 112 113 114 115
}

int PDArgsGetSequenceStartPos(PD_Arguments args,
                              uint64_t ID,
                              PD_IVector seqPos) {
L
livc 已提交
116 117 118 119
  if (args == nullptr || seqPos == nullptr) return kPD_NULLPTR;
  auto iv = castIVec(seqPos);
  auto a = castArg(args);
  if (ID >= a->args.size()) return kPD_OUT_OF_RANGE;
Y
Yu Yang 已提交
120 121
  paddle::Argument& arg = a->args[ID];
  iv->vec = arg.sequenceStartPositions->getMutableVector(false);
L
livc 已提交
122
  return kPD_NO_ERROR;
Y
Yu Yang 已提交
123 124 125 126 127
}

int PDArgsGetSubSequenceStartPos(PD_Arguments args,
                                 uint64_t ID,
                                 PD_IVector subSeqPos) {
L
livc 已提交
128 129 130 131
  if (args == nullptr || subSeqPos == nullptr) return kPD_NULLPTR;
  auto iv = castIVec(subSeqPos);
  auto a = castArg(args);
  if (ID >= a->args.size()) return kPD_OUT_OF_RANGE;
Y
Yu Yang 已提交
132 133
  paddle::Argument& arg = a->args[ID];
  iv->vec = arg.subSequenceStartPositions->getMutableVector(false);
L
livc 已提交
134
  return kPD_NO_ERROR;
Y
Yu Yang 已提交
135
}
Y
Yu Yang 已提交
136
}