提交 b5288289 编写于 作者: Y Yu Yang

Rename some API to C-Style

上级 08113b20
...@@ -23,25 +23,25 @@ using paddle::capi::cast; ...@@ -23,25 +23,25 @@ using paddle::capi::cast;
extern "C" { extern "C" {
PD_Arguments PDArgsCreateNone() { return new paddle::capi::CArguments(); } PD_Arguments PDArgsCreateNone() { return new paddle::capi::CArguments(); }
PD_Error PDArgsDestroy(PD_Arguments args) { paddle_error PDArgsDestroy(PD_Arguments args) {
if (args == nullptr) return kPD_NULLPTR; if (args == nullptr) return kPD_NULLPTR;
delete castArg(args); delete castArg(args);
return kPD_NO_ERROR; return kPD_NO_ERROR;
} }
PD_Error PDArgsGetSize(PD_Arguments args, uint64_t* size) { paddle_error PDArgsGetSize(PD_Arguments args, uint64_t* size) {
if (args == nullptr || size == nullptr) return kPD_NULLPTR; if (args == nullptr || size == nullptr) return kPD_NULLPTR;
*size = castArg(args)->args.size(); *size = castArg(args)->args.size();
return kPD_NO_ERROR; return kPD_NO_ERROR;
} }
PD_Error PDArgsResize(PD_Arguments args, uint64_t size) { paddle_error PDArgsResize(PD_Arguments args, uint64_t size) {
if (args == nullptr) return kPD_NULLPTR; if (args == nullptr) return kPD_NULLPTR;
castArg(args)->args.resize(size); castArg(args)->args.resize(size);
return kPD_NO_ERROR; return kPD_NO_ERROR;
} }
PD_Error PDArgsSetValue(PD_Arguments args, uint64_t ID, PD_Matrix mat) { paddle_error PDArgsSetValue(PD_Arguments args, uint64_t ID, paddle_matrix mat) {
if (args == nullptr || mat == nullptr) return kPD_NULLPTR; if (args == nullptr || mat == nullptr) return kPD_NULLPTR;
auto m = paddle::capi::cast<paddle::capi::CMatrix>(mat); auto m = paddle::capi::cast<paddle::capi::CMatrix>(mat);
if (m->mat == nullptr) return kPD_NULLPTR; if (m->mat == nullptr) return kPD_NULLPTR;
...@@ -51,7 +51,7 @@ PD_Error PDArgsSetValue(PD_Arguments args, uint64_t ID, PD_Matrix mat) { ...@@ -51,7 +51,7 @@ PD_Error PDArgsSetValue(PD_Arguments args, uint64_t ID, PD_Matrix mat) {
return kPD_NO_ERROR; return kPD_NO_ERROR;
} }
PD_Error PDArgsGetValue(PD_Arguments args, uint64_t ID, PD_Matrix mat) { paddle_error PDArgsGetValue(PD_Arguments args, uint64_t ID, paddle_matrix mat) {
if (args == nullptr || mat == nullptr) return kPD_NULLPTR; if (args == nullptr || mat == nullptr) return kPD_NULLPTR;
auto m = paddle::capi::cast<paddle::capi::CMatrix>(mat); auto m = paddle::capi::cast<paddle::capi::CMatrix>(mat);
auto a = castArg(args); auto a = castArg(args);
...@@ -60,7 +60,7 @@ PD_Error PDArgsGetValue(PD_Arguments args, uint64_t ID, PD_Matrix mat) { ...@@ -60,7 +60,7 @@ PD_Error PDArgsGetValue(PD_Arguments args, uint64_t ID, PD_Matrix mat) {
return kPD_NO_ERROR; return kPD_NO_ERROR;
} }
PD_Error PDArgsGetIds(PD_Arguments args, uint64_t ID, PD_IVector ids) { paddle_error PDArgsGetIds(PD_Arguments args, uint64_t ID, paddle_ivector ids) {
if (args == nullptr || ids == nullptr) return kPD_NULLPTR; if (args == nullptr || ids == nullptr) return kPD_NULLPTR;
auto iv = castIVec(ids); auto iv = castIVec(ids);
auto a = castArg(args); auto a = castArg(args);
...@@ -69,7 +69,7 @@ PD_Error PDArgsGetIds(PD_Arguments args, uint64_t ID, PD_IVector ids) { ...@@ -69,7 +69,7 @@ PD_Error PDArgsGetIds(PD_Arguments args, uint64_t ID, PD_IVector ids) {
return kPD_NO_ERROR; return kPD_NO_ERROR;
} }
PD_Error PDArgsSetIds(PD_Arguments args, uint64_t ID, PD_IVector ids) { paddle_error PDArgsSetIds(PD_Arguments args, uint64_t ID, paddle_ivector ids) {
//! TODO(lizhao): Complete this method. //! TODO(lizhao): Complete this method.
if (args == nullptr || ids == nullptr) return kPD_NULLPTR; if (args == nullptr || ids == nullptr) return kPD_NULLPTR;
auto iv = paddle::capi::cast<paddle::capi::CIVector>(ids); auto iv = paddle::capi::cast<paddle::capi::CIVector>(ids);
...@@ -80,9 +80,9 @@ PD_Error PDArgsSetIds(PD_Arguments args, uint64_t ID, PD_IVector ids) { ...@@ -80,9 +80,9 @@ PD_Error PDArgsSetIds(PD_Arguments args, uint64_t ID, PD_IVector ids) {
return kPD_NO_ERROR; return kPD_NO_ERROR;
} }
PD_Error PDArgsSetSequenceStartPos(PD_Arguments args, paddle_error PDArgsSetSequenceStartPos(PD_Arguments args,
uint64_t ID, uint64_t ID,
PD_IVector seqPos) { paddle_ivector seqPos) {
if (args == nullptr || seqPos == nullptr) return kPD_NULLPTR; if (args == nullptr || seqPos == nullptr) return kPD_NULLPTR;
auto iv = paddle::capi::cast<paddle::capi::CIVector>(seqPos); auto iv = paddle::capi::cast<paddle::capi::CIVector>(seqPos);
if (iv->vec == nullptr) return kPD_NULLPTR; if (iv->vec == nullptr) return kPD_NULLPTR;
...@@ -93,9 +93,9 @@ PD_Error PDArgsSetSequenceStartPos(PD_Arguments args, ...@@ -93,9 +93,9 @@ PD_Error PDArgsSetSequenceStartPos(PD_Arguments args,
return kPD_NO_ERROR; return kPD_NO_ERROR;
} }
PD_Error PDArgsSetSubSequenceStartPos(PD_Arguments args, paddle_error PDArgsSetSubSequenceStartPos(PD_Arguments args,
uint64_t ID, uint64_t ID,
PD_IVector subSeqPos) { paddle_ivector subSeqPos) {
if (args == nullptr || subSeqPos == nullptr) return kPD_NULLPTR; if (args == nullptr || subSeqPos == nullptr) return kPD_NULLPTR;
auto iv = paddle::capi::cast<paddle::capi::CIVector>(subSeqPos); auto iv = paddle::capi::cast<paddle::capi::CIVector>(subSeqPos);
if (iv->vec == nullptr) return kPD_NULLPTR; if (iv->vec == nullptr) return kPD_NULLPTR;
...@@ -106,9 +106,9 @@ PD_Error PDArgsSetSubSequenceStartPos(PD_Arguments args, ...@@ -106,9 +106,9 @@ PD_Error PDArgsSetSubSequenceStartPos(PD_Arguments args,
return kPD_NO_ERROR; return kPD_NO_ERROR;
} }
PD_Error PDArgsGetSequenceStartPos(PD_Arguments args, paddle_error PDArgsGetSequenceStartPos(PD_Arguments args,
uint64_t ID, uint64_t ID,
PD_IVector seqPos) { paddle_ivector seqPos) {
if (args == nullptr || seqPos == nullptr) return kPD_NULLPTR; if (args == nullptr || seqPos == nullptr) return kPD_NULLPTR;
auto iv = castIVec(seqPos); auto iv = castIVec(seqPos);
auto a = castArg(args); auto a = castArg(args);
...@@ -118,9 +118,9 @@ PD_Error PDArgsGetSequenceStartPos(PD_Arguments args, ...@@ -118,9 +118,9 @@ PD_Error PDArgsGetSequenceStartPos(PD_Arguments args,
return kPD_NO_ERROR; return kPD_NO_ERROR;
} }
PD_Error PDArgsGetSubSequenceStartPos(PD_Arguments args, paddle_error PDArgsGetSubSequenceStartPos(PD_Arguments args,
uint64_t ID, uint64_t ID,
PD_IVector subSeqPos) { paddle_ivector subSeqPos) {
if (args == nullptr || subSeqPos == nullptr) return kPD_NULLPTR; if (args == nullptr || subSeqPos == nullptr) return kPD_NULLPTR;
auto iv = castIVec(subSeqPos); auto iv = castIVec(subSeqPos);
auto a = castArg(args); auto a = castArg(args);
......
...@@ -12,14 +12,14 @@ configure_file(config.h.in config.h @ONLY) ...@@ -12,14 +12,14 @@ configure_file(config.h.in config.h @ONLY)
# PaddleCAPI.h is the only header we exposed. It currently only used for model # PaddleCAPI.h is the only header we exposed. It currently only used for model
# inference. # inference.
set(CAPI_HEADER PaddleCAPI.h) file(GLOB CAPI_HEADERS *.h)
set(CAPI_PRIVATE_HEADER PaddleCAPIPrivate.h) set(CAPI_PRIVATE_HEADER PaddleCAPIPrivate.h)
list(REMOVE_ITEM CAPI_HEADERS ${CAPI_PRIVATE_HEADER})
file(GLOB CAPI_SOURCES *.cpp) file(GLOB CAPI_SOURCES *.cpp)
# building paddle_capi # building paddle_capi
add_library(paddle_capi STATIC ${CAPI_SOURCES}) add_library(paddle_capi STATIC ${CAPI_HEADERS} ${CAPI_PRIVATE_HEADER}
${CAPI_SOURCES})
target_include_directories(paddle_capi PUBLIC ${CMAKE_CURRENT_BINARY_DIR}) target_include_directories(paddle_capi PUBLIC ${CMAKE_CURRENT_BINARY_DIR})
......
...@@ -38,7 +38,7 @@ NeuralNetwork* newCustomNerualNetwork(const std::string& name, ...@@ -38,7 +38,7 @@ NeuralNetwork* newCustomNerualNetwork(const std::string& name,
} // namespace paddle } // namespace paddle
extern "C" { extern "C" {
PD_Error PDGradientMachineCreateForPredict(PD_GradientMachine* machine, paddle_error PDGradientMachineCreateForPredict(PD_GradientMachine* machine,
void* modelConfigProtobuf, void* modelConfigProtobuf,
int size) { int size) {
if (modelConfigProtobuf == nullptr) return kPD_NULLPTR; if (modelConfigProtobuf == nullptr) return kPD_NULLPTR;
...@@ -55,12 +55,12 @@ PD_Error PDGradientMachineCreateForPredict(PD_GradientMachine* machine, ...@@ -55,12 +55,12 @@ PD_Error PDGradientMachineCreateForPredict(PD_GradientMachine* machine,
return kPD_NO_ERROR; return kPD_NO_ERROR;
} }
PD_Error PDGradientMachineDestroy(PD_GradientMachine machine) { paddle_error PDGradientMachineDestroy(PD_GradientMachine machine) {
delete cast(machine); delete cast(machine);
return kPD_NO_ERROR; return kPD_NO_ERROR;
} }
PD_Error PDGradientMachineLoadParameterFromDisk(PD_GradientMachine machine, paddle_error PDGradientMachineLoadParameterFromDisk(PD_GradientMachine machine,
const char* path) { const char* path) {
auto m = cast(machine); auto m = cast(machine);
if (m == nullptr || path == nullptr || m->machine == nullptr) if (m == nullptr || path == nullptr || m->machine == nullptr)
...@@ -69,7 +69,7 @@ PD_Error PDGradientMachineLoadParameterFromDisk(PD_GradientMachine machine, ...@@ -69,7 +69,7 @@ PD_Error PDGradientMachineLoadParameterFromDisk(PD_GradientMachine machine,
return kPD_NO_ERROR; return kPD_NO_ERROR;
} }
PD_Error PDGradientMachineForward(PD_GradientMachine machine, paddle_error PDGradientMachineForward(PD_GradientMachine machine,
PD_Arguments inArgs, PD_Arguments inArgs,
PD_Arguments outArgs, PD_Arguments outArgs,
bool isTrain) { bool isTrain) {
...@@ -83,7 +83,7 @@ PD_Error PDGradientMachineForward(PD_GradientMachine machine, ...@@ -83,7 +83,7 @@ PD_Error PDGradientMachineForward(PD_GradientMachine machine,
return kPD_NO_ERROR; return kPD_NO_ERROR;
} }
PD_Error PDGradientMachineCreateSharedParam(PD_GradientMachine origin, paddle_error PDGradientMachineCreateSharedParam(PD_GradientMachine origin,
void* modelConfigProtobuf, void* modelConfigProtobuf,
int size, int size,
PD_GradientMachine* slave) { PD_GradientMachine* slave) {
......
...@@ -29,7 +29,7 @@ static void initPaddle(int argc, char** argv) { ...@@ -29,7 +29,7 @@ static void initPaddle(int argc, char** argv) {
} }
extern "C" { extern "C" {
PD_Error PDInit(int argc, char** argv) { paddle_error PDInit(int argc, char** argv) {
std::vector<char*> realArgv; std::vector<char*> realArgv;
realArgv.reserve(argc + 1); realArgv.reserve(argc + 1);
realArgv.push_back(strdup("")); realArgv.push_back(strdup(""));
......
...@@ -18,22 +18,28 @@ limitations under the License. */ ...@@ -18,22 +18,28 @@ limitations under the License. */
#define cast(v) paddle::capi::cast<paddle::capi::CMatrix>(v) #define cast(v) paddle::capi::cast<paddle::capi::CMatrix>(v)
extern "C" { extern "C" {
PD_Matrix PDMatCreate(uint64_t height, uint64_t width, bool useGpu) { paddle_matrix paddle_matrix_create(uint64_t height,
uint64_t width,
bool useGpu) {
auto ptr = new paddle::capi::CMatrix(); auto ptr = new paddle::capi::CMatrix();
ptr->mat = paddle::Matrix::create(height, width, false, useGpu); ptr->mat = paddle::Matrix::create(height, width, false, useGpu);
return ptr; return ptr;
} }
PD_Matrix PDMatCreateNone() { return new paddle::capi::CMatrix(); } paddle_matrix paddle_matrix_create_none() {
return new paddle::capi::CMatrix();
}
PD_Error PDMatDestroy(PD_Matrix mat) { paddle_error paddle_matrix_destroy(paddle_matrix mat) {
if (mat == nullptr) return kPD_NULLPTR; if (mat == nullptr) return kPD_NULLPTR;
auto ptr = cast(mat); auto ptr = cast(mat);
delete ptr; delete ptr;
return kPD_NO_ERROR; return kPD_NO_ERROR;
} }
PD_Error PDMatCopyToRow(PD_Matrix mat, uint64_t rowID, pd_real* rowArray) { paddle_error paddle_matrix_set_row(paddle_matrix mat,
uint64_t rowID,
pd_real* rowArray) {
if (mat == nullptr) return kPD_NULLPTR; if (mat == nullptr) return kPD_NULLPTR;
auto ptr = cast(mat); auto ptr = cast(mat);
if (ptr->mat == nullptr) return kPD_NULLPTR; if (ptr->mat == nullptr) return kPD_NULLPTR;
...@@ -48,7 +54,9 @@ PD_Error PDMatCopyToRow(PD_Matrix mat, uint64_t rowID, pd_real* rowArray) { ...@@ -48,7 +54,9 @@ PD_Error PDMatCopyToRow(PD_Matrix mat, uint64_t rowID, pd_real* rowArray) {
return kPD_NO_ERROR; return kPD_NO_ERROR;
} }
PD_Error PDMatGetRow(PD_Matrix mat, uint64_t rowID, pd_real** rawRowBuffer) { paddle_error paddle_matrix_get_row(paddle_matrix mat,
uint64_t rowID,
pd_real** rawRowBuffer) {
if (mat == nullptr) return kPD_NULLPTR; if (mat == nullptr) return kPD_NULLPTR;
auto ptr = cast(mat); auto ptr = cast(mat);
if (ptr->mat == nullptr) return kPD_NULLPTR; if (ptr->mat == nullptr) return kPD_NULLPTR;
...@@ -57,7 +65,9 @@ PD_Error PDMatGetRow(PD_Matrix mat, uint64_t rowID, pd_real** rawRowBuffer) { ...@@ -57,7 +65,9 @@ PD_Error PDMatGetRow(PD_Matrix mat, uint64_t rowID, pd_real** rawRowBuffer) {
return kPD_NO_ERROR; return kPD_NO_ERROR;
} }
PD_Error PDMatGetShape(PD_Matrix mat, uint64_t* height, uint64_t* width) { paddle_error paddle_matrix_get_shape(paddle_matrix mat,
uint64_t* height,
uint64_t* width) {
if (mat == nullptr) return kPD_NULLPTR; if (mat == nullptr) return kPD_NULLPTR;
if (height != nullptr) { if (height != nullptr) {
*height = cast(mat)->mat->getHeight(); *height = cast(mat)->mat->getHeight();
......
...@@ -17,10 +17,9 @@ limitations under the License. */ ...@@ -17,10 +17,9 @@ limitations under the License. */
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#include "config.h" #include "config.h"
#include "error.h"
// Since we only support linux and macos in compile, always use clang or #include "matrix.h"
// gcc 4.8+. DLL_IMPORT/DLL_EXPORT is as simple as below. #include "vector.h"
#define PD_API __attribute__((visibility("default")))
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
...@@ -33,132 +32,6 @@ extern "C" { ...@@ -33,132 +32,6 @@ extern "C" {
* NOTE: This is an experimental API, it could be changed. * NOTE: This is an experimental API, it could be changed.
*/ */
/**
* Error Type for Paddle API.
*/
typedef enum {
kPD_NO_ERROR = 0,
kPD_NULLPTR = 1,
kPD_OUT_OF_RANGE = 2,
kPD_PROTOBUF_ERROR = 3,
kPD_UNDEFINED_ERROR = -1,
} PD_Error;
/**
* Int Vector Functions. Return will be a PD_Error type.
*/
typedef void* PD_IVector;
/**
* @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 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 PD_IVector PDIVectorCreate(int* array,
uint64_t size,
bool copy,
bool useGPU);
/**
* @brief PDIVecDestroy destory an int vector.
* @param ivec vector to be destoried.
* @return PD_Error
*/
PD_API PD_Error PDIVecDestroy(PD_IVector ivec);
/**
* @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);
/**
* @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);
/**
* @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;
/**
* @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);
/**
* @brief PDMatDestroy Destroy a matrix.
* @param mat
* @return PD_Error
*/
PD_API PD_Error PDMatDestroy(PD_Matrix mat);
/**
* @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);
/**
* @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);
/**
* @brief PDMatCreateNone Create None Matrix
* @return
*/
PD_API PD_Matrix PDMatCreateNone();
/**
* @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 * Arguments functions. Each argument means layer output. Arguments means a
* array of arguemnt. * array of arguemnt.
...@@ -174,25 +47,25 @@ PD_API PD_Arguments PDArgsCreateNone(); ...@@ -174,25 +47,25 @@ PD_API PD_Arguments PDArgsCreateNone();
/** /**
* @brief PDArgsDestroy Destroy the arguments * @brief PDArgsDestroy Destroy the arguments
* @param args arguments to destroy * @param args arguments to destroy
* @return PD_Error * @return paddle_error
*/ */
PD_API PD_Error PDArgsDestroy(PD_Arguments args); PD_API paddle_error PDArgsDestroy(PD_Arguments args);
/** /**
* @brief PDArgsGetSize Get size of arguments array * @brief PDArgsGetSize Get size of arguments array
* @param [in] args arguments array * @param [in] args arguments array
* @param [out] size array size * @param [out] size array size
* @return PD_Error * @return paddle_error
*/ */
PD_API PD_Error PDArgsGetSize(PD_Arguments args, uint64_t* size); PD_API paddle_error PDArgsGetSize(PD_Arguments args, uint64_t* size);
/** /**
* @brief PDArgsResize Resize a arguments array. * @brief PDArgsResize Resize a arguments array.
* @param args arguments array. * @param args arguments array.
* @param size target size of array * @param size target size of array
* @return PD_Error * @return paddle_error
*/ */
PD_API PD_Error PDArgsResize(PD_Arguments args, uint64_t size); PD_API paddle_error PDArgsResize(PD_Arguments args, uint64_t size);
/** /**
* @brief PDArgsSetValue Set value matrix of one argument in array, which index * @brief PDArgsSetValue Set value matrix of one argument in array, which index
...@@ -200,9 +73,11 @@ PD_API PD_Error PDArgsResize(PD_Arguments args, uint64_t size); ...@@ -200,9 +73,11 @@ PD_API PD_Error PDArgsResize(PD_Arguments args, uint64_t size);
* @param args arguments array * @param args arguments array
* @param ID array index * @param ID array index
* @param mat matrix pointer * @param mat matrix pointer
* @return PD_Error * @return paddle_error
*/ */
PD_API PD_Error PDArgsSetValue(PD_Arguments args, uint64_t ID, PD_Matrix mat); PD_API paddle_error PDArgsSetValue(PD_Arguments args,
uint64_t ID,
paddle_matrix mat);
/** /**
* @brief PDArgsGetValue Get value matrix of one argument in array, which index * @brief PDArgsGetValue Get value matrix of one argument in array, which index
...@@ -210,9 +85,11 @@ PD_API PD_Error PDArgsSetValue(PD_Arguments args, uint64_t ID, PD_Matrix mat); ...@@ -210,9 +85,11 @@ PD_API PD_Error PDArgsSetValue(PD_Arguments args, uint64_t ID, PD_Matrix mat);
* @param [in] args arguments array * @param [in] args arguments array
* @param [in] ID array index * @param [in] ID array index
* @param [out] mat matrix pointer * @param [out] mat matrix pointer
* @return PD_Error * @return paddle_error
*/ */
PD_API PD_Error PDArgsGetValue(PD_Arguments args, uint64_t ID, PD_Matrix mat); PD_API paddle_error PDArgsGetValue(PD_Arguments args,
uint64_t ID,
paddle_matrix mat);
/** /**
* @brief PDArgsGetIds Get the integer vector of one argument in array, which * @brief PDArgsGetIds Get the integer vector of one argument in array, which
...@@ -220,9 +97,11 @@ PD_API PD_Error PDArgsGetValue(PD_Arguments args, uint64_t ID, PD_Matrix mat); ...@@ -220,9 +97,11 @@ PD_API PD_Error PDArgsGetValue(PD_Arguments args, uint64_t ID, PD_Matrix mat);
* @param args arguments array * @param args arguments array
* @param ID array index * @param ID array index
* @param ids integer vector pointer * @param ids integer vector pointer
* @return PD_Error * @return paddle_error
*/ */
PD_API PD_Error PDArgsGetIds(PD_Arguments args, uint64_t ID, PD_IVector ids); PD_API paddle_error PDArgsGetIds(PD_Arguments args,
uint64_t ID,
paddle_ivector ids);
/** /**
* @brief PDArgsSetIds Set the integer vector of one argument in array, which * @brief PDArgsSetIds Set the integer vector of one argument in array, which
...@@ -230,9 +109,11 @@ PD_API PD_Error PDArgsGetIds(PD_Arguments args, uint64_t ID, PD_IVector ids); ...@@ -230,9 +109,11 @@ PD_API PD_Error PDArgsGetIds(PD_Arguments args, uint64_t ID, PD_IVector ids);
* @param [in] args arguments array * @param [in] args arguments array
* @param [in] ID array index * @param [in] ID array index
* @param [out] ids integer vector pointer * @param [out] ids integer vector pointer
* @return PD_Error * @return paddle_error
*/ */
PD_API PD_Error PDArgsSetIds(PD_Arguments args, uint64_t ID, PD_IVector ids); PD_API paddle_error PDArgsSetIds(PD_Arguments args,
uint64_t ID,
paddle_ivector ids);
/** /**
* @brief PDArgsSetSequenceStartPos Set sequence start position vector of one * @brief PDArgsSetSequenceStartPos Set sequence start position vector of one
...@@ -240,22 +121,22 @@ PD_API PD_Error PDArgsSetIds(PD_Arguments args, uint64_t ID, PD_IVector ids); ...@@ -240,22 +121,22 @@ PD_API PD_Error PDArgsSetIds(PD_Arguments args, uint64_t ID, PD_IVector ids);
* @param args arguments array * @param args arguments array
* @param ID array index * @param ID array index
* @param seqPos sequence position array. * @param seqPos sequence position array.
* @return PD_Error * @return paddle_error
*/ */
PD_API PD_Error PDArgsSetSequenceStartPos(PD_Arguments args, PD_API paddle_error PDArgsSetSequenceStartPos(PD_Arguments args,
uint64_t ID, uint64_t ID,
PD_IVector seqPos); paddle_ivector seqPos);
/** /**
* @brief PDArgsGetSequenceStartPos Get sequence start position vector of one * @brief PDArgsGetSequenceStartPos Get sequence start position vector of one
* argument in array, which index is `ID`. * argument in array, which index is `ID`.
* @param [in] args arguments array * @param [in] args arguments array
* @param [in] ID array index * @param [in] ID array index
* @param [out] seqPos sequence position array * @param [out] seqPos sequence position array
* @return PD_Error * @return paddle_error
*/ */
PD_API PD_Error PDArgsGetSequenceStartPos(PD_Arguments args, PD_API paddle_error PDArgsGetSequenceStartPos(PD_Arguments args,
uint64_t ID, uint64_t ID,
PD_IVector seqPos); paddle_ivector seqPos);
/** /**
* @brief PDArgsSetSubSequenceStartPos Set sub-sequence start position vector of * @brief PDArgsSetSubSequenceStartPos Set sub-sequence start position vector of
...@@ -263,11 +144,11 @@ PD_API PD_Error PDArgsGetSequenceStartPos(PD_Arguments args, ...@@ -263,11 +144,11 @@ PD_API PD_Error PDArgsGetSequenceStartPos(PD_Arguments args,
* @param args arguments array * @param args arguments array
* @param ID array index * @param ID array index
* @param subSeqPos sub-sequence start position array. * @param subSeqPos sub-sequence start position array.
* @return PD_Error * @return paddle_error
*/ */
PD_API PD_Error PDArgsSetSubSequenceStartPos(PD_Arguments args, PD_API paddle_error PDArgsSetSubSequenceStartPos(PD_Arguments args,
uint64_t ID, uint64_t ID,
PD_IVector subSeqPos); paddle_ivector subSeqPos);
/** /**
* @brief PDArgsGetSubSequenceStartPos Get sub-sequence start position vector of * @brief PDArgsGetSubSequenceStartPos Get sub-sequence start position vector of
...@@ -275,11 +156,11 @@ PD_API PD_Error PDArgsSetSubSequenceStartPos(PD_Arguments args, ...@@ -275,11 +156,11 @@ PD_API PD_Error PDArgsSetSubSequenceStartPos(PD_Arguments args,
* @param args arguments array * @param args arguments array
* @param ID array index * @param ID array index
* @param subSeqPos sub-sequence start position array * @param subSeqPos sub-sequence start position array
* @return PD_Error * @return paddle_error
*/ */
PD_API PD_Error PDArgsGetSubSequenceStartPos(PD_Arguments args, PD_API paddle_error PDArgsGetSubSequenceStartPos(PD_Arguments args,
uint64_t ID, uint64_t ID,
PD_IVector subSeqPos); paddle_ivector subSeqPos);
/** /**
* @brief GradientMachine means a neural network. * @brief GradientMachine means a neural network.
*/ */
...@@ -291,19 +172,18 @@ typedef void* PD_GradientMachine; ...@@ -291,19 +172,18 @@ typedef void* PD_GradientMachine;
* @param [out] machine that used for model inference. * @param [out] machine that used for model inference.
* @param [in] modelConfigProtobuf * @param [in] modelConfigProtobuf
* @param [in] size * @param [in] size
* @return PD_Error * @return paddle_error
*/ */
PD_API PD_Error PDGradientMachineCreateForPredict(PD_GradientMachine* machine, PD_API paddle_error PDGradientMachineCreateForPredict(
void* modelConfigProtobuf, PD_GradientMachine* machine, void* modelConfigProtobuf, int size);
int size);
/** /**
* @brief PDGradientMachineLoadParameterFromDisk Load parameter from disk. * @brief PDGradientMachineLoadParameterFromDisk Load parameter from disk.
* @param machine Gradient Machine. * @param machine Gradient Machine.
* @param path local directory path. * @param path local directory path.
* @return PD_Error * @return paddle_error
*/ */
PD_API PD_Error PDGradientMachineLoadParameterFromDisk( PD_API paddle_error PDGradientMachineLoadParameterFromDisk(
PD_GradientMachine machine, const char* path); PD_GradientMachine machine, const char* path);
/** /**
...@@ -312,9 +192,9 @@ PD_API PD_Error PDGradientMachineLoadParameterFromDisk( ...@@ -312,9 +192,9 @@ PD_API PD_Error PDGradientMachineLoadParameterFromDisk(
* @param inArgs input arguments * @param inArgs input arguments
* @param outArgs output arguments * @param outArgs output arguments
* @param isTrain is train or not * @param isTrain is train or not
* @return PD_Error * @return paddle_error
*/ */
PD_API PD_Error PDGradientMachineForward(PD_GradientMachine machine, PD_API paddle_error PDGradientMachineForward(PD_GradientMachine machine,
PD_Arguments inArgs, PD_Arguments inArgs,
PD_Arguments outArgs, PD_Arguments outArgs,
bool isTrain); bool isTrain);
...@@ -326,9 +206,10 @@ PD_API PD_Error PDGradientMachineForward(PD_GradientMachine machine, ...@@ -326,9 +206,10 @@ PD_API PD_Error PDGradientMachineForward(PD_GradientMachine machine,
* @param [in] modelConfigProtobuf model config protobuf * @param [in] modelConfigProtobuf model config protobuf
* @param [in] size of model config buffer. * @param [in] size of model config buffer.
* @param [out] slave gradient machine, the output value. * @param [out] slave gradient machine, the output value.
* @return PD_Error * @return paddle_error
*/ */
PD_API PD_Error PDGradientMachineCreateSharedParam(PD_GradientMachine origin, PD_API paddle_error
PDGradientMachineCreateSharedParam(PD_GradientMachine origin,
void* modelConfigProtobuf, void* modelConfigProtobuf,
int size, int size,
PD_GradientMachine* slave); PD_GradientMachine* slave);
...@@ -336,14 +217,14 @@ PD_API PD_Error PDGradientMachineCreateSharedParam(PD_GradientMachine origin, ...@@ -336,14 +217,14 @@ PD_API PD_Error PDGradientMachineCreateSharedParam(PD_GradientMachine origin,
/** /**
* @brief PDGradientMachineDestroy Destroy a gradient machine * @brief PDGradientMachineDestroy Destroy a gradient machine
* @param machine that need to destroy * @param machine that need to destroy
* @return PD_Error * @return paddle_error
*/ */
PD_API PD_Error PDGradientMachineDestroy(PD_GradientMachine machine); PD_API paddle_error PDGradientMachineDestroy(PD_GradientMachine machine);
/** /**
* Initialize Paddle. * Initialize Paddle.
*/ */
PD_API PD_Error PDInit(int argc, char** argv); PD_API paddle_error PDInit(int argc, char** argv);
#ifdef __cplusplus #ifdef __cplusplus
} }
......
...@@ -19,9 +19,14 @@ using paddle::capi::cast; ...@@ -19,9 +19,14 @@ using paddle::capi::cast;
extern "C" { extern "C" {
PD_IVector PDIVecCreateNone() { return new paddle::capi::CIVector(); } paddle_ivector paddle_ivector_create_none() {
return new paddle::capi::CIVector();
}
PD_IVector PDIVectorCreate(int* array, uint64_t size, bool copy, bool useGPU) { paddle_ivector paddle_ivector_create(int* array,
uint64_t size,
bool copy,
bool useGPU) {
auto ptr = new paddle::capi::CIVector(); auto ptr = new paddle::capi::CIVector();
if (copy) { if (copy) {
ptr->vec = paddle::IVector::create(size, useGPU); ptr->vec = paddle::IVector::create(size, useGPU);
...@@ -32,13 +37,13 @@ PD_IVector PDIVectorCreate(int* array, uint64_t size, bool copy, bool useGPU) { ...@@ -32,13 +37,13 @@ PD_IVector PDIVectorCreate(int* array, uint64_t size, bool copy, bool useGPU) {
return ptr; return ptr;
} }
PD_Error PDIVecDestroy(PD_IVector ivec) { paddle_error paddle_ivector_destroy(paddle_ivector ivec) {
if (ivec == nullptr) return kPD_NULLPTR; if (ivec == nullptr) return kPD_NULLPTR;
delete cast<paddle::capi::CIVector>(ivec); delete cast<paddle::capi::CIVector>(ivec);
return kPD_NO_ERROR; return kPD_NO_ERROR;
} }
PD_Error PDIVectorGet(PD_IVector ivec, int** buffer) { paddle_error paddle_ivector_get(paddle_ivector ivec, int** buffer) {
if (ivec == nullptr || buffer == nullptr) return kPD_NULLPTR; if (ivec == nullptr || buffer == nullptr) return kPD_NULLPTR;
auto v = cast<paddle::capi::CIVector>(ivec); auto v = cast<paddle::capi::CIVector>(ivec);
if (v->vec == nullptr) return kPD_NULLPTR; if (v->vec == nullptr) return kPD_NULLPTR;
...@@ -46,7 +51,7 @@ PD_Error PDIVectorGet(PD_IVector ivec, int** buffer) { ...@@ -46,7 +51,7 @@ PD_Error PDIVectorGet(PD_IVector ivec, int** buffer) {
return kPD_NO_ERROR; return kPD_NO_ERROR;
} }
PD_Error PDIVectorResize(PD_IVector ivec, uint64_t size) { paddle_error paddle_ivector_resize(paddle_ivector ivec, uint64_t size) {
if (ivec == nullptr) return kPD_NULLPTR; if (ivec == nullptr) return kPD_NULLPTR;
auto v = cast<paddle::capi::CIVector>(ivec); auto v = cast<paddle::capi::CIVector>(ivec);
if (v->vec == nullptr) return kPD_NULLPTR; if (v->vec == nullptr) return kPD_NULLPTR;
...@@ -54,7 +59,7 @@ PD_Error PDIVectorResize(PD_IVector ivec, uint64_t size) { ...@@ -54,7 +59,7 @@ PD_Error PDIVectorResize(PD_IVector ivec, uint64_t size) {
return kPD_NO_ERROR; return kPD_NO_ERROR;
} }
PD_Error PDIVectorGetSize(PD_IVector ivec, uint64_t* size) { paddle_error paddle_ivector_get_size(paddle_ivector ivec, uint64_t* size) {
if (ivec == nullptr) return kPD_NULLPTR; if (ivec == nullptr) return kPD_NULLPTR;
auto v = cast<paddle::capi::CIVector>(ivec); auto v = cast<paddle::capi::CIVector>(ivec);
if (v->vec == nullptr) return kPD_NULLPTR; if (v->vec == nullptr) return kPD_NULLPTR;
......
...@@ -3,4 +3,8 @@ ...@@ -3,4 +3,8 @@
typedef @PADDLE_FLOAT_TYPE@ pd_real; typedef @PADDLE_FLOAT_TYPE@ pd_real;
// Since we only support linux and macos in compile, always use clang or
// gcc 4.8+. DLL_IMPORT/DLL_EXPORT is as simple as below.
#define PD_API __attribute__((visibility("default")))
#endif #endif
#ifndef __PADDLE_CAPI_ERROR_H__
#define __PADDLE_CAPI_ERROR_H__
/**
* Error Type for Paddle API.
*/
typedef enum {
kPD_NO_ERROR = 0,
kPD_NULLPTR = 1,
kPD_OUT_OF_RANGE = 2,
kPD_PROTOBUF_ERROR = 3,
kPD_UNDEFINED_ERROR = -1,
} paddle_error;
#endif
/* 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. */
#ifndef __PADDLE_CAPI_MATRIX_H__
#define __PADDLE_CAPI_MATRIX_H__
#include <stdint.h>
#include "config.h"
#include "error.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* Matrix functions. Return will be a paddle_error type.
*/
typedef void* paddle_matrix;
/**
* @brief paddle_matrix_create Create a dense matrix
* @param height matrix height.
* @param width matrix width
* @param useGpu use GPU of not
* @return Matrix handler
*/
PD_API paddle_matrix paddle_matrix_create(uint64_t height,
uint64_t width,
bool useGpu);
/**
* @brief paddle_matrix_destroy Destroy a matrix.
* @param mat
* @return paddle_error
*/
PD_API paddle_error paddle_matrix_destroy(paddle_matrix mat);
/**
* @brief paddle_matrix_set_row Set a row to matrix.
* @param mat Target Matrix
* @param rowID Index of row
* @param rowArray Row data.
* @return paddle_error
*/
PD_API paddle_error paddle_matrix_set_row(paddle_matrix mat,
uint64_t rowID,
pd_real* rowArray);
/**
* @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 paddle_error
*/
PD_API paddle_error paddle_matrix_get_row(paddle_matrix mat,
uint64_t rowID,
pd_real** rawRowBuffer);
/**
* @brief PDMatCreateNone Create None Matrix
* @return
*/
PD_API paddle_matrix paddle_matrix_create_none();
/**
* @brief PDMatGetShape get the shape of matrix
* @param mat target matrix
* @param height The height of matrix
* @param width The width of matrix
* @return paddle_error
*/
PD_API paddle_error paddle_matrix_get_shape(paddle_matrix mat,
uint64_t* height,
uint64_t* width);
#ifdef __cplusplus
}
#endif
#endif
...@@ -39,14 +39,14 @@ TEST(CAPIArguments, value) { ...@@ -39,14 +39,14 @@ TEST(CAPIArguments, value) {
PD_Arguments args = PDArgsCreateNone(); PD_Arguments args = PDArgsCreateNone();
ASSERT_EQ(kPD_NO_ERROR, PDArgsResize(args, 1)); ASSERT_EQ(kPD_NO_ERROR, PDArgsResize(args, 1));
PD_Matrix mat = PDMatCreate(128, 64, false); paddle_matrix mat = paddle_matrix_create(128, 64, false);
for (size_t i = 0; i < 128; ++i) { for (size_t i = 0; i < 128; ++i) {
std::vector<pd_real> sampleBuf = randomBuffer(64); std::vector<pd_real> sampleBuf = randomBuffer(64);
PDMatCopyToRow(mat, i, sampleBuf.data()); paddle_matrix_set_row(mat, i, sampleBuf.data());
} }
ASSERT_EQ(kPD_NO_ERROR, PDArgsSetValue(args, 0, mat)); ASSERT_EQ(kPD_NO_ERROR, PDArgsSetValue(args, 0, mat));
PD_Matrix val = PDMatCreateNone(); paddle_matrix val = paddle_matrix_create_none();
ASSERT_EQ(kPD_NO_ERROR, PDArgsGetValue(args, 0, val)); ASSERT_EQ(kPD_NO_ERROR, PDArgsGetValue(args, 0, val));
...@@ -54,15 +54,15 @@ TEST(CAPIArguments, value) { ...@@ -54,15 +54,15 @@ TEST(CAPIArguments, value) {
pd_real* row1; pd_real* row1;
pd_real* row2; pd_real* row2;
ASSERT_EQ(kPD_NO_ERROR, PDMatGetRow(mat, i, &row1)); ASSERT_EQ(kPD_NO_ERROR, paddle_matrix_get_row(mat, i, &row1));
ASSERT_EQ(kPD_NO_ERROR, PDMatGetRow(val, i, &row2)); ASSERT_EQ(kPD_NO_ERROR, paddle_matrix_get_row(val, i, &row2));
ASSERT_EQ(row1, row2); ASSERT_EQ(row1, row2);
} }
PD_IVector ivec = PDIVecCreateNone(); paddle_ivector ivec = paddle_ivector_create_none();
ASSERT_EQ(kPD_NO_ERROR, PDIVecDestroy(ivec)); ASSERT_EQ(kPD_NO_ERROR, paddle_ivector_destroy(ivec));
ASSERT_EQ(kPD_NO_ERROR, PDMatDestroy(val)); ASSERT_EQ(kPD_NO_ERROR, paddle_matrix_destroy(val));
ASSERT_EQ(kPD_NO_ERROR, PDMatDestroy(mat)); ASSERT_EQ(kPD_NO_ERROR, paddle_matrix_destroy(mat));
ASSERT_EQ(kPD_NO_ERROR, PDArgsDestroy(args)); ASSERT_EQ(kPD_NO_ERROR, PDArgsDestroy(args));
} }
...@@ -70,15 +70,15 @@ TEST(CAPIArguments, ids) { ...@@ -70,15 +70,15 @@ TEST(CAPIArguments, ids) {
PD_Arguments args = PDArgsCreateNone(); PD_Arguments args = PDArgsCreateNone();
ASSERT_EQ(kPD_NO_ERROR, PDArgsResize(args, 1)); ASSERT_EQ(kPD_NO_ERROR, PDArgsResize(args, 1));
PD_IVector ivec; paddle_ivector ivec;
int array[3] = {1, 2, 3}; int array[3] = {1, 2, 3};
ivec = PDIVectorCreate(array, 3, true, false); ivec = paddle_ivector_create(array, 3, true, false);
ASSERT_EQ(kPD_NO_ERROR, PDArgsSetIds(args, 0, ivec)); ASSERT_EQ(kPD_NO_ERROR, PDArgsSetIds(args, 0, ivec));
PD_IVector val = PDIVecCreateNone(); paddle_ivector val = paddle_ivector_create_none();
ASSERT_EQ(kPD_NO_ERROR, PDArgsGetIds(args, 0, val)); ASSERT_EQ(kPD_NO_ERROR, PDArgsGetIds(args, 0, val));
ASSERT_EQ(kPD_NO_ERROR, PDIVecDestroy(ivec)); ASSERT_EQ(kPD_NO_ERROR, paddle_ivector_destroy(ivec));
ASSERT_EQ(kPD_NO_ERROR, PDIVecDestroy(val)); ASSERT_EQ(kPD_NO_ERROR, paddle_ivector_destroy(val));
ASSERT_EQ(kPD_NO_ERROR, PDArgsDestroy(args)); ASSERT_EQ(kPD_NO_ERROR, PDArgsDestroy(args));
} }
...@@ -87,24 +87,24 @@ void testSequenceHelper(T1 setter, T2 getter) { ...@@ -87,24 +87,24 @@ void testSequenceHelper(T1 setter, T2 getter) {
PD_Arguments args = PDArgsCreateNone(); PD_Arguments args = PDArgsCreateNone();
ASSERT_EQ(kPD_NO_ERROR, PDArgsResize(args, 1)); ASSERT_EQ(kPD_NO_ERROR, PDArgsResize(args, 1));
PD_IVector ivec; paddle_ivector ivec;
int array[3] = {1, 2, 3}; int array[3] = {1, 2, 3};
ivec = PDIVectorCreate(array, 3, true, false); ivec = paddle_ivector_create(array, 3, true, false);
ASSERT_EQ(kPD_NO_ERROR, setter(args, 0, ivec)); ASSERT_EQ(kPD_NO_ERROR, setter(args, 0, ivec));
PD_IVector val = PDIVecCreateNone(); paddle_ivector val = paddle_ivector_create_none();
ASSERT_EQ(kPD_NO_ERROR, getter(args, 0, val)); ASSERT_EQ(kPD_NO_ERROR, getter(args, 0, val));
uint64_t size; uint64_t size;
ASSERT_EQ(kPD_NO_ERROR, PDIVectorGetSize(val, &size)); ASSERT_EQ(kPD_NO_ERROR, paddle_ivector_get_size(val, &size));
int* rawBuf; int* rawBuf;
ASSERT_EQ(kPD_NO_ERROR, PDIVectorGet(val, &rawBuf)); ASSERT_EQ(kPD_NO_ERROR, paddle_ivector_get(val, &rawBuf));
for (size_t i = 0; i < size; ++i) { for (size_t i = 0; i < size; ++i) {
ASSERT_EQ(array[i], rawBuf[i]); ASSERT_EQ(array[i], rawBuf[i]);
} }
ASSERT_EQ(kPD_NO_ERROR, PDIVecDestroy(ivec)); ASSERT_EQ(kPD_NO_ERROR, paddle_ivector_destroy(ivec));
ASSERT_EQ(kPD_NO_ERROR, PDIVecDestroy(val)); ASSERT_EQ(kPD_NO_ERROR, paddle_ivector_destroy(val));
ASSERT_EQ(kPD_NO_ERROR, PDArgsDestroy(args)); ASSERT_EQ(kPD_NO_ERROR, PDArgsDestroy(args));
} }
......
...@@ -59,12 +59,12 @@ TEST(GradientMachine, testPredict) { ...@@ -59,12 +59,12 @@ TEST(GradientMachine, testPredict) {
PD_Arguments inArgs = PDArgsCreateNone(); PD_Arguments inArgs = PDArgsCreateNone();
ASSERT_EQ(kPD_NO_ERROR, PDArgsResize(inArgs, 1)); ASSERT_EQ(kPD_NO_ERROR, PDArgsResize(inArgs, 1));
PD_Matrix mat = PDMatCreate(1, 100, false); paddle_matrix mat = paddle_matrix_create(1, 100, false);
static_assert(std::is_same<pd_real, paddle::real>::value, ""); static_assert(std::is_same<pd_real, paddle::real>::value, "");
auto data = randomBuffer(100); auto data = randomBuffer(100);
pd_real* rowPtr; pd_real* rowPtr;
ASSERT_EQ(kPD_NO_ERROR, PDMatGetRow(mat, 0, &rowPtr)); ASSERT_EQ(kPD_NO_ERROR, paddle_matrix_get_row(mat, 0, &rowPtr));
memcpy(rowPtr, data.data(), data.size() * sizeof(pd_real)); memcpy(rowPtr, data.data(), data.size() * sizeof(pd_real));
ASSERT_EQ(kPD_NO_ERROR, PDArgsSetValue(inArgs, 0, mat)); ASSERT_EQ(kPD_NO_ERROR, PDArgsSetValue(inArgs, 0, mat));
...@@ -87,16 +87,16 @@ TEST(GradientMachine, testPredict) { ...@@ -87,16 +87,16 @@ TEST(GradientMachine, testPredict) {
auto matPaddle = paddleOutArgs[0].value; auto matPaddle = paddleOutArgs[0].value;
uint64_t height, width; uint64_t height, width;
ASSERT_EQ(kPD_NO_ERROR, PDMatGetShape(mat, &height, &width)); ASSERT_EQ(kPD_NO_ERROR, paddle_matrix_get_shape(mat, &height, &width));
ASSERT_EQ(matPaddle->getHeight(), height); ASSERT_EQ(matPaddle->getHeight(), height);
ASSERT_EQ(matPaddle->getWidth(), width); ASSERT_EQ(matPaddle->getWidth(), width);
ASSERT_EQ(kPD_NO_ERROR, PDMatGetRow(mat, 0, &rowPtr)); ASSERT_EQ(kPD_NO_ERROR, paddle_matrix_get_row(mat, 0, &rowPtr));
for (size_t i = 0; i < width; ++i) { for (size_t i = 0; i < width; ++i) {
ASSERT_NEAR(matPaddle->getData()[i], rowPtr[i], 1e-5); ASSERT_NEAR(matPaddle->getData()[i], rowPtr[i], 1e-5);
} }
ASSERT_EQ(kPD_NO_ERROR, PDMatDestroy(mat)); ASSERT_EQ(kPD_NO_ERROR, paddle_matrix_destroy(mat));
ASSERT_EQ(kPD_NO_ERROR, PDArgsDestroy(inArgs)); ASSERT_EQ(kPD_NO_ERROR, PDArgsDestroy(inArgs));
ASSERT_EQ(kPD_NO_ERROR, PDArgsDestroy(outArgs)); ASSERT_EQ(kPD_NO_ERROR, PDArgsDestroy(outArgs));
std::swap(machineSlave, machine); std::swap(machineSlave, machine);
......
...@@ -16,30 +16,31 @@ limitations under the License. */ ...@@ -16,30 +16,31 @@ limitations under the License. */
#include "gtest/gtest.h" #include "gtest/gtest.h"
TEST(CAPIMatrix, create) { TEST(CAPIMatrix, create) {
PD_Matrix mat = PDMatCreate(128, 32, false); paddle_matrix mat = paddle_matrix_create(128, 32, false);
std::vector<pd_real> sampleRow; std::vector<pd_real> sampleRow;
sampleRow.resize(32); sampleRow.resize(32);
for (size_t i = 0; i < sampleRow.size(); ++i) { for (size_t i = 0; i < sampleRow.size(); ++i) {
sampleRow[i] = 1.0 / (i + 1.0); sampleRow[i] = 1.0 / (i + 1.0);
} }
ASSERT_EQ(kPD_NO_ERROR, PDMatCopyToRow(mat, 0, sampleRow.data())); ASSERT_EQ(kPD_NO_ERROR, paddle_matrix_set_row(mat, 0, sampleRow.data()));
ASSERT_EQ(kPD_OUT_OF_RANGE, PDMatCopyToRow(mat, 128, sampleRow.data())); ASSERT_EQ(kPD_OUT_OF_RANGE,
paddle_matrix_set_row(mat, 128, sampleRow.data()));
pd_real* arrayPtr; pd_real* arrayPtr;
ASSERT_EQ(kPD_NO_ERROR, PDMatGetRow(mat, 0, &arrayPtr)); ASSERT_EQ(kPD_NO_ERROR, paddle_matrix_get_row(mat, 0, &arrayPtr));
for (size_t i = 0; i < sampleRow.size(); ++i) { for (size_t i = 0; i < sampleRow.size(); ++i) {
ASSERT_NEAR(sampleRow[i], arrayPtr[i], 1e-5); ASSERT_NEAR(sampleRow[i], arrayPtr[i], 1e-5);
} }
uint64_t height, width; uint64_t height, width;
ASSERT_EQ(kPD_NO_ERROR, PDMatGetShape(mat, &height, &width)); ASSERT_EQ(kPD_NO_ERROR, paddle_matrix_get_shape(mat, &height, &width));
ASSERT_EQ(128UL, height); ASSERT_EQ(128UL, height);
ASSERT_EQ(32UL, width); ASSERT_EQ(32UL, width);
ASSERT_EQ(kPD_NO_ERROR, PDMatDestroy(mat)); ASSERT_EQ(kPD_NO_ERROR, paddle_matrix_destroy(mat));
} }
TEST(CAPIMatrix, createNone) { TEST(CAPIMatrix, createNone) {
PD_Matrix mat = PDMatCreateNone(); paddle_matrix mat = paddle_matrix_create_none();
ASSERT_EQ(kPD_NO_ERROR, PDMatDestroy(mat)); ASSERT_EQ(kPD_NO_ERROR, paddle_matrix_destroy(mat));
} }
...@@ -16,16 +16,16 @@ limitations under the License. */ ...@@ -16,16 +16,16 @@ limitations under the License. */
#include "gtest/gtest.h" #include "gtest/gtest.h"
TEST(CAPIVector, create) { TEST(CAPIVector, create) {
PD_IVector vec; paddle_ivector vec;
int array[3] = {1, 2, 3}; int array[3] = {1, 2, 3};
vec = PDIVectorCreate(array, 3, true, false); vec = paddle_ivector_create(array, 3, true, false);
ASSERT_EQ(kPD_NO_ERROR, PDIVectorResize(vec, 1000)); ASSERT_EQ(kPD_NO_ERROR, paddle_ivector_resize(vec, 1000));
uint64_t size; uint64_t size;
ASSERT_EQ(kPD_NO_ERROR, PDIVectorGetSize(vec, &size)); ASSERT_EQ(kPD_NO_ERROR, paddle_ivector_get_size(vec, &size));
ASSERT_EQ(kPD_NO_ERROR, PDIVecDestroy(vec)); ASSERT_EQ(kPD_NO_ERROR, paddle_ivector_destroy(vec));
} }
TEST(CAPIVector, createNone) { TEST(CAPIVector, createNone) {
PD_IVector vec = PDIVecCreateNone(); paddle_ivector vec = paddle_ivector_create_none();
ASSERT_EQ(kPD_NO_ERROR, PDIVecDestroy(vec)); ASSERT_EQ(kPD_NO_ERROR, paddle_ivector_destroy(vec));
} }
/* 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. */
#ifndef __PADDLE_CAPI_VECTOR_H__
#define __PADDLE_CAPI_VECTOR_H__
#include <stdbool.h>
#include <stdint.h>
#include "config.h"
#include "error.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* Int Vector Functions. Return will be a paddle_error type.
*/
typedef void* paddle_ivector;
/**
* @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 paddle_ivector paddle_ivector_create_none();
/**
* @brief paddle_ivector_create create a paddle int vector
* @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 paddle_error
*/
PD_API paddle_ivector paddle_ivector_create(int* array,
uint64_t size,
bool copy,
bool useGPU);
/**
* @brief paddle_ivector_destroy destory an int vector.
* @param ivec vector to be destoried.
* @return paddle_error
*/
PD_API paddle_error paddle_ivector_destroy(paddle_ivector ivec);
/**
* @brief paddle_ivector_get 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 paddle_error
*/
PD_API paddle_error paddle_ivector_get(paddle_ivector ivec, int** buffer);
/**
* @brief paddle_ivector_resize resize the int vector.
* @param [in] ivec: int vector
* @param [in] size: size to change
* @return paddle_error
*/
PD_API paddle_error paddle_ivector_resize(paddle_ivector ivec, uint64_t size);
/**
* @brief paddle_ivector_get_size get the size of int vector.
* @param [in] ivec: int vector
* @param [out] size: return size of this int vector.
* @return paddle_error
*/
PD_API paddle_error paddle_ivector_get_size(paddle_ivector ivec,
uint64_t* size);
#ifdef __cplusplus
}
#endif
#endif
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册