Skip to content
Open
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
82 changes: 81 additions & 1 deletion src/cpp/allocators/greedy_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@

#include <algorithm>
#include <numeric>
#include <span>
#include <stdexcept>
#include <string>
#include <tuple>
#include <utility>

namespace omnimalloc {

TemporalOverlaps compute_temporal_overlaps(
namespace {

// Sweep line over the single timeline; valid only for all-scalar input.
TemporalOverlaps compute_scalar_overlaps(
const std::vector<Allocation>& allocations) {
std::vector<std::tuple<int64_t, bool, size_t>> events;
events.reserve(allocations.size() * 2);
Expand Down Expand Up @@ -41,6 +47,80 @@ TemporalOverlaps compute_temporal_overlaps(
return overlaps;
}

// Component spans of all starts and ends, validated to one dimension and
// cached up front to keep the quadratic conflict loops branch-free.
struct ClockSpans {
std::vector<std::span<const int64_t>> starts;
std::vector<std::span<const int64_t>> ends;
};

ClockSpans cache_clock_spans(const std::vector<Allocation>& allocations) {
ClockSpans spans;
spans.starts.reserve(allocations.size());
spans.ends.reserve(allocations.size());
if (allocations.empty()) {
return spans;
}
const size_t dim = allocations.front().dim();
for (const auto& alloc : allocations) {
if (alloc.dim() != dim) {
throw std::invalid_argument(
"clock dimension mismatch: " + std::to_string(dim) + " vs " +
std::to_string(alloc.dim()));
}
spans.starts.push_back(alloc.start_vec());
spans.ends.push_back(alloc.end_vec());
}
return spans;
}

// Pairwise happens-before conflict test, O(n^2 * T); no linear timeline to
// sweep once lifetimes are vector clocks. Calls `on_conflict(i, j)` with i < j
// for every temporally overlapping pair.
template <typename OnConflict>
void for_each_conflict_pair(const std::vector<Allocation>& allocations,
OnConflict&& on_conflict) {
const auto [starts, ends] = cache_clock_spans(allocations);
for (size_t i = 0; i < allocations.size(); ++i) {
for (size_t j = i + 1; j < allocations.size(); ++j) {
if (!happens_before(ends[i], starts[j]) &&
!happens_before(ends[j], starts[i])) {
on_conflict(i, j);
}
}
}
}

TemporalOverlaps compute_vector_overlaps(
const std::vector<Allocation>& allocations) {
TemporalOverlaps overlaps;
for_each_conflict_pair(allocations, [&](size_t i, size_t j) {
overlaps[allocations[i].id()].insert(allocations[j].id());
overlaps[allocations[j].id()].insert(allocations[i].id());
});
return overlaps;
}

} // namespace

TemporalOverlaps compute_temporal_overlaps(
const std::vector<Allocation>& allocations) {
const bool all_scalar =
std::ranges::all_of(allocations, &Allocation::is_scalar_time);
return all_scalar ? compute_scalar_overlaps(allocations)
: compute_vector_overlaps(allocations);
}

std::vector<int64_t> compute_conflict_degrees(
const std::vector<Allocation>& allocations) {
std::vector<int64_t> degrees(allocations.size(), 0);
for_each_conflict_pair(allocations, [&](size_t i, size_t j) {
++degrees[i];
++degrees[j];
});
return degrees;
}

int64_t peak_of(const std::vector<Allocation>& placed) {
int64_t peak = 0;
for (const auto& alloc : placed) {
Expand Down
5 changes: 5 additions & 0 deletions src/cpp/allocators/greedy_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ using TemporalOverlaps =
[[nodiscard]] TemporalOverlaps compute_temporal_overlaps(
const std::vector<Allocation>& allocations);

// Per-allocation count of temporally overlapping allocations, aligned with
// `allocations`. Counts with multiplicity, so duplicate ids stay distinct.
[[nodiscard]] std::vector<int64_t> compute_conflict_degrees(
const std::vector<Allocation>& allocations);

// Greedily place allocations in order using first-fit, reusing a precomputed
// overlap map
[[nodiscard]] std::vector<Allocation> first_fit_place(
Expand Down
6 changes: 6 additions & 0 deletions src/cpp/allocators/supermalloc/partition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,12 @@ void Partition::init_search_state() {
}

Partition Partition::from_allocations(std::vector<Allocation> allocations) {
// The section grid needs a linear timeline; reject vector clocks here.
if (!std::ranges::all_of(allocations, &Allocation::is_scalar_time)) {
throw std::invalid_argument(
"Partition requires scalar time lifetimes; linearize vector clocks "
"first");
}
// Every offset, top, and floor + total the search computes is bounded by
// twice the total size, so rejecting sums above INT64_MAX / 2 rules out
// signed overflow everywhere downstream.
Expand Down
8 changes: 8 additions & 0 deletions src/cpp/allocators/telamalloc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <optional>
#include <random>
#include <set>
#include <stdexcept>
#include <tuple>
#include <unordered_set>
#include <utility>
Expand Down Expand Up @@ -320,6 +321,13 @@ TelamallocAllocator::TelamallocAllocator(TelamallocConfig config)

std::vector<Allocation> TelamallocAllocator::allocate(
const std::vector<Allocation>& allocations) const {
// The event sweeps and load bounds need a linear timeline; reject vector
// clocks here.
if (!std::ranges::all_of(allocations, &Allocation::is_scalar_time)) {
throw std::invalid_argument(
"TelamallocAllocator requires scalar time lifetimes; linearize vector "
"clocks first");
}
if (allocations.size() < 2) {
return first_fit_place(allocations, compute_temporal_overlaps(allocations));
}
Expand Down
37 changes: 30 additions & 7 deletions src/cpp/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@ namespace nb = nanobind;
using namespace nb::literals;
using namespace omnimalloc;

namespace {

// nanobind's default caster renders std::vector as list; the Python surface
// wants tuples so scalar/tuple time points stay consistent under == and hash.
nb::object time_to_python(const TimePoint& time) {
if (const auto* scalar = std::get_if<int64_t>(&time)) {
return nb::int_(*scalar);
}
return nb::tuple(nb::cast(std::get<std::vector<int64_t>>(time)));
}

} // namespace

NB_MODULE(_cpp, m) {
// BufferKind enum
nb::enum_<BufferKind>(m, "BufferKind")
Expand All @@ -52,14 +65,22 @@ NB_MODULE(_cpp, m) {

// Allocation class
nb::class_<Allocation>(m, "Allocation")
.def(nb::init<IdType, int64_t, int64_t, int64_t, std::optional<int64_t>,
std::optional<BufferKind>>(),
.def(nb::init<IdType, int64_t, TimePoint, TimePoint,
std::optional<int64_t>, std::optional<BufferKind>>(),
"id"_a, "size"_a, "start"_a, "end"_a, "offset"_a = nb::none(),
"kind"_a = nb::none())
.def_prop_ro("id", &Allocation::id, nb::rv_policy::copy)
.def_prop_ro("size", &Allocation::size)
.def_prop_ro("start", &Allocation::start)
.def_prop_ro("end", &Allocation::end)
.def_prop_ro(
"start",
[](const Allocation& a) { return time_to_python(a.start_time()); },
nb::for_getter(
nb::sig("def start(self, /) -> int | tuple[int, ...]")))
.def_prop_ro(
"end",
[](const Allocation& a) { return time_to_python(a.end_time()); },
nb::for_getter(nb::sig("def end(self, /) -> int | tuple[int, ...]")))
.def_prop_ro("dim", &Allocation::dim)
.def_prop_ro("offset", &Allocation::offset, nb::rv_policy::copy)
.def_prop_ro("kind", &Allocation::kind, nb::rv_policy::copy)
.def_prop_ro("is_allocated", &Allocation::is_allocated)
Expand Down Expand Up @@ -88,12 +109,12 @@ NB_MODULE(_cpp, m) {
.def("__hash__", std::hash<Allocation>{})
.def("__getstate__",
[](const Allocation& a) {
return std::make_tuple(a.id(), a.size(), a.start(), a.end(),
a.offset(), a.kind());
return std::make_tuple(a.id(), a.size(), a.start_time(),
a.end_time(), a.offset(), a.kind());
})
.def("__setstate__",
[](Allocation& a,
const std::tuple<IdType, int64_t, int64_t, int64_t,
const std::tuple<IdType, int64_t, TimePoint, TimePoint,
std::optional<int64_t>,
std::optional<BufferKind>>& state) {
new (&a) Allocation(std::get<0>(state), std::get<1>(state),
Expand All @@ -103,6 +124,8 @@ NB_MODULE(_cpp, m) {

m.def("compute_temporal_overlaps", &compute_temporal_overlaps,
"allocations"_a, nb::rv_policy::move);
m.def("compute_conflict_degrees", &compute_conflict_degrees, "allocations"_a,
nb::rv_policy::move);
m.def("first_fit_place", &first_fit_place, "allocations"_a, "overlaps"_a,
nb::rv_policy::move);

Expand Down
Loading
Loading