CUDAAlgorithms.hpp
Go to the documentation of this file.
1 // This code is based on Jet framework.
2 // Copyright (c) 2018 Doyub Kim
3 // CubbyFlow is voxel-based fluid simulation engine for computer games.
4 // Copyright (c) 2020 CubbyFlow Team
5 // Core Part: Chris Ohk, Junwoo Hwang, Jihong Sin, Seungwoo Yoo
6 // AI Part: Dongheon Cho, Minseo Kim
7 // We are making my contributions/submissions to this project solely in our
8 // personal capacity and are not conveying any rights to any intellectual
9 // property of any third parties.
10 
11 #ifndef CUBBYFLOW_CUDA_ALGORITHMS_HPP
12 #define CUBBYFLOW_CUDA_ALGORITHMS_HPP
13 
14 #ifdef CUBBYFLOW_USE_CUDA
15 
16 #include <Core/CUDA/CUDAUtils.hpp>
17 
18 #include <stdio.h>
19 
20 namespace CubbyFlow
21 {
22 #ifdef __CUDACC__
23 
24 template <typename T>
25 __global__ void CUDAFillKernel(T* dst, size_t n, T val)
26 {
27  size_t i = blockIdx.x * blockDim.x + threadIdx.x;
28 
29  if (i < n)
30  {
31  dst[i] = val;
32  }
33 }
34 
35 template <typename T>
36 void CUDAFill(T* dst, size_t n, const T& val)
37 {
38  if (n == 0)
39  {
40  return;
41  }
42 
43  unsigned int numBlocks, numThreads;
44  CUDAComputeGridSize((unsigned int)n, 256, numBlocks, numThreads);
45  CUDAFillKernel<<<numBlocks, numThreads>>>(dst, n, val);
46 
47  CUBBYFLOW_CUDA_CHECK_LAST_ERROR("Failed executing CUDAFillKernel");
48 }
49 
50 #endif
51 
52 template <typename T>
53 __host__ __device__ inline void CUDASwap(T& a, T& b)
54 {
55  T tmp = a;
56  a = b;
57  b = tmp;
58 }
59 
60 // TODO: Rename it to something else to indicate this is a memory copy.
61 // TODO: Also, having CUDA prefix may collide with CUDA API.
62 template <typename T>
63 void CUDACopy(const T* src, size_t n, T* dst,
64  cudaMemcpyKind kind = cudaMemcpyDeviceToDevice)
65 {
66  CUBBYFLOW_CUDA_CHECK(cudaMemcpy(dst, src, n * sizeof(T), kind));
67 }
68 
69 template <typename T>
70 void CUDACopyDeviceToDevice(const T* src, size_t n, T* dst)
71 {
72  CUDACopy(src, n, dst, cudaMemcpyDeviceToDevice);
73 }
74 
75 template <typename T>
76 void CUDACopyHostToDevice(const T* src, size_t n, T* dst)
77 {
78  CUDACopy(src, n, dst, cudaMemcpyHostToDevice);
79 }
80 
81 template <typename T>
82 void CUDACopyDeviceToHost(const T* src, size_t n, T* dst)
83 {
84  CUDACopy(src, n, dst, cudaMemcpyDeviceToHost);
85 }
86 } // namespace CubbyFlow
87 
88 #endif
89 
90 #endif
Definition: pybind11Utils.hpp:20