Skip to content

Commit

Permalink
Allow setting the number of threads per proof segment in hw_vdf_client
Browse files Browse the repository at this point in the history
Add option --segment-threads that specifies the number of threads
computing each proof segment.
  • Loading branch information
rostislav authored and cmmarslender committed Oct 2, 2023
1 parent 154aff8 commit e7dad49
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 16 deletions.
5 changes: 3 additions & 2 deletions README_ASIC.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,11 @@ List of options [default, min - max]:
Allows connecting to a timelord running on a remote host. Useful when running multiple machines with VDF hardware connecting to a single timelord.
--vdfs-mask N - mask for enabling VDF engines [7, 1 - 7]
The ASIC has 3 VDF engines numbered 0, 1, 2. If not running all 3 engines, the mask can be specified to enable specific engines. It must be the result of bitwise OR of the engine bits (1, 2, 4 for engines 0, 1, 2).
--vdf-threads N - number of software threads per VDF engine [4, 2 - 64]
--vdf-threads N - max number of software threads per VDF engine [4, 2 - 64]
Number of software threads computing intermediate values and proofs per VDF engine.
--proof-threads N - number of proof threads per VDF engine
--proof-threads N - max number of proof threads per VDF engine
Number of software threads only computing proofs per VDF engine. Must be less than --vdf-threads.
--segment-threads N - number of proof threads per segment [2, 1 - 8]
--auto-freq-period N - auto-adjust frequency every N seconds [0, 10 - inf]
--list - list available devices and exit
```
Expand Down
10 changes: 7 additions & 3 deletions src/hw/hw_proof.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ void hw_proof_process_work(struct vdf_state *vdf)
vdf->idx, i, iters, proof->seg_iters, is_chkp ? " [checkpoint]" : "");
vdf->queued_proofs.erase(vdf->queued_proofs.begin());
vdf->aux_threads_busy |= 1UL << i;
vdf->n_proof_threads += PARALLEL_PROVER_N_THREADS;
vdf->n_proof_threads += vdf->segment_threads;
proof->flags |= HW_VDF_PROOF_FLAG_STARTED;
std::thread(hw_compute_proof, vdf, idx, proof, i).detach();
}
Expand Down Expand Up @@ -569,7 +569,7 @@ void hw_stop_proof(struct vdf_state *vdf)
class HwProver : public ParallelProver {
public:
HwProver(Segment segm, integer D, struct vdf_state *vdf)
: ParallelProver(segm, D)
: ParallelProver(segm, D, vdf->segment_threads)
{
this->vdf = vdf;
k = FindK(segm.length);
Expand Down Expand Up @@ -727,7 +727,7 @@ void hw_compute_proof(struct vdf_state *vdf, size_t proof_idx, struct vdf_proof
out:
if (thr_idx < vdf->max_aux_threads) {
vdf->aux_threads_busy &= ~(1UL << thr_idx);
vdf->n_proof_threads -= PARALLEL_PROVER_N_THREADS;
vdf->n_proof_threads -= vdf->segment_threads;
}
}

Expand Down Expand Up @@ -806,6 +806,10 @@ void init_vdf_state(struct vdf_state *vdf, struct vdf_proof_opts *opts, const ch
if (opts && opts->max_proof_threads) {
vdf->max_proof_threads = opts->max_proof_threads;
}
vdf->segment_threads = 2;
if (opts && opts->segment_threads) {
vdf->segment_threads = opts->segment_threads;
}

mpz_set_str(vdf->D.impl, d_str, 0);
mpz_set(vdf->L.impl, vdf->D.impl);
Expand Down
2 changes: 2 additions & 0 deletions src/hw/hw_proof.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ struct vdf_proof {
struct vdf_proof_opts {
uint8_t max_aux_threads;
uint8_t max_proof_threads;
uint8_t segment_threads;
};

struct vdf_state {
Expand Down Expand Up @@ -85,6 +86,7 @@ struct vdf_state {
uint8_t idx;
uint8_t max_aux_threads;
uint8_t max_proof_threads;
uint8_t segment_threads;
bool completed;
bool stopping;
bool init_done;
Expand Down
10 changes: 7 additions & 3 deletions src/hw/hw_vdf_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,7 @@ int parse_opts(int argc, char **argv, struct vdf_client_opts *opts)
opts->auto_freq = false;
opts->vpo.max_aux_threads = HW_VDF_DEFAULT_MAX_AUX_THREADS;
opts->vpo.max_proof_threads = 0;
opts->vpo.segment_threads = 0;
opts->vdfs_mask = 0;

while ((ret = getopt_long(argc, argv, "", long_opts, &long_idx)) == 1) {
Expand All @@ -451,8 +452,10 @@ int parse_opts(int argc, char **argv, struct vdf_client_opts *opts)
} else if (long_idx == 5) {
opts->vpo.max_proof_threads = strtoul(optarg, NULL, 0);
} else if (long_idx == 6) {
opts->do_list = true;
opts->vpo.segment_threads = strtoul(optarg, NULL, 0);
} else if (long_idx == 7) {
opts->do_list = true;
} else if (long_idx == 8) {
opts->auto_freq = true;
opts->auto_freq_period = strtoul(optarg, NULL, 0);
}
Expand Down Expand Up @@ -525,8 +528,9 @@ int hw_vdf_client_main(int argc, char **argv)
" --voltage N - set board voltage [%.2f, 0.7 - 1.0]\n"
" --ip A.B.C.D - timelord IP address [localhost]\n"
" --vdfs-mask N - mask for enabling VDF engines [7, 1 - 7]\n"
" --vdf-threads N - number of software threads per VDF engine [4, 2 - 64]\n"
" --proof-threads N - number of proof threads per VDF engine\n"
" --vdf-threads N - max number of software threads per VDF engine [4, 2 - 64]\n"
" --proof-threads N - max number of proof threads per VDF engine\n"
" --segment-threads N - number of proof threads per segment [2, 1 - 8]\n"
" --auto-freq-period N - auto-adjust frequency every N seconds [0, 10 - inf]\n"
" --list - list available devices and exit",
argv[0], (int)HW_VDF_DEF_FREQ, HW_VDF_DEF_VOLTAGE);
Expand Down
12 changes: 7 additions & 5 deletions src/prover_parallel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "proof_common.h"
#include "util.h"

#define PARALLEL_PROVER_N_THREADS 2
#define PROVER_MAX_SEGMENT_THREADS 8

class ParallelProver : public Prover {
private:
Expand Down Expand Up @@ -76,23 +76,25 @@ class ParallelProver : public Prover {
x_vals[thr_idx] = x;
}
public:
ParallelProver(Segment segm, integer D) : Prover(segm, D) {}
ParallelProver(Segment segm, integer D, size_t n_thr) : Prover(segm, D) {
this->n_threads = n_thr;
}
void GenerateProof();

protected:
integer B;
integer L;
form id;
form x_vals[PARALLEL_PROVER_N_THREADS];
form x_vals[PROVER_MAX_SEGMENT_THREADS];
size_t n_threads;
};

void ParallelProver::GenerateProof() {
PulmarkReducer reducer;
size_t n_threads = PARALLEL_PROVER_N_THREADS;
uint32_t len = l / n_threads;
uint32_t rem = l % n_threads;
uint32_t start = l;
std::thread threads[n_threads];
std::thread threads[PROVER_MAX_SEGMENT_THREADS];

this->B = GetB(D, segm.x, segm.y);
this->L = root(-D, 4);
Expand Down
9 changes: 6 additions & 3 deletions src/vdf_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,17 +295,20 @@ class Prover {
std::atomic<bool> is_finished;
};

#define PARALLEL_PROVER_N_THREADS 2
#define PROVER_MAX_SEGMENT_THREADS 8

class ParallelProver : public Prover {
public:
ParallelProver(Segment segm, integer D) : Prover(segm, D) {}
ParallelProver(Segment segm, integer D, size_t n_thr) : Prover(segm, D) {
this->n_threads = n_thr;
}
void GenerateProof();
protected:
integer B;
integer L;
form id;
form x_vals[PARALLEL_PROVER_N_THREADS];
form x_vals[PROVER_MAX_SEGMENT_THREADS];
size_t n_threads;
};

void nudupl_form(form &a, form &b, integer &D, integer &L);
Expand Down

0 comments on commit e7dad49

Please sign in to comment.