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