Benchmarks for recover and zero_poly

This commit is contained in:
Ben Edgington 2021-02-28 17:00:42 +00:00
parent 79a9419791
commit a28860ae39
6 changed files with 198 additions and 2 deletions

View File

@ -1,6 +1,6 @@
TESTS = bls12_381_test das_extension_test c_kzg_util_test fft_common_test fft_fr_test fft_g1_test \
fk20_proofs_test kzg_proofs_test poly_test recover_test utility_test zero_poly_test
BENCH = fft_fr_bench fft_g1_bench
BENCH = fft_fr_bench fft_g1_bench recover_bench zero_poly_bench
LIB_SRC = bls12_381.c c_kzg_util.c das_extension.c fft_common.c fft_fr.c fft_g1.c fk20_proofs.c kzg_proofs.c poly.c recover.c utility.c zero_poly.c
LIB_OBJ = $(LIB_SRC:.c=.o)

View File

@ -50,6 +50,7 @@ long run_bench(int scale, int max_seconds) {
free(out);
free(data);
free_fft_settings(&fs);
return total_time / nits;
}

View File

@ -50,6 +50,7 @@ long run_bench(int scale, int max_seconds) {
free(out);
free(data);
free_fft_settings(&fs);
return total_time / nits;
}

107
src/recover_bench.c Normal file
View File

@ -0,0 +1,107 @@
/*
* Copyright 2021 Benjamin Edgington
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdlib.h> // malloc(), free(), atoi()
#include <stdio.h> // printf()
#include <assert.h> // assert()
#include <unistd.h> // EXIT_SUCCESS/FAILURE
#include "bench_util.h"
#include "test_util.h"
#include "fft_fr.h"
#include "recover.h"
// Run the benchmark for `max_seconds` and return the time per iteration in nanoseconds.
long run_bench(int scale, int max_seconds) {
timespec_t t0, t1;
unsigned long total_time = 0, nits = 0;
FFTSettings fs;
assert(C_KZG_OK == new_fft_settings(&fs, scale));
// Allocate on the heap to avoid stack overflow for large sizes
fr_t *poly = malloc(fs.max_width * sizeof(fr_t));
for (int i = 0; i < fs.max_width / 2; i++) {
fr_from_uint64(&poly[i], i);
}
for (int i = fs.max_width / 2; i < fs.max_width; i++) {
poly[i] = fr_zero;
}
fr_t *data = malloc(fs.max_width * sizeof(fr_t));
assert(C_KZG_OK == fft_fr(data, poly, false, fs.max_width, &fs));
fr_t *samples = malloc(fs.max_width * sizeof(fr_t));
for (int i = 0; i < fs.max_width; i++) {
samples[i] = data[i];
}
// randomly zero out half of the points
for (int i = 0; i < fs.max_width / 2; i++) {
int j = rand() % fs.max_width;
while (fr_is_null(&samples[j])) j = rand() % fs.max_width;
samples[j] = fr_null;
}
fr_t *recovered = malloc(fs.max_width * sizeof(fr_t));
while (total_time < max_seconds * NANO) {
clock_gettime(CLOCK_REALTIME, &t0);
assert(C_KZG_OK == recover_poly_from_samples(recovered, samples, fs.max_width, &fs));
clock_gettime(CLOCK_REALTIME, &t1);
// Verify the result is correct
for (int i = 0; i < fs.max_width; i++) {
assert(fr_equal(&data[i], &recovered[i]));
}
nits++;
total_time += tdiff(t0, t1);
}
free(recovered);
free(samples);
free(data);
free(poly);
free_fft_settings(&fs);
return total_time / nits;
}
int main(int argc, char *argv[]) {
int nsec = 0;
switch (argc) {
case 1:
nsec = NSEC;
break;
case 2:
nsec = atoi(argv[1]);
break;
default:
break;
};
if (nsec == 0) {
printf("Usage: %s [test time in seconds > 0]\n", argv[0]);
exit(EXIT_FAILURE);
}
printf("*** Benchmarking Recover From Samples, %d second%s per test.\n", nsec, nsec == 1 ? "" : "s");
for (int scale = 5; scale <= 15; scale++) {
printf("recover/scale_%d %lu ns/op\n", scale, run_bench(scale, nsec));
}
return EXIT_SUCCESS;
}

View File

@ -156,7 +156,7 @@ C_KZG_RET reduce_leaves(fr_t *dst, uint64_t len_dst, fr_t *scratch, uint64_t len
* @retval C_CZK_ERROR An internal error occurred
* @retval C_CZK_MALLOC Memory allocation failed
*
* @todo What is the performance impact of tuning `per_leaf_poly`?
* @todo What is the performance impact of tuning `per_leaf_poly` and `reduction factor`?
*/
C_KZG_RET zero_polynomial_via_multiplication(fr_t *zero_eval, fr_t *zero_poly, uint64_t *zero_poly_len, uint64_t length,
const uint64_t *missing_indices, uint64_t len_missing,

87
src/zero_poly_bench.c Normal file
View File

@ -0,0 +1,87 @@
/*
* Copyright 2021 Benjamin Edgington
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdlib.h> // malloc(), free(), atoi()
#include <stdio.h> // printf()
#include <assert.h> // assert()
#include <unistd.h> // EXIT_SUCCESS/FAILURE
#include "bench_util.h"
#include "test_util.h"
#include "fft_fr.h"
#include "zero_poly.h"
// Run the benchmark for `max_seconds` and return the time per iteration in nanoseconds.
long run_bench(int scale, int max_seconds) {
timespec_t t0, t1;
unsigned long total_time = 0, nits = 0;
FFTSettings fs;
assert(C_KZG_OK == new_fft_settings(&fs, scale));
// Allocate on the heap to avoid stack overflow for large sizes
uint64_t *missing = malloc(fs.max_width * sizeof(uint64_t));
for (int i = 0; i < fs.max_width; i++) {
missing[i] = i;
}
shuffle(missing, fs.max_width);
fr_t *zero_eval = malloc(fs.max_width * sizeof(fr_t));
fr_t *zero_poly = malloc(fs.max_width * sizeof(fr_t));
uint64_t zero_poly_len;
while (total_time < max_seconds * NANO) {
clock_gettime(CLOCK_REALTIME, &t0);
// Half missing leaves enough FFT computation space
assert(C_KZG_OK == zero_polynomial_via_multiplication(zero_eval, zero_poly, &zero_poly_len, fs.max_width,
missing, fs.max_width / 2, &fs));
clock_gettime(CLOCK_REALTIME, &t1);
nits++;
total_time += tdiff(t0, t1);
}
free(zero_poly);
free(zero_eval);
free(missing);
free_fft_settings(&fs);
return total_time / nits;
}
int main(int argc, char *argv[]) {
int nsec = 0;
switch (argc) {
case 1:
nsec = NSEC;
break;
case 2:
nsec = atoi(argv[1]);
break;
default:
break;
};
if (nsec == 0) {
printf("Usage: %s [test time in seconds > 0]\n", argv[0]);
exit(EXIT_FAILURE);
}
printf("*** Benchmarking Zero Polynomial, %d second%s per test.\n", nsec, nsec == 1 ? "" : "s");
for (int scale = 5; scale <= 15; scale++) {
printf("zero_poly/scale_%d %lu ns/op\n", scale, run_bench(scale, nsec));
}
return EXIT_SUCCESS;
}