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

Rename some API to C-Style

上级 08113b20
......@@ -23,25 +23,25 @@ using paddle::capi::cast;
extern "C" {
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;
delete castArg(args);
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;
*size = castArg(args)->args.size();
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;
castArg(args)->args.resize(size);
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;
auto m = paddle::capi::cast<paddle::capi::CMatrix>(mat);
if (m->mat == nullptr) return kPD_NULLPTR;
......@@ -51,7 +51,7 @@ PD_Error PDArgsSetValue(PD_Arguments args, uint64_t ID, PD_Matrix mat) {
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;
auto m = paddle::capi::cast<paddle::capi::CMatrix>(mat);
auto a = castArg(args);
......@@ -60,7 +60,7 @@ PD_Error PDArgsGetValue(PD_Arguments args, uint64_t ID, PD_Matrix mat) {
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;
auto iv = castIVec(ids);
auto a = castArg(args);
......@@ -69,7 +69,7 @@ PD_Error PDArgsGetIds(PD_Arguments args, uint64_t ID, PD_IVector ids) {
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.
if (args == nullptr || ids == nullptr) return kPD_NULLPTR;
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) {
return kPD_NO_ERROR;
}
PD_Error PDArgsSetSequenceStartPos(PD_Arguments args,
uint64_t ID,
PD_IVector seqPos) {
paddle_error PDArgsSetSequenceStartPos(PD_Arguments args,
uint64_t ID,
paddle_ivector seqPos) {
if (args == nullptr || seqPos == nullptr) return kPD_NULLPTR;
auto iv = paddle::capi::cast<paddle::capi::CIVector>(seqPos);
if (iv->vec == nullptr) return kPD_NULLPTR;
......@@ -93,9 +93,9 @@ PD_Error PDArgsSetSequenceStartPos(PD_Arguments args,
return kPD_NO_ERROR;
}
PD_Error PDArgsSetSubSequenceStartPos(PD_Arguments args,
uint64_t ID,
PD_IVector subSeqPos) {
paddle_error PDArgsSetSubSequenceStartPos(PD_Arguments args,
uint64_t ID,
paddle_ivector subSeqPos) {
if (args == nullptr || subSeqPos == nullptr) return kPD_NULLPTR;
auto iv = paddle::capi::cast<paddle::capi::CIVector>(subSeqPos);
if (iv->vec == nullptr) return kPD_NULLPTR;
......@@ -106,9 +106,9 @@ PD_Error PDArgsSetSubSequenceStartPos(PD_Arguments args,
return kPD_NO_ERROR;
}
PD_Error PDArgsGetSequenceStartPos(PD_Arguments args,
uint64_t ID,
PD_IVector seqPos) {
paddle_error PDArgsGetSequenceStartPos(PD_Arguments args,
uint64_t ID,
paddle_ivector seqPos) {
if (args == nullptr || seqPos == nullptr) return kPD_NULLPTR;
auto iv = castIVec(seqPos);
auto a = castArg(args);
......@@ -118,9 +118,9 @@ PD_Error PDArgsGetSequenceStartPos(PD_Arguments args,
return kPD_NO_ERROR;
}
PD_Error PDArgsGetSubSequenceStartPos(PD_Arguments args,
uint64_t ID,
PD_IVector subSeqPos) {
paddle_error PDArgsGetSubSequenceStartPos(PD_Arguments args,
uint64_t ID,
paddle_ivector subSeqPos) {
if (args == nullptr || subSeqPos == nullptr) return kPD_NULLPTR;
auto iv = castIVec(subSeqPos);
auto a = castArg(args);
......
......@@ -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
# inference.
set(CAPI_HEADER PaddleCAPI.h)
file(GLOB CAPI_HEADERS *.h)
set(CAPI_PRIVATE_HEADER PaddleCAPIPrivate.h)
list(REMOVE_ITEM CAPI_HEADERS ${CAPI_PRIVATE_HEADER})
file(GLOB CAPI_SOURCES *.cpp)
# 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})
......
......@@ -38,9 +38,9 @@ NeuralNetwork* newCustomNerualNetwork(const std::string& name,
} // namespace paddle
extern "C" {
PD_Error PDGradientMachineCreateForPredict(PD_GradientMachine* machine,
void* modelConfigProtobuf,
int size) {
paddle_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 @@ PD_Error PDGradientMachineCreateForPredict(PD_GradientMachine* machine,
return kPD_NO_ERROR;
}
PD_Error PDGradientMachineDestroy(PD_GradientMachine machine) {
paddle_error PDGradientMachineDestroy(PD_GradientMachine machine) {
delete cast(machine);
return kPD_NO_ERROR;
}
PD_Error PDGradientMachineLoadParameterFromDisk(PD_GradientMachine machine,
const char* path) {
paddle_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 @@ PD_Error PDGradientMachineLoadParameterFromDisk(PD_GradientMachine machine,
return kPD_NO_ERROR;
}
PD_Error PDGradientMachineForward(PD_GradientMachine machine,
PD_Arguments inArgs,
PD_Arguments outArgs,
bool isTrain) {
paddle_error PDGradientMachineForward(PD_GradientMachine machine,
PD_Arguments inArgs,
PD_Arguments outArgs,
bool isTrain) {
auto m = cast(machine);
auto in = paddle::capi::cast<paddle::capi::CArguments>(inArgs);
auto out = paddle::capi::cast<paddle::capi::CArguments>(outArgs);
......@@ -83,10 +83,10 @@ PD_Error PDGradientMachineForward(PD_GradientMachine machine,
return kPD_NO_ERROR;
}
PD_Error PDGradientMachineCreateSharedParam(PD_GradientMachine origin,
void* modelConfigProtobuf,
int size,
PD_GradientMachine* slave) {
paddle_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;
......
......@@ -29,7 +29,7 @@ static void initPaddle(int argc, char** argv) {
}
extern "C" {
PD_Error PDInit(int argc, char** argv) {
paddle_error PDInit(int argc, char** argv) {
std::vector<char*> realArgv;
realArgv.reserve(argc + 1);
realArgv.push_back(strdup(""));
......
......@@ -18,22 +18,28 @@ limitations under the License. */
#define cast(v) paddle::capi::cast<paddle::capi::CMatrix>(v)
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();
ptr->mat = paddle::Matrix::create(height, width, false, useGpu);
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;
auto ptr = cast(mat);
delete ptr;
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;
auto ptr = cast(mat);
if (ptr->mat == nullptr) return kPD_NULLPTR;
......@@ -48,7 +54,9 @@ PD_Error PDMatCopyToRow(PD_Matrix mat, uint64_t rowID, pd_real* rowArray) {
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;
auto ptr = cast(mat);
if (ptr->mat == nullptr) return kPD_NULLPTR;
......@@ -57,7 +65,9 @@ PD_Error PDMatGetRow(PD_Matrix mat, uint64_t rowID, pd_real** rawRowBuffer) {
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 (height != nullptr) {
*height = cast(mat)->mat->getHeight();
......
......@@ -17,10 +17,9 @@ limitations under the License. */
#include <stdbool.h>
#include <stdint.h>
#include "config.h"
// 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")))
#include "error.h"
#include "matrix.h"
#include "vector.h"
#ifdef __cplusplus
extern "C" {
......@@ -33,132 +32,6 @@ extern "C" {
* 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
* array of arguemnt.
......@@ -174,25 +47,25 @@ PD_API PD_Arguments PDArgsCreateNone();
/**
* @brief PDArgsDestroy Destroy the arguments
* @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
* @param [in] args arguments array
* @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.
* @param args arguments 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
......@@ -200,9 +73,11 @@ PD_API PD_Error PDArgsResize(PD_Arguments args, uint64_t size);
* @param args arguments array
* @param ID array index
* @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
......@@ -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] ID array index
* @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
......@@ -220,9 +97,11 @@ PD_API PD_Error PDArgsGetValue(PD_Arguments args, uint64_t ID, PD_Matrix mat);
* @param args arguments array
* @param ID array index
* @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
......@@ -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] ID array index
* @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
......@@ -240,22 +121,22 @@ PD_API PD_Error PDArgsSetIds(PD_Arguments args, uint64_t ID, PD_IVector ids);
* @param args arguments array
* @param ID array index
* @param seqPos sequence position array.
* @return PD_Error
* @return paddle_error
*/
PD_API PD_Error PDArgsSetSequenceStartPos(PD_Arguments args,
uint64_t ID,
PD_IVector seqPos);
PD_API paddle_error PDArgsSetSequenceStartPos(PD_Arguments args,
uint64_t ID,
paddle_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
* @return paddle_error
*/
PD_API PD_Error PDArgsGetSequenceStartPos(PD_Arguments args,
uint64_t ID,
PD_IVector seqPos);
PD_API paddle_error PDArgsGetSequenceStartPos(PD_Arguments args,
uint64_t ID,
paddle_ivector seqPos);
/**
* @brief PDArgsSetSubSequenceStartPos Set sub-sequence start position vector of
......@@ -263,11 +144,11 @@ PD_API PD_Error PDArgsGetSequenceStartPos(PD_Arguments args,
* @param args arguments array
* @param ID array index
* @param subSeqPos sub-sequence start position array.
* @return PD_Error
* @return paddle_error
*/
PD_API PD_Error PDArgsSetSubSequenceStartPos(PD_Arguments args,
uint64_t ID,
PD_IVector subSeqPos);
PD_API paddle_error PDArgsSetSubSequenceStartPos(PD_Arguments args,
uint64_t ID,
paddle_ivector subSeqPos);
/**
* @brief PDArgsGetSubSequenceStartPos Get sub-sequence start position vector of
......@@ -275,11 +156,11 @@ PD_API PD_Error PDArgsSetSubSequenceStartPos(PD_Arguments args,
* @param args arguments array
* @param ID array index
* @param subSeqPos sub-sequence start position array
* @return PD_Error
* @return paddle_error
*/
PD_API PD_Error PDArgsGetSubSequenceStartPos(PD_Arguments args,
uint64_t ID,
PD_IVector subSeqPos);
PD_API paddle_error PDArgsGetSubSequenceStartPos(PD_Arguments args,
uint64_t ID,
paddle_ivector subSeqPos);
/**
* @brief GradientMachine means a neural network.
*/
......@@ -291,19 +172,18 @@ typedef void* PD_GradientMachine;
* @param [out] machine that used for model inference.
* @param [in] modelConfigProtobuf
* @param [in] size
* @return PD_Error
* @return paddle_error
*/
PD_API PD_Error PDGradientMachineCreateForPredict(PD_GradientMachine* machine,
void* modelConfigProtobuf,
int size);
PD_API paddle_error PDGradientMachineCreateForPredict(
PD_GradientMachine* machine, void* modelConfigProtobuf, int size);
/**
* @brief PDGradientMachineLoadParameterFromDisk Load parameter from disk.
* @param machine Gradient Machine.
* @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);
/**
......@@ -312,12 +192,12 @@ PD_API PD_Error PDGradientMachineLoadParameterFromDisk(
* @param inArgs input arguments
* @param outArgs output arguments
* @param isTrain is train or not
* @return PD_Error
* @return paddle_error
*/
PD_API PD_Error PDGradientMachineForward(PD_GradientMachine machine,
PD_Arguments inArgs,
PD_Arguments outArgs,
bool isTrain);
PD_API paddle_error PDGradientMachineForward(PD_GradientMachine machine,
PD_Arguments inArgs,
PD_Arguments outArgs,
bool isTrain);
/**
* @brief PDGradientMachineCreateSharedParam Create a gradient machine, which
......@@ -326,24 +206,25 @@ PD_API PD_Error PDGradientMachineForward(PD_GradientMachine 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
* @return paddle_error
*/
PD_API PD_Error PDGradientMachineCreateSharedParam(PD_GradientMachine origin,
void* modelConfigProtobuf,
int size,
PD_GradientMachine* slave);
PD_API paddle_error
PDGradientMachineCreateSharedParam(PD_GradientMachine origin,
void* modelConfigProtobuf,
int size,
PD_GradientMachine* slave);
/**
* @brief PDGradientMachineDestroy Destroy a gradient machine
* @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.
*/
PD_API PD_Error PDInit(int argc, char** argv);
PD_API paddle_error PDInit(int argc, char** argv);
#ifdef __cplusplus
}
......
......@@ -19,9 +19,14 @@ using paddle::capi::cast;
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();
if (copy) {
ptr->vec = paddle::IVector::create(size, useGPU);
......@@ -32,13 +37,13 @@ PD_IVector PDIVectorCreate(int* array, uint64_t size, bool copy, bool useGPU) {
return ptr;
}
PD_Error PDIVecDestroy(PD_IVector ivec) {
paddle_error paddle_ivector_destroy(paddle_ivector ivec) {
if (ivec == nullptr) return kPD_NULLPTR;
delete cast<paddle::capi::CIVector>(ivec);
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;
auto v = cast<paddle::capi::CIVector>(ivec);
if (v->vec == nullptr) return kPD_NULLPTR;
......@@ -46,7 +51,7 @@ PD_Error PDIVectorGet(PD_IVector ivec, int** buffer) {
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;
auto v = cast<paddle::capi::CIVector>(ivec);
if (v->vec == nullptr) return kPD_NULLPTR;
......@@ -54,7 +59,7 @@ PD_Error PDIVectorResize(PD_IVector ivec, uint64_t size) {
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;
auto v = cast<paddle::capi::CIVector>(ivec);
if (v->vec == nullptr) return kPD_NULLPTR;
......
......@@ -3,4 +3,8 @@
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
#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) {
PD_Arguments args = PDArgsCreateNone();
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) {
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));
PD_Matrix val = PDMatCreateNone();
paddle_matrix val = paddle_matrix_create_none();
ASSERT_EQ(kPD_NO_ERROR, PDArgsGetValue(args, 0, val));
......@@ -54,15 +54,15 @@ TEST(CAPIArguments, value) {
pd_real* row1;
pd_real* row2;
ASSERT_EQ(kPD_NO_ERROR, PDMatGetRow(mat, i, &row1));
ASSERT_EQ(kPD_NO_ERROR, PDMatGetRow(val, i, &row2));
ASSERT_EQ(kPD_NO_ERROR, paddle_matrix_get_row(mat, i, &row1));
ASSERT_EQ(kPD_NO_ERROR, paddle_matrix_get_row(val, i, &row2));
ASSERT_EQ(row1, row2);
}
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));
paddle_ivector ivec = paddle_ivector_create_none();
ASSERT_EQ(kPD_NO_ERROR, paddle_ivector_destroy(ivec));
ASSERT_EQ(kPD_NO_ERROR, paddle_matrix_destroy(val));
ASSERT_EQ(kPD_NO_ERROR, paddle_matrix_destroy(mat));
ASSERT_EQ(kPD_NO_ERROR, PDArgsDestroy(args));
}
......@@ -70,15 +70,15 @@ TEST(CAPIArguments, ids) {
PD_Arguments args = PDArgsCreateNone();
ASSERT_EQ(kPD_NO_ERROR, PDArgsResize(args, 1));
PD_IVector ivec;
paddle_ivector ivec;
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));
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, PDIVecDestroy(ivec));
ASSERT_EQ(kPD_NO_ERROR, PDIVecDestroy(val));
ASSERT_EQ(kPD_NO_ERROR, paddle_ivector_destroy(ivec));
ASSERT_EQ(kPD_NO_ERROR, paddle_ivector_destroy(val));
ASSERT_EQ(kPD_NO_ERROR, PDArgsDestroy(args));
}
......@@ -87,24 +87,24 @@ void testSequenceHelper(T1 setter, T2 getter) {
PD_Arguments args = PDArgsCreateNone();
ASSERT_EQ(kPD_NO_ERROR, PDArgsResize(args, 1));
PD_IVector ivec;
paddle_ivector ivec;
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));
PD_IVector val = PDIVecCreateNone();
paddle_ivector val = paddle_ivector_create_none();
ASSERT_EQ(kPD_NO_ERROR, getter(args, 0, val));
uint64_t size;
ASSERT_EQ(kPD_NO_ERROR, PDIVectorGetSize(val, &size));
ASSERT_EQ(kPD_NO_ERROR, paddle_ivector_get_size(val, &size));
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) {
ASSERT_EQ(array[i], rawBuf[i]);
}
ASSERT_EQ(kPD_NO_ERROR, PDIVecDestroy(ivec));
ASSERT_EQ(kPD_NO_ERROR, PDIVecDestroy(val));
ASSERT_EQ(kPD_NO_ERROR, paddle_ivector_destroy(ivec));
ASSERT_EQ(kPD_NO_ERROR, paddle_ivector_destroy(val));
ASSERT_EQ(kPD_NO_ERROR, PDArgsDestroy(args));
}
......
......@@ -59,12 +59,12 @@ TEST(GradientMachine, testPredict) {
PD_Arguments inArgs = PDArgsCreateNone();
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, "");
auto data = randomBuffer(100);
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));
ASSERT_EQ(kPD_NO_ERROR, PDArgsSetValue(inArgs, 0, mat));
......@@ -87,16 +87,16 @@ TEST(GradientMachine, testPredict) {
auto matPaddle = paddleOutArgs[0].value;
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->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) {
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(outArgs));
std::swap(machineSlave, machine);
......
......@@ -16,30 +16,31 @@ limitations under the License. */
#include "gtest/gtest.h"
TEST(CAPIMatrix, create) {
PD_Matrix mat = PDMatCreate(128, 32, false);
paddle_matrix mat = paddle_matrix_create(128, 32, false);
std::vector<pd_real> sampleRow;
sampleRow.resize(32);
for (size_t i = 0; i < sampleRow.size(); ++i) {
sampleRow[i] = 1.0 / (i + 1.0);
}
ASSERT_EQ(kPD_NO_ERROR, PDMatCopyToRow(mat, 0, sampleRow.data()));
ASSERT_EQ(kPD_OUT_OF_RANGE, PDMatCopyToRow(mat, 128, sampleRow.data()));
ASSERT_EQ(kPD_NO_ERROR, paddle_matrix_set_row(mat, 0, sampleRow.data()));
ASSERT_EQ(kPD_OUT_OF_RANGE,
paddle_matrix_set_row(mat, 128, sampleRow.data()));
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) {
ASSERT_NEAR(sampleRow[i], arrayPtr[i], 1e-5);
}
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(32UL, width);
ASSERT_EQ(kPD_NO_ERROR, PDMatDestroy(mat));
ASSERT_EQ(kPD_NO_ERROR, paddle_matrix_destroy(mat));
}
TEST(CAPIMatrix, createNone) {
PD_Matrix mat = PDMatCreateNone();
ASSERT_EQ(kPD_NO_ERROR, PDMatDestroy(mat));
paddle_matrix mat = paddle_matrix_create_none();
ASSERT_EQ(kPD_NO_ERROR, paddle_matrix_destroy(mat));
}
......@@ -16,16 +16,16 @@ limitations under the License. */
#include "gtest/gtest.h"
TEST(CAPIVector, create) {
PD_IVector vec;
paddle_ivector vec;
int array[3] = {1, 2, 3};
vec = PDIVectorCreate(array, 3, true, false);
ASSERT_EQ(kPD_NO_ERROR, PDIVectorResize(vec, 1000));
vec = paddle_ivector_create(array, 3, true, false);
ASSERT_EQ(kPD_NO_ERROR, paddle_ivector_resize(vec, 1000));
uint64_t size;
ASSERT_EQ(kPD_NO_ERROR, PDIVectorGetSize(vec, &size));
ASSERT_EQ(kPD_NO_ERROR, PDIVecDestroy(vec));
ASSERT_EQ(kPD_NO_ERROR, paddle_ivector_get_size(vec, &size));
ASSERT_EQ(kPD_NO_ERROR, paddle_ivector_destroy(vec));
}
TEST(CAPIVector, createNone) {
PD_IVector vec = PDIVecCreateNone();
ASSERT_EQ(kPD_NO_ERROR, PDIVecDestroy(vec));
paddle_ivector vec = paddle_ivector_create_none();
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.
先完成此消息的编辑!
想要评论请 注册