Skip to content

Commit

Permalink
fix: don't use pinned memory in cpu_backend (PROOF-921) (#196)
Browse files Browse the repository at this point in the history
don't use pinned memory in cpu_backend
  • Loading branch information
rnburn authored Oct 30, 2024
1 parent 41dc7bf commit 1e0df4a
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 18 deletions.
23 changes: 23 additions & 0 deletions cbindings/fixed_pedersen.t.cc
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,29 @@ TEST_CASE("we can compute multi-exponentiations with a fixed set of generators")
sxt_multiexp_handle_free(hp);
}

SECTION("we can read and write a handle to a file with cpu backend") {
cbn::reset_backend_for_testing();
const sxt_config config = {SXT_CPU_BACKEND, 0};

REQUIRE(sxt_init(&config) == 0);
bastst::temp_file temp_file{std::ios::binary};
temp_file.stream().close();

wrapped_handle h{generators.data(), 2};
REQUIRE(h.h != nullptr);

sxt_multiexp_handle_write_to_file(h.h, temp_file.name().c_str());

auto hp = sxt_multiexp_handle_new_from_file(SXT_CURVE_RISTRETTO255, temp_file.name().c_str());

uint8_t scalars[] = {1, 0, 0, 2};
c21t::element_p3 res;
sxt_fixed_multiexponentiation(&res, hp, 2, 1, 2, scalars);
REQUIRE(res == generators[0] + 2 * 256 * generators[1]);

sxt_multiexp_handle_free(hp);
}

SECTION("we can compute a multiexponentiation in packed form") {
cbn::reset_backend_for_testing();
const sxt_config config = {SXT_GPU_BACKEND, 0};
Expand Down
14 changes: 0 additions & 14 deletions sxt/cbindings/backend/computational_backend.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,6 @@
#include "sxt/multiexp/pippenger2/in_memory_partition_table_accessor.h"

namespace sxt::cbnbck {
//--------------------------------------------------------------------------------------------------
// read_partition_table_accessor
//--------------------------------------------------------------------------------------------------
std::unique_ptr<mtxpp2::partition_table_accessor_base>
computational_backend::read_partition_table_accessor(cbnb::curve_id_t curve_id,
const char* filename) const noexcept {
std::unique_ptr<mtxpp2::partition_table_accessor_base> res;
cbnb::switch_curve_type(
curve_id, [&]<class U, class T>(std::type_identity<U>, std::type_identity<T>) noexcept {
res = std::make_unique<mtxpp2::in_memory_partition_table_accessor<U>>(filename);
});
return res;
}

//--------------------------------------------------------------------------------------------------
// write_partition_table_accessor
//--------------------------------------------------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions sxt/cbindings/backend/computational_backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ class computational_backend {
const unsigned* output_lengths, unsigned num_outputs,
const uint8_t* scalars) const noexcept = 0;

std::unique_ptr<mtxpp2::partition_table_accessor_base>
read_partition_table_accessor(cbnb::curve_id_t curve_id, const char* filename) const noexcept;
virtual std::unique_ptr<mtxpp2::partition_table_accessor_base>
read_partition_table_accessor(cbnb::curve_id_t curve_id, const char* filename) const noexcept = 0;

void write_partition_table_accessor(cbnb::curve_id_t curve_id,
const mtxpp2::partition_table_accessor_base& accessor,
Expand Down
15 changes: 15 additions & 0 deletions sxt/cbindings/backend/cpu_backend.cc
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,21 @@ void cpu_backend::fixed_multiexponentiation(void* res, cbnb::curve_id_t curve_id
});
}

//--------------------------------------------------------------------------------------------------
// read_partition_table_accessor
//--------------------------------------------------------------------------------------------------
std::unique_ptr<mtxpp2::partition_table_accessor_base>
cpu_backend::read_partition_table_accessor(cbnb::curve_id_t curve_id,
const char* filename) const noexcept {
std::unique_ptr<mtxpp2::partition_table_accessor_base> res;
cbnb::switch_curve_type(curve_id, [&]<class U, class T>(std::type_identity<U>,
std::type_identity<T>) noexcept {
res =
std::make_unique<mtxpp2::in_memory_partition_table_accessor<U>>(filename, basm::alloc_t{});
});
return res;
}

