diff --git a/Makefile.am b/Makefile.am index deca6e61df..6f3be4c200 100644 --- a/Makefile.am +++ b/Makefile.am @@ -4,9 +4,12 @@ AUTOMAKE_OPTIONS = foreign subdir-objects AM_CPPFLAGS = -I$(top_srcdir)/src MSHADOW_FLAGS = -DMSHADOW_USE_CUDA=0 -DMSHADOW_USE_CBLAS=1 -DMSHADOW_USE_MKL=0 +DEFAULT_FLAGS = -Wall -pthread -fPIC -std=c++11 -Wno-unknown-pragmas \ + $(MSHADOW_FLAGS) -DCPU_ONLY=1 -funroll-loops -DTHREADED -AC_CXXFLAGS = -O3 -Wall -pthread -fPIC -std=c++11 -Wno-unknown-pragmas \ - $(MSHADOW_FLAGS) -DCPU_ONLY=1 -funroll-loops +CFLAGS = -O2 $(DEBUG) +CXXFLAGS = -O2 $(DEBUG) +AC_CXXFLAGS = -O2 $(DEBUG) INCLUDES = -I$(top_srcdir)/include @@ -78,21 +81,38 @@ SINGA_HDRS := include/singa.h \ include/communication/msg.h \ include/communication/socket.h +GTEST_SRCS := include/gtest/gtest-all.cc +GTEST_HRDS := include/gtest/gtest.h +TEST_SRCS := include/gtest/gtest_main.cc \ + src/test/test_cluster.cc \ + src/test/test_common.cc \ + src/test/test_msg.cc \ + src/test/test_neuralnet.cc \ + src/test/test_paramslicer.cc \ + src/test/test_shard.cc -lib_LTLIBRARIES = libsinga.la +#EXTRA_PROGRAMS = $(PROGS) +EXTRA_PROGRAMS = singatest +#EXTRA_LTLIBRARIES = $(LTLIBS) +EXTRA_LTLIBRARIES = libgtest.la + +lib_LTLIBRARIES = libsinga.la $(LTLIBS) +bin_PROGRAMS = singa singatool $(PROGS) + +#lib_LTLIBRARIES = libsinga.la libsinga_la_SOURCES = $(PROTO_HDRS) $(PROTO_SRCS) $(SINGA_HDRS) $(SINGA_SRCS) -libsinga_la_CXXFLAGS = -O3 -Wall -pthread -fPIC -std=c++11 -Wno-unknown-pragmas \ - $(MSHADOW_FLAGS) -DCPU_ONLY=1 -funroll-loops -gdwarf-2 -msse3 \ - -gstrict-dwarf -Woverloaded-virtual -DTHREADED -fpermissive +libsinga_la_CXXFLAGS = $(DEFAULT_FLAGS) -gdwarf-2 -msse3 \ + -gstrict-dwarf -Woverloaded-virtual -fpermissive if LMDB libsinga_la_CXXFLAGS += -DUSE_LMDB endif libsinga_la_LDFLAGS = -I./include -bin_PROGRAMS = singa + + +#bin_PROGRAMS = singa singa_SOURCES = src/main.cc -singa_CXXFLAGS = -O3 -Wall -pthread -fPIC -std=c++11 -MMD -Wno-unknown-pragmas \ - $(MSHADOW_FLAGS) -DCPU_ONLY=1 -funroll-loops -DTHREADED +singa_CXXFLAGS = $(DEFAULT_FLAGS) -MMD singa_LDFLAGS = -I./include \ -lsinga \ -lglog \ @@ -106,9 +126,9 @@ if LMDB singa_LDFLAGS += -llmdb endif -bin_PROGRAMS += singatool +#bin_PROGRAMS += singatool singatool_SOURCES = src/utils/tool.cc -singatool_CXXFLAGS = -O3 -Wall -pthread -fPIC -std=c++11 -MMD -Wno-unknown-pragmas \ +singatool_CXXFLAGS = -Wall -pthread -fPIC -std=c++11 -MMD -Wno-unknown-pragmas \ -funroll-loops -DTHREADED singatool_LDFLAGS = -I./include \ -lsinga \ @@ -116,6 +136,33 @@ singatool_LDFLAGS = -I./include \ -lprotobuf \ -lzookeeper_mt +#lib_LTLIBRARIES += libgtest.la +libgtest_la_SOURCES = $(GTEST_HDRS) $(GTEST_SRCS) +libgtest_la_CXXFLAGS = $(DEFAULT_FLAGS) -gdwarf-2 -msse3 \ + -gstrict-dwarf -Woverloaded-virtual -fpermissive +if LMDB +libgtest_la_CXXFLAGS += -DUSE_LMDB +endif +libgtest_la_LDFLAGS = -I./include + +#bin_PROGRAMS += singatest + +singatest_SOURCES = $(GTEST_HDRS) $(TEST_SRCS) +singatest_CXXFLAGS = $(DEFAULT_FLAGS) +singatest_LDFLAGS = -I./include \ + -lsinga \ + -lglog \ + -lprotobuf \ + -lrt \ + -lopenblas \ + -lzmq \ + -lczmq \ + -lzookeeper_mt \ + -lgtest +if LMDB +singatest_LDFLAGS += -llmdb +endif + clean-local: rm -rf $(PROTO_SRCS) $(PROTO_HDRS) rm -rf $(PROTO_PYS) diff --git a/configure.ac b/configure.ac index 569c406fd6..fa54546b86 100644 --- a/configure.ac +++ b/configure.ac @@ -44,6 +44,31 @@ if test x"$enable_lmdb" = x"yes"; then AC_DEFINE(LMDB, 1, [Enable Option layer]) fi +AC_ARG_ENABLE(test, + AS_HELP_STRING([--enable-test],[enable singa test]), + [enable_test=yes],[enable_test=no]) +AM_CONDITIONAL(SINGATEST, test "$enable_test" = yes) +if test x"$enable_test" != x"no"; then + PROGS='singatest' + LTLIBS='libgtest.la' +else + PROGS='' + LTLIBS='' +fi +AC_SUBST([PROGS]) +AC_SUBST([LTLIBS]) + +AC_ARG_ENABLE(debug, + AS_HELP_STRING([--enable-debug],[enable debug mode]), + [enable_debug=yes],[enable_debug=no]) +AM_CONDITIONAL(DEBUG, test "$enable_debug" = yes) +if test x"$enable_debug" != x"no"; then + DEBUG='-g' +else + DEBUG='' +fi +AC_SUBST([DEBUG]) + #AC_CHECK_LIB([opencv_imgproc], [main], [], [ # AC_MSG_ERROR([unable to find opencv_imgproc lib]) # ]) diff --git a/src/test/test_common.cc b/src/test/test_common.cc new file mode 100644 index 0000000000..b84e860d41 --- /dev/null +++ b/src/test/test_common.cc @@ -0,0 +1,113 @@ +#include "gtest/gtest.h" +#include "utils/common.h" +#include +#include +#include + +using std::string; +using std::vector; +using namespace singa; + +TEST(CommonTest, TestIntVecToString) { + + vector num_vec {2, 3, 5, 7, 11}; + string str = "(2, 3, 5, 7, 11, )"; + ASSERT_EQ(str, IntVecToString(num_vec)); +} + +TEST(CommonTest, TestStringPrintf) { + const char* str_a = "abc"; + const char* str_b = "edfgh"; + const char* str_c = " !@#"; + const char* str_d = "1"; + const char* str_e = "2"; + const char* str_f = "3"; + + string fmt_a = "%s%s%s"; + string fmt_b = "[%s] [%s] [%s] "; + + string str_d_a = "abcedfgh !@#"; + string str_d_b = "[1] [2] [3] "; + + ASSERT_EQ(str_d_a, StringPrintf(fmt_a, str_a, str_b, str_c)); + ASSERT_EQ(str_d_b, StringPrintf(fmt_b, str_d, str_e, str_f)); +} + +TEST(CommonTest, TestGCDLCM) { + int a = 2, b = 5, c = 10, d = 15; + + ASSERT_EQ(1, gcd(a, b)); + ASSERT_EQ(5, gcd(c, d)); + ASSERT_EQ(10, LeastCommonMultiple(b, c)); + ASSERT_EQ(30, LeastCommonMultiple(c, d)); +} + +TEST(CommonTest, TestMetric) { + string str, msg; + Metric metric; + metric.Add("a", 0.5); + metric.Add("b", 0.5); + metric.Add("a", 1.5); + str = metric.ToLogString(); + msg = metric.ToString(); + metric.Reset(); + metric.ParseFrom(msg); + ASSERT_EQ(str, metric.ToLogString()); +} + +TEST(CommonTest, TestSlice) { + vector> slices_0; + vector sizes {14112, 96, 256, 884736, 384}; + ASSERT_EQ(slices_0, Slice(0, sizes)); + + vector> slices_1 { + { 14112 }, + { 96 }, + { 256 }, + { 884736 }, + { 384 }, + }; + + vector> slices_2 { + { 14112 }, + { 96 }, + { 256 }, + { 435328, 449408 }, + { 384 }, + }; + + vector> slices_4 { + { 14112 }, + { 96 }, + { 256 }, + { 210432,224896,224896,224512 }, + { 384 }, + }; + + vector> slices_8 { + { 14112 }, + { 96 }, + { 256 }, + { 97984,112448,112448,112448,112448,112448,112448,112064 }, + { 384 }, + }; + + ASSERT_EQ(slices_1, Slice(1, sizes)); + ASSERT_EQ(slices_2, Slice(2, sizes)); + ASSERT_EQ(slices_4, Slice(4, sizes)); + ASSERT_EQ(slices_8, Slice(8, sizes)); +} + +TEST(CommonTest, TestPartitionSlices) { + vector slices { + 97984,112448,112448,112448,112448,112448,112448,112064 + }; + vector box_1 { 0, 0, 0, 0, 0, 0, 0, 0 }; + vector box_2 { 0, 0, 0, 0, 1, 1, 1, 1 }; + vector box_4 { 0, 0, 1, 1, 2, 2, 3, 3 }; + vector box_8 { 0, 1, 2, 3, 4, 5, 6, 7 }; + ASSERT_EQ(box_1, PartitionSlices(1, slices)); + ASSERT_EQ(box_2, PartitionSlices(2, slices)); + ASSERT_EQ(box_4, PartitionSlices(4, slices)); + ASSERT_EQ(box_8, PartitionSlices(8, slices)); +}