diff --git a/paddle/capi/Arguments.cpp b/paddle/capi/Arguments.cpp index 678a80ac3f4d5ca5aa3321d0b089fe5a8c465af1..baabd44cc0c63caea6838ada9ea94e2b71876ea1 100644 --- a/paddle/capi/Arguments.cpp +++ b/paddle/capi/Arguments.cpp @@ -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(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(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(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(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(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(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(iv->vec) = + a->args[ID].sequenceStartPositions; + return kPD_NO_ERROR; } } diff --git a/paddle/capi/Vector.cpp b/paddle/capi/Vector.cpp index a2e6f3507de3d1fcc3693bf6fce42e8ed5991644..38a5fbc00a8bebd524816d8a39364d7ca0c196c8 100644 --- a/paddle/capi/Vector.cpp +++ b/paddle/capi/Vector.cpp @@ -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(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(ivec); + if (v->vec == nullptr) return kPD_NULLPTR; + *size = v->vec->getSize(); + return kPD_NO_ERROR; } } diff --git a/paddle/capi/tests/test_Arguments.cpp b/paddle/capi/tests/test_Arguments.cpp index 4a18ffbf47413477ef4bc94af70a1a34e7471bf4..1186d2921ba463b9b7e4156e9985e458d52484b0 100644 --- a/paddle/capi/tests/test_Arguments.cpp +++ b/paddle/capi/tests/test_Arguments.cpp @@ -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)); +} diff --git a/paddle/capi/tests/test_Vector.cpp b/paddle/capi/tests/test_Vector.cpp index 2697e80b92c220897a17325b1e7549403ba85ba8..547d0ef20d16865b376ee80045c14a3002ee70d3 100644 --- a/paddle/capi/tests/test_Vector.cpp +++ b/paddle/capi/tests/test_Vector.cpp @@ -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)); }