//
// Created by rafa on 2/5/21.
//

#include "cupac.cuh"
#include <iostream>


unsigned long long *
autoPairCount(long long npart, const double *positions, double boxsize, int nside, double minsep, double maxsep, int nbins) {

    // Build Grid struct from positions
    Grid *grid = getGrid(boxsize, nside, npart, positions);

    // Get orderedPositions from Grid
    double *orderedPositions;
    long long *numParticlesInGrid, *offset;

    cudaMallocManaged(&orderedPositions, 3 * npart * sizeof(double));
    cudaMallocManaged(&numParticlesInGrid, nside * nside * nside * sizeof(long long));
    cudaMallocManaged(&offset, nside * nside * nside * sizeof(long long));

    gridToOrderedArray(grid, nside, orderedPositions, numParticlesInGrid, offset);

    // Free grid
    for (int i = 0; i < nside * nside * nside; i++) {
        if (grid[i].np > 0) {
            free(grid[i].pos);
        }
    }
    free(grid);

    // Set up rbinsSquared
    double *rbins, *rbinsSquared;

    cudaMallocManaged(&rbins, nbins * sizeof(double));
    cudaMallocManaged(&rbinsSquared, nbins * sizeof(double));

    for (int k = 0; k <= nbins; k++) {
        rbins[k] = (double) (pow(10, k * (log10(maxsep) - log10(minsep)) / nbins + log10(minsep)));
        rbinsSquared[k] = rbins[k] * rbins[k];
    }

    // Precompute key and iwrap for boundary conditions
    double agrid = boxsize / nside;
    int index_max = (int) (maxsep / agrid) + 1;
    int keysize = nside + 2 * index_max;
    int *key, *iwrap;

    cudaMallocManaged(&key, keysize * sizeof(int));
    cudaMallocManaged(&iwrap, keysize * sizeof(int));

    for (int ii = -index_max; ii <= nside + index_max; ii++) {
        if (ii < 0) {
            key[ii + index_max] = ii + nside;
            iwrap[ii + index_max] = -1;
        } else if (ii >= nside) {
            key[ii + index_max] = ii - nside;
            iwrap[ii + index_max] = 1;
        } else {
            key[ii + index_max] = ii;
            iwrap[ii + index_max] = 0;
        }
    }

    // Set up histogram
    unsigned long long *paircounts;

    cudaMallocManaged(&paircounts, nbins * sizeof(unsigned long long));

    for (int k = 0; k < nbins; k++) {
        paircounts[k] = 0;
    }

    // Get maximum potential gridSize and blockSize
    int gridSize, blockSize;
    cudaOccupancyMaxPotentialBlockSize(&gridSize, &blockSize, doPairCount, 0, 0);
    doPairCount<<<gridSize, blockSize>>>(npart, orderedPositions, numParticlesInGrid, offset, boxsize, nside, minsep,
                                         maxsep, nbins, rbinsSquared, paircounts, key, iwrap);

    cudaDeviceSynchronize();

    cudaFree(orderedPositions);
    cudaFree(numParticlesInGrid);
    cudaFree(offset);
    cudaFree(rbins);
    cudaFree(rbinsSquared);
    cudaFree(key);
    cudaFree(iwrap);

    return paircounts;
}
	.file	"tmpxft_002fee6d_00000000-6_cupac.cudafe1.cpp"
	.text
#APP
	.globl _ZSt21ios_base_library_initv

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB3638:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE3638:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	autoPairCount
	.type	autoPairCount, @function
