From ba9463b16f6aa7f7d1a6fd6d81c93d322c76b803 Mon Sep 17 00:00:00 2001 From: Daniel Arndt Date: Fri, 20 Oct 2017 14:48:12 +0200 Subject: [PATCH] Add CUDA quick test --- tests/quick_tests/CMakeLists.txt | 11 +++++- tests/quick_tests/cuda.cu | 62 ++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 tests/quick_tests/cuda.cu diff --git a/tests/quick_tests/CMakeLists.txt b/tests/quick_tests/CMakeLists.txt index 925d38d5fc..f31178b9fb 100644 --- a/tests/quick_tests/CMakeLists.txt +++ b/tests/quick_tests/CMakeLists.txt @@ -36,7 +36,11 @@ MACRO(make_quicktest test_basename build_name mpi_run) STRING(TOLOWER ${build_name} _build_lowercase) SET(_target ${test_basename}.${_build_lowercase}) LIST(APPEND ALL_TESTS "${_target}") - ADD_EXECUTABLE(${_target} EXCLUDE_FROM_ALL ${test_basename}.cc) + IF(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${test_basename}.cc") + ADD_EXECUTABLE(${_target} EXCLUDE_FROM_ALL ${test_basename}.cc) + ELSE() + ADD_EXECUTABLE(${_target} EXCLUDE_FROM_ALL ${test_basename}.cu) + ENDIF() DEAL_II_INSOURCE_SETUP_TARGET(${_target} ${build_name}) IF("${mpi_run}" STREQUAL "") @@ -164,6 +168,11 @@ IF (DEAL_II_WITH_ASSIMP) make_quicktest("assimp" ${_mybuild} "") ENDIF() +# Test CUDA +IF (DEAL_II_WITH_CUDA) + make_quicktest("cuda" ${_mybuild} "") +ENDIF() + # A custom test target: ADD_CUSTOM_TARGET(test diff --git a/tests/quick_tests/cuda.cu b/tests/quick_tests/cuda.cu new file mode 100644 index 0000000000..2c65d06cd2 --- /dev/null +++ b/tests/quick_tests/cuda.cu @@ -0,0 +1,62 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2017 by the deal.II authors +// +// This file is part of the deal.II library. +// +// The deal.II library is free software; you can use it, redistribute +// it, and/or modify it under the terms of the GNU Lesser General +// Public License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// The full text of the license can be found in the file LICENSE at +// the top level of the deal.II distribution. +// +// --------------------------------------------------------------------- + + +#include +#include +#include +#include + +using namespace dealii; + +__global__ void double_value(double *x, double *y) +{ + y[threadIdx.x] = 2. * x[threadIdx.x]; +} + +int main() +{ + constexpr int n = 4; + + std::array host_x {}; + std::iota(host_x.begin(), host_x.end(), 1); + std::array host_y {}; + + // Copy input data to device. + double *device_x; + double *device_y; + cudaMalloc(&device_x, n * sizeof(double)); + cudaMalloc(&device_y, n * sizeof(double)); + cudaMemcpy(device_x, host_x.data(), n * sizeof(double), + cudaMemcpyHostToDevice); + + // Launch the kernel. + double_value<<<1, n>>>(device_x, device_y); + + // Copy output data to host. + cudaDeviceSynchronize(); + cudaMemcpy(host_y.data(), device_y, n * sizeof(double), + cudaMemcpyDeviceToHost); + + // Print the results and test + for (int i = 0; i < n; ++i) + { + std::cout << "y[" << i << "] = " << host_y[i] << "\n"; + AssertThrow(std::abs(host_y[i]-2*host_x[i])<1.e-10, ExcInternalError()); + } + + cudaDeviceReset(); + return 0; +} -- 2.39.5