Commit 1b4259f7 authored by Paul McCarthy's avatar Paul McCarthy 🚵
Browse files

ENH: example hybrid CPU/GPU project

parent 778bd66b
include ${FSLCONFDIR}/default.mk
PROJNAME = example_cuda_project
# All shared library dependencies required
# for linking CUDA libraries and executables
# must be added as CUDALIBS
CUDALIBS = -lcudart -lcuda
# This makefile is constructed such that:
# - "make" will compile only CPU binaries
# - "make gpu=1" will compile both CPU and GPU binaries
# - "make cpu=0 gpu=1" will compile only GPU binaries
SOFILES =
XFILES =
cpu ?= 1
gpu ?= 0
ifeq ($(cpu), 1)
XFILES += example_non_cuda_exe
endif
ifeq ($(gpu), 1)
XFILES += example_cuda_exe
SOFILES += libfsl-example_cuda_lib.so
endif
all: ${XFILES} ${SOFILES}
libfsl-example_cuda_lib.so: example_cuda_lib.cu
${NVCC} ${NVCCFLAGS} -shared -o $@ $^ ${NVCCLDFLAGS}
example_cuda_exe: example_cuda_exe.cpp libfsl-example_cuda_lib.so
${NVCC} ${NVCCFLAGS} -o $@ $< -lfsl-example_cuda_lib ${NVCCLDFLAGS}
example_non_cuda_exe: example_non_cuda_exe.cpp
${CXX} ${CXXFLAGS} -o $@ $< ${LDFLAGS}
#include <iostream>
#include "example_cuda_lib.h"
int main(int argc, char *argv[]) {
if (argc < 2) {
std::cout << "Usage: example_cuda_exe name" << std::endl;
return 1;
}
helloworld(argv[1]);
return 0;
}
#include <string>
#include <iostream>
#include "example_cuda_lib.h"
void helloworld(std::string s) {
int driverVersion;
int runtimeVersion;
char *memory;
std::cout << "Hello, " << s << std::endl;
if (cudaDriverGetVersion(&driverVersion) != 0) {
std::cout << "Error querying CUDA driver version!" << std::endl;
}
std::cout << "CUDA driver version: " << driverVersion << std::endl;
if (cudaRuntimeGetVersion(&runtimeVersion) != 0) {
std::cout << "Error querying CUDA runtime version!" << std::endl;
return;
}
std::cout << "CUDA runtime version: " << runtimeVersion << std::endl;
if (cudaMalloc(&memory, 1024) != 0) {
std::cout << "Error allocating memory!" << std::endl;
}
if (cudaFree(memory) != 0) {
std::cout << "Error freeing memory!" << std::endl;
}
std::cout << "Successfully allocated memory on GPU" << std::endl;
}
#ifndef EXAMPLE_CPP_LIB_H
#define EXAMPLE_CPP_LIB_H
#include <string>
#include <iostream>
void helloworld(std::string s);
#endif
#include <iostream>
int main(int argc, char *argv[]) {
if (argc < 2) {
std::cout << "Usage: example_non_cuda_exe name" << std::endl;
return 1;
}
std::cout << "Hello, " << argv[1] << std::endl;
return 0;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment