-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcpuWolaDll_choosethreads.c
212 lines (157 loc) · 6.56 KB
/
cpuWolaDll_choosethreads.c
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include <math.h>
#include "stdio.h"
#include <stdlib.h>
#include "fftw3.h"
#include <string.h>
#include <ipp.h>
// #include <pthread.h>
#include <windows.h>
#include <process.h>
// #define NUM_THREADS 22
#define DLL_EXPORT __declspec(dllexport)
#ifdef __cplusplus
extern "C" {
#endif
// definition of thread data
struct thread_data{
int thread_t_ID;
fftw_complex *thread_y;
double *thread_f_tap;
int thread_L;
int thread_N;
int thread_Dec;
int thread_nprimePts;
fftw_complex *thread_fin;
fftw_complex *thread_fout;
Ipp64fc *thread_tones;
fftw_complex *thread_out;
int thread_NUM_THREADS;
fftw_plan *thread_allplans;
};
// // declare global thread stuff
// struct thread_data thread_data_array[NUM_THREADS];
// // test fftw_plans array on stack for threads, works
// fftw_plan allplans[NUM_THREADS]; // REMEMBER TO CHECK FFTW PLANS CREATION IN THE ENTRY FUNCTION
unsigned __stdcall threaded_wola(void *pArgs){
// void *threaded_wola(void *pArgs){
struct thread_data *inner_data;
inner_data = (struct thread_data *)pArgs;
int t_ID = inner_data->thread_t_ID;
fftw_complex *y = inner_data->thread_y;
int L = inner_data->thread_L;
int N = inner_data->thread_N;
int Dec = inner_data->thread_Dec;
int nprimePts = inner_data->thread_nprimePts;
double *f_tap = inner_data->thread_f_tap;
fftw_complex *fin = inner_data->thread_fin;
fftw_complex *fout = inner_data->thread_fout;
Ipp64fc *tones = inner_data->thread_tones;
fftw_complex *out = inner_data->thread_out; // for R2018
int NUM_THREADS = inner_data->thread_NUM_THREADS;
fftw_plan *allplans = inner_data->thread_allplans;
// end of assignments
int nprime, n, a, b; // declare to simulate threads later
int k;
int tone_idx;
// pick point based on thread number
for (nprime = t_ID; nprime<nprimePts; nprime=nprime+NUM_THREADS){
n = nprime*Dec;
for (a = 0; a<N; a++){
fin[a][0] = 0; // init to 0
fin[a][1] = 0;
for (b = 0; b<L/N; b++){
if (n - (b*N+a) >= 0){
fin[a][0] = fin[a][0] + y[n-(b*N+a)][0] * f_tap[b*N+a];
fin[a][1] = fin[a][1] + y[n-(b*N+a)][1] * f_tap[b*N+a];
} // fin is fftw_complex
}
}
fftw_execute(allplans[t_ID]); // this should place them into another fftw_complex fout
// // === old code for up to bin overlap of 2 ===
// if (Dec*2 == N && nprime % 2 != 0){ // only if using overlapping channels, do some phase corrections when nprime is odd
// for (k=1; k<N; k=k+2){ // all even k are definitely even in the product anyway
// fout[k][0] = -fout[k][0];
// fout[k][1] = -fout[k][1];
// }
// }
// memcpy(&out[nprime*N],fout,sizeof(fftw_complex)*N);
// === new code for general bin overlaps ===
tone_idx = nprime % (N/Dec);
ippsMul_64fc((Ipp64fc*)fout, &tones[tone_idx * N], (Ipp64fc*)&out[nprime*N], N);
}
_endthreadex(0);
return 0;
}
extern DLL_EXPORT int cpuWola(fftw_complex *y, double *f_tap, int fftlen, int Dec, int nprimePts, int L, fftw_complex *out, int NUM_THREADS){
// int cpuWola(fftw_complex *y, double *f_tap, int fftlen, int Dec, int nprimePts, int L, fftw_complex *out){
ippInit();
Ipp64fc *tones;
Ipp64f phase = 0;
Ipp64f rFreq = 0;
fftw_complex *fin, *fout;
int i, t;
// declare thread stuff
struct thread_data *thread_data_array = (struct thread_data *)malloc(sizeof(struct thread_data)*NUM_THREADS);
// test fftw_plans array on stack for threads, works
fftw_plan *allplans = (fftw_plan*)malloc(sizeof(fftw_plan)*NUM_THREADS); // REMEMBER TO CHECK FFTW PLANS CREATION IN THE ENTRY FUNCTION
tones = ippsMalloc_64fc_L(fftlen/Dec * fftlen); // the tone is fftlen elements, and we need fftlen/Dec of them before the phase correction repeats
for (i=0; i<fftlen/Dec; i++){
rFreq = -(Ipp64f)i * (Ipp64f)Dec / (Ipp64f)fftlen;
if (rFreq<0){rFreq = rFreq + 1;} // basically other than the first one, all are negative, so shift to the positive equivalent
ippsTone_64fc(&tones[i*fftlen], fftlen, 1.0, rFreq, &phase, ippAlgHintNone);
}
fin = fftw_alloc_complex(fftlen*NUM_THREADS);
fout = fftw_alloc_complex(fftlen*NUM_THREADS);
allplans[0] = fftw_plan_dft_1d(fftlen, fin, fout, FFTW_BACKWARD, FFTW_ESTIMATE); // FFTW_MEASURE seems to cut execution time by ~10%, but FFTW_ESTIMATE takes ~0.001s whereas MEASURE takes ~0.375s
for (i=1;i<NUM_THREADS;i++){
allplans[i] = fftw_plan_dft_1d(fftlen, &fin[fftlen*i], &fout[fftlen*i], FFTW_BACKWARD, FFTW_ESTIMATE); // make the other plans, not executing them yet
}
// HANDLE ThreadList[NUM_THREADS]; // handles to threads
HANDLE *ThreadList = (HANDLE*)malloc(NUM_THREADS*sizeof(HANDLE));
// // stuff for pthreads
// pthread_t ThreadList[NUM_THREADS];
// pthread_attr_t attr;
// pthread_attr_init(&attr);
// pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
for (t=0; t<NUM_THREADS; t++){
thread_data_array[t].thread_t_ID = t;
thread_data_array[t].thread_f_tap = f_tap;
thread_data_array[t].thread_L = L;
thread_data_array[t].thread_N = fftlen;
thread_data_array[t].thread_Dec = Dec;
thread_data_array[t].thread_nprimePts = nprimePts;
thread_data_array[t].thread_y = y;
thread_data_array[t].thread_fin = &fin[t*fftlen];
thread_data_array[t].thread_fout = &fout[t*fftlen];
thread_data_array[t].thread_tones = tones;
thread_data_array[t].thread_out = out; // for R2018
thread_data_array[t].thread_NUM_THREADS = NUM_THREADS;
thread_data_array[t].thread_allplans = allplans;
// pthread_create(&ThreadList[t], &attr, threaded_wola, (void *)&thread_data_array[t]);
ThreadList[t] = (HANDLE)_beginthreadex(NULL,0,&threaded_wola,(void*)&thread_data_array[t],0,NULL);
// printf("Beginning threadID %i..\n",thread_data_array[t].thread_t_ID);
}
// for (i = 0; i < NUM_THREADS; i++) {
// if(pthread_join(ThreadList[i], NULL)) { // this essentially waits for all above threads
// fprintf(stderr, "Error joining threadn");
// return 2;
// }
// }
WaitForMultipleObjects(NUM_THREADS,ThreadList,1,INFINITE);
// ============== CLEANUP =================
// close threads
// printf("Closing threads...\n");
for(t=0;t<NUM_THREADS;t++){
CloseHandle(ThreadList[t]);
// printf("Closing threadID %i.. %i\n",(int)ThreadIDList[t],WaitForThread[t]);
}
printf("All threads closed! \n");
for (i=0;i<NUM_THREADS;i++){fftw_destroy_plan(allplans[i]);}
fftw_free(fin);
fftw_free(fout);
ippsFree(tones);
free(ThreadList);
free(allplans);
free(thread_data_array);
return 0;
}