我计划在Qt框架中开始cuda编程。我想从一个简单的例子开始。系统信息:操作系统:ubuintu 18.04 LTSQt版本:5.14编译器:GCCCUDA版本:10.2GPU:具有计算能力6.1的NVIDIA GTX 1060
我进行了很多搜索,发现了这个有用的主题:https://cudaspace.wordpress.com/2012/07/05/qt-creator-cuda-linux-review/
我逐步按照主题进行操作,并使用自己的cuda体系结构和其他要件制作了.pro文件。这是我的项目.pro文件的内容:
QT -= gui QT += core CONFIG += c++11 console CONFIG -= app_bundle DEFINES += QT_DEPRECATED_WARNINGS qnx: target.path = /tmp/$${TARGET}/bin else: unix:!android: target.path = /opt/$${TARGET}/bin !isEmpty(target.path): INSTALLS += target DESTDIR = $$system(pwd) OBJECTS_DIR = $$DESTDIR/Obj # C++ flags QMAKE_CXXFLAGS_RELEASE =-03 CUDA_SOURCES += cuda_code.cu= SOURCES += main.cpp \ cuda_code.cu CUDA_DIR = /usr/local/cuda INCLUDEPATH += $$CUDA_DIR/include QMAKE_LIBDIR += $$CUDA_DIR/lib64 LIBS += -lcudart -lcuda CUDA_ARCH = sm_61 NVCCFLAGS = --compiler-options -fno-strict-aliasing -use_fast_math --ptxas-options=-v CUDA_INC = $$join(INCLUDEPATH,' -I','-I',' ') cuda.commands = $$CUDA_DIR/bin/nvcc -m64 -O3 -arch=$$CUDA_ARCH -c $$NVCCFLAGS \ $$CUDA_INC $$LIBS ${QMAKE_FILE_NAME} -o ${QMAKE_FILE_OUT} \ 2>&1 | sed -r \"s/\\(([0-9]+)\\)/:\\1/g\" 1>&2 cuda.dependency_type = TYPE_C cuda.depend_command = $$CUDA_DIR/bin/nvcc -O3 -M $$CUDA_INC $$NVCCFLAGS ${QMAKE_FILE_NAME} cuda.input = CUDA_SOURCES cuda.output = ${OBJECTS_DIR}${QMAKE_FILE_BASE}_cuda.o QMAKE_EXTRA_COMPILERS += cuda
现在这是main.cpp的内容
#include <QtCore/QCoreApplication> #include <iostream> using namespace std; #include <cuda_runtime.h> #include <cuda_code.cu> extern "C" cudaError_t cuda_main(); int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); // run your cuda application cudaError_t cuerr = cuda_main(); // check for errors is always a good practice! if (cuerr != cudaSuccess) cout << "CUDA Error: " << cudaGetErrorString( cuerr ) << endl; return a.exec(); }
这是.cu文件的内容:
#include <thrust/host_vector.h> #include <thrust/device_vector.h> #include <thrust/sort.h> extern "C" cudaError_t cuda_main() { // generate 16M random numbers on the host thrust::host_vector<int> h_vec(1 << 24); thrust::generate(h_vec.begin(), h_vec.end(), rand); // transfer data to the device thrust::device_vector<int> d_vec = h_vec; // sort data on the device (805 Mkeys/sec on GeForce GTX 480) thrust::sort(d_vec.begin(), d_vec.end()); // transfer data back to host thrust::copy(d_vec.begin(), d_vec.end(), h_vec.begin()); return cudaGetLastError(); }
首先我使用build->运行qmak并成功完成。但是当我想运行它时,出现此错误:
make: *** No rule to make target 'cuda_code.o', needed by 'Obj/cuda_code_cuda.o'. Stop. 19:18:20: The process "/usr/bin/make" exited with code 2. Error while building/deploying project untitled6 (kit: Desktop Qt 5.14.0 GCC 64bit) When executing step "Make"
几天来我进行了大量搜索并测试了解决方案,但没有成功。
我计划在Qt框架中开始cuda编程。我想从一个简单的例子开始。系统信息:操作系统:ubuintu 18.04 LTS Qt版本:5.14编译器:GCC CUDA版本:10.2 GPU:...
我在此链接中使用@Gred解决方案解决了这个问题: