Allow caffe2 to detect if cuda lib has been linked, and also fix oss build error.

Summary: Closes https://github.com/caffe2/caffe2/pull/1114

Reviewed By: pietern

Differential Revision: D5686557

Pulled By: Yangqing

fbshipit-source-id: 6b7245ebbe4eeb025ce9d0fe8fda427a0c3d9770
This commit is contained in:
Yangqing Jia
2017-08-23 18:17:18 -07:00
committed by Facebook Github Bot
parent 16549ed92b
commit 93e12e75df
10 changed files with 64 additions and 4 deletions

5
caffe2/core/common.cc Normal file
View File

@ -0,0 +1,5 @@
#include "caffe2/core/common.h"
namespace caffe2 {
bool g_caffe2_has_cuda_linked = false;
} // namespace caffe2

View File

@ -208,6 +208,19 @@ class SkipIndices<> {
}
};
// A global variable to mark if Caffe2 has cuda linked to the current runtime.
// Do not directly use this variable, but instead use the HasCudaRuntime()
// function below.
extern bool g_caffe2_has_cuda_linked;
// HasCudaRuntime() tells the program whether the binary has Cuda runtime
// linked. This function should not be used in static initialization functions
// as the underlying boolean variable is going to be switched on when one
// loads libcaffe2_gpu.so.
inline bool HasCudaRuntime() {
return g_caffe2_has_cuda_linked;
}
} // namespace caffe2
#endif // CAFFE2_CORE_COMMON_H_

View File

@ -268,4 +268,18 @@ const char* curandGetErrorString(curandStatus_t error) {
// To suppress compiler warning.
return "Unrecognized curand error string";
}
// Turn on the flag g_caffe2_has_cuda_linked to true for HasCudaRuntime()
// function.
extern bool g_caffe2_has_cuda_linked;
namespace {
class CudaRuntimeFlagFlipper {
public:
CudaRuntimeFlagFlipper() {
g_caffe2_has_cuda_linked = true;
}
};
static CudaRuntimeFlagFlipper g_flipper;
} // namespace
} // namespace caffe2

View File

@ -90,6 +90,11 @@ int NumCudaDevices();
* cuda gpus present in the machine, or there are hardware configuration
* problems like an insufficient driver, this function will still return false,
* meaning that there is no usable GPU present.
*
* In the open source build, it is possible that Caffe2's GPU code is
* dynamically loaded, and as a result a library could be only linked to the
* CPU code, but want to test if cuda is later available or not. In this case,
* one should use HasCudaRuntime() from common.h.
*/
inline bool HasCudaGPU() { return NumCudaDevices() > 0; }

View File

@ -16,6 +16,10 @@ std::shared_ptr<void> shared_from_new(std::pair<void*, MemoryDeleter>&& p) {
}
}
TEST(CUDATest, HasCudaRuntime) {
EXPECT_TRUE(HasCudaRuntime());
}
TEST(CUDAContextTest, TestAllocDealloc) {
if (!HasCudaGPU()) return;
CUDAContext context(0);

View File

@ -233,7 +233,9 @@ TEST(NetTest, ChainingForDifferentDevices) {
}
}
)DOC";
checkChainingAndRun(spec, {{0, {0}}, {1, {1, 2}}, {3, {3}}});
if (HasCudaRuntime()) {
checkChainingAndRun(spec, {{0, {0}}, {1, {1, 2}}, {3, {3}}});
}
}
TEST(NetTest, ChainingForFork) {

View File

@ -90,9 +90,14 @@ TEST(OperatorTest, RegistryWorks) {
op_def.set_type("JustTest");
unique_ptr<OperatorBase> op = CreateOperator(op_def, &ws);
EXPECT_NE(nullptr, op.get());
op_def.mutable_device_option()->set_device_type(CUDA);
op = CreateOperator(op_def, &ws);
EXPECT_NE(nullptr, op.get());
// After introducing events, CUDA operator creation has to have CUDA compiled
// as it needs to instantiate an Event object with CUDAContext. Thus we will
// guard this test below.
if (HasCudaRuntime()) {
op_def.mutable_device_option()->set_device_type(CUDA);
op = CreateOperator(op_def, &ws);
EXPECT_NE(nullptr, op.get());
}
}
TEST(OperatorTest, RegistryWrongDevice) {

View File

@ -1,6 +1,11 @@
#ifndef CAFFE2_OPERATORS_SINUSOID_POSITION_ENCODING_OP_H_
#define CAFFE2_OPERATORS_SINUSOID_POSITION_ENCODING_OP_H_
#ifdef _MSC_VER
#define _USE_MATH_DEFINES
#endif // _MSC_VER
#include <cmath>
#include "caffe2/core/operator.h"
#include "Eigen/Core"

View File

@ -94,3 +94,9 @@ endif()
if (IOS)
add_definitions("-mfpu=neon-fp16")
endif()
# ---[ If we are buidling on ios, we should turn off deprecated-declarations
# due to protobuf.
if (IOS)
add_definitions("-Wno-deprecated-declarations")
endif()

View File

@ -35,6 +35,7 @@ cmake .. \
-DCMAKE_BUILD_TYPE=Release \
-DIOS_PLATFORM=${IOS_PLATFORM} \
-DUSE_CUDA=OFF \
-DUSE_OPENCV=OFF \
-DBUILD_TEST=OFF \
-DBUILD_BINARY=OFF \
-DCMAKE_C_FLAGS=-fembed-bitcode \