39 #ifndef PCL_GPU_KINFU_CUDA_UTILS_HPP_ 40 #define PCL_GPU_KINFU_CUDA_UTILS_HPP_ 48 __device__ __host__ __forceinline__
void swap ( T& a, T& b )
57 __device__ __forceinline__
static float 59 __device__ __forceinline__
static float 62 __device__ __forceinline__
static float 63 min() {
return 1.175494351e-38f; };
64 __device__ __forceinline__
static float 65 max() {
return 3.402823466e+38f; };
70 __device__ __forceinline__
static short 71 max() {
return SHRT_MAX; };
74 __device__ __forceinline__
float 75 dot(
const float3& v1,
const float3& v2)
77 return v1.x * v2.x + v1.y*v2.y + v1.z*v2.z;
80 __device__ __forceinline__ float3&
83 vec.x += v; vec.y += v; vec.z += v;
return vec;
86 __device__ __forceinline__ float3
89 return make_float3(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z);
92 __device__ __forceinline__ float3&
95 vec.x *= v; vec.y *= v; vec.z *= v;
return vec;
98 __device__ __forceinline__ float3
101 return make_float3(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z);
104 __device__ __forceinline__ float3
107 return make_float3(v1.x * v, v1.y * v, v1.z * v);
110 __device__ __forceinline__
float 113 return sqrt(
dot(v, v));
116 __device__ __forceinline__ float3
119 return v * rsqrt(
dot(v, v));
122 __device__ __host__ __forceinline__ float3
123 cross(
const float3& v1,
const float3& v2)
125 return make_float3(v1.y * v2.z - v1.z * v2.y, v1.z * v2.x - v1.x * v2.z, v1.x * v2.y - v1.y * v2.x);
128 __device__ __forceinline__
void computeRoots2(
const float& b,
const float& c, float3& roots)
131 float d = b * b - 4.f * c;
137 roots.z = 0.5f * (b + sd);
138 roots.y = 0.5f * (b - sd);
141 __device__ __forceinline__
void 150 const float s_inv3 = 1.f/3.f;
151 const float s_sqrt3 = sqrtf(3.f);
154 float c2_over_3 = c2 * s_inv3;
155 float a_over_3 = (c1 - c2*c2_over_3)*s_inv3;
159 float half_b = 0.5f * (c0 + c2_over_3 * (2.f * c2_over_3 * c2_over_3 - c1));
161 float q = half_b * half_b + a_over_3 * a_over_3 * a_over_3;
166 float rho = sqrtf(-a_over_3);
167 float theta = atan2f (sqrtf (-q), half_b)*s_inv3;
168 float cos_theta = __cosf (theta);
169 float sin_theta = __sinf (theta);
170 roots.x = c2_over_3 + 2.f * rho * cos_theta;
171 roots.y = c2_over_3 - rho * (cos_theta + s_sqrt3 * sin_theta);
172 roots.z = c2_over_3 - rho * (cos_theta - s_sqrt3 * sin_theta);
175 if (roots.x >= roots.y)
176 swap(roots.x, roots.y);
178 if (roots.y >= roots.z)
180 swap(roots.y, roots.z);
182 if (roots.x >= roots.y)
183 swap (roots.x, roots.y);
197 __device__ __host__ __forceinline__ float3&
operator[](
int i) {
return data[i]; }
198 __device__ __host__ __forceinline__
const float3&
operator[](
int i)
const {
return data[i]; }
204 static __forceinline__ __device__ float3
215 if(!isMuchSmallerThan(src.x, src.z) || !isMuchSmallerThan(src.y, src.z))
217 float invnm = rsqrtf(src.x*src.x + src.y*src.y);
218 perp.x = -src.y * invnm;
219 perp.y = src.x * invnm;
228 float invnm = rsqrtf(src.z * src.z + src.y * src.y);
230 perp.y = -src.z * invnm;
231 perp.z = src.y * invnm;
237 __device__ __forceinline__
238 Eigen33(
volatile float* mat_pkg_arg) : mat_pkg(mat_pkg_arg) {}
239 __device__ __forceinline__
void 245 float max01 = fmaxf( fabsf(mat_pkg[0]), fabsf(mat_pkg[1]) );
246 float max23 = fmaxf( fabsf(mat_pkg[2]), fabsf(mat_pkg[3]) );
247 float max45 = fmaxf( fabsf(mat_pkg[4]), fabsf(mat_pkg[5]) );
248 float m0123 = fmaxf( max01, max23);
249 float scale = fmaxf( max45, m0123);
264 float c0 = m00() * m11() * m22()
265 + 2.f * m01() * m02() * m12()
266 - m00() * m12() * m12()
267 - m11() * m02() * m02()
268 - m22() * m01() * m01();
269 float c1 = m00() * m11() -
275 float c2 = m00() + m11() + m22();
281 evecs[0] = make_float3(1.f, 0.f, 0.f);
282 evecs[1] = make_float3(0.f, 1.f, 0.f);
283 evecs[2] = make_float3(0.f, 0.f, 1.f);
288 tmp[0] = row0(); tmp[1] = row1(); tmp[2] = row2();
289 tmp[0].x -= evals.z; tmp[1].y -= evals.z; tmp[2].z -= evals.z;
291 vec_tmp[0] =
cross(tmp[0], tmp[1]);
292 vec_tmp[1] =
cross(tmp[0], tmp[2]);
293 vec_tmp[2] =
cross(tmp[1], tmp[2]);
295 float len1 =
dot (vec_tmp[0], vec_tmp[0]);
296 float len2 =
dot (vec_tmp[1], vec_tmp[1]);
297 float len3 =
dot (vec_tmp[2], vec_tmp[2]);
299 if (len1 >= len2 && len1 >= len3)
301 evecs[2] = vec_tmp[0] * rsqrtf (len1);
303 else if (len2 >= len1 && len2 >= len3)
305 evecs[2] = vec_tmp[1] * rsqrtf (len2);
309 evecs[2] = vec_tmp[2] * rsqrtf (len3);
313 evecs[0] =
cross(evecs[1], evecs[2]);
318 tmp[0] = row0(); tmp[1] = row1(); tmp[2] = row2();
319 tmp[0].x -= evals.x; tmp[1].y -= evals.x; tmp[2].z -= evals.x;
321 vec_tmp[0] =
cross(tmp[0], tmp[1]);
322 vec_tmp[1] =
cross(tmp[0], tmp[2]);
323 vec_tmp[2] =
cross(tmp[1], tmp[2]);
325 float len1 =
dot(vec_tmp[0], vec_tmp[0]);
326 float len2 =
dot(vec_tmp[1], vec_tmp[1]);
327 float len3 =
dot(vec_tmp[2], vec_tmp[2]);
329 if (len1 >= len2 && len1 >= len3)
331 evecs[0] = vec_tmp[0] * rsqrtf(len1);
333 else if (len2 >= len1 && len2 >= len3)
335 evecs[0] = vec_tmp[1] * rsqrtf(len2);
339 evecs[0] = vec_tmp[2] * rsqrtf(len3);
343 evecs[2] =
cross(evecs[0], evecs[1]);
348 tmp[0] = row0(); tmp[1] = row1(); tmp[2] = row2();
349 tmp[0].x -= evals.z; tmp[1].y -= evals.z; tmp[2].z -= evals.z;
351 vec_tmp[0] =
cross(tmp[0], tmp[1]);
352 vec_tmp[1] =
cross(tmp[0], tmp[2]);
353 vec_tmp[2] =
cross(tmp[1], tmp[2]);
355 float len1 =
dot(vec_tmp[0], vec_tmp[0]);
356 float len2 =
dot(vec_tmp[1], vec_tmp[1]);
357 float len3 =
dot(vec_tmp[2], vec_tmp[2]);
361 unsigned int min_el = 2;
362 unsigned int max_el = 2;
363 if (len1 >= len2 && len1 >= len3)
366 evecs[2] = vec_tmp[0] * rsqrtf (len1);
368 else if (len2 >= len1 && len2 >= len3)
371 evecs[2] = vec_tmp[1] * rsqrtf (len2);
376 evecs[2] = vec_tmp[2] * rsqrtf (len3);
379 tmp[0] = row0(); tmp[1] = row1(); tmp[2] = row2();
380 tmp[0].x -= evals.y; tmp[1].y -= evals.y; tmp[2].z -= evals.y;
382 vec_tmp[0] =
cross(tmp[0], tmp[1]);
383 vec_tmp[1] =
cross(tmp[0], tmp[2]);
384 vec_tmp[2] =
cross(tmp[1], tmp[2]);
386 len1 =
dot(vec_tmp[0], vec_tmp[0]);
387 len2 =
dot(vec_tmp[1], vec_tmp[1]);
388 len3 =
dot(vec_tmp[2], vec_tmp[2]);
390 if (len1 >= len2 && len1 >= len3)
393 evecs[1] = vec_tmp[0] * rsqrtf (len1);
394 min_el = len1 <= mmax[min_el] ? 1 : min_el;
395 max_el = len1 > mmax[max_el] ? 1 : max_el;
397 else if (len2 >= len1 && len2 >= len3)
400 evecs[1] = vec_tmp[1] * rsqrtf (len2);
401 min_el = len2 <= mmax[min_el] ? 1 : min_el;
402 max_el = len2 > mmax[max_el] ? 1 : max_el;
407 evecs[1] = vec_tmp[2] * rsqrtf (len3);
408 min_el = len3 <= mmax[min_el] ? 1 : min_el;
409 max_el = len3 > mmax[max_el] ? 1 : max_el;
412 tmp[0] = row0(); tmp[1] = row1(); tmp[2] = row2();
413 tmp[0].x -= evals.x; tmp[1].y -= evals.x; tmp[2].z -= evals.x;
415 vec_tmp[0] =
cross(tmp[0], tmp[1]);
416 vec_tmp[1] =
cross(tmp[0], tmp[2]);
417 vec_tmp[2] =
cross(tmp[1], tmp[2]);
419 len1 =
dot (vec_tmp[0], vec_tmp[0]);
420 len2 =
dot (vec_tmp[1], vec_tmp[1]);
421 len3 =
dot (vec_tmp[2], vec_tmp[2]);
424 if (len1 >= len2 && len1 >= len3)
427 evecs[0] = vec_tmp[0] * rsqrtf (len1);
428 min_el = len3 <= mmax[min_el] ? 0 : min_el;
429 max_el = len3 > mmax[max_el] ? 0 : max_el;
431 else if (len2 >= len1 && len2 >= len3)
434 evecs[0] = vec_tmp[1] * rsqrtf (len2);
435 min_el = len3 <= mmax[min_el] ? 0 : min_el;
436 max_el = len3 > mmax[max_el] ? 0 : max_el;
441 evecs[0] = vec_tmp[2] * rsqrtf (len3);
442 min_el = len3 <= mmax[min_el] ? 0 : min_el;
443 max_el = len3 > mmax[max_el] ? 0 : max_el;
446 unsigned mid_el = 3 - min_el - max_el;
447 evecs[min_el] =
normalized(
cross( evecs[(min_el+1) % 3], evecs[(min_el+2) % 3] ) );
448 evecs[mid_el] =
normalized(
cross( evecs[(mid_el+1) % 3], evecs[(mid_el+2) % 3] ) );
454 volatile float* mat_pkg;
456 __device__ __forceinline__
float m00()
const {
return mat_pkg[0]; }
457 __device__ __forceinline__
float m01()
const {
return mat_pkg[1]; }
458 __device__ __forceinline__
float m02()
const {
return mat_pkg[2]; }
459 __device__ __forceinline__
float m10()
const {
return mat_pkg[1]; }
460 __device__ __forceinline__
float m11()
const {
return mat_pkg[3]; }
461 __device__ __forceinline__
float m12()
const {
return mat_pkg[4]; }
462 __device__ __forceinline__
float m20()
const {
return mat_pkg[2]; }
463 __device__ __forceinline__
float m21()
const {
return mat_pkg[4]; }
464 __device__ __forceinline__
float m22()
const {
return mat_pkg[5]; }
466 __device__ __forceinline__ float3 row0()
const {
return make_float3( m00(), m01(), m02() ); }
467 __device__ __forceinline__ float3 row1()
const {
return make_float3( m10(), m11(), m12() ); }
468 __device__ __forceinline__ float3 row2()
const {
return make_float3( m20(), m21(), m22() ); }
470 __device__ __forceinline__
static bool isMuchSmallerThan (
float x,
float y)
474 return x * x <= prec_sqr * y * y;
480 static __device__ __forceinline__
unsigned int stride()
482 return blockDim.x * blockDim.y * blockDim.z;
485 static __device__ __forceinline__
int 488 return threadIdx.z * blockDim.x * blockDim.y + threadIdx.y * blockDim.x + threadIdx.x;
491 template<
int CTA_SIZE,
typename T,
class BinOp>
492 static __device__ __forceinline__
void reduce(
volatile T* buffer, BinOp op)
497 if (CTA_SIZE >= 1024) {
if (tid < 512) buffer[tid] = val = op(val, buffer[tid + 512]); __syncthreads(); }
498 if (CTA_SIZE >= 512) {
if (tid < 256) buffer[tid] = val = op(val, buffer[tid + 256]); __syncthreads(); }
499 if (CTA_SIZE >= 256) {
if (tid < 128) buffer[tid] = val = op(val, buffer[tid + 128]); __syncthreads(); }
500 if (CTA_SIZE >= 128) {
if (tid < 64) buffer[tid] = val = op(val, buffer[tid + 64]); __syncthreads(); }
504 if (CTA_SIZE >= 64) { buffer[tid] = val = op(val, buffer[tid + 32]); }
505 if (CTA_SIZE >= 32) { buffer[tid] = val = op(val, buffer[tid + 16]); }
506 if (CTA_SIZE >= 16) { buffer[tid] = val = op(val, buffer[tid + 8]); }
507 if (CTA_SIZE >= 8) { buffer[tid] = val = op(val, buffer[tid + 4]); }
508 if (CTA_SIZE >= 4) { buffer[tid] = val = op(val, buffer[tid + 2]); }
509 if (CTA_SIZE >= 2) { buffer[tid] = val = op(val, buffer[tid + 1]); }
513 template<
int CTA_SIZE,
typename T,
class BinOp>
514 static __device__ __forceinline__ T
reduce(
volatile T* buffer, T init, BinOp op)
517 T val = buffer[tid] = init;
520 if (CTA_SIZE >= 1024) {
if (tid < 512) buffer[tid] = val = op(val, buffer[tid + 512]); __syncthreads(); }
521 if (CTA_SIZE >= 512) {
if (tid < 256) buffer[tid] = val = op(val, buffer[tid + 256]); __syncthreads(); }
522 if (CTA_SIZE >= 256) {
if (tid < 128) buffer[tid] = val = op(val, buffer[tid + 128]); __syncthreads(); }
523 if (CTA_SIZE >= 128) {
if (tid < 64) buffer[tid] = val = op(val, buffer[tid + 64]); __syncthreads(); }
527 if (CTA_SIZE >= 64) { buffer[tid] = val = op(val, buffer[tid + 32]); }
528 if (CTA_SIZE >= 32) { buffer[tid] = val = op(val, buffer[tid + 16]); }
529 if (CTA_SIZE >= 16) { buffer[tid] = val = op(val, buffer[tid + 8]); }
530 if (CTA_SIZE >= 8) { buffer[tid] = val = op(val, buffer[tid + 4]); }
531 if (CTA_SIZE >= 4) { buffer[tid] = val = op(val, buffer[tid + 2]); }
532 if (CTA_SIZE >= 2) { buffer[tid] = val = op(val, buffer[tid + 1]); }
549 static __device__ __forceinline__
unsigned int 553 asm(
"mov.u32 %0, %laneid;" :
"=r"(ret) );
557 static __device__ __forceinline__
unsigned int id()
559 int tid = threadIdx.z * blockDim.x * blockDim.y + threadIdx.y * blockDim.x + threadIdx.x;
563 static __device__ __forceinline__
566 #if (__CUDA_ARCH__ >= 200) 568 asm(
"mov.u32 %0, %lanemask_lt;" :
"=r"(ret) );
571 return 0xFFFFFFFF >> (32 -
laneId());
584 static __device__ __forceinline__
int 587 const unsigned int lane = tid & 31;
591 int partial = ptr[tid];
593 ptr[tid] = partial = partial + ptr[tid + 16];
594 ptr[tid] = partial = partial + ptr[tid + 8];
595 ptr[tid] = partial = partial + ptr[tid + 4];
596 ptr[tid] = partial = partial + ptr[tid + 2];
597 ptr[tid] = partial = partial + ptr[tid + 1];
599 return ptr[tid - lane];
602 static __forceinline__ __device__
int 603 Ballot(
int predicate,
volatile int* cta_buffer)
605 #if __CUDA_ARCH__ >= 200 607 return __ballot(predicate);
610 cta_buffer[tid] = predicate ? (1 << (tid & 31)) : 0;
615 static __forceinline__ __device__
bool 616 All(
int predicate,
volatile int* cta_buffer)
618 #if __CUDA_ARCH__ >= 200 620 return __all(predicate);
623 cta_buffer[tid] = predicate ? 1 : 0;
__device__ __host__ __forceinline__ void swap(T &a, T &b)
static __device__ __forceinline__ unsigned int stride()
static __device__ __forceinline__ void reduce(volatile T *buffer, BinOp op)
__device__ __forceinline__ float3 & operator+=(float3 &vec, const float &v)
static __device__ __forceinline__ T reduce(volatile T *buffer, T init, BinOp op)
__device__ __forceinline__ float3 operator-(const float3 &v1, const float3 &v2)
__device__ static __forceinline__ float quiet_NaN()
__device__ __forceinline__ float3 normalized(const float3 &v)
__device__ __host__ __forceinline__ const float3 & operator[](int i) const
__device__ __forceinline__ float3 operator*(const Mat33 &m, const float3 &vec)
__device__ __host__ __forceinline__ float3 cross(const float3 &v1, const float3 &v2)
static __device__ __forceinline__ int binaryExclScan(int ballot_mask)
__device__ static __forceinline__ float max()
static __device__ __forceinline__ unsigned int id()
__device__ __forceinline__ void compute(Mat33 &tmp, Mat33 &vec_tmp, Mat33 &evecs, float3 &evals)
static __device__ __forceinline__ int laneMaskLt()
__device__ static __forceinline__ float min()
__device__ __forceinline__ float dot(const float3 &v1, const float3 &v2)
__device__ static __forceinline__ type epsilon()
static __forceinline__ __device__ bool All(int predicate, volatile int *cta_buffer)
static __forceinline__ __device__ int Ballot(int predicate, volatile int *cta_buffer)
static __forceinline__ __device__ float3 unitOrthogonal(const float3 &src)
__device__ __forceinline__ float3 & operator*=(float3 &vec, const float &v)
__device__ __forceinline__ void computeRoots2(const float &b, const float &c, float3 &roots)
__device__ __forceinline__ float3 operator+(const float3 &v1, const float3 &v2)
static __device__ __forceinline__ int flattenedThreadId()
static __device__ __forceinline__ unsigned int laneId()
Returns the warp lane ID of the calling thread.
__device__ __host__ __forceinline__ float norm(const float3 &v1, const float3 &v2)
__device__ static __forceinline__ float epsilon()
__device__ static __forceinline__ short max()
__device__ __forceinline__ Eigen33(volatile float *mat_pkg_arg)
__device__ __host__ __forceinline__ float3 & operator[](int i)
__device__ __forceinline__ void computeRoots3(float c0, float c1, float c2, float3 &roots)
static __device__ __forceinline__ int warp_reduce(volatile int *ptr, const unsigned int tid)