| 1 |
#include "kernel_pinhole_fixed_pose_res_jac.h" |
| 2 |
#include "memops.cuh" |
| 3 |
#include <cooperative_groups.h> |
| 4 |
#include <cooperative_groups/details/partitioning.h> |
| 5 |
#include <cooperative_groups/memcpy_async.h> |
| 6 |
#include <cooperative_groups/reduce.h> |
| 7 |
#include <cuda_runtime.h> |
| 8 |
|
| 9 |
namespace cg = cooperative_groups; |
| 10 |
|
| 11 |
namespace caspar { |
| 12 |
|
| 13 |
__global__ void __launch_bounds__(1024, 1) |
| 14 |
PinholeFixedPoseResJacKernel(double* sensor_from_rig, |
| 15 |
unsigned int sensor_from_rig_num_alloc, |
| 16 |
double* calib, |
| 17 |
unsigned int calib_num_alloc, |
| 18 |
SharedIndex* calib_indices, |
| 19 |
double* point, |
| 20 |
unsigned int point_num_alloc, |
| 21 |
SharedIndex* point_indices, |
| 22 |
double* pixel, |
| 23 |
unsigned int pixel_num_alloc, |
| 24 |
double* pose, |
| 25 |
unsigned int pose_num_alloc, |
| 26 |
double* out_res, |
| 27 |
unsigned int out_res_num_alloc, |
| 28 |
double* out_calib_jac, |
| 29 |
unsigned int out_calib_jac_num_alloc, |
| 30 |
double* const out_calib_njtr, |
| 31 |
unsigned int out_calib_njtr_num_alloc, |
| 32 |
double* const out_calib_precond_diag, |
| 33 |
unsigned int out_calib_precond_diag_num_alloc, |
| 34 |
double* const out_calib_precond_tril, |
| 35 |
unsigned int out_calib_precond_tril_num_alloc, |
| 36 |
double* out_point_jac, |
| 37 |
unsigned int out_point_jac_num_alloc, |
| 38 |
double* const out_point_njtr, |
| 39 |
unsigned int out_point_njtr_num_alloc, |
| 40 |
double* const out_point_precond_diag, |
| 41 |
unsigned int out_point_precond_diag_num_alloc, |
| 42 |
double* const out_point_precond_tril, |
| 43 |
unsigned int out_point_precond_tril_num_alloc, |
| 44 |
size_t problem_size) { |
| 45 |
const int global_thread_idx = blockIdx.x * blockDim.x + threadIdx.x; |
| 46 |
__shared__ uint8_t inout_shared[16384]; |
| 47 |
|
| 48 |
__shared__ SharedIndex calib_indices_loc[1024]; |
| 49 |
calib_indices_loc[threadIdx.x] = |
| 50 |
(global_thread_idx < problem_size |
| 51 |
? calib_indices[global_thread_idx] |
| 52 |
: SharedIndex{0xffffffff, 0xffff, 0xffff}); |
| 53 |
__shared__ SharedIndex point_indices_loc[1024]; |
| 54 |
point_indices_loc[threadIdx.x] = |
| 55 |
(global_thread_idx < problem_size |
| 56 |
? point_indices[global_thread_idx] |
| 57 |
: SharedIndex{0xffffffff, 0xffff, 0xffff}); |
| 58 |
|
| 59 |
double r0, r1, r2, r3, r4, r5, r6, r7, r8, r9, r10, r11, r12, r13, r14, r15, |
| 60 |
r16, r17, r18, r19, r20, r21, r22, r23, r24, r25, r26, r27, r28, r29, r30, |
| 61 |
r31, r32, r33, r34, r35, r36, r37, r38, r39, r40, r41, r42, r43, r44, r45, |
| 62 |
r46, r47; |
| 63 |
LoadShared<2, double, double>( |
| 64 |
calib, 2 * calib_num_alloc, calib_indices_loc, (double*)inout_shared); |
| 65 |
if (global_thread_idx < problem_size) { |
| 66 |
ReadShared2<double>( |
| 67 |
(double*)inout_shared, calib_indices_loc[threadIdx.x].target, r0, r1); |
| 68 |
}; |
| 69 |
__syncthreads(); |
| 70 |
if (global_thread_idx < problem_size) { |
| 71 |
ReadIdx2<1024, double, double, double2>( |
| 72 |
pixel, 0 * pixel_num_alloc, global_thread_idx, r2, r3); |
| 73 |
r4 = -1.00000000000000000e+00; |
| 74 |
r2 = fma(r2, r4, r0); |
| 75 |
r0 = 1.00000000000000008e-15; |
| 76 |
ReadIdx1<1024, double, double, double>( |
| 77 |
sensor_from_rig, 6 * sensor_from_rig_num_alloc, global_thread_idx, r5); |
| 78 |
}; |
| 79 |
LoadShared<2, double, double>( |
| 80 |
point, 0 * point_num_alloc, point_indices_loc, (double*)inout_shared); |
| 81 |
if (global_thread_idx < problem_size) { |
| 82 |
ReadShared2<double>( |
| 83 |
(double*)inout_shared, point_indices_loc[threadIdx.x].target, r6, r7); |
| 84 |
}; |
| 85 |
__syncthreads(); |
| 86 |
if (global_thread_idx < problem_size) { |
| 87 |
ReadIdx2<1024, double, double, double2>(sensor_from_rig, |
| 88 |
2 * sensor_from_rig_num_alloc, |
| 89 |
global_thread_idx, |
| 90 |
r8, |
| 91 |
r9); |
| 92 |
ReadIdx2<1024, double, double, double2>( |
| 93 |
pose, 2 * pose_num_alloc, global_thread_idx, r10, r11); |
| 94 |
ReadIdx2<1024, double, double, double2>(sensor_from_rig, |
| 95 |
0 * sensor_from_rig_num_alloc, |
| 96 |
global_thread_idx, |
| 97 |
r12, |
| 98 |
r13); |
| 99 |
ReadIdx2<1024, double, double, double2>( |
| 100 |
pose, 0 * pose_num_alloc, global_thread_idx, r14, r15); |
| 101 |
r16 = fma(r12, r15, r9 * r10); |
| 102 |
r17 = r13 * r14; |
| 103 |
r16 = fma(r4, r17, r16); |
| 104 |
r16 = fma(r8, r11, r16); |
| 105 |
r17 = 2.00000000000000000e+00; |
| 106 |
r18 = fma(r12, r11, r9 * r14); |
| 107 |
r19 = r8 * r15; |
| 108 |
r18 = fma(r4, r19, r18); |
| 109 |
r18 = fma(r13, r10, r18); |
| 110 |
r19 = r17 * r18; |
| 111 |
r20 = r16 * r19; |
| 112 |
r21 = r12 * r10; |
| 113 |
r21 = fma(r4, r21, r9 * r15); |
| 114 |
r21 = fma(r13, r11, r21); |
| 115 |
r21 = fma(r8, r14, r21); |
| 116 |
r22 = -2.00000000000000000e+00; |
| 117 |
r23 = fma(r13, r15, r12 * r14); |
| 118 |
r23 = fma(r8, r10, r23); |
| 119 |
r23 = fma(r4, r23, r9 * r11); |
| 120 |
r11 = r22 * r23; |
| 121 |
r24 = fma(r21, r11, r20); |
| 122 |
r5 = fma(r6, r24, r5); |
| 123 |
ReadIdx2<1024, double, double, double2>( |
| 124 |
pose, 4 * pose_num_alloc, global_thread_idx, r25, r26); |
| 125 |
r27 = r12 * r8; |
| 126 |
r27 = r27 * r17; |
| 127 |
r28 = r13 * r9; |
| 128 |
r29 = fma(r22, r28, r27); |
| 129 |
ReadIdx1<1024, double, double, double>( |
| 130 |
pose, 6 * pose_num_alloc, global_thread_idx, r30); |
| 131 |
r31 = r12 * r12; |
| 132 |
r31 = r22 * r31; |
| 133 |
r32 = 1.00000000000000000e+00; |
| 134 |
r33 = r13 * r13; |
| 135 |
r33 = fma(r22, r33, r32); |
| 136 |
r34 = r31 + r33; |
| 137 |
r35 = r13 * r8; |
| 138 |
r35 = r35 * r17; |
| 139 |
r36 = r12 * r9; |
| 140 |
r36 = fma(r17, r36, r35); |
| 141 |
r37 = r17 * r16; |
| 142 |
r37 = r37 * r21; |
| 143 |
r38 = fma(r23, r19, r37); |
| 144 |
}; |
| 145 |
LoadShared<1, double, double>( |
| 146 |
point, 2 * point_num_alloc, point_indices_loc, (double*)inout_shared); |
| 147 |
if (global_thread_idx < problem_size) { |
| 148 |
ReadShared1<double>( |
| 149 |
(double*)inout_shared, point_indices_loc[threadIdx.x].target, r39); |
| 150 |
}; |
| 151 |
__syncthreads(); |
| 152 |
if (global_thread_idx < problem_size) { |
| 153 |
r40 = r21 * r21; |
| 154 |
r40 = r22 * r40; |
| 155 |
r41 = r32 + r40; |
| 156 |
r42 = r18 * r18; |
| 157 |
r42 = r42 * r22; |
| 158 |
r41 = r41 + r42; |
| 159 |
r5 = fma(r25, r29, r5); |
| 160 |
r5 = fma(r30, r34, r5); |
| 161 |
r5 = fma(r26, r36, r5); |
| 162 |
r5 = fma(r7, r38, r5); |
| 163 |
r5 = fma(r39, r41, r5); |
| 164 |
r36 = copysign(1.0, r5); |
| 165 |
r36 = fma(r0, r36, r5); |
| 166 |
r0 = 1.0 / r36; |
| 167 |
}; |
| 168 |
LoadShared<2, double, double>( |
| 169 |
calib, 0 * calib_num_alloc, calib_indices_loc, (double*)inout_shared); |
| 170 |
if (global_thread_idx < problem_size) { |
| 171 |
ReadShared2<double>( |
| 172 |
(double*)inout_shared, calib_indices_loc[threadIdx.x].target, r5, r34); |
| 173 |
}; |
| 174 |
__syncthreads(); |
| 175 |
if (global_thread_idx < problem_size) { |
| 176 |
ReadIdx2<1024, double, double, double2>(sensor_from_rig, |
| 177 |
4 * sensor_from_rig_num_alloc, |
| 178 |
global_thread_idx, |
| 179 |
r29, |
| 180 |
r43); |
| 181 |
r40 = r32 + r40; |
| 182 |
r44 = r16 * r16; |
| 183 |
r44 = r22 * r44; |
| 184 |
r40 = r40 + r44; |
| 185 |
r29 = fma(r6, r40, r29); |
| 186 |
r19 = r21 * r19; |
| 187 |
r45 = fma(r16, r11, r19); |
| 188 |
r46 = r17 * r21; |
| 189 |
r46 = fma(r23, r46, r20); |
| 190 |
r28 = fma(r17, r28, r27); |
| 191 |
r27 = r8 * r9; |
| 192 |
r20 = r12 * r13; |
| 193 |
r20 = r20 * r17; |
| 194 |
r27 = fma(r22, r27, r20); |
| 195 |
r47 = r8 * r8; |
| 196 |
r47 = r22 * r47; |
| 197 |
r33 = r47 + r33; |
| 198 |
r29 = fma(r7, r45, r29); |
| 199 |
r29 = fma(r39, r46, r29); |
| 200 |
r29 = fma(r30, r28, r29); |
| 201 |
r29 = fma(r26, r27, r29); |
| 202 |
r29 = fma(r25, r33, r29); |
| 203 |
r33 = r5 * r29; |
| 204 |
r2 = fma(r0, r33, r2); |
| 205 |
r3 = fma(r3, r4, r1); |
| 206 |
r1 = r17 * r16; |
| 207 |
r1 = fma(r23, r1, r19); |
| 208 |
r6 = fma(r6, r1, r43); |
| 209 |
r43 = r8 * r9; |
| 210 |
r43 = fma(r17, r43, r20); |
| 211 |
r47 = r32 + r47; |
| 212 |
r47 = r47 + r31; |
| 213 |
r31 = r12 * r9; |
| 214 |
r31 = fma(r22, r31, r35); |
| 215 |
r11 = fma(r18, r11, r37); |
| 216 |
r44 = r32 + r44; |
| 217 |
r44 = r44 + r42; |
| 218 |
r6 = fma(r25, r43, r6); |
| 219 |
r6 = fma(r26, r47, r6); |
| 220 |
r6 = fma(r30, r31, r6); |
| 221 |
r6 = fma(r39, r11, r6); |
| 222 |
r6 = fma(r7, r44, r6); |
| 223 |
r7 = r34 * r6; |
| 224 |
r3 = fma(r0, r7, r3); |
| 225 |
WriteIdx2<1024, double, double, double2>( |
| 226 |
out_res, 0 * out_res_num_alloc, global_thread_idx, r2, r3); |
| 227 |
r39 = r29 * r0; |
| 228 |
r31 = r6 * r0; |
| 229 |
WriteIdx2<1024, double, double, double2>(out_calib_jac, |
| 230 |
0 * out_calib_jac_num_alloc, |
| 231 |
global_thread_idx, |
| 232 |
r39, |
| 233 |
r31); |
| 234 |
r30 = r4 * r29; |
| 235 |
r30 = r30 * r2; |
| 236 |
r30 = r30 * r0; |
| 237 |
r47 = r4 * r6; |
| 238 |
r47 = r47 * r3; |
| 239 |
r47 = r47 * r0; |
| 240 |
WriteSum2<double, double>((double*)inout_shared, r30, r47); |
| 241 |
}; |
| 242 |
FlushSumShared<2, double>(out_calib_njtr, |
| 243 |
0 * out_calib_njtr_num_alloc, |
| 244 |
calib_indices_loc, |
| 245 |
(double*)inout_shared); |
| 246 |
if (global_thread_idx < problem_size) { |
| 247 |
r47 = r4 * r2; |
| 248 |
r30 = r4 * r3; |
| 249 |
WriteSum2<double, double>((double*)inout_shared, r47, r30); |
| 250 |
}; |
| 251 |
FlushSumShared<2, double>(out_calib_njtr, |
| 252 |
2 * out_calib_njtr_num_alloc, |
| 253 |
calib_indices_loc, |
| 254 |
(double*)inout_shared); |
| 255 |
if (global_thread_idx < problem_size) { |
| 256 |
r29 = r29 * r29; |
| 257 |
r36 = r36 * r36; |
| 258 |
r36 = 1.0 / r36; |
| 259 |
r29 = r29 * r36; |
| 260 |
r6 = r6 * r6; |
| 261 |
r6 = r6 * r36; |
| 262 |
WriteSum2<double, double>((double*)inout_shared, r29, r6); |
| 263 |
}; |
| 264 |
FlushSumShared<2, double>(out_calib_precond_diag, |
| 265 |
0 * out_calib_precond_diag_num_alloc, |
| 266 |
calib_indices_loc, |
| 267 |
(double*)inout_shared); |
| 268 |
if (global_thread_idx < problem_size) { |
| 269 |
WriteSum2<double, double>((double*)inout_shared, r32, r32); |
| 270 |
}; |
| 271 |
FlushSumShared<2, double>(out_calib_precond_diag, |
| 272 |
2 * out_calib_precond_diag_num_alloc, |
| 273 |
calib_indices_loc, |
| 274 |
(double*)inout_shared); |
| 275 |
if (global_thread_idx < problem_size) { |
| 276 |
r32 = 0.00000000000000000e+00; |
| 277 |
WriteSum2<double, double>((double*)inout_shared, r32, r39); |
| 278 |
}; |
| 279 |
FlushSumShared<2, double>(out_calib_precond_tril, |
| 280 |
0 * out_calib_precond_tril_num_alloc, |
| 281 |
calib_indices_loc, |
| 282 |
(double*)inout_shared); |
| 283 |
if (global_thread_idx < problem_size) { |
| 284 |
WriteSum2<double, double>((double*)inout_shared, r31, r32); |
| 285 |
}; |
| 286 |
FlushSumShared<2, double>(out_calib_precond_tril, |
| 287 |
4 * out_calib_precond_tril_num_alloc, |
| 288 |
calib_indices_loc, |
| 289 |
(double*)inout_shared); |
| 290 |
if (global_thread_idx < problem_size) { |
| 291 |
r32 = r5 * r40; |
| 292 |
r36 = r4 * r36; |
| 293 |
r33 = r36 * r33; |
| 294 |
r32 = fma(r24, r33, r0 * r32); |
| 295 |
r31 = r24 * r36; |
| 296 |
r39 = r34 * r1; |
| 297 |
r39 = fma(r0, r39, r7 * r31); |
| 298 |
WriteIdx2<1024, double, double, double2>(out_point_jac, |
| 299 |
0 * out_point_jac_num_alloc, |
| 300 |
global_thread_idx, |
| 301 |
r32, |
| 302 |
r39); |
| 303 |
r31 = r5 * r45; |
| 304 |
r31 = fma(r0, r31, r38 * r33); |
| 305 |
r6 = r34 * r44; |
| 306 |
r29 = r38 * r36; |
| 307 |
r29 = fma(r7, r29, r0 * r6); |
| 308 |
WriteIdx2<1024, double, double, double2>(out_point_jac, |
| 309 |
2 * out_point_jac_num_alloc, |
| 310 |
global_thread_idx, |
| 311 |
r31, |
| 312 |
r29); |
| 313 |
r6 = r5 * r46; |
| 314 |
r6 = fma(r0, r6, r41 * r33); |
| 315 |
r33 = r34 * r11; |
| 316 |
r30 = r41 * r36; |
| 317 |
r30 = fma(r7, r30, r0 * r33); |
| 318 |
WriteIdx2<1024, double, double, double2>( |
| 319 |
out_point_jac, 4 * out_point_jac_num_alloc, global_thread_idx, r6, r30); |
| 320 |
r33 = r4 * r2; |
| 321 |
r7 = r4 * r3; |
| 322 |
r7 = fma(r39, r7, r32 * r33); |
| 323 |
r33 = r4 * r2; |
| 324 |
r0 = r4 * r3; |
| 325 |
r0 = fma(r29, r0, r31 * r33); |
| 326 |
WriteSum2<double, double>((double*)inout_shared, r7, r0); |
| 327 |
}; |
| 328 |
FlushSumShared<2, double>(out_point_njtr, |
| 329 |
0 * out_point_njtr_num_alloc, |
| 330 |
point_indices_loc, |
| 331 |
(double*)inout_shared); |
| 332 |
if (global_thread_idx < problem_size) { |
| 333 |
r0 = r4 * r3; |
| 334 |
r7 = r4 * r2; |
| 335 |
r7 = fma(r6, r7, r30 * r0); |
| 336 |
WriteSum1<double, double>((double*)inout_shared, r7); |
| 337 |
}; |
| 338 |
FlushSumShared<1, double>(out_point_njtr, |
| 339 |
2 * out_point_njtr_num_alloc, |
| 340 |
point_indices_loc, |
| 341 |
(double*)inout_shared); |
| 342 |
if (global_thread_idx < problem_size) { |
| 343 |
r7 = fma(r39, r39, r32 * r32); |
| 344 |
r0 = fma(r29, r29, r31 * r31); |
| 345 |
WriteSum2<double, double>((double*)inout_shared, r7, r0); |
| 346 |
}; |
| 347 |
FlushSumShared<2, double>(out_point_precond_diag, |
| 348 |
0 * out_point_precond_diag_num_alloc, |
| 349 |
point_indices_loc, |
| 350 |
(double*)inout_shared); |
| 351 |
if (global_thread_idx < problem_size) { |
| 352 |
r0 = fma(r30, r30, r6 * r6); |
| 353 |
WriteSum1<double, double>((double*)inout_shared, r0); |
| 354 |
}; |
| 355 |
FlushSumShared<1, double>(out_point_precond_diag, |
| 356 |
2 * out_point_precond_diag_num_alloc, |
| 357 |
point_indices_loc, |
| 358 |
(double*)inout_shared); |
| 359 |
if (global_thread_idx < problem_size) { |
| 360 |
r0 = fma(r32, r31, r39 * r29); |
| 361 |
r39 = fma(r39, r30, r32 * r6); |
| 362 |
WriteSum2<double, double>((double*)inout_shared, r0, r39); |
| 363 |
}; |
| 364 |
FlushSumShared<2, double>(out_point_precond_tril, |
| 365 |
0 * out_point_precond_tril_num_alloc, |
| 366 |
point_indices_loc, |
| 367 |
(double*)inout_shared); |
| 368 |
if (global_thread_idx < problem_size) { |
| 369 |
r30 = fma(r29, r30, r31 * r6); |
| 370 |
WriteSum1<double, double>((double*)inout_shared, r30); |
| 371 |
}; |
| 372 |
FlushSumShared<1, double>(out_point_precond_tril, |
| 373 |
2 * out_point_precond_tril_num_alloc, |
| 374 |
point_indices_loc, |
| 375 |
(double*)inout_shared); |
| 376 |
} |
| 377 |
|
| 378 |
void PinholeFixedPoseResJac(double* sensor_from_rig, |
| 379 |
unsigned int sensor_from_rig_num_alloc, |
| 380 |
double* calib, |
| 381 |
unsigned int calib_num_alloc, |
| 382 |
SharedIndex* calib_indices, |
| 383 |
double* point, |
| 384 |
unsigned int point_num_alloc, |
| 385 |
SharedIndex* point_indices, |
| 386 |
double* pixel, |
| 387 |
unsigned int pixel_num_alloc, |
| 388 |
double* pose, |
| 389 |
unsigned int pose_num_alloc, |
| 390 |
double* out_res, |
| 391 |
unsigned int out_res_num_alloc, |
| 392 |
double* out_calib_jac, |
| 393 |
unsigned int out_calib_jac_num_alloc, |
| 394 |
double* const out_calib_njtr, |
| 395 |
unsigned int out_calib_njtr_num_alloc, |
| 396 |
double* const out_calib_precond_diag, |
| 397 |
unsigned int out_calib_precond_diag_num_alloc, |
| 398 |
double* const out_calib_precond_tril, |
| 399 |
unsigned int out_calib_precond_tril_num_alloc, |
| 400 |
double* out_point_jac, |
| 401 |
unsigned int out_point_jac_num_alloc, |
| 402 |
double* const out_point_njtr, |
| 403 |
unsigned int out_point_njtr_num_alloc, |
| 404 |
double* const out_point_precond_diag, |
| 405 |
unsigned int out_point_precond_diag_num_alloc, |
| 406 |
double* const out_point_precond_tril, |
| 407 |
unsigned int out_point_precond_tril_num_alloc, |
| 408 |
size_t problem_size) { |
| 409 |
if (problem_size == 0) { |
| 410 |
return; |
| 411 |
} |
| 412 |
|
| 413 |
const int n_blocks = (problem_size + 1024 - 1) / 1024; |
| 414 |
PinholeFixedPoseResJacKernel<<<n_blocks, 1024>>>( |
| 415 |
sensor_from_rig, |
| 416 |
sensor_from_rig_num_alloc, |
| 417 |
calib, |
| 418 |
calib_num_alloc, |
| 419 |
calib_indices, |
| 420 |
point, |
| 421 |
point_num_alloc, |
| 422 |
point_indices, |
| 423 |
pixel, |
| 424 |
pixel_num_alloc, |
| 425 |
pose, |
| 426 |
pose_num_alloc, |
| 427 |
out_res, |
| 428 |
out_res_num_alloc, |
| 429 |
out_calib_jac, |
| 430 |
out_calib_jac_num_alloc, |
| 431 |
out_calib_njtr, |
| 432 |
out_calib_njtr_num_alloc, |
| 433 |
out_calib_precond_diag, |
| 434 |
out_calib_precond_diag_num_alloc, |
| 435 |
out_calib_precond_tril, |
| 436 |
out_calib_precond_tril_num_alloc, |
| 437 |
out_point_jac, |
| 438 |
out_point_jac_num_alloc, |
| 439 |
out_point_njtr, |
| 440 |
out_point_njtr_num_alloc, |
| 441 |
out_point_precond_diag, |
| 442 |
out_point_precond_diag_num_alloc, |
| 443 |
out_point_precond_tril, |
| 444 |
out_point_precond_tril_num_alloc, |
| 445 |
problem_size); |
| 446 |
} |
| 447 |
|
| 448 |
} // namespace caspar |