ax_check_cuda.m4 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #####
  2. #
  3. # SYNOPSIS
  4. #
  5. # AX_CHECK_CUDA
  6. #
  7. # DESCRIPTION
  8. #
  9. # Figures out if CUDA Driver API/nvcc is available, i.e. existence of:
  10. # nvcc
  11. # cuda.h
  12. # libcuda.a
  13. #
  14. # If something isn't found, fails straight away.
  15. #
  16. # The following variables are substituted in the makefile:
  17. # NVCC : the nvcc compiler command.
  18. # NVCCFLAGS : nvcc specific flags
  19. # CUDA_CFLAGS : CUDA includes
  20. # CUDA_LDLIBS : CUDA libraries
  21. # NVCC_OK : Automake conditional (defined if CUDA is enabled)
  22. #
  23. # Defines HAVE_CUDA in config.h
  24. #
  25. # LICENCE
  26. # Public domain
  27. #
  28. #####
  29. AC_DEFUN([AX_CHECK_CUDA], [
  30. # Provide your CUDA path with this
  31. AC_ARG_WITH([cuda],
  32. [AS_HELP_STRING([--with-cuda=PATH],[prefix where CUDA is installed @<:@default=no@:>@])],
  33. [],
  34. [with_cuda=no])
  35. NVCC=no
  36. CUDA_CFLAGS=
  37. CUDA_LDLIBS=
  38. if test "x$with_cuda" != "xno"
  39. then
  40. # -----------------------------------------
  41. # Setup CUDA paths
  42. # -----------------------------------------
  43. if test "x$with_cuda" != "xyes"
  44. then
  45. AX_NORMALIZE_PATH([with_cuda], ["/"])
  46. CUDAPATH="$with_cuda"
  47. CUDA_CFLAGS+=" -I$with_cuda/include"
  48. CUDA_LDLIBS+=" -L$with_cuda/lib64"
  49. else
  50. AC_CHECK_FILE(/usr/local/cuda/,[CUDAPATH="/usr/local/cuda"],[])
  51. AC_CHECK_FILE(/usr/local/cuda/include,[CUDA_CFLAGS+=" -I/usr/local/cuda/include"],[CUDA_CFLAGS=""])
  52. AC_CHECK_FILE(/usr/local/cuda/lib64,[CUDA_LDLIBS+=" -L/usr/local/cuda/lib64"],[])
  53. fi
  54. CUDA_LDLIBS+=" -lcuda -lcudart -lcublas"
  55. # -----------------------------------------
  56. # Checking for nvcc
  57. # -----------------------------------------
  58. AC_PATH_PROG([NVCC],[nvcc],[no],[$PATH:$CUDAPATH/bin])
  59. if test "x$NVCC" = "xno"
  60. then
  61. AC_MSG_ERROR([Cannot find nvcc compiler. To enable CUDA, please add path to
  62. nvcc in the PATH environment variable and/or specify the path
  63. where CUDA is installed using: --with-cuda=PATH])
  64. fi
  65. # -----------------------------------------
  66. # Setup nvcc flags
  67. # -----------------------------------------
  68. AC_ARG_VAR(NVCCFLAGS,[Additional nvcc flags (example: NVCCFLAGS="-arch=compute_30 -code=sm_30")])
  69. if test x$DEBUG = xtrue
  70. then
  71. NVCCFLAGS+=" -g"
  72. else
  73. NVCCFLAGS+=" -O3"
  74. fi
  75. AC_ARG_ENABLE([emu],
  76. AC_HELP_STRING([--enable-emu],[turn on device emulation for CUDA]),
  77. [case "${enableval}" in
  78. yes) EMULATION=true;;
  79. no) EMULATION=false;;
  80. *) AC_MSG_ERROR([bad value ${enableval} for --enable-emu]);;
  81. esac],
  82. [EMULATION=false])
  83. if test x$EMULATION = xtrue
  84. then
  85. NVCCFLAGS+=" -deviceemu"
  86. fi
  87. # -----------------------------------------
  88. # Check if nvcc works
  89. # -----------------------------------------
  90. ac_compile_nvcc=no
  91. AC_MSG_CHECKING([whether nvcc works])
  92. cat>conftest.cu<<EOF
  93. __global__ static void test_cuda() {
  94. const int tid = threadIdx.x;
  95. const int bid = blockIdx.x;
  96. __syncthreads();
  97. }
  98. EOF
  99. if $NVCC -c $NVCCFLAGS conftest.cu &> /dev/null
  100. then
  101. ac_compile_nvcc=yes
  102. fi
  103. rm -f conftest.cu conftest.o
  104. AC_MSG_RESULT([$ac_compile_nvcc])
  105. if test "x$ac_compile_nvcc" = "xno"
  106. then
  107. AC_MSG_ERROR([CUDA compiler has problems.])
  108. fi
  109. # -----------------------------------------
  110. # Check for headers and libraries
  111. # -----------------------------------------
  112. ax_save_CXXFLAGS="${CXXFLAGS}"
  113. ax_save_LIBS="${LIBS}"
  114. CXXFLAGS="$CUDA_CFLAGS $CXXFLAGS"
  115. LIBS="$CUDA_LDLIBS $LIBS"
  116. # And the header and the lib
  117. AC_CHECK_HEADER([cuda.h], [], AC_MSG_FAILURE([Couldn't find cuda.h]), [#include <cuda.h>])
  118. AC_CHECK_HEADER([cuda_runtime_api.h], [], AC_MSG_FAILURE([Couldn't find cuda_runtime_api.h]), [#include <cuda_runtime_api.h>])
  119. AC_CHECK_HEADER([cublas.h], [], AC_MSG_FAILURE([Couldn't find cublas.h]), [#include <cublas.h>])
  120. AC_CHECK_LIB([cuda], [cuInit], [], AC_MSG_FAILURE([Couldn't find libcuda]))
  121. AC_CHECK_LIB([cudart], [cudaMalloc], [], AC_MSG_FAILURE([Couldn't find libcudart]))
  122. AC_CHECK_LIB([cublas], [cublasInit], [], AC_MSG_FAILURE([Couldn't find libcublas]))
  123. # Returning to the original flags
  124. CXXFLAGS=${ax_save_CXXFLAGS}
  125. LIBS=${ax_save_LIBS}
  126. AC_DEFINE(HAVE_CUDA,1,[Define if we have CUDA])
  127. fi
  128. # Announcing the new variables
  129. AC_SUBST([NVCC])
  130. AC_SUBST([NVCCFLAGS])
  131. AC_SUBST([CUDA_CFLAGS])
  132. AC_SUBST([CUDA_LDLIBS])
  133. AM_CONDITIONAL([NVCC_OK], [test "x$ac_compile_nvcc" = "xyes"])
  134. ])