Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/142.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用msvc 15编译Qt5.13中的CUDA代码 我必须在Qt中与其他C++文件一起构建CUDA代码。但这是失败的。我尝试了Stackoverflow中的可用示例,但也失败了。我已经附上了样本代码。请提供建议_C++_Qt_Cuda_Gpu_Nvidia - Fatal编程技术网

使用msvc 15编译Qt5.13中的CUDA代码 我必须在Qt中与其他C++文件一起构建CUDA代码。但这是失败的。我尝试了Stackoverflow中的可用示例,但也失败了。我已经附上了样本代码。请提供建议

使用msvc 15编译Qt5.13中的CUDA代码 我必须在Qt中与其他C++文件一起构建CUDA代码。但这是失败的。我尝试了Stackoverflow中的可用示例,但也失败了。我已经附上了样本代码。请提供建议,c++,qt,cuda,gpu,nvidia,C++,Qt,Cuda,Gpu,Nvidia,.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 c

.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
}
QMAKE_CFLAGS_RELEASE += /MT
QMAKE_CXXFLAGS_RELEASE += /MT
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(); }
#包括“mainwindow.h”
#包括
#包括
#包括
#包括
无效向量加法(常量浮点*a,常量浮点*b,浮点*c,整数n);
void printary(常量浮点*a,常量无符号整数n){
QString s=“(”;
无符号整数ii;
对于(ii=0;iiqDebug()通过在.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
}
QMAKE_CFLAGS_RELEASE += /MT
QMAKE_CXXFLAGS_RELEASE += /MT

你还没有显示nvcc错误,如果有。我已经更新了错误日志谢谢,从Visual Studio切换到Qt总是很痛苦的