提交 f22573bd 编写于 作者: W wangyang59

changed to isUsingGpu() in PaddleAPI.h and throw exceptions instead of CHECK

上级 2be3a747
...@@ -53,10 +53,14 @@ Matrix* Matrix::createDense(const std::vector<float>& data, size_t height, ...@@ -53,10 +53,14 @@ Matrix* Matrix::createDense(const std::vector<float>& data, size_t height,
} }
Matrix* Matrix::createDenseFromNumpy(float* data, int dim1, int dim2, Matrix* Matrix::createDenseFromNumpy(float* data, int dim1, int dim2,
bool copy, bool useGpu) { bool copy, bool useGpu)
throw (UnsupportError) {
if (useGpu) { if (useGpu) {
/// Gpu mode only supports copy=True /// Gpu mode only supports copy=True
CHECK(copy); if (!copy) {
UnsupportError e;
throw e;
}
return Matrix::createGpuDenseFromNumpy(data, dim1, dim2); return Matrix::createGpuDenseFromNumpy(data, dim1, dim2);
} else { } else {
return Matrix::createCpuDenseFromNumpy(data, dim1, dim2, copy); return Matrix::createCpuDenseFromNumpy(data, dim1, dim2, copy);
......
...@@ -43,7 +43,7 @@ using namespace paddle::enumeration_wrapper; // NOLINT ...@@ -43,7 +43,7 @@ using namespace paddle::enumeration_wrapper; // NOLINT
void initPaddle(int argc, char** argv); void initPaddle(int argc, char** argv);
/// Return FLAGS_use_gpu /// Return FLAGS_use_gpu
bool isUseGpu(); bool isUsingGpu();
/// Return true if this py_paddle is compiled in GPU Version /// Return true if this py_paddle is compiled in GPU Version
bool isGpuVersion(); bool isGpuVersion();
...@@ -105,7 +105,7 @@ public: ...@@ -105,7 +105,7 @@ public:
* Create A Matrix with height,width, which is filled by zero. * Create A Matrix with height,width, which is filled by zero.
*/ */
static Matrix* createZero(size_t height, size_t width, static Matrix* createZero(size_t height, size_t width,
bool useGpu = isUseGpu()); bool useGpu = isUsingGpu());
/** /**
* Create Sparse Matrix. * Create Sparse Matrix.
...@@ -118,7 +118,7 @@ public: ...@@ -118,7 +118,7 @@ public:
*/ */
static Matrix* createSparse(size_t height, size_t width, size_t nnz, static Matrix* createSparse(size_t height, size_t width, size_t nnz,
bool isNonVal = true, bool trans = false, bool isNonVal = true, bool trans = false,
bool useGpu = isUseGpu()); bool useGpu = isUsingGpu());
/** /**
* Create Dense Matrix. * Create Dense Matrix.
...@@ -127,11 +127,12 @@ public: ...@@ -127,11 +127,12 @@ public:
* @note the value will be copy into a new matrix. * @note the value will be copy into a new matrix.
*/ */
static Matrix* createDense(const std::vector<float>& data, size_t height, static Matrix* createDense(const std::vector<float>& data, size_t height,
size_t width, bool useGpu = isUseGpu()); size_t width, bool useGpu = isUsingGpu());
static Matrix* createDenseFromNumpy(float* data, int dim1, int dim2, static Matrix* createDenseFromNumpy(float* data, int dim1, int dim2,
bool copy = true, bool copy = true,
bool useGpu = isUseGpu()); bool useGpu = isUsingGpu())
throw (UnsupportError) ;
/** /**
* Create Cpu Dense Matrix from numpy matrix, dtype=float32 * Create Cpu Dense Matrix from numpy matrix, dtype=float32
...@@ -229,7 +230,7 @@ public: ...@@ -229,7 +230,7 @@ public:
~Vector(); ~Vector();
/// Create Vector filled with zero. /// Create Vector filled with zero.
static Vector* createZero(size_t sz, bool useGpu = isUseGpu()); static Vector* createZero(size_t sz, bool useGpu = isUsingGpu());
/** /**
* Create Vector from list of float. * Create Vector from list of float.
...@@ -237,10 +238,11 @@ public: ...@@ -237,10 +238,11 @@ public:
* It will create a new vector, and copy data into it. * It will create a new vector, and copy data into it.
*/ */
static Vector* create(const std::vector<float>& data, static Vector* create(const std::vector<float>& data,
bool useGpu = isUseGpu()); bool useGpu = isUsingGpu());
static Vector* createVectorFromNumpy(float* data, int dim, bool copy = true, static Vector* createVectorFromNumpy(float* data, int dim, bool copy = true,
bool useGpu = isUseGpu()); bool useGpu = isUsingGpu())
throw (UnsupportError) ;
/** /**
* Create Cpu Vector from numpy array, which dtype=float32 * Create Cpu Vector from numpy array, which dtype=float32
* *
...@@ -290,17 +292,18 @@ class IVector { ...@@ -290,17 +292,18 @@ class IVector {
public: public:
/// Create IVector filled with zero /// Create IVector filled with zero
static IVector* createZero(size_t sz, bool useGpu = isUseGpu()); static IVector* createZero(size_t sz, bool useGpu = isUsingGpu());
/** /**
* Create IVector from list of int. * Create IVector from list of int.
* It will create a new vector, and copy data into it. * It will create a new vector, and copy data into it.
*/ */
static IVector* create(const std::vector<int>& data, static IVector* create(const std::vector<int>& data,
bool useGpu = isUseGpu()); bool useGpu = isUsingGpu());
static IVector* createVectorFromNumpy(int* data, int dim, bool copy = true, static IVector* createVectorFromNumpy(int* data, int dim, bool copy = true,
bool useGpu = isUseGpu()); bool useGpu = isUsingGpu())
throw (UnsupportError) ;
/** /**
* Create Cpu IVector from numpy array, which dtype=int32 * Create Cpu IVector from numpy array, which dtype=int32
......
...@@ -41,7 +41,7 @@ IntWithFloatArray::IntWithFloatArray(const float* v, const int* i, size_t l, ...@@ -41,7 +41,7 @@ IntWithFloatArray::IntWithFloatArray(const float* v, const int* i, size_t l,
bool f) bool f)
: valBuf(v), idxBuf(i), length(l), needFree(f) {} : valBuf(v), idxBuf(i), length(l), needFree(f) {}
bool isUseGpu() {return FLAGS_use_gpu;} bool isUsingGpu() {return FLAGS_use_gpu;}
bool isGpuVersion() { bool isGpuVersion() {
#ifdef PADDLE_ONLY_CPU #ifdef PADDLE_ONLY_CPU
......
...@@ -40,10 +40,14 @@ IVector* IVector::create(const std::vector<int>& data, bool useGpu) { ...@@ -40,10 +40,14 @@ IVector* IVector::create(const std::vector<int>& data, bool useGpu) {
} }
IVector* IVector::createVectorFromNumpy(int* data, int dim, bool copy, IVector* IVector::createVectorFromNumpy(int* data, int dim, bool copy,
bool useGpu) { bool useGpu)
throw (UnsupportError) {
if (useGpu) { if (useGpu) {
/// if use gpu only copy=true is supported /// if use gpu only copy=true is supported
CHECK(copy); if (!copy) {
UnsupportError e;
throw e;
}
return IVector::createGpuVectorFromNumpy(data, dim); return IVector::createGpuVectorFromNumpy(data, dim);
} else { } else {
return IVector::createCpuVectorFromNumpy(data, dim, copy); return IVector::createCpuVectorFromNumpy(data, dim, copy);
...@@ -200,10 +204,14 @@ Vector* Vector::createByPaddleVectorPtr(void* ptr) { ...@@ -200,10 +204,14 @@ Vector* Vector::createByPaddleVectorPtr(void* ptr) {
} }
Vector* Vector::createVectorFromNumpy(float* data, int dim, bool copy, Vector* Vector::createVectorFromNumpy(float* data, int dim, bool copy,
bool useGpu) { bool useGpu)
throw (UnsupportError) {
if (useGpu) { if (useGpu) {
/// if use gpu only copy=True is supported /// if use gpu only copy=True is supported
CHECK(copy); if (!copy) {
UnsupportError e;
throw e;
}
return Vector::createGpuVectorFromNumpy(data, dim); return Vector::createGpuVectorFromNumpy(data, dim);
} else { } else {
return Vector::createCpuVectorFromNumpy(data, dim, copy); return Vector::createCpuVectorFromNumpy(data, dim, copy);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册