#include <stdlib.h>
#include <sys/time.h>
#include <stdio.h>
#include <cuda.h>
#include <math.h>

//#define N 1000000
#define SQRT_TWO_PI 2.506628274631000
#define BLOCK_D1 1024
#define BLOCK_D2 1
#define BLOCK_D3 1

// Note: Needs compute capability >= 2.0 for calculation with doubles, so compile with:
// nvcc kernelExample.cu -arch=compute_20 -code=sm_20,compute_20 -o kernelExample
// -use_fast_math doesn't seem to have any effect on speed

// CUDA kernel:
__global__ void calc_calib(short* raws, short* darks, int N) {
  // note that this assumes no third dimension to the grid
  // id of the block
  int myblock = blockIdx.x + blockIdx.y * gridDim.x;
  // size of each block (within grid of blocks)
  int blocksize = blockDim.x * blockDim.y * blockDim.z;
  // id of thread in a given block
  int subthread = threadIdx.z*(blockDim.x * blockDim.y) + threadIdx.y*blockDim.x + threadIdx.x;
  // assign overall id/index of the thread
  int idx = myblock * blocksize + subthread;

  if(idx < N) {
    raws[idx] -= darks[idx];
  }
}

// CPU analog for speed comparison
int calc_calib_cpu(short* raws, short* darks, int N) {
  for(int idx = 0; idx < N; idx++){
    raws[idx] -= darks[idx];
  }
  return 0;
}

/* ---------------------- host code -----------------------------*/
void fill( short *p, int n, int val ) {
  for(int i = 0; i < n; i++){
    p[i] = val;
  } 
}

double read_timer() {
  struct timeval end;
  gettimeofday( &end, NULL);
  return end.tv_sec+1.e-6*end.tv_usec;
}

int main (int argc, char *argv[]) {
  short* cpu_raws;
  short* gpu_raws;
  short* cpu_darks;
  short* gpu_darks;
  int N;
  cudaError_t cudaStat;

  printf("==========================================\n");
  for( N = 2296960; N <= 2296960; N+=2296960 ) {
    cpu_raws = (short*) malloc( sizeof(short)*N );
    cudaStat = cudaMalloc(&gpu_raws, sizeof(short)*N);
    if(cudaStat != cudaSuccess) {
      printf ("device memory allocation failed on gpu_raws");
      return EXIT_FAILURE;
    }
    cpu_darks = (short*) malloc( sizeof(short)*N );
    cudaStat = cudaMalloc(&gpu_darks, sizeof(short)*N);
    if(cudaStat != cudaSuccess) {
      printf ("device memory allocation failed on gpu_darks");
      return EXIT_FAILURE;
    }

    // fixed block dimensions (1024x1x1 threads)
    const dim3 blockSize(BLOCK_D1, BLOCK_D2, BLOCK_D3);

    // determine number of blocks we need for a given problem size
    int tmp = ceil(pow(N/(BLOCK_D1 * BLOCK_D2 * BLOCK_D3), 0.5));
    printf("Grid dimension is %i x %i\n", tmp, tmp);
    dim3 gridSize(tmp, tmp, 1);

    int nthreads = BLOCK_D1*BLOCK_D2*BLOCK_D3*tmp*tmp;
    if (nthreads < N){
      printf("\n================ NOT ENOUGH THREADS TO COVER N=%d =======================\n\n", N);
    } else {
      printf("Launching %d threads (N=%d)\n", nthreads, N);
    }

    // simulate 'data'
    fill(cpu_raws, N, 3);
    fill(cpu_darks, N, 1);
    printf("Input values (raw): %d %d %d...\n", cpu_raws[0], cpu_raws[1], cpu_raws[2]);
    printf("Input values (dark): %d %d %d...\n", cpu_darks[0], cpu_darks[1], cpu_darks[2]);
    cudaDeviceSynchronize();
    double tInit = read_timer();

    // copy input data to the GPU
    cudaStat = cudaMemcpy(gpu_raws, cpu_raws, N*sizeof(short), cudaMemcpyHostToDevice);
    printf("Memory Copy from Host to Device (raw)");
    if (cudaStat){
      printf("failed.\n");
    } else {
      printf("successful.\n");
    }
    cudaStat = cudaMemcpy(gpu_darks, cpu_darks, N*sizeof(short), cudaMemcpyHostToDevice);
    printf("Memory Copy from Host to Device (dark)");
    if (cudaStat){
      printf("failed.\n");
    } else {
      printf("successful.\n");
    }
    cudaDeviceSynchronize();
    double tTransferToGPU = read_timer();

    // do the calculation
    calc_calib<<<gridSize, blockSize>>>(gpu_raws, gpu_darks, N);

    cudaDeviceSynchronize();
    double tCalc = read_timer();

    cudaStat = cudaMemcpy(cpu_raws, gpu_raws, N*sizeof(short), cudaMemcpyDeviceToHost);
    printf("Memory Copy from Device to Host (raw) ");
    if (cudaStat){
      printf("failed.\n");
    } else {
      printf("successful.\n");
    }
    cudaDeviceSynchronize();
    double tTransferFromGPU = read_timer();

    printf("Output values: %d %d %d...%d %d %d\n", cpu_raws[0], cpu_raws[1], cpu_raws[2], cpu_raws[N-3], cpu_raws[N-2], cpu_raws[N-1]);

    // do calculation on CPU for comparison (unfair as this will only use one core)
    fill(cpu_raws, N, 3);
    fill(cpu_darks, N, 1);
    double tInit2 = read_timer();
    calc_calib_cpu(cpu_raws, cpu_darks, N);
    double tCalcCPU = read_timer();

    printf("Output values (CPU): %d %d %d...\n", cpu_raws[0], cpu_raws[1], cpu_raws[2]);

    printf("Timing results for n = %d\n", N);
    printf("Transfer to GPU time: %f\n", tTransferToGPU - tInit);
    printf("Calculation time (GPU): %f\n", tCalc - tTransferToGPU);
    printf("Calculation time (CPU): %f\n", tCalcCPU - tInit2);
    printf("Transfer from GPU time: %f\n", tTransferFromGPU - tCalc);

    printf("Freeing memory...\n");
    printf("==============================================\n");
    free(cpu_raws);
    free(cpu_darks);
    cudaFree(gpu_raws);
    cudaFree(gpu_darks);

  }
  printf("\n\nFinished.\n\n");
  return 0;

}






	.file	"tmpxft_003564dd_00000000-6_kernelExample.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2033:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2033:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z14calc_calib_cpuPsS_i
	.type	_Z14calc_calib_cpuPsS_i, @function
_Z14calc_calib_cpuPsS_i:
.LFB2027:
	.cfi_startproc
	endbr64
	xorl	%eax, %eax
.L3:
	cmpl	%eax, %edx
	jle	.L6
	movw	(%rsi,%rax,2), %cx
	subw	%cx, (%rdi,%rax,2)
	incq	%rax
	jmp	.L3
.L6:
	xorl	%eax, %eax
	ret
	.cfi_endproc
.LFE2027:
	.size	_Z14calc_calib_cpuPsS_i, .-_Z14calc_calib_cpuPsS_i
	.globl	_Z4fillPsii
	.type	_Z4fillPsii, @function
_Z4fillPsii:
.LFB2028:
	.cfi_startproc
	endbr64
	xorl	%eax, %eax
