算法工程师的第一个CUDA程序

1.Windows平台的点云处理CUDA算法,先写CMakeList

cmake_minimum_required(VERSION 3.18 FATAL_ERROR)
project(CUDA_PointCloudDemo LANGUAGES CXX CUDA)

# 关键修改:设置正确的计算能力(GTX 1660 Ti是7.5)
set(CMAKE_CUDA_ARCHITECTURES "75")  # 对应Turing架构

set(CMAKE_CUDA_COMPILER "C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.8/bin/nvcc.exe")
set(CUDA_TOOLKIT_ROOT_DIR "C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.8")

find_package(CUDA REQUIRED)  # 显式查找CUDA
include_directories(${CUDA_INCLUDE_DIRS})



# 启用更快的数学计算(牺牲少量精度)
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} --use_fast_math")

# 创建可执行文件
add_executable(${PROJECT_NAME}
    main.cu
)

# 链接CUDA库
target_link_libraries(${PROJECT_NAME} PRIVATE
     ${CUDA_LIBRARIES}  # 直接链接CUDA库
)

# 针对Windows的特定设置
if(MSVC)
    # 禁用特定警告
    target_compile_options(${PROJECT_NAME} PRIVATE
        "$<$<COMPILE_LANGUAGE:CUDA>:-Xcompiler=/wd4819>"  # 解决VS中文编码警告
        "$<$<COMPILE_LANGUAGE:CUDA>:-Xcompiler=/utf-8>"
    )
    
    # 启用GPU调试信息(Debug模式时)
    string(APPEND CMAKE_CUDA_FLAGS_DEBUG " -G")
endif()

2.代码实现文件需要由原来的C++ cpp后缀改为cu后缀,main入口函数形式一样的

#include <iostream>
#include <fstream>
#include <vector>
#include <cuda_runtime.h>

// CUDA核函数:对每个点进行坐标缩放
__global__ void scalePoints(float* points, int numPoints, float scale) {
    int idx = blockIdx.x * blockDim.x + threadIdx.x;
    if (idx < numPoints) {
        points[3*idx] *= scale;     // X坐标
        points[3*idx+1] *= scale;   // Y坐标
        points[3*idx+2] *= scale;   // Z坐标
    }
}

// 读取点云文件
bool readPointCloud(const char* filename, std::vector<float>& points) {
    std::ifstream file(filename);
    if (!file.is_open()) return false;

    float x, y, z;
    while (file >> x >> y >> z) {
        points.push_back(x);
        points.push_back(y);
        points.push_back(z);
    }
    file.close();
    return true;
}

// 保存点云文件
bool savePointCloud(const char* filename, const float* points, int numPoints) {
    std::ofstream file(filename);
    if (!file.is_open()) return false;

    for (int i = 0; i < numPoints; ++i) {
        file << points[3*i] << " " 
             << points[3*i+1] << " " 
             << points[3*i+2] << "\n";
    }
    file.close();
    return true;
}

int main() {
    // 读取点云数据
    std::vector<float> h_points;
    if (!readPointCloud("point_cloud.xyz", h_points)) {
        std::cerr << "Error reading point cloud file!" << std::endl;
        return 1;
    }
    int numPoints = h_points.size() / 3;

    // 分配设备内存
    float* d_points;
    cudaMalloc(&d_points, h_points.size() * sizeof(float));
    
    // 拷贝数据到设备
    cudaMemcpy(d_points, h_points.data(), h_points.size() * sizeof(float), cudaMemcpyHostToDevice);

    // 设置CUDA执行配置
    const int blockSize = 256;
    int gridSize = (numPoints + blockSize - 1) / blockSize;

    // 执行核函数(缩放因子设为2.0)
    scalePoints<<<gridSize, blockSize>>>(d_points, numPoints, 1.1f);
    
    // 等待核函数完成
    cudaDeviceSynchronize();

    // 拷贝结果回主机
    cudaMemcpy(h_points.data(), d_points, h_points.size() * sizeof(float), cudaMemcpyDeviceToHost);

    // 保存结果
    if (!savePointCloud("output.xyz", h_points.data(), numPoints)) {
        std::cerr << "Error saving output file!" << std::endl;
    }

    // 清理资源
    cudaFree(d_points);

    std::cout << "Processing completed! Results saved to output.xyz" << std::endl;
    return 0;
}

这是读取一个大量点云文件,然后对点云缩放处理

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

老猿的春天

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值