在Qt中使用g++与nvcc混合编译

    转载自https://blog.csdn.net/bisheng250/article/details/53611237

    这次是通过在pro文件中添加QMAKE_EXTRA_COMPILERS添加额外编译完成,其它就是配置参数之类的

.pro配置如下

#-------------------------------------------------
#
# Project created by QtCreator 2019-03-31T17:08:06
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = cudaQt
TEMPLATE = app

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

CUDA_SOURCES += $$PWD/cuda/kernel.cu
CUDA_SDK = /usr/local/cuda
CUDA_DIR = /usr/local/cuda
SYSTEM_NAME = ubuntu
SYSTEM_TYPE = 64
CUDA_ARCH = sm_50
NVCC_OPTIONS = --use_fast_math

INCLUDEPATH += $$CUDA_DIR/include
QMAKE_LIBDIR +=$$CUDA_DIR/lib64
CUDA_OBJECTS_DIR = $$PWD/cuda
CUDA_LIBS = -lcuda -lcudart
CUDA_INC =$$join(INCLUDEPATH,'" -I"','-I"','"')
#LIBS += $$join(CUDA_LIBS,'.so ', '', '.so')
LIBS += $$CUDA_LIBS

CONFIG(debug, debug|release) {
    # Debug mode
    cuda_d.input = CUDA_SOURCES
    cuda_d.output = $$CUDA_OBJECTS_DIR/${QMAKE_FILE_BASE}.o
    cuda_d.commands = $$CUDA_DIR/bin/nvcc -D_DEBUG $$NVCC_OPTIONS $$CUDA_INC $$NVCC_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}.o
    cuda.commands = $$CUDA_DIR/bin/nvcc $$NVCC_OPTIONS $$CUDA_INC $$NVCC_LIBS --machine $$SYSTEM_TYPE -arch=$$CUDA_ARCH -c -o ${QMAKE_FILE_OUT} ${QMAKE_FILE_NAME}
    cuda.dependency_type = TYPE_C
    QMAKE_EXTRA_COMPILERS += cuda
}

 工程目录结构如下

在Qt中使用g++与nvcc混合编译_第1张图片

在cpp代码中使用使用一般调用函数一样,声明就可以了。

#include "mainwindow.h"

extern "C" void runCudaPart(); //cuda核函数
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    runCudaPart();
}

MainWindow::~MainWindow()
{

}
#include 
#include 
#include 
extern "C" void runCudaPart(); //声明cuda核函数

__global__ void addAry( int * ary1, int * ary2 )
{
    int indx = threadIdx.x;
    ary1[ indx ] += ary2[ indx ];
}

// Main cuda function
void runCudaPart() {

    int ary1[32];
    int ary2[32];
    int res[32];

    for( int i=0 ; i<32 ; i++ )
    {
        ary1[i] = i;
        ary2[i] = 2*i;
        res[i]=0;
    }
    int * d_ary1, *d_ary2;
    cudaMalloc((void**)&d_ary1, 32*sizeof(int));
    cudaMalloc((void**)&d_ary2, 32*sizeof(int));
    cudaMemcpy((void*)d_ary1, (void*)ary1, 32*sizeof(int), cudaMemcpyHostToDevice);
    cudaMemcpy((void*)d_ary2, (void*)ary2, 32*sizeof(int), cudaMemcpyHostToDevice);
    addAry<<<1,32>>>(d_ary1,d_ary2);

    cudaMemcpy((void*)res, (void*)d_ary1, 32*sizeof(int), cudaMemcpyDeviceToHost);
    for( int i=0 ; i<32 ; i++ )
        std::cout << "result[" << i << "] = " << i << std::endl;
    cudaFree(d_ary1);
    cudaFree(d_ary2);
}

   编译运行可以看到有输出

在Qt中使用g++与nvcc混合编译_第2张图片

    外一种方法则是使用qt的cmake建立工程,这样就和普通使用cmake编译cuda工程一样了。 

你可能感兴趣的:(Qt,cuda)