-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathidft_mt.c
More file actions
executable file
·92 lines (79 loc) · 1.99 KB
/
idft_mt.c
File metadata and controls
executable file
·92 lines (79 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include "performanceNow.h"
#include "readFloatFromInt16.h"
#include "dft.h"
#include "writeFloat.h"
typedef struct {
int threadid;
int nthread;
int len;
float* src;
float* re;
float* im;
} thread_t;
void* thread_func(void* arg) {
thread_t* d = (thread_t*)arg;
int id = d->threadid;
int len = d->len;
int nthread = d->nthread;
int start = id * len / nthread;
int end = id == nthread - 1 ? len : (id + 1) * len / nthread;
for (int i = start; i < end; i++) {
dft1(d->src, len, d->re, d->im, i);
}
return NULL;
}
int check(int nthread, float* src, size_t len, float* re, float* im) {
double now = performanceNow();
clock_t nowc = clock();
pthread_t threads[nthread];
thread_t data[nthread];
for (int i = 0; i < nthread; i++) {
data[i].threadid = i;
data[i].nthread = nthread;
data[i].len = len;
data[i].src = src;
data[i].re = re;
data[i].im = im;
pthread_create(&threads[i], NULL, thread_func, &data[i]);
}
for (int i = 0; i < nthread; i++) {
pthread_join(threads[i], NULL);
}
double dt = performanceNow() - now;
double dtc = (double)(clock() - nowc) / CLOCKS_PER_SEC;
printf("%d,", nthread);
printf("%f,", dt);
printf("%f\n", dtc);
return 0;
}
int main(void) {
int benchmark = 0;
const char* fn = "sekaideichiban.wav-r.i16.bin";
const char* fnre = "sekaideichiban.wav-re.f32.bin";
const char* fnim = "sekaideichiban.wav-im.f32.bin";
size_t len = 0;
float* src = readFloatFromInt16(fn, &len);
if (!src) {
return 1;
}
printf("len: %zu\n", len);
float* re = (float*)malloc(len * sizeof(float));
float* im = (float*)malloc(len * sizeof(float));
printf("nthread,time,cputime\n");
if (benchmark) {
for (int i = 1; i <= 20; i++) {
check(i, src, len, re, im);
}
} else {
check(6, src, len, re, im); // for M1 Pro
}
writeFloat(fnre, re, len);
writeFloat(fnim, im, len);
free(re);
free(im);
free(src);
return 0;
}