Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compile a binary to run all tests at once. #179

Merged
merged 3 commits into from
Mar 17, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ HXX_SRCS := $(shell find include/$(PROJECT) ! -name "*.hpp")
# CU_SRCS are the cuda source files
CU_SRCS := $(shell find src/$(PROJECT) -name "*.cu")
# TEST_SRCS are the test source files
TEST_MAIN_SRC := src/$(PROJECT)/test/test_caffe_main.cpp
TEST_SRCS := $(shell find src/$(PROJECT) -name "test_*.cpp")
TEST_SRCS := $(filter-out $(TEST_MAIN_SRC), $(TEST_SRCS))
GTEST_SRC := src/gtest/gtest-all.cpp
# TEST_HDRS are the test header files
TEST_HDRS := $(shell find src/$(PROJECT) -name "test_*.hpp")
Expand Down Expand Up @@ -74,6 +76,7 @@ GTEST_OBJ := $(addprefix $(BUILD_DIR)/, ${GTEST_SRC:.cpp=.o})
TOOL_BINS := ${TOOL_OBJS:.o=.bin}
EXAMPLE_BINS := ${EXAMPLE_OBJS:.o=.bin}
TEST_BINS := ${TEST_OBJS:.o=.testbin}
TEST_ALL_BIN := $(BUILD_DIR)/src/$(PROJECT)/test/test_all.testbin

##############################
# Derive include and lib directories
Expand Down Expand Up @@ -133,7 +136,7 @@ $(LINT_REPORT): $(NONGEN_CXX_SRCS)
echo "Found 1 or more lint errors; see log at $(FAILED_LINT_REPORT)"; \
exit 1)

test: init $(TEST_BINS)
test: init $(TEST_BINS) $(TEST_ALL_BIN)

tools: init $(TOOL_BINS)

Expand Down Expand Up @@ -163,11 +166,14 @@ $(STATIC_NAME): init $(PROTO_OBJS) $(OBJS)
ar rcs $(STATIC_NAME) $(PROTO_OBJS) $(OBJS)
@echo

runtest: test
for testbin in $(TEST_BINS); do $$testbin $(TEST_GPUID); done
runtest: $(TEST_ALL_BIN)
$(TEST_ALL_BIN)

$(TEST_BINS): %.testbin : %.o $(GTEST_OBJ) $(STATIC_NAME) $(TEST_HDRS)
$(CXX) $< $(GTEST_OBJ) $(STATIC_NAME) -o $@ $(CXXFLAGS) $(LDFLAGS) $(WARNINGS)
$(CXX) $(TEST_MAIN_SRC) $< $(GTEST_OBJ) $(STATIC_NAME) -o $@ $(CXXFLAGS) $(LDFLAGS) $(WARNINGS)

$(TEST_ALL_BIN): $(TEST_OBJS)
$(CXX) $(TEST_MAIN_SRC) $(TEST_OBJS) $(GTEST_OBJ) $(STATIC_NAME) -o $(TEST_ALL_BIN) $(CXXFLAGS) $(LDFLAGS) $(WARNINGS)

$(TOOL_BINS): %.bin : %.o $(STATIC_NAME)
$(CXX) $< $(STATIC_NAME) -o $@ $(CXXFLAGS) $(LDFLAGS) $(WARNINGS)
Expand Down
12 changes: 12 additions & 0 deletions docs/development.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@ We'd appreciate your contribution to the documentation effort!

**Testing**: run `make runtest` to check the project tests. New code requires new tests. Pull requests that fail tests will not be accepted.

The `googletest` framework we use provides many additional options, which you can access by running the test binaries directly. One of the more useful options is `--gtest_filter`, which allows you to filter tests by name:

# run all tests with CPU in the name
build/src/caffe/test/test_all.testbin --gtest_filter='*CPU*'

# run all tests without GPU in the name (note the leading minus sign)
build/src/caffe/test/test_all.testbin --gtest_filter=-'*GPU*'

To get a list of all options `googletest` provides, simply pass the `--help` flag:

build/src/caffe/test/test_all.testbin --help

**Style**

- Follow [Google C++ style](http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml) and [Google python style](http://google-styleguide.googlecode.com/svn/trunk/pyguide.html) + [PEP 8](http://legacy.python.org/dev/peps/pep-0008/).
Expand Down
32 changes: 32 additions & 0 deletions src/caffe/test/test_caffe_main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2013 Yangqing Jia

// The main caffe test code. Your test cpp code should include this hpp
// to allow a main function to be compiled into the binary.

#include "test_caffe_main.hpp"

namespace caffe {
cudaDeviceProp CAFFE_TEST_CUDA_PROP;
}

using caffe::CAFFE_TEST_CUDA_PROP;

int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
::google::InitGoogleLogging(argv[0]);
// Before starting testing, let's first print out a few cuda defice info.
int device;
cudaGetDeviceCount(&device);
cout << "Cuda number of devices: " << device << endl;
if (argc > 1) {
// Use the given device
device = atoi(argv[1]);
cudaSetDevice(device);
cout << "Setting to use device " << device << endl;
}
cudaGetDevice(&device);
cout << "Current device id: " << device << endl;
cudaGetDeviceProperties(&CAFFE_TEST_CUDA_PROP, device);
// invoke the test.
return RUN_ALL_TESTS();
}
28 changes: 1 addition & 27 deletions src/caffe/test/test_caffe_main.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,6 @@
using std::cout;
using std::endl;

namespace caffe {

cudaDeviceProp CAFFE_TEST_CUDA_PROP;

} // namespace caffe

using caffe::CAFFE_TEST_CUDA_PROP;

int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
::google::InitGoogleLogging(argv[0]);
// Before starting testing, let's first print out a few cuda defice info.
int device;
cudaGetDeviceCount(&device);
cout << "Cuda number of devices: " << device << endl;
if (argc > 1) {
// Use the given device
device = atoi(argv[1]);
cudaSetDevice(device);
cout << "Setting to use device " << device << endl;
}
cudaGetDevice(&device);
cout << "Current device id: " << device << endl;
cudaGetDeviceProperties(&CAFFE_TEST_CUDA_PROP, device);
// invoke the test.
return RUN_ALL_TESTS();
}
int main(int argc, char** argv);

#endif // CAFFE_TEST_TEST_CAFFE_MAIN_HPP_
1 change: 1 addition & 0 deletions src/caffe/test/test_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ TEST_F(CommonTest, TestVslStream) {
}

TEST_F(CommonTest, TestBrewMode) {
Caffe::set_mode(Caffe::CPU);
EXPECT_EQ(Caffe::mode(), Caffe::CPU);
Caffe::set_mode(Caffe::GPU);
EXPECT_EQ(Caffe::mode(), Caffe::GPU);
Expand Down