|
| 1 | + |
| 2 | +#include <iostream> |
| 3 | +#include <functional> |
| 4 | +#include <algorithm> |
| 5 | +#include <cfloat> |
| 6 | +#include <thrust/pair.h> |
| 7 | +#include <cuda.h> |
| 8 | +#include <cuda_fp16.h> |
| 9 | +#include <cuda_runtime.h> |
| 10 | +#include "NvInfer.h" |
| 11 | + |
| 12 | + |
| 13 | + |
| 14 | +#define BLOCKSIZE 512 |
| 15 | + |
| 16 | +#define ivpair thrust::pair<scalar_t, int> |
| 17 | + |
| 18 | + |
| 19 | +template<typename scalar_t> |
| 20 | +__forceinline__ __device__ void reduce_max(ivpair* sdata, int blocksize, int tid) { |
| 21 | + __syncthreads(); |
| 22 | + for (int s{blocksize / 2}; s > 0; s >>= 1) { |
| 23 | + if (tid < s) { |
| 24 | + if (sdata[tid].first < sdata[tid + s].first) { |
| 25 | + sdata[tid] = sdata[tid + s]; |
| 26 | + } |
| 27 | + } |
| 28 | + __syncthreads(); |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | + |
| 33 | +template<typename scalar_t> |
| 34 | +__global__ void arg_max_depth(const int n_size, |
| 35 | + const int dimsize, const int m_size, |
| 36 | + const scalar_t *inten, |
| 37 | + int *oten) { |
| 38 | + extern __shared__ __align__(sizeof(ivpair)) unsigned char sdata_raw[]; |
| 39 | + ivpair *sdata = reinterpret_cast<ivpair*>(sdata_raw); |
| 40 | + sdata = sdata + blockDim.x * threadIdx.y; |
| 41 | + |
| 42 | + int sample_offset = gridDim.x * blockDim.y; |
| 43 | + int bid = threadIdx.y + blockIdx.x * blockDim.y; |
| 44 | + int samplesize = n_size * m_size; |
| 45 | + |
| 46 | + for (int i{bid}; i < samplesize; i += sample_offset) { |
| 47 | + int n_idx = i / m_size; |
| 48 | + int m_idx = i % m_size; |
| 49 | + |
| 50 | + /// NOTE: This is not memory-safe when dimsize < blockDim.x |
| 51 | + int idx = n_idx * dimsize * m_size + threadIdx.x * m_size + m_idx; |
| 52 | + ivpair maxp = thrust::make_pair(inten[idx], threadIdx.x); |
| 53 | + int j = threadIdx.x + blockDim.x; |
| 54 | + for (; j < dimsize; j += blockDim.x) { |
| 55 | + idx += blockDim.x * m_size; |
| 56 | + scalar_t val = inten[idx]; |
| 57 | + if (val > maxp.first) { |
| 58 | + maxp = thrust::make_pair(val, j); |
| 59 | + } |
| 60 | + } |
| 61 | + sdata[threadIdx.x] = maxp; |
| 62 | + __syncthreads(); |
| 63 | + reduce_max(sdata, blockDim.x, threadIdx.x); |
| 64 | + |
| 65 | + idx = n_idx * m_size + m_idx; |
| 66 | + oten[idx] = sdata[0].second; |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | + |
| 71 | +template<typename scalar_t> |
| 72 | +__global__ void arg_max_spatial(const int n_size, |
| 73 | + const int dimsize, const int m_size, |
| 74 | + const scalar_t *inten, |
| 75 | + int *oten) { |
| 76 | + |
| 77 | + int sample_offset = gridDim.x * blockDim.x; |
| 78 | + int tid = threadIdx.x + blockIdx.x * blockDim.x; |
| 79 | + int samplesize = n_size * m_size; |
| 80 | + |
| 81 | + for (int i{tid}; i < samplesize; i += sample_offset) { |
| 82 | + int n_idx = i / m_size; |
| 83 | + int m_idx = i % m_size; |
| 84 | + |
| 85 | + // obtain max |
| 86 | + int idx = n_idx * dimsize * m_size + m_idx; |
| 87 | + scalar_t max_val = inten[idx]; |
| 88 | + int res = 0; |
| 89 | + for (int j{1}; j < dimsize; ++j) { |
| 90 | + idx += m_size; |
| 91 | + scalar_t val = inten[idx]; |
| 92 | + if (val > max_val) { |
| 93 | + max_val = val; |
| 94 | + res = j; |
| 95 | + } |
| 96 | + } |
| 97 | + idx = n_idx * m_size + m_idx; |
| 98 | + oten[idx] = res; |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | + |
| 103 | +void argMaxFunc(const void *inten, |
| 104 | + void *oten, const int n_size, |
| 105 | + const int dimsize, const int m_size, |
| 106 | + cudaStream_t* stream) { |
| 107 | + if (inten == nullptr or oten == nullptr) std::abort(); |
| 108 | + |
| 109 | + int samplesize = n_size * m_size; |
| 110 | + int shm_size = 0; |
| 111 | + dim3 grid, block; |
| 112 | + |
| 113 | + if (dimsize <= 256) { |
| 114 | + int blockx, gridx; |
| 115 | + cudaOccupancyMaxPotentialBlockSize(&gridx, &blockx, |
| 116 | + arg_max_spatial<float>, 0, samplesize); |
| 117 | + gridx = std::min(4096, gridx << 2); |
| 118 | + block.x = blockx; grid.x = gridx; |
| 119 | + |
| 120 | + if (stream == nullptr) { |
| 121 | + arg_max_spatial<float><<<grid, block, shm_size>>>( |
| 122 | + n_size, dimsize, m_size, |
| 123 | + reinterpret_cast<const float*>(inten), |
| 124 | + reinterpret_cast<int*>(oten)); |
| 125 | + } else { |
| 126 | + arg_max_spatial<float><<<grid, block, shm_size, *stream>>>( |
| 127 | + n_size, dimsize, m_size, |
| 128 | + reinterpret_cast<const float*>(inten), |
| 129 | + reinterpret_cast<int*>(oten)); |
| 130 | + } |
| 131 | + |
| 132 | + } else { |
| 133 | + int blockx, blocky, gridx; |
| 134 | + shm_size = (sizeof(float) + sizeof(int)) * BLOCKSIZE; |
| 135 | + int block_lmt = std::min(BLOCKSIZE, dimsize); |
| 136 | + blockx = 32; |
| 137 | + while (blockx <= block_lmt) blockx = (blockx << 1); |
| 138 | + blockx = (blockx >> 1); // must make sure dimsize > blockx |
| 139 | + blocky = BLOCKSIZE / blockx; |
| 140 | + gridx = std::min(4096, samplesize / blocky); |
| 141 | + block.x = blockx; block.y = blocky; grid.x = gridx; |
| 142 | + |
| 143 | + if (stream == nullptr) { |
| 144 | + arg_max_depth<float><<<grid, block, shm_size>>>( |
| 145 | + n_size, dimsize, m_size, |
| 146 | + reinterpret_cast<const float*>(inten), |
| 147 | + reinterpret_cast<int*>(oten)); |
| 148 | + } else { |
| 149 | + arg_max_depth<float><<<grid, block, shm_size, *stream>>>( |
| 150 | + n_size, dimsize, m_size, |
| 151 | + reinterpret_cast<const float*>(inten), |
| 152 | + reinterpret_cast<int*>(oten)); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + |
| 157 | +} |
| 158 | + |
0 commit comments