Linux下Qt+CUDA调试并运行

  • Post author:
  • Post category:linux


Qt与CUDA相结合具体的操作主要修改qt项目中的配置文件pro。下面以测试的项目为例。

因为这是一个测试案例,代码很简单,下面将这几个文件的代码贴出来,方面后面对应pro文件和Makefile文件中的内容。

1.main.cpp

 1 #include "mainwindow.h"
 2 #include <QApplication>
 3 
 4 int main(int argc, char *argv[])
 5 {
 6     printf("main1 \n");
 7 
 8     QApplication a(argc, argv);
 9     MainWindow w;
10     w.show();
11 
12    printf("main2 \n");
13     return a.exec();
14 }

2.mainwindow.cpp

 1 #include "ui_mainwindow.h"
 2 #include "binlinearinterpolationcuda.h"
 3 
 4 
 5 MainWindow::MainWindow(QWidget *parent) :
 6     QMainWindow(parent),
 7     ui(new Ui::MainWindow)
 8 {
 9     ui->setupUi(this);
10     printf("MainWindow \n");
11 
12     BinlinearInterpolation();//cuda代码的入口
13 }
14 
15 MainWindow::~MainWindow()
16 {
17     delete ui;
18 }

3.binlinearinterpolationcuda.cu

 1 #include "binlinearinterpolationcuda.h"
 2 
 3 
 4 __global__ void Binlinear1_kernel(float *M,float *N,int *x,int *width1);
 5 __global__ void Binlinear2_kernel(float *N,float *V,int *y,int *width1);
 6 void BinlinearInterpolation(){
 7 /****/
 8     Binlinear1_kernel<<<gridDim,blockDim>>>(M,N,x,width1);
 9     Binlinear2_kernel<<<gridDim1,blockDim1>>>(N,V,y,width1);
10 /***/
11     
12 }

4.binlinearinterpolationcuda.h

 1 #ifndef BINLINEARINTERPOLATIONCUDA_H
 2 #define BINLINEARINTERPOLATIONCUDA_H
 3 #include "cuda_runtime.h"
 4 #include "device_launch_parameters.h"
 5 #include <stdio.h>
 6 #include "malloc.h"
 7 #define  WIDTH 11
 8 
 9 #define  HEIGHT 10
10 #define  X_INTER 3
11 #define  Y_INTER 3
12 #define  BLOCK_SIZE 8
13 
14 void BinlinearInterpolation();
15 #endif // BINLINEARINTERPOLATIONCUDA_H

5.mainwindow.h

 1 #ifndef MAINWINDOW_H
 2 #define MAINWINDOW_H
 3 
 4 #include <QMainWindow>
 5 
 6 namespace Ui {
 7 class MainWindow;
 8 }
 9 
10 class MainWindow : public QMainWindow
11 {
12     Q_OBJECT
13 
14 public:
15     explicit MainWindow(QWidget *parent = 0);
16     ~MainWindow();
17 
18     void creatMenu();
19 
20 private:
21     Ui::MainWindow *ui;
22  
23 
24 };
25 
26 #endif // MAINWINDOW_H

6.pro文件的配置

 1 ######################################################################
 2 # Automatically generated by qmake (2.01a) Thu Nov 2 11:20:26 2017
 3 ######################################################################
 4 
 5 TEMPLATE = app
 6 TARGET = 
 7 DEPENDPATH += .
 8 INCLUDEPATH += . \
 9     /usr/local/include \
10     /usr/include
11 
12 QT += core gui
13 CONFIG += debug
14 
15 HEADERS += mainwindow.h \
16     binlinearinterpolationcuda.h
17 FORMS += mainwindow.ui
18 SOURCES += main.cpp mainwindow.cpp
19 LIBS += -L"/usr/local/lib" \
20     -L"/usr/local/cuda/lib64" \
21     -lcudart \
22     -lcufft
23 
24 DEPENDPATH += .
25 
26 OTHER_FILES += binlinearinterpolationcuda.cu
27 
28 CUDA_SOURCES += binlinearinterpolationcuda.cu
29 
30 CUDA_SDK = "/usr/local/cuda"   # Path to cuda SDK install
31 CUDA_DIR = "/usr/local/cuda"            # Path to cuda toolkit install
32 SYSTEM_NAME = linux         # Depending on your system either 'Win32', 'x64', or 'Win64'
33 SYSTEM_TYPE = 64            # '32' or '64', depending on your system
34 CUDA_ARCH = sm_21           # Type of CUDA architecture, for example 'compute_10', 'compute_11', 'sm_10'
35 NVCC_OPTIONS = --use_fast_math
36 
37 
38 INCLUDEPATH += $$CUDA_DIR/include
39 QMAKE_LIBDIR += $$CUDA_DIR/lib64/
40 
41 CUDA_OBJECTS_DIR = ./
42 
43 CUDA_LIBS = cudart cufft
44 CUDA_INC = $$join(INCLUDEPATH,'" -I"','-I"','"')
45 NVCC_LIBS = $$join(CUDA_LIBS,' -l','-l', '')
46 
47 CONFIG(debug, debug|release) {
48     # Debug mode
49     cuda_d.input = CUDA_SOURCES
50     cuda_d.output = $$CUDA_OBJECTS_DIR/${QMAKE_FILE_BASE}_cuda.o
51     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}
52     cuda_d.dependency_type = TYPE_C
53     QMAKE_EXTRA_COMPILERS += cuda_d
54 }
55 else {
56     # Release mode
57     cuda.input = CUDA_SOURCES
58     cuda.output = $$CUDA_OBJECTS_DIR/${QMAKE_FILE_BASE}_cuda.o
59     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}
60     cuda.dependency_type = TYPE_C
61     QMAKE_EXTRA_COMPILERS += cuda
62 }

pro文件的配置是参照http://blog.163.com/qimo601@126/blog/static/15822093201401305739673/中提到的方法。该方法非常适用,比其他需要修改Makefile文件的方法更加实用。

7.修改完pro文件后,就可以qmake->make->执行目标文件

8.Makefile文件

  1 #############################################################################
  2 # Makefile for building: qtcuda
  3 # Generated by qmake (2.01a) (Qt 4.8.1) on: Thu Nov 2 19:39:13 2017
  4 # Project:  qtcuda.pro
  5 # Template: app
  6 # Command: /usr/local/Trolltech/Qt-4.8.1/bin/qmake -o Makefile qtcuda.pro
  7 #############################################################################
  8 
  9 ####### Compiler, tools and options
 10 
 11 CC            = gcc
 12 CXX           = g++
 13 DEFINES       = -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED
 14 CFLAGS        = -m64 -pipe -g -Wall -W -D_REENTRANT $(DEFINES)
 15 CXXFLAGS      = -m64 -pipe -g -Wall -W -D_REENTRANT $(DEFINES)
 16 INCPATH       = -I/usr/local/Trolltech/Qt-4.8.1/mkspecs/linux-g++-64 -I. -I/usr/local/Trolltech/Qt-4.8.1/include/QtCore -I/usr/local/Trolltech/Qt-4.8.1/include/QtGui -I/usr/local/Trolltech/Qt-4.8.1/include -I. -I/usr/local/include -I/usr/include -I/usr/local/cuda/include -I. -I.
 17 LINK          = g++
 18 LFLAGS        = -m64 -Wl,-rpath,/usr/local/Trolltech/Qt-4.8.1/lib
 19 LIBS          = $(SUBLIBS)  -L/usr/local/cuda/lib64/ -L/usr/local/Trolltech/Qt-4.8.1/lib -L/usr/local/lib -L/usr/local/cuda/lib64 -lcudart -lcufft -lQtGui -L/usr/local/Trolltech/Qt-4.8.1/lib -L/usr/X11R6/lib64 -lQtCore -lpthread 
 20 AR            = ar cqs
 21 RANLIB        = 
 22 QMAKE         = /usr/local/Trolltech/Qt-4.8.1/bin/qmake
 23 TAR           = tar -cf
 24 COMPRESS      = gzip -9f
 25 COPY          = cp -f
 26 SED           = sed
 27 COPY_FILE     = $(COPY)
 28 COPY_DIR      = $(COPY) -r
 29 STRIP         = strip
 30 INSTALL_FILE  = install -m 644 -p
 31 INSTALL_DIR   = $(COPY_DIR)
 32 INSTALL_PROGRAM = install -m 755 -p
 33 DEL_FILE      = rm -f
 34 SYMLINK       = ln -f -s
 35 DEL_DIR       = rmdir
 36 MOVE          = mv -f
 37 CHK_DIR_EXISTS= test -d
 38 MKDIR         = mkdir -p
 39 
 40 ####### Output directory
 41 
 42 OBJECTS_DIR   = ./
 43 
 44 ####### Files
 45 
 46 SOURCES       = main.cpp \
 47         mainwindow.cpp moc_mainwindow.cpp
 48 OBJECTS       = binlinearinterpolationcuda_cuda.o \
 49         main.o \
 50         mainwindow.o \
 51         moc_mainwindow.o
 52 DIST          = /usr/local/Trolltech/Qt-4.8.1/mkspecs/common/unix.conf \
 53         /usr/local/Trolltech/Qt-4.8.1/mkspecs/common/linux.conf \
 54         /usr/local/Trolltech/Qt-4.8.1/mkspecs/common/gcc-base.conf \
 55         /usr/local/Trolltech/Qt-4.8.1/mkspecs/common/gcc-base-unix.conf \
 56         /usr/local/Trolltech/Qt-4.8.1/mkspecs/common/g++-base.conf \
 57         /usr/local/Trolltech/Qt-4.8.1/mkspecs/common/g++-unix.conf \
 58         /usr/local/Trolltech/Qt-4.8.1/mkspecs/qconfig.pri \
 59         /usr/local/Trolltech/Qt-4.8.1/mkspecs/modules/qt_webkit_version.pri \
 60         /usr/local/Trolltech/Qt-4.8.1/mkspecs/features/qt_functions.prf \
 61         /usr/local/Trolltech/Qt-4.8.1/mkspecs/features/qt_config.prf \
 62         /usr/local/Trolltech/Qt-4.8.1/mkspecs/features/exclusive_builds.prf \
 63         /usr/local/Trolltech/Qt-4.8.1/mkspecs/features/default_pre.prf \
 64         /usr/local/Trolltech/Qt-4.8.1/mkspecs/features/debug.prf \
 65         /usr/local/Trolltech/Qt-4.8.1/mkspecs/features/default_post.prf \
 66         /usr/local/Trolltech/Qt-4.8.1/mkspecs/features/unix/gdb_dwarf_index.prf \
 67         /usr/local/Trolltech/Qt-4.8.1/mkspecs/features/warn_on.prf \
 68         /usr/local/Trolltech/Qt-4.8.1/mkspecs/features/qt.prf \
 69         /usr/local/Trolltech/Qt-4.8.1/mkspecs/features/unix/thread.prf \
 70         /usr/local/Trolltech/Qt-4.8.1/mkspecs/features/moc.prf \
 71         /usr/local/Trolltech/Qt-4.8.1/mkspecs/features/resources.prf \
 72         /usr/local/Trolltech/Qt-4.8.1/mkspecs/features/uic.prf \
 73         /usr/local/Trolltech/Qt-4.8.1/mkspecs/features/yacc.prf \
 74         /usr/local/Trolltech/Qt-4.8.1/mkspecs/features/lex.prf \
 75         /usr/local/Trolltech/Qt-4.8.1/mkspecs/features/include_source_dir.prf \
 76         qtcuda.pro
 77 QMAKE_TARGET  = qtcuda
 78 DESTDIR       = 
 79 TARGET        = qtcuda
 80 
 81 first: all
 82 ####### Implicit rules
 83 
 84 .SUFFIXES: .o .c .cpp .cc .cxx .C
 85 
 86 .cpp.o:
 87     $(CXX) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<"
 88 
 89 .cc.o:
 90     $(CXX) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<"
 91 
 92 .cxx.o:
 93     $(CXX) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<"
 94 
 95 .C.o:
 96     $(CXX) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<"
 97 
 98 .c.o:
 99     $(CC) -c $(CFLAGS) $(INCPATH) -o "$@" "$<"
100 
101 ####### Build rules
102 
103 all: Makefile $(TARGET)
104 
105 $(TARGET): ui_mainwindow.h $(OBJECTS)  
106     $(LINK) $(LFLAGS) -o $(TARGET) $(OBJECTS) $(OBJCOMP) $(LIBS)
107     { test -n "$(DESTDIR)" && DESTDIR="$(DESTDIR)" || DESTDIR=.; } && test $$(gdb --version | sed -e 's,[^0-9]\+\([0-9]\)\.\([0-9]\).*,\1\2,;q') -gt 72 && gdb --nx --batch --quiet -ex 'set confirm off' -ex "save gdb-index $$DESTDIR" -ex quit '$(TARGET)' && test -f $(TARGET).gdb-index && objcopy --add-section '.gdb_index=$(TARGET).gdb-index' --set-section-flags '.gdb_index=readonly' '$(TARGET)' '$(TARGET)' && rm -f $(TARGET).gdb-index || true
108 
109 Makefile: qtcuda.pro  /usr/local/Trolltech/Qt-4.8.1/mkspecs/linux-g++-64/qmake.conf /usr/local/Trolltech/Qt-4.8.1/mkspecs/common/unix.conf \
110         /usr/local/Trolltech/Qt-4.8.1/mkspecs/common/linux.conf \
111         /usr/local/Trolltech/Qt-4.8.1/mkspecs/common/gcc-base.conf \
112         /usr/local/Trolltech/Qt-4.8.1/mkspecs/common/gcc-base-unix.conf \
113         /usr/local/Trolltech/Qt-4.8.1/mkspecs/common/g++-base.conf \
114         /usr/local/Trolltech/Qt-4.8.1/mkspecs/common/g++-unix.conf \
115         /usr/local/Trolltech/Qt-4.8.1/mkspecs/qconfig.pri \
116         /usr/local/Trolltech/Qt-4.8.1/mkspecs/modules/qt_webkit_version.pri \
117         /usr/local/Trolltech/Qt-4.8.1/mkspecs/features/qt_functions.prf \
118         /usr/local/Trolltech/Qt-4.8.1/mkspecs/features/qt_config.prf \
119         /usr/local/Trolltech/Qt-4.8.1/mkspecs/features/exclusive_builds.prf \
120         /usr/local/Trolltech/Qt-4.8.1/mkspecs/features/default_pre.prf \
121         /usr/local/Trolltech/Qt-4.8.1/mkspecs/features/debug.prf \
122         /usr/local/Trolltech/Qt-4.8.1/mkspecs/features/default_post.prf \
123         /usr/local/Trolltech/Qt-4.8.1/mkspecs/features/unix/gdb_dwarf_index.prf \
124         /usr/local/Trolltech/Qt-4.8.1/mkspecs/features/warn_on.prf \
125         /usr/local/Trolltech/Qt-4.8.1/mkspecs/features/qt.prf \
126         /usr/local/Trolltech/Qt-4.8.1/mkspecs/features/unix/thread.prf \
127         /usr/local/Trolltech/Qt-4.8.1/mkspecs/features/moc.prf \
128         /usr/local/Trolltech/Qt-4.8.1/mkspecs/features/resources.prf \
129         /usr/local/Trolltech/Qt-4.8.1/mkspecs/features/uic.prf \
130         /usr/local/Trolltech/Qt-4.8.1/mkspecs/features/yacc.prf \
131         /usr/local/Trolltech/Qt-4.8.1/mkspecs/features/lex.prf \
132         /usr/local/Trolltech/Qt-4.8.1/mkspecs/features/include_source_dir.prf \
133         /usr/local/Trolltech/Qt-4.8.1/lib/libQtGui.prl \
134         /usr/local/Trolltech/Qt-4.8.1/lib/libQtCore.prl
135     $(QMAKE) -o Makefile qtcuda.pro
136 /usr/local/Trolltech/Qt-4.8.1/mkspecs/common/unix.conf:
137 /usr/local/Trolltech/Qt-4.8.1/mkspecs/common/linux.conf:
138 /usr/local/Trolltech/Qt-4.8.1/mkspecs/common/gcc-base.conf:
139 /usr/local/Trolltech/Qt-4.8.1/mkspecs/common/gcc-base-unix.conf:
140 /usr/local/Trolltech/Qt-4.8.1/mkspecs/common/g++-base.conf:
141 /usr/local/Trolltech/Qt-4.8.1/mkspecs/common/g++-unix.conf:
142 /usr/local/Trolltech/Qt-4.8.1/mkspecs/qconfig.pri:
143 /usr/local/Trolltech/Qt-4.8.1/mkspecs/modules/qt_webkit_version.pri:
144 /usr/local/Trolltech/Qt-4.8.1/mkspecs/features/qt_functions.prf:
145 /usr/local/Trolltech/Qt-4.8.1/mkspecs/features/qt_config.prf:
146 /usr/local/Trolltech/Qt-4.8.1/mkspecs/features/exclusive_builds.prf:
147 /usr/local/Trolltech/Qt-4.8.1/mkspecs/features/default_pre.prf:
148 /usr/local/Trolltech/Qt-4.8.1/mkspecs/features/debug.prf:
149 /usr/local/Trolltech/Qt-4.8.1/mkspecs/features/default_post.prf:
150 /usr/local/Trolltech/Qt-4.8.1/mkspecs/features/unix/gdb_dwarf_index.prf:
151 /usr/local/Trolltech/Qt-4.8.1/mkspecs/features/warn_on.prf:
152 /usr/local/Trolltech/Qt-4.8.1/mkspecs/features/qt.prf:
153 /usr/local/Trolltech/Qt-4.8.1/mkspecs/features/unix/thread.prf:
154 /usr/local/Trolltech/Qt-4.8.1/mkspecs/features/moc.prf:
155 /usr/local/Trolltech/Qt-4.8.1/mkspecs/features/resources.prf:
156 /usr/local/Trolltech/Qt-4.8.1/mkspecs/features/uic.prf:
157 /usr/local/Trolltech/Qt-4.8.1/mkspecs/features/yacc.prf:
158 /usr/local/Trolltech/Qt-4.8.1/mkspecs/features/lex.prf:
159 /usr/local/Trolltech/Qt-4.8.1/mkspecs/features/include_source_dir.prf:
160 /usr/local/Trolltech/Qt-4.8.1/lib/libQtGui.prl:
161 /usr/local/Trolltech/Qt-4.8.1/lib/libQtCore.prl:
162 qmake:  FORCE
163     @$(QMAKE) -o Makefile qtcuda.pro
164 
165 dist: 
166     @$(CHK_DIR_EXISTS) .tmp/qtcuda1.0.0 || $(MKDIR) .tmp/qtcuda1.0.0 
167     $(COPY_FILE) --parents $(SOURCES) $(DIST) .tmp/qtcuda1.0.0/ && $(COPY_FILE) --parents binlinearinterpolationcuda.cu .tmp/qtcuda1.0.0/ && $(COPY_FILE) --parents mainwindow.h binlinearinterpolationcuda.h .tmp/qtcuda1.0.0/ && $(COPY_FILE) --parents main.cpp mainwindow.cpp .tmp/qtcuda1.0.0/ && $(COPY_FILE) --parents mainwindow.ui .tmp/qtcuda1.0.0/ && (cd `dirname .tmp/qtcuda1.0.0` && $(TAR) qtcuda1.0.0.tar qtcuda1.0.0 && $(COMPRESS) qtcuda1.0.0.tar) && $(MOVE) `dirname .tmp/qtcuda1.0.0`/qtcuda1.0.0.tar.gz . && $(DEL_FILE) -r .tmp/qtcuda1.0.0
168 
169 
170 clean:compiler_clean 
171     -$(DEL_FILE) $(OBJECTS)
172     -$(DEL_FILE) *~ core *.core
173 
174 
175 ####### Sub-libraries
176 
177 distclean: clean
178     -$(DEL_FILE) $(TARGET) 
179     -$(DEL_FILE) Makefile
180 
181 
182 check: first
183 
184 mocclean: compiler_moc_header_clean compiler_moc_source_clean
185 
186 mocables: compiler_moc_header_make_all compiler_moc_source_make_all
187 
188 compiler_cuda_d_make_all: binlinearinterpolationcuda_cuda.o
189 compiler_cuda_d_clean:
190     -$(DEL_FILE) binlinearinterpolationcuda_cuda.o
191 binlinearinterpolationcuda_cuda.o: binlinearinterpolationcuda.h \
192         binlinearinterpolationcuda.cu
193     /usr/local/cuda/bin/nvcc -D_DEBUG --use_fast_math -I"." -I"/usr/local/include" -I"/usr/include" -I"/usr/local/cuda/include" -lcudart -lcufft --machine 64 -arch=sm_21 -c -o binlinearinterpolationcuda_cuda.o binlinearinterpolationcuda.cu
194 
195 compiler_moc_header_make_all: moc_mainwindow.cpp
196 compiler_moc_header_clean:
197     -$(DEL_FILE) moc_mainwindow.cpp
198 moc_mainwindow.cpp: mainwindow.h
199     /usr/local/Trolltech/Qt-4.8.1/bin/moc $(DEFINES) $(INCPATH) mainwindow.h -o moc_mainwindow.cpp
200 
201 compiler_rcc_make_all:
202 compiler_rcc_clean:
203 compiler_image_collection_make_all: qmake_image_collection.cpp
204 compiler_image_collection_clean:
205     -$(DEL_FILE) qmake_image_collection.cpp
206 compiler_moc_source_make_all:
207 compiler_moc_source_clean:
208 compiler_uic_make_all: ui_mainwindow.h
209 compiler_uic_clean:
210     -$(DEL_FILE) ui_mainwindow.h
211 ui_mainwindow.h: mainwindow.ui
212     /usr/local/Trolltech/Qt-4.8.1/bin/uic mainwindow.ui -o ui_mainwindow.h
213 
214 compiler_yacc_decl_make_all:
215 compiler_yacc_decl_clean:
216 compiler_yacc_impl_make_all:
217 compiler_yacc_impl_clean:
218 compiler_lex_make_all:
219 compiler_lex_clean:
220 compiler_clean: compiler_cuda_d_clean compiler_moc_header_clean compiler_uic_clean 
221 
222 ####### Compile
223 
224 main.o: main.cpp mainwindow.h
225     $(CXX) -c $(CXXFLAGS) $(INCPATH) -o main.o main.cpp
226 
227 mainwindow.o: mainwindow.cpp mainwindow.h \
228         ui_mainwindow.h \
229         binlinearinterpolationcuda.h
230     $(CXX) -c $(CXXFLAGS) $(INCPATH) -o mainwindow.o mainwindow.cpp
231 
232 moc_mainwindow.o: moc_mainwindow.cpp 
233     $(CXX) -c $(CXXFLAGS) $(INCPATH) -o moc_mainwindow.o moc_mainwindow.cpp
234 
235 ####### Install
236 
237 install:   FORCE
238 
239 uninstall:   FORCE
240 
241 FORCE:

有兴趣的同学,可以根据pro文件中的内容都Makefile中的代码。

9.有关pro文件的配置情况,可以参看“http://blog.csdn.net/liang19890820/article/details/51774724”

转载于:https://www.cnblogs.com/zhangjxblog/p/7774800.html