sandboxed-api/oss-internship-2020/pffft/test_pffft_sandboxed.cc

154 lines
4.0 KiB
C++
Raw Normal View History

2020-08-17 19:21:19 +08:00
#include <assert.h>
2020-08-17 19:20:56 +08:00
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/times.h>
2020-08-17 19:21:19 +08:00
#include <syscall.h>
#include <time.h>
2020-08-17 19:20:56 +08:00
2020-08-17 19:21:19 +08:00
#include "fftpack.h"
2020-08-17 19:20:56 +08:00
#include "pffft_sapi.sapi.h"
#include "sandboxed_api/util/flag.h"
#include "sandboxed_api/vars.h"
ABSL_DECLARE_FLAG(string, sandbox2_danger_danger_permit_all);
ABSL_DECLARE_FLAG(string, sandbox2_danger_danger_permit_all_and_log);
class pffftSapiSandbox : public pffftSandbox {
2020-08-17 19:21:19 +08:00
public:
std::unique_ptr<sandbox2::Policy> ModifyPolicy(
sandbox2::PolicyBuilder*) override {
return sandbox2::PolicyBuilder()
2020-08-17 19:20:56 +08:00
.AllowStaticStartup()
.AllowOpen()
.AllowRead()
.AllowWrite()
.AllowSystemMalloc()
.AllowExit()
.AllowSyscalls({
2020-08-17 19:21:19 +08:00
__NR_futex,
__NR_close,
__NR_getrusage,
2020-08-17 19:20:56 +08:00
})
.DisableNamespaces()
.BuildOrDie();
2020-08-17 19:21:19 +08:00
}
2020-08-17 19:20:56 +08:00
};
2020-08-17 19:21:19 +08:00
double frand() { return rand() / (double)RAND_MAX; }
2020-08-17 19:20:56 +08:00
2020-08-17 19:21:19 +08:00
double uclock_sec(void) { return (double)clock() / (double)CLOCKS_PER_SEC; }
2020-08-17 19:20:56 +08:00
int array_output_format = 0;
2020-08-17 19:21:19 +08:00
void show_output(const char* name, int N, int cplx, float flops, float t0,
float t1, int max_iter) {
float mflops = flops / 1e6 / (t1 - t0 + 1e-16);
2020-08-17 19:20:56 +08:00
if (array_output_format) {
if (flops != -1) {
printf("|%9.0f ", mflops);
2020-08-17 19:21:19 +08:00
} else
printf("| n/a ");
2020-08-17 19:20:56 +08:00
} else {
if (flops != -1) {
2020-08-17 19:21:19 +08:00
printf("N=%5d, %s %16s : %6.0f MFlops [t=%6.0f ns, %d runs]\n", N,
(cplx ? "CPLX" : "REAL"), name, mflops,
(t1 - t0) / 2 / max_iter * 1e9, max_iter);
2020-08-17 19:20:56 +08:00
}
}
fflush(stdout);
}
2020-08-17 19:21:19 +08:00
/*
2020-08-17 19:20:56 +08:00
For debug:
2020-08-17 19:21:19 +08:00
SAPI_VLOG_LEVEL=1 ./pffft_sandboxed --v=100
--sandbox2_danger_danger_permit_all_and_log my_aux_file
2020-08-17 19:20:56 +08:00
*/
int main(int argc, char* argv[]) {
gflags::ParseCommandLineFlags(&argc, &argv, true);
2020-08-17 19:21:19 +08:00
int Nvalues[] = {64, 96, 128, 160, 192, 256,
384, 5 * 96, 512, 5 * 128, 3 * 256, 800,
1024, 2048, 2400, 4096, 8192, 9 * 1024,
16384, 32768, 256 * 1024, 1024 * 1024, -1};
2020-08-17 19:20:56 +08:00
int i;
printf("initializing sandbox...\n");
pffftSapiSandbox sandbox;
sandbox.Init().IgnoreError();
pffftApi api(&sandbox);
2020-08-17 19:21:19 +08:00
2020-08-17 19:20:56 +08:00
int N, cplx;
cplx = 0;
for (i = 0; i < 5; i++) {
N = Nvalues[i];
2020-08-17 19:21:19 +08:00
2020-08-17 19:20:56 +08:00
int Nfloat = N * (cplx ? 2 : 1);
int Nbytes = Nfloat * sizeof(float);
int pass;
float ref[Nbytes], in[Nbytes], out[Nbytes], tmp[Nbytes], tmp2[Nbytes];
sapi::v::Array<float> ref_(ref, Nbytes);
sapi::v::Array<float> in_(in, Nbytes);
sapi::v::Array<float> out_(out, Nbytes);
sapi::v::Array<float> tmp_(tmp, Nbytes);
sapi::v::Array<float> tmp2_(tmp2, Nbytes);
float wrk[2 * Nbytes + 15 * sizeof(float)];
sapi::v::Array<float> wrk_(wrk, 2 * Nbytes + 15 * sizeof(float));
float ref_max = 0;
int k;
Nfloat = (cplx ? N * 2 : N);
float X[Nbytes], Y[Nbytes], Z[Nbytes];
sapi::v::Array<float> X_(X, Nbytes), Y_(Y, Nbytes), Z_(Z, Nbytes);
double t0, t1, flops;
2020-08-17 19:21:19 +08:00
int max_iter = 5120000 / N * 4;
#ifdef __arm__
max_iter /= 4;
#endif
2020-08-17 19:20:56 +08:00
int iter;
for (k = 0; k < Nfloat; ++k) {
2020-08-17 19:21:19 +08:00
X[k] = 0;
2020-08-17 19:20:56 +08:00
}
// FFTPack benchmark
{
2020-08-17 19:21:19 +08:00
int max_iter_ =
max_iter / 4; // SIMD_SZ == 4 (returning value of pffft_simd_size())
2020-08-17 19:20:56 +08:00
if (max_iter_ == 0) max_iter_ = 1;
if (cplx) {
api.cffti(N, wrk_.PtrBoth()).IgnoreError();
} else {
api.rffti(N, wrk_.PtrBoth()).IgnoreError();
}
2020-08-17 19:21:19 +08:00
t0 = uclock_sec();
2020-08-17 19:20:56 +08:00
for (iter = 0; iter < max_iter_; ++iter) {
if (cplx) {
api.cfftf(N, X_.PtrBoth(), wrk_.PtrBoth()).IgnoreError();
api.cfftb(N, X_.PtrBoth(), wrk_.PtrBoth()).IgnoreError();
} else {
api.rfftf(N, X_.PtrBoth(), wrk_.PtrBoth()).IgnoreError();
api.rfftb(N, X_.PtrBoth(), wrk_.PtrBoth()).IgnoreError();
}
}
t1 = uclock_sec();
2020-08-17 19:21:19 +08:00
flops = (max_iter_ * 2) * ((cplx ? 5 : 2.5) * N * log((double)N) / M_LN2);
show_output("FFTPack", N, cplx, flops, t0, t1, max_iter_);
2020-08-17 19:20:56 +08:00
}
}
return 0;
}