.L8:
	cmpl	%eax, %esi
	jle	.L10
	movw	%dx, (%rdi,%rax,2)
	incq	%rax
	jmp	.L8
.L10:
	ret
	.cfi_endproc
.LFE2028:
	.size	_Z4fillPsii, .-_Z4fillPsii
	.globl	_Z10read_timerv
	.type	_Z10read_timerv, @function
_Z10read_timerv:
.LFB2029:
	.cfi_startproc
	endbr64
	subq	$40, %rsp
	.cfi_def_cfa_offset 48
	xorl	%esi, %esi
	movq	%fs:40, %rax
	movq	%rax, 24(%rsp)
	xorl	%eax, %eax
	leaq	8(%rsp), %rdi
	call	gettimeofday@PLT
	cvtsi2sdq	16(%rsp), %xmm0
	mulsd	.LC0(%rip), %xmm0
	cvtsi2sdq	8(%rsp), %xmm1
	addsd	%xmm1, %xmm0
	movq	24(%rsp), %rax
	subq	%fs:40, %rax
	je	.L12
	call	__stack_chk_fail@PLT
.L12:
	addq	$40, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2029:
	.size	_Z10read_timerv, .-_Z10read_timerv
	.globl	_Z33__device_stub__Z10calc_calibPsS_iPsS_i
	.type	_Z33__device_stub__Z10calc_calibPsS_iPsS_i, @function
