-
Notifications
You must be signed in to change notification settings - Fork 11
/
ocl_convolution.c
70 lines (60 loc) · 2.02 KB
/
ocl_convolution.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
// ©2017-2018 Yuichiro Nakada
// clang -Os ocl_convolution.c -o ocl_convolution `pkg-config --libs --cflags OpenCL`
// clang -Os ocl_convolution.c -o ocl_convolution -framework opencl
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include "ocl.h"
char kernel_code[] =
#include "ocl_convolution.cl"
const unsigned int inputSignalWidth = 8;
const unsigned int inputSignalHeight = 8;
unsigned int inputSignal[inputSignalWidth][inputSignalHeight] =
{
{3, 1, 1, 4, 8, 2, 1, 3},
{4, 2, 1, 1, 2, 1, 2, 3},
{4, 4, 4, 4, 3, 2, 2, 2},
{9, 8, 3, 8, 9, 0, 0, 0},
{9, 3, 3, 9, 0, 0, 0, 0},
{0, 9, 0, 8, 0, 0, 0, 0},
{3, 0, 8, 8, 9, 4, 4, 4},
{5, 9, 8, 1, 8, 1, 1, 1}
};
const unsigned int outputSignalWidth = 6;
const unsigned int outputSignalHeight = 6;
unsigned int outputSignal[outputSignalWidth][outputSignalHeight];
const unsigned int maskWidth = 3;
const unsigned int maskHeight = 3;
unsigned int mask[maskWidth][maskHeight] =
{
{1, 1, 1}, {1, 0, 1}, {1, 1, 1},
};
args_t args[] = {
{ CL_MEM_READ_ONLY, sizeof(unsigned int) * inputSignalHeight * inputSignalWidth, 0, inputSignal, OCL_WRITE },
{ CL_MEM_READ_ONLY, sizeof(unsigned int) * maskHeight * maskWidth, 0, mask, OCL_WRITE },
{ CL_MEM_WRITE_ONLY, sizeof(unsigned int) * outputSignalHeight * outputSignalWidth, 0, outputSignal, OCL_READ },
{ 0, sizeof(unsigned int), 0, (void*)&inputSignalWidth, 0 },
{ 0, sizeof(unsigned int), 0, (void*)&maskWidth, 0 },
{ 0, 0, 0, 0, 0 },
};
ocl_t kernel[] = {
{ "convolve", 0, 1,{outputSignalWidth * outputSignalHeight,0,0,},{1,0,0,}, args },
};
int ksz = sizeof(kernel)/sizeof(kernel[0]);
int main()
{
oclSetup(0, 0);
oclKernel(kernel, ksz, "-cl-denorms-are-zero -cl-finite-math-only -cl-fast-relaxed-math -Werror", kernel_code);
oclKernelArgs(kernel, ksz);
oclKernelArgsWrite(args);
oclRun(&kernel[0]);
oclKernelArgsRead(args);
oclReleaseKernel(kernel, ksz);
oclFinish();
for (int y = 0; y < outputSignalHeight; y++) {
for (int x = 0; x < outputSignalWidth; x++) {
printf("%d ", outputSignal[x][y]);
}
printf("\n");
}
}