From 2a920ca7bad109be7dfe9cf0290647d805abdaff Mon Sep 17 00:00:00 2001 From: Kenneth Jao Date: Fri, 21 Apr 2023 15:51:02 -0500 Subject: [PATCH] Added compilation example --- docs/source/usage.rst | 53 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/docs/source/usage.rst b/docs/source/usage.rst index 009bba5..fdd26a8 100644 --- a/docs/source/usage.rst +++ b/docs/source/usage.rst @@ -244,3 +244,56 @@ BLAS Examples Compilation and Linking ======================= +To compile with this library, there are only a few things necessary. +First, it is recommended you use the provided template ``Makefile``, which can be +easily modified to suit your project needs. It already default handles the compilation +and linking with ``nvcc``, so long as you fulfill a few requirements. + +#. Use the compiler flag ``CUDA`` to mark where GPU-specific code is, if necessary. +#. Any files that use ``CUDA`` functionality, (i.e defining a kernel), should have the file + extension ``.cu.cpp``. +#. When including the ``Core.h`` header file, in only **one** file, you must define the + macro ``CUDATOOLS_IMPLEMENTATION``. That file must also compile for CUDA, so its + extension must be ``.cu.cpp``. It's recommended to put this with your kernel definitions. + +Afterwards the ``Makefile`` will have two targets ``cpu`` and ``gpu``, which compile +the CPU and GPU compatible binaries respectively. As an example, we can look at the whole +file for the first example: + +.. code-block:: cpp + + // main.cu.cpp + #define CUDATOOLS_IMPLEMENTATION + #include + + DEFINE_KERNEL(add, int x, int y) { + printf("Kernel: %i\n", x + y); + } + + int main() { + KERNEL(add, CudaTools::Kernel::basic(1), 1, 1); // Prints 2. + return 0; + } + +.. code-block:: + + // Makefile + + CC := g++-10 + NVCC := nvcc + CFLAGS := -Wall -std=c++17 -fopenmp -MMD + NVCC_FLAGS := -MMD -w -Xcompiler + + INCLUDE := ../../ + LIBS_DIR := + LIBS_DIR_GPU := /usr/local/cuda/lib64 + LIBS := + LIBS_GPU := cuda cudart cublas + + TARGET = coreKernel + SRC_DIR = . + BUILD_DIR = build + +The lines above are the first few lines of the ``Makefile``, which are the only +lines you should need to modify, consisting of libraries and flags, as well as +the name of the target.