From 29f6670f11c4ac505cad0f779430dea01358c025 Mon Sep 17 00:00:00 2001 From: Tea Date: Sat, 7 Nov 2015 14:09:59 +0800 Subject: [PATCH] Replace unistd functions with cross platform counterparts --- Makefile | 2 +- cmake/Dependencies.cmake | 2 +- include/caffe/util/io.hpp | 30 +++++++++++------------------- src/caffe/test/test_benchmark.cpp | 6 +++--- 4 files changed, 16 insertions(+), 24 deletions(-) diff --git a/Makefile b/Makefile index 4a1d41d5a82..f5dbf432f75 100644 --- a/Makefile +++ b/Makefile @@ -170,7 +170,7 @@ ifneq ($(CPU_ONLY), 1) LIBRARIES := cudart cublas curand endif -LIBRARIES += glog gflags protobuf boost_system m hdf5_hl hdf5 +LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_hl hdf5 # handle IO dependencies USE_LEVELDB ?= 1 diff --git a/cmake/Dependencies.cmake b/cmake/Dependencies.cmake index 5651e2b086d..51a803c1a73 100644 --- a/cmake/Dependencies.cmake +++ b/cmake/Dependencies.cmake @@ -2,7 +2,7 @@ set(Caffe_LINKER_LIBS "") # ---[ Boost -find_package(Boost 1.46 REQUIRED COMPONENTS system thread) +find_package(Boost 1.46 REQUIRED COMPONENTS system thread filesystem) include_directories(SYSTEM ${Boost_INCLUDE_DIR}) list(APPEND Caffe_LINKER_LIBS ${Boost_LIBRARIES}) diff --git a/include/caffe/util/io.hpp b/include/caffe/util/io.hpp index d6cfa442fca..6b7332548b0 100644 --- a/include/caffe/util/io.hpp +++ b/include/caffe/util/io.hpp @@ -1,7 +1,7 @@ #ifndef CAFFE_UTIL_IO_H_ #define CAFFE_UTIL_IO_H_ -#include +#include #include #include "google/protobuf/message.h" @@ -12,31 +12,23 @@ namespace caffe { using ::google::protobuf::Message; +using ::boost::filesystem::path; inline void MakeTempFilename(string* temp_filename) { temp_filename->clear(); - *temp_filename = "/tmp/caffe_test.XXXXXX"; - char* temp_filename_cstr = new char[temp_filename->size() + 1]; - // NOLINT_NEXT_LINE(runtime/printf) - strcpy(temp_filename_cstr, temp_filename->c_str()); - int fd = mkstemp(temp_filename_cstr); - CHECK_GE(fd, 0) << "Failed to open a temporary file at: " << *temp_filename; - close(fd); - *temp_filename = temp_filename_cstr; - delete[] temp_filename_cstr; + const path& model = boost::filesystem::temp_directory_path() + /"caffe_test.%%%%%%"; + *temp_filename = boost::filesystem::unique_path(model).string(); } inline void MakeTempDir(string* temp_dirname) { temp_dirname->clear(); - *temp_dirname = "/tmp/caffe_test.XXXXXX"; - char* temp_dirname_cstr = new char[temp_dirname->size() + 1]; - // NOLINT_NEXT_LINE(runtime/printf) - strcpy(temp_dirname_cstr, temp_dirname->c_str()); - char* mkdtemp_result = mkdtemp(temp_dirname_cstr); - CHECK(mkdtemp_result != NULL) - << "Failed to create a temporary directory at: " << *temp_dirname; - *temp_dirname = temp_dirname_cstr; - delete[] temp_dirname_cstr; + const path& model = boost::filesystem::temp_directory_path() + /"caffe_test.%%%%%%"; + const path& dir = boost::filesystem::unique_path(model).string(); + bool directoryCreated = boost::filesystem::create_directory(dir); + CHECK(directoryCreated); + *temp_dirname = dir.string(); } bool ReadProtoFromTextFile(const char* filename, Message* proto); diff --git a/src/caffe/test/test_benchmark.cpp b/src/caffe/test/test_benchmark.cpp index 43aaa639b3c..b03fdf69a8a 100644 --- a/src/caffe/test/test_benchmark.cpp +++ b/src/caffe/test/test_benchmark.cpp @@ -1,4 +1,4 @@ -#include // for usleep +#include #include "gtest/gtest.h" @@ -64,7 +64,7 @@ TYPED_TEST(BenchmarkTest, TestTimerMilliSeconds) { EXPECT_FALSE(timer.running()); EXPECT_FALSE(timer.has_run_at_least_once()); timer.Start(); - usleep(300 * 1000); + boost::this_thread::sleep(boost::posix_time::milliseconds(300)); EXPECT_GE(timer.MilliSeconds(), 300 - kMillisecondsThreshold); EXPECT_LE(timer.MilliSeconds(), 300 + kMillisecondsThreshold); EXPECT_TRUE(timer.initted()); @@ -79,7 +79,7 @@ TYPED_TEST(BenchmarkTest, TestTimerSeconds) { EXPECT_FALSE(timer.running()); EXPECT_FALSE(timer.has_run_at_least_once()); timer.Start(); - usleep(300 * 1000); + boost::this_thread::sleep(boost::posix_time::milliseconds(300)); EXPECT_GE(timer.Seconds(), 0.3 - kMillisecondsThreshold / 1000.); EXPECT_LE(timer.Seconds(), 0.3 + kMillisecondsThreshold / 1000.); EXPECT_TRUE(timer.initted());