提交 3519c630 编写于 作者: L livc 提交者: Yu Yang

complete some functions of c-api.

上级 97c64254
......@@ -75,34 +75,66 @@ int PDArgsGetIds(PD_Arguments args, uint64_t ID, PD_IVector ids) {
int PDArgsSetIds(PD_Arguments args, uint64_t ID, PD_IVector ids) {
//! TODO(lizhao): Complete this method.
return kPD_UNDEFINED_ERROR;
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;
}
int PDArgsSetSequenceStartPos(PD_Arguments args,
uint64_t ID,
PD_IVector seqPos) {
//! TODO(lizhao): Complete this method.
return kPD_UNDEFINED_ERROR;
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;
}
int PDArgsSetSubSequenceStartPos(PD_Arguments args,
uint64_t ID,
PD_IVector subSeqPos) {
//! TODO(lizhao): Complete this method.
return kPD_UNDEFINED_ERROR;
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;
a->args[ID].sequenceStartPositions =
std::make_shared<paddle::ICpuGpuVector>(iv->vec);
return kPD_NO_ERROR;
}
int PDArgsGetSequenceStartPos(PD_Arguments args,
uint64_t ID,
PD_IVector seqPos) {
//! TODO(lizhao): Complete this method.
return kPD_UNDEFINED_ERROR;
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;
std::make_shared<paddle::ICpuGpuVector>(iv->vec) =
a->args[ID].sequenceStartPositions;
return kPD_NO_ERROR;
}
int PDArgsGetSubSequenceStartPos(PD_Arguments args,
uint64_t ID,
PD_IVector subSeqPos) {
//! TODO(lizhao): Complete this method.
return kPD_UNDEFINED_ERROR;
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;
std::make_shared<paddle::ICpuGpuVector>(iv->vec) =
a->args[ID].sequenceStartPositions;
return kPD_NO_ERROR;
}
}
......@@ -28,7 +28,16 @@ int PDIVecCreateNone(PD_IVector* ivec) {
int PDIVectorCreate(PD_IVector* ivec, int* array, uint64_t size, bool copy) {
//! TODO(lizhao): Complete this method.
return kPD_UNDEFINED_ERROR;
if (ivec == nullptr) return kPD_NULLPTR;
auto ptr = new paddle::capi::CIVector();
if (copy) {
ptr->vec = paddle::IVector::create(size, false);
ptr->vec->copyFrom(array, size);
} else {
ptr->vec = paddle::IVector::create(array, size, false);
}
*ivec = ptr;
return kPD_NO_ERROR;
}
int PDIVecDestroy(PD_IVector ivec) {
......@@ -47,11 +56,19 @@ int PDIVectorGet(PD_IVector ivec, int** buffer) {
int PDIVectorResize(PD_IVector ivec, uint64_t size) {
//! TODO(lizhao): Complete this method.
return kPD_UNDEFINED_ERROR;
if (ivec == nullptr) return kPD_NULLPTR;
auto v = cast<paddle::capi::CIVector>(ivec);
if (v->vec == nullptr) return kPD_NULLPTR;
v->vec->resize(size);
return kPD_NO_ERROR;
}
int PDIVectorGetSize(PD_IVector ivec, uint64_t* size) {
//! TODO(lizhao): Complete this method.
return kPD_UNDEFINED_ERROR;
if (ivec == nullptr) return kPD_NULLPTR;
auto v = cast<paddle::capi::CIVector>(ivec);
if (v->vec == nullptr) return kPD_NULLPTR;
*size = v->vec->getSize();
return kPD_NO_ERROR;
}
}
......@@ -70,3 +70,39 @@ TEST(CAPIArguments, value) {
ASSERT_EQ(kPD_NO_ERROR, PDMatDestroy(mat));
ASSERT_EQ(kPD_NO_ERROR, PDArgsDestroy(args));
}
TEST(CAPIArguments, ids) {
PD_Arguments args;
ASSERT_EQ(kPD_NO_ERROR, PDArgsCreateNone(&args));
ASSERT_EQ(kPD_NO_ERROR, PDArgsResize(args, 1));
PD_IVector ivec;
int array[3] = {1, 2, 3};
ASSERT_EQ(kPD_NO_ERROR, PDIVectorCreate(&ivec, array, 3, true));
ASSERT_EQ(kPD_NO_ERROR, PDArgsSetIds(args, 0, ivec));
PD_IVector val;
ASSERT_EQ(kPD_NO_ERROR, PDIVecCreateNone(&val));
ASSERT_EQ(kPD_NO_ERROR, PDArgsGetIds(args, 0, val));
ASSERT_EQ(kPD_NO_ERROR, PDIVecDestroy(ivec));
ASSERT_EQ(kPD_NO_ERROR, PDIVecDestroy(val));
ASSERT_EQ(kPD_NO_ERROR, PDArgsDestroy(args));
}
TEST(CAPIArguments, Sequence) {
PD_Arguments args;
ASSERT_EQ(kPD_NO_ERROR, PDArgsCreateNone(&args));
ASSERT_EQ(kPD_NO_ERROR, PDArgsResize(args, 1));
PD_IVector ivec;
int array[3] = {1, 2, 3};
ASSERT_EQ(kPD_NO_ERROR, PDIVectorCreate(&ivec, array, 3, true));
ASSERT_EQ(kPD_NO_ERROR, PDArgsSetSequenceStartPos(args, 0, ivec));
PD_IVector val;
ASSERT_EQ(kPD_NO_ERROR, PDIVecCreateNone(&val));
ASSERT_EQ(kPD_NO_ERROR, PDArgsGetSequenceStartPos(args, 0, val));
ASSERT_EQ(kPD_NO_ERROR, PDIVecDestroy(ivec));
ASSERT_EQ(kPD_NO_ERROR, PDIVecDestroy(val));
ASSERT_EQ(kPD_NO_ERROR, PDArgsDestroy(args));
}
......@@ -17,10 +17,17 @@ limitations under the License. */
TEST(CAPIVector, create) {
PD_IVector vec;
ASSERT_EQ(kPD_NO_ERROR, PDIVecCreateNone(&vec));
int array[3] = {1, 2, 3};
ASSERT_EQ(kPD_NO_ERROR, PDIVectorCreate(&vec, array, 3, true));
ASSERT_EQ(kPD_NO_ERROR, PDIVectorCreate(&vec, array, 3, false));
ASSERT_EQ(kPD_NO_ERROR, PDIVectorResize(vec, 1000));
uint64_t size;
ASSERT_EQ(kPD_NO_ERROR, PDIVectorGetSize(vec, &size));
ASSERT_EQ(1000, size);
ASSERT_EQ(kPD_NO_ERROR, PDIVecDestroy(vec));
}
TEST(CAPIVector, createNone) {
PD_IVector vec;
ASSERT_EQ(kPD_NO_ERROR, PDIVecCreateNone(&vec));
ASSERT_EQ(kPD_NO_ERROR, PDIVecDestroy(vec));
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册