CUDA and Automake

Note: I am by far no expert on this – (I only spent the past few days trying to wrap my head around automake, and before that, never opened a configure.ac file before) – just wanted to share my experiences.

Below is an excerpt from configure.ac and Makefile.am files I modified to add CUDA to an existing automake/autoconf setup (the .. .. implies other unmodified/existing lines).

#                     -*- configure.ac -*-
# Process this file with autoconf to produce a configure script.
..
..
AM_INIT_AUTOMAKE(no-dependencies)
AC_CONFIG_HEADER([config.h])
..
..
# initialize array index
N="0"
..
..

#=================================================

# Setup default CUDA paths
CUDA_INSTALL_PATH="/usr/local/cuda"
CUDA_SDK_INSTALL_PATH="/usr/local/NVIDIA_GPU_Computing_SDK"

# Check: enable CUDA?
AC_MSG_CHECKING([whether to use CUDA library])
AC_ARG_ENABLE(cuda,
    [AS_HELP_STRING(--enable-cuda,enable CUDA support)],
    cudaval="yes",
    cudaval="no")
AC_MSG_RESULT([$cudaval])

# check to see if CUDA been selected
if test "$cudaval" == "no"; then
    AC_MSG_WARN([Not using CUDA!])
fi

# Setup custom CUDA paths
AC_ARG_WITH([cuda],
   [AS_HELP_STRING(
        [--with-cuda=PATH],
        [prefix where CUDA is installed @<:@default=auto@:>@])],
   [CUDA_INSTALL_PATH=$withval],
   [with_cuda=auto])
AC_ARG_WITH([cuda-sdk],
   [AS_HELP_STRING(
        [--with-cuda-sdk=PATH],
        [prefix where CUDA SDK is installed @<:@default=auto@:>@])],
   [CUDA_SDK_INSTALL_PATH=$withval],
   [with_cuda_sdk=auto])

# Setup nvcc flags
if test  "$cudaval" = "yes"; then
    NVCCFLAGS="$NVCCFLAGS"
    CUDA_CFLAGS="$CUDA_CFLAGS"
    CUDA_CFLAGS="$CUDA_CFLAGS -I$CUDA_SDK_INSTALL_PATH/C/common/inc/"
    CUDA_CFLAGS="$CUDA_CFLAGS -I$CUDA_INSTALL_PATH/include"
    CUDA_LDFLAGS="-L$CUDA_INSTALL_PATH/lib64"
    CUDA_LIBS="-lcuda -lcudart -lcufft"
    NVCCFLAGS="$NVCCFLAGS $CUDA_CFLAGS $CUDA_LDFLAGS $CUDA_LIBS"
    NVCC="nvcc"
    AC_SUBST(NVCC)
    AC_SUBST(NVCCFLAGS)
    AC_DEFINE([_CUDA],[1],[Defined if CUDA should be used])
    eval "ARRAY${N}='-D_CUDA'"
    N=`expr $N + 1`
fi

#=================================================
..
..
# construct compiler flags
N=`expr $N - 1`
TMP0=
eval TMP0="\$ARRAY0"
for i in $(seq 1 $N)
do
    eval TMP0="\$TMP0' '\$ARRAY${i}"
done

# set C compiler flags
AC_SUBST(CDFLAGS, $TMP0)
..
..
..

When --enable-cuda is set, the above configure.ac will generate the variables NVCC and NVCCFLAGS for use in an Makefile.am, for example:

#
#           -*- Makefile.am -*-

..
..

.cu.o:
    $(NVCC) $(NVCCFLAGS)  -c -o $@ $<

..
..

