Skip to content

Commit

Permalink
Merge pull request #2118 from gangliao/new_tensor
Browse files Browse the repository at this point in the history
Add cmake for majel
  • Loading branch information
wangkuiyi authored May 12, 2017
2 parents 4764303 + d444251 commit f9c21f5
Show file tree
Hide file tree
Showing 11 changed files with 117 additions and 88 deletions.
9 changes: 0 additions & 9 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,6 @@ RUN git clone https://github.com/woboq/woboq_codebrowser /woboq && \
-DCMAKE_BUILD_TYPE=Release . \
make)

# Install gtest.
#
# NOTE: This is added for quick hack of the development work of
# majel-in-paddle.
RUN git clone https://github.com/google/googletest /gtest && \
cd /gtest && \
git checkout -b release-1.8.0 && \
cmake . && make install

# Configure OpenSSH server. c.f. https://docs.docker.com/engine/examples/running_ssh_service
RUN mkdir /var/run/sshd
RUN echo 'root:root' | chpasswd
Expand Down
10 changes: 0 additions & 10 deletions majel/Makefile

This file was deleted.

61 changes: 0 additions & 61 deletions majel/place.cu

This file was deleted.

8 changes: 8 additions & 0 deletions paddle/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ add_subdirectory(pserver)
add_subdirectory(trainer)
add_subdirectory(scripts)

find_package(boost QUIET)

if(Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
add_subdirectory(majel)
endif()

if(WITH_C_API)
add_subdirectory(capi)
endif()
Expand Down
2 changes: 2 additions & 0 deletions paddle/majel/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
build
third-party
34 changes: 34 additions & 0 deletions paddle/majel/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
cmake_minimum_required(VERSION 3.0)

if(GTEST_INCLUDE_DIR AND GTEST_LIBRARIES)
message("-- Found gtest (include: ${GTEST_INCLUDE_DIR}, library: ${GTEST_LIBRARIES})")
else()
# find #include <majel/xx.h>
get_filename_component(PARENT_DIR ${CMAKE_CURRENT_SOURCE_DIR} DIRECTORY)
include_directories(${PARENT_DIR})

# find cmake directory modules
get_filename_component(PARENT_DIR ${PARENT_DIR} DIRECTORY)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${PARENT_DIR}/cmake")

# enable c++11
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

# enable gtest
set(THIRD_PARTY_PATH ${CMAKE_CURRENT_SOURCE_DIR}/third_party)
set(WITH_TESTING ON)
include(external/gtest)
endif()

########################### Build Majel #############################
set(MAJEL_CXX_FILES place.cpp)
set(MAJEL_CUDA_FILES "")

if(CUDA_FOUND)
cuda_add_library(majel ${MAJEL_CUDA_FILES} ${MAJEL_CXX_FILES})
else()
add_library(majel ${MAJEL_CXX_FILES})
endif()
#####################################################################

add_subdirectory(test)
49 changes: 49 additions & 0 deletions paddle/majel/place.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include <majel/place.h>

namespace majel {

namespace detail {

class PlacePrinter : public boost::static_visitor<> {
private:
std::ostream& os_;

public:
PlacePrinter(std::ostream& os) : os_(os) {}

void operator()(const CpuPlace&) { os_ << "CpuPlace"; }

void operator()(const GpuPlace& p) { os_ << "GpuPlace(" << p.device << ")"; }
};

} // namespace majel

static Place the_default_place;

void set_place(const Place& place) { the_default_place = place; }

const Place& get_place() { return the_default_place; }

const GpuPlace default_gpu() { return GpuPlace(0); }

const CpuPlace default_cpu() { return CpuPlace(); }

bool is_gpu_place(const Place& p) {
return boost::apply_visitor(IsGpuPlace(), p);
}

bool is_cpu_place(const Place& p) {
return !boost::apply_visitor(IsGpuPlace(), p);
}

bool places_are_same_class(const Place& p1, const Place& p2) {
return is_gpu_place(p1) == is_gpu_place(p2);
}

std::ostream& operator<<(std::ostream& os, const majel::Place& p) {
majel::detail::PlacePrinter printer(os);
boost::apply_visitor(printer, p);
return os;
}

} // namespace majel
File renamed without changes.
10 changes: 10 additions & 0 deletions paddle/majel/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
file(GLOB_RECURSE ALL_TEST_FILES RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "*.cpp" "*.cc")

add_executable(majel_tests ${ALL_TEST_FILES})
add_dependencies(majel_tests majel)
target_link_libraries(majel_tests
${Boost_LIBRARIES}
${GTEST_LIBRARIES}
majel
)
add_test(majel_tests majel_tests)
16 changes: 8 additions & 8 deletions majel/place_test.cu → paddle/majel/test/place_test.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "gtest/gtest.h"
#include "majel/place.h"
#include <sstream>
#include "gtest/gtest.h"

TEST(Place, Equality) {
majel::CpuPlace cpu;
Expand All @@ -18,12 +18,12 @@ TEST(Place, Equality) {
}

TEST(Place, Default) {
EXPECT_TRUE(majel::is_gpu_place( majel::get_place()));
EXPECT_TRUE(majel::is_gpu_place( majel::default_gpu()));
EXPECT_TRUE(majel::is_cpu_place( majel::default_cpu()));
EXPECT_TRUE(majel::is_gpu_place(majel::get_place()));
EXPECT_TRUE(majel::is_gpu_place(majel::default_gpu()));
EXPECT_TRUE(majel::is_cpu_place(majel::default_cpu()));

majel::set_place(majel::CpuPlace());
EXPECT_TRUE(majel::is_cpu_place( majel::get_place()));
EXPECT_TRUE(majel::is_cpu_place(majel::get_place()));
}

TEST(Place, Print) {
Expand All @@ -33,8 +33,8 @@ TEST(Place, Print) {
EXPECT_EQ("GpuPlace(1)", ss.str());
}
{
std::stringstream ss;
ss << majel::CpuPlace();
EXPECT_EQ("CpuPlace", ss.str());
std::stringstream ss;
ss << majel::CpuPlace();
EXPECT_EQ("CpuPlace", ss.str());
}
}
6 changes: 6 additions & 0 deletions paddle/majel/test/test_framework.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "gtest/gtest.h"

int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

0 comments on commit f9c21f5

Please sign in to comment.