//--------------------------------------------------------------------------------------------------
// get_cpu_backend
//--------------------------------------------------------------------------------------------------
Expand Down
4 changes: 4 additions & 0 deletions sxt/cbindings/backend/cpu_backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ class cpu_backend final : public computational_backend {
const unsigned* output_bit_table, const unsigned* output_lengths,
unsigned num_outputs,
const uint8_t* scalars) const noexcept override;

std::unique_ptr<mtxpp2::partition_table_accessor_base>
read_partition_table_accessor(cbnb::curve_id_t curve_id,
const char* filename) const noexcept override;
};

//--------------------------------------------------------------------------------------------------
Expand Down
14 changes: 14 additions & 0 deletions sxt/cbindings/backend/gpu_backend.cc
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,20 @@ void gpu_backend::fixed_multiexponentiation(void* res, cbnb::curve_id_t curve_id
});
}

//--------------------------------------------------------------------------------------------------
// read_partition_table_accessor
//--------------------------------------------------------------------------------------------------
std::unique_ptr<mtxpp2::partition_table_accessor_base>
gpu_backend::read_partition_table_accessor(cbnb::curve_id_t curve_id,
const char* filename) const noexcept {
std::unique_ptr<mtxpp2::partition_table_accessor_base> res;
cbnb::switch_curve_type(
curve_id, [&]<class U, class T>(std::type_identity<U>, std::type_identity<T>) noexcept {
res = std::make_unique<mtxpp2::in_memory_partition_table_accessor<U>>(filename);
});
return res;
}

//--------------------------------------------------------------------------------------------------
// get_gpu_backend
//--------------------------------------------------------------------------------------------------
Expand Down
4 changes: 4 additions & 0 deletions sxt/cbindings/backend/gpu_backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ class gpu_backend final : public computational_backend {
const unsigned* output_bit_table, const unsigned* output_lengths,
unsigned num_outputs,
const uint8_t* scalars) const noexcept override;

std::unique_ptr<mtxpp2::partition_table_accessor_base>
read_partition_table_accessor(cbnb::curve_id_t curve_id,
const char* filename) const noexcept override;
};

//--------------------------------------------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions sxt/multiexp/pippenger2/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ sxt_cc_component(
"//sxt/base/device:memory_utility",
"//sxt/base/error:assert",
"//sxt/base/error:panic",
"//sxt/base/memory:alloc",
"//sxt/memory/management:managed_array",
"//sxt/memory/resource:pinned_resource",
],
Expand Down
6 changes: 4 additions & 2 deletions sxt/multiexp/pippenger2/in_memory_partition_table_accessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "sxt/base/device/memory_utility.h"
#include "sxt/base/error/assert.h"
#include "sxt/base/error/panic.h"
#include "sxt/base/memory/alloc.h"
#include "sxt/memory/management/managed_array.h"
#include "sxt/memory/resource/pinned_resource.h"
#include "sxt/multiexp/pippenger2/partition_table_accessor.h"
Expand All @@ -37,8 +38,9 @@ template <class T>
class in_memory_partition_table_accessor final : public partition_table_accessor<T> {

public:
explicit in_memory_partition_table_accessor(std::string_view filename) noexcept
: table_{memr::get_pinned_resource()} {
explicit in_memory_partition_table_accessor(
std::string_view filename, basm::alloc_t alloc = memr::get_pinned_resource()) noexcept
: table_{alloc} {
std::ifstream in{std::string{filename}, std::ios::binary};
if (!in.good()) {
baser::panic("failed to open {}: {}", filename, std::strerror(errno));
Expand Down

0 comments on commit 1e0df4a

Please sign in to comment.