The source code that accompanies this book is available on http://www.cudahandbook.com, and it is open source, copyrighted with the 2-clause BSD license.
The CUDA Handbook Library, located in the chLib/ directory of the source code, contains a portable library with support for timing, threading, driver API utilities, and more. They are described in more detail in Appendix A.
Arguments over brace placement aside, the main feature of the code in
this book that will engender comment is the goto-based
error handling mechanism. Functions that perform multiple resource
allocations (or other operations that might fail, and where failure
should be propagated to the caller) are structured around an Initialize
/ ErrorCheck / Cleanup idiom, similar to a pattern commonly used in
Linux kernel code.
On failure, all cleanup is performed by the same body of code at the
end of the function. It is important to initialize the resources to
guaranteed-invalid values at the top of the function, so the cleanup
code knows which resources must be freed. If a resource allocation or
other function fails, the code performs a goto to the
cleanup code.
chError.h, described in Section A.6, defines error-handling macros
for the CUDA runtime and the driver API that implement this idiom:
cuda() prepends “cuda” to its argument and checks the
return value, so cuda(Memcpy( ... )); expands to a checked
call to cudaMemcpy(); the cu() macro does the same for
driver API functions1. For a fuller treatment of CUDA
error handling — including the reasoning behind this idiom — see CUDA
Error Handling: A Definitive Guide.
The SDK is a shared experience for all CUDA developers, so we assume you’ve installed the CUDA SDK and that you can build CUDA programs with it. Occasionally, existing SDK samples are modified to illustrate a point; and for portable wrappers for certain operations (such as CPU thread creation), it seems preferable not to reinvent the wheel.
The SDK also includes the GLUT (GL Utility Library), a convenient library that enables OpenGL applications to target a variety of operating systems from the same code base. GLUT is designed to build demo-quality applications, not production-quality, but it fits the bill for our needs.
The concise formulation of these macros is due to Allan MacKinnon.↩︎