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