autoPairCount:
.LFB3635:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	movl	%ecx, %r12d
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	xorl	%ebp, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	movl	%edx, %ebx
	movq	%rsi, %rdx
	movq	%rdi, %rsi
	movl	%ebx, %r13d
	imull	%ebx, %r13d
	subq	$328, %rsp
	.cfi_def_cfa_offset 384
	movq	%rdi, 8(%rsp)
	movl	%ebx, %edi
	imull	%ebx, %r13d
	movsd	%xmm0, 16(%rsp)
	movsd	%xmm1, 24(%rsp)
	movsd	%xmm2, 32(%rsp)
	movq	%fs:40, %rax
	movq	%rax, 312(%rsp)
	xorl	%eax, %eax
	movslq	%r13d, %r15
	call	_Z7getGriddixPKd@PLT
	imulq	$24, 8(%rsp), %rsi
	salq	$3, %r15
	leaq	80(%rsp), %rdi
	movl	$1, %edx
	movq	%rax, (%rsp)
	call	cudaMallocManaged@PLT
	movq	%r15, %rsi
	leaq	88(%rsp), %rdi
	movl	$1, %edx
	call	cudaMallocManaged@PLT
	movq	%r15, %rsi
	leaq	96(%rsp), %rdi
	movl	$1, %edx
	call	cudaMallocManaged@PLT
	movq	96(%rsp), %r8
	movq	(%rsp), %rdi
	movl	%ebx, %esi
	movq	88(%rsp), %rcx
	movq	80(%rsp), %rdx
	call	_Z18gridToOrderedArrayP4GridiPdPxS2_@PLT
	movq	(%rsp), %r15
.L3:
	cmpl	%ebp, %r13d
	jle	.L27
	cmpq	$0, (%r15)
	jle	.L4
	movq	8(%r15), %rdi
	call	free@PLT
.L4:
	incl	%ebp
	addq	$16, %r15
	jmp	.L3
.L27:
	movq	(%rsp), %rdi
	movslq	%r12d, %rbp
	xorl	%r13d, %r13d
	salq	$3, %rbp
	call	free@PLT
	leaq	104(%rsp), %rdi
	movl	$1, %edx
	movq	%rbp, %rsi
	call	cudaMallocManaged@PLT
	leaq	112(%rsp), %rdi
	movl	$1, %edx
	movq	%rbp, %rsi
	call	cudaMallocManaged@PLT
.L6:
	cmpl	%r13d, %r12d
	jl	.L28
	movsd	32(%rsp), %xmm0
	call	log10@PLT
	movsd	%xmm0, 40(%rsp)
	movsd	24(%rsp), %xmm0
	call	log10@PLT
	movsd	%xmm0, (%rsp)
	movsd	24(%rsp), %xmm0
	call	log10@PLT
	cvtsi2sdl	%r13d, %xmm2
	movsd	40(%rsp), %xmm3
	subsd	(%rsp), %xmm3
	movq	104(%rsp), %rax
	leaq	(%rax,%r13,8), %r15
	mulsd	%xmm3, %xmm2
	movaps	%xmm2, %xmm1
	cvtsi2sdl	%r12d, %xmm2
	divsd	%xmm2, %xmm1
	addsd	%xmm0, %xmm1
	movsd	.LC0(%rip), %xmm0
	call	pow@PLT
	movq	112(%rsp), %rax
	movsd	%xmm0, (%r15)
	mulsd	%xmm0, %xmm0
	movsd	%xmm0, (%rax,%r13,8)
	incq	%r13
	jmp	.L6
.L28:
	cvtsi2sdl	%ebx, %xmm0
	movsd	16(%rsp), %xmm1
	leaq	120(%rsp), %rdi
	movl	$1, %edx
	divsd	%xmm0, %xmm1
	movsd	32(%rsp), %xmm0
	divsd	%xmm1, %xmm0
	cvttsd2sil	%xmm0, %r13d
	leal	1(%r13), %r15d
	leal	(%rbx,%r15,2), %esi
	addl	%ebx, %r15d
	movslq	%esi, %rsi
	salq	$2, %rsi
	movq	%rsi, (%rsp)
	call	cudaMallocManaged@PLT
	movq	(%rsp), %rsi
	movl	$1, %edx
	leaq	128(%rsp), %rdi
	call	cudaMallocManaged@PLT
	movl	%r13d, %eax
	movq	120(%rsp), %rsi
	movslq	%r13d, %r13
	salq	$2, %r13
	notl	%eax
	addq	%r13, %rsi
	cltq
	addq	128(%rsp), %r13
