cmake发现cuda但未能在Windows上找到cuda库

时间:2017-01-17 20:32:14

标签: windows cuda cmake

我有一个很小的cmake项目在Linux上工作得很好但在Windows 10(我尝试使用两台不同的计算机)上使用最新版本的cmake和CUDA 8失败。它发现CUDA很好,但找不到库。我的cmake文件:

cmake_minimum_required(VERSION 3.0)
project(myproject)

find_package(CUDA REQUIRED)

cuda_add_library(myproject STATIC matrix_mm.cu)
target_link_libraries(myproject ${CUDA_CUBLAS_LIBRARIES})

message(STATUS "")
message(STATUS "FoundCUDA              : ${CUDA_FOUND}")
message(STATUS "Cuda cublas libraries  : ${CUDA_CUBLAS_LIBRARIES}")

在同一文件夹中,我有标题matrix_mm.cuh:

#include <cstdlib>

namespace myproject {

float* cuda_mm(const float *a, const float *b, const size_t m, const size_t k, const size_t n);

} /* end namespace myproject */

和matrix_mm.cu:

#include <cublas_v2.h>
#include "matrix_mm.cuh"

namespace myproject {

// Adapted from https://solarianprogrammer.com/2012/05/31/matrix-multiplication-cuda-cublas-curand-thrust/

void gpu_blas_mmul(const float *a, const float *b, float *c, const size_t m, const size_t k, const size_t n) {
  int lda = m, ldb = k, ldc = m;
  const float alf = 1;
  const float bet = 0;
  const float *alpha = &alf;
  const float *beta = &bet;

  // Create a handle for CUBLAS
  cublasHandle_t handle;
  cublasCreate(&handle);

  // Do the actual multiplication
  cublasSgemm(handle, CUBLAS_OP_N, CUBLAS_OP_N, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc);

  // Destroy the handle
  cublasDestroy(handle);
}

float* cuda_mm(const float *a, const float *b, const size_t m, const size_t k, const size_t n) {
  size_t const a_bytes = m * k * sizeof(float);
  size_t const b_bytes = k * n * sizeof(float);
  size_t const c_bytes = m * n * sizeof(float);
  float* c = (float*)std::malloc(c_bytes);

  float *d_A, *d_B, *d_C;
  cudaMalloc(&d_A, a_bytes);
  cudaMalloc(&d_B, b_bytes);
  cudaMalloc(&d_C, c_bytes);

  cudaMemcpy(d_A, a, a_bytes, cudaMemcpyHostToDevice);
  cudaMemcpy(d_B, b, b_bytes, cudaMemcpyHostToDevice);

  gpu_blas_mmul(d_A, d_B, d_C, m, k, n);

  cudaMemcpy(c, d_C, c_bytes, cudaMemcpyDeviceToHost);

  cudaFree(d_A);
  cudaFree(d_B);
  cudaFree(d_C);

  return c;
}

} /* end namespace myproject */

在Linux上我得到:

-- FoundCUDA              : TRUE
-- Toolkit root           : /usr
-- Cuda cublas libraries  : /usr/lib/x86_64-linux-gnu/libcublas.so

在两台Windows 10计算机上,我得到了

-- FoundCUDA              : TRUE
-- Toolkit root           : C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v8.0
-- Cuda cublas libraries  : CUDA_cublas_LIBRARY-NOTFOUND;CUDA_cublas_device_LIBRARY-NOTFOUND

...当然它无法编译,因为链接器无法找到cublas。

我尝试了很多东西:使用lib SHARED而不是STATIC,我确保Cuda在Windows中使用#&环境变量等,但没有任何作用。

1 个答案:

答案 0 :(得分:1)

这是用于在Windows 10上使用CMake 3.7.1查找CUDA 8的CMake片段:

cmake_minimum_required(VERSION 3.7)
project(myproject)

# Check for CUDA ENV vars
IF(NOT DEFINED ENV{CUDA_PATH})
    MESSAGE(FATAL_ERROR "CUDA_PATH Environment variable is not set.")
ENDIF(NOT DEFINED ENV{CUDA_PATH})

# Set the toolkit path
FILE(TO_CMAKE_PATH "$ENV{CUDA_PATH}" CUDA_TOOLKIT_ROOT_DIR)
SET(CUDA_TOOLKIT_ROOT_DIR ${CUDA_TOOLKIT_ROOT_DIR} CACHE STRING "Root directory of the Cuda Library" FORCE)

# Find the package
find_package(CUDA REQUIRED)

# Create and interface library as a link target (Requires CMake 3.7.0+)
add_library(cuda INTERFACE)
set_target_properties(cuda PROPERTIES
    INTERFACE_INCLUDE_DIRECTORIES ${CUDA_INCLUDE_DIRS}
    INTERFACE_LINK_LIBRARIES "${CUDA_LIBRARIES};${CUDA_CUFFT_LIBRARIES};${CUDA_CUBLAS_LIBRARIES}"
)

SET(CUDA_HOST_COMPILATION_CPP ON)

cuda_add_library(myproject STATIC matrix_mm.cu)
target_link_libraries(myproject cuda)

我认为库周围的引号很重要,因为Windows路径将包含空格。

我还要确保删除缓存并重新生成项目。当变量值在表面上显示正确时(或者当您对非FORCE缓存变量进行更改时),这通常是错误的原因。