Skip to content

Commit 49d70c4

Browse files
authored
Merge pull request #2091 from wangkuiyi/majel-place
Add Majel's Place concept to Paddle
2 parents 49b7785 + 02990b8 commit 49d70c4

File tree

5 files changed

+171
-1
lines changed

5 files changed

+171
-1
lines changed

Dockerfile

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ RUN apt-get update && \
2929
curl sed grep graphviz libjpeg-dev zlib1g-dev \
3030
python-numpy python-matplotlib gcc g++ \
3131
automake locales clang-format-3.8 swig doxygen cmake \
32-
liblapack-dev liblapacke-dev \
32+
liblapack-dev liblapacke-dev libboost-dev \
3333
clang-3.8 llvm-3.8 libclang-3.8-dev && \
3434
apt-get clean -y
3535

@@ -61,6 +61,15 @@ RUN git clone https://github.com/woboq/woboq_codebrowser /woboq && \
6161
-DCMAKE_BUILD_TYPE=Release . \
6262
make)
6363

64+
# Install gtest.
65+
#
66+
# NOTE: This is added for quick hack of the development work of
67+
# majel-in-paddle.
68+
RUN git clone https://github.com/google/googletest /gtest && \
69+
cd /gtest && \
70+
git checkout -b release-1.8.0 && \
71+
cmake . && make install
72+
6473
# Configure OpenSSH server. c.f. https://docs.docker.com/engine/examples/running_ssh_service
6574
RUN mkdir /var/run/sshd
6675
RUN echo 'root:root' | chpasswd

majel/Makefile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
CCFLAGS = -std=c++11 -I/work/paddle
2+
CC = nvcc
3+
4+
all : place_test
5+
6+
place.o : place.h place.cu
7+
$(CC) $(CCFLAGS) -c place.cu -o $@
8+
9+
place_test : place.o place_test.cu
10+
$(CC) $(CCFLAGS) -lgtest -lgtest_main $^ -o $@

majel/place.cu

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include <majel/place.h>
2+
3+
namespace majel {
4+
5+
namespace detail {
6+
7+
class PlacePrinter
8+
: public boost::static_visitor<> {
9+
private:
10+
std::ostream& os_;
11+
public:
12+
PlacePrinter(std::ostream& os) : os_(os) {}
13+
14+
void operator()(const CpuPlace&) {
15+
os_ << "CpuPlace";
16+
}
17+
18+
void operator()(const GpuPlace& p) {
19+
os_ << "GpuPlace(" << p.device << ")";
20+
}
21+
};
22+
23+
} // namespace majel
24+
25+
static Place the_default_place;
26+
27+
void set_place(const Place& place) {
28+
the_default_place = place;
29+
}
30+
31+
const Place& get_place() {
32+
return the_default_place;
33+
}
34+
35+
const GpuPlace default_gpu() {
36+
return GpuPlace(0);
37+
}
38+
39+
const CpuPlace default_cpu() {
40+
return CpuPlace();
41+
}
42+
43+
bool is_gpu_place(const Place& p) {
44+
return boost::apply_visitor(IsGpuPlace(), p);
45+
}
46+
47+
bool is_cpu_place(const Place& p) {
48+
return !boost::apply_visitor(IsGpuPlace(), p);
49+
}
50+
51+
bool places_are_same_class(const Place& p1, const Place& p2) {
52+
return is_gpu_place(p1) == is_gpu_place(p2);
53+
}
54+
55+
std::ostream& operator<<(std::ostream& os, const majel::Place& p) {
56+
majel::detail::PlacePrinter printer(os);
57+
boost::apply_visitor(printer, p);
58+
return os;
59+
}
60+
61+
} // namespace majel

majel/place.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#pragma once
2+
#include <boost/variant.hpp>
3+
#include <iostream>
4+
5+
namespace majel {
6+
7+
struct CpuPlace {
8+
CpuPlace() {} // WORKAROUND: for some reason, omitting this constructor
9+
// causes errors with boost 1.59 and OSX
10+
// needed for variant equality comparison
11+
inline bool operator==(const CpuPlace&) const { return true; }
12+
13+
inline bool operator!=(const CpuPlace&) const { return false; }
14+
};
15+
16+
struct GpuPlace {
17+
GpuPlace(int d) : device(d) {}
18+
19+
// needed for variant equality comparison
20+
inline bool operator==(const GpuPlace& o) const { return device == o.device; }
21+
22+
inline bool operator!=(const GpuPlace& o) const { return !(*this == o); }
23+
24+
GpuPlace() : GpuPlace(0) {}
25+
int device;
26+
};
27+
28+
class IsGpuPlace : public boost::static_visitor<bool> {
29+
public:
30+
bool operator()(const CpuPlace&) const { return false; }
31+
32+
bool operator()(const GpuPlace& gpu) const { return true; }
33+
};
34+
35+
typedef boost::variant<GpuPlace, CpuPlace> Place;
36+
37+
void set_place(const Place&);
38+
39+
const Place& get_place();
40+
41+
const GpuPlace default_gpu();
42+
const CpuPlace default_cpu();
43+
44+
bool is_gpu_place(const Place&);
45+
bool is_cpu_place(const Place&);
46+
bool places_are_same_class(const Place&, const Place&);
47+
48+
std::ostream& operator<<(std::ostream&, const majel::Place&);
49+
50+
} // namespace majel

majel/place_test.cu

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include "gtest/gtest.h"
2+
#include "majel/place.h"
3+
#include <sstream>
4+
5+
TEST(Place, Equality) {
6+
majel::CpuPlace cpu;
7+
majel::GpuPlace g0(0), g1(1), gg0(0);
8+
9+
EXPECT_EQ(cpu, cpu);
10+
EXPECT_EQ(g0, g0);
11+
EXPECT_EQ(g1, g1);
12+
EXPECT_EQ(g0, gg0);
13+
14+
EXPECT_NE(g0, g1);
15+
16+
EXPECT_TRUE(majel::places_are_same_class(g0, gg0));
17+
EXPECT_FALSE(majel::places_are_same_class(g0, cpu));
18+
}
19+
20+
TEST(Place, Default) {
21+
EXPECT_TRUE(majel::is_gpu_place( majel::get_place()));
22+
EXPECT_TRUE(majel::is_gpu_place( majel::default_gpu()));
23+
EXPECT_TRUE(majel::is_cpu_place( majel::default_cpu()));
24+
25+
majel::set_place(majel::CpuPlace());
26+
EXPECT_TRUE(majel::is_cpu_place( majel::get_place()));
27+
}
28+
29+
TEST(Place, Print) {
30+
{
31+
std::stringstream ss;
32+
ss << majel::GpuPlace(1);
33+
EXPECT_EQ("GpuPlace(1)", ss.str());
34+
}
35+
{
36+
std::stringstream ss;
37+
ss << majel::CpuPlace();
38+
EXPECT_EQ("CpuPlace", ss.str());
39+
}
40+
}

0 commit comments

Comments
 (0)