_Z33__device_stub__Z10calc_calibPsS_iPsS_i:
.LFB2055:
	.cfi_startproc
	endbr64
	subq	$136, %rsp
	.cfi_def_cfa_offset 144
	movq	%rdi, 24(%rsp)
	leaq	40(%rsp), %rcx
	leaq	48(%rsp), %rdi
	movq	%rsi, 16(%rsp)
	leaq	60(%rsp), %rsi
	movl	%edx, 12(%rsp)
	leaq	32(%rsp), %rdx
	movq	%fs:40, %rax
	movq	%rax, 120(%rsp)
	xorl	%eax, %eax
	leaq	24(%rsp), %rax
	movl	$1, 56(%rsp)
	movq	%rax, 96(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 104(%rsp)
	leaq	12(%rsp), %rax
	movq	%rax, 112(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 48(%rsp)
	movq	%rax, 60(%rsp)
	movl	$1, 68(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L14
	pushq	40(%rsp)
	.cfi_def_cfa_offset 152
	leaq	_Z10calc_calibPsS_i(%rip), %rdi
	pushq	40(%rsp)
	.cfi_def_cfa_offset 160
	movq	76(%rsp), %rcx
	movl	84(%rsp), %r8d
	movq	64(%rsp), %rsi
	movl	72(%rsp), %edx
	leaq	112(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 152
	popq	%rdx
	.cfi_def_cfa_offset 144
.L14:
	movq	120(%rsp), %rax
	subq	%fs:40, %rax
	je	.L16
	call	__stack_chk_fail@PLT
.L16:
	addq	$136, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2055:
	.size	_Z33__device_stub__Z10calc_calibPsS_iPsS_i, .-_Z33__device_stub__Z10calc_calibPsS_iPsS_i
	.globl	_Z10calc_calibPsS_i
	.type	_Z10calc_calibPsS_i, @function
_Z10calc_calibPsS_i:
.LFB2056:
	.cfi_startproc
	endbr64
	jmp	_Z33__device_stub__Z10calc_calibPsS_iPsS_i
	.cfi_endproc
.LFE2056:
	.size	_Z10calc_calibPsS_i, .-_Z10calc_calibPsS_i
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC1:
	.string	"==========================================\n"
.LC2:
	.string	"device memory allocation failed on gpu_raws"
.LC3:
	.string	"device memory allocation failed on gpu_darks"
.LC4:
	.string	"Grid dimension is %i x %i\n"
.LC5:
	.string	"Launching %d threads (N=%d)\n"
.LC6:
	.string	"Input values (raw): %d %d %d...\n"
.LC7:
	.string	"Input values (dark): %d %d %d...\n"
.LC8:
	.string	"Memory Copy from Host to Device (raw)"
.LC9:
	.string	"failed.\n"
.LC10:
	.string	"successful.\n"
.LC11:
	.string	"Memory Copy from Host to Device (dark)"
.LC12:
	.string	"Memory Copy from Device to Host (raw) "
.LC13:
	.string	"Output values: %d %d %d...%d %d %d\n"
.LC14:
	.string	"Output values (CPU): %d %d %d...\n"
.LC15:
	.string	"Timing results for n = %d\n"
.LC16:
	.string	"Transfer to GPU time: %f\n"
.LC17:
	.string	"Calculation time (GPU): %f\n"
.LC18:
	.string	"Calculation time (CPU): %f\n"
.LC19:
	.string	"Transfer from GPU time: %f\n"
.LC20:
	.string	"Freeing memory...\n"
.LC21:
	.string	"==============================================\n"
.LC22:
	.string	"\n\nFinished.\n\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2030:
	.cfi_startproc
	endbr64
	pushq	%r12
	.cfi_def_cfa_offset 16
	.cfi_offset 12, -16
	leaq	.LC1(%rip), %rsi
	movl	$2, %edi
	pushq	%rbp
	.cfi_def_cfa_offset 24
	.cfi_offset 6, -24
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset 3, -32
	subq	$96, %rsp
	.cfi_def_cfa_offset 128
	movq	%fs:40, %rax
	movq	%rax, 88(%rsp)
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	$4593920, %edi
	call	malloc@PLT
	movl	$4593920, %esi
	leaq	48(%rsp), %rdi
	movq	%rax, %rbx
	call	cudaMalloc@PLT
	leaq	.LC2(%rip), %rsi
	testl	%eax, %eax
	jne	.L36
	movl	$4593920, %edi
	call	malloc@PLT
	leaq	56(%rsp), %rdi
	movl	$4593920, %esi
	movq	%rax, %rbp
	call	cudaMalloc@PLT
	testl	%eax, %eax
	je	.L22
	leaq	.LC3(%rip), %rsi
.L36:
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	$1, %eax
	jmp	.L19
.L22:
	movl	$4194305, %eax
	movl	$48, %ecx
	movl	$48, %edx
	movl	$2, %edi
	salq	$10, %rax
	leaq	.LC4(%rip), %rsi
	movl	$1, 72(%rsp)
	movq	%rax, 64(%rsp)
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	$2296960, %ecx
	movl	$2359296, %edx
	movabsq	$206158430256, %rax
	movq	%rax, 76(%rsp)
	movl	$2, %edi
	xorl	%eax, %eax
	leaq	.LC5(%rip), %rsi
	movl	$1, 84(%rsp)
	call	__printf_chk@PLT
	movl	$2296960, %esi
	movl	$3, %edx
	movq	%rbx, %rdi
	call	_Z4fillPsii
	movl	$1, %edx
	movq	%rbp, %rdi
	call	_Z4fillPsii
	movswl	2(%rbx), %ecx
	movswl	(%rbx), %edx
	xorl	%eax, %eax
	movswl	4(%rbx), %r8d
	leaq	.LC6(%rip), %rsi
	movl	$2, %edi
	call	__printf_chk@PLT
	movswl	4(%rbp), %r8d
	movswl	2(%rbp), %ecx
	xorl	%eax, %eax
	movswl	0(%rbp), %edx
	leaq	.LC7(%rip), %rsi
	movl	$2, %edi
	call	__printf_chk@PLT
	call	cudaDeviceSynchronize@PLT
	call	_Z10read_timerv
	movq	48(%rsp), %rdi
	movl	$1, %ecx
	movq	%rbx, %rsi
	movl	$4593920, %edx
	movsd	%xmm0, 16(%rsp)
	call	cudaMemcpy@PLT
	leaq	.LC8(%rip), %rsi
	movl	$2, %edi
	movl	%eax, %r12d
	xorl	%eax, %eax
	call	__printf_chk@PLT
	leaq	.LC9(%rip), %rsi
	testl	%r12d, %r12d
	jne	.L33
	leaq	.LC10(%rip), %rsi
.L33:
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movq	56(%rsp), %rdi
	movl	$1, %ecx
	movq	%rbp, %rsi
	movl	$4593920, %edx
	call	cudaMemcpy@PLT
	leaq	.LC11(%rip), %rsi
	movl	$2, %edi
	movl	%eax, %r12d
	xorl	%eax, %eax
	call	__printf_chk@PLT
	leaq	.LC9(%rip), %rsi
	testl	%r12d, %r12d
	jne	.L34
	leaq	.LC10(%rip), %rsi
.L34:
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	call	cudaDeviceSynchronize@PLT
	call	_Z10read_timerv
	movl	72(%rsp), %ecx
	movl	84(%rsp), %esi
	xorl	%r9d, %r9d
	movq	64(%rsp), %rdx
	movq	76(%rsp), %rdi
	xorl	%r8d, %r8d
	movsd	%xmm0, (%rsp)
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L27
	movq	56(%rsp), %rsi
	movq	48(%rsp), %rdi
	movl	$2296960, %edx
	call	_Z33__device_stub__Z10calc_calibPsS_iPsS_i
.L27:
	call	cudaDeviceSynchronize@PLT
	call	_Z10read_timerv
	movq	48(%rsp), %rsi
	movl	$2, %ecx
	movq	%rbx, %rdi
	movl	$4593920, %edx
	movsd	%xmm0, 8(%rsp)
	call	cudaMemcpy@PLT
	leaq	.LC12(%rip), %rsi
	movl	$2, %edi
	movl	%eax, %r12d
	xorl	%eax, %eax
	call	__printf_chk@PLT
	leaq	.LC9(%rip), %rsi
	testl	%r12d, %r12d
	jne	.L35
	leaq	.LC10(%rip), %rsi
.L35:
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	call	cudaDeviceSynchronize@PLT
	call	_Z10read_timerv
	movswl	4593918(%rbx), %eax
	movswl	(%rbx), %edx
	leaq	.LC13(%rip), %rsi
	movswl	2(%rbx), %ecx
	movsd	%xmm0, 40(%rsp)
	movswl	4(%rbx), %r8d
	movl	$2, %edi
	pushq	%rax
	.cfi_def_cfa_offset 136
	movswl	4593916(%rbx), %eax
	movswl	4593914(%rbx), %r9d
	pushq	%rax
	.cfi_def_cfa_offset 144
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	$2296960, %esi
	movl	$3, %edx
	movq	%rbx, %rdi
	call	_Z4fillPsii
	movl	$1, %edx
	movq	%rbp, %rdi
	call	_Z4fillPsii
	call	_Z10read_timerv
	movl	$2296960, %edx
	movq	%rbp, %rsi
	movq	%rbx, %rdi
	movsd	%xmm0, 40(%rsp)
	call	_Z14calc_calib_cpuPsS_i
	call	_Z10read_timerv
	movswl	2(%rbx), %ecx
	movswl	4(%rbx), %r8d
	leaq	.LC14(%rip), %rsi
	movsd	%xmm0, 48(%rsp)
	popq	%rax
	.cfi_def_cfa_offset 136
	popq	%rdx
	.cfi_def_cfa_offset 128
	movl	$2, %edi
	movswl	(%rbx), %edx
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	$2296960, %edx
	leaq	.LC15(%rip), %rsi
	xorl	%eax, %eax
	movl	$2, %edi
	call	__printf_chk@PLT
	movsd	(%rsp), %xmm0
	subsd	16(%rsp), %xmm0
	movb	$1, %al
	leaq	.LC16(%rip), %rsi
	movl	$2, %edi
	call	__printf_chk@PLT
	movsd	8(%rsp), %xmm0
	subsd	(%rsp), %xmm0
	movb	$1, %al
	leaq	.LC17(%rip), %rsi
	movl	$2, %edi
	call	__printf_chk@PLT
	movsd	32(%rsp), %xmm2
	subsd	24(%rsp), %xmm2
	movb	$1, %al
	leaq	.LC18(%rip), %rsi
	movl	$2, %edi
	movaps	%xmm2, %xmm0
	call	__printf_chk@PLT
	movsd	40(%rsp), %xmm1
	subsd	8(%rsp), %xmm1
	movb	$1, %al
	leaq	.LC19(%rip), %rsi
	movl	$2, %edi
	movaps	%xmm1, %xmm0
	call	__printf_chk@PLT
	leaq	.LC20(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	leaq	.LC21(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movq	%rbx, %rdi
	call	free@PLT
	movq	%rbp, %rdi
	call	free@PLT
	movq	48(%rsp), %rdi
	call	cudaFree@PLT
	movq	56(%rsp), %rdi
	call	cudaFree@PLT
	leaq	.LC22(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	xorl	%eax, %eax
.L19:
	movq	88(%rsp), %rdx
	subq	%fs:40, %rdx
	je	.L31
	call	__stack_chk_fail@PLT
.L31:
	addq	$96, %rsp
	.cfi_def_cfa_offset 32
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%rbp
	.cfi_def_cfa_offset 16
	popq	%r12
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2030:
	.size	main, .-main
	.section	.rodata.str1.1
.LC23:
	.string	"_Z10calc_calibPsS_i"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2058:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	leaq	_ZL15__fatDeviceText(%rip), %rdi
	call	__cudaRegisterFatBinary@PLT
	pushq	$0
	.cfi_def_cfa_offset 24
	xorl	%r9d, %r9d
	orl	$-1, %r8d
	pushq	$0
	.cfi_def_cfa_offset 32
	leaq	.LC23(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z10calc_calibPsS_i(%rip), %rsi
	pushq	$0
	.cfi_def_cfa_offset 40
	movq	%rdx, %rcx
	pushq	$0
	.cfi_def_cfa_offset 48
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	call	__cudaRegisterFunction@PLT
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	call	__cudaRegisterFatBinaryEnd@PLT
	leaq	_ZL26__cudaUnregisterBinaryUtilv(%rip), %rdi
	popq	%rdx
	.cfi_def_cfa_offset 8
	jmp	atexit@PLT
	.cfi_endproc
.LFE2058:
	.size	_ZL24__sti____cudaRegisterAllv, .-_ZL24__sti____cudaRegisterAllv
	.section	.init_array,"aw"
	.align 8
	.quad	_ZL24__sti____cudaRegisterAllv
	.section	.nvFatBinSegment,"aw"
	.align 8
	.type	_ZL15__fatDeviceText, @object
	.size	_ZL15__fatDeviceText, 24
_ZL15__fatDeviceText:
	.long	1180844977
	.long	1
	.quad	fatbinData
	.quad	0
	.local	_ZL20__cudaFatCubinHandle
	.comm	_ZL20__cudaFatCubinHandle,8,8
	.section	.rodata.cst8,"aM",@progbits,8
	.align 8
.LC0:
	.long	-1598689907
	.long	1051772663
	.ident	"GCC: (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:

#include <stdlib.h>
#include <sys/time.h>
#include <stdio.h>
#include <hip/hip_runtime.h>
#include <math.h>

//#define N 1000000
#define SQRT_TWO_PI 2.506628274631000
#define BLOCK_D1 1024
#define BLOCK_D2 1
#define BLOCK_D3 1

// Note: Needs compute capability >= 2.0 for calculation with doubles, so compile with:
// nvcc kernelExample.cu -arch=compute_20 -code=sm_20,compute_20 -o kernelExample
// -use_fast_math doesn't seem to have any effect on speed

// CUDA kernel:
__global__ void calc_calib(short* raws, short* darks, int N) {
  // note that this assumes no third dimension to the grid
  // id of the block
  int myblock = blockIdx.x + blockIdx.y * gridDim.x;
  // size of each block (within grid of blocks)
  int blocksize = blockDim.x * blockDim.y * blockDim.z;
  // id of thread in a given block
  int subthread = threadIdx.z*(blockDim.x * blockDim.y) + threadIdx.y*blockDim.x + threadIdx.x;
  // assign overall id/index of the thread
  int idx = myblock * blocksize + subthread;

  if(idx < N) {
    raws[idx] -= darks[idx];
  }
}

// CPU analog for speed comparison
int calc_calib_cpu(short* raws, short* darks, int N) {
  for(int idx = 0; idx < N; idx++){
    raws[idx] -= darks[idx];
  }
  return 0;
}

/* ---------------------- host code -----------------------------*/
void fill( short *p, int n, int val ) {
  for(int i = 0; i < n; i++){
    p[i] = val;
  } 
}

double read_timer() {
  struct timeval end;
  gettimeofday( &end, NULL);
  return end.tv_sec+1.e-6*end.tv_usec;
}

int main (int argc, char *argv[]) {
  short* cpu_raws;
  short* gpu_raws;
  short* cpu_darks;
  short* gpu_darks;
  int N;
  hipError_t cudaStat;

  printf("==========================================\n");
  for( N = 2296960; N <= 2296960; N+=2296960 ) {
    cpu_raws = (short*) malloc( sizeof(short)*N );
    cudaStat = hipMalloc(&gpu_raws, sizeof(short)*N);
    if(cudaStat != hipSuccess) {
      printf ("device memory allocation failed on gpu_raws");
      return EXIT_FAILURE;
    }
    cpu_darks = (short*) malloc( sizeof(short)*N );
    cudaStat = hipMalloc(&gpu_darks, sizeof(short)*N);
    if(cudaStat != hipSuccess) {
      printf ("device memory allocation failed on gpu_darks");
      return EXIT_FAILURE;
    }

    // fixed block dimensions (1024x1x1 threads)
    const dim3 blockSize(BLOCK_D1, BLOCK_D2, BLOCK_D3);

    // determine number of blocks we need for a given problem size
    int tmp = ceil(pow(N/(BLOCK_D1 * BLOCK_D2 * BLOCK_D3), 0.5));
    printf("Grid dimension is %i x %i\n", tmp, tmp);
    dim3 gridSize(tmp, tmp, 1);

    int nthreads = BLOCK_D1*BLOCK_D2*BLOCK_D3*tmp*tmp;
    if (nthreads < N){
      printf("\n================ NOT ENOUGH THREADS TO COVER N=%d =======================\n\n", N);
    } else {
      printf("Launching %d threads (N=%d)\n", nthreads, N);
    }

    // simulate 'data'
    fill(cpu_raws, N, 3);
    fill(cpu_darks, N, 1);
    printf("Input values (raw): %d %d %d...\n", cpu_raws[0], cpu_raws[1], cpu_raws[2]);
    printf("Input values (dark): %d %d %d...\n", cpu_darks[0], cpu_darks[1], cpu_darks[2]);
    hipDeviceSynchronize();
    double tInit = read_timer();

    // copy input data to the GPU
    cudaStat = hipMemcpy(gpu_raws, cpu_raws, N*sizeof(short), hipMemcpyHostToDevice);
    printf("Memory Copy from Host to Device (raw)");
    if (cudaStat){
      printf("failed.\n");
    } else {
      printf("successful.\n");
    }
    cudaStat = hipMemcpy(gpu_darks, cpu_darks, N*sizeof(short), hipMemcpyHostToDevice);
    printf("Memory Copy from Host to Device (dark)");
    if (cudaStat){
      printf("failed.\n");
    } else {
      printf("successful.\n");
    }
    hipDeviceSynchronize();
    double tTransferToGPU = read_timer();

    // do the calculation
    calc_calib<<<gridSize, blockSize>>>(gpu_raws, gpu_darks, N);

    hipDeviceSynchronize();
    double tCalc = read_timer();

    cudaStat = hipMemcpy(cpu_raws, gpu_raws, N*sizeof(short), hipMemcpyDeviceToHost);
    printf("Memory Copy from Device to Host (raw) ");
    if (cudaStat){
      printf("failed.\n");
    } else {
      printf("successful.\n");
    }
    hipDeviceSynchronize();
    double tTransferFromGPU = read_timer();

    printf("Output values: %d %d %d...%d %d %d\n", cpu_raws[0], cpu_raws[1], cpu_raws[2], cpu_raws[N-3], cpu_raws[N-2], cpu_raws[N-1]);

    // do calculation on CPU for comparison (unfair as this will only use one core)
    fill(cpu_raws, N, 3);
    fill(cpu_darks, N, 1);
    double tInit2 = read_timer();
    calc_calib_cpu(cpu_raws, cpu_darks, N);
    double tCalcCPU = read_timer();

    printf("Output values (CPU): %d %d %d...\n", cpu_raws[0], cpu_raws[1], cpu_raws[2]);

    printf("Timing results for n = %d\n", N);
    printf("Transfer to GPU time: %f\n", tTransferToGPU - tInit);
    printf("Calculation time (GPU): %f\n", tCalc - tTransferToGPU);
    printf("Calculation time (CPU): %f\n", tCalcCPU - tInit2);
    printf("Transfer from GPU time: %f\n", tTransferFromGPU - tCalc);

    printf("Freeing memory...\n");
    printf("==============================================\n");
    free(cpu_raws);
    free(cpu_darks);
    hipFree(gpu_raws);
    hipFree(gpu_darks);

  }
  printf("\n\nFinished.\n\n");
  return 0;

}






	.text
	.file	"kernelExample.hip"
	.globl	_Z25__device_stub__calc_calibPsS_i # -- Begin function _Z25__device_stub__calc_calibPsS_i
	.type	_Z25__device_stub__calc_calibPsS_i,@function
_Z25__device_stub__calc_calibPsS_i:     # @_Z25__device_stub__calc_calibPsS_i
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%r13
	.cfi_def_cfa_offset 32
	pushq	%r12
	.cfi_def_cfa_offset 40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	subq	$112, %rsp
	.cfi_def_cfa_offset 160
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	leaq	40(%rsp), %rax
	movq	%rdi, (%rax)
	leaq	32(%rsp), %rcx
	movq	%rsi, (%rcx)
	leaq	12(%rsp), %rsi
	movl	%edx, (%rsi)
	leaq	80(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rcx, 8(%rbx)
	movq	%rsi, 16(%rbx)
	leaq	64(%rsp), %r14
	leaq	48(%rsp), %r15
	leaq	24(%rsp), %r12
	leaq	16(%rsp), %r13
	movq	%r14, %rdi
	movq	%r15, %rsi
	movq	%r12, %rdx
	movq	%r13, %rcx
	callq	__hipPopCallConfiguration
	movq	(%r14), %rsi
	movl	8(%r14), %edx
	movq	(%r15), %rcx
	movl	8(%r15), %r8d
	movl	$_Z10calc_calibPsS_i, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$128, %rsp
	.cfi_adjust_cfa_offset -128
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end0:
	.size	_Z25__device_stub__calc_calibPsS_i, .Lfunc_end0-_Z25__device_stub__calc_calibPsS_i
	.cfi_endproc
                                        # -- End function
	.globl	_Z14calc_calib_cpuPsS_i         # -- Begin function _Z14calc_calib_cpuPsS_i
	.type	_Z14calc_calib_cpuPsS_i,@function
_Z14calc_calib_cpuPsS_i:                # @_Z14calc_calib_cpuPsS_i
	.cfi_startproc
# %bb.0:
	testl	%edx, %edx
	jle	.LBB1_3
# %bb.1:                                # %.lr.ph.preheader
	movl	%edx, %eax
	xorl	%ecx, %ecx
.LBB1_2:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movzwl	(%rsi,%rcx,2), %edx
	subw	%dx, (%rdi,%rcx,2)
	incq	%rcx
	cmpq	%rcx, %rax
	jne	.LBB1_2
.LBB1_3:                                # %._crit_edge
	xorl	%eax, %eax
	retq
.Lfunc_end1:
	.size	_Z14calc_calib_cpuPsS_i, .Lfunc_end1-_Z14calc_calib_cpuPsS_i
	.cfi_endproc
                                        # -- End function
	.globl	_Z4fillPsii                     # -- Begin function _Z4fillPsii
	.type	_Z4fillPsii,@function
_Z4fillPsii:                            # @_Z4fillPsii
	.cfi_startproc
# %bb.0:
	testl	%esi, %esi
	jle	.LBB2_3
# %bb.1:                                # %.lr.ph
	movl	%esi, %eax
	xorl	%ecx, %ecx
.LBB2_2:                                # =>This Inner Loop Header: Depth=1
	movw	%dx, (%rdi,%rcx,2)
	incq	%rcx
	cmpq	%rcx, %rax
	jne	.LBB2_2
.LBB2_3:                                # %._crit_edge
	retq
.Lfunc_end2:
	.size	_Z4fillPsii, .Lfunc_end2-_Z4fillPsii
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function _Z10read_timerv
.LCPI3_0:
	.quad	0x3eb0c6f7a0b5ed8d              # double 9.9999999999999995E-7
	.text
	.globl	_Z10read_timerv
	.type	_Z10read_timerv,@function
_Z10read_timerv:                        # @_Z10read_timerv
	.cfi_startproc
# %bb.0:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	subq	$16, %rsp
	.cfi_def_cfa_offset 32
	.cfi_offset %rbx, -16
	movq	%rsp, %rbx
	movq	%rbx, %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	cvtsi2sdq	(%rbx), %xmm1
	cvtsi2sdq	8(%rbx), %xmm0
	mulsd	.LCPI3_0(%rip), %xmm0
	addsd	%xmm1, %xmm0
	addq	$16, %rsp
	.cfi_def_cfa_offset 16
	popq	%rbx
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end3:
	.size	_Z10read_timerv, .Lfunc_end3-_Z10read_timerv
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function main
.LCPI4_0:
	.quad	0x3eb0c6f7a0b5ed8d              # double 9.9999999999999995E-7
	.text
	.globl	main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:                                # %.critedge
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%r15
	.cfi_def_cfa_offset 24
	pushq	%r14
	.cfi_def_cfa_offset 32
	pushq	%r13
	.cfi_def_cfa_offset 40
	pushq	%r12
	.cfi_def_cfa_offset 48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	subq	$88, %rsp
	.cfi_def_cfa_offset 144
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movl	$.Lstr, %edi
	callq	puts@PLT
	movl	$4593920, %edi                  # imm = 0x461900
	callq	malloc
	movq	%rax, %rbx
	leaq	48(%rsp), %rdi
	movl	$4593920, %esi                  # imm = 0x461900
	callq	hipMalloc
	testl	%eax, %eax
	je	.LBB4_3
# %bb.1:
	movl	$.L.str.1, %edi
	jmp	.LBB4_2
.LBB4_3:
	movl	$4593920, %edi                  # imm = 0x461900
	callq	malloc
	movq	%rax, %r14
	leaq	64(%rsp), %rdi
	movl	$4593920, %esi                  # imm = 0x461900
	callq	hipMalloc
	testl	%eax, %eax
	je	.LBB4_5
# %bb.4:
	movl	$.L.str.2, %edi
.LBB4_2:
	xorl	%eax, %eax
	callq	printf
	movl	$1, %r15d
.LBB4_18:
	movl	%r15d, %eax
	addq	$88, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%r12
	.cfi_def_cfa_offset 40
	popq	%r13
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.LBB4_5:
	.cfi_def_cfa_offset 144
	xorl	%r15d, %r15d
	movl	$.L.str.3, %edi
	movl	$48, %esi
	movl	$48, %edx
	xorl	%eax, %eax
	callq	printf
	movl	$.L.str.5, %edi
	movl	$2359296, %esi                  # imm = 0x240000
	movl	$2296960, %edx                  # imm = 0x230C80
	xorl	%eax, %eax
	callq	printf
.LBB4_6:                                # =>This Inner Loop Header: Depth=1
	movw	$3, (%rbx,%r15,2)
	incq	%r15
	cmpq	$2296960, %r15                  # imm = 0x230C80
	jne	.LBB4_6
# %bb.7:                                # %_Z4fillPsii.exit.preheader
	xorl	%eax, %eax
.LBB4_8:                                # %_Z4fillPsii.exit
                                        # =>This Inner Loop Header: Depth=1
	movw	$1, (%r14,%rax,2)
	incq	%rax
	cmpq	$2296960, %rax                  # imm = 0x230C80
	jne	.LBB4_8
# %bb.9:                                # %_Z4fillPsii.exit95
	movswl	(%rbx), %esi
	movswl	2(%rbx), %edx
	movswl	4(%rbx), %ecx
	movl	$.L.str.6, %edi
	xorl	%eax, %eax
	callq	printf
	movswl	(%r14), %esi
	movswl	2(%r14), %edx
	movswl	4(%r14), %ecx
	movl	$.L.str.7, %edi
	xorl	%eax, %eax
	callq	printf
	callq	hipDeviceSynchronize
	leaq	72(%rsp), %r15
	movq	%r15, %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	cvtsi2sdq	(%r15), %xmm0
	movsd	%xmm0, 24(%rsp)                 # 8-byte Spill
	xorps	%xmm0, %xmm0
	cvtsi2sdq	8(%r15), %xmm0
	mulsd	.LCPI4_0(%rip), %xmm0
	movsd	%xmm0, 56(%rsp)                 # 8-byte Spill
	movq	48(%rsp), %rdi
	movl	$4593920, %edx                  # imm = 0x461900
	movq	%rbx, %rsi
	movl	$1, %ecx
	callq	hipMemcpy
	movl	%eax, %ebp
	movl	$.L.str.8, %edi
	xorl	%eax, %eax
	callq	printf
	testl	%ebp, %ebp
	movl	$.Lstr.6, %r12d
	movl	$.Lstr.7, %r15d
	movl	$.Lstr.7, %edi
	cmoveq	%r12, %rdi
	callq	puts@PLT
	movq	64(%rsp), %rdi
	movl	$4593920, %edx                  # imm = 0x461900
	movq	%r14, %rsi
	movl	$1, %ecx
	callq	hipMemcpy
	movl	%eax, %ebp
	movl	$.L.str.11, %edi
	xorl	%eax, %eax
	callq	printf
	testl	%ebp, %ebp
	movl	$.Lstr.7, %edi
	cmoveq	%r12, %rdi
	callq	puts@PLT
	callq	hipDeviceSynchronize
	leaq	72(%rsp), %r12
	movq	%r12, %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	xorps	%xmm0, %xmm0
	cvtsi2sdq	(%r12), %xmm0
	movsd	%xmm0, 32(%rsp)                 # 8-byte Spill
	xorps	%xmm0, %xmm0
	cvtsi2sdq	8(%r12), %xmm0
	mulsd	.LCPI4_0(%rip), %xmm0
	movsd	%xmm0, 40(%rsp)                 # 8-byte Spill
	movabsq	$206158430256, %rdi             # imm = 0x3000000030
	movabsq	$4294968320, %rdx               # imm = 0x100000400
	movl	$1, %esi
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB4_11
# %bb.10:
	movq	48(%rsp), %rdi
	movq	64(%rsp), %rsi
	movl	$2296960, %edx                  # imm = 0x230C80
	callq	_Z25__device_stub__calc_calibPsS_i
.LBB4_11:
	movsd	56(%rsp), %xmm0                 # 8-byte Reload
                                        # xmm0 = mem[0],zero
	addsd	24(%rsp), %xmm0                 # 8-byte Folded Reload
	movsd	%xmm0, 56(%rsp)                 # 8-byte Spill
	movsd	40(%rsp), %xmm0                 # 8-byte Reload
                                        # xmm0 = mem[0],zero
	addsd	32(%rsp), %xmm0                 # 8-byte Folded Reload
	movsd	%xmm0, 40(%rsp)                 # 8-byte Spill
	callq	hipDeviceSynchronize
	xorl	%r13d, %r13d
	leaq	72(%rsp), %r12
	movq	%r12, %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	xorps	%xmm0, %xmm0
	cvtsi2sdq	(%r12), %xmm0
	cvtsi2sdq	8(%r12), %xmm1
	movsd	.LCPI4_0(%rip), %xmm2           # xmm2 = mem[0],zero
	mulsd	%xmm2, %xmm1
	addsd	%xmm0, %xmm1
	movsd	%xmm1, 32(%rsp)                 # 8-byte Spill
	movq	48(%rsp), %rsi
	movl	$4593920, %edx                  # imm = 0x461900
	movq	%rbx, %rdi
	movl	$2, %ecx
	callq	hipMemcpy
	movl	%eax, %ebp
	movl	$.L.str.12, %edi
	xorl	%eax, %eax
	callq	printf
	testl	%ebp, %ebp
	movl	$.Lstr.6, %eax
	cmoveq	%rax, %r15
	movq	%r15, %rdi
	callq	puts@PLT
	callq	hipDeviceSynchronize
	leaq	72(%rsp), %r15
	movq	%r15, %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	xorps	%xmm0, %xmm0
	cvtsi2sdq	8(%r15), %xmm0
	movq	(%r15), %r15
	mulsd	.LCPI4_0(%rip), %xmm0
	movsd	%xmm0, 24(%rsp)                 # 8-byte Spill
	movswl	(%rbx), %esi
	movswl	2(%rbx), %edx
	movswl	4(%rbx), %ecx
	movswl	4593914(%rbx), %r8d
	movswl	4593916(%rbx), %r9d
	movswl	4593918(%rbx), %eax
	movl	%eax, (%rsp)
	movl	$.L.str.13, %edi
	xorl	%eax, %eax
	callq	printf
.LBB4_12:                               # =>This Inner Loop Header: Depth=1
	movw	$3, (%rbx,%r13,2)
	incq	%r13
	cmpq	$2296960, %r13                  # imm = 0x230C80
	jne	.LBB4_12
# %bb.13:                               # %_Z4fillPsii.exit99.preheader
	xorps	%xmm0, %xmm0
	cvtsi2sd	%r15, %xmm0
	movsd	%xmm0, 16(%rsp)                 # 8-byte Spill
	xorl	%eax, %eax
.LBB4_14:                               # %_Z4fillPsii.exit99
                                        # =>This Inner Loop Header: Depth=1
	movw	$1, (%r14,%rax,2)
	incq	%rax
	cmpq	$2296960, %rax                  # imm = 0x230C80
	jne	.LBB4_14
# %bb.15:                               # %_Z4fillPsii.exit103
	xorl	%r12d, %r12d
	leaq	72(%rsp), %r15
	movq	%r15, %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	movq	(%r15), %rax
	xorps	%xmm1, %xmm1
	cvtsi2sdq	8(%r15), %xmm1
	mulsd	.LCPI4_0(%rip), %xmm1
.LBB4_16:                               # %.lr.ph.i
                                        # =>This Inner Loop Header: Depth=1
	movzwl	(%r14,%r12,2), %ecx
	subw	%cx, (%rbx,%r12,2)
	incq	%r12
	cmpq	$2296960, %r12                  # imm = 0x230C80
	jne	.LBB4_16
# %bb.17:                               # %_Z14calc_calib_cpuPsS_i.exit
	movsd	24(%rsp), %xmm0                 # 8-byte Reload
                                        # xmm0 = mem[0],zero
	addsd	16(%rsp), %xmm0                 # 8-byte Folded Reload
	movsd	%xmm0, 24(%rsp)                 # 8-byte Spill
	xorps	%xmm0, %xmm0
	cvtsi2sd	%rax, %xmm0
	addsd	%xmm0, %xmm1
	movsd	%xmm1, 16(%rsp)                 # 8-byte Spill
	xorl	%r15d, %r15d
	leaq	72(%rsp), %r12
	movq	%r12, %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	xorps	%xmm0, %xmm0
	cvtsi2sdq	(%r12), %xmm0
	xorps	%xmm1, %xmm1
	cvtsi2sdq	8(%r12), %xmm1
	mulsd	.LCPI4_0(%rip), %xmm1
	addsd	%xmm0, %xmm1
	subsd	16(%rsp), %xmm1                 # 8-byte Folded Reload
	movsd	%xmm1, 16(%rsp)                 # 8-byte Spill
	movswl	(%rbx), %esi
	movswl	2(%rbx), %edx
	movswl	4(%rbx), %ecx
	movl	$.L.str.14, %edi
	xorl	%eax, %eax
	callq	printf
	movl	$.L.str.15, %edi
	movl	$2296960, %esi                  # imm = 0x230C80
	xorl	%eax, %eax
	callq	printf
	movsd	40(%rsp), %xmm0                 # 8-byte Reload
                                        # xmm0 = mem[0],zero
	subsd	56(%rsp), %xmm0                 # 8-byte Folded Reload
	movl	$.L.str.16, %edi
	movb	$1, %al
	callq	printf
	movsd	32(%rsp), %xmm0                 # 8-byte Reload
                                        # xmm0 = mem[0],zero
	subsd	40(%rsp), %xmm0                 # 8-byte Folded Reload
	movl	$.L.str.17, %edi
	movb	$1, %al
	callq	printf
	movl	$.L.str.18, %edi
	movsd	16(%rsp), %xmm0                 # 8-byte Reload
                                        # xmm0 = mem[0],zero
	movb	$1, %al
	callq	printf
	movsd	24(%rsp), %xmm0                 # 8-byte Reload
                                        # xmm0 = mem[0],zero
	subsd	32(%rsp), %xmm0                 # 8-byte Folded Reload
	movl	$.L.str.19, %edi
	movb	$1, %al
	callq	printf
	movl	$.Lstr.8, %edi
	callq	puts@PLT
	movl	$.Lstr.9, %edi
	callq	puts@PLT
	movq	%rbx, %rdi
	callq	free
	movq	%r14, %rdi
	callq	free
	movq	48(%rsp), %rdi
	callq	hipFree
	movq	64(%rsp), %rdi
	callq	hipFree
	movl	$.Lstr.1, %edi
	callq	puts@PLT
	jmp	.LBB4_18
.Lfunc_end4:
	.size	main, .Lfunc_end4-main
	.cfi_endproc
                                        # -- End function
	.p2align	4, 0x90                         # -- Begin function __hip_module_ctor
	.type	__hip_module_ctor,@function
__hip_module_ctor:                      # @__hip_module_ctor
	.cfi_startproc
# %bb.0:
	subq	$40, %rsp
	.cfi_def_cfa_offset 48
	movq	__hip_gpubin_handle(%rip), %rdi
	testq	%rdi, %rdi
	jne	.LBB5_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rdi
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB5_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z10calc_calibPsS_i, %esi
	movl	$.L__unnamed_1, %edx
	movl	$.L__unnamed_1, %ecx
	movl	$-1, %r8d
	xorl	%r9d, %r9d
	callq	__hipRegisterFunction
	movl	$__hip_module_dtor, %edi
	addq	$40, %rsp
	.cfi_def_cfa_offset 8
	jmp	atexit                          # TAILCALL
.Lfunc_end5:
	.size	__hip_module_ctor, .Lfunc_end5-__hip_module_ctor
	.cfi_endproc
                                        # -- End function
	.p2align	4, 0x90                         # -- Begin function __hip_module_dtor
	.type	__hip_module_dtor,@function
__hip_module_dtor:                      # @__hip_module_dtor
	.cfi_startproc
# %bb.0:
	movq	__hip_gpubin_handle(%rip), %rdi
	testq	%rdi, %rdi
	je	.LBB6_2
# %bb.1:
	pushq	%rax
	.cfi_def_cfa_offset 16
	callq	__hipUnregisterFatBinary
	movq	$0, __hip_gpubin_handle(%rip)
	addq	$8, %rsp
	.cfi_def_cfa_offset 8
.LBB6_2:
	retq
.Lfunc_end6:
	.size	__hip_module_dtor, .Lfunc_end6-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	_Z10calc_calibPsS_i,@object     # @_Z10calc_calibPsS_i
	.section	.rodata,"a",@progbits
	.globl	_Z10calc_calibPsS_i
	.p2align	3, 0x0
_Z10calc_calibPsS_i:
	.quad	_Z25__device_stub__calc_calibPsS_i
	.size	_Z10calc_calibPsS_i, 8

	.type	.L.str.1,@object                # @.str.1
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str.1:
	.asciz	"device memory allocation failed on gpu_raws"
	.size	.L.str.1, 44

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"device memory allocation failed on gpu_darks"
	.size	.L.str.2, 45

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"Grid dimension is %i x %i\n"
	.size	.L.str.3, 27

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"Launching %d threads (N=%d)\n"
	.size	.L.str.5, 29

	.type	.L.str.6,@object                # @.str.6
.L.str.6:
	.asciz	"Input values (raw): %d %d %d...\n"
	.size	.L.str.6, 33

	.type	.L.str.7,@object                # @.str.7
.L.str.7:
	.asciz	"Input values (dark): %d %d %d...\n"
	.size	.L.str.7, 34

	.type	.L.str.8,@object                # @.str.8
.L.str.8:
	.asciz	"Memory Copy from Host to Device (raw)"
	.size	.L.str.8, 38

	.type	.L.str.11,@object               # @.str.11
.L.str.11:
	.asciz	"Memory Copy from Host to Device (dark)"
	.size	.L.str.11, 39

	.type	.L.str.12,@object               # @.str.12
.L.str.12:
	.asciz	"Memory Copy from Device to Host (raw) "
	.size	.L.str.12, 39

	.type	.L.str.13,@object               # @.str.13
.L.str.13:
	.asciz	"Output values: %d %d %d...%d %d %d\n"
	.size	.L.str.13, 36

	.type	.L.str.14,@object               # @.str.14
.L.str.14:
	.asciz	"Output values (CPU): %d %d %d...\n"
	.size	.L.str.14, 34

	.type	.L.str.15,@object               # @.str.15
.L.str.15:
	.asciz	"Timing results for n = %d\n"
	.size	.L.str.15, 27

	.type	.L.str.16,@object               # @.str.16
.L.str.16:
	.asciz	"Transfer to GPU time: %f\n"
	.size	.L.str.16, 26

	.type	.L.str.17,@object               # @.str.17
.L.str.17:
	.asciz	"Calculation time (GPU): %f\n"
	.size	.L.str.17, 28

	.type	.L.str.18,@object               # @.str.18
.L.str.18:
	.asciz	"Calculation time (CPU): %f\n"
	.size	.L.str.18, 28

	.type	.L.str.19,@object               # @.str.19
.L.str.19:
	.asciz	"Transfer from GPU time: %f\n"
	.size	.L.str.19, 28

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z10calc_calibPsS_i"
	.size	.L__unnamed_1, 20

	.type	__hip_fatbin_wrapper,@object    # @__hip_fatbin_wrapper
	.section	.hipFatBinSegment,"a",@progbits
	.p2align	3, 0x0
__hip_fatbin_wrapper:
	.long	1212764230                      # 0x48495046
	.long	1                               # 0x1
	.quad	__hip_fatbin
	.quad	0
	.size	__hip_fatbin_wrapper, 24

	.type	__hip_gpubin_handle,@object     # @__hip_gpubin_handle
	.local	__hip_gpubin_handle
	.comm	__hip_gpubin_handle,8,8
	.section	.init_array,"aw",@init_array
	.p2align	3, 0x0
	.quad	__hip_module_ctor
	.type	__hip_cuid_,@object             # @__hip_cuid_
	.bss
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               # 0x0
	.size	__hip_cuid_, 1

	.type	.Lstr,@object                   # @str
	.section	.rodata.str1.1,"aMS",@progbits,1
.Lstr:
	.asciz	"=========================================="
	.size	.Lstr, 43

	.type	.Lstr.1,@object                 # @str.1
.Lstr.1:
	.asciz	"\n\nFinished.\n"
	.size	.Lstr.1, 13

	.type	.Lstr.6,@object                 # @str.6
.Lstr.6:
	.asciz	"successful."
	.size	.Lstr.6, 12

	.type	.Lstr.7,@object                 # @str.7
.Lstr.7:
	.asciz	"failed."
	.size	.Lstr.7, 8

	.type	.Lstr.8,@object                 # @str.8
.Lstr.8:
	.asciz	"Freeing memory..."
	.size	.Lstr.8, 18

	.type	.Lstr.9,@object                 # @str.9
.Lstr.9:
	.asciz	"=============================================="
	.size	.Lstr.9, 47

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym _Z25__device_stub__calc_calibPsS_i
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z10calc_calibPsS_i
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z10calc_calibPsS_i     ; -- Begin function _Z10calc_calibPsS_i
	.globl	_Z10calc_calibPsS_i
	.p2align	8
	.type	_Z10calc_calibPsS_i,@function
_Z10calc_calibPsS_i:                    ; @_Z10calc_calibPsS_i
; %bb.0:
	s_clause 0x1
	s_load_b32 s4, s[0:1], 0x18
	s_load_b64 s[2:3], s[0:1], 0x24
	v_bfe_u32 v1, v0, 20, 10
	s_waitcnt lgkmcnt(0)
	s_mul_i32 s4, s4, s15
	s_and_b32 s3, s3, 0xffff
	s_add_i32 s4, s4, s14
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(SKIP_4) | instid1(VALU_DEP_1)
	v_mad_u64_u32 v[2:3], null, s4, s3, v[1:2]
	v_bfe_u32 v1, v0, 10, 10
	s_load_b32 s3, s[0:1], 0x10
	s_lshr_b32 s4, s2, 16
	s_and_b32 s2, s2, 0xffff
	v_mad_u64_u32 v[3:4], null, v2, s4, v[1:2]
	v_and_b32_e32 v2, 0x3ff, v0
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_2) | instid1(VALU_DEP_1)
	v_mad_u64_u32 v[0:1], null, v3, s2, v[2:3]
	s_mov_b32 s2, exec_lo
	s_waitcnt lgkmcnt(0)
	v_cmpx_gt_i32_e64 s3, v0
	s_cbranch_execz .LBB0_2
; %bb.1:
	s_load_b128 s[0:3], s[0:1], 0x0
	v_ashrrev_i32_e32 v1, 31, v0
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_1)
	v_lshlrev_b64 v[0:1], 1, v[0:1]
	s_waitcnt lgkmcnt(0)
	v_add_co_u32 v2, vcc_lo, s2, v0
	s_delay_alu instid0(VALU_DEP_2)
	v_add_co_ci_u32_e32 v3, vcc_lo, s3, v1, vcc_lo
	v_add_co_u32 v0, vcc_lo, s0, v0
	v_add_co_ci_u32_e32 v1, vcc_lo, s1, v1, vcc_lo
	global_load_u16 v2, v[2:3], off
	global_load_u16 v3, v[0:1], off
	s_waitcnt vmcnt(0)
	v_sub_nc_u16 v2, v3, v2
	global_store_b16 v[0:1], v2, off
.LBB0_2:
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z10calc_calibPsS_i
		.amdhsa_group_segment_fixed_size 0
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 280
		.amdhsa_user_sgpr_count 14
		.amdhsa_user_sgpr_dispatch_ptr 0
		.amdhsa_user_sgpr_queue_ptr 0
		.amdhsa_user_sgpr_kernarg_segment_ptr 1
		.amdhsa_user_sgpr_dispatch_id 0
		.amdhsa_user_sgpr_private_segment_size 0
		.amdhsa_wavefront_size32 1
		.amdhsa_uses_dynamic_stack 0
		.amdhsa_enable_private_segment 0
		.amdhsa_system_sgpr_workgroup_id_x 1
		.amdhsa_system_sgpr_workgroup_id_y 1
		.amdhsa_system_sgpr_workgroup_id_z 0
		.amdhsa_system_sgpr_workgroup_info 0
		.amdhsa_system_vgpr_workitem_id 2
		.amdhsa_next_free_vgpr 5
		.amdhsa_next_free_sgpr 16
		.amdhsa_float_round_mode_32 0
		.amdhsa_float_round_mode_16_64 0
		.amdhsa_float_denorm_mode_32 3
		.amdhsa_float_denorm_mode_16_64 3
		.amdhsa_dx10_clamp 1
		.amdhsa_ieee_mode 1
		.amdhsa_fp16_overflow 0
		.amdhsa_workgroup_processor_mode 1
		.amdhsa_memory_ordered 1
		.amdhsa_forward_progress 0
		.amdhsa_shared_vgpr_count 0
		.amdhsa_exception_fp_ieee_invalid_op 0
		.amdhsa_exception_fp_denorm_src 0
		.amdhsa_exception_fp_ieee_div_zero 0
		.amdhsa_exception_fp_ieee_overflow 0
		.amdhsa_exception_fp_ieee_underflow 0
		.amdhsa_exception_fp_ieee_inexact 0
		.amdhsa_exception_int_div_zero 0
	.end_amdhsa_kernel
	.text
.Lfunc_end0:
	.size	_Z10calc_calibPsS_i, .Lfunc_end0-_Z10calc_calibPsS_i
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 240
; NumSgprs: 18
; NumVgprs: 5
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 0
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 5
; Occupancy: 16
; WaveLimiterHint : 0
; COMPUTE_PGM_RSRC2:SCRATCH_EN: 0
; COMPUTE_PGM_RSRC2:USER_SGPR: 14
; COMPUTE_PGM_RSRC2:TRAP_HANDLER: 0
; COMPUTE_PGM_RSRC2:TGID_X_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Y_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Z_EN: 0
; COMPUTE_PGM_RSRC2:TIDIG_COMP_CNT: 2
	.text
	.p2alignl 7, 3214868480
	.fill 96, 4, 3214868480
	.type	__hip_cuid_,@object             ; @__hip_cuid_
	.section	.bss,"aw",@nobits
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               ; 0x0
	.size	__hip_cuid_, 1

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym __hip_cuid_
	.amdgpu_metadata
---
amdhsa.kernels:
  - .args:
      - .address_space:  global
        .offset:         0
        .size:           8
        .value_kind:     global_buffer
      - .address_space:  global
        .offset:         8
        .size:           8
        .value_kind:     global_buffer
      - .offset:         16
        .size:           4
        .value_kind:     by_value
      - .offset:         24
        .size:           4
        .value_kind:     hidden_block_count_x
      - .offset:         28
        .size:           4
        .value_kind:     hidden_block_count_y
      - .offset:         32
        .size:           4
        .value_kind:     hidden_block_count_z
      - .offset:         36
        .size:           2
        .value_kind:     hidden_group_size_x
      - .offset:         38
        .size:           2
        .value_kind:     hidden_group_size_y
      - .offset:         40
        .size:           2
        .value_kind:     hidden_group_size_z
      - .offset:         42
        .size:           2
        .value_kind:     hidden_remainder_x
      - .offset:         44
        .size:           2
        .value_kind:     hidden_remainder_y
      - .offset:         46
        .size:           2
        .value_kind:     hidden_remainder_z
      - .offset:         64
        .size:           8
        .value_kind:     hidden_global_offset_x
      - .offset:         72
        .size:           8
        .value_kind:     hidden_global_offset_y
      - .offset:         80
        .size:           8
        .value_kind:     hidden_global_offset_z
      - .offset:         88
        .size:           2
        .value_kind:     hidden_grid_dims
    .group_segment_fixed_size: 0
    .kernarg_segment_align: 8
    .kernarg_segment_size: 280
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z10calc_calibPsS_i
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z10calc_calibPsS_i.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     5
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
