欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

CUDA编程(一):QT中编写cuda代码

程序员文章站 2022-07-12 20:03:01
...

可以下载一下demo:https://pan.baidu.com/s/1mUsb0iIOA5tnX7MOSpcHMQ  j8j2

1.pro文件配置(在已有工程的配置的内容下,添加以下内容)

#在已有的工程上添加以下内容
####cuda库########
LIBS += -L"/usr/local/lib" \
    -L"/usr/local/cuda/lib64" \
    -lcudart \
    -lcufft

DEPENDPATH += .

#你所编写的cuda文件#######
OTHER_FILES += bilinear.cu

CUDA_SOURCES += bilinear.cu



CUDA_SDK = "/usr/local/cuda-8.0/"   # Path to cuda SDK install
CUDA_DIR = "/usr/local/cuda-8.0/"            # Path to cuda toolkit install
#####系统类型,计算能力###########
SYSTEM_NAME = linux         # Depending on your system either 'Win32', 'x64', or 'Win64'
SYSTEM_TYPE = 64            # '32' or '64', depending on your system
CUDA_ARCH = sm_21           # Type of CUDA architecture, for example 'compute_10', 'compute_11', 'sm_10'
NVCC_OPTIONS = --use_fast_math


INCLUDEPATH += $$CUDA_DIR/include
QMAKE_LIBDIR += $$CUDA_DIR/lib64/

CUDA_OBJECTS_DIR = ./

CUDA_LIBS = cudart cufft
CUDA_INC = $$join(INCLUDEPATH,'" -I"','-I"','"')
NVCC_LIBS = $$join(CUDA_LIBS,' -l','-l', '')

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 -O3 -c -o ${QMAKE_FILE_OUT} ${QMAKE_FILE_NAME}
    cuda.dependency_type = TYPE_C
    QMAKE_EXTRA_COMPILERS += cuda
}

 

2.main.cpp

#include<stdio.h>
#include "bilinear.h"
int main(void)
{
   showhello();
   while(1);
}
//nvcc -arch sm_20

3.cuda文件:bilinear.cu

#include "bilinear.h"

extern "C"
__global__ void hellofromGPU(void)
{
    printf("GPU:hello sunyi\n");
}

void showhello(void)
{
    hellofromGPU <<<1,10>>>();
    cudaDeviceSynchronize();

}

4.头文件

#ifndef BILINEAR_H
#define BILINEAR_H
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <stdio.h>
#include "malloc.h"
#define  WIDTH 11

#define  HEIGHT 10
#define  X_INTER 3
#define  Y_INTER 3
#define  BLOCK_SIZE 8

void showhello(void);
#endif // BINLINEARINTERPOLATIONCUDA_H

 

相关标签: cuda