使用msvc 15在Qt5.13中编译CUDA代码

问题描述 投票:0回答:1

我必须在Qt中与其他c ++文件一起构建一个cuda代码。但这已经失败了。我尝试了Stackoverflow中提供的示例,但是也失败了。我已经附上了示例代码。请提供建议

。pro文件

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++11

# The following define makes your compiler emit warnings if you use
# any Qt feature that has 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

# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
    main.cpp \
    mainwindow.cpp

HEADERS += \
    mainwindow.h

FORMS += \
    mainwindow.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target


CUDA_PATH = "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0"
INCLUDEPATH += $$CUDA_PATH\include

OTHER_FILES +=  vectorAddition.cu
CUDA_SOURCES += vectorAddition.cu
SYSTEM_TYPE = 64
CUDA_ARCH = sm_50
NVCC_OPTIONS = --use_fast_math
QMAKE_LIBDIR += "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0\lib\x64"
CUDA_LIBS = cuda cudart
# The following makes sure all path names (which often include spaces) are put between quotation marks
CUDA_INC = $$join(INCLUDEPATH,'" -I"','-I"','"')
NVCC_LIBS = $$join(CUDA_LIBS,' -l','-l', '')
LIBS += $$join(CUDA_LIBS,'.lib ', '', '.lib')

#Configuration of the Cuda compiler
CONFIG(debug, debug|release) {
    # Debug mode
    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 --machine $$SYSTEM_TYPE -arch=$$CUDA_ARCH -c -o ${QMAKE_FILE_OUT} ${QMAKE_FILE_NAME}
    cuda_d.dependency_type = TYPE_C
    QMAKE_EXTRA_COMPILERS += cuda_d
}
else {
    # Release mode
    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 --machine $$SYSTEM_TYPE -arch=$$CUDA_ARCH -c -o ${QMAKE_FILE_OUT} ${QMAKE_FILE_NAME}
    cuda.dependency_type = TYPE_C
    QMAKE_EXTRA_COMPILERS += cuda
}

MainWindow.cpp

#include "mainwindow.h"

#include <QApplication>
#include <QDebug>
#include <cuda.h>
#include <builtin_types.h> //#include <drvapi_error_string.h>

void vectorAddition(const float* a, const float* b, float* c, int n);

void printArray(const float* a, const unsigned int n) {
    QString s = "(";
    unsigned int ii;
    for (ii = 0; ii < n - 1; ++ii)
        s.append(QString::number(a[ii])).append(", ");
    s.append(QString::number(a[ii])).append(")");

    qDebug() << s; }

int main(int argc, char *argv[]) {
    QApplication app(argc, argv);
    MainWindow w;
    w.show();

    QCoreApplication(argc, argv);

        int deviceCount = 0;
        int cudaDevice = 0;
        char cudaDeviceName [100];

        unsigned int N = 50;
        float *a, *b, *c;

        cuInit(0);
        cuDeviceGetCount(&deviceCount);
        cuDeviceGet(&cudaDevice, 0);
        cuDeviceGetName(cudaDeviceName, 100, cudaDevice);
        qDebug() << "Number of devices: " << deviceCount;
        qDebug() << "Device name:" << cudaDeviceName;

        a = new float [N];    b = new float [N];    c = new float [N];
        for (unsigned int ii = 0; ii < N; ++ii) {
            a[ii] = qrand();
            b[ii] = qrand();
        }

        // This is the function call in which the kernel is called
        vectorAddition(a, b, c, N);

        qDebug() << "input a:"; printArray(a, N);
        qDebug() << "input b:"; printArray(b, N);
        qDebug() << "output c:"; printArray(c, N);

        if (a) delete a;
        if (b) delete b;
        if (c) delete c;

    return app.exec(); }

vectorAddition.cu

#include <cuda.h>
#include <builtin_types.h>

extern "C"
__global__ void vectorAdditionCUDA(const float* a, const float* b, float* c, int n)
{
    int ii = blockDim.x * blockIdx.x + threadIdx.x;
    if (ii < n)
        c[ii] = a[ii] + b[ii];
}

void vectorAddition(const float* a, const float* b, float* c, int n) {
    float *a_cuda, *b_cuda, *c_cuda;
    unsigned int nBytes = sizeof(float) * n;
    int threadsPerBlock = 256;
    int blocksPerGrid   = (n + threadsPerBlock - 1) / threadsPerBlock;

    // allocate and copy memory into the device
    cudaMalloc((void **)& a_cuda, nBytes);
    cudaMalloc((void **)& b_cuda, nBytes);
    cudaMalloc((void **)& c_cuda, nBytes);
    cudaMemcpy(a_cuda, a, nBytes, cudaMemcpyHostToDevice);
    cudaMemcpy(b_cuda, b, nBytes, cudaMemcpyHostToDevice);

    vectorAdditionCUDA<<<blocksPerGrid, threadsPerBlock>>>(a_cuda, b_cuda, c_cuda, n);

    // load the answer back into the host
    cudaMemcpy(c, c_cuda, nBytes, cudaMemcpyDeviceToHost);

    cudaFree(a_cuda);
    cudaFree(b_cuda);
    cudaFree(c_cuda);
}

错误日志

main.obj:错误LNK2038:检测到'RuntimeLibrary'不匹配:值'MD_DynamicRelease'与vectorAddition_cuda.o中的'MT_StaticRelease'不匹配mainwindow.obj:错误LNK2038:针对“ RuntimeLibrary”检测到不匹配:值“ MD_DynamicRelease”与vectorAddition_cuda.o中的值“ MT_StaticRelease”不匹配moc_mainwindow.obj:错误LNK2038:检测到“ RuntimeLibrary”的不匹配:值“ MD_DynamicRelease”与vectorAddition_cuda.o中的值“ MT_StaticRelease”不匹配。

我必须在Qt中与其他c ++文件一起构建一个cuda代码。但这已经失败了。我尝试了Stackoverflow中提供的示例,但是也失败了。我附上了...

c++ qt cuda gpu nvidia
1个回答
0
投票

通过在.pro文件中添加两个标志解决

QMAKE_CFLAGS_RELEASE += /MT
QMAKE_CXXFLAGS_RELEASE += /MT
© www.soinside.com 2019 - 2024. All rights reserved.