In addition, a _CUDA variable gets defined (#define _CUDA 1), for use in any file getting compiled with the above Makefile.am/configure.ac setup.

Hopefully, this can save at least someone else time (as I’ve wasted enough on this), and give a nice pointer in the right direction to anyone interested in adding CUDA to an existing ./configure setup.

Enjoy!

5 thoughts on “CUDA and Automake”

  1. Hi Chris,
    what is that CUDA_CFLAGS ? i came acroos the same modification in a project. but when i tried to make after configuration, it returns the error : NVCC@ : command not found.

  2. Hi,
    Actually, CUDA_CFLAGS is really just a temporary variable to accumulate the nvcc parameters, and eventually becomes NVCCFLAGS for convenience.
    Just make sure nvcc is in your PATH environment variable (ex /usr/local/cuda/bin).
    ~Chris

  3. Hi Chris, i found that generated makefile is dont track changing of .cu files, so when i change .cu files it has no effect onto ‘make’ that’s why i must use ‘make clean & make’…

    P.S. Chris, can you upload the file, or change the view of that, because if i copy that it copies with html special chars.Thanks in advance!

  4. Hi Andrey,
    Thanks for the info, not sure how to fix that just yet…
    Unfortunately, the project I used this on is no longer available for me to access the code, but here is a raw paste of what is above:

    -*- configure.ac -*-
    # Process this file with autoconf to produce a configure script.
    ..
    ..
    AM_INIT_AUTOMAKE(no-dependencies)
    AC_CONFIG_HEADER([config.h])
    ..
    ..
    # initialize array index
    N=”0″
    ..
    ..

    #=================================================

    # Setup default CUDA paths
    CUDA_INSTALL_PATH=”/usr/local/cuda”
    CUDA_SDK_INSTALL_PATH=”/usr/local/NVIDIA_GPU_Computing_SDK”

    # Check: enable CUDA?
    AC_MSG_CHECKING([whether to use CUDA library])
    AC_ARG_ENABLE(cuda,
    [AS_HELP_STRING(–enable-cuda,enable CUDA support)],
    cudaval=”yes”,
    cudaval=”no”)
    AC_MSG_RESULT([$cudaval])

    # check to see if CUDA been selected
    if test “$cudaval” == “no”; then
    AC_MSG_WARN([Not using CUDA!])
    fi

    # Setup custom CUDA paths
    AC_ARG_WITH([cuda],
    [AS_HELP_STRING(
    [–with-cuda=PATH],
    [prefix where CUDA is installed @<:@default=auto@:>@])],
    [CUDA_INSTALL_PATH=$withval],
    [with_cuda=auto])
    AC_ARG_WITH([cuda-sdk],
    [AS_HELP_STRING(
    [–with-cuda-sdk=PATH],
    [prefix where CUDA SDK is installed @<:@default=auto@:>@])],
    [CUDA_SDK_INSTALL_PATH=$withval],
    [with_cuda_sdk=auto])

    # Setup nvcc flags
    if test “$cudaval” = “yes”; then
    NVCCFLAGS=”$NVCCFLAGS”
    CUDA_CFLAGS=”$CUDA_CFLAGS”
    CUDA_CFLAGS=”$CUDA_CFLAGS -I$CUDA_SDK_INSTALL_PATH/C/common/inc/”
    CUDA_CFLAGS=”$CUDA_CFLAGS -I$CUDA_INSTALL_PATH/include”
    CUDA_LDFLAGS=”-L$CUDA_INSTALL_PATH/lib64″
    CUDA_LIBS=”-lcuda -lcudart -lcufft”
    NVCCFLAGS=”$NVCCFLAGS $CUDA_CFLAGS $CUDA_LDFLAGS $CUDA_LIBS”
    NVCC=”nvcc”
    AC_SUBST(NVCC)
    AC_SUBST(NVCCFLAGS)
    AC_DEFINE([_CUDA],[1],[Defined if CUDA should be used])
    eval “ARRAY${N}=’-D_CUDA'”
    N=`expr $N + 1`
    fi

    #=================================================
    ..
    ..
    # construct compiler flags
    N=`expr $N – 1`
    TMP0=
    eval TMP0=”\$ARRAY0″
    for i in $(seq 1 $N)
    do
    eval TMP0=”\$TMP0′ ‘\$ARRAY${i}”
    done

    # set C compiler flags
    AC_SUBST(CDFLAGS, $TMP0)
    ..
    ..
    ..

    Thanks for the interest, and good luck on your project!
    ~Chris

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.