Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bohr: the pybind11 bindings #634

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "bridge/bohr/pybind11"]
path = bridge/bohr/pybind11
url = https://github.com/pybind/pybind11.git
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down
11 changes: 11 additions & 0 deletions bridge/bohr/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
86 changes: 86 additions & 0 deletions bridge/bohr/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <bhxx/bhxx.hpp>


namespace py = pybind11;

bhxx::BhArray<double> (&add_double)(const bhxx::BhArray<double>&, const bhxx::BhArray<double>&) = bhxx::add;
bhxx::BhArray<float> (&add_float)(const bhxx::BhArray<float>&, const bhxx::BhArray<float>&) = bhxx::add;
bhxx::BhArray<double> (&add_double_scalar)(const bhxx::BhArray<double>&, double) = bhxx::add;

PYBIND11_MODULE(example, m) {

py::class_<bhxx::Shape>(m, "Shape")
.def(py::init())
.def(py::init<const std::vector<uint64_t> &>())
.def("__str__",
[](const bhxx::Shape &self) {
return self.pprint();
})
;

py::implicitly_convertible<std::vector<uint64_t>, bhxx::Shape>();

py::class_<bhxx::Stride>(m, "Stride")
.def(py::init())
.def(py::init<const std::vector<uint64_t> &>())
.def("__str__",
[](const bhxx::Stride &self) {
return self.pprint();
})
;

py::implicitly_convertible<std::vector<int64_t>, bhxx::Stride>();

py::class_<bhxx::BhBase>(m, "BhBase")
.def(py::init())
;

py::class_<bhxx::BhArrayUnTypedCore>(m, "BhArray")
.def(py::init())
;

py::class_<bhxx::BhArray<float>>(m, "BhArrayFloat32")
.def(py::init())
.def(py::init<const bhxx::Shape &, const bhxx::Stride &>())
.def("__str__",
[](const bhxx::BhArray<float> &self) {
std::stringstream ss;
self.pprint(ss, 0, self.rank()-1);
return ss.str();
})
;

py::class_<bhxx::BhArray<double>>(m, "BhArrayFloat64")
.def(py::init())
.def(py::init<const bhxx::Shape &, const bhxx::Stride &>())
.def("__str__",
[](const bhxx::BhArray<double> &self) {
std::stringstream ss;
self.pprint(ss, 0, self.rank()-1);
return ss.str();
})
;

m.def("add", static_cast<bhxx::BhArray<double> (&)(const bhxx::BhArray<double>&, const bhxx::BhArray<double>&)>(bhxx::add), py::arg("in1"), py::arg("in2"));
m.def("add", static_cast<bhxx::BhArray<double> (&)(const bhxx::BhArray<double>&, double)>(bhxx::add), py::arg("in1"), py::arg("in2"));
m.def("add", static_cast<bhxx::BhArray<double> (&)(double, const bhxx::BhArray<double>&)>(bhxx::add), py::arg("in1"), py::arg("in2"));
m.def("add", static_cast<bhxx::BhArray<float> (&)(const bhxx::BhArray<float>&, const bhxx::BhArray<float>&)>(bhxx::add), py::arg("in1"), py::arg("in2"));
m.def("add", static_cast<bhxx::BhArray<float> (&)(const bhxx::BhArray<float>&, float)>(bhxx::add), py::arg("in1"), py::arg("in2"));
m.def("add", static_cast<bhxx::BhArray<float> (&)(float, const bhxx::BhArray<float>&)>(bhxx::add), py::arg("in1"), py::arg("in2"));

m.def("flush", bhxx::flush);
m.def("empty", static_cast<bhxx::BhArray<double> (&)(bhxx::Shape)>(bhxx::empty), py::arg("shape"));
m.def("full", static_cast<bhxx::BhArray<double> (&)(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
}
1 change: 1 addition & 0 deletions bridge/bohr/pybind11
Submodule pybind11 added at 4f72ef
50 changes: 50 additions & 0 deletions bridge/bohr/small_bench.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import time
import numpy as np
import bohrium as bh
import example as bohr

ITER = 10000
SHAPE = (10000,)


def compute(m, a, b):
return m.add(m.add(m.add(a, b), 42), b)


def numpy_bench():
a = np.full(SHAPE, 42)
b = np.full(SHAPE, 43)
for _ in range(ITER):
compute(np, a, b)


def bohr_bench():
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(SHAPE, 42)
b = bh.full(SHAPE, 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)
4 changes: 4 additions & 0 deletions bridge/cxx/include/bhxx/BhBase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
15 changes: 14 additions & 1 deletion include/bohrium/bh_static_vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ If not, see <http://www.gnu.org/licenses/>.
#include <functional>
#include <numeric>
#include <ostream>
#include <vector>
#include <boost/container/static_vector.hpp>
#include <sstream>

Expand All @@ -43,6 +44,18 @@ class BhStaticVector : public boost::container::static_vector<T, BH_MAXDIM> {
// This is C++11 syntax for exposing the constructor from the parent class
using boost::container::static_vector<T, BH_MAXDIM>::static_vector;

BhStaticVector(const std::vector<uint64_t> &vec) : boost::container::static_vector<T, BH_MAXDIM>() {
for(const auto& v: vec) {
this->push_back(v);
}
}

BhStaticVector(const std::vector<int64_t> &vec) : boost::container::static_vector<T, BH_MAXDIM>() {
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});
Expand All @@ -61,7 +74,7 @@ class BhStaticVector : public boost::container::static_vector<T, BH_MAXDIM> {
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();
Expand Down