From 72e228e1c68f1604716fa0e593f5cbbb49042402 Mon Sep 17 00:00:00 2001 From: "Mads R. B. Kristensen" Date: Sun, 11 Aug 2019 16:27:18 +0200 Subject: [PATCH 1/3] Testing concept --- .gitmodules | 3 + CMakeLists.txt | 2 + bridge/bohr/CMakeLists.txt | 11 ++++ bridge/bohr/main.cpp | 86 ++++++++++++++++++++++++++++ bridge/bohr/pybind11 | 1 + bridge/cxx/include/bhxx/BhBase.hpp | 4 ++ include/bohrium/bh_static_vector.hpp | 15 ++++- 7 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 .gitmodules create mode 100644 bridge/bohr/CMakeLists.txt create mode 100644 bridge/bohr/main.cpp create mode 160000 bridge/bohr/pybind11 diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 000000000..096fede28 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "bridge/bohr/pybind11"] + path = bridge/bohr/pybind11 + url = https://github.com/pybind/pybind11.git diff --git a/CMakeLists.txt b/CMakeLists.txt index bdb305532..4026f8eea 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -157,6 +157,8 @@ add_subdirectory(bridge/py_api) add_subdirectory(bridge/npbackend) add_subdirectory(bridge/bh107) +add_subdirectory(bridge/bohr) + add_subdirectory(test) string(REPLACE ";" ", " BH_OPENMP_LIBS "${BH_OPENMP_LIBS}") diff --git a/bridge/bohr/CMakeLists.txt b/bridge/bohr/CMakeLists.txt new file mode 100644 index 000000000..5e214219f --- /dev/null +++ b/bridge/bohr/CMakeLists.txt @@ -0,0 +1,11 @@ +cmake_minimum_required(VERSION 2.8) + +add_subdirectory(pybind11) + +pybind11_add_module(example main.cpp) + +include_directories(${CMAKE_SOURCE_DIR}/include) +include_directories(${CMAKE_BINARY_DIR}/include) +include_directories(${CMAKE_SOURCE_DIR}/bridge/cxx/include) +include_directories(${CMAKE_BINARY_DIR}/bridge/cxx/include) +target_link_libraries(example bhxx) \ No newline at end of file diff --git a/bridge/bohr/main.cpp b/bridge/bohr/main.cpp new file mode 100644 index 000000000..4943fa1f2 --- /dev/null +++ b/bridge/bohr/main.cpp @@ -0,0 +1,86 @@ +#include +#include +#include + + +namespace py = pybind11; + +bhxx::BhArray (&add_double)(const bhxx::BhArray&, const bhxx::BhArray&) = bhxx::add; +bhxx::BhArray (&add_float)(const bhxx::BhArray&, const bhxx::BhArray&) = bhxx::add; +bhxx::BhArray (&add_double_scalar)(const bhxx::BhArray&, double) = bhxx::add; + +PYBIND11_MODULE(example, m) { + + py::class_(m, "Shape") + .def(py::init()) + .def(py::init &>()) + .def("__str__", + [](const bhxx::Shape &self) { + return self.pprint(); + }) + ; + + py::implicitly_convertible, bhxx::Shape>(); + + py::class_(m, "Stride") + .def(py::init()) + .def(py::init &>()) + .def("__str__", + [](const bhxx::Stride &self) { + return self.pprint(); + }) + ; + + py::implicitly_convertible, bhxx::Stride>(); + + py::class_(m, "BhBase") + .def(py::init()) + ; + + py::class_(m, "BhArray") + .def(py::init()) + ; + + py::class_>(m, "BhArrayFloat32") + .def(py::init()) + .def(py::init()) + .def("__str__", + [](const bhxx::BhArray &self) { + std::stringstream ss; + self.pprint(ss, 0, self.rank()-1); + return ss.str(); + }) + ; + + py::class_>(m, "BhArrayFloat64") + .def(py::init()) + .def(py::init()) + .def("__str__", + [](const bhxx::BhArray &self) { + std::stringstream ss; + self.pprint(ss, 0, self.rank()-1); + return ss.str(); + }) + ; + + m.def("add", static_cast (&)(const bhxx::BhArray&, const bhxx::BhArray&)>(bhxx::add), py::arg("in1"), py::arg("in2")); + m.def("add", static_cast (&)(const bhxx::BhArray&, double)>(bhxx::add), py::arg("in1"), py::arg("in2")); + m.def("add", static_cast (&)(double, const bhxx::BhArray&)>(bhxx::add), py::arg("in1"), py::arg("in2")); + m.def("add", static_cast (&)(const bhxx::BhArray&, const bhxx::BhArray&)>(bhxx::add), py::arg("in1"), py::arg("in2")); + m.def("add", static_cast (&)(const bhxx::BhArray&, float)>(bhxx::add), py::arg("in1"), py::arg("in2")); + m.def("add", static_cast (&)(float, const bhxx::BhArray&)>(bhxx::add), py::arg("in1"), py::arg("in2")); + + m.def("flush", bhxx::flush); + m.def("empty", static_cast (&)(bhxx::Shape)>(bhxx::empty), py::arg("shape")); + m.def("full", static_cast (&)(bhxx::Shape, double)>(bhxx::full), py::arg("shape"), py::arg("val")); + + + + + +#ifdef VERSION_INFO + m.attr("__version__") = VERSION_INFO; +#else + m.attr("__version__") = "dev"; +#endif +} \ No newline at end of file diff --git a/bridge/bohr/pybind11 b/bridge/bohr/pybind11 new file mode 160000 index 000000000..c9d32a81f --- /dev/null +++ b/bridge/bohr/pybind11 @@ -0,0 +1 @@ +Subproject commit c9d32a81f40ad540015814edf13b29980c63e39c diff --git a/bridge/cxx/include/bhxx/BhBase.hpp b/bridge/cxx/include/bhxx/BhBase.hpp index 9f1cfcb29..10b7150c7 100644 --- a/bridge/cxx/include/bhxx/BhBase.hpp +++ b/bridge/cxx/include/bhxx/BhBase.hpp @@ -29,6 +29,10 @@ namespace bhxx { /** The base underlying (multiple) arrays */ class BhBase : public bh_base { public: + + // This is C++11 syntax for exposing the constructor from the parent class + using bh_base::bh_base; + /** Is the memory managed referenced by bh_base's data pointer * managed by Bohrium or is it owned externally * diff --git a/include/bohrium/bh_static_vector.hpp b/include/bohrium/bh_static_vector.hpp index d562ae418..e7af3ccdc 100644 --- a/include/bohrium/bh_static_vector.hpp +++ b/include/bohrium/bh_static_vector.hpp @@ -25,6 +25,7 @@ If not, see . #include #include #include +#include #include #include @@ -43,6 +44,18 @@ class BhStaticVector : public boost::container::static_vector { // This is C++11 syntax for exposing the constructor from the parent class using boost::container::static_vector::static_vector; + BhStaticVector(const std::vector &vec) : boost::container::static_vector() { + for(const auto& v: vec) { + this->push_back(v); + } + } + + BhStaticVector(const std::vector &vec) : boost::container::static_vector() { + for(const auto& v: vec) { + this->push_back(v); + } + } + /// The sum of the elements in this vector virtual T sum() const { return std::accumulate(this->begin(), this->end(), T{0}); @@ -61,7 +74,7 @@ class BhStaticVector : public boost::container::static_vector { auto it = this->begin(); ss << *it; ++it; - for (; it != this->end(); ++it) ss << ',' << *it; + for (; it != this->end(); ++it) ss << ", " << *it; } ss << ')'; return ss.str(); From 4b53772905ccb40e4edeed1d54b014e7bf4b75e5 Mon Sep 17 00:00:00 2001 From: "Mads R. B. Kristensen" Date: Sun, 11 Aug 2019 16:35:43 +0200 Subject: [PATCH 2/3] Added a small benchmark --- bridge/bohr/small_bench.py | 44 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 bridge/bohr/small_bench.py diff --git a/bridge/bohr/small_bench.py b/bridge/bohr/small_bench.py new file mode 100644 index 000000000..391475083 --- /dev/null +++ b/bridge/bohr/small_bench.py @@ -0,0 +1,44 @@ +import time +import numpy as np +import bohrium as bh +import example as bohr + +ITER = 10000 + +def compute(m, a, b): + return m.add(m.add(m.add(a,b), 42), b) + +def numpy_bench(): + a = np.full((2000,), 42) + b = np.full((2000,), 43) + for _ in range(ITER): + compute(np, a, b) + +def bohr_bench(): + a = bohr.full((2000,), 42) + b = bohr.full((2000,), 43) + for _ in range(ITER): + compute(bohr, a, b) + bohr.flush() + +def bh_bench(): + a = bh.full((2000,), 42) + b = bh.full((2000,), 43) + for _ in range(ITER): + compute(bh, a, b) + bh.flush() + +t1 = time.time() +numpy_bench() +t2 = time.time() +print("Numpy: ", t2-t1) + +t1 = time.time() +bohr_bench() +t2 = time.time() +print("Bohr: ", t2-t1) + +t1 = time.time() +bh_bench() +t2 = time.time() +print("Bohrium: ", t2-t1) \ No newline at end of file From d41ab8e43fcc3097b9e6e568ccc87ec339523840 Mon Sep 17 00:00:00 2001 From: "Mads R. B. Kristensen" Date: Tue, 18 Feb 2020 11:04:04 +0100 Subject: [PATCH 3/3] clean up --- bridge/bohr/pybind11 | 2 +- bridge/bohr/small_bench.py | 28 +++++++++++++++++----------- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/bridge/bohr/pybind11 b/bridge/bohr/pybind11 index c9d32a81f..4f72ef846 160000 --- a/bridge/bohr/pybind11 +++ b/bridge/bohr/pybind11 @@ -1 +1 @@ -Subproject commit c9d32a81f40ad540015814edf13b29980c63e39c +Subproject commit 4f72ef846fe8453596230ac285eeaa0ce3278bb4 diff --git a/bridge/bohr/small_bench.py b/bridge/bohr/small_bench.py index 391475083..1f480105f 100644 --- a/bridge/bohr/small_bench.py +++ b/bridge/bohr/small_bench.py @@ -4,41 +4,47 @@ import example as bohr ITER = 10000 +SHAPE = (10000,) + def compute(m, a, b): - return m.add(m.add(m.add(a,b), 42), b) + return m.add(m.add(m.add(a, b), 42), b) + def numpy_bench(): - a = np.full((2000,), 42) - b = np.full((2000,), 43) + a = np.full(SHAPE, 42) + b = np.full(SHAPE, 43) for _ in range(ITER): compute(np, a, b) + def bohr_bench(): - a = bohr.full((2000,), 42) - b = bohr.full((2000,), 43) + a = bohr.full(SHAPE, 42) + b = bohr.full(SHAPE, 43) for _ in range(ITER): compute(bohr, a, b) bohr.flush() + def bh_bench(): - a = bh.full((2000,), 42) - b = bh.full((2000,), 43) + a = bh.full(SHAPE, 42) + b = bh.full(SHAPE, 43) for _ in range(ITER): compute(bh, a, b) - bh.flush() + bh.flush() + t1 = time.time() numpy_bench() t2 = time.time() -print("Numpy: ", t2-t1) +print("Numpy: ", t2 - t1) t1 = time.time() bohr_bench() t2 = time.time() -print("Bohr: ", t2-t1) +print("Bohr: ", t2 - t1) t1 = time.time() bh_bench() t2 = time.time() -print("Bohrium: ", t2-t1) \ No newline at end of file +print("Bohrium: ", t2 - t1)