Skip to content

Commit a9162c4

Browse files
authored
Add DFT planner testing. (#653)
Convert DFT benchmarking utility to C++.
1 parent a07f18d commit a9162c4

File tree

8 files changed

+453
-165
lines changed

8 files changed

+453
-165
lines changed

include/sleefdft.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ IMPORT void SleefDFT_float_execute(struct SleefDFT *ptr, const float *in, float
4949
IMPORT void SleefDFT_dispose(struct SleefDFT *ptr);
5050

5151
IMPORT void SleefDFT_setPath(struct SleefDFT *ptr, char *pathStr);
52+
IMPORT int SleefDFT_getPath(struct SleefDFT *ptr, char *pathStr, int pathStrSize);
5253

5354
//
5455

src/dft-tester/CMakeLists.txt

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -81,24 +81,34 @@ if((NOT MSVC) AND NOT SLEEF_CLANG_ON_WINDOWS)
8181
target_compile_definitions(${TARGET_MEASUREDFT} PRIVATE ${COMMON_TARGET_DEFINITIONS})
8282
target_link_libraries(${TARGET_MEASUREDFT} ${COMMON_LINK_LIBRARIES})
8383
set_target_properties(${TARGET_MEASUREDFT} PROPERTIES ${COMMON_TARGET_PROPERTIES})
84-
85-
# Target executable bench1dst
86-
set(TARGET_BENCH1DST "bench1dst")
87-
add_executable(${TARGET_BENCH1DST} bench1d.c ${PROJECT_SOURCE_DIR}/include/sleefdft.h)
88-
add_dependencies(${TARGET_BENCH1DST} ${TARGET_HEADERS} ${TARGET_LIBSLEEF} ${TARGET_LIBDFT})
89-
target_compile_definitions(${TARGET_BENCH1DST} PRIVATE ${COMMON_TARGET_DEFINITIONS})
90-
target_link_libraries(${TARGET_BENCH1DST} ${COMMON_LINK_LIBRARIES})
91-
set_target_properties(${TARGET_BENCH1DST} PROPERTIES ${COMMON_TARGET_PROPERTIES})
92-
93-
# Target executable bench1dmt
94-
set(TARGET_BENCH1DMT "bench1dmt")
95-
add_executable(${TARGET_BENCH1DMT} bench1d.c ${PROJECT_SOURCE_DIR}/include/sleefdft.h)
96-
add_dependencies(${TARGET_BENCH1DMT} ${TARGET_HEADERS} ${TARGET_LIBSLEEF} ${TARGET_LIBDFT})
97-
target_compile_definitions(${TARGET_BENCH1DMT} PRIVATE ${COMMON_TARGET_DEFINITIONS} MULTITHREAD=1)
98-
target_link_libraries(${TARGET_BENCH1DMT} ${COMMON_LINK_LIBRARIES})
99-
set_target_properties(${TARGET_BENCH1DMT} PROPERTIES ${COMMON_TARGET_PROPERTIES})
10084
endif()
10185

86+
# Target executable bench1dst
87+
set(TARGET_BENCH1DST "bench1dst")
88+
add_executable(${TARGET_BENCH1DST} bench1d.cpp ${PROJECT_SOURCE_DIR}/include/sleefdft.h)
89+
add_dependencies(${TARGET_BENCH1DST} ${TARGET_HEADERS} ${TARGET_LIBSLEEF} ${TARGET_LIBDFT})
90+
target_compile_definitions(${TARGET_BENCH1DST} PRIVATE ${COMMON_TARGET_DEFINITIONS})
91+
target_link_libraries(${TARGET_BENCH1DST} ${COMMON_LINK_LIBRARIES})
92+
set_target_properties(${TARGET_BENCH1DST} PROPERTIES ${COMMON_TARGET_PROPERTIES})
93+
94+
# Target executable bench1dmt
95+
set(TARGET_BENCH1DMT "bench1dmt")
96+
add_executable(${TARGET_BENCH1DMT} bench1d.cpp ${PROJECT_SOURCE_DIR}/include/sleefdft.h)
97+
add_dependencies(${TARGET_BENCH1DMT} ${TARGET_HEADERS} ${TARGET_LIBSLEEF} ${TARGET_LIBDFT})
98+
target_compile_definitions(${TARGET_BENCH1DMT} PRIVATE ${COMMON_TARGET_DEFINITIONS} MULTITHREAD=1)
99+
target_link_libraries(${TARGET_BENCH1DMT} ${COMMON_LINK_LIBRARIES})
100+
set_target_properties(${TARGET_BENCH1DMT} PROPERTIES ${COMMON_TARGET_PROPERTIES})
101+
102+
# Target executable test_dftplanner
103+
set(TARGET_TEST_DFTPLANNER "test_dftplanner")
104+
add_executable(${TARGET_TEST_DFTPLANNER} test_dftplanner.cpp ${PROJECT_SOURCE_DIR}/include/sleefdft.h)
105+
add_dependencies(${TARGET_TEST_DFTPLANNER} ${TARGET_HEADERS} ${TARGET_LIBSLEEF} ${TARGET_LIBDFT})
106+
target_compile_definitions(${TARGET_TEST_DFTPLANNER} PRIVATE ${COMMON_TARGET_DEFINITIONS})
107+
target_link_libraries(${TARGET_TEST_DFTPLANNER} ${COMMON_LINK_LIBRARIES})
108+
set_target_properties(${TARGET_TEST_DFTPLANNER} PROPERTIES ${COMMON_TARGET_PROPERTIES})
109+
add_test(NAME ${TARGET_TEST_DFTPLANNER} COMMAND $<TARGET_FILE:${TARGET_TEST_DFTPLANNER}> ${PROJECT_BINARY_DIR}/test.plan)
110+
set_tests_properties(${TARGET_TEST_DFTPLANNER} PROPERTIES COST 2)
111+
102112
# Target executable roundtriptest1ddp
103113
set(TARGET_ROUNDTRIPTEST1DDP "roundtriptest1ddp")
104114
add_executable(${TARGET_ROUNDTRIPTEST1DDP} roundtriptest1d.c ${PROJECT_SOURCE_DIR}/include/sleefdft.h)
@@ -188,15 +198,15 @@ if (LIBFFTW3 AND NOT SLEEF_DISABLE_FFTW)
188198

189199
# Target executable bench1dfftwst
190200
set(TARGET_BENCH1DFFTWST "bench1dfftwst")
191-
add_executable(${TARGET_BENCH1DFFTWST} bench1d.c ${PROJECT_SOURCE_DIR}/include/sleefdft.h)
201+
add_executable(${TARGET_BENCH1DFFTWST} bench1d.cpp ${PROJECT_SOURCE_DIR}/include/sleefdft.h)
192202
target_compile_definitions(${TARGET_BENCH1DFFTWST} PRIVATE ${COMMON_TARGET_DEFINITIONS} USEFFTW=1)
193203
target_link_libraries(${TARGET_BENCH1DFFTWST} ${COMMON_LINK_LIBRARIES} ${LIBFFTW3})
194204
set_target_properties(${TARGET_BENCH1DFFTWST} PROPERTIES ${COMMON_TARGET_PROPERTIES})
195205

196206
if (FALSE)
197207
# Target executable bench1dfftwmt
198208
set(TARGET_BENCH1DFFTWMT "bench1dfftwmt")
199-
add_executable(${TARGET_BENCH1DFFTWMT} bench1d.c ${PROJECT_SOURCE_DIR}/include/sleefdft.h)
209+
add_executable(${TARGET_BENCH1DFFTWMT} bench1d.cpp ${PROJECT_SOURCE_DIR}/include/sleefdft.h)
200210
target_compile_definitions(${TARGET_BENCH1DFFTWMT} PRIVATE ${COMMON_TARGET_DEFINITIONS} USEFFTW=1 MULTITHREAD=1)
201211
target_link_libraries(${TARGET_BENCH1DFFTWMT} ${COMMON_LINK_LIBRARIES} ${LIBFFTW3})
202212
set_target_properties(${TARGET_BENCH1DFFTWMT} PROPERTIES ${COMMON_TARGET_PROPERTIES})
Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,15 @@
33
// (See accompanying file LICENSE.txt or copy at
44
// http://www.boost.org/LICENSE_1_0.txt)
55

6-
#define _DEFAULT_SOURCE
7-
#define _XOPEN_SOURCE 700
8-
9-
#include <stdio.h>
10-
#include <stdlib.h>
11-
#include <stdint.h>
12-
#include <assert.h>
13-
#include <math.h>
6+
#include <cstdio>
7+
#include <cstdlib>
8+
#include <cstdint>
9+
#include <cstring>
10+
#include <cassert>
11+
#include <cmath>
1412
#include <complex.h>
15-
#include <time.h>
16-
#include <unistd.h>
17-
#include <sys/time.h>
13+
#include <ctime>
14+
#include <chrono>
1815

1916
#ifdef USEFFTW
2017
#include <fftw3.h>
@@ -27,16 +24,19 @@
2724
typedef double real;
2825

2926
static uint64_t gettime() {
30-
struct timespec tp;
31-
clock_gettime(CLOCK_MONOTONIC, &tp);
32-
return (uint64_t)tp.tv_sec * 1000000000 + ((uint64_t)tp.tv_nsec);
27+
return std::chrono::duration_cast<std::chrono::nanoseconds>
28+
(std::chrono::system_clock::now() - std::chrono::system_clock::from_time_t(0)).count();
3329
}
3430

3531
#define REPEAT 8
3632

3733
int main(int argc, char **argv) {
3834
if (argc == 1) {
35+
#ifdef USEFFTW
3936
fprintf(stderr, "%s <log2n>\n", argv[0]);
37+
#else
38+
fprintf(stderr, "%s <log2n> <plan file name> <plan string>\n", argv[0]);
39+
#endif
4040
exit(-1);
4141
}
4242

@@ -49,7 +49,7 @@ int main(int argc, char **argv) {
4949
}
5050

5151
const int n = 1 << log2n;
52-
const int64_t niter = (int)(100000000000.0 / n / log2n);
52+
const int64_t niter = (int64_t)(100000000000.0 / n / log2n);
5353

5454
printf("Number of iterations = %lld\n", (long long int)niter);
5555

@@ -66,12 +66,16 @@ int main(int argc, char **argv) {
6666
//fftw_plan w = fftw_plan_dft_1d(n, in, out, backward ? FFTW_BACKWARD : FFTW_FORWARD, FFTW_PATIENT);
6767

6868
for(int i=0;i<n;i++) {
69-
in[i] = (2.0 * (rand() / (double)RAND_MAX) - 1) + (2.0 * (rand() / (double)RAND_MAX) - 1) * _Complex_I;
69+
auto a = (2.0 * (rand() / (double)RAND_MAX) - 1) + (2.0 * (rand() / (double)RAND_MAX) - 1) * _Complex_I;
70+
in[i][0] = creal(a);
71+
in[i][1] = cimag(a);
7072
}
7173

7274
for(int64_t i=0;i<niter/2;i++) fftw_execute(w);
7375
#else
74-
SleefDFT_setPlanFilePath(NULL, NULL, SLEEF_PLAN_RESET);
76+
const char *planfn = (argc < 3 || strcmp(argv[2], "-") == 0) ? NULL : argv[2];
77+
78+
SleefDFT_setPlanFilePath(planfn, NULL, SLEEF_PLAN_AUTOMATIC);
7579

7680
real *in = (real *)Sleef_malloc(n*2 * sizeof(real));
7781
real *out = (real *)Sleef_malloc(n*2 * sizeof(real));
@@ -81,12 +85,12 @@ int main(int argc, char **argv) {
8185
#else
8286
int mode = SLEEF_MODE_MEASURE | SLEEF_MODE_VERBOSE | SLEEF_MODE_NO_MT;
8387
#endif
84-
if (argc >= 3) mode = SLEEF_MODE_VERBOSE | SLEEF_MODE_ESTIMATE;
88+
if (argc >= 4) mode = SLEEF_MODE_VERBOSE | SLEEF_MODE_ESTIMATE;
8589

8690
if (backward) mode |= SLEEF_MODE_BACKWARD;
8791
struct SleefDFT *p = SleefDFT_double_init1d(n, in, out, mode);
8892

89-
if (argc >= 3) SleefDFT_setPath(p, argv[2]);
93+
if (argc >= 4) SleefDFT_setPath(p, argv[3]);
9094

9195
for(int i=0;i<n*2;i++) {
9296
in[i] = (2.0 * (rand() / (double)RAND_MAX) - 1);

src/dft-tester/test_dftplanner.cpp

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
// Copyright Naoki Shibata and contributors 2010 - 2025.
2+
// Distributed under the Boost Software License, Version 1.0.
3+
// (See accompanying file LICENSE.txt or copy at
4+
// http://www.boost.org/LICENSE_1_0.txt)
5+
6+
#include <iostream>
7+
#include <vector>
8+
9+
#include <cstdio>
10+
#include <cstdlib>
11+
#include <cstdint>
12+
#include <cstring>
13+
#include <cmath>
14+
15+
#include "sleef.h"
16+
#include "sleefdft.h"
17+
18+
using namespace std;
19+
20+
int main(int argc, char **argv) {
21+
if (argc == 1) {
22+
fprintf(stderr, "%s <plan file name>\n", argv[0]);
23+
exit(-1);
24+
}
25+
26+
SleefDFT_setPlanFilePath(argv[1], NULL, SLEEF_PLAN_AUTOMATIC);
27+
28+
double *din = (double *)Sleef_malloc(1024*2 * sizeof(double));
29+
double *dout = (double *)Sleef_malloc(1024*2 * sizeof(double));
30+
31+
float *fin = (float *)Sleef_malloc(1024*2 * sizeof(double));
32+
float *fout = (float *)Sleef_malloc(1024*2 * sizeof(double));
33+
34+
#ifdef MULTITHREAD
35+
int mode = SLEEF_MODE_MEASURE | SLEEF_MODE_VERBOSE;
36+
#else
37+
int mode = SLEEF_MODE_MEASURE | SLEEF_MODE_VERBOSE | SLEEF_MODE_NO_MT;
38+
#endif
39+
40+
SleefDFT *p;
41+
42+
//
43+
44+
p = SleefDFT_double_init1d(1024, din, dout, mode);
45+
46+
vector<char> pathd1024(1024);
47+
SleefDFT_getPath(p, pathd1024.data(), pathd1024.size());
48+
49+
cout << "Path (1024) : " << pathd1024.data() << endl;
50+
51+
SleefDFT_dispose(p);
52+
53+
//
54+
55+
p = SleefDFT_double_init1d(512, din, dout, mode);
56+
57+
vector<char> pathd512(512);
58+
SleefDFT_getPath(p, pathd512.data(), pathd512.size());
59+
60+
cout << "Path (512) : " << pathd512.data() << endl;
61+
62+
SleefDFT_dispose(p);
63+
64+
//
65+
66+
p = SleefDFT_float_init1d(1024, fin, fout, mode);
67+
68+
vector<char> pathf1024(1024);
69+
SleefDFT_getPath(p, pathf1024.data(), pathf1024.size());
70+
71+
cout << "Path (1024) : " << pathf1024.data() << endl;
72+
73+
SleefDFT_dispose(p);
74+
75+
//
76+
77+
p = SleefDFT_float_init1d(512, fin, fout, mode);
78+
79+
vector<char> pathf512(512);
80+
SleefDFT_getPath(p, pathf512.data(), pathf512.size());
81+
82+
cout << "Path (512) : " << pathf512.data() << endl;
83+
84+
SleefDFT_dispose(p);
85+
86+
//
87+
88+
p = SleefDFT_double_init1d(1024, din, dout, mode);
89+
90+
vector<char> pathd1024_2(1024);
91+
SleefDFT_getPath(p, pathd1024_2.data(), pathd1024_2.size());
92+
93+
cout << "Path2 (1024) : " << pathd1024_2.data() << endl;
94+
95+
SleefDFT_dispose(p);
96+
97+
if (pathd1024.size() != pathd1024_2.size() || memcmp(pathd1024.data(), pathd1024_2.data(), pathd1024.size()) != 0) {
98+
cerr << "Paths do not match" << endl;
99+
exit(-1);
100+
}
101+
102+
//
103+
104+
p = SleefDFT_double_init1d(512, din, dout, mode);
105+
106+
vector<char> pathd512_2(512);
107+
SleefDFT_getPath(p, pathd512_2.data(), pathd512_2.size());
108+
109+
cout << "Path2 (512) : " << pathd512_2.data() << endl;
110+
111+
SleefDFT_dispose(p);
112+
113+
if (pathd512.size() != pathd512_2.size() || memcmp(pathd512.data(), pathd512_2.data(), pathd512.size()) != 0) {
114+
cerr << "Paths do not match" << endl;
115+
exit(-1);
116+
}
117+
118+
//
119+
120+
p = SleefDFT_float_init1d(1024, fin, fout, mode);
121+
122+
vector<char> pathf1024_2(1024);
123+
SleefDFT_getPath(p, pathf1024_2.data(), pathf1024_2.size());
124+
125+
cout << "Path2 (1024) : " << pathf1024_2.data() << endl;
126+
127+
SleefDFT_dispose(p);
128+
129+
if (pathf1024.size() != pathf1024_2.size() || memcmp(pathf1024.data(), pathf1024_2.data(), pathf1024.size()) != 0) {
130+
cerr << "Paths do not match" << endl;
131+
exit(-1);
132+
}
133+
134+
//
135+
136+
p = SleefDFT_float_init1d(512, fin, fout, mode);
137+
138+
vector<char> pathf512_2(512);
139+
SleefDFT_getPath(p, pathf512_2.data(), pathf512_2.size());
140+
141+
cout << "Path2 (512) : " << pathf512_2.data() << endl;
142+
143+
SleefDFT_dispose(p);
144+
145+
if (pathf512.size() != pathf512_2.size() || memcmp(pathf512.data(), pathf512_2.data(), pathf512.size()) != 0) {
146+
cerr << "Paths do not match" << endl;
147+
exit(-1);
148+
}
149+
150+
//
151+
152+
Sleef_free(din);
153+
Sleef_free(dout);
154+
Sleef_free(fin);
155+
Sleef_free(fout);
156+
157+
cerr << "OK" << endl;
158+
159+
exit(0);
160+
}

0 commit comments

Comments
 (0)