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

时间:2018-04-20 17:27:57

标签: c++ cuda mingw

我在尝试运行一个简单的测试程序以开始学习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:\Qt\Qt5.10.0\Tools\mingw530_32\bin\mingw32-make.exe" 
C:\Qt\Qt5.10.0\5.10.0\mingw53_32\bin\qmake.exe -o Makefile ..\test01\test01.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 debug\test01.exe debug/cuda/vectoradd_cuda.o debug/obj/main.o  -L"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.1\lib\x64" -lcuda -lcudart -lcudadevrt -LC:\Qt\Qt5.10.0\5.10.0\mingw53_32\lib C:\Qt\Qt5.10.0\5.10.0\mingw53_32\lib\libQt5Cored.a 
Makefile.Debug:64: recipe for target 'debug\test01.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 debug\test01.exe debug/cuda/vectoradd_cuda.o debug/obj/main.o "-LC:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.1\lib\x64" -lcuda -lcudart -lcudadevrt -LC:\Qt\Qt5.10.0\5.10.0\mingw53_32\lib C:\Qt\Qt5.10.0\5.10.0\mingw53_32\lib\libQt5Cored.a, ...) failed.
make (e=2): Le fichier spécifié est introuvable.

mingw32-make[1]: *** [debug\test01.exe] Error 2
Makefile:36: recipe for target 'debug' failed
mingw32-make: *** [debug] Error 2
19:00:05: Le processus "C:\Qt\Qt5.10.0\Tools\mingw530_32\bin\mingw32-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_DIR\include

# 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
}

main.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);
}

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

您要执行的操作不受支持。

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