Cholla 3.0.1-dev
Cholla - Massively parallel hydro on GPUs
Loading...
Searching...
No Matches
cuda_utilities.h
1
8#pragma once
9
10#include <string>
11
12// Local Includes
13#include "../global/global.h"
14#include "../global/global_cuda.h"
15#include "../utils/gpu.hpp"
16
17namespace cuda_utilities
18{
29inline __host__ __device__ void compute3DIndices(int const &id, int const &nx, int const &ny, int &xid, int &yid,
30 int &zid)
31{
32 zid = id / (nx * ny);
33 yid = (id - zid * nx * ny) / nx;
34 xid = id - zid * nx * ny - yid * nx;
35}
36
47inline __host__ __device__ int compute1DIndex(int const &xid, int const &yid, int const &zid, int const &nx,
48 int const &ny)
49{
50 return xid + yid * nx + zid * nx * ny;
51}
52
53inline __host__ __device__ void Get_Real_Indices(int const &n_ghost, int const &nx, int const &ny, int const &nz,
54 int &is, int &ie, int &js, int &je, int &ks, int &ke)
55{
56 is = n_ghost;
57 ie = nx - n_ghost;
58 if (ny == 1) {
59 js = 0;
60 je = 1;
61 } else {
62 js = n_ghost;
63 je = ny - n_ghost;
64 }
65 if (nz == 1) {
66 ks = 0;
67 ke = 1;
68 } else {
69 ks = n_ghost;
70 ke = nz - n_ghost;
71 }
72}
73
80inline void initGpuMemory(Real *ptr, size_t N) { GPU_Error_Check(cudaMemset(ptr, 0, N)); }
81
82// =====================================================================
90template <typename T>
92 public:
104 AutomaticLaunchParams(T &kernel, size_t numElements = 0)
105 {
106 // Get the max number of threads per block allowed by this kernel
107 cudaFuncAttributes kernel_attrs{};
108 GPU_Error_Check(cudaFuncGetAttributes(&kernel_attrs, reinterpret_cast<const void *>(&kernel)));
109
110 // Determine the launch parameters
111 cudaOccupancyMaxPotentialBlockSize(&numBlocks, &threadsPerBlock, kernel, 0, kernel_attrs.maxThreadsPerBlock);
112
113 if (numElements > 0) {
114 // This line is needed to check that threadsPerBlock isn't zero. Somewhere inside
115 // cudaOccupancyMaxPotentialBlockSize threadsPerBlock can be zero according to clang-tidy so this line sets it to
116 // a more reasonable value
117 threadsPerBlock = (threadsPerBlock == 0) ? TPB : threadsPerBlock;
118
119 // Compute the number of blocks
120 numBlocks = (numElements + threadsPerBlock - 1) / threadsPerBlock;
121 }
122 }
123
126
128 int get_threadsPerBlock() const { return threadsPerBlock; }
130 int get_numBlocks() const { return numBlocks; }
131
132 private:
134 int threadsPerBlock;
136 int numBlocks;
137};
138// =====================================================================
139
140// =====================================================================
146void Print_GPU_Memory_Usage(std::string const &additional_text = "");
147// =====================================================================
148} // end namespace cuda_utilities
Struct to determine the optimal number of blocks and threads per block to use when launching a kernel...
Definition cuda_utilities.h:91
int get_numBlocks() const
Getter for numBlocks.
Definition cuda_utilities.h:130
AutomaticLaunchParams(T &kernel, size_t numElements=0)
Construct a new AutomaticLaunchParams object. By default it generates values of numBlocks and threads...
Definition cuda_utilities.h:104
~AutomaticLaunchParams()=default
Defaulted Destructor.
int get_threadsPerBlock() const
Getter for threadsPerBlock.
Definition cuda_utilities.h:128