提交 afa2a88d 编写于 作者: F fengjiayi

add conditional compilation for tensor

上级 66cf21c8
...@@ -51,6 +51,7 @@ class Tensor { ...@@ -51,6 +51,7 @@ class Tensor {
!(holder_->place() == !(holder_->place() ==
place) /* some versions of boost::variant don't have operator!= */ place) /* some versions of boost::variant don't have operator!= */
|| holder_->size() < numel_ * sizeof(T) + offset_) { || holder_->size() < numel_ * sizeof(T) + offset_) {
#ifdef __CUDACC__
switch (place.which()) { switch (place.which()) {
case 0: case 0:
holder_.reset(new PlaceholderImpl<T, platform::GPUPlace>( holder_.reset(new PlaceholderImpl<T, platform::GPUPlace>(
...@@ -62,6 +63,10 @@ class Tensor { ...@@ -62,6 +63,10 @@ class Tensor {
boost::get<platform::CPUPlace>(place), numel_ * sizeof(T))); boost::get<platform::CPUPlace>(place), numel_ * sizeof(T)));
break; break;
} }
#else
holder_.reset(new PlaceholderImpl<T, platform::CPUPlace>(
boost::get<platform::CPUPlace>(place), numel_ * sizeof(T)));
#endif
offset_ = 0; offset_ = 0;
} }
......
...@@ -72,7 +72,7 @@ TEST(Tensor, MutableData) { ...@@ -72,7 +72,7 @@ TEST(Tensor, MutableData) {
p2 = src_tensor.mutable_data<float>(make_ddim({2, 2}), CPUPlace()); p2 = src_tensor.mutable_data<float>(make_ddim({2, 2}), CPUPlace());
EXPECT_EQ(p1, p2); EXPECT_EQ(p1, p2);
} }
#ifdef __CUDACC__ #ifdef __CUDACC__
{ {
Tensor src_tensor; Tensor src_tensor;
float* p1 = nullptr; float* p1 = nullptr;
...@@ -94,7 +94,7 @@ TEST(Tensor, MutableData) { ...@@ -94,7 +94,7 @@ TEST(Tensor, MutableData) {
p2 = src_tensor.mutable_data<float>(make_ddim({2, 2}), GPUPlace()); p2 = src_tensor.mutable_data<float>(make_ddim({2, 2}), GPUPlace());
EXPECT_EQ(p1, p2); EXPECT_EQ(p1, p2);
} }
#endif #endif
} }
TEST(Tensor, ShareDataFrom) { TEST(Tensor, ShareDataFrom) {
...@@ -123,7 +123,7 @@ TEST(Tensor, ShareDataFrom) { ...@@ -123,7 +123,7 @@ TEST(Tensor, ShareDataFrom) {
ASSERT_EQ(src_tensor.data<int>(), dst_tensor.data<int>()); ASSERT_EQ(src_tensor.data<int>(), dst_tensor.data<int>());
} }
#ifdef __CUDACC__ #ifdef __CUDACC__
{ {
Tensor src_tensor; Tensor src_tensor;
Tensor dst_tensor; Tensor dst_tensor;
...@@ -131,7 +131,7 @@ TEST(Tensor, ShareDataFrom) { ...@@ -131,7 +131,7 @@ TEST(Tensor, ShareDataFrom) {
dst_tensor.ShareDataFrom<int>(src_tensor); dst_tensor.ShareDataFrom<int>(src_tensor);
ASSERT_EQ(src_tensor.data<int>(), dst_tensor.data<int>()); ASSERT_EQ(src_tensor.data<int>(), dst_tensor.data<int>());
} }
#endif #endif
} }
TEST(Tensor, Slice) { TEST(Tensor, Slice) {
...@@ -160,7 +160,7 @@ TEST(Tensor, Slice) { ...@@ -160,7 +160,7 @@ TEST(Tensor, Slice) {
EXPECT_EQ(src_data_address + 3 * 4 * 1 * sizeof(int), slice_data_address); EXPECT_EQ(src_data_address + 3 * 4 * 1 * sizeof(int), slice_data_address);
} }
#ifdef __CUDACC__ #ifdef __CUDACC__
{ {
Tensor src_tensor; Tensor src_tensor;
src_tensor.mutable_data<double>(make_ddim({6, 9}), GPUPlace()); src_tensor.mutable_data<double>(make_ddim({6, 9}), GPUPlace());
...@@ -182,7 +182,7 @@ TEST(Tensor, Slice) { ...@@ -182,7 +182,7 @@ TEST(Tensor, Slice) {
EXPECT_EQ(slice_data_address, slice_mutable_data_address); EXPECT_EQ(slice_data_address, slice_mutable_data_address);
EXPECT_EQ(src_data_address + 9 * 2 * sizeof(double), slice_data_address); EXPECT_EQ(src_data_address + 9 * 2 * sizeof(double), slice_data_address);
} }
#endif #endif
} }
TEST(Tensor, CopyFrom) { TEST(Tensor, CopyFrom) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册