.L8:
	movl	%eax, %edx
	cmpl	%r15d, %eax
	jg	.L29
	testl	%eax, %eax
	jns	.L9
	addl	%ebx, %edx
	orl	$-1, %ecx
	jmp	.L10
.L9:
	cmpl	%ebx, %edx
	jge	.L11
	movl	%eax, %edx
	xorl	%ecx, %ecx
	jmp	.L10
.L11:
	subl	%ebx, %edx
	movl	$1, %ecx
.L10:
	movl	%edx, 4(%rsi,%rax,4)
	movl	%ecx, 4(%r13,%rax,4)
	incq	%rax
	jmp	.L8
.L29:
	movl	$1, %edx
	leaq	136(%rsp), %rdi
	movq	%rbp, %rsi
	call	cudaMallocManaged@PLT
	movq	136(%rsp), %rdx
	xorl	%eax, %eax
.L13:
	cmpl	%eax, %r12d
	jle	.L30
	xorl	%ecx, %ecx
	movq	%rcx, (%rdx,%rax,8)
	incq	%rax
	jmp	.L13
.L30:
	leaq	64(%rsp), %rdi
	call	cudaGetDevice@PLT
	testl	%eax, %eax
	jne	.L16
	movl	64(%rsp), %edx
	leaq	68(%rsp), %rdi
	movl	$39, %esi
	call	cudaDeviceGetAttribute@PLT
	testl	%eax, %eax
	jne	.L16
	movl	64(%rsp), %edx
	leaq	72(%rsp), %rdi
	movl	$10, %esi
	call	cudaDeviceGetAttribute@PLT
	testl	%eax, %eax
	jne	.L16
	movl	64(%rsp), %edx
	leaq	76(%rsp), %rdi
	movl	$1, %esi
	call	cudaDeviceGetAttribute@PLT
	testl	%eax, %eax
	jne	.L16
	movl	64(%rsp), %edx
	leaq	144(%rsp), %rdi
	movl	$16, %esi
	call	cudaDeviceGetAttribute@PLT
	testl	%eax, %eax
	jne	.L16
	movq	_Z11doPairCountxPKdPKxS2_diddiS0_PyPKiS5_@GOTPCREL(%rip), %rsi
	leaq	168(%rsp), %rdi
	call	cudaFuncGetAttributes@PLT
	testl	%eax, %eax
	jne	.L16
	movl	68(%rsp), %eax
	movl	192(%rsp), %r13d
	movl	72(%rsp), %r15d
	movl	%eax, (%rsp)
	movl	76(%rsp), %eax
	cmpl	%eax, %r13d
	cmovg	%eax, %r13d
	xorl	%r9d, %r9d
	xorl	%r10d, %r10d
	xorl	%r14d, %r14d
	leal	-1(%r15,%r13), %ebp
	movl	%ebp, %eax
	cltd
	idivl	%r15d
	leaq	156(%rsp), %rax
	movq	%rax, 40(%rsp)
	subl	%edx, %ebp
.L18:
	testl	%ebp, %ebp
	jle	.L20
	cmpl	%ebp, %r13d
	movl	%ebp, %edx
	movq	40(%rsp), %rdi
	movl	%r10d, 60(%rsp)
	cmovle	%r13d, %edx
	movq	_Z11doPairCountxPKdPKxS2_diddiS0_PyPKiS5_@GOTPCREL(%rip), %rsi
	xorl	%r8d, %r8d
	xorl	%ecx, %ecx
	movl	%r9d, 56(%rsp)
	movl	%edx, 52(%rsp)
	call	cudaOccupancyMaxActiveBlocksPerMultiprocessorWithFlags@PLT
	testl	%eax, %eax
	jne	.L16
	movl	52(%rsp), %edx
	movl	156(%rsp), %eax
	movl	56(%rsp), %r9d
	movl	60(%rsp), %r10d
	movl	%edx, %ecx
	imull	%eax, %ecx
	cmpl	%r9d, %ecx
	jle	.L19
	movl	%ecx, %r9d
	movl	%eax, %r10d
	movl	%edx, %r14d
