Skip to content

Multi weight storage #1008

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

Draft
wants to merge 19 commits into
base: develop
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
7 changes: 4 additions & 3 deletions .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,12 @@ cmake --build --preset tidy
To autofix, use:

```bash
cmake --preset --preset tidy -DCMAKE_CXX_CLANG_TIDY="clang-tidy;--fix"
cmake --build --preset tidy -j1
cmake --preset --preset tidy-fix
cmake --build --preset tidy-fix
```

Remember to build single-threaded if applying fixes!
We also provide matching `--workflow`'s, but you'll need a newer CMake for that
(you can use pip to get it, though).

## Include what you use

Expand Down
11 changes: 9 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,15 @@ option(BOOST_HISTOGRAM_ERRORS "Make warnings errors (for CI mostly)")
# Adding warnings
# Boost.Histogram doesn't pass sign -Wsign-conversion
if("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang" OR "${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU")
target_compile_options(_core PRIVATE -Wall -Wextra -pedantic-errors -Wconversion -Wsign-compare
-Wno-unused-value)
target_compile_options(
_core
PRIVATE -Wall
-Wextra
-pedantic-errors
-Wconversion
-Wsign-compare
-Wno-unused-value
-Wno-sign-conversion)
if(BOOST_HISTOGRAM_ERRORS)
target_compile_options(_core PRIVATE -Werror)
endif()
Expand Down
31 changes: 31 additions & 0 deletions CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@
"cacheVariables": {
"CMAKE_CXX_CLANG_TIDY": "clang-tidy;--warnings-as-errors=*"
}
},
{
"name": "tidy-fix",
"displayName": "Clang-tidy autofix",
"inherits": "tidy",
"cacheVariables": {
"CMAKE_CXX_CLANG_TIDY": "clang-tidy;--warnings-as-errors=*;--fix"
}
}
],
"buildPresets": [
Expand All @@ -38,6 +46,13 @@
"displayName": "Clang-tidy build",
"configurePreset": "tidy",
"nativeToolOptions": ["-k0"]
},
{
"name": "tidy-fix",
"displayName": "Clang-tidy autofix build",
"configurePreset": "tidy-fix",
"jobs": 1,
"nativeToolOptions": ["-k0"]
}
],
"testPresets": [
Expand All @@ -59,6 +74,22 @@
{ "type": "build", "name": "default" },
{ "type": "test", "name": "default" }
]
},
{
"name": "tidy",
"displayName": "Clang-tidy workflow",
"steps": [
{ "type": "configure", "name": "tidy" },
{ "type": "build", "name": "tidy" }
]
},
{
"name": "tidy-fix",
"displayName": "Clang-tidy autofix workflow",
"steps": [
{ "type": "configure", "name": "tidy-fix" },
{ "type": "build", "name": "tidy-fix" }
]
}
]
}
33 changes: 31 additions & 2 deletions include/bh_python/fill.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <bh_python/overload.hpp>
#include <bh_python/vector_string_caster.hpp>

#include <boost/core/ignore_unused.hpp>
#include <boost/histogram/detail/accumulator_traits.hpp>
#include <boost/histogram/detail/axes.hpp>
#include <boost/histogram/sample.hpp>
Expand Down Expand Up @@ -181,7 +182,7 @@ void fill_impl(bh::detail::accumulator_traits_holder<true>,
finalize_args(kwargs);

// releasing gil here is safe, we don't manipulate refcounts
py::gil_scoped_release const lock;
const py::gil_scoped_release lock;
variant::visit(
overload([&h, &vargs](const variant::monostate&) { h.fill(vargs); },
[&h, &vargs](const auto& w) { h.fill(vargs, bh::weight(w)); }),
Expand All @@ -203,7 +204,7 @@ void fill_impl(bh::detail::accumulator_traits_holder<true, const double&>,
throw std::invalid_argument("Sample array must be 1D");

// releasing gil here is safe, we don't manipulate refcounts
py::gil_scoped_release const lock;
const py::gil_scoped_release lock;
variant::visit(
overload([&h, &vargs, &sarray](
const variant::monostate&) { h.fill(vargs, bh::sample(sarray)); },
Expand All @@ -213,6 +214,34 @@ void fill_impl(bh::detail::accumulator_traits_holder<true, const double&>,
weight);
}

// for multi_weight
template <class Histogram, class VArgs>
void fill_impl(bh::detail::accumulator_traits_holder<false, const boost::span<double>&>,
Histogram& h,
const VArgs& vargs,
const weight_t& weight,
py::kwargs& kwargs) {
boost::ignore_unused(weight);
auto s = required_arg(kwargs, "sample");
finalize_args(kwargs);
auto sarray = py::cast<c_array_t<double>>(s);
if(sarray.ndim() != 2)
throw std::invalid_argument("Sample array for MultiWeight must be 2D");

auto buf = sarray.request();
// releasing gil here is safe, we don't manipulate refcounts
const py::gil_scoped_release lock;
const auto buf_shape0 = static_cast<std::size_t>(buf.shape[0]);
const auto buf_shape1 = static_cast<std::size_t>(buf.shape[1]);
auto* src = static_cast<double*>(buf.ptr);
std::vector<boost::span<double>> vec_s;
vec_s.reserve(buf_shape0);
for(std::size_t i = 0; i < buf_shape0; i++) {
vec_s.emplace_back(src + (i * buf_shape1), buf_shape1);
}
h.fill(vargs, bh::sample(vec_s));
}

} // namespace detail

template <class Histogram>
Expand Down
10 changes: 10 additions & 0 deletions include/bh_python/histogram.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <bh_python/accumulators/mean.hpp>
#include <bh_python/accumulators/weighted_mean.hpp>
#include <bh_python/accumulators/weighted_sum.hpp>
#include <bh_python/multi_weight.hpp>

#include <boost/histogram/detail/axes.hpp>
#include <boost/histogram/histogram.hpp>
Expand Down Expand Up @@ -96,6 +97,15 @@ py::buffer_info make_buffer(bh::histogram<A, bh::unlimited_storage<Allocator>>&
return detail::make_buffer_impl(axes, flow, static_cast<double*>(buffer.ptr));
}

/// Specialization for multi_weight buffer
template <class A, class T>
py::buffer_info make_buffer(bh::histogram<A, bh::multi_weight<T>>& h, bool flow) {
const auto& axes = bh::unsafe_access::axes(h);
auto& storage = bh::unsafe_access::storage(h);
return detail::make_buffer_impl(
axes, flow, static_cast<double*>(storage.get_buffer()));
}

/// Compute the bin of an array from a runtime list
/// For example, [1,3,2] will return that bin of an array
template <class F, int Opt>
Expand Down
Loading