Skip to content

Commit

Permalink
Windows Stack workarounds
Browse files Browse the repository at this point in the history
Reduce stack requirements by allocating on heap in order to fit into Windows 1 mb stack size (unchangeable due to pybind11). Fix tests to close prover file handle before removing file. Remove unused stackallocator.
  • Loading branch information
hoffmang9 authored Mar 31, 2020
2 parents d0357d4 + 22b0bf3 commit e07b746
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 24 deletions.
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
target_link_libraries(ProofOfSpace fse)
target_link_libraries(HellmanAttacks fse)
target_link_libraries(RunTests fse)
elseif (WIN32)
target_link_libraries(chiapos PRIVATE fse)
target_link_libraries(ProofOfSpace fse)
target_link_libraries(HellmanAttacks fse)
target_link_libraries(RunTests fse)
else()
target_link_libraries(chiapos PRIVATE fse stdc++fs)
target_link_libraries(ProofOfSpace fse stdc++fs)
Expand Down
39 changes: 34 additions & 5 deletions src/bits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@
#include <string>
#include <utility>
#include "./util.hpp"
#include "./stack_allocator.h"
#include <functional>
#include <memory>
#include <iostream>

using namespace std;

#define kBufSize 5

Expand All @@ -33,10 +36,23 @@
struct SmallVector {
typedef uint16_t size_type;

SmallVector() noexcept {
SmallVector() {
v_ = new uint128_t[5];
count_ = 0;
}

SmallVector(const SmallVector &other)
{
v_ = new uint128_t[5];
count_=other.count_;
for (size_type i = 0; i < other.count_; i++)
v_[i] = other.v_[i];
}

~SmallVector() {
delete[] v_;
}

uint128_t& operator[] (const uint16_t index) {
return v_[index];
}
Expand All @@ -61,7 +77,7 @@ struct SmallVector {
}

private:
uint128_t v_[5];
uint128_t *v_;
size_type count_;
};

Expand All @@ -71,10 +87,23 @@ struct SmallVector {
struct ParkVector {
typedef uint32_t size_type;

ParkVector() noexcept {
ParkVector() {
v_ = new uint128_t[1024];
count_ = 0;
}

ParkVector(const ParkVector &other)
{
v_ = new uint128_t[1024];
count_=other.count_;
for (size_type i = 0; i < other.count_; i++)
v_[i] = other.v_[i];
}

~ParkVector() {
delete[] v_;
}

uint128_t& operator[] (const uint32_t index) {
return v_[index];
}
Expand All @@ -99,7 +128,7 @@ struct ParkVector {
}

private:
uint128_t v_[1024];
uint128_t *v_;
size_type count_;
};

Expand Down
22 changes: 20 additions & 2 deletions src/plotter_disk.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -664,9 +664,17 @@ class DiskPlotter {
// Sort keys represent the ordering of entries, sorted by (y, pos, offset),
// but using less bits (only k+1 instead of 2k + 9, etc.)
// This is a map from old position to array of sort keys (one for each R entry with this pos)
Bits old_sort_keys[kReadMinusWrite][kMaxMatchesSingleEntry];
//Bits old_sort_keys[kReadMinusWrite][kMaxMatchesSingleEntry];
Bits **old_sort_keys = new Bits*[kReadMinusWrite];
for(int i = 0; i < kReadMinusWrite; ++i) {
old_sort_keys[i] = new Bits[kMaxMatchesSingleEntry];
}
// Map from old position to other positions that it matches with
uint64_t old_offsets[kReadMinusWrite][kMaxMatchesSingleEntry];
//uint64_t old_offsets[kReadMinusWrite][kMaxMatchesSingleEntry];
uint64_t **old_offsets = new uint64_t*[kReadMinusWrite];
for(int i = 0; i < kReadMinusWrite; ++i) {
old_offsets[i] = new uint64_t[kMaxMatchesSingleEntry];
}
// Map from old position to count (number of times it appears)
uint16_t old_counters[kReadMinusWrite];

Expand Down Expand Up @@ -877,6 +885,16 @@ class DiskPlotter {
delete[] left_entry_buf;
delete[] new_left_entry_buf;
delete[] right_entry_buf;

for(int i = 0; i < kReadMinusWrite; ++i) {
delete [] old_offsets[i];
}
delete [] old_offsets;

for(int i = 0; i < kReadMinusWrite; ++i) {
delete [] old_sort_keys[i];
}
delete [] old_sort_keys;
}
delete[] memory;
}
Expand Down
24 changes: 9 additions & 15 deletions src/sort_on_disk.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,10 @@ class SortOnDiskUtils {
* index, to the given index.
*/
inline static uint64_t ExtractNum(uint8_t* bytes, uint32_t len_bytes, uint32_t begin_bits, uint32_t take_bits) {
uint32_t start_index = begin_bits / 8;
uint32_t end_index;
if ((begin_bits + take_bits) / 8 > len_bytes - 1) {
take_bits = (len_bytes) * 8 - begin_bits;
take_bits = len_bytes * 8 - begin_bits;
}
end_index = (begin_bits + take_bits) / 8;

assert(take_bits <= 64);
uint64_t sum = bytes[start_index] & ((1 << (8 - (begin_bits % 8))) - 1);
for (uint32_t i = start_index + 1; i <= end_index; i++) {
sum = (sum << 8);
if(i < len_bytes) // Fix to prevent overflow and maintain compatibility. Function may only return 57 bits in some cases
sum += bytes[i];
}
return sum >> (8 - ((begin_bits + take_bits) % 8));
return Util::SliceInt64FromBytes(bytes, len_bytes, begin_bits, take_bits);
}

/*
Expand Down Expand Up @@ -92,10 +81,15 @@ class Disk {

class FileDisk : public Disk {
public:
inline explicit FileDisk(const std::string& filename) {
FileDisk(const std::string& filename) {
buf_ = new char[BUF_SIZE];
Initialize(filename);
}

~FileDisk() {
delete[] buf_;
}

inline void Close() {
f_.close();
}
Expand Down Expand Up @@ -153,7 +147,7 @@ class FileDisk : public Disk {
}

std::string filename_;
char buf_[BUF_SIZE];
char *buf_;
std::fstream f_;
};

Expand Down
6 changes: 4 additions & 2 deletions tests/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -384,9 +384,10 @@ TEST_CASE("Plotting") {

TEST_CASE("Invalid plot") {
SECTION("File gets deleted") {
string filename = "invalid-plot.dat";
{
DiskPlotter plotter = DiskPlotter();
uint8_t memo[5] = {1, 2, 3, 4, 5};
string filename = "invalid-plot.dat";
uint8_t k = 22;
plotter.CreatePlotDisk(".", ".", filename, k, memo, 5, plot_id_1, 32);
DiskProver prover(filename);
Expand All @@ -402,11 +403,12 @@ TEST_CASE("Invalid plot") {
LargeBits quality = verifier.ValidateProof(plot_id_1, k, challenge, proof_data, k*8);
REQUIRE(quality == qualities[index]);
}
delete[] proof_data;
}
REQUIRE(remove(filename.c_str()) == 0);
REQUIRE_THROWS_WITH([&](){
DiskProver p(filename);
}(), "Invalid file " + filename);
delete[] proof_data;
}
}

Expand Down
1 change: 1 addition & 0 deletions tests/test_python_bindings.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def test_k_21(self):
print(f"total proofs {total_proofs} out of {iterations}\
{total_proofs / iterations}")
assert total_proofs == 4647
pr = None
Path("myplot.dat").unlink()


Expand Down
24 changes: 24 additions & 0 deletions windows-build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/sh -x

# Stop in case of error
set -e

#python -m pip install cibuildwheel==1.3.0


# build just python 3.7
export CIBW_BUILD="cp37-win_amd64"
#export CIBW_BEFORE_BUILD_WINDOWS="python -m pip install --upgrade pip"
export CIBW_TEST_REQUIRES="pytest"
export CIBW_TEST_COMMAND="py.test -v {project}/tests"


export PATH=$PATH:"C:\msys64\mingw64\bin\cmake.exe"
echo "PWD is $PWD"

cmake -G "MSYS Makefiles" --build .
make
$PWD/RunTests.exe
#Test failing for windows:
py.test -v $PWD/tests/
#pip -vv wheel .

0 comments on commit e07b746

Please sign in to comment.