-
Notifications
You must be signed in to change notification settings - Fork 0
/
hello_fft_2d.c
139 lines (119 loc) · 4.55 KB
/
hello_fft_2d.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
/*
BCM2835 "GPU_FFT" release 2.0
Copyright (c) 2014, Andrew Holme.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <string.h>
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include "mailbox.h"
#include "gpu_fft_trans.h"
#include "hello_fft_2d_bitmap.h"
#define log2_N 9
#define N (1<<log2_N)
#define GPU_FFT_ROW(fft, io, y) ((fft)->io+(fft)->step*(y))
#define timespec_diff_ns(x, y) \
((((x).tv_sec-(y).tv_sec)*1000000000LL)+((x).tv_nsec-(y).tv_nsec))
int main(int argc, char *argv[]) {
int x, y, ret, mb = mbox_open();
struct timespec t[4];
struct GPU_FFT_COMPLEX *row;
struct GPU_FFT_TRANS *trans;
struct GPU_FFT *fft_pass[2];
BITMAPFILEHEADER bfh;
BITMAPINFOHEADER bih;
// Create Windows bitmap file
FILE *fp = fopen("hello_fft_2d.bmp", "wb");
if (!fp) return -666;
// Write bitmap header
memset(&bfh, 0, sizeof(bfh));
bfh.bfType = 0x4D42; //"BM"
bfh.bfSize = N*N*3;
bfh.bfOffBits = sizeof(bfh) + sizeof(bih);
fwrite(&bfh, sizeof(bfh), 1, fp);
// Write bitmap info
memset(&bih, 0, sizeof(bih));
bih.biSize = sizeof(bih);
bih.biWidth = N;
bih.biHeight = N;
bih.biPlanes = 1;
bih.biBitCount = 24;
bih.biCompression = BI_RGB;
fwrite(&bih, sizeof(bih), 1, fp);
// Prepare 1st FFT pass
ret = gpu_fft_prepare(mb, log2_N, GPU_FFT_REV, N, &fft_pass[0]);
if (ret) {
return ret;
}
// Prepare 2nd FFT pass
ret = gpu_fft_prepare(mb, log2_N, GPU_FFT_REV, N, &fft_pass[1]);
if (ret) {
gpu_fft_release(fft_pass[0]);
return ret;
}
// Transpose from 1st pass output to 2nd pass input
ret = gpu_fft_trans_prepare(mb, fft_pass[0], fft_pass[1], &trans);
if (ret) {
gpu_fft_release(fft_pass[0]);
gpu_fft_release(fft_pass[1]);
return ret;
}
// Clear input array
for (y=0; y<N; y++) {
row = GPU_FFT_ROW(fft_pass[0], in, y);
for (x=0; x<N; x++) row[x].re = row[x].im = 0;
}
// Setup input data
GPU_FFT_ROW(fft_pass[0], in, 2)[ 2].re = 60;
GPU_FFT_ROW(fft_pass[0], in, N-2)[N-2].re = 60;
// ==> FFT() ==> T() ==> FFT() ==>
usleep(1); /* yield to OS */
clock_gettime(CLOCK_MONOTONIC, &t[0]);
gpu_fft_execute(fft_pass[0]);
clock_gettime(CLOCK_MONOTONIC, &t[1]);
gpu_fft_trans_execute(trans);
clock_gettime(CLOCK_MONOTONIC, &t[2]);
gpu_fft_execute(fft_pass[1]);
clock_gettime(CLOCK_MONOTONIC, &t[3]);
// Write output to bmp file
for (y=0; y<N; y++) {
row = GPU_FFT_ROW(fft_pass[1], out, y);
for (x=0; x<N; x++) {
fputc(128+row[x].re, fp); // blue
fputc(128+row[x].re, fp); // green
fputc(128+row[x].re, fp); // red
}
}
printf( "1st FFT %6lld usecs\n"
"Transpose %6lld usecs\n"
"2nd FFT %6lld usecs\n",
timespec_diff_ns(t[3], t[2])/1000,
timespec_diff_ns(t[2], t[1])/1000,
timespec_diff_ns(t[1], t[0])/1000);
// Clean-up properly. Videocore memory lost if not freed !
gpu_fft_release(fft_pass[0]);
gpu_fft_release(fft_pass[1]);
gpu_fft_trans_release(trans);
return 0;
}