feat: nasty comment

This commit is contained in:
2025-10-18 12:18:06 +09:00
parent 6e74373d44
commit cdcdcd1b93

View File

@@ -1,17 +1,14 @@
#include <vector> #include <algorithm>
#include <fstream>
#include <sstream>
#include <iostream>
#include <cuda_runtime.h>
#include <algorithm>
#include <chrono> #include <chrono>
#include <cuda_runtime.h>
#include <fstream>
#include <iostream>
#include <sstream>
#include <vector>
__global__ void find_nearest_B( __global__ void find_nearest_B(const float3 *__restrict__ A,
const float3 *__restrict__ A, const float3 *__restrict__ B, int *nearest_idx,
const float3 *__restrict__ B, int N, int M) {
int *nearest_idx,
int N, int M)
{
int idx = blockDim.x * blockIdx.x + threadIdx.x; int idx = blockDim.x * blockIdx.x + threadIdx.x;
if (idx >= N) if (idx >= N)
return; return;
@@ -20,15 +17,13 @@ __global__ void find_nearest_B(
float min_dist = 1e30f; float min_dist = 1e30f;
int min_j = -1; int min_j = -1;
for (int j = 0; j < M; ++j) for (int j = 0; j < M; ++j) {
{
float dx = a.x - B[j].x; float dx = a.x - B[j].x;
float dy = a.y - B[j].y; float dy = a.y - B[j].y;
float dz = a.z - B[j].z; float dz = a.z - B[j].z;
float dist = dx * dx + dy * dy + dz * dz; float dist = dx * dx + dy * dy + dz * dz;
if (dist < min_dist) if (dist < min_dist) {
{
min_dist = dist; min_dist = dist;
min_j = j; min_j = j;
} }
@@ -37,23 +32,19 @@ __global__ void find_nearest_B(
nearest_idx[idx] = min_j; nearest_idx[idx] = min_j;
} }
std::vector<float3> load_coords_from_file(const std::string &filename) std::vector<float3> load_coords_from_file(const std::string &filename) {
{
std::vector<float3> coords; std::vector<float3> coords;
std::ifstream file(filename); std::ifstream file(filename);
if (!file) if (!file) {
{
std::cerr << "Unable to open file: " << filename << std::endl; std::cerr << "Unable to open file: " << filename << std::endl;
return coords; return coords;
} }
std::string line; std::string line;
while (std::getline(file, line)) while (std::getline(file, line)) {
{
std::istringstream iss(line); std::istringstream iss(line);
float x, y, z; float x, y, z;
if (iss >> x >> y >> z) if (iss >> x >> y >> z) {
{
coords.push_back(make_float3(x, y, z)); coords.push_back(make_float3(x, y, z));
} }
} }
@@ -64,10 +55,8 @@ std::vector<float3> load_coords_from_file(const std::string &filename)
void save_results_sorted(const std::string &filename, void save_results_sorted(const std::string &filename,
const std::vector<float3> &h_A, const std::vector<float3> &h_A,
const std::vector<float3> &h_B, const std::vector<float3> &h_B,
const std::vector<int> &indices) const std::vector<int> &indices) {
{ struct Entry {
struct Entry
{
float ax, az; float ax, az;
float bx, bz; float bx, bz;
float dist; float dist;
@@ -75,8 +64,7 @@ void save_results_sorted(const std::string &filename,
std::vector<Entry> entries; std::vector<Entry> entries;
for (size_t i = 0; i < indices.size(); ++i) for (size_t i = 0; i < indices.size(); ++i) {
{
float3 a = h_A[i]; float3 a = h_A[i];
float3 b = h_B[indices[i]]; float3 b = h_B[indices[i]];
@@ -88,20 +76,17 @@ void save_results_sorted(const std::string &filename,
entries.push_back({a.x, a.z, b.x, b.z, dist}); entries.push_back({a.x, a.z, b.x, b.z, dist});
} }
std::sort(entries.begin(), entries.end(), [](const Entry &e1, const Entry &e2) std::sort(entries.begin(), entries.end(),
{ return e1.dist < e2.dist; }); [](const Entry &e1, const Entry &e2) { return e1.dist < e2.dist; });
std::ofstream file(filename); std::ofstream file(filename);
for (const auto &e : entries) for (const auto &e : entries) {
{ file << e.ax << " " << e.az << " " << e.bx << " " << e.bz << " " << e.dist
file << e.ax << " " << e.az << " " << std::endl;
<< e.bx << " " << e.bz << " "
<< e.dist << std::endl;
} }
} }
int main() int main() {
{
auto t_start = std::chrono::high_resolution_clock::now(); auto t_start = std::chrono::high_resolution_clock::now();
std::vector<float3> h_A = load_coords_from_file("data/cities.txt"); std::vector<float3> h_A = load_coords_from_file("data/cities.txt");
@@ -110,8 +95,7 @@ int main()
int N = h_A.size(); int N = h_A.size();
int M = h_B.size(); int M = h_B.size();
if (N == 0 || M == 0) if (N == 0 || M == 0) {
{
std::cerr << "Coords empty." << std::endl; std::cerr << "Coords empty." << std::endl;
return 1; return 1;
} }
@@ -129,7 +113,6 @@ int main()
int threads = 256; int threads = 256;
int blocks = (N + threads - 1) / threads; int blocks = (N + threads - 1) / threads;
// ✅ CUDA 커널 시간 측정 시작
cudaEvent_t start, stop; cudaEvent_t start, stop;
cudaEventCreate(&start); cudaEventCreate(&start);
cudaEventCreate(&stop); cudaEventCreate(&stop);
@@ -144,7 +127,8 @@ int main()
std::cout << "CUDA kernel time: " << milliseconds << " ms" << std::endl; std::cout << "CUDA kernel time: " << milliseconds << " ms" << std::endl;
std::vector<int> h_nearest_idx(N); std::vector<int> h_nearest_idx(N);
cudaMemcpy(h_nearest_idx.data(), d_nearest_idx, sizeof(int) * N, cudaMemcpyDeviceToHost); cudaMemcpy(h_nearest_idx.data(), d_nearest_idx, sizeof(int) * N,
cudaMemcpyDeviceToHost);
save_results_sorted("output.txt", h_A, h_B, h_nearest_idx); save_results_sorted("output.txt", h_A, h_B, h_nearest_idx);