Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
157 lines (128 loc) · 5.28 KB
/
Copy pathmain.cpp
File metadata and controls
157 lines (128 loc) · 5.28 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
#ifdef _WIN32
#define NOMINMAX
#endif
#include "OptixRenderer.h"
#include <chrono>
#include <cuda_runtime.h>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#define CUDA_CHECK(call) \
{ \
cudaError_t res = call; \
if (res != cudaSuccess) { \
std::cerr << "CUDA Error (Main): " << cudaGetErrorString(res) \
<< std::endl; \
exit(1); \
} \
}
bool loadOFF(const std::string &filename, std::vector<float3> &vertices,
std::vector<uint3> &indices) {
std::ifstream file(filename);
if (!file.is_open()) {
std::cerr << "Failed to open file: " << filename << std::endl;
return false;
}
std::string header;
file >> header;
if (header != "OFF") {
std::cerr << "Invalid .off file format." << std::endl;
return false;
}
int numVerts, numFaces, numEdges;
file >> numVerts >> numFaces >> numEdges;
vertices.resize(numVerts);
for (int i = 0; i < numVerts; ++i)
file >> vertices[i].x >> vertices[i].y >> vertices[i].z;
indices.resize(numFaces);
for (int i = 0; i < numFaces; ++i) {
int n;
file >> n >> indices[i].x >> indices[i].y >> indices[i].z;
}
return true;
}
void printTime(
const std::string &phase,
std::chrono::time_point<std::chrono::high_resolution_clock> start,
std::chrono::time_point<std::chrono::high_resolution_clock> end) {
std::chrono::duration<double, std::milli> duration = end - start;
std::cout << "[Profiling] " << phase << ": " << duration.count() << " ms"
<< std::endl;
}
int main(int argc, char **argv) {
try {
std::string off_filename = "cube.off";
if (argc > 1)
off_filename = argv[1];
// --- PHASE 0: WARMUP ---
auto t_warmup_start = std::chrono::high_resolution_clock::now();
CUDA_CHECK(cudaFree(0));
auto t_warmup_end = std::chrono::high_resolution_clock::now();
std::vector<float3> h_vertices;
std::vector<uint3> h_indices;
auto t0 = std::chrono::high_resolution_clock::now();
if (!loadOFF(off_filename, h_vertices, h_indices))
return -1;
auto t1 = std::chrono::high_resolution_clock::now();
unsigned int num_points = h_vertices.size();
// --- PHASE 1: GPU MEMORY ALLOCATION ---
float3 *d_vertices;
uint3 *d_indices;
unsigned int *d_results;
size_t vert_bytes = num_points * sizeof(float3);
size_t ind_bytes = h_indices.size() * sizeof(uint3);
size_t res_bytes = num_points * sizeof(unsigned int);
CUDA_CHECK(cudaMalloc(&d_vertices, vert_bytes));
CUDA_CHECK(cudaMemcpy(d_vertices, h_vertices.data(), vert_bytes,
cudaMemcpyHostToDevice));
CUDA_CHECK(cudaMalloc(&d_indices, ind_bytes));
CUDA_CHECK(cudaMemcpy(d_indices, h_indices.data(), ind_bytes,
cudaMemcpyHostToDevice));
CUDA_CHECK(cudaMalloc(&d_results, res_bytes));
// --- PHASE 2: OPTIX PIPELINE & BVH ---
auto t2 = std::chrono::high_resolution_clock::now();
OptixRenderer renderer;
auto t3 = std::chrono::high_resolution_clock::now();
auto t4 = std::chrono::high_resolution_clock::now();
renderer.buildGeometry(d_vertices, num_points, d_indices, h_indices.size());
auto t5 = std::chrono::high_resolution_clock::now();
// --- PHASE 3: LAUNCH RAYS ---
auto t6 = std::chrono::high_resolution_clock::now();
renderer.render(d_vertices, num_points, d_results);
auto t7 = std::chrono::high_resolution_clock::now();
// --- PHASE 4: DOWNLOAD & CLEANUP ---
std::vector<unsigned int> h_results(num_points);
CUDA_CHECK(cudaMemcpy(h_results.data(), d_results, res_bytes,
cudaMemcpyDeviceToHost));
CUDA_CHECK(cudaFree(d_vertices));
CUDA_CHECK(cudaFree(d_indices));
CUDA_CHECK(cudaFree(d_results));
// --- PRINT METRICS ---
std::cout << "\n--- PERFORMANCE METRICS ---\n";
printTime("GPU Warmup ", t_warmup_start, t_warmup_end);
printTime("OFF File I/O Load ", t0, t1);
printTime("OptiX Pipeline Init ", t2, t3);
printTime("BVH Mesh Build ", t4, t5);
printTime("Point Analysis ", t6, t7);
std::cout << "---------------------------\n";
// --- RESULT STATISTICS ---
unsigned int hits = 0;
for (unsigned int i = 0; i < num_points; ++i) {
if (h_results[i] == 1)
hits++;
}
std::cout << "\n--- SPATIAL ANALYSIS RESULTS ---\n";
std::cout << "Total points analyzed : " << num_points << "\n";
std::cout << "Points with hit (1) : " << hits << "\n";
std::cout << "Free line of sight (0): " << (num_points - hits) << "\n";
std::cout << "--------------------------------\n";
std::cout << "Sample (First 10 points):\n";
for (unsigned int i = 0; i < std::min(num_points, 10u); ++i) {
std::cout << "Point " << i << ": " << h_results[i] << "\n";
}
} catch (const std::exception &e) {
std::cerr << "Exception: " << e.what() << std::endl;
}
return 0;
}