Ubuntu上使用QT creator執行cuda程式

eastgeneral發表於2024-05-31
突發奇想想嘗試一下QT介面中使用CUDA加速過的程式,然後查了一下資料,總結一下有以下幾點吧

1、CUDA配置全部放在.pro檔案中

2、main.cpp為主函式使用g++編譯

3、kernel.cu為核函式使用nvcc編譯

不多說上程式碼


以下為main.cpp程式碼

#include <QtCore/QCoreApplication>

extern "C"
void runCudaPart();

int main(int argc, char *argv[])
{
runCudaPart();
}

以下為kernel.cu程式碼

// CUDA-C includes
#include <cuda.h>

#include <cuda_runtime.h>

#include <stdio.h>

extern "C"
//Adds two arrays
void runCudaPart();


__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++ )
printf( "result[%d] = %d\n", i, res[i]);


cudaFree(d_ary1);
cudaFree(d_ary2);
}


以下為配置檔案cudaQT.pro

QT += core

QT -= gui

TARGET = cudaQTS
CONFIG += console
CONFIG -= app_bundle

TEMPLATE = app


SOURCES += main.cpp


# This makes the .cu files appear in your project
OTHER_FILES += ./kernel.cu

# CUDA settings <-- may change depending on your system
CUDA_SOURCES += ./kernel.cu
CUDA_SDK = "/usr/local/cuda-7.5/" # Path to cuda SDK install
CUDA_DIR = "/usr/local/cuda-7.5/" # Path to cuda toolkit install

# DO NOT EDIT BEYOND THIS UNLESS YOU KNOW WHAT YOU ARE DOING....

SYSTEM_NAME = ubuntu # Depending on your system either 'Win32', 'x64', or 'Win64'
SYSTEM_TYPE = 64 # '32' or '64', depending on your system
CUDA_ARCH = sm_50 # Type of CUDA architecture, for example 'compute_10', 'compute_11', 'sm_10'
NVCC_OPTIONS = --use_fast_math


# include paths
INCLUDEPATH += $$CUDA_DIR/include

# library directories
QMAKE_LIBDIR += $$CUDA_DIR/lib64/

CUDA_OBJECTS_DIR = ./


# Add the necessary libraries
CUDA_LIBS = -lcuda -lcudart

# The following makes sure all path names (which often include spaces) are put between quotation marks
CUDA_INC = $$join(INCLUDEPATH,'" -I"','-I"','"')
#LIBS += $$join(CUDA_LIBS,'.so ', '', '.so')
LIBS += $$CUDA_LIBS

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




結果圖,能編譯並執行


後續可以試試把一個大工程塞進來
————————————————

版權宣告:本文為博主原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處連結和本宣告。

原文連結:https://blog.csdn.net/bisheng250/article/details/53611237

相關文章