-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
169 lines (137 loc) · 5.85 KB
/
main.cpp
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
// =============================================================================
// (c) LEMS, Brown University
// Chiang-Heng Chien ([email protected])
// June 2022
// =============================================================================
#include <cmath>
#include <fstream>
#include <iterator>
#include <iostream>
#include <string.h>
#include <vector>
#include <stdint.h>
#include "indices.hpp"
// cpu
#include "cpu_toed.hpp"
#include "cpu_toed.cpp"
// gpu
#include "gpu_toed.hpp"
// curvelet
#include "./curvelet/form_curvelet_main.hpp"
//------------------------------------------------------------------------------
// TODO: add CPU-GPU error checking code
template<typename T>
void initialize_TOED_edges( T* &TOED_edges, int height, int width )
{
TOED_edges = new T[(2*height)*(2*width)*4];
// initialization
for (int i = 0; i < (2*height)*(2*width); i++) {
for (int j = 0; j < 4; j++) {
TOED_edges(i, j) = 0;
}
}
}
//------------------------------------------------------------------------------
int main(int argc, char **argv)
{
// -- Exit if the input image file doesn't open --
std::string filename(argv[1]);
std::ifstream infile(filename, std::ios::binary);
if (!infile.is_open())
{
std::cout << "File " << filename << " not found in directory." << std::endl;
return 0;
}
char type[10];
int height, width, intensity;
// -- Storing header information and copying into the new ouput images --
infile >> type >> width >> height >> intensity;
// read number of threads if passed through command line
int nthreads = 1;
if(argc > 2) {
nthreads = atoi( argv[2] );
}
int gpu_id = 0;
if(argc > 3) {
gpu_id = atoi( argv[3] );
}
cudacheck( cudaSetDevice(gpu_id) );
// -- define parameters (This could be changed to argv input arguments but now let's make it fixed)
int kernel_size = 17;
int sigma = 2;
// ==================================== THIRD-ORDER EDGE DETECTOR STARTS HERE ===============================================
int edge_num;
if ( !Use_Double_Precision && !Use_Single_Precision ) {
std::cout << "You must choose either single or double precision in indices.hpp file!" << std::endl;
exit(1);
}
if (Use_Double_Precision) {
double *TOED_edges;
initialize_TOED_edges<double>( TOED_edges, height, width );
printf("############################################\n");
printf("## Double Precision Test ##\n");
printf("############################################\n");
printf("\n ==> CPU Test (OpenMP %d threads) \n", nthreads);
printf("============================================\n");
ThirdOrderEdgeDetectionCPU<double> toedCPU_fp64(height, width, sigma, kernel_size, nthreads);
toedCPU_fp64.preprocessing(infile);
toedCPU_fp64.convolve_img();
edge_num = toedCPU_fp64.non_maximum_suppresion(TOED_edges);
// go back to the beginning of the file
infile.clear();
infile.seekg(0);
infile >> type >> width >> height >> intensity;
printf("\n ==> GPU Test \n");
printf("=============================================\n");
ThirdOrderEdgeDetectionGPU<double> toedGPU_fp64(gpu_id, height, width, kernel_size, sigma); // -- class constructor --
toedGPU_fp64.preprocessing(infile); // -- preprocessing: array initialization --
toedGPU_fp64.convolve_img(); // -- convolve image with Gaussian derivative filter --
toedGPU_fp64.non_maximum_suppresion();
//> Double precision allows curve formation
#if CurvelFormation
// -- settings --
double nrad = 3.5;
double gap = 1.5;
double dx = 0.4;
double dt = 15;
double token_len = 1;
double max_k = 0.3;
unsigned cvlet_style = 3;
unsigned max_size_to_group = 7;
unsigned output_type = 0;
curvelet_formation( height, width, TOED_edges, edge_num, 4,
nrad, gap, dx, dt, token_len, max_k,
cvlet_style, max_size_to_group, output_type);
#endif
delete[] TOED_edges;
}
if (Use_Single_Precision) {
float *TOED_edges;
initialize_TOED_edges<float>( TOED_edges, height, width );
// go back to the beginning of the file
infile.clear();
infile.seekg(0);
infile >> type >> width >> height >> intensity;
printf("############################################\n");
printf("## Single Precision Test ##\n");
printf("############################################\n");
printf("\n ==> CPU Test (OpenMP %d threads) \n", nthreads);
printf("=============================================\n");
ThirdOrderEdgeDetectionCPU<float> toedCPU_fp32(height, width, sigma, kernel_size, nthreads);
toedCPU_fp32.preprocessing(infile);
toedCPU_fp32.convolve_img();
edge_num = toedCPU_fp32.non_maximum_suppresion(TOED_edges);
// go back to the beginning of the file
infile.clear();
infile.seekg(0);
infile >> type >> width >> height >> intensity;
printf("\n ==> GPU Test \n");
printf("=============================================\n");
ThirdOrderEdgeDetectionGPU<float> toedGPU_fp32(gpu_id, height, width, kernel_size, sigma); // -- class constructor --
toedGPU_fp32.preprocessing(infile); // -- preprocessing: array initialization --
toedGPU_fp32.convolve_img(); // -- convolve image with Gaussian derivative filter --
toedGPU_fp32.non_maximum_suppresion();
delete[] TOED_edges;
}
return 0;
}