From 36e573893fc39d324ccf2f2962300da6da5898a2 Mon Sep 17 00:00:00 2001 From: Tianqi Chen Date: Fri, 2 Jun 2017 10:49:49 -0700 Subject: [PATCH] [BUILD/TEST] Formalize tests, project structure (#14) * [BUILD/TEST] Formalize tests, project structure * update cmake to reflect version --- .gitignore | 2 +- .travis.yml | 46 +++++++++++++++++++++++++ CMakeLists.txt | 10 ++++-- Makefile | 31 +++++++++++------ NEWS.md | 7 ++++ README.md | 7 ++++ {include => contrib}/dlpack/dlpackcpp.h | 14 ++++---- contrib/mock_c.c | 7 ++++ src/mock.cc => contrib/mock_main.cc | 1 + include/dlpack/dlpack.h | 16 ++++++--- tests/scripts/task_build.sh | 6 ++++ tests/scripts/task_lint.sh | 23 +++++++++++++ tests/travis/run_test.sh | 12 +++++++ tests/travis/setup.sh | 5 +++ 14 files changed, 161 insertions(+), 26 deletions(-) create mode 100644 .travis.yml create mode 100644 NEWS.md rename {include => contrib}/dlpack/dlpackcpp.h (86%) create mode 100644 contrib/mock_c.c rename src/mock.cc => contrib/mock_main.cc (79%) create mode 100755 tests/scripts/task_build.sh create mode 100755 tests/scripts/task_lint.sh create mode 100755 tests/travis/run_test.sh create mode 100644 tests/travis/setup.sh diff --git a/.gitignore b/.gitignore index 320cbaf..21c857e 100644 --- a/.gitignore +++ b/.gitignore @@ -29,4 +29,4 @@ *.app *~ build -mock +bin diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..b4b3fa2 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,46 @@ +sudo: false + +dist: trusty + +language: cpp + +os: + - linux + - osx + +osx_image: xcode8 + +env: + # code analysis + - TASK=all_test + +branches: + only: + - master + +matrix: + exclude: + - os: osx + env: TASK=lint + +# dependent apt packages +addons: + apt: + sources: + - ubuntu-toolchain-r-test + packages: + - doxygen + - wget + - graphviz + - unzip + +install: + - source tests/travis/setup.sh + +script: + - tests/travis/run_test.sh + +cache: + directories: + - ${HOME}/.cache/usr + diff --git a/CMakeLists.txt b/CMakeLists.txt index ddad531..a76f928 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,7 +8,7 @@ cmake_minimum_required(VERSION 3.2 FATAL_ERROR) # Set variables: # * PROJECT_NAME # * PROJECT_VERSION -project(dlpack VERSION 0.0.0 LANGUAGES C CXX) +project(dlpack VERSION 0.1.0 LANGUAGES C CXX) ##### # Change the default build type from Debug to Release, while still @@ -48,8 +48,12 @@ if(BUILD_DOCS) endif(BUILD_DOCS) set(DLPACK_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include) - +list(APPEND DLPACK_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/contrib) +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/bin) include_directories(${DLPACK_INCLUDE_DIR}) -add_executable(mock ${CMAKE_CURRENT_SOURCE_DIR}/src/mock.cc) + +set(DLPACK_MOCK_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/contrib/mock_main.cc + ${CMAKE_CURRENT_SOURCE_DIR}/contrib/mock_c.c) +add_executable(mock ${DLPACK_MOCK_SRCS}) diff --git a/Makefile b/Makefile index 8dc282b..2bf8ae7 100644 --- a/Makefile +++ b/Makefile @@ -1,24 +1,35 @@ -.PHONY: clean all test doc +.PHONY: clean all test doc lint -all: mock +all: bin/mock LDFLAGS = -CFLAGS = -std=c++11 -Wall -O3 -Iinclude +CFLAGS = -Wall -O3 -Iinclude -Icontrib +CXXFLAGS = -std=c++11 $(CFLAGS) -SRC = $(wildcard src/*.cc src/*/*.cc src/*/*/*.cc) -ALL_OBJ = $(patsubst src/%.cc, build/%.o, $(SRC)) +SRC = $(wildcard contrib/*.cc contrib/*.c) +ALL_CXX_OBJ = $(patsubst contrib/%.cc, build/%.o, $(SRC)) +ALL_C_OBJ = $(patsubst contrib/%.c, build/%.o, $(SRC)) +ALL_OBJ = $(ALL_CC_OBJ) $(ALL_CXX_OBJ) doc: doxygen docs/Doxyfile -build/%.o: src/%.cc +lint: + ./tests/scripts/task_lint.sh + +build/%.o: contrib/%.cc + @mkdir -p $(@D) + $(CXX) $(CXXFLAGS) -MM -MT build/$*.o $< >build/$*.d + $(CXX) -c $(CXXFLAGS) -c $< -o $@ + +build/%.o: contrib/%.c @mkdir -p $(@D) - $(CXX) $(CFLAGS) -MM -MT build/$*.o $< >build/$*.d - $(CXX) -c $(CFLAGS) -c $< -o $@ + $(CC) $(CFLAGS) -MM -MT build/$*.o $< >build/$*.d + $(CC) -c $(CFLAGS) -c $< -o $@ -mock: $(ALL_OBJ) +bin/mock: $(ALL_OBJ) @mkdir -p $(@D) - $(CXX) $(CFLAGS) -o $@ $(filter %.o %.a, $^) $(LDFLAGS) + $(CXX) $(CXXFLAGS) -o $@ $(filter %.o %.a, $^) $(LDFLAGS) clean: $(RM) -rf build */*/*/*~ */*.o */*/*.o */*/*/*.o */*.d */*/*.d */*/*/*.d diff --git a/NEWS.md b/NEWS.md new file mode 100644 index 0000000..9129d13 --- /dev/null +++ b/NEWS.md @@ -0,0 +1,7 @@ +DLPack Change Log +================= + +This file records the changes in DLPack in reverse chronological order. + +## v0.1 +- Finalize DLTensor structure \ No newline at end of file diff --git a/README.md b/README.md index a36e286..e5d2cd0 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ # DLPack +[![Build Status](https://travis-ci.org/dmlc/dlpack.svg?branch=master)](https://travis-ci.org/dmlc/dlpack) + RFC for common tensor and operator guidelines in deep learning systems. Started by [this issue](https://github.com/dmlc/mxnet/issues/4735) @@ -13,6 +15,11 @@ This enables: We do not intend to implement of Tensor and Ops, but instead use this as common interface to reuse tensor and ops across frameworks. +## Project Structure +There are two major components so far +- include: stablized headers +- contrib: in progress unstable libraries + ## Credit The initial interface so far base on discussions between @soumith @piiswrong @Yangqing @naibaf7 @bhack @edgarriba @tqchen diff --git a/include/dlpack/dlpackcpp.h b/contrib/dlpack/dlpackcpp.h similarity index 86% rename from include/dlpack/dlpackcpp.h rename to contrib/dlpack/dlpackcpp.h index c25f2de..d1f56cc 100644 --- a/include/dlpack/dlpackcpp.h +++ b/contrib/dlpack/dlpackcpp.h @@ -3,8 +3,10 @@ * \file dlpackcpp.h * \brief Example C++ wrapper of DLPack */ -#ifndef DLPACKCPP_H_ -#define DLPACKCPP_H_ +#ifndef DLPACK_DLPACKCPP_H_ +#define DLPACK_DLPACKCPP_H_ + +#include #include // for int64_t etc #include // for free() @@ -13,8 +15,6 @@ #include #include -#include "./dlpack.h" - namespace dlpack { // Example container wrapping of DLTensor. @@ -47,8 +47,8 @@ class DLTContainer { shape_ = shape; int64_t sz = std::accumulate(std::begin(shape), std::end(shape), int64_t(1), std::multiplies()); - // Note: this won't work on OSX. - handle_.data = aligned_alloc(256, sz); + int ret = posix_memalign(&handle_.data, 256, sz); + if (ret != 0) throw std::bad_alloc(); handle_.shape = &shape_[0]; handle_.ndim = static_cast(shape.size()); } @@ -62,4 +62,4 @@ class DLTContainer { }; } // namespace dlpack -#endif // DLPACKCPP_H_ +#endif // DLPACK_DLPACKCPP_H_ diff --git a/contrib/mock_c.c b/contrib/mock_c.c new file mode 100644 index 0000000..57c5b71 --- /dev/null +++ b/contrib/mock_c.c @@ -0,0 +1,7 @@ +// Copyright by contributors +// This file is used to make sure the package is C compatible +#include + +int GetNDim(DLTensor *t) { + return t->ndim; +} diff --git a/src/mock.cc b/contrib/mock_main.cc similarity index 79% rename from src/mock.cc rename to contrib/mock_main.cc index 8418a0a..03798ce 100644 --- a/src/mock.cc +++ b/contrib/mock_main.cc @@ -1,3 +1,4 @@ +// Copyright by contributors #include #include diff --git a/include/dlpack/dlpack.h b/include/dlpack/dlpack.h index 4f897d5..3a6680e 100644 --- a/include/dlpack/dlpack.h +++ b/include/dlpack/dlpack.h @@ -3,8 +3,8 @@ * \file dlpack.h * \brief The common header of DLPack. */ -#ifndef DLPACK_H_ -#define DLPACK_H_ +#ifndef DLPACK_DLPACK_H_ +#define DLPACK_DLPACK_H_ #ifdef __cplusplus #define DLPACK_EXTERN_C extern "C" @@ -12,6 +12,9 @@ #define DLPACK_EXTERN_C #endif +/*! \brief The current version of dlpack */ +#define DLPACK_VERSION 010 + /*! \brief DLPACK_DLL prefix for windows */ #ifdef _WIN32 #ifdef DLPACK_EXPORTS @@ -26,7 +29,9 @@ #include #include -DLPACK_EXTERN_C { +#ifdef __cplusplus +extern "C" { +#endif /*! * \brief The device type in DLContext. */ @@ -106,6 +111,7 @@ typedef struct { /*! \brief The offset in bytes to the beginning pointer to data */ uint64_t byte_offset; } DLTensor; - +#ifdef __cplusplus } // DLPACK_EXTERN_C -#endif // DLPACK_H_ +#endif +#endif // DLPACK_DLPACK_H_ diff --git a/tests/scripts/task_build.sh b/tests/scripts/task_build.sh new file mode 100755 index 0000000..0014c69 --- /dev/null +++ b/tests/scripts/task_build.sh @@ -0,0 +1,6 @@ +#!/bin/bash +make || exit -1 +mkdir -p build +cd build +cmake .. || exit -1 +make || exit -1 diff --git a/tests/scripts/task_lint.sh b/tests/scripts/task_lint.sh new file mode 100755 index 0000000..190836b --- /dev/null +++ b/tests/scripts/task_lint.sh @@ -0,0 +1,23 @@ +#!/bin/bash +mkdir -p bin + +if [ ! -f bin/lint.py ]; then + echo "Grab linter ..." + wget https://raw.githubusercontent.com/dmlc/dmlc-core/master/scripts/lint.py + mv lint.py bin/lint.py +fi + +echo "Check codestyle of c++ code..." +python bin/lint.py dlpack cpp include contrib + +echo "Check doxygen generation..." +make doc 2>log.txt +(cat log.txt| grep -v ENABLE_PREPROCESSING |grep -v "unsupported tag") > logclean.txt +echo "---------Error Log----------" +cat logclean.txt +echo "----------------------------" +(cat logclean.txt|grep warning) && exit -1 +(cat logclean.txt|grep error) && exit -1 +rm logclean.txt +rm log.txt +echo "All checks passed..." diff --git a/tests/travis/run_test.sh b/tests/travis/run_test.sh new file mode 100755 index 0000000..38f3fd3 --- /dev/null +++ b/tests/travis/run_test.sh @@ -0,0 +1,12 @@ +#!/bin/bash +if [ ${TASK} == "lint" ] || [ ${TASK} == "all_test" ]; then + if [ ! ${TRAVIS_OS_NAME} == "osx" ]; then + ./tests/scripts/task_lint.sh || exit -1 + fi +fi + +if [ ${TASK} == "build" ] || [ ${TASK} == "all_test" ]; then + ./tests/scripts/task_build.sh || exit -1 +fi + +echo "All travis test passed.." diff --git a/tests/travis/setup.sh b/tests/travis/setup.sh new file mode 100644 index 0000000..634c488 --- /dev/null +++ b/tests/travis/setup.sh @@ -0,0 +1,5 @@ +if [ ${TASK} == "lint" ] || [ ${TASK} == "all_test" ]; then + if [ ! ${TRAVIS_OS_NAME} == "osx" ]; then + pip install --user cpplint + fi +fi \ No newline at end of file