Programming CUDA Using Go
This example demonstrates that we can programming CUDA using Go. It works with Amazon AWS GPU instances (64-bit AMD x64 system) and Nvidia Jetson TK1 development board (32-bit ARM7l system).
cgo tool in Go's toolchain enables calling C programs from Go
cgo assumes that the C/C++ compiler accepts
command line parameters that GCC accepts. For example, Clang is
compatible with GCC. However, NVCC isn't.
Thanks to the author of this Stackoverflow question, who proposed a solution:
- Build device code and its C wrapper into a shared library using NVCC.
- Link the shared library with the Go program using CGO.
In our case, the "device code and its C wrapper" are in
c++/Makefile shows how to build a shared library
The Go program in our case is
square/square.go. After building the
shared library by typing
make -f in directory
c++, we can build
and run the Go program
cd square go install -v && go test -v && go vet LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH $GOPATH/bin/square