Wie sollte ein sehr einfaches Makefile aussehen für Cuda unter Linux kompilieren
-
05-07-2019 - |
Frage
Ich möchte eine sehr einfache Hallo Welt Ebene Cuda-Programm unter Linux kompilieren. Ich habe drei Dateien:
- der Kernel: helloWorld.cu
- main-Methode: Helloworld.cpp
- gemeinsamer Header: helloWorld.h
Könnten Sie schreiben Sie mir ein einfaches Makefile dies mit nvcc und g zu kompilieren ++?
Danke,
Gabor
Lösung
Nur für den Fall, hier ist meine Variante. Ich benutze es CUDA-Projekte auf dem Mac zu kompilieren, aber ich denke, es ist Linux auch gerecht wird. Es erfordert CUDA SDK.
BINDIR = ./ # places compiled binary in current directory
EXECUTABLE := helloWorld
CCFILES := helloWorld.cpp
CUFILES := helloWorld.cu
# an ugly part - setting rootdir for CUDA SDK makefile
# look for common.mk - I don't know where SDK installs it on Linux -
# and change ROOTDIR accordingly
ROOTDIR := /Developer/GPU\ Computing/C/common
include $(ROOTDIR)/../common/common.mk
Andere Tipps
Ich habe noch nie von Cuda gehört, aber von der Online-Dokumentation sieht es aus, als ob X.cu sollte in X.o kompiliert werden, so mit helloWorld.cu und Helloworld.cpp ist keine gute Idee. Mit Ihrer Erlaubnis werde ich den „Kern“ helloKernel.cu umbenennen, dann sollte diese Arbeit:
NVCC = nvcc helloWorld.o: helloWorld.cpp helloWorld.h $(NVCC) -c %< -o $@ helloKernel.o: helloKernel.cu $(NVCC) -c %< -o $@ helloWorld: helloWorld.o helloKernel.o $(NVCC) %^ -o $@
(Beachten Sie, dass diese führenden Leerzeichen sind Registerkarten.)
Wenn das funktioniert, versuchen Sie eine Slicker Version:
NVCC = nvcc helloWorld.o: %.o : %.cpp %.h helloKernel.o: %.o : %.cu %.o: $(NVCC) -c %< -o $@ helloWorld: helloWorld.o helloKernel.o $(NVCC) %^ -o $@
Meine Version, die ausführliche aber transparent:
myapp: myapp.o g++ -fPIC -o $@ $< -L /usr/local/cuda/lib -lcudart myapp.o: myapp.cu /usr/local/cuda/bin/nvcc --compiler-options -fno-strict-aliasing \ -I/usr/local/cuda/include \ -DUNIX -O2 -o $@ -c $< matrixMul: matrixMul.o g++ -fPIC -o $@ $< -L /usr/local/cuda/lib -lcudart # It MUST be named .cu or nvcc compiles as regular C !!! (no __global__) matrixMul.o: matrixMul.cu /usr/local/cuda/bin/nvcc --compiler-options -fno-strict-aliasing \ -I/usr/local/cuda/include \ -DUNIX -O2 -o $@ -c $<
Hier ist ein Beispiel, was ein aktuelles Projekt aussieht. Wie Sie sehen können gibt es einige OpenGL-Bibliotheken ist
ce : cudaExample.c cudaExample.h
cp cudaExample.c cudaExample.cu
/usr/local/cuda/bin/nvcc -arch=sm_20 -o ce -lglut -lGL -lGLU -lXext -lXmu -lX11 -lm cudaExample.cu
dann laufen make ce
und ./ce