Skip to content

Instantly share code, notes, and snippets.

@tschoonj
Created May 10, 2014 14:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tschoonj/a3b1c346d1cef1cf2a23 to your computer and use it in GitHub Desktop.
Save tschoonj/a3b1c346d1cef1cf2a23 to your computer and use it in GitHub Desktop.
gists for blogpost on cuda device library
#####
#
# SYNOPSIS
#
# AX_CHECK_CUDA
#
# DESCRIPTION
#
# Figures out if CUDA Driver API/nvcc is available, i.e. existence of:
# cuda.h
# libcuda.so
# nvcc
#
# If something isn't found, fails straight away.
#
# Locations of these are included in
# CUDA_CFLAGS and
# CUDA_LDFLAGS.
# Path to nvcc is included as
# NVCC_PATH
# in config.h
#
# The author is personally using CUDA such that the .cu code is generated
# at runtime, so don't expect any automake magic to exist for compile time
# compilation of .cu files.
#
# LICENCE
# Public domain
#
# AUTHOR
# wili
#
#####
AC_DEFUN([AX_CHECK_CUDA], [
# Provide your CUDA path with this
AC_ARG_WITH(cuda, [ --with-cuda=PREFIX Prefix of your CUDA installation], [cuda_prefix=$withval], [cuda_prefix="/usr/local/cuda"])
# Setting the prefix to the default if only --with-cuda was given
if test "$cuda_prefix" == "yes"; then
if test "$withval" == "yes"; then
cuda_prefix="/usr/local/cuda"
fi
fi
# Checking for nvcc
AC_MSG_CHECKING([nvcc in $cuda_prefix/bin])
if test -x "$cuda_prefix/bin/nvcc"; then
AC_MSG_RESULT([found])
AC_DEFINE_UNQUOTED([NVCC_PATH], ["$cuda_prefix/bin/nvcc"], [Path to nvcc binary])
# We need to add the CUDA search directories for header and lib searches
CUDA_CFLAGS=""
# Saving the current flags
ax_save_CFLAGS="${CFLAGS}"
ax_save_LDFLAGS="${LDFLAGS}"
# Announcing the new variables
AC_SUBST([CUDA_CFLAGS])
AC_SUBST([CUDA_LDFLAGS])
AC_SUBST([NVCC],[$cuda_prefix/bin/nvcc])
AC_CHECK_FILE([$cuda_prefix/lib64],[lib64_found=yes],[lib64_found=no])
if test "x$lib64_found" = xno ; then
AC_CHECK_FILE([$cuda_prefix/lib],[lib32_found=yes],[lib32_found=no])
if test "x$lib32_found" = xyes ; then
AC_SUBST([CUDA_LIBDIR],[$cuda_prefix/lib])
else
AC_MSG_WARN([Couldn't find cuda lib directory])
VALID_CUDA=no
fi
else
AC_CHECK_SIZEOF([long])
if test "x$ac_cv_sizeof_long" = "x8" ; then
AC_SUBST([CUDA_LIBDIR],[$cuda_prefix/lib64])
CUDA_CFLAGS+=" -m64"
elif test "x$ac_cv_sizeof_long" = "x4" ; then
AC_CHECK_FILE([$cuda_prefix/lib32],[lib32_found=yes],[lib32_found=no])
if test "x$lib32_found" = xyes ; then
AC_SUBST([CUDA_LIBDIR],[$cuda_prefix/lib])
CUDA_CFLAGS+=" -m32"
else
AC_MSG_WARN([Couldn't find cuda lib directory])
VALID_CUDA=no
fi
else
AC_MSG_ERROR([Could not determine size of long variable type])
fi
fi
if test "x$VALID_CUDA" != xno ; then
CUDA_CFLAGS+=" -I$cuda_prefix/include"
CFLAGS="$CUDA_CFLAGS $CFLAGS"
CUDA_LDFLAGS="-L$CUDA_LIBDIR"
LDFLAGS="$CUDA_LDFLAGS $LDFLAGS"
# And the header and the lib
AC_CHECK_HEADER([cuda.h], [],
AC_MSG_WARN([Couldn't find cuda.h])
VALID_CUDA=no
,[#include <cuda.h>])
if test "x$VALID_CUDA" != "xno" ; then
AC_CHECK_LIB([cuda], [cuInit], [VALID_CUDA=yes], AC_MSG_WARN([Couldn't find libcuda]
VALID_CUDA=no))
fi
fi
# Returning to the original flags
CFLAGS=${ax_save_CFLAGS}
LDFLAGS=${ax_save_LDFLAGS}
else
AC_MSG_RESULT([not found!])
AC_MSG_WARN([nvcc was not found in $cuda_prefix/bin])
VALID_CUDA=no
fi
if test "x$enable_cuda" = xyes && test x$VALID_CUDA = xyes ; then
AC_MSG_NOTICE([Building with CUDA bindings])
elif test "x$enable_cuda" = xyes && test x$VALID_CUDA = xno ; then
AC_MSG_ERROR([Cannot build CUDA bindings. Check errors])
fi
])
xraylibincludedir = ${includedir}/xraylib
if ENABLE_CUDA
lib_LIBRARIES=libxrlcuda.a
xraylibinclude_HEADERS = xraylib-cuda.h
libxrlcuda_a_SOURCES = xraylib-cuda.cu xraylib-cuda.h \
xraylib-cuda-private.h \
atomiclevelwidth.cu \
atomicweight.cu \
auger_trans.cu \
comptonprofiles.cu \
coskron.cu \
cross_sections.cu \
densities.cu \
edges.cu \
fi.cu \
fii.cu \
fluor_yield.cu \
jump.cu \
radrate.cu \
scattering.cu \
kissel_pe.cu \
xrf_cross_sections_aux.cu \
cs_line.cu \
splint.cu \
fluor_lines.cu \
polarized.cu \
cs_barns.cu
libxrlcuda_a_CFLAGS = $(CUDA_CFLAGS) -I$(top_srcdir)/src -I$(top_srcdir)/include
libxrlcuda_a_AR = $(NVCC) -arch=sm_20 -lib -o
.cu.o: xraylib-cuda.h xraylib-cuda-private.h
$(NVCC) $(libxrlcuda_a_CFLAGS) -arch=sm_20 -dc -o $@ $<
endif
{% endcodeblock %}
if ENABLE_CUDA
check_PROGRAMS += xrlexample11
endif
if ENABLE_CUDA
xrlexample11_SOURCES = xrlexample11.cu
xrlexample11_LDADD = xrlcudalink.o ../src/libxrl.la $(CUDA_LDFLAGS) ../cuda/libxrlcuda.a -lcudart
xrlexample11_CFLAGS = -I${top_srcdir}/include -I${top_builddir}/include -I${top_srcdir}/cuda -I${top_srcdir}/src
TESTS_ENVIRONMENT = DYLD_LIBRARY_PATH="$(CUDA_LIBDIR)" LD_LIBRARY_PATH="$(CUDA_LIBDIR)"
endif
.cu.o: ../cuda/libxrlcuda.a
$(NVCC) $(xrlexample11_CFLAGS) -arch=sm_20 -dc -o $@ $<
xrlcudalink.o: xrlexample11.o ../cuda/libxrlcuda.a
$(NVCC) -arch=sm_20 -dlink xrlexample11.o ../cuda/libxrlcuda.a -o xrlcudalink.o
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment