使用 QtCreator 构建 CUDA/C++ 测试程序时出现错误 2

Error 2 when building CUDA/C++ test program with QtCreator

本文关键字:错误 测试程序 C++ QtCreator 构建 CUDA 使用      更新时间:2023-10-16

我在尝试运行一个简单的测试程序来开始学习 CUDA 时遇到了一些麻烦。我正在尝试让一个标准的C++主文件调用另一个文件中的内核函数。

编译步骤似乎运行良好,但看起来链接器不知道如何处理 .o 文件以及如何创建.exe我从这里尝试了一些修改/选项:https://devblogs.nvidia.com/separate-compilation-linking-cuda-device-code/

我正在尝试在编译后使用 mingw 作为链接。我会错过一些额外的图书馆吗?所有.o文件都在正确的位置正确创建。

但我仍然无法让我的程序运行。

以下是错误日志:

19:00:03: Exécution des étapes pour le projet test01...
19:00:03: Configuration inchangée, étape qmake sautée.
19:00:03: Débute : "C:QtQt5.10.0Toolsmingw530_32binmingw32-make.exe" 
C:QtQt5.10.05.10.0mingw53_32binqmake.exe -o Makefile ..test01test01.pro -spec win32-g++ "CONFIG+=debug" "CONFIG+=qml_debug"
C:/Qt/Qt5.10.0/Tools/mingw530_32/bin/mingw32-make -f Makefile.Debug
mingw32-make[1]: Entering directory 'D:/lab_stuff/programmation/C++/test01/build-test01-Desktop_Qt_5_10_0_MinGW_32bit-Debug'
mingw32_make.exe -Wl,-subsystem,console -mthreads -o debugtest01.exe debug/cuda/vectoradd_cuda.o debug/obj/main.o  -L"C:Program FilesNVIDIA GPU Computing ToolkitCUDAv9.1libx64" -lcuda -lcudart -lcudadevrt -LC:QtQt5.10.05.10.0mingw53_32lib C:QtQt5.10.05.10.0mingw53_32liblibQt5Cored.a 
Makefile.Debug:64: recipe for target 'debugtest01.exe' failed
mingw32-make[1]: Leaving directory 'D:/lab_stuff/programmation/C++/test01/build-test01-Desktop_Qt_5_10_0_MinGW_32bit-Debug'
process_begin: CreateProcess(NULL, mingw32_make.exe -Wl,-subsystem,console -mthreads -o debugtest01.exe debug/cuda/vectoradd_cuda.o debug/obj/main.o "-LC:Program FilesNVIDIA GPU Computing ToolkitCUDAv9.1libx64" -lcuda -lcudart -lcudadevrt -LC:QtQt5.10.05.10.0mingw53_32lib C:QtQt5.10.05.10.0mingw53_32liblibQt5Cored.a, ...) failed.
make (e=2): Le fichier spécifié est introuvable.
mingw32-make[1]: *** [debugtest01.exe] Error 2
Makefile:36: recipe for target 'debug' failed
mingw32-make: *** [debug] Error 2
19:00:05: Le processus "C:QtQt5.10.0Toolsmingw530_32binmingw32-make.exe" s'est terminé avec le code 2.
Erreur lors de la compilation/déploiement du projet test01 (kit : Desktop Qt 5.10.0 MinGW 32bit)
When executing step "Make"
19:00:05: Temps écoulé : 00:02.

.pro 文件: QT -= GUI

