91 for (
int i = 0; i < D; i++) {
96 if (MAX_THREADS_PER_BLOCK == -1) {
102 if ((blockSize > 0) && (blockSize != THREADS_PER_BLOCK)) {
105 if (
meshDims[D-1] != MESH_DIMS.x) match =
false;
107 if (
meshDims[D-2] != MESH_DIMS.y) match =
false;
109 if (1 != MESH_DIMS.y) match =
false;
112 if (
meshDims[D-3] != MESH_DIMS.z) match =
false;
114 if (1 != MESH_DIMS.z) match =
false;
117 if (
meshDims[0] != MESH_DIMS.x) match =
false;
119 if (
meshDims[1] != MESH_DIMS.y) match =
false;
121 if (1 != MESH_DIMS.y) match =
false;
124 if (
meshDims[2] != MESH_DIMS.z) match =
false;
126 if (1 != MESH_DIMS.z) match =
false;
140 if (D > 1) MESH_DIMS.y =
meshDims[D-2];
141 if (D > 2) MESH_DIMS.z =
meshDims[D-3];
144 if (D > 1) MESH_DIMS.y =
meshDims[1];
145 if (D > 2) MESH_DIMS.z =
meshDims[2];
149 bool manualBlockSize =
false;
151 blockSize = MAX_THREADS_PER_BLOCK;
153 manualBlockSize =
true;
154 if ((blockSize & (blockSize - 1)) != 0) {
155 UTIL_THROW(
"Manual block size entry must be a power of 2.");
157 if (blockSize < WARP_SIZE) {
158 Log::file() <<
"\nRequested threads per block: " << blockSize
159 <<
"\nWarp size: " << WARP_SIZE << std::endl;
160 UTIL_THROW(
"Threads per block cannot be smaller than warp size.");
163 THREADS_PER_BLOCK = blockSize;
167 if (MESH_DIMS.x % WARP_SIZE == 0) {
171 BLOCK_DIMS.x = WARP_SIZE;
172 }
else if ((MESH_DIMS.x & (MESH_DIMS.x - 1)) == 0) {
176 BLOCK_DIMS.x = MESH_DIMS.x;
181 if ((MESH_DIMS.x / (WARP_SIZE/2)) % 2 == 0) {
183 BLOCK_DIMS.x = WARP_SIZE / 2;
186 BLOCK_DIMS.x = WARP_SIZE;
192 while (BLOCK_DIMS.y < MESH_DIMS.y) {
199 while (BLOCK_DIMS.z < MESH_DIMS.z) {
204 if (BLOCK_DIMS.z > 64) {
209 GRID_DIMS.z = MESH_DIMS.z / BLOCK_DIMS.z;
210 if (MESH_DIMS.z % BLOCK_DIMS.z) GRID_DIMS.z++;
213 blockSize = BLOCK_DIMS.x * BLOCK_DIMS.y * BLOCK_DIMS.z;
214 if (blockSize >= THREADS_PER_BLOCK) {
216 while (blockSize > THREADS_PER_BLOCK) {
218 int yOvershoot = BLOCK_DIMS.y * GRID_DIMS.y - MESH_DIMS.y;
219 int zOvershoot = BLOCK_DIMS.z * GRID_DIMS.z - MESH_DIMS.z;
222 if ((yOvershoot > zOvershoot) || (BLOCK_DIMS.z == 1)) {
225 GRID_DIMS.y = MESH_DIMS.y / BLOCK_DIMS.y;
226 if (MESH_DIMS.y % BLOCK_DIMS.y) GRID_DIMS.y++;
229 GRID_DIMS.z = MESH_DIMS.z / BLOCK_DIMS.z;
230 if (MESH_DIMS.z % BLOCK_DIMS.z) GRID_DIMS.z++;
233 blockSize = BLOCK_DIMS.x * BLOCK_DIMS.y * BLOCK_DIMS.z;
241 while ((BLOCK_DIMS.x < MESH_DIMS.x) && (blockSize < THREADS_PER_BLOCK))
246 if (blockSize < WARP_SIZE) {
249 while (blockSize < WARP_SIZE) {
255 THREADS_PER_BLOCK = blockSize;
259 GRID_DIMS.x = MESH_DIMS.x / BLOCK_DIMS.x;
260 if (MESH_DIMS.x % BLOCK_DIMS.x) GRID_DIMS.x++;
263 if ((manualBlockSize) && (blockSize < THREADS_PER_BLOCK)) {
264 Log::file() <<
"WARNING: The number of threads per block ("
266 <<
") will be smaller than \nthe requested size of "
267 << THREADS_PER_BLOCK <<
"." << std::endl;
271 if ((MESH_DIMS.x % BLOCK_DIMS.x) || (MESH_DIMS.y % BLOCK_DIMS.y) ||
272 (MESH_DIMS.z % BLOCK_DIMS.z)) {
273 UNUSED_THREADS =
true;
275 UNUSED_THREADS =
false;
292 if ((MAX_THREADS_PER_BLOCK & (MAX_THREADS_PER_BLOCK - 1)) != 0) {
293 Log::file() <<
"\nMax threads per block: " << MAX_THREADS_PER_BLOCK
295 UTIL_THROW(
"Max threads per block must be a power of two.");
299 if (MAX_THREADS_PER_BLOCK % WARP_SIZE != 0)
301 Log::file() <<
"\nMax threads per block: " << MAX_THREADS_PER_BLOCK
302 <<
"\nWarp size: " << WARP_SIZE << std::endl;
303 UTIL_THROW(
"Max threads per block must be a multiple of warp size.");
307 if (THREADS_PER_BLOCK % WARP_SIZE != 0)
309 Log::file() <<
"\nThreads per block: " << THREADS_PER_BLOCK
310 <<
"\nWarp size: " << WARP_SIZE << std::endl;
311 UTIL_THROW(
"Threads per block must be a multiple of warp size.");
315 if ((BLOCK_DIMS.x < 1) || (BLOCK_DIMS.y < 1) || (BLOCK_DIMS.z < 1)) {
316 Log::file() <<
"\nBlock dimensions: " << BLOCK_DIMS.x <<
" "
317 << BLOCK_DIMS.y <<
" " << BLOCK_DIMS.z << std::endl;
318 UTIL_THROW(
"Block dimensions must each be a power of two.");
322 if (BLOCK_DIMS.x * BLOCK_DIMS.y * BLOCK_DIMS.z != THREADS_PER_BLOCK) {
323 UTIL_THROW(
"THREADS_PER_BLOCK not properly set.");
327 if ((BLOCK_DIMS.x * GRID_DIMS.x < MESH_DIMS.x) ||
328 (BLOCK_DIMS.y * GRID_DIMS.y < MESH_DIMS.y) ||
329 (BLOCK_DIMS.z * GRID_DIMS.z < MESH_DIMS.z)) {
330 Log::file() <<
"\nBlock dimensions: " << BLOCK_DIMS.x <<
" "
331 << BLOCK_DIMS.y <<
" " << BLOCK_DIMS.z << std::endl;
332 Log::file() <<
"\nGrid dimensions: " << GRID_DIMS.x <<
" "
333 << GRID_DIMS.y <<
" " << GRID_DIMS.z << std::endl;
334 Log::file() <<
"\nMesh dimensions: " << MESH_DIMS.x <<
" "
335 << MESH_DIMS.y <<
" " << MESH_DIMS.z << std::endl;
336 UTIL_THROW(
"Thread grid smaller than the requested mesh.");
344 if (MAX_THREADS_PER_SM % MAX_THREADS_PER_BLOCK != 0) {
345 Log::file() <<
"WARNING: The number of threads per block ("
346 << MAX_THREADS_PER_BLOCK
347 <<
") is not an even divisor of the maximum number"
348 <<
" of threads per streaming multiprocessor ("
349 << MAX_THREADS_PER_SM
350 <<
"). Performance will be suboptimal."