-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmeanfilter.cpp
More file actions
executable file
·161 lines (142 loc) · 4.56 KB
/
meanfilter.cpp
File metadata and controls
executable file
·161 lines (142 loc) · 4.56 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
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
/*
* Copyright (c) 2014 - Keith Ha (keith4ever@gmail.com)
* All content herein is protected by U.S. copyright and other applicable intellectual property laws
* and may not be copied without the expressive permission of Keith Ha, who reserves all rights.
* Reuse of any of the content for any purpose without the permission of Keith Ha
* is strictly and expressively prohibited.
*/
#include "meanfilter.h"
#include "Meanfilter4D.h"
const float inputTensor[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14, 15, 16, 17, 18, 19};
Config::Config() {
m_bCPUCompute = false;
m_bPrintOut = false;
m_bDefaultDim = true;
m_dim[0] = D1DIM;
m_dim[1] = D2DIM;
m_dim[2] = D3DIM;
m_dim[3] = D4DIM;
}
Config::~Config() {
}
void Config::generateRandNum(int size, float* buf)
{
float* ptr = buf;
// __PerfTimerStart__
for(int i=0; i < size; i++)
ptr[i] = (rand() % (1<<10));
// __PerfTimerEnd__
}
bool Config::parseArguments(int argc, char **argv) {
for (int i = 1; i < argc; i++)
{
/* if (stricmp(argv[i], "-i") == 0)
{
if (++i >= argc)
{
fprintf(stderr, "invalid parameter for %s\n", argv[i - 1]);
return false;
}
inputDir = argv[i];
}
else*/
if (strcasecmp(argv[i], "-cpu") == 0)
{
m_bCPUCompute = true;
}
else if (strcasecmp(argv[i], "-p") == 0)
{
m_bPrintOut = true;
}
else if (strcasecmp(argv[i], "-d1") == 0)
{
int data = 0;
if (++i >= argc || sscanf(argv[i], "%d", &data) != 1)
{
fprintf(stderr, "invalid parameter for %s\n", argv[i - 1]);
return false;
}
m_dim[0] = data; m_bDefaultDim = false;
}
else if (strcasecmp(argv[i], "-d2") == 0)
{
int data = 0;
if (++i >= argc || sscanf(argv[i], "%d", &data) != 1)
{
fprintf(stderr, "invalid parameter for %s\n", argv[i - 1]);
return false;
}
m_dim[1] = data; m_bDefaultDim = false;
}
else if (strcasecmp(argv[i], "-d3") == 0)
{
int data = 0;
if (++i >= argc || sscanf(argv[i], "%d", &data) != 1)
{
fprintf(stderr, "invalid parameter for %s\n", argv[i - 1]);
return false;
}
m_dim[2] = data; m_bDefaultDim = false;
}
else if (strcasecmp(argv[i], "-d4") == 0)
{
int data = 0;
if (++i >= argc || sscanf(argv[i], "%d", &data) != 1)
{
fprintf(stderr, "invalid parameter for %s\n", argv[i - 1]);
return false;
}
m_dim[3] = data; m_bDefaultDim = false;
}
else
{
fprintf(stderr, "invalid parameter %s\n", argv[i++]);
return false;
}
}
return true;
}
void Config::checkFileAndRead(float* buf, int bufsize, char* file)
{
FILE* fp;
if (buf == NULL || file == NULL) return;
string filename(file);
fp = fopen(filename.c_str(), "r");
if (fp == NULL) {
cerr << file << " doesn't exist !" << endl;
exit(-1);
}
fseek(fp, 0L, SEEK_END);
size_t size = ftell(fp);
if(size < bufsize){
cerr << file << " size is less than " << bufsize << " !" << endl;
exit(-1);
}
fread(buf, 1, bufsize, fp);
fclose(fp);
}
int main(int argc, char* argv[]) {
auto pFilter = make_shared<Meanfilter4D>();
auto pConfig = make_shared<Config>();
pConfig->parseArguments(argc, argv);
int inBufferSize = pConfig->getDataSize();
float *pInputBuffer = new float[inBufferSize];
float *pOutputBuffer = new float[inBufferSize];
if (pConfig->m_bDefaultDim) {
memcpy(pInputBuffer, inputTensor, sizeof(float) * inBufferSize);
} else {
pConfig->generateRandNum(inBufferSize, pInputBuffer);
}
pFilter->init(pConfig->m_dim[0], pConfig->m_dim[1], pConfig->m_dim[2], pConfig->m_dim[3],
pConfig->isCPUCompute());
pFilter->execute(pInputBuffer, pOutputBuffer);
if(pConfig->m_bPrintOut)
pFilter->printOut(pOutputBuffer);
pFilter->deinit();
delete [] pInputBuffer;
delete [] pOutputBuffer;
return 0;
}