]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Add CUDA quick test
authorDaniel Arndt <daniel.arndt@iwr.uni-heidelberg.de>
Fri, 20 Oct 2017 12:48:12 +0000 (14:48 +0200)
committerDaniel Arndt <daniel.arndt@iwr.uni-heidelberg.de>
Fri, 20 Oct 2017 13:05:51 +0000 (15:05 +0200)
tests/quick_tests/CMakeLists.txt
tests/quick_tests/cuda.cu [new file with mode: 0644]

index 925d38d5fc050f7b7d7ea336d8f6002478595cb6..f31178b9fbd2526c08f1790794dc48634ac98850 100644 (file)
@@ -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 (file)
index 0000000..2c65d06
--- /dev/null
@@ -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 <deal.II/base/exceptions.h>
+#include <array>
+#include <iostream>
+#include <numeric>
+
+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<double, n> host_x {};
+  std::iota(host_x.begin(), host_x.end(), 1);
+  std::array<double, n> 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;
+}

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.