#include "kernel_pinhole_split_fixed_pose_fixed_principal_point_fixed_point_res_jac.h"
#include "memops.cuh"
#include <cooperative_groups.h>
#include <cooperative_groups/details/partitioning.h>
#include <cooperative_groups/memcpy_async.h>
#include <cooperative_groups/reduce.h>
#include <cuda_runtime.h>

namespace cg = cooperative_groups;

namespace caspar {

__global__ void __launch_bounds__(1024, 1)
    PinholeSplitFixedPoseFixedPrincipalPointFixedPointResJacKernel(
        double* sensor_from_rig,
        unsigned int sensor_from_rig_num_alloc,
        double* focal,
        unsigned int focal_num_alloc,
        SharedIndex* focal_indices,
        double* pixel,
        unsigned int pixel_num_alloc,
        double* pose,
        unsigned int pose_num_alloc,
        double* principal_point,
        unsigned int principal_point_num_alloc,
        double* point,
        unsigned int point_num_alloc,
        double* out_res,
        unsigned int out_res_num_alloc,
        double* const out_focal_njtr,
        unsigned int out_focal_njtr_num_alloc,
        double* const out_focal_precond_diag,
        unsigned int out_focal_precond_diag_num_alloc,
        double* const out_focal_precond_tril,
        unsigned int out_focal_precond_tril_num_alloc,
        size_t problem_size) {
  const int global_thread_idx = blockIdx.x * blockDim.x + threadIdx.x;
  __shared__ uint8_t inout_shared[16384];

  __shared__ SharedIndex focal_indices_loc[1024];
  focal_indices_loc[threadIdx.x] =
      (global_thread_idx < problem_size
           ? focal_indices[global_thread_idx]
           : SharedIndex{0xffffffff, 0xffff, 0xffff});

  double r0, r1, r2, r3, r4, r5, r6, r7, r8, r9, r10, r11, r12, r13, r14, r15,
      r16, r17, r18, r19, r20, r21, r22, r23, r24, r25, r26, r27, r28, r29, r30,
      r31, r32, r33, r34, r35, r36, r37, r38, r39, r40, r41, r42, r43;

  if (global_thread_idx < problem_size) {
    ReadIdx2<1024, double, double, double2>(principal_point,
                                            0 * principal_point_num_alloc,
                                            global_thread_idx,
                                            r0,
                                            r1);
    ReadIdx2<1024, double, double, double2>(
        pixel, 0 * pixel_num_alloc, global_thread_idx, r2, r3);
    r4 = -1.00000000000000000e+00;
    r2 = fma(r2, r4, r0);
  };
  LoadShared<2, double, double>(
      focal, 0 * focal_num_alloc, focal_indices_loc, (double*)inout_shared);
  if (global_thread_idx < problem_size) {
    ReadShared2<double>(
        (double*)inout_shared, focal_indices_loc[threadIdx.x].target, r0, r5);
  };
  __syncthreads();
  if (global_thread_idx < problem_size) {
    ReadIdx2<1024, double, double, double2>(sensor_from_rig,
                                            4 * sensor_from_rig_num_alloc,
                                            global_thread_idx,
                                            r6,
                                            r7);
    ReadIdx2<1024, double, double, double2>(
        point, 0 * point_num_alloc, global_thread_idx, r8, r9);
    r10 = -2.00000000000000000e+00;
    ReadIdx2<1024, double, double, double2>(sensor_from_rig,
                                            2 * sensor_from_rig_num_alloc,
                                            global_thread_idx,
                                            r11,
                                            r12);
    ReadIdx2<1024, double, double, double2>(
        pose, 2 * pose_num_alloc, global_thread_idx, r13, r14);
    ReadIdx2<1024, double, double, double2>(sensor_from_rig,
                                            0 * sensor_from_rig_num_alloc,
                                            global_thread_idx,
                                            r15,
                                            r16);
    ReadIdx2<1024, double, double, double2>(
        pose, 0 * pose_num_alloc, global_thread_idx, r17, r18);
    r19 = fma(r15, r18, r12 * r13);
    r20 = r16 * r17;
    r19 = fma(r4, r20, r19);
    r19 = fma(r11, r14, r19);
    r20 = r19 * r19;
    r20 = r10 * r20;
    r21 = 1.00000000000000000e+00;
    r22 = r15 * r13;
    r22 = fma(r4, r22, r12 * r18);
    r22 = fma(r16, r14, r22);
    r22 = fma(r11, r17, r22);
    r23 = r22 * r22;
    r23 = fma(r10, r23, r21);
    r24 = r20 + r23;
    r24 = fma(r8, r24, r6);
    r6 = 2.00000000000000000e+00;
    r25 = fma(r15, r14, r12 * r17);
    r26 = r11 * r18;
    r25 = fma(r4, r26, r25);
    r25 = fma(r16, r13, r25);
    r26 = r6 * r25;
    r27 = r22 * r26;
    r28 = fma(r16, r18, r15 * r17);
    r28 = fma(r11, r13, r28);
    r28 = fma(r4, r28, r12 * r14);
    r14 = r10 * r28;
    r29 = fma(r19, r14, r27);
    ReadIdx1<1024, double, double, double>(
        point, 2 * point_num_alloc, global_thread_idx, r30);
    r31 = r6 * r22;
    r32 = r19 * r26;
    r31 = fma(r28, r31, r32);
    ReadIdx1<1024, double, double, double>(
        pose, 6 * pose_num_alloc, global_thread_idx, r33);
    r34 = r15 * r11;
    r34 = r34 * r6;
    r35 = r16 * r12;
    r36 = fma(r6, r35, r34);
    ReadIdx2<1024, double, double, double2>(
        pose, 4 * pose_num_alloc, global_thread_idx, r37, r38);
    r39 = r11 * r12;
    r40 = r15 * r16;
    r40 = r40 * r6;
    r39 = fma(r10, r39, r40);
    r41 = r16 * r16;
    r41 = r41 * r10;
    r42 = r21 + r41;
    r43 = r11 * r11;
    r43 = r10 * r43;
    r42 = r42 + r43;
    r24 = fma(r9, r29, r24);
    r24 = fma(r30, r31, r24);
    r24 = fma(r33, r36, r24);
    r24 = fma(r38, r39, r24);
    r24 = fma(r37, r42, r24);
    r42 = 1.00000000000000008e-15;
    ReadIdx1<1024, double, double, double>(
        sensor_from_rig, 6 * sensor_from_rig_num_alloc, global_thread_idx, r39);
    r32 = fma(r22, r14, r32);
    r32 = fma(r8, r32, r39);
    r35 = fma(r10, r35, r34);
    r41 = r21 + r41;
    r34 = r15 * r15;
    r34 = r10 * r34;
    r41 = r41 + r34;
    r39 = r16 * r11;
    r39 = r39 * r6;
    r36 = r15 * r12;
    r36 = fma(r6, r36, r39);
    r31 = r6 * r19;
    r31 = r31 * r22;
    r26 = fma(r28, r26, r31);
    r29 = r25 * r25;
    r29 = r29 * r10;
    r23 = r29 + r23;
    r32 = fma(r37, r35, r32);
    r32 = fma(r33, r41, r32);
    r32 = fma(r38, r36, r32);
    r32 = fma(r9, r26, r32);
    r32 = fma(r30, r23, r32);
    r23 = copysign(1.0, r32);
    r23 = fma(r42, r23, r32);
    r42 = 1.0 / r23;
    r32 = r24 * r42;
    r2 = fma(r0, r32, r2);
    r3 = fma(r3, r4, r1);
    r1 = r6 * r19;
    r1 = fma(r28, r1, r27);
    r1 = fma(r8, r1, r7);
    r8 = r11 * r12;
    r8 = fma(r6, r8, r40);
    r43 = r21 + r43;
    r43 = r43 + r34;
    r34 = r15 * r12;
    r34 = fma(r10, r34, r39);
    r14 = fma(r25, r14, r31);
    r20 = r21 + r20;
    r20 = r20 + r29;
    r1 = fma(r37, r8, r1);
    r1 = fma(r38, r43, r1);
    r1 = fma(r33, r34, r1);
    r1 = fma(r30, r14, r1);
    r1 = fma(r9, r20, r1);
    r20 = r5 * r1;
    r3 = fma(r42, r20, r3);
    WriteIdx2<1024, double, double, double2>(
        out_res, 0 * out_res_num_alloc, global_thread_idx, r2, r3);
    r2 = r4 * r2;
    r2 = r2 * r32;
    r4 = r4 * r1;
    r4 = r4 * r3;
    r4 = r4 * r42;
    WriteSum2<double, double>((double*)inout_shared, r2, r4);
  };
  FlushSumShared<2, double>(out_focal_njtr,
                            0 * out_focal_njtr_num_alloc,
                            focal_indices_loc,
                            (double*)inout_shared);
  if (global_thread_idx < problem_size) {
    r24 = r24 * r24;
    r23 = r23 * r23;
    r23 = 1.0 / r23;
    r24 = r24 * r23;
    r4 = r1 * r1;
    r4 = r23 * r4;
    WriteSum2<double, double>((double*)inout_shared, r24, r4);
  };
  FlushSumShared<2, double>(out_focal_precond_diag,
                            0 * out_focal_precond_diag_num_alloc,
                            focal_indices_loc,
                            (double*)inout_shared);
}

void PinholeSplitFixedPoseFixedPrincipalPointFixedPointResJac(
    double* sensor_from_rig,
    unsigned int sensor_from_rig_num_alloc,
    double* focal,
    unsigned int focal_num_alloc,
    SharedIndex* focal_indices,
    double* pixel,
    unsigned int pixel_num_alloc,
    double* pose,
    unsigned int pose_num_alloc,
    double* principal_point,
    unsigned int principal_point_num_alloc,
    double* point,
    unsigned int point_num_alloc,
    double* out_res,
    unsigned int out_res_num_alloc,
    double* const out_focal_njtr,
    unsigned int out_focal_njtr_num_alloc,
    double* const out_focal_precond_diag,
    unsigned int out_focal_precond_diag_num_alloc,
    double* const out_focal_precond_tril,
    unsigned int out_focal_precond_tril_num_alloc,
    size_t problem_size) {
  if (problem_size == 0) {
    return;
  }

  const int n_blocks = (problem_size + 1024 - 1) / 1024;
  PinholeSplitFixedPoseFixedPrincipalPointFixedPointResJacKernel<<<n_blocks,
                                                                   1024>>>(
      sensor_from_rig,
      sensor_from_rig_num_alloc,
      focal,
      focal_num_alloc,
      focal_indices,
      pixel,
      pixel_num_alloc,
      pose,
      pose_num_alloc,
      principal_point,
      principal_point_num_alloc,
      point,
      point_num_alloc,
      out_res,
      out_res_num_alloc,
      out_focal_njtr,
      out_focal_njtr_num_alloc,
      out_focal_precond_diag,
      out_focal_precond_diag_num_alloc,
      out_focal_precond_tril,
      out_focal_precond_tril_num_alloc,
      problem_size);
}

}  // namespace caspar