CONFIG += console
CONFIG -= app_bundle
# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# Source files
SOURCES += main.cpp
# This makes the .cu files appear in your project
# OTHER_FILES +=  vectoradd.cu
# CUDA settings <-- may change depending on your system
CUDA_SOURCES += vectoradd.cu
CUDA_DIR = "C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v9.1"            # Path to cuda toolkit install
SYSTEM_NAME = x64
NVCC_OPTIONS = --use_fast_math -dlink
# include paths
INCLUDEPATH += $$CUDA_DIRinclude
# library directories
QMAKE_LIBDIR += $$CUDA_DIR/lib/$$SYSTEM_NAME 
# Add the necessary libraries
LIBS += -lcuda -lcudart 
QMAKE_LINK = mingw32_make.exe
# The following makes sure all path names (which often include spaces) are put between quotation marks
CUDA_INC = $$join(INCLUDEPATH,'" -I"','-I"','"')
# Configuration of the Cuda compiler
CONFIG(debug, debug|release) {
    # Debug mode
    DESTDIR = debug
    OBJECTS_DIR = debug/obj
    CUDA_OBJECTS_DIR = debug/cuda
    cuda_d.input = CUDA_SOURCES
    cuda_d.output = $$CUDA_OBJECTS_DIR/${QMAKE_FILE_BASE}_cuda.o
    cuda_d.commands = $$CUDA_DIR/bin/nvcc.exe -D_DEBUG $$NVCC_OPTIONS $$CUDA_INC $$LIBS -c -o ${QMAKE_FILE_OUT} ${QMAKE_FILE_NAME}
    cuda_d.dependency_type = TYPE_C
    QMAKE_EXTRA_COMPILERS += cuda_d
}
else {
    # Release mode
    DESTDIR = release
    OBJECTS_DIR = release/obj
    CUDA_OBJECTS_DIR = release/cuda
    cuda.input = CUDA_SOURCES
    cuda.output = $$CUDA_OBJECTS_DIR/${QMAKE_FILE_BASE}_cuda.o
    cuda.commands = $$CUDA_DIR/bin/nvcc.exe $$NVCC_OPTIONS $$CUDA_INC $$LIBS -c -o ${QMAKE_FILE_OUT} ${QMAKE_FILE_NAME}
    cuda.dependency_type = TYPE_C
    QMAKE_EXTRA_COMPILERS += cuda
}

主.cpp文件:

#include <cmath>
#include <chrono>
#include <iostream>
#include <cuda.h>
typedef std::chrono::high_resolution_clock timer;
typedef std::chrono::duration<float, std::ratio<1, 1000> > chrono;
void add_wrapper(int n, float *x, float *y);
int main()
{
    std::chrono::time_point<timer> start = timer::now();
    int N = 1<<20;
    float *x = new float[N];
    float *y = new float[N];
    for(int i=0; i < N; ++i)
    {
        x[i] = 1.0f;
        y[i] = 2.0f;
    }
    add_wrapper(N, x, y);
    float maxError = 0.0f;
    for(int i=0; i < N; ++i)
        maxError = fmax(maxError, fabs(y[i] - 3.0f));
    std::cout << "Max error: " << maxError << " obtained in ";
    std::cout << std::chrono::duration_cast<chrono>(timer::now() - start).count() << " ms n";
    delete[] x;
    delete[] y;
    return 0;
}

和 vectoradd.cu 文件:

#include <cuda.h>
#include <cuda_runtime.h>
__global__
void addVectors(int n, float *x, float *y)
{
    for(int i=0; i < n; ++i)
        y[i] = x[i] + y[i];
}
void add_wrapper(int n, float *x, float *y)
{
    float *d_x = new float[n];
    float *d_y = new float[n];
    cudaMallocManaged(&x, sizeof(float));
    cudaMallocManaged(&y, sizeof(float));
    cudaMemcpy(d_x, x, sizeof(float), cudaMemcpyHostToDevice);
    cudaMemcpy(d_y, y, sizeof(float), cudaMemcpyHostToDevice);
    addVectors<<<1, 1>>>(n, d_x, d_y);
    cudaDeviceSynchronize();
    cudaMemcpy(x, d_x, sizeof(float), cudaMemcpyDeviceToHost);
    cudaMemcpy(y, d_y, sizeof(float), cudaMemcpyDeviceToHost);
    cudaFree(d_x);
    cudaFree(d_y);
}

提前感谢您的帮助。

您尝试执行的操作不受支持。

根据文档,Windows平台上唯一支持的工具链是Visual Studio。你可以使用 QT Creator 和 Qmake 来构建项目,但你必须使用 Microsoft 工具链来编译和链接代码。不支持 MinGW 和 gcc。另请注意,最新版本的 CUDA 也不支持 32 位工具链。您可以使用工具链交叉编译为 32 位目标,但 Windows 上的本机 32 位工具链支持几年前就被删除了。