| 1 |
/* ---------------------------------------------------------------------------- |
| 2 |
* SymForce - Copyright 2025, Skydio, Inc. |
| 3 |
* This source code is under the Apache 2.0 license found in the LICENSE file. |
| 4 |
* ---------------------------------------------------------------------------- */ |
| 5 |
|
| 6 |
#pragma once |
| 7 |
|
| 8 |
#include <stdio.h> |
| 9 |
|
| 10 |
#include <cooperative_groups.h> |
| 11 |
#include <cooperative_groups/memcpy_async.h> |
| 12 |
#include <cooperative_groups/reduce.h> |
| 13 |
#include <cuda_runtime.h> |
| 14 |
|
| 15 |
#include "shared_indices.h" |
| 16 |
|
| 17 |
namespace cg = cooperative_groups; |
| 18 |
|
| 19 |
// READ INDEXED |
| 20 |
namespace caspar { |
| 21 |
|
| 22 |
template <uint block_size, typename KernelT, typename StorageT, typename VecT> |
| 23 |
__forceinline__ __device__ void ReadIdx1(const StorageT* const input, const uint offset, |
| 24 |
const uint idx, KernelT& x) { |
| 25 |
x = input[offset + idx]; |
| 26 |
} |
| 27 |
|
| 28 |
template <uint block_size, typename KernelT, typename StorageT, typename VecT> |
| 29 |
__forceinline__ __device__ void ReadIdx2(const StorageT* const input, const uint offset, |
| 30 |
const uint idx, KernelT& x, KernelT& y) { |
| 31 |
const VecT tmp = *reinterpret_cast<const VecT*>(&input[offset + idx * 2]); |
| 32 |
x = tmp.x; |
| 33 |
y = tmp.y; |
| 34 |
} |
| 35 |
|
| 36 |
template <uint block_size, typename KernelT, typename StorageT, typename VecT> |
| 37 |
__forceinline__ __device__ void ReadIdx3(const StorageT* const input, const uint offset, |
| 38 |
const uint idx, KernelT& x, KernelT& y, KernelT& z) { |
| 39 |
const VecT tmp = *reinterpret_cast<const VecT*>(&input[offset + idx * 4]); |
| 40 |
x = tmp.x; |
| 41 |
y = tmp.y; |
| 42 |
z = tmp.z; |
| 43 |
} |
| 44 |
|
| 45 |
template <uint block_size, typename KernelT, typename StorageT, typename VecT> |
| 46 |
__forceinline__ __device__ void ReadIdx4(const StorageT* const input, const uint offset, |
| 47 |
const uint idx, KernelT& x, KernelT& y, KernelT& z, |
| 48 |
KernelT& w) { |
| 49 |
const VecT tmp = *reinterpret_cast<const VecT*>(&input[offset + idx * 4]); |
| 50 |
x = tmp.x; |
| 51 |
y = tmp.y; |
| 52 |
z = tmp.z; |
| 53 |
w = tmp.w; |
| 54 |
} |
| 55 |
// READ INDEXED WITH DEFAULT |
| 56 |
template <uint block_size, typename KernelT, typename StorageT, typename VecT> |
| 57 |
|
| 58 |
__forceinline__ __device__ void ReadIdxWithDefault1(const StorageT* const input, const uint offset, |
| 59 |
const uint num_alloc, const int idx, KernelT& x, |
| 60 |
const KernelT x_default) { |
| 61 |
if (idx >= 0 && idx < num_alloc) { |
| 62 |
x = input[offset + idx]; |
| 63 |
} else { |
| 64 |
x = x_default; |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
template <uint block_size, typename KernelT, typename StorageT, typename VecT> |
| 69 |
|
| 70 |
__forceinline__ __device__ void ReadIdxWithDefault2(const StorageT* const input, const uint offset, |
| 71 |
const uint num_alloc, const int idx, KernelT& x, |
| 72 |
KernelT& y, KernelT x_default, |
| 73 |
KernelT y_default) { |
| 74 |
VecT tmp; |
| 75 |
if (idx >= 0 && idx < num_alloc) { |
| 76 |
tmp = *reinterpret_cast<const VecT*>(&input[offset + idx * 2]); |
| 77 |
x = tmp.x; |
| 78 |
y = tmp.y; |
| 79 |
} else { |
| 80 |
x = x_default; |
| 81 |
y = y_default; |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
template <uint block_size, typename KernelT, typename StorageT, typename VecT> |
| 86 |
|
| 87 |
__forceinline__ __device__ void ReadIdxWithDefault3(const StorageT* const input, const uint offset, |
| 88 |
const uint num_alloc, const int idx, KernelT& x, |
| 89 |
KernelT& y, KernelT& z, KernelT x_default, |
| 90 |
KernelT y_default, KernelT z_default) { |
| 91 |
VecT tmp; |
| 92 |
if (idx >= 0 && idx < num_alloc) { |
| 93 |
tmp = *reinterpret_cast<const VecT*>(&input[offset + idx * 4]); |
| 94 |
x = tmp.x; |
| 95 |
y = tmp.y; |
| 96 |
z = tmp.z; |
| 97 |
} else { |
| 98 |
x = x_default; |
| 99 |
y = y_default; |
| 100 |
z = z_default; |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
template <uint block_size, typename KernelT, typename StorageT, typename VecT> |
| 105 |
|
| 106 |
__forceinline__ __device__ void ReadIdxWithDefault4(const StorageT* const input, const uint offset, |
| 107 |
const uint num_alloc, const int idx, KernelT& x, |
| 108 |
KernelT& y, KernelT& z, KernelT& w, |
| 109 |
KernelT x_default, KernelT y_default, |
| 110 |
KernelT z_default, KernelT w_default) { |
| 111 |
VecT tmp; |
| 112 |
if (idx >= 0 && idx < num_alloc) { |
| 113 |
tmp = *reinterpret_cast<const VecT*>(&input[offset + idx * 4]); |
| 114 |
x = tmp.x; |
| 115 |
y = tmp.y; |
| 116 |
z = tmp.z; |
| 117 |
w = tmp.w; |
| 118 |
} else { |
| 119 |
x = x_default; |
| 120 |
y = y_default; |
| 121 |
z = z_default; |
| 122 |
w = w_default; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
// WRITE INDEXED |
| 127 |
|
| 128 |
template <uint block_size, typename KernelT, typename StorageT, typename VecT> |
| 129 |
|
| 130 |
__forceinline__ __device__ void WriteIdx1(StorageT* const output, const uint offset, const uint idx, |
| 131 |
const KernelT x) { |
| 132 |
output[offset + idx] = x; |
| 133 |
} |
| 134 |
|
| 135 |
template <uint block_size, typename KernelT, typename StorageT, typename VecT> |
| 136 |
|
| 137 |
__forceinline__ __device__ void WriteIdx2(StorageT* const output, const uint offset, const uint idx, |
| 138 |
const KernelT x, const KernelT y) { |
| 139 |
VecT tmp; |
| 140 |
tmp.x = x; |
| 141 |
tmp.y = y; |
| 142 |
*reinterpret_cast<VecT*>(&output[offset + idx * 2]) = tmp; |
| 143 |
} |
| 144 |
|
| 145 |
template <uint block_size, typename KernelT, typename StorageT, typename VecT> |
| 146 |
|
| 147 |
__forceinline__ __device__ void WriteIdx3(StorageT* const output, const uint offset, const uint idx, |
| 148 |
const KernelT x, const KernelT y, const KernelT z) { |
| 149 |
VecT tmp; |
| 150 |
tmp.x = x; |
| 151 |
tmp.y = y; |
| 152 |
tmp.z = z; |
| 153 |
*reinterpret_cast<VecT*>(&output[offset + idx * 4]) = tmp; |
| 154 |
} |
| 155 |
|
| 156 |
template <uint block_size, typename KernelT, typename StorageT, typename VecT> |
| 157 |
|
| 158 |
__forceinline__ __device__ void WriteIdx4(StorageT* const output, const uint offset, const uint idx, |
| 159 |
const KernelT x, const KernelT y, const KernelT z, |
| 160 |
const KernelT w) { |
| 161 |
VecT tmp; |
| 162 |
tmp.x = x; |
| 163 |
tmp.y = y; |
| 164 |
tmp.z = z; |
| 165 |
tmp.w = w; |
| 166 |
*reinterpret_cast<VecT*>(&output[offset + idx * 4]) = tmp; |
| 167 |
} |
| 168 |
|
| 169 |
// WRITE ADD |
| 170 |
template <uint block_size, typename KernelT, typename StorageT, typename VecT> |
| 171 |
|
| 172 |
__forceinline__ __device__ void AddIdx1(StorageT* const output, const uint offset, const uint idx, |
| 173 |
const KernelT x) { |
| 174 |
output[offset + idx] += x; |
| 175 |
} |
| 176 |
|
| 177 |
template <uint block_size, typename KernelT, typename StorageT, typename VecT> |
| 178 |
|
| 179 |
__forceinline__ __device__ void AddIdx2(StorageT* const output, const uint offset, const uint idx, |
| 180 |
const KernelT x, const KernelT y) { |
| 181 |
const VecT existing = *reinterpret_cast<const VecT*>(&output[offset + idx * 2]); |
| 182 |
VecT tmp; |
| 183 |
tmp.x = x + existing.x; |
| 184 |
tmp.y = y + existing.y; |
| 185 |
*reinterpret_cast<VecT*>(&output[offset + idx * 2]) = tmp; |
| 186 |
} |
| 187 |
|
| 188 |
template <uint block_size, typename KernelT, typename StorageT, typename VecT> |
| 189 |
|
| 190 |
__forceinline__ __device__ void AddIdx3(StorageT* const output, const uint offset, const uint idx, |
| 191 |
const KernelT x, const KernelT y, const KernelT z) { |
| 192 |
const VecT existing = *reinterpret_cast<const VecT*>(&output[offset + idx * 4]); |
| 193 |
VecT tmp; |
| 194 |
tmp.x = x + existing.x; |
| 195 |
tmp.y = y + existing.y; |
| 196 |
tmp.z = z + existing.z; |
| 197 |
*reinterpret_cast<VecT*>(&output[offset + idx * 4]) = tmp; |
| 198 |
} |
| 199 |
|
| 200 |
template <uint block_size, typename KernelT, typename StorageT, typename VecT> |
| 201 |
|
| 202 |
__forceinline__ __device__ void AddIdx4(StorageT* const output, const uint offset, const uint idx, |
| 203 |
const KernelT x, const KernelT y, const KernelT z, |
| 204 |
const KernelT w) { |
| 205 |
const VecT existing = *reinterpret_cast<const VecT*>(&output[offset + idx * 4]); |
| 206 |
VecT tmp; |
| 207 |
tmp.x = x + existing.x; |
| 208 |
tmp.y = y + existing.y; |
| 209 |
tmp.z = z + existing.z; |
| 210 |
tmp.w = w + existing.w; |
| 211 |
*reinterpret_cast<VecT*>(&output[offset + idx * 4]) = tmp; |
| 212 |
} |
| 213 |
|
| 214 |
// WRITE SUM |
| 215 |
/** |
| 216 |
* The WriteSumx writes data to shared local data. |
| 217 |
* flush_sum or FlushSumBlock should be called after all WriteSumx calls to perform reduction |
| 218 |
* and write to the output. |
| 219 |
*/ |
| 220 |
template <typename KernelT, typename StorageT> |
| 221 |
__forceinline__ __device__ void WriteSum1(StorageT* const inout_shared, const KernelT x) { |
| 222 |
inout_shared[threadIdx.x] = x; |
| 223 |
} |
| 224 |
|
| 225 |
template <typename KernelT, typename StorageT> |
| 226 |
__forceinline__ __device__ void WriteSum2(StorageT* const inout_shared, const KernelT x, |
| 227 |
const KernelT y) { |
| 228 |
inout_shared[threadIdx.x * 2 + 0] = x; |
| 229 |
inout_shared[threadIdx.x * 2 + 1] = y; |
| 230 |
} |
| 231 |
|
| 232 |
template <typename KernelT, typename StorageT> |
| 233 |
__forceinline__ __device__ void WriteSum3(StorageT* const inout_shared, const KernelT x, |
| 234 |
const KernelT y, const KernelT z) { |
| 235 |
inout_shared[threadIdx.x * 3 + 0] = x; |
| 236 |
inout_shared[threadIdx.x * 3 + 1] = y; |
| 237 |
inout_shared[threadIdx.x * 3 + 2] = z; |
| 238 |
} |
| 239 |
|
| 240 |
template <typename KernelT, typename StorageT> |
| 241 |
__forceinline__ __device__ void WriteSum4(StorageT* const inout_shared, const KernelT x, |
| 242 |
const KernelT y, const KernelT z, const KernelT w) { |
| 243 |
inout_shared[threadIdx.x * 4 + 0] = x; |
| 244 |
inout_shared[threadIdx.x * 4 + 1] = y; |
| 245 |
inout_shared[threadIdx.x * 4 + 2] = z; |
| 246 |
inout_shared[threadIdx.x * 4 + 3] = w; |
| 247 |
} |
| 248 |
|
| 249 |
/** |
| 250 |
* Function used to perform collaborative reductions. Read more on caspar.argtypes.accessors.Sum. |
| 251 |
*/ |
| 252 |
template <uint dim_target, typename StorageT> |
| 253 |
__forceinline__ __device__ void FlushSumShared(StorageT* const output, const uint offset, |
| 254 |
const SharedIndex* const indices, |
| 255 |
StorageT* const inout_shared) { |
| 256 |
__syncthreads(); |
| 257 |
|
| 258 |
const SharedIndex idx = indices[threadIdx.x]; |
| 259 |
uint unique = 0xffffffff; |
| 260 |
if (idx.argsort != 0xffff) { // 0xffff indicates the thread is not used. |
| 261 |
unique = indices[indices[idx.argsort].target].unique; |
| 262 |
} |
| 263 |
const cg::coalesced_group group = cg::labeled_partition(cg::coalesced_threads(), unique); |
| 264 |
|
| 265 |
#pragma unroll |
| 266 |
for (int i = 0; i < dim_target; i++) { |
| 267 |
const SharedIndex idx = indices[threadIdx.x]; |
| 268 |
StorageT tot; |
| 269 |
if (idx.argsort != 0xffff) { // 0xffff indicates the thread is not used. |
| 270 |
tot = cg::reduce(group, inout_shared[idx.argsort * dim_target + i], cg::plus<StorageT>()); |
| 271 |
} |
| 272 |
__syncthreads(); |
| 273 |
inout_shared[threadIdx.x * dim_target + i] = 0.0f; |
| 274 |
__syncthreads(); |
| 275 |
|
| 276 |
// 0xffff indicates the thread is not used. |
| 277 |
if (idx.argsort != 0xffff && group.thread_rank() == 0) { |
| 278 |
atomicAdd_block(&inout_shared[indices[idx.argsort].target * dim_target + i], tot); |
| 279 |
} |
| 280 |
__syncthreads(); |
| 281 |
} |
| 282 |
|
| 283 |
constexpr uint dim_aligned = dim_target == 3 ? 4 : dim_target; |
| 284 |
for (int i = 0; i < dim_target; i++) { |
| 285 |
const uint val_idx = threadIdx.x + blockDim.x * i; |
| 286 |
const uint obj_idx = indices[val_idx / dim_target].unique; |
| 287 |
const uint elem = val_idx % dim_target; |
| 288 |
// 0xffffffff indicates this thread has nothing to write. |
| 289 |
if (obj_idx == 0xffffffff) { |
| 290 |
break; |
| 291 |
} |
| 292 |
atomicAdd(&output[offset + obj_idx * dim_aligned + elem], inout_shared[val_idx]); |
| 293 |
} |
| 294 |
__syncthreads(); |
| 295 |
} |
| 296 |
|
| 297 |
/** |
| 298 |
* Function used to perform a reduction over the block. |
| 299 |
* |
| 300 |
* Read more on caspar.argtypes.accessors.BlockSum. |
| 301 |
*/ |
| 302 |
template <uint dim_target, typename StorageT> |
| 303 |
__forceinline__ __device__ void FlushSumBlock(StorageT* const output, StorageT* const inout_shared, |
| 304 |
const bool valid) { |
| 305 |
__syncthreads(); |
| 306 |
const cg::coalesced_group group = cg::binary_partition(cg::coalesced_threads(), valid); |
| 307 |
constexpr uint dim_aligned = dim_target == 3 ? 4 : dim_target; |
| 308 |
|
| 309 |
#pragma unroll |
| 310 |
for (int i = 0; i < dim_target; i++) { |
| 311 |
StorageT tot; |
| 312 |
|
| 313 |
if (valid) { |
| 314 |
tot = cg::reduce(group, inout_shared[threadIdx.x * dim_target + i], cg::plus<StorageT>()); |
| 315 |
} |
| 316 |
__syncthreads(); |
| 317 |
inout_shared[threadIdx.x * dim_target + i] = 0.0f; |
| 318 |
__syncthreads(); |
| 319 |
|
| 320 |
if (valid && group.thread_rank() == 0) { |
| 321 |
// TODO(Emil Martes): use first warp to reduce instead of atomicAdd_block |
| 322 |
atomicAdd_block(&inout_shared[i], tot); |
| 323 |
} |
| 324 |
__syncthreads(); |
| 325 |
} |
| 326 |
for (int i = threadIdx.x; i < dim_target; i += blockDim.x) { |
| 327 |
output[blockIdx.x * dim_aligned + i] = inout_shared[i]; |
| 328 |
} |
| 329 |
} |
| 330 |
template <typename StorageT> |
| 331 |
__forceinline__ __device__ void SumStore(StorageT* const shared_tmp, StorageT* const inout_shared, |
| 332 |
const uint offset, const bool valid, StorageT data) { |
| 333 |
auto group = cg::tiled_partition<32>(cg::this_thread_block()); |
| 334 |
|
| 335 |
__syncthreads(); |
| 336 |
StorageT tot = cg::reduce(group, valid ? data : 0.0f, cg::plus<StorageT>()); |
| 337 |
if (group.thread_rank() == 0) { |
| 338 |
inout_shared[group.meta_group_rank()] = tot; |
| 339 |
} |
| 340 |
__syncthreads(); |
| 341 |
if (group.meta_group_rank() == 0) { |
| 342 |
tot = cg::reduce(group, inout_shared[group.thread_rank()], cg::plus<StorageT>()); |
| 343 |
if (group.thread_rank() == 0) { |
| 344 |
shared_tmp[offset] = tot; |
| 345 |
} |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
template <typename StorageT> |
| 350 |
__forceinline__ __device__ void SumFlushFinal(const StorageT* const shared_tmp, |
| 351 |
StorageT* const output, const uint dim) { |
| 352 |
__syncthreads(); |
| 353 |
if (threadIdx.x < dim) { |
| 354 |
atomicAdd(&output[threadIdx.x], shared_tmp[threadIdx.x]); |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
/** |
| 359 |
* Function used to perform a reduction over a block and add to the output. |
| 360 |
* |
| 361 |
* Read more on caspar.argtypes.accessors.BlockSumAdd. |
| 362 |
*/ |
| 363 |
template <uint dim_target, typename StorageT> |
| 364 |
__forceinline__ __device__ void FlushSumBlockAdd(StorageT* const output, |
| 365 |
StorageT* const inout_shared, const bool valid) { |
| 366 |
__syncthreads(); |
| 367 |
const cg::coalesced_group group = cg::binary_partition(cg::coalesced_threads(), valid); |
| 368 |
constexpr uint dim_aligned = dim_target == 3 ? 4 : dim_target; |
| 369 |
|
| 370 |
#pragma unroll |
| 371 |
for (int i = 0; i < dim_target; i++) { |
| 372 |
StorageT tot; |
| 373 |
|
| 374 |
if (valid) { |
| 375 |
tot = cg::reduce(group, inout_shared[threadIdx.x * dim_target + i], cg::plus<StorageT>()); |
| 376 |
} |
| 377 |
__syncthreads(); |
| 378 |
inout_shared[threadIdx.x * dim_target + i] = 0.0f; |
| 379 |
__syncthreads(); |
| 380 |
|
| 381 |
if (valid && group.thread_rank() == 0) { |
| 382 |
// TODO(Emil Martes): use first warp to reduce instead of atomicAdd_block |
| 383 |
atomicAdd_block(&inout_shared[i], tot); |
| 384 |
} |
| 385 |
__syncthreads(); |
| 386 |
} |
| 387 |
for (int i = threadIdx.x; i < dim_target; i += blockDim.x) { |
| 388 |
output[blockIdx.x * dim_aligned + i] += inout_shared[i]; |
| 389 |
} |
| 390 |
} |
| 391 |
|
| 392 |
// READ SHARED |
| 393 |
|
| 394 |
/** |
| 395 |
* Function used to load data from global memory into shared memory. |
| 396 |
* |
| 397 |
* Read more on caspar.argtypes.accessors.Shared. |
| 398 |
* Should be followed by ReadSharedx. |
| 399 |
*/ |
| 400 |
template <uint dim_target, typename KernelT, typename StorageT> |
| 401 |
__forceinline__ __device__ void LoadShared(const StorageT* const input, const uint offset, |
| 402 |
const SharedIndex* const indices, |
| 403 |
KernelT* const inout_shared) { |
| 404 |
__syncthreads(); |
| 405 |
constexpr uint dim_aligned = dim_target == 3 ? 4 : dim_target; |
| 406 |
|
| 407 |
#pragma unroll |
| 408 |
for (int i = 0; i < dim_target; i++) { |
| 409 |
const uint val_idx = blockDim.x * i + threadIdx.x; |
| 410 |
const uint obj_idx = indices[val_idx / dim_target].unique; |
| 411 |
const uint elem = val_idx % dim_target; |
| 412 |
|
| 413 |
// 0xffffffff indicates this thread has nothing to read. |
| 414 |
if (obj_idx == 0xffffffff) { |
| 415 |
break; |
| 416 |
} |
| 417 |
inout_shared[val_idx] = input[offset + obj_idx * dim_aligned + elem]; |
| 418 |
} |
| 419 |
__syncthreads(); |
| 420 |
} |
| 421 |
|
| 422 |
/** |
| 423 |
* Function used to load data from a unique global element into shared memory. |
| 424 |
* |
| 425 |
* Read more on caspar.argtypes.accessors.Unique. |
| 426 |
* Should be followed by ReadSharedx. |
| 427 |
*/ |
| 428 |
template <uint dim_target, typename KernelT, typename StorageT> |
| 429 |
__forceinline__ __device__ void LoadUnique(const StorageT* const input, const uint offset, |
| 430 |
KernelT* const inout_shared) { |
| 431 |
__syncthreads(); |
| 432 |
if (threadIdx.x < dim_target) { |
| 433 |
inout_shared[threadIdx.x] = input[offset + threadIdx.x]; |
| 434 |
} |
| 435 |
__syncthreads(); |
| 436 |
} |
| 437 |
|
| 438 |
// READ SHARED |
| 439 |
template <typename KernelT> |
| 440 |
__forceinline__ __device__ void ReadShared1(const KernelT* const inout_shared, const uint target, |
| 441 |
KernelT& x) { |
| 442 |
x = inout_shared[target]; |
| 443 |
} |
| 444 |
|
| 445 |
template <typename KernelT> |
| 446 |
__forceinline__ __device__ void ReadShared2(const KernelT* const inout_shared, const uint target, |
| 447 |
KernelT& x, KernelT& y) { |
| 448 |
x = inout_shared[target * 2 + 0]; |
| 449 |
y = inout_shared[target * 2 + 1]; |
| 450 |
} |
| 451 |
|
| 452 |
template <typename KernelT> |
| 453 |
__forceinline__ __device__ void ReadShared3(const KernelT* const inout_shared, const uint target, |
| 454 |
KernelT& x, KernelT& y, KernelT& z) { |
| 455 |
x = inout_shared[target * 3 + 0]; |
| 456 |
y = inout_shared[target * 3 + 1]; |
| 457 |
z = inout_shared[target * 3 + 2]; |
| 458 |
} |
| 459 |
|
| 460 |
template <typename KernelT> |
| 461 |
__forceinline__ __device__ void ReadShared4(const KernelT* const inout_shared, const uint target, |
| 462 |
KernelT& x, KernelT& y, KernelT& z, KernelT& w) { |
| 463 |
x = inout_shared[target * 4 + 0]; |
| 464 |
y = inout_shared[target * 4 + 1]; |
| 465 |
z = inout_shared[target * 4 + 2]; |
| 466 |
w = inout_shared[target * 4 + 3]; |
| 467 |
} |
| 468 |
|
| 469 |
// READ OVERLAPPED |
| 470 |
template <uint block_size, typename KernelT, typename StorageT, typename VecT> |
| 471 |
|
| 472 |
__forceinline__ __device__ void ReadAndShuffle1(KernelT* const inout_shared, |
| 473 |
const StorageT* const input, const uint offset, |
| 474 |
const uint idx, bool is_last, bool should_read, |
| 475 |
KernelT& x0, KernelT& x1) { |
| 476 |
if (should_read) { |
| 477 |
x0 = input[offset + idx]; |
| 478 |
inout_shared[threadIdx.x] = x0; |
| 479 |
} |
| 480 |
__syncthreads(); |
| 481 |
if (should_read && !is_last) { |
| 482 |
x1 = inout_shared[threadIdx.x + 1]; |
| 483 |
} |
| 484 |
__syncthreads(); |
| 485 |
if (should_read && is_last) { |
| 486 |
x1 = input[offset + idx + 1]; |
| 487 |
} |
| 488 |
} |
| 489 |
template <uint block_size, typename KernelT, typename StorageT, typename VecT> |
| 490 |
|
| 491 |
__forceinline__ __device__ void ReadAndShuffle2(KernelT* const inout_shared, |
| 492 |
const StorageT* const input, const uint offset, |
| 493 |
const uint idx, bool is_last, bool should_read, |
| 494 |
KernelT& x0, KernelT& y0, KernelT& x1, |
| 495 |
KernelT& y1) { |
| 496 |
VecT tmp; |
| 497 |
if (should_read) { |
| 498 |
tmp = reinterpret_cast<const VecT*>(&input[offset])[idx]; |
| 499 |
x0 = tmp.x; |
| 500 |
y0 = tmp.y; |
| 501 |
reinterpret_cast<VecT*>(inout_shared)[threadIdx.x] = tmp; |
| 502 |
} |
| 503 |
__syncthreads(); |
| 504 |
if (should_read && !is_last) { |
| 505 |
tmp = reinterpret_cast<VecT*>(inout_shared)[threadIdx.x + 1]; |
| 506 |
} |
| 507 |
__syncthreads(); |
| 508 |
if (should_read && is_last) { |
| 509 |
tmp = reinterpret_cast<const VecT*>(&input[offset])[idx + 1]; |
| 510 |
} |
| 511 |
x1 = tmp.x; |
| 512 |
y1 = tmp.y; |
| 513 |
} |
| 514 |
|
| 515 |
template <uint block_size, typename KernelT, typename StorageT, typename VecT> |
| 516 |
|
| 517 |
__forceinline__ __device__ void ReadAndShuffle3(KernelT* const inout_shared, |
| 518 |
const StorageT* const input, const uint offset, |
| 519 |
const uint idx, bool is_last, bool should_read, |
| 520 |
KernelT& x0, KernelT& y0, KernelT& z0, KernelT& x1, |
| 521 |
KernelT& y1, KernelT& z1) { |
| 522 |
VecT tmp; |
| 523 |
if (should_read) { |
| 524 |
tmp = reinterpret_cast<const VecT*>(&input[offset])[idx]; |
| 525 |
x0 = tmp.x; |
| 526 |
y0 = tmp.y; |
| 527 |
z0 = tmp.z; |
| 528 |
reinterpret_cast<VecT*>(inout_shared)[threadIdx.x] = tmp; |
| 529 |
} |
| 530 |
__syncthreads(); |
| 531 |
if (should_read && !is_last) { |
| 532 |
tmp = reinterpret_cast<VecT*>(inout_shared)[threadIdx.x + 1]; |
| 533 |
} |
| 534 |
__syncthreads(); |
| 535 |
if (should_read && is_last) { |
| 536 |
tmp = reinterpret_cast<const VecT*>(&input[offset])[idx + 1]; |
| 537 |
} |
| 538 |
x1 = tmp.x; |
| 539 |
y1 = tmp.y; |
| 540 |
z1 = tmp.z; |
| 541 |
} |
| 542 |
|
| 543 |
template <uint block_size, typename KernelT, typename StorageT, typename VecT> |
| 544 |
|
| 545 |
__forceinline__ __device__ void ReadAndShuffle4(KernelT* const inout_shared, |
| 546 |
const StorageT* const input, const uint offset, |
| 547 |
const uint idx, bool is_last, bool should_read, |
| 548 |
KernelT& x0, KernelT& y0, KernelT& z0, KernelT& w0, |
| 549 |
KernelT& x1, KernelT& y1, KernelT& z1, |
| 550 |
KernelT& w1) { |
| 551 |
VecT tmp; |
| 552 |
if (should_read) { |
| 553 |
tmp = reinterpret_cast<const VecT*>(&input[offset])[idx]; |
| 554 |
x0 = tmp.x; |
| 555 |
y0 = tmp.y; |
| 556 |
z0 = tmp.z; |
| 557 |
w0 = tmp.w; |
| 558 |
reinterpret_cast<VecT*>(inout_shared)[threadIdx.x] = tmp; |
| 559 |
} |
| 560 |
__syncthreads(); |
| 561 |
if (should_read && !is_last) { |
| 562 |
tmp = reinterpret_cast<VecT*>(inout_shared)[threadIdx.x + 1]; |
| 563 |
} |
| 564 |
__syncthreads(); |
| 565 |
if (should_read && is_last) { |
| 566 |
tmp = reinterpret_cast<const VecT*>(&input[offset])[idx + 1]; |
| 567 |
} |
| 568 |
x1 = tmp.x; |
| 569 |
y1 = tmp.y; |
| 570 |
z1 = tmp.z; |
| 571 |
w1 = tmp.w; |
| 572 |
} |
| 573 |
// READ WITH DEFAULT |
| 574 |
template <uint block_size, typename KernelT, typename StorageT, typename VecT> |
| 575 |
|
| 576 |
__forceinline__ __device__ void ReadAndShuffleWithDefault1( |
| 577 |
KernelT* const inout_shared, const StorageT* const input, const uint offset, const int idx, |
| 578 |
bool is_last, bool should_read, const uint data_size, int step, KernelT& x0, KernelT& x1, |
| 579 |
KernelT x_default) { |
| 580 |
if (should_read && idx >= 0 && idx < data_size) { |
| 581 |
x0 = input[offset + idx]; |
| 582 |
inout_shared[threadIdx.x] = x0; |
| 583 |
} else { |
| 584 |
x0 = x_default; |
| 585 |
inout_shared[threadIdx.x] = x_default; |
| 586 |
} |
| 587 |
__syncthreads(); |
| 588 |
|
| 589 |
if (!is_last) { |
| 590 |
x1 = inout_shared[threadIdx.x + 1]; |
| 591 |
} else if (idx + step >= 0 && idx + step < data_size) { |
| 592 |
x1 = input[offset + idx + step]; |
| 593 |
} else { |
| 594 |
x1 = x_default; |
| 595 |
} |
| 596 |
__syncthreads(); |
| 597 |
} |
| 598 |
template <uint block_size, typename KernelT, typename StorageT, typename VecT> |
| 599 |
|
| 600 |
__forceinline__ __device__ void ReadAndShuffleWithDefault2( |
| 601 |
KernelT* const inout_shared, const StorageT* const input, const uint offset, const int idx, |
| 602 |
bool is_last, bool should_read, const uint data_size, int step, KernelT& x0, KernelT& y0, |
| 603 |
KernelT& x1, KernelT& y1, KernelT x_default, KernelT y_default) { |
| 604 |
VecT tmp; |
| 605 |
if (should_read && idx >= 0 && idx < data_size) { |
| 606 |
tmp = reinterpret_cast<const VecT*>(&input[offset])[idx]; |
| 607 |
} else { |
| 608 |
tmp.x = x_default; |
| 609 |
tmp.y = y_default; |
| 610 |
} |
| 611 |
x0 = tmp.x; |
| 612 |
y0 = tmp.y; |
| 613 |
reinterpret_cast<VecT*>(inout_shared)[threadIdx.x] = tmp; |
| 614 |
__syncthreads(); |
| 615 |
if (!is_last) { |
| 616 |
tmp = reinterpret_cast<VecT*>(inout_shared)[threadIdx.x + 1]; |
| 617 |
} else if (idx + step >= 0 && idx + step < data_size) { |
| 618 |
tmp = reinterpret_cast<const VecT*>(&input[offset])[idx + step]; |
| 619 |
} else { |
| 620 |
tmp.x = x_default; |
| 621 |
tmp.y = y_default; |
| 622 |
} |
| 623 |
x1 = tmp.x; |
| 624 |
y1 = tmp.y; |
| 625 |
__syncthreads(); |
| 626 |
} |
| 627 |
template <uint block_size, typename KernelT, typename StorageT, typename VecT> |
| 628 |
|
| 629 |
__forceinline__ __device__ void ReadAndShuffleWithDefault3( |
| 630 |
KernelT* const inout_shared, const StorageT* const input, const uint offset, const int idx, |
| 631 |
bool is_last, bool should_read, const uint data_size, int step, KernelT& x0, KernelT& y0, |
| 632 |
KernelT& z0, KernelT& x1, KernelT& y1, KernelT& z1, KernelT x_default, KernelT y_default, |
| 633 |
KernelT z_default) { |
| 634 |
VecT tmp; |
| 635 |
if (should_read && idx >= 0 && idx < data_size) { |
| 636 |
tmp = reinterpret_cast<const VecT*>(&input[offset])[idx]; |
| 637 |
} else { |
| 638 |
tmp.x = x_default; |
| 639 |
tmp.y = y_default; |
| 640 |
tmp.z = z_default; |
| 641 |
} |
| 642 |
x0 = tmp.x; |
| 643 |
y0 = tmp.y; |
| 644 |
z0 = tmp.z; |
| 645 |
reinterpret_cast<VecT*>(inout_shared)[threadIdx.x] = tmp; |
| 646 |
__syncthreads(); |
| 647 |
|
| 648 |
if (!is_last) { |
| 649 |
tmp = reinterpret_cast<VecT*>(inout_shared)[threadIdx.x + 1]; |
| 650 |
} else if (idx + step >= 0 && idx + step < data_size) { |
| 651 |
tmp = reinterpret_cast<const VecT*>(&input[offset])[idx + step]; |
| 652 |
} else { |
| 653 |
tmp.x = x_default; |
| 654 |
tmp.y = y_default; |
| 655 |
tmp.z = z_default; |
| 656 |
} |
| 657 |
x1 = tmp.x; |
| 658 |
y1 = tmp.y; |
| 659 |
z1 = tmp.z; |
| 660 |
__syncthreads(); |
| 661 |
} |
| 662 |
template <uint block_size, typename KernelT, typename StorageT, typename VecT> |
| 663 |
|
| 664 |
__forceinline__ __device__ void ReadAndShuffleWithDefault4( |
| 665 |
KernelT* const inout_shared, const StorageT* const input, const uint offset, const int idx, |
| 666 |
bool is_last, bool should_read, const uint data_size, int step, KernelT& x0, KernelT& y0, |
| 667 |
KernelT& z0, KernelT& w0, KernelT& x1, KernelT& y1, KernelT& z1, KernelT& w1, KernelT x_default, |
| 668 |
KernelT y_default, KernelT z_default, KernelT w_default) { |
| 669 |
VecT tmp; |
| 670 |
if (should_read && idx >= 0 && idx < data_size) { |
| 671 |
tmp = reinterpret_cast<const VecT*>(&input[offset])[idx]; |
| 672 |
} else { |
| 673 |
tmp.x = x_default; |
| 674 |
tmp.y = y_default; |
| 675 |
tmp.z = z_default; |
| 676 |
tmp.w = w_default; |
| 677 |
} |
| 678 |
x0 = tmp.x; |
| 679 |
y0 = tmp.y; |
| 680 |
z0 = tmp.z; |
| 681 |
w0 = tmp.w; |
| 682 |
reinterpret_cast<VecT*>(inout_shared)[threadIdx.x] = tmp; |
| 683 |
__syncthreads(); |
| 684 |
|
| 685 |
if (!is_last) { |
| 686 |
tmp = reinterpret_cast<VecT*>(inout_shared)[threadIdx.x + 1]; |
| 687 |
} else if (idx + step >= 0 && idx + step < data_size) { |
| 688 |
tmp = reinterpret_cast<const VecT*>(&input[offset])[idx + step]; |
| 689 |
} else { |
| 690 |
tmp.x = x_default; |
| 691 |
tmp.y = y_default; |
| 692 |
tmp.z = z_default; |
| 693 |
tmp.w = w_default; |
| 694 |
} |
| 695 |
x1 = tmp.x; |
| 696 |
y1 = tmp.y; |
| 697 |
z1 = tmp.z; |
| 698 |
w1 = tmp.w; |
| 699 |
__syncthreads(); |
| 700 |
} |
| 701 |
|
| 702 |
template <bool do_add, uint block_size, typename KernelT, typename StorageT, typename VecT> |
| 703 |
__forceinline__ __device__ void ShuffleAndWrite1(StorageT* const inout_shared, |
| 704 |
StorageT* const output, const uint offset, |
| 705 |
const uint gtidx, uint problem_size, |
| 706 |
const KernelT x0, const KernelT x1) { |
| 707 |
if (gtidx < problem_size) { |
| 708 |
inout_shared[threadIdx.x + 1] = x1; |
| 709 |
} |
| 710 |
if (threadIdx.x == 0) { |
| 711 |
inout_shared[0] = x0; |
| 712 |
} |
| 713 |
__syncthreads(); |
| 714 |
VecT from_prev; |
| 715 |
if (gtidx <= problem_size) { |
| 716 |
from_prev = inout_shared[threadIdx.x]; |
| 717 |
} |
| 718 |
if (threadIdx.x == 0) { |
| 719 |
from_prev = 0.0f; |
| 720 |
} |
| 721 |
|
| 722 |
const uint block_start = gtidx & (-1u << 10); |
| 723 |
{ |
| 724 |
bool help_intrablock_down = block_start != 0 && threadIdx.x < 1; |
| 725 |
bool help_intrablock_up = |
| 726 |
block_start + 1024 <= problem_size && threadIdx.x >= 1 && threadIdx.x < 2; |
| 727 |
|
| 728 |
StorageT data; |
| 729 |
if (help_intrablock_down) { |
| 730 |
data = inout_shared[threadIdx.x * 1025]; |
| 731 |
} |
| 732 |
if (help_intrablock_up) { |
| 733 |
data = inout_shared[(threadIdx.x) * 1025 + 1024 - 1025]; |
| 734 |
} |
| 735 |
__syncthreads(); |
| 736 |
if (help_intrablock_down || help_intrablock_up) { |
| 737 |
if (block_start != 0 && block_start + 1024 <= problem_size) { |
| 738 |
uint idx = threadIdx.x < 1 ? threadIdx.x : threadIdx.x - 1u + 1024u; |
| 739 |
atomicAdd(&output[offset + block_start + idx], data); |
| 740 |
} else if (block_start == 0) { |
| 741 |
atomicAdd(&output[offset + block_start + threadIdx.x - 1u + 1024u], data); |
| 742 |
} else { |
| 743 |
atomicAdd(&output[offset + block_start + threadIdx.x], data); |
| 744 |
} |
| 745 |
} |
| 746 |
} |
| 747 |
if ((threadIdx.x > 0 || block_start == 0) && gtidx <= problem_size) { |
| 748 |
VecT tmp = from_prev; |
| 749 |
if (gtidx < problem_size) { |
| 750 |
tmp += x0; |
| 751 |
} |
| 752 |
if (do_add) { |
| 753 |
tmp += output[offset + gtidx]; |
| 754 |
} |
| 755 |
output[offset + gtidx] = tmp; |
| 756 |
} |
| 757 |
} |
| 758 |
|
| 759 |
template <bool do_add, uint block_size, typename KernelT, typename StorageT, typename VecT> |
| 760 |
__forceinline__ __device__ void ShuffleAndWrite2(StorageT* const inout_shared, |
| 761 |
StorageT* const output, const uint offset, |
| 762 |
const uint gtidx, uint problem_size, |
| 763 |
const KernelT x0, const KernelT y0, |
| 764 |
const KernelT x1, const KernelT y1) { |
| 765 |
if (gtidx < problem_size) { |
| 766 |
inout_shared[threadIdx.x + 1] = x1; |
| 767 |
inout_shared[threadIdx.x + 1 + 1025] = y1; |
| 768 |
} |
| 769 |
if (threadIdx.x == 0) { |
| 770 |
inout_shared[0] = x0; |
| 771 |
inout_shared[1025] = y0; |
| 772 |
} |
| 773 |
__syncthreads(); |
| 774 |
VecT from_prev; |
| 775 |
if (gtidx <= problem_size) { |
| 776 |
from_prev.x = inout_shared[threadIdx.x]; |
| 777 |
from_prev.y = inout_shared[threadIdx.x + 1025]; |
| 778 |
} |
| 779 |
if (threadIdx.x == 0) { |
| 780 |
from_prev.x = 0.0f; |
| 781 |
from_prev.y = 0.0f; |
| 782 |
} |
| 783 |
|
| 784 |
const uint block_start = gtidx & (-1u << 10); |
| 785 |
{ |
| 786 |
bool help_intrablock_down = block_start != 0 && threadIdx.x < 2; |
| 787 |
bool help_intrablock_up = |
| 788 |
block_start + 1024 <= problem_size && threadIdx.x >= 2 && threadIdx.x < 4; |
| 789 |
|
| 790 |
StorageT data; |
| 791 |
if (help_intrablock_down) { |
| 792 |
data = inout_shared[threadIdx.x * 1025]; |
| 793 |
} |
| 794 |
if (help_intrablock_up) { |
| 795 |
data = inout_shared[(threadIdx.x) * 1025 + 1024 - 2 * 1025]; |
| 796 |
} |
| 797 |
__syncthreads(); |
| 798 |
if (help_intrablock_down || help_intrablock_up) { |
| 799 |
if (block_start != 0 && block_start + 1024 <= problem_size) { |
| 800 |
uint idx = threadIdx.x < 2 ? threadIdx.x : threadIdx.x - 2u + 2u * 1024u; |
| 801 |
atomicAdd(&output[offset + 2 * block_start + idx], data); |
| 802 |
} else if (block_start == 0) { |
| 803 |
atomicAdd(&output[offset + 2 * block_start + threadIdx.x - 2u + 2u * 1024u], data); |
| 804 |
} else { |
| 805 |
atomicAdd(&output[offset + 2 * block_start + threadIdx.x], data); |
| 806 |
} |
| 807 |
} |
| 808 |
} |
| 809 |
if ((threadIdx.x > 0 || block_start == 0) && gtidx <= problem_size) { |
| 810 |
VecT tmp2 = from_prev; |
| 811 |
if (gtidx < problem_size) { |
| 812 |
tmp2.x += x0; |
| 813 |
tmp2.y += y0; |
| 814 |
} |
| 815 |
if (do_add) { |
| 816 |
VecT current = reinterpret_cast<VecT*>(&output[offset])[gtidx]; |
| 817 |
tmp2.x += current.x; |
| 818 |
tmp2.y += current.y; |
| 819 |
} |
| 820 |
reinterpret_cast<VecT*>(&output[offset])[gtidx] = tmp2; |
| 821 |
} |
| 822 |
} |
| 823 |
|
| 824 |
template <bool do_add, uint block_size, typename KernelT, typename StorageT, typename VecT> |
| 825 |
__forceinline__ __device__ void ShuffleAndWrite3(StorageT* const inout_shared, |
| 826 |
StorageT* const output, const uint offset, |
| 827 |
const uint gtidx, uint problem_size, |
| 828 |
const KernelT x0, const KernelT y0, |
| 829 |
const KernelT z0, const KernelT x1, |
| 830 |
const KernelT y1, const KernelT z1) { |
| 831 |
if (gtidx < problem_size) { |
| 832 |
inout_shared[threadIdx.x + 1] = x1; |
| 833 |
inout_shared[threadIdx.x + 1 + 1025] = y1; |
| 834 |
inout_shared[threadIdx.x + 1 + 2 * 1025] = z1; |
| 835 |
} |
| 836 |
if (threadIdx.x == 0) { |
| 837 |
inout_shared[0] = x0; |
| 838 |
inout_shared[1025] = y0; |
| 839 |
inout_shared[2 * 1025] = z0; |
| 840 |
} |
| 841 |
__syncthreads(); |
| 842 |
VecT from_prev; |
| 843 |
if (gtidx <= problem_size) { |
| 844 |
from_prev.x = inout_shared[threadIdx.x]; |
| 845 |
from_prev.y = inout_shared[threadIdx.x + 1025]; |
| 846 |
from_prev.z = inout_shared[threadIdx.x + 2 * 1025]; |
| 847 |
} |
| 848 |
if (threadIdx.x == 0) { |
| 849 |
from_prev.x = 0.0f; |
| 850 |
from_prev.y = 0.0f; |
| 851 |
from_prev.z = 0.0f; |
| 852 |
} |
| 853 |
|
| 854 |
const uint block_start = gtidx & (-1u << 10); |
| 855 |
{ |
| 856 |
bool help_intrablock_down = block_start != 0 && threadIdx.x < 3; |
| 857 |
bool help_intrablock_up = |
| 858 |
block_start + 1024 <= problem_size && threadIdx.x >= 3 && threadIdx.x < 6; |
| 859 |
|
| 860 |
StorageT data; |
| 861 |
if (help_intrablock_down) { |
| 862 |
data = inout_shared[threadIdx.x * 1025]; |
| 863 |
} |
| 864 |
if (help_intrablock_up) { |
| 865 |
data = inout_shared[(threadIdx.x) * 1025 + 1024 - 3 * 1025]; |
| 866 |
} |
| 867 |
__syncthreads(); |
| 868 |
if (help_intrablock_down || help_intrablock_up) { |
| 869 |
if (block_start != 0 && block_start + 1024 <= problem_size) { |
| 870 |
uint idx = threadIdx.x < 3 ? threadIdx.x : threadIdx.x - 3u + 4u * 1024u; |
| 871 |
atomicAdd(&output[offset + 4 * block_start + idx], data); |
| 872 |
} else if (block_start == 0) { |
| 873 |
atomicAdd(&output[offset + 4 * block_start + threadIdx.x - 3u + 4u * 1024u], data); |
| 874 |
} else { |
| 875 |
atomicAdd(&output[offset + 4 * block_start + threadIdx.x], data); |
| 876 |
} |
| 877 |
} |
| 878 |
} |
| 879 |
if ((threadIdx.x > 0 || block_start == 0) && gtidx <= problem_size) { |
| 880 |
VecT tmp3 = from_prev; |
| 881 |
if (gtidx < problem_size) { |
| 882 |
tmp3.x += x0; |
| 883 |
tmp3.y += y0; |
| 884 |
tmp3.z += z0; |
| 885 |
} |
| 886 |
if (do_add) { |
| 887 |
VecT current = reinterpret_cast<VecT*>(&output[offset])[gtidx]; |
| 888 |
tmp3.x += current.x; |
| 889 |
tmp3.y += current.y; |
| 890 |
tmp3.z += current.z; |
| 891 |
} |
| 892 |
reinterpret_cast<VecT*>(&output[offset])[gtidx] = tmp3; |
| 893 |
} |
| 894 |
} |
| 895 |
|
| 896 |
template <bool do_add, uint block_size, typename KernelT, typename StorageT, typename VecT> |
| 897 |
__forceinline__ __device__ void ShuffleAndWrite4( |
| 898 |
StorageT* const inout_shared, StorageT* const output, const uint offset, const uint gtidx, |
| 899 |
uint problem_size, const KernelT x0, const KernelT y0, const KernelT z0, const KernelT w0, |
| 900 |
const KernelT x1, const KernelT y1, const KernelT z1, const KernelT w1) { |
| 901 |
if (gtidx < problem_size) { |
| 902 |
inout_shared[threadIdx.x + 1] = x1; |
| 903 |
inout_shared[threadIdx.x + 1 + 1025] = y1; |
| 904 |
inout_shared[threadIdx.x + 1 + 2 * 1025] = z1; |
| 905 |
inout_shared[threadIdx.x + 1 + 3 * 1025] = w1; |
| 906 |
} |
| 907 |
if (threadIdx.x == 0) { |
| 908 |
inout_shared[0] = x0; |
| 909 |
inout_shared[1025] = y0; |
| 910 |
inout_shared[2 * 1025] = z0; |
| 911 |
inout_shared[3 * 1025] = w0; |
| 912 |
} |
| 913 |
__syncthreads(); |
| 914 |
VecT from_prev; |
| 915 |
if (gtidx <= problem_size) { |
| 916 |
from_prev.x = inout_shared[threadIdx.x]; |
| 917 |
from_prev.y = inout_shared[threadIdx.x + 1025]; |
| 918 |
from_prev.z = inout_shared[threadIdx.x + 2 * 1025]; |
| 919 |
from_prev.w = inout_shared[threadIdx.x + 3 * 1025]; |
| 920 |
} |
| 921 |
if (threadIdx.x == 0) { |
| 922 |
from_prev.x = 0.0f; |
| 923 |
from_prev.y = 0.0f; |
| 924 |
from_prev.z = 0.0f; |
| 925 |
from_prev.w = 0.0f; |
| 926 |
} |
| 927 |
|
| 928 |
const uint block_start = gtidx & (-1u << 10); |
| 929 |
{ |
| 930 |
bool help_intrablock_down = block_start != 0 && threadIdx.x < 4; |
| 931 |
bool help_intrablock_up = |
| 932 |
block_start + 1024 <= problem_size && threadIdx.x >= 4 && threadIdx.x < 8; |
| 933 |
|
| 934 |
StorageT data; |
| 935 |
if (help_intrablock_down) { |
| 936 |
data = inout_shared[threadIdx.x * 1025]; |
| 937 |
} |
| 938 |
if (help_intrablock_up) { |
| 939 |
data = inout_shared[(threadIdx.x) * 1025 + 1024 - 4 * 1025]; |
| 940 |
} |
| 941 |
__syncthreads(); |
| 942 |
if (help_intrablock_down || help_intrablock_up) { |
| 943 |
if (block_start != 0 && block_start + 1024 <= problem_size) { |
| 944 |
uint idx = threadIdx.x < 4 ? threadIdx.x : threadIdx.x - 4u + 4u * 1024u; |
| 945 |
atomicAdd(&output[offset + 4 * block_start + idx], data); |
| 946 |
} else if (block_start == 0) { |
| 947 |
atomicAdd(&output[offset + 4 * block_start + threadIdx.x - 4u + 4u * 1024u], data); |
| 948 |
} else { |
| 949 |
atomicAdd(&output[offset + 4 * block_start + threadIdx.x], data); |
| 950 |
} |
| 951 |
} |
| 952 |
} |
| 953 |
if ((threadIdx.x > 0 || block_start == 0) && gtidx <= problem_size) { |
| 954 |
VecT tmp4 = from_prev; |
| 955 |
if (gtidx < problem_size) { |
| 956 |
tmp4.x += x0; |
| 957 |
tmp4.y += y0; |
| 958 |
tmp4.z += z0; |
| 959 |
tmp4.w += w0; |
| 960 |
} |
| 961 |
if (do_add) { |
| 962 |
VecT current = reinterpret_cast<VecT*>(&output[offset])[gtidx]; |
| 963 |
tmp4.x += current.x; |
| 964 |
tmp4.y += current.y; |
| 965 |
tmp4.z += current.z; |
| 966 |
tmp4.w += current.w; |
| 967 |
} |
| 968 |
reinterpret_cast<VecT*>(&output[offset])[gtidx] = tmp4; |
| 969 |
} |
| 970 |
} |
| 971 |
|
| 972 |
} // namespace caspar |