.L19:
	cmpl	%r9d, (%rsp)
	je	.L20
	subl	%r15d, %ebp
	jmp	.L18
.L20:
	imull	144(%rsp), %r10d
	movl	%r10d, 48(%rsp)
.L16:
	movl	48(%rsp), %ecx
	movl	%r14d, 156(%rsp)
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movabsq	$4294967297, %rax
	movq	%rax, 160(%rsp)
	movq	156(%rsp), %rdx
	movl	%ecx, 144(%rsp)
	movl	164(%rsp), %ecx
	movq	%rax, 148(%rsp)
	movq	144(%rsp), %rdi
	movl	152(%rsp), %esi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L22
	pushq	128(%rsp)
	.cfi_def_cfa_offset 392
	movl	%r12d, %r9d
	movl	%ebx, %r8d
	pushq	128(%rsp)
	.cfi_def_cfa_offset 400
	pushq	152(%rsp)
	.cfi_def_cfa_offset 408
	pushq	136(%rsp)
	.cfi_def_cfa_offset 416
	movsd	64(%rsp), %xmm2
	movsd	56(%rsp), %xmm1
	movsd	48(%rsp), %xmm0
	movq	128(%rsp), %rcx
	movq	120(%rsp), %rdx
	movq	112(%rsp), %rsi
	movq	40(%rsp), %rdi
	call	_Z11doPairCountxPKdPKxS2_diddiS0_PyPKiS5_@PLT
	addq	$32, %rsp
	.cfi_def_cfa_offset 384
.L22:
	call	cudaDeviceSynchronize@PLT
	movq	80(%rsp), %rdi
	call	cudaFree@PLT
	movq	88(%rsp), %rdi
	call	cudaFree@PLT
	movq	96(%rsp), %rdi
	call	cudaFree@PLT
	movq	104(%rsp), %rdi
	call	cudaFree@PLT
	movq	112(%rsp), %rdi
	call	cudaFree@PLT
	movq	120(%rsp), %rdi
	call	cudaFree@PLT
	movq	128(%rsp), %rdi
	call	cudaFree@PLT
	movq	136(%rsp), %rax
	movq	312(%rsp), %rdx
	subq	%fs:40, %rdx
	je	.L23
	call	__stack_chk_fail@PLT
.L23:
	addq	$328, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%rbp
	.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
	ret
	.cfi_endproc
.LFE3635:
	.size	autoPairCount, .-autoPairCount
	.section	.text.startup,"ax",@progbits
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB3661:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	leaq	_ZL15__fatDeviceText(%rip), %rdi
	call	__cudaRegisterFatBinary@PLT
	movq	%rax, %rdi
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	call	__cudaRegisterFatBinaryEnd@PLT
	leaq	_ZL26__cudaUnregisterBinaryUtilv(%rip), %rdi
	popq	%rdx
	.cfi_def_cfa_offset 8
	jmp	atexit@PLT
	.cfi_endproc
.LFE3661:
	.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	0
	.long	1076101120
	.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:

//
// Created by rafa on 2/5/21.
//

#ifndef CUPAC_CUPAC_CUH
#define CUPAC_CUPAC_CUH

#include "define.cuh"
#include "grid.cuh"
#include "counter.cuh"


extern "C" {
unsigned long long *
autoPairCount(long long npart, const double *positions, double boxsize, int nside, double minsep, double maxsep,
              int nbins);
}

#endif //CUPAC_CUPAC_CUH

	.text
	.file	"cupac.hip"
	.type	__hip_cuid_,@object             # @__hip_cuid_
	.bss
	.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_

	.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_
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.amdgpu_metadata
---
amdhsa.kernels:  []
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
