diff --git a/paddle/capi/Arguments.cpp b/paddle/capi/Arguments.cpp index 8d00bda3cb90cda9950533fff86c7c2f54b0448e..3d60165962d5d53e0df9053c0698fa90c69c1e94 100644 --- a/paddle/capi/Arguments.cpp +++ b/paddle/capi/Arguments.cpp @@ -21,31 +21,27 @@ using paddle::capi::cast; #define castIVec(v) cast(v) extern "C" { -int PDArgsCreateNone(PD_Arguments* args) { - auto ptr = new paddle::capi::CArguments(); - *args = ptr; - return kPD_NO_ERROR; -} +PD_Arguments PDArgsCreateNone() { return new paddle::capi::CArguments(); } -int PDArgsDestroy(PD_Arguments args) { +PD_Error PDArgsDestroy(PD_Arguments args) { if (args == nullptr) return kPD_NULLPTR; delete castArg(args); return kPD_NO_ERROR; } -int PDArgsGetSize(PD_Arguments args, uint64_t* size) { +PD_Error PDArgsGetSize(PD_Arguments args, uint64_t* size) { if (args == nullptr || size == nullptr) return kPD_NULLPTR; *size = castArg(args)->args.size(); return kPD_NO_ERROR; } -int PDArgsResize(PD_Arguments args, uint64_t size) { +PD_Error PDArgsResize(PD_Arguments args, uint64_t size) { if (args == nullptr) return kPD_NULLPTR; castArg(args)->args.resize(size); return kPD_NO_ERROR; } -int PDArgsSetValue(PD_Arguments args, uint64_t ID, PD_Matrix mat) { +PD_Error PDArgsSetValue(PD_Arguments args, uint64_t ID, PD_Matrix mat) { if (args == nullptr || mat == nullptr) return kPD_NULLPTR; auto m = paddle::capi::cast(mat); if (m->mat == nullptr) return kPD_NULLPTR; @@ -55,7 +51,7 @@ int PDArgsSetValue(PD_Arguments args, uint64_t ID, PD_Matrix mat) { return kPD_NO_ERROR; } -int PDArgsGetValue(PD_Arguments args, uint64_t ID, PD_Matrix mat) { +PD_Error PDArgsGetValue(PD_Arguments args, uint64_t ID, PD_Matrix mat) { if (args == nullptr || mat == nullptr) return kPD_NULLPTR; auto m = paddle::capi::cast(mat); auto a = castArg(args); @@ -64,7 +60,7 @@ int PDArgsGetValue(PD_Arguments args, uint64_t ID, PD_Matrix mat) { return kPD_NO_ERROR; } -int PDArgsGetIds(PD_Arguments args, uint64_t ID, PD_IVector ids) { +PD_Error 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); @@ -73,7 +69,7 @@ int PDArgsGetIds(PD_Arguments args, uint64_t ID, PD_IVector ids) { return kPD_NO_ERROR; } -int PDArgsSetIds(PD_Arguments args, uint64_t ID, PD_IVector ids) { +PD_Error PDArgsSetIds(PD_Arguments args, uint64_t ID, PD_IVector ids) { //! TODO(lizhao): Complete this method. if (args == nullptr || ids == nullptr) return kPD_NULLPTR; auto iv = paddle::capi::cast(ids); @@ -84,9 +80,9 @@ int PDArgsSetIds(PD_Arguments args, uint64_t ID, PD_IVector ids) { return kPD_NO_ERROR; } -int PDArgsSetSequenceStartPos(PD_Arguments args, - uint64_t ID, - PD_IVector seqPos) { +PD_Error PDArgsSetSequenceStartPos(PD_Arguments args, + uint64_t ID, + PD_IVector seqPos) { if (args == nullptr || seqPos == nullptr) return kPD_NULLPTR; auto iv = paddle::capi::cast(seqPos); if (iv->vec == nullptr) return kPD_NULLPTR; @@ -97,9 +93,9 @@ int PDArgsSetSequenceStartPos(PD_Arguments args, return kPD_NO_ERROR; } -int PDArgsSetSubSequenceStartPos(PD_Arguments args, - uint64_t ID, - PD_IVector subSeqPos) { +PD_Error PDArgsSetSubSequenceStartPos(PD_Arguments args, + uint64_t ID, + PD_IVector subSeqPos) { if (args == nullptr || subSeqPos == nullptr) return kPD_NULLPTR; auto iv = paddle::capi::cast(subSeqPos); if (iv->vec == nullptr) return kPD_NULLPTR; @@ -110,9 +106,9 @@ int PDArgsSetSubSequenceStartPos(PD_Arguments args, return kPD_NO_ERROR; } -int PDArgsGetSequenceStartPos(PD_Arguments args, - uint64_t ID, - PD_IVector seqPos) { +PD_Error PDArgsGetSequenceStartPos(PD_Arguments args, + uint64_t ID, + PD_IVector seqPos) { if (args == nullptr || seqPos == nullptr) return kPD_NULLPTR; auto iv = castIVec(seqPos); auto a = castArg(args); @@ -122,9 +118,9 @@ int PDArgsGetSequenceStartPos(PD_Arguments args, return kPD_NO_ERROR; } -int PDArgsGetSubSequenceStartPos(PD_Arguments args, - uint64_t ID, - PD_IVector subSeqPos) { +PD_Error PDArgsGetSubSequenceStartPos(PD_Arguments args, + uint64_t ID, + PD_IVector subSeqPos) { if (args == nullptr || subSeqPos == nullptr) return kPD_NULLPTR; auto iv = castIVec(subSeqPos); auto a = castArg(args); diff --git a/paddle/capi/GradientMachine.cpp b/paddle/capi/GradientMachine.cpp index de3a339fa62a82b0ffdaef2af2c9faeb04b33302..ed0cfd884093512fe2440efe558cb9aad252b505 100644 --- a/paddle/capi/GradientMachine.cpp +++ b/paddle/capi/GradientMachine.cpp @@ -38,9 +38,9 @@ NeuralNetwork* newCustomNerualNetwork(const std::string& name, } // namespace paddle extern "C" { -int PDGradientMachineCreateForPredict(PD_GradientMachine* machine, - void* modelConfigProtobuf, - int size) { +PD_Error PDGradientMachineCreateForPredict(PD_GradientMachine* machine, + void* modelConfigProtobuf, + int size) { if (modelConfigProtobuf == nullptr) return kPD_NULLPTR; paddle::ModelConfig config; if (!config.ParseFromArray(modelConfigProtobuf, size) || @@ -55,13 +55,13 @@ int PDGradientMachineCreateForPredict(PD_GradientMachine* machine, return kPD_NO_ERROR; } -int PDGradientMachineDestroy(PD_GradientMachine machine) { +PD_Error PDGradientMachineDestroy(PD_GradientMachine machine) { delete cast(machine); return kPD_NO_ERROR; } -int PDGradientMachineLoadParameterFromDisk(PD_GradientMachine machine, - const char* path) { +PD_Error PDGradientMachineLoadParameterFromDisk(PD_GradientMachine machine, + const char* path) { auto m = cast(machine); if (m == nullptr || path == nullptr || m->machine == nullptr) return kPD_NULLPTR; @@ -69,10 +69,10 @@ int PDGradientMachineLoadParameterFromDisk(PD_GradientMachine machine, return kPD_NO_ERROR; } -int PDGradientMachineForward(PD_GradientMachine machine, - PD_Arguments inArgs, - PD_Arguments outArgs, - bool isTrain) { +PD_Error PDGradientMachineForward(PD_GradientMachine machine, + PD_Arguments inArgs, + PD_Arguments outArgs, + bool isTrain) { auto m = cast(machine); auto in = paddle::capi::cast(inArgs); auto out = paddle::capi::cast(outArgs); @@ -83,10 +83,10 @@ int PDGradientMachineForward(PD_GradientMachine machine, return kPD_NO_ERROR; } -int PDGradientMachineCreateSharedParam(PD_GradientMachine origin, - void* modelConfigProtobuf, - int size, - PD_GradientMachine* slave) { +PD_Error PDGradientMachineCreateSharedParam(PD_GradientMachine origin, + void* modelConfigProtobuf, + int size, + PD_GradientMachine* slave) { auto o = cast(origin); if (origin == nullptr || slave == nullptr || o->machine == nullptr) { return kPD_NULLPTR; diff --git a/paddle/capi/Main.cpp b/paddle/capi/Main.cpp index e310eb540475def6eabd7564b47b0eab55c0ee66..9314071b4bcf973ea0d4ed87caba67f90089e8dc 100644 --- a/paddle/capi/Main.cpp +++ b/paddle/capi/Main.cpp @@ -29,7 +29,7 @@ static void initPaddle(int argc, char** argv) { } extern "C" { -int PDInit(int argc, char** argv) { +PD_Error PDInit(int argc, char** argv) { std::vector realArgv; realArgv.reserve(argc + 1); realArgv.push_back(strdup("")); diff --git a/paddle/capi/Matrix.cpp b/paddle/capi/Matrix.cpp index db32c945412ecb5f25c20f7539f03550d46be3ed..bc25f84344c1b61c2464e705c6a7bb448c1c60fd 100644 --- a/paddle/capi/Matrix.cpp +++ b/paddle/capi/Matrix.cpp @@ -18,27 +18,22 @@ limitations under the License. */ #define cast(v) paddle::capi::cast(v) extern "C" { -int PDMatCreate(PD_Matrix* mat, uint64_t height, uint64_t width, bool useGpu) { +PD_Matrix PDMatCreate(uint64_t height, uint64_t width, bool useGpu) { auto ptr = new paddle::capi::CMatrix(); ptr->mat = paddle::Matrix::create(height, width, false, useGpu); - *mat = ptr; - return kPD_NO_ERROR; + return ptr; } -int PDMatCreateNone(PD_Matrix* mat) { - auto ptr = new paddle::capi::CMatrix(); - *mat = ptr; - return kPD_NO_ERROR; -} +PD_Matrix PDMatCreateNone() { return new paddle::capi::CMatrix(); } -int PDMatDestroy(PD_Matrix mat) { +PD_Error PDMatDestroy(PD_Matrix mat) { if (mat == nullptr) return kPD_NULLPTR; auto ptr = cast(mat); delete ptr; return kPD_NO_ERROR; } -int PDMatCopyToRow(PD_Matrix mat, uint64_t rowID, pd_real* rowArray) { +PD_Error PDMatCopyToRow(PD_Matrix mat, uint64_t rowID, pd_real* rowArray) { if (mat == nullptr) return kPD_NULLPTR; auto ptr = cast(mat); if (ptr->mat == nullptr) return kPD_NULLPTR; @@ -53,7 +48,7 @@ int PDMatCopyToRow(PD_Matrix mat, uint64_t rowID, pd_real* rowArray) { return kPD_NO_ERROR; } -int PDMatGetRow(PD_Matrix mat, uint64_t rowID, pd_real** rawRowBuffer) { +PD_Error PDMatGetRow(PD_Matrix mat, uint64_t rowID, pd_real** rawRowBuffer) { if (mat == nullptr) return kPD_NULLPTR; auto ptr = cast(mat); if (ptr->mat == nullptr) return kPD_NULLPTR; @@ -62,7 +57,7 @@ int PDMatGetRow(PD_Matrix mat, uint64_t rowID, pd_real** rawRowBuffer) { return kPD_NO_ERROR; } -int PDMatGetShape(PD_Matrix mat, uint64_t* height, uint64_t* width) { +PD_Error PDMatGetShape(PD_Matrix mat, uint64_t* height, uint64_t* width) { if (mat == nullptr) return kPD_NULLPTR; if (height != nullptr) { *height = cast(mat)->mat->getHeight(); diff --git a/paddle/capi/PaddleCAPI.h b/paddle/capi/PaddleCAPI.h index 6eea60ef749152a2a69c68874ffc1156cb1b81ae..94a9fc497f93ae1141e89386a8aa4c0e63090df2 100644 --- a/paddle/capi/PaddleCAPI.h +++ b/paddle/capi/PaddleCAPI.h @@ -49,48 +49,115 @@ typedef enum { */ typedef void* PD_IVector; -PD_API int PDIVecCreateNone(PD_IVector* ivec); +/** + * @brief Create an none int vector. It just a handler and store nothing. Used + * to get output from other api. + * @return None int vector. + */ +PD_API PD_IVector PDIVecCreateNone(); /** * @brief PDIVectorCreate create a paddle int vector - * @param [out] ivec: output int vector. - * @param [in] array: input array. - * @param [in] size: input array size. - * @param [in] copy: memory copy or just use same memory. True if copy. + * @param array: input array. + * @param size: input array size. + * @param copy: memory copy or just use same memory. True if copy. + * @param useGPU: True if use GPU * @return PD_Error */ -PD_API int PDIVectorCreate(PD_IVector* ivec, - int* array, - uint64_t size, - bool copy); +PD_API PD_IVector PDIVectorCreate(int* array, + uint64_t size, + bool copy, + bool useGPU); -PD_API int PDIVecDestroy(PD_IVector ivec); +/** + * @brief PDIVecDestroy destory an int vector. + * @param ivec vector to be destoried. + * @return PD_Error + */ +PD_API PD_Error PDIVecDestroy(PD_IVector ivec); -PD_API int PDIVectorGet(PD_IVector ivec, int** buffer); +/** + * @brief PDIVectorGet get raw buffer stored inside this int vector. It could be + * GPU memory if this int vector is stored in GPU. + * @param [in] ivec int vector + * @param [out] buffer the return buffer pointer. + * @return PD_Error + */ +PD_API PD_Error PDIVectorGet(PD_IVector ivec, int** buffer); -PD_API int PDIVectorResize(PD_IVector ivec, uint64_t size); +/** + * @brief PDIVectorResize resize the int vector. + * @param [in] ivec: int vector + * @param [in] size: size to change + * @return PD_Error + */ +PD_API PD_Error PDIVectorResize(PD_IVector ivec, uint64_t size); -PD_API int PDIVectorGetSize(PD_IVector ivec, uint64_t* size); +/** + * @brief PDIVectorGetSize get the size of int vector. + * @param [in] ivec: int vector + * @param [out] size: return size of this int vector. + * @return PD_Error + */ +PD_API PD_Error PDIVectorGetSize(PD_IVector ivec, uint64_t* size); /** * Matrix functions. Return will be a PD_Error type. */ typedef void* PD_Matrix; -PD_API int PDMatCreate(PD_Matrix* mat, - uint64_t height, - uint64_t width, - bool useGpu); +/** + * @brief PDMatCreate Create a dense matrix + * @param height matrix height. + * @param width matrix width + * @param useGpu use GPU of not + * @return Matrix handler + */ +PD_API PD_Matrix PDMatCreate(uint64_t height, uint64_t width, bool useGpu); -PD_API int PDMatDestroy(PD_Matrix mat); +/** + * @brief PDMatDestroy Destroy a matrix. + * @param mat + * @return PD_Error + */ +PD_API PD_Error PDMatDestroy(PD_Matrix mat); -PD_API int PDMatCopyToRow(PD_Matrix mat, uint64_t rowID, pd_real* rowArray); +/** + * @brief PDMatCopyToRow Copy a row to matrix. + * @param mat Target Matrix + * @param rowID Index of row + * @param rowArray Row data. + * @return PD_Error + */ +PD_API PD_Error PDMatCopyToRow(PD_Matrix mat, + uint64_t rowID, + pd_real* rowArray); -PD_API int PDMatGetRow(PD_Matrix mat, uint64_t rowID, pd_real** rawRowBuffer); +/** + * @brief PDMatGetRow Get raw row buffer from matrix + * @param [in] mat Target matrix + * @param [in] rowID Index of row. + * @param [out] rawRowBuffer Row Buffer + * @return PD_Error + */ +PD_API PD_Error PDMatGetRow(PD_Matrix mat, + uint64_t rowID, + pd_real** rawRowBuffer); -PD_API int PDMatCreateNone(PD_Matrix* mat); +/** + * @brief PDMatCreateNone Create None Matrix + * @return + */ +PD_API PD_Matrix PDMatCreateNone(); -PD_API int PDMatGetShape(PD_Matrix mat, uint64_t* height, uint64_t* width); +/** + * @brief PDMatGetShape get the shape of matrix + * @param mat target matrix + * @param height The height of matrix + * @param width The width of matrix + * @return PD_Error + */ +PD_API PD_Error PDMatGetShape(PD_Matrix mat, uint64_t* height, uint64_t* width); /** * Arguments functions. Each argument means layer output. Arguments means a @@ -98,65 +165,185 @@ PD_API int PDMatGetShape(PD_Matrix mat, uint64_t* height, uint64_t* width); */ typedef void* PD_Arguments; -PD_API int PDArgsCreateNone(PD_Arguments* args); - -PD_API int PDArgsDestroy(PD_Arguments args); +/** + * @brief PDArgsCreateNone Create a array of arguments, which size is zero. + * @return Arguemnts + */ +PD_API PD_Arguments PDArgsCreateNone(); -PD_API int PDArgsGetSize(PD_Arguments args, uint64_t* size); +/** + * @brief PDArgsDestroy Destroy the arguments + * @param args arguments to destroy + * @return PD_Error + */ +PD_API PD_Error PDArgsDestroy(PD_Arguments args); -PD_API int PDArgsResize(PD_Arguments args, uint64_t size); +/** + * @brief PDArgsGetSize Get size of arguments array + * @param [in] args arguments array + * @param [out] size array size + * @return PD_Error + */ +PD_API PD_Error PDArgsGetSize(PD_Arguments args, uint64_t* size); -PD_API int PDArgsSetValue(PD_Arguments args, uint64_t ID, PD_Matrix mat); +/** + * @brief PDArgsResize Resize a arguments array. + * @param args arguments array. + * @param size target size of array + * @return PD_Error + */ +PD_API PD_Error PDArgsResize(PD_Arguments args, uint64_t size); -PD_API int PDArgsGetValue(PD_Arguments args, uint64_t ID, PD_Matrix mat); +/** + * @brief PDArgsSetValue Set value matrix of one argument in array, which index + * is `ID`. + * @param args arguments array + * @param ID array index + * @param mat matrix pointer + * @return PD_Error + */ +PD_API PD_Error PDArgsSetValue(PD_Arguments args, uint64_t ID, PD_Matrix mat); -PD_API int PDArgsGetIds(PD_Arguments args, uint64_t ID, PD_IVector ids); +/** + * @brief PDArgsGetValue Get value matrix of one argument in array, which index + * is `ID`. + * @param [in] args arguments array + * @param [in] ID array index + * @param [out] mat matrix pointer + * @return PD_Error + */ +PD_API PD_Error PDArgsGetValue(PD_Arguments args, uint64_t ID, PD_Matrix mat); -PD_API int PDArgsSetIds(PD_Arguments args, uint64_t ID, PD_IVector ids); +/** + * @brief PDArgsGetIds Get the integer vector of one argument in array, which + * index is `ID`. + * @param args arguments array + * @param ID array index + * @param ids integer vector pointer + * @return PD_Error + */ +PD_API PD_Error PDArgsGetIds(PD_Arguments args, uint64_t ID, PD_IVector ids); -PD_API int PDArgsSetSequenceStartPos(PD_Arguments args, - uint64_t ID, - PD_IVector seqPos); +/** + * @brief PDArgsSetIds Set the integer vector of one argument in array, which + * index is `ID`. + * @param [in] args arguments array + * @param [in] ID array index + * @param [out] ids integer vector pointer + * @return PD_Error + */ +PD_API PD_Error PDArgsSetIds(PD_Arguments args, uint64_t ID, PD_IVector ids); -PD_API int PDArgsGetSequenceStartPos(PD_Arguments args, - uint64_t ID, - PD_IVector seqPos); +/** + * @brief PDArgsSetSequenceStartPos Set sequence start position vector of one + * argument in array, which index is `ID`. + * @param args arguments array + * @param ID array index + * @param seqPos sequence position array. + * @return PD_Error + */ +PD_API PD_Error PDArgsSetSequenceStartPos(PD_Arguments args, + uint64_t ID, + PD_IVector seqPos); +/** + * @brief PDArgsGetSequenceStartPos Get sequence start position vector of one + * argument in array, which index is `ID`. + * @param [in] args arguments array + * @param [in] ID array index + * @param [out] seqPos sequence position array + * @return PD_Error + */ +PD_API PD_Error PDArgsGetSequenceStartPos(PD_Arguments args, + uint64_t ID, + PD_IVector seqPos); -PD_API int PDArgsSetSubSequenceStartPos(PD_Arguments args, - uint64_t ID, - PD_IVector subSeqPos); +/** + * @brief PDArgsSetSubSequenceStartPos Set sub-sequence start position vector of + * one argument in array, which index is `ID`. + * @param args arguments array + * @param ID array index + * @param subSeqPos sub-sequence start position array. + * @return PD_Error + */ +PD_API PD_Error PDArgsSetSubSequenceStartPos(PD_Arguments args, + uint64_t ID, + PD_IVector subSeqPos); -PD_API int PDArgsGetSubSequenceStartPos(PD_Arguments args, - uint64_t ID, - PD_IVector subSeqPos); +/** + * @brief PDArgsGetSubSequenceStartPos Get sub-sequence start position vector of + * one argument in array, which index is `ID`. + * @param args arguments array + * @param ID array index + * @param subSeqPos sub-sequence start position array + * @return PD_Error + */ +PD_API PD_Error PDArgsGetSubSequenceStartPos(PD_Arguments args, + uint64_t ID, + PD_IVector subSeqPos); /** * @brief GradientMachine means a neural network. */ typedef void* PD_GradientMachine; -PD_API int PDGradientMachineCreateForPredict(PD_GradientMachine* machine, - void* modelConfigProtobuf, - int size); +/** + * @brief PDGradientMachineCreateForPredict Create a gradient machine used for + * model inference. + * @param [out] machine that used for model inference. + * @param [in] modelConfigProtobuf + * @param [in] size + * @return PD_Error + */ +PD_API PD_Error PDGradientMachineCreateForPredict(PD_GradientMachine* machine, + void* modelConfigProtobuf, + int size); -PD_API int PDGradientMachineLoadParameterFromDisk(PD_GradientMachine machine, - const char* path); +/** + * @brief PDGradientMachineLoadParameterFromDisk Load parameter from disk. + * @param machine Gradient Machine. + * @param path local directory path. + * @return PD_Error + */ +PD_API PD_Error PDGradientMachineLoadParameterFromDisk( + PD_GradientMachine machine, const char* path); -PD_API int PDGradientMachineForward(PD_GradientMachine machine, - PD_Arguments inArgs, - PD_Arguments outArgs, - bool isTrain); +/** + * @brief PDGradientMachineForward Forward a gradient machine + * @param machine Gradient machine + * @param inArgs input arguments + * @param outArgs output arguments + * @param isTrain is train or not + * @return PD_Error + */ +PD_API PD_Error PDGradientMachineForward(PD_GradientMachine machine, + PD_Arguments inArgs, + PD_Arguments outArgs, + bool isTrain); -PD_API int PDGradientMachineCreateSharedParam(PD_GradientMachine origin, - void* modelConfigProtobuf, - int size, - PD_GradientMachine* slave); +/** + * @brief PDGradientMachineCreateSharedParam Create a gradient machine, which + * parameters are shared from another gradient machine. + * @param [in] origin gradient machine + * @param [in] modelConfigProtobuf model config protobuf + * @param [in] size of model config buffer. + * @param [out] slave gradient machine, the output value. + * @return PD_Error + */ +PD_API PD_Error PDGradientMachineCreateSharedParam(PD_GradientMachine origin, + void* modelConfigProtobuf, + int size, + PD_GradientMachine* slave); -PD_API int PDGradientMachineDestroy(PD_GradientMachine machine); +/** + * @brief PDGradientMachineDestroy Destroy a gradient machine + * @param machine that need to destroy + * @return PD_Error + */ +PD_API PD_Error PDGradientMachineDestroy(PD_GradientMachine machine); /** * Initialize Paddle. */ -PD_API int PDInit(int argc, char** argv); +PD_API PD_Error PDInit(int argc, char** argv); #ifdef __cplusplus } diff --git a/paddle/capi/Vector.cpp b/paddle/capi/Vector.cpp index af2192551370f4d6ec3e0affc2cf8fa5b3ac2021..514d65fec8f6aff72613a4dc391d57cef7fb9c52 100644 --- a/paddle/capi/Vector.cpp +++ b/paddle/capi/Vector.cpp @@ -19,33 +19,26 @@ using paddle::capi::cast; extern "C" { -int PDIVecCreateNone(PD_IVector* ivec) { - if (ivec == nullptr) return kPD_NULLPTR; - auto ptr = new paddle::capi::CIVector(); - *ivec = ptr; - return kPD_NO_ERROR; -} +PD_IVector PDIVecCreateNone() { return new paddle::capi::CIVector(); } -int PDIVectorCreate(PD_IVector* ivec, int* array, uint64_t size, bool copy) { - if (ivec == nullptr) return kPD_NULLPTR; +PD_IVector PDIVectorCreate(int* array, uint64_t size, bool copy, bool useGPU) { auto ptr = new paddle::capi::CIVector(); if (copy) { - ptr->vec = paddle::IVector::create(size, false); + ptr->vec = paddle::IVector::create(size, useGPU); ptr->vec->copyFrom(array, size); } else { - ptr->vec = paddle::IVector::create(array, size, false); + ptr->vec = paddle::IVector::create(array, size, useGPU); } - *ivec = ptr; - return kPD_NO_ERROR; + return ptr; } -int PDIVecDestroy(PD_IVector ivec) { +PD_Error PDIVecDestroy(PD_IVector ivec) { if (ivec == nullptr) return kPD_NULLPTR; delete cast(ivec); return kPD_NO_ERROR; } -int PDIVectorGet(PD_IVector ivec, int** buffer) { +PD_Error PDIVectorGet(PD_IVector ivec, int** buffer) { if (ivec == nullptr || buffer == nullptr) return kPD_NULLPTR; auto v = cast(ivec); if (v->vec == nullptr) return kPD_NULLPTR; @@ -53,7 +46,7 @@ int PDIVectorGet(PD_IVector ivec, int** buffer) { return kPD_NO_ERROR; } -int PDIVectorResize(PD_IVector ivec, uint64_t size) { +PD_Error PDIVectorResize(PD_IVector ivec, uint64_t size) { if (ivec == nullptr) return kPD_NULLPTR; auto v = cast(ivec); if (v->vec == nullptr) return kPD_NULLPTR; @@ -61,7 +54,7 @@ int PDIVectorResize(PD_IVector ivec, uint64_t size) { return kPD_NO_ERROR; } -int PDIVectorGetSize(PD_IVector ivec, uint64_t* size) { +PD_Error PDIVectorGetSize(PD_IVector ivec, uint64_t* size) { if (ivec == nullptr) return kPD_NULLPTR; auto v = cast(ivec); if (v->vec == nullptr) return kPD_NULLPTR; diff --git a/paddle/capi/tests/test_Arguments.cpp b/paddle/capi/tests/test_Arguments.cpp index 9357f3a58468e9d7ffc38fc595895c5e5568a09a..e015b94e12cd9f8737efd6f810f306e3199718ea 100644 --- a/paddle/capi/tests/test_Arguments.cpp +++ b/paddle/capi/tests/test_Arguments.cpp @@ -28,8 +28,7 @@ static std::vector randomBuffer(size_t bufSize) { } TEST(CAPIArguments, create) { - PD_Arguments args; - ASSERT_EQ(kPD_NO_ERROR, PDArgsCreateNone(&args)); + PD_Arguments args = PDArgsCreateNone(); uint64_t size; ASSERT_EQ(kPD_NO_ERROR, PDArgsGetSize(args, &size)); ASSERT_EQ(0UL, size); @@ -37,20 +36,17 @@ TEST(CAPIArguments, create) { } TEST(CAPIArguments, value) { - PD_Arguments args; - ASSERT_EQ(kPD_NO_ERROR, PDArgsCreateNone(&args)); + PD_Arguments args = PDArgsCreateNone(); ASSERT_EQ(kPD_NO_ERROR, PDArgsResize(args, 1)); - PD_Matrix mat; - ASSERT_EQ(kPD_NO_ERROR, PDMatCreate(&mat, 128, 64, false)); + PD_Matrix mat = PDMatCreate(128, 64, false); for (size_t i = 0; i < 128; ++i) { std::vector sampleBuf = randomBuffer(64); PDMatCopyToRow(mat, i, sampleBuf.data()); } ASSERT_EQ(kPD_NO_ERROR, PDArgsSetValue(args, 0, mat)); - PD_Matrix val; - ASSERT_EQ(kPD_NO_ERROR, PDMatCreateNone(&val)); + PD_Matrix val = PDMatCreateNone(); ASSERT_EQ(kPD_NO_ERROR, PDArgsGetValue(args, 0, val)); @@ -63,8 +59,7 @@ TEST(CAPIArguments, value) { ASSERT_EQ(row1, row2); } - PD_IVector ivec; - ASSERT_EQ(kPD_NO_ERROR, PDIVecCreateNone(&ivec)); + PD_IVector ivec = PDIVecCreateNone(); ASSERT_EQ(kPD_NO_ERROR, PDIVecDestroy(ivec)); ASSERT_EQ(kPD_NO_ERROR, PDMatDestroy(val)); ASSERT_EQ(kPD_NO_ERROR, PDMatDestroy(mat)); @@ -72,17 +67,15 @@ TEST(CAPIArguments, value) { } TEST(CAPIArguments, ids) { - PD_Arguments args; - ASSERT_EQ(kPD_NO_ERROR, PDArgsCreateNone(&args)); + PD_Arguments args = PDArgsCreateNone(); 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)); + ivec = PDIVectorCreate(array, 3, true, false); ASSERT_EQ(kPD_NO_ERROR, PDArgsSetIds(args, 0, ivec)); - PD_IVector val; - ASSERT_EQ(kPD_NO_ERROR, PDIVecCreateNone(&val)); + PD_IVector val = PDIVecCreateNone(); ASSERT_EQ(kPD_NO_ERROR, PDArgsGetIds(args, 0, val)); ASSERT_EQ(kPD_NO_ERROR, PDIVecDestroy(ivec)); ASSERT_EQ(kPD_NO_ERROR, PDIVecDestroy(val)); @@ -91,17 +84,15 @@ TEST(CAPIArguments, ids) { template void testSequenceHelper(T1 setter, T2 getter) { - PD_Arguments args; - ASSERT_EQ(kPD_NO_ERROR, PDArgsCreateNone(&args)); + PD_Arguments args = PDArgsCreateNone(); 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)); + ivec = PDIVectorCreate(array, 3, true, false); ASSERT_EQ(kPD_NO_ERROR, setter(args, 0, ivec)); - PD_IVector val; - ASSERT_EQ(kPD_NO_ERROR, PDIVecCreateNone(&val)); + PD_IVector val = PDIVecCreateNone(); ASSERT_EQ(kPD_NO_ERROR, getter(args, 0, val)); uint64_t size; ASSERT_EQ(kPD_NO_ERROR, PDIVectorGetSize(val, &size)); diff --git a/paddle/capi/tests/test_GradientMachine.cpp b/paddle/capi/tests/test_GradientMachine.cpp index 427b37b085a0ae31066cbe79db9913e03d38f981..acee99bcc4b23e564d0f4ae6b89a6b5e9aed8a8b 100644 --- a/paddle/capi/tests/test_GradientMachine.cpp +++ b/paddle/capi/tests/test_GradientMachine.cpp @@ -55,14 +55,11 @@ TEST(GradientMachine, testPredict) { PDGradientMachineCreateSharedParam( machine, &buffer[0], (int)buffer.size(), &machineSlave)); std::swap(machineSlave, machine); - PD_Arguments outArgs; - ASSERT_EQ(kPD_NO_ERROR, PDArgsCreateNone(&outArgs)); + PD_Arguments outArgs = PDArgsCreateNone(); - PD_Arguments inArgs; - ASSERT_EQ(kPD_NO_ERROR, PDArgsCreateNone(&inArgs)); + PD_Arguments inArgs = PDArgsCreateNone(); ASSERT_EQ(kPD_NO_ERROR, PDArgsResize(inArgs, 1)); - PD_Matrix mat; - ASSERT_EQ(kPD_NO_ERROR, PDMatCreate(&mat, 1, 100, false)); + PD_Matrix mat = PDMatCreate(1, 100, false); static_assert(std::is_same::value, ""); auto data = randomBuffer(100); diff --git a/paddle/capi/tests/test_Matrix.cpp b/paddle/capi/tests/test_Matrix.cpp index 4192dd6bfb533875a6b946b5170ec2fbf1fbb7e4..1d38162add21a0d0ee1f3a6f5770faddb8da1e66 100644 --- a/paddle/capi/tests/test_Matrix.cpp +++ b/paddle/capi/tests/test_Matrix.cpp @@ -16,8 +16,7 @@ limitations under the License. */ #include "gtest/gtest.h" TEST(CAPIMatrix, create) { - PD_Matrix mat; - ASSERT_EQ(kPD_NO_ERROR, PDMatCreate(&mat, 128, 32, false)); + PD_Matrix mat = PDMatCreate(128, 32, false); std::vector sampleRow; sampleRow.resize(32); for (size_t i = 0; i < sampleRow.size(); ++i) { @@ -41,7 +40,6 @@ TEST(CAPIMatrix, create) { } TEST(CAPIMatrix, createNone) { - PD_Matrix mat; - ASSERT_EQ(kPD_NO_ERROR, PDMatCreateNone(&mat)); + PD_Matrix mat = PDMatCreateNone(); ASSERT_EQ(kPD_NO_ERROR, PDMatDestroy(mat)); } diff --git a/paddle/capi/tests/test_Vector.cpp b/paddle/capi/tests/test_Vector.cpp index 547d0ef20d16865b376ee80045c14a3002ee70d3..b3558fe0fdd171702f06ae3336db21ea3887cc11 100644 --- a/paddle/capi/tests/test_Vector.cpp +++ b/paddle/capi/tests/test_Vector.cpp @@ -18,8 +18,7 @@ limitations under the License. */ TEST(CAPIVector, create) { PD_IVector 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)); + vec = PDIVectorCreate(array, 3, true, false); ASSERT_EQ(kPD_NO_ERROR, PDIVectorResize(vec, 1000)); uint64_t size; ASSERT_EQ(kPD_NO_ERROR, PDIVectorGetSize(vec, &size)); @@ -27,7 +26,6 @@ TEST(CAPIVector, create) { } TEST(CAPIVector, createNone) { - PD_IVector vec; - ASSERT_EQ(kPD_NO_ERROR, PDIVecCreateNone(&vec)); + PD_IVector vec = PDIVecCreateNone(); ASSERT_EQ(kPD_NO_ERROR, PDIVecDestroy(vec)); }