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

int maxThreadsPerBlock = 128;
cudaEvent_t beginEvent;
cudaEvent_t endEvent;

// global calls
void initCuda (int N) {
	cudaEventCreate(&beginEvent);
	cudaEventCreate(&endEvent);
}

__global__ void naive_scan (float* in_arr, float* scan_arr, int size, int depth) {
	int index = threadIdx.x + blockIdx.x * blockDim.x;
	
	int val = 0;

	int in_index = index;

	if (depth == 1) {
		in_index--;
	}

	if (in_index >= 0 && index < size) {
		int exp_2 = 1;
		for (int i = 1; i < depth; i++) {
			exp_2 *= 2;
		}
		val = in_arr[in_index];
		if (in_index >= exp_2) {
			val += in_arr[in_index - exp_2];
		}
	}

	if (index < size) {
		scan_arr[index] = val;
	}
}

__global__ void shared_scan (float* in_arr, float* scan_arr, int size, int depth) {
	__shared__ float in_arr_s1 [1];	//contains the lower numbers
	//__shared__ float in_arr_s2 [blockDim.x];	//contains the higher numbers

	int index = threadIdx.x + blockIdx.x * blockDim.x;

	int exp_2 = 1;
	for (int i = 1; i < depth; i++) {
		exp_2 *= 2;
	}

	float sValue = 0;

	if (index < size) {
		in_arr_s1[index] = in_arr[index];
	}
	__syncthreads();

	int in_index = index;
	if (depth == 1) {
		in_index--;
	}

	if (in_index >= 0 && index < size) {
		sValue += in_arr_s1[in_index];
		if (in_index >= exp_2) {
			sValue += in_arr_s1[in_index - exp_2];
		}
	}
	//in_arr_s2[index] = in_arr[index];
	if (index < size) {
		scan_arr[index] = sValue;
	}
	__syncthreads();
}

void cudaScan (float* in_arr, float* out_arr, int size) {
	int numBlocks = ceil(size/(float)maxThreadsPerBlock);
	int threadsPerBlock = min(size, maxThreadsPerBlock);
	
	float* arr1, * arr2;
	cudaMalloc((void**)&arr1, size*sizeof(float));
	cudaMalloc((void**)&arr2, size*sizeof(float));
	
	float time;
	int max_depth = ceil(log2((float)size));
	cudaMemcpy(arr1, in_arr, size*sizeof(float), cudaMemcpyHostToDevice);
	cudaEventRecord(beginEvent, 0);
	for (int i = 1; i <= max_depth; i++) {	// not sure why it's ceil(log2(size)) but it works.
		shared_scan<<<numBlocks, maxThreadsPerBlock>>>(arr1, arr2, size, i);
		//cudaThreadSynchronize();	// taking these out causes it to fail occasionally.
		float* temp = arr1;
		arr1 = arr2;
		arr2 = temp;
	}
	cudaEventRecord(endEvent, 0);
	cudaEventSynchronize(endEvent);

	cudaEventElapsedTime(&time, beginEvent, endEvent);
	std::cout << "cudaGPUTime for size " << size << " was " << time << "ms" << std::endl;
	

	cudaMemcpy(out_arr, arr1, size*sizeof(float), cudaMemcpyDeviceToHost);
}

__global__ void scatter (float* in_arr, float* temp_arr, float* scan_arr, float* out_arr, int size) {
	int index = threadIdx.x + blockIdx.x * blockDim.x;

	if (index < size && temp_arr[index] == 1) {
		out_arr[(int)scan_arr[index]] = in_arr[index];
	}
}

__global__ void compute (float* in_arr, float* out_arr, int size) {
	//compute this array based on some function
	int index = threadIdx.x + blockIdx.x * blockDim.x;

	out_arr[index] = index % 2;
}

void cudaStreamCompaction (float* in_arr, float* out_arr, int size) {
	int numBlocks = ceil(size/(float)maxThreadsPerBlock);
	int threadsPerBlock = min(size, maxThreadsPerBlock);
	float* temp_arr, *scan_arr;
	float* arr, *compact_arr;

	cudaMalloc((void**)&temp_arr, size*sizeof(int));
	cudaMalloc((void**)&scan_arr, size*sizeof(int));
	cudaMalloc((void**)&arr, size*sizeof(float));
	cudaMalloc((void**)&compact_arr, size*sizeof(float));

	cudaMemcpy(arr, in_arr, size*sizeof(float), cudaMemcpyHostToDevice);

	compute<<<numBlocks, threadsPerBlock>>>(arr, temp_arr, size);
	cudaScan(arr, scan_arr, size);
	scatter<<<numBlocks, threadsPerBlock>>>(arr, temp_arr, scan_arr, out_arr, size);
	
	cudaMemcpy(out_arr, compact_arr, size*sizeof(float), cudaMemcpyDeviceToHost);
}
	.file	"tmpxft_002542c6_00000000-6_compaction.cudafe1.cpp"
	.text
#APP
	.globl _ZSt21ios_base_library_initv

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB3640:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE3640:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z8initCudai
	.type	_Z8initCudai, @function
_Z8initCudai:
.LFB3635:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	leaq	beginEvent(%rip), %rdi
	call	cudaEventCreate@PLT
	leaq	endEvent(%rip), %rdi
	popq	%rdx
	.cfi_def_cfa_offset 8
	jmp	cudaEventCreate@PLT
	.cfi_endproc
.LFE3635:
	.size	_Z8initCudai, .-_Z8initCudai
	.globl	_Z34__device_stub__Z10naive_scanPfS_iiPfS_ii
	.type	_Z34__device_stub__Z10naive_scanPfS_iiPfS_ii, @function
_Z34__device_stub__Z10naive_scanPfS_iiPfS_ii:
.LFB3662:
	.cfi_startproc
	endbr64
	subq	$152, %rsp
	.cfi_def_cfa_offset 160
	movq	%rdi, 24(%rsp)
	leaq	56(%rsp), %rdi
	movq	%rsi, 16(%rsp)
	leaq	68(%rsp), %rsi
	movl	%edx, 12(%rsp)
	leaq	40(%rsp), %rdx
	movl	%ecx, 8(%rsp)
	leaq	48(%rsp), %rcx
	movq	%fs:40, %rax
	movq	%rax, 136(%rsp)
	xorl	%eax, %eax
	leaq	24(%rsp), %rax
	movl	$1, 64(%rsp)
	movq	%rax, 104(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 112(%rsp)
	leaq	12(%rsp), %rax
	movq	%rax, 120(%rsp)
	leaq	8(%rsp), %rax
	movq	%rax, 128(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 56(%rsp)
	movq	%rax, 68(%rsp)
	movl	$1, 76(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L4
	pushq	48(%rsp)
	.cfi_def_cfa_offset 168
	leaq	_Z10naive_scanPfS_ii(%rip), %rdi
	pushq	48(%rsp)
	.cfi_def_cfa_offset 176
	movq	84(%rsp), %rcx
	movl	92(%rsp), %r8d
	movq	72(%rsp), %rsi
	movl	80(%rsp), %edx
	leaq	120(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 168
	popq	%rdx
	.cfi_def_cfa_offset 160
.L4:
	movq	136(%rsp), %rax
	subq	%fs:40, %rax
	je	.L6
	call	__stack_chk_fail@PLT
.L6:
	addq	$152, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE3662:
	.size	_Z34__device_stub__Z10naive_scanPfS_iiPfS_ii, .-_Z34__device_stub__Z10naive_scanPfS_iiPfS_ii
	.globl	_Z10naive_scanPfS_ii
	.type	_Z10naive_scanPfS_ii, @function
_Z10naive_scanPfS_ii:
.LFB3663:
	.cfi_startproc
	endbr64
	jmp	_Z34__device_stub__Z10naive_scanPfS_iiPfS_ii
	.cfi_endproc
.LFE3663:
	.size	_Z10naive_scanPfS_ii, .-_Z10naive_scanPfS_ii
	.globl	_Z35__device_stub__Z11shared_scanPfS_iiPfS_ii
	.type	_Z35__device_stub__Z11shared_scanPfS_iiPfS_ii, @function
_Z35__device_stub__Z11shared_scanPfS_iiPfS_ii:
.LFB3664:
	.cfi_startproc
	endbr64
	subq	$152, %rsp
	.cfi_def_cfa_offset 160
	movq	%rdi, 24(%rsp)
	leaq	56(%rsp), %rdi
	movq	%rsi, 16(%rsp)
	leaq	68(%rsp), %rsi
	movl	%edx, 12(%rsp)
	leaq	40(%rsp), %rdx
	movl	%ecx, 8(%rsp)
	leaq	48(%rsp), %rcx
	movq	%fs:40, %rax
	movq	%rax, 136(%rsp)
	xorl	%eax, %eax
	leaq	24(%rsp), %rax
	movl	$1, 64(%rsp)
	movq	%rax, 104(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 112(%rsp)
	leaq	12(%rsp), %rax
	movq	%rax, 120(%rsp)
	leaq	8(%rsp), %rax
	movq	%rax, 128(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 56(%rsp)
	movq	%rax, 68(%rsp)
	movl	$1, 76(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L10
	pushq	48(%rsp)
	.cfi_def_cfa_offset 168
	leaq	_Z11shared_scanPfS_ii(%rip), %rdi
	pushq	48(%rsp)
	.cfi_def_cfa_offset 176
	movq	84(%rsp), %rcx
	movl	92(%rsp), %r8d
	movq	72(%rsp), %rsi
	movl	80(%rsp), %edx
	leaq	120(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 168
	popq	%rdx
	.cfi_def_cfa_offset 160
.L10:
	movq	136(%rsp), %rax
	subq	%fs:40, %rax
	je	.L12
	call	__stack_chk_fail@PLT
.L12:
	addq	$152, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE3664:
	.size	_Z35__device_stub__Z11shared_scanPfS_iiPfS_ii, .-_Z35__device_stub__Z11shared_scanPfS_iiPfS_ii
	.globl	_Z11shared_scanPfS_ii
	.type	_Z11shared_scanPfS_ii, @function
_Z11shared_scanPfS_ii:
.LFB3665:
	.cfi_startproc
	endbr64
	jmp	_Z35__device_stub__Z11shared_scanPfS_iiPfS_ii
	.cfi_endproc
.LFE3665:
	.size	_Z11shared_scanPfS_ii, .-_Z11shared_scanPfS_ii
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"cudaGPUTime for size "
.LC1:
	.string	" was "
.LC2:
	.string	"ms"
	.text
	.globl	_Z8cudaScanPfS_i
	.type	_Z8cudaScanPfS_i, @function
_Z8cudaScanPfS_i:
.LFB3636:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	cvtsi2ssl	%edx, %xmm2
	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
	movq	%rsi, %r12
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	movaps	%xmm2, %xmm0
	movd	%xmm2, %r13d
	movq	%rdi, %rbp
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	movl	%edx, %ebx
	subq	$72, %rsp
	.cfi_def_cfa_offset 128
	cvtsi2ssl	maxThreadsPerBlock(%rip), %xmm1
	movq	%fs:40, %rax
	movq	%rax, 56(%rsp)
	xorl	%eax, %eax
	divss	%xmm1, %xmm0
	call	ceilf@PLT
	movslq	%ebx, %rax
	leaq	16(%rsp), %rdi
	salq	$2, %rax
	cvttss2sil	%xmm0, %r15d
	movq	%rax, %rsi
	movq	%rax, 8(%rsp)
	call	cudaMalloc@PLT
	movq	8(%rsp), %rsi
	leaq	24(%rsp), %rdi
	call	cudaMalloc@PLT
	movd	%r13d, %xmm0
	movabsq	$4294967297, %r13
	call	log2f@PLT
	call	ceilf@PLT
	movq	8(%rsp), %rdx
	movq	16(%rsp), %rdi
	movq	%rbp, %rsi
	movl	$1, %ecx
	cvttss2sil	%xmm0, %r14d
	movl	$1, %ebp
	call	cudaMemcpy@PLT
	movq	beginEvent(%rip), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
.L16:
	cmpl	%r14d, %ebp
	jg	.L21
	movl	maxThreadsPerBlock(%rip), %eax
	movq	%r13, 48(%rsp)
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movq	%r13, 36(%rsp)
	movl	52(%rsp), %ecx
	movl	%eax, 44(%rsp)
	movl	40(%rsp), %esi
	movl	%r15d, 32(%rsp)
	movq	44(%rsp), %rdx
	movq	32(%rsp), %rdi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L17
	movq	24(%rsp), %rsi
	movq	16(%rsp), %rdi
	movl	%ebp, %ecx
	movl	%ebx, %edx
	call	_Z35__device_stub__Z11shared_scanPfS_iiPfS_ii
.L17:
	movq	16(%rsp), %rax
	movq	24(%rsp), %rdx
	incl	%ebp
	movq	%rdx, 16(%rsp)
	movq	%rax, 24(%rsp)
	jmp	.L16
.L21:
	movq	endEvent(%rip), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movq	endEvent(%rip), %rdi
	call	cudaEventSynchronize@PLT
	movq	endEvent(%rip), %rdx
	leaq	44(%rsp), %rdi
	movq	beginEvent(%rip), %rsi
	call	cudaEventElapsedTime@PLT
	leaq	.LC0(%rip), %rsi
	leaq	_ZSt4cout(%rip), %rdi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movl	%ebx, %esi
	movq	%rax, %rdi
	call	_ZNSolsEi@PLT
	leaq	.LC1(%rip), %rsi
	movq	%rax, %rdi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	cvtss2sd	44(%rsp), %xmm0
	movq	%rax, %rdi
	call	_ZNSo9_M_insertIdEERSoT_@PLT
	leaq	.LC2(%rip), %rsi
	movq	%rax, %rdi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movq	%rax, %rdi
	call	_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@PLT
	movq	8(%rsp), %rdx
	movq	16(%rsp), %rsi
	movq	%r12, %rdi
	movl	$2, %ecx
	call	cudaMemcpy@PLT
	movq	56(%rsp), %rax
	subq	%fs:40, %rax
	je	.L19
	call	__stack_chk_fail@PLT
.L19:
	addq	$72, %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
.LFE3636:
	.size	_Z8cudaScanPfS_i, .-_Z8cudaScanPfS_i
	.globl	_Z33__device_stub__Z7scatterPfS_S_S_iPfS_S_S_i
	.type	_Z33__device_stub__Z7scatterPfS_S_S_iPfS_S_S_i, @function
_Z33__device_stub__Z7scatterPfS_S_S_iPfS_S_S_i:
.LFB3666:
	.cfi_startproc
	endbr64
	subq	$168, %rsp
	.cfi_def_cfa_offset 176
	movq	%rdi, 40(%rsp)
	leaq	64(%rsp), %rdi
	movq	%rsi, 32(%rsp)
	leaq	76(%rsp), %rsi
	movq	%rdx, 24(%rsp)
	leaq	48(%rsp), %rdx
	movq	%rcx, 16(%rsp)
	leaq	56(%rsp), %rcx
	movl	%r8d, 12(%rsp)
	movq	%fs:40, %rax
	movq	%rax, 152(%rsp)
	xorl	%eax, %eax
	leaq	40(%rsp), %rax
	movl	$1, 72(%rsp)
	movq	%rax, 112(%rsp)
	leaq	32(%rsp), %rax
	movq	%rax, 120(%rsp)
	leaq	24(%rsp), %rax
	movq	%rax, 128(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 136(%rsp)
	leaq	12(%rsp), %rax
	movq	%rax, 144(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 64(%rsp)
	movq	%rax, 76(%rsp)
	movl	$1, 84(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L22
	pushq	56(%rsp)
	.cfi_def_cfa_offset 184
	leaq	_Z7scatterPfS_S_S_i(%rip), %rdi
	pushq	56(%rsp)
	.cfi_def_cfa_offset 192
	movq	92(%rsp), %rcx
	movl	100(%rsp), %r8d
	movq	80(%rsp), %rsi
	movl	88(%rsp), %edx
	leaq	128(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 184
	popq	%rdx
	.cfi_def_cfa_offset 176
.L22:
	movq	152(%rsp), %rax
	subq	%fs:40, %rax
	je	.L24
	call	__stack_chk_fail@PLT
.L24:
	addq	$168, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE3666:
	.size	_Z33__device_stub__Z7scatterPfS_S_S_iPfS_S_S_i, .-_Z33__device_stub__Z7scatterPfS_S_S_iPfS_S_S_i
	.globl	_Z7scatterPfS_S_S_i
	.type	_Z7scatterPfS_S_S_i, @function
_Z7scatterPfS_S_S_i:
.LFB3667:
	.cfi_startproc
	endbr64
	jmp	_Z33__device_stub__Z7scatterPfS_S_S_iPfS_S_S_i
	.cfi_endproc
.LFE3667:
	.size	_Z7scatterPfS_S_S_i, .-_Z7scatterPfS_S_S_i
	.globl	_Z29__device_stub__Z7computePfS_iPfS_i
	.type	_Z29__device_stub__Z7computePfS_iPfS_i, @function
_Z29__device_stub__Z7computePfS_iPfS_i:
.LFB3668:
	.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	.L27
	pushq	40(%rsp)
	.cfi_def_cfa_offset 152
	leaq	_Z7computePfS_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
.L27:
	movq	120(%rsp), %rax
	subq	%fs:40, %rax
	je	.L29
	call	__stack_chk_fail@PLT
.L29:
	addq	$136, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE3668:
	.size	_Z29__device_stub__Z7computePfS_iPfS_i, .-_Z29__device_stub__Z7computePfS_iPfS_i
	.globl	_Z7computePfS_i
	.type	_Z7computePfS_i, @function
_Z7computePfS_i:
.LFB3669:
	.cfi_startproc
	endbr64
	jmp	_Z29__device_stub__Z7computePfS_iPfS_i
	.cfi_endproc
.LFE3669:
	.size	_Z7computePfS_i, .-_Z7computePfS_i
	.globl	_Z20cudaStreamCompactionPfS_i
	.type	_Z20cudaStreamCompactionPfS_i, @function
_Z20cudaStreamCompactionPfS_i:
.LFB3637:
	.cfi_startproc
	endbr64
	pushq	%r14
	.cfi_def_cfa_offset 16
	.cfi_offset 14, -16
	pushq	%r13
	.cfi_def_cfa_offset 24
	.cfi_offset 13, -24
	movq	%rdi, %r13
	pushq	%r12
	.cfi_def_cfa_offset 32
	.cfi_offset 12, -32
	movq	%rsi, %r12
	pushq	%rbp
	.cfi_def_cfa_offset 40
	.cfi_offset 6, -40
	movslq	%edx, %rbp
	pushq	%rbx
	.cfi_def_cfa_offset 48
	.cfi_offset 3, -48
	cvtsi2ssl	%ebp, %xmm0
	movq	%rbp, %rbx
	subq	$80, %rsp
	.cfi_def_cfa_offset 128
	movl	maxThreadsPerBlock(%rip), %r14d
	movq	%fs:40, %rax
	movq	%rax, 72(%rsp)
	xorl	%eax, %eax
	cvtsi2ssl	%r14d, %xmm1
	divss	%xmm1, %xmm0
	call	ceilf@PLT
	cmpl	%ebp, %r14d
	leaq	16(%rsp), %rdi
	cmovg	%ebp, %r14d
	salq	$2, %rbp
	movss	%xmm0, 12(%rsp)
	movq	%rbp, %rsi
	call	cudaMalloc@PLT
	leaq	24(%rsp), %rdi
	movq	%rbp, %rsi
	call	cudaMalloc@PLT
	leaq	32(%rsp), %rdi
	movq	%rbp, %rsi
	call	cudaMalloc@PLT
	leaq	40(%rsp), %rdi
	movq	%rbp, %rsi
	call	cudaMalloc@PLT
	movq	32(%rsp), %rdi
	movq	%r13, %rsi
	movq	%rbp, %rdx
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	movl	%r14d, 60(%rsp)
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movabsq	$4294967297, %rax
	movss	12(%rsp), %xmm0
	movq	%rax, 64(%rsp)
	movl	68(%rsp), %ecx
	cvttss2sil	%xmm0, %r13d
	movq	%rax, 52(%rsp)
	movq	60(%rsp), %rdx
	movl	56(%rsp), %esi
	movl	%r13d, 48(%rsp)
	movq	48(%rsp), %rdi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L33
	movq	16(%rsp), %rsi
	movq	32(%rsp), %rdi
	movl	%ebx, %edx
	call	_Z29__device_stub__Z7computePfS_iPfS_i
.L33:
	movq	24(%rsp), %rsi
	movq	32(%rsp), %rdi
	movl	%ebx, %edx
	call	_Z8cudaScanPfS_i
	movl	%r14d, 60(%rsp)
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movabsq	$4294967297, %rax
	movl	%r13d, 48(%rsp)
	movq	%rax, 64(%rsp)
	movl	68(%rsp), %ecx
	movq	%rax, 52(%rsp)
	movq	60(%rsp), %rdx
	movq	48(%rsp), %rdi
	movl	56(%rsp), %esi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L34
	movq	24(%rsp), %rdx
	movq	16(%rsp), %rsi
	movl	%ebx, %r8d
	movq	%r12, %rcx
	movq	32(%rsp), %rdi
	call	_Z33__device_stub__Z7scatterPfS_S_S_iPfS_S_S_i
.L34:
	movq	40(%rsp), %rsi
	movl	$2, %ecx
	movq	%rbp, %rdx
	movq	%r12, %rdi
	call	cudaMemcpy@PLT
	movq	72(%rsp), %rax
	subq	%fs:40, %rax
	je	.L35
	call	__stack_chk_fail@PLT
.L35:
	addq	$80, %rsp
	.cfi_def_cfa_offset 48
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%rbp
	.cfi_def_cfa_offset 32
	popq	%r12
	.cfi_def_cfa_offset 24
	popq	%r13
	.cfi_def_cfa_offset 16
	popq	%r14
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE3637:
	.size	_Z20cudaStreamCompactionPfS_i, .-_Z20cudaStreamCompactionPfS_i
	.section	.rodata.str1.1
.LC3:
	.string	"_Z7computePfS_i"
.LC4:
	.string	"_Z7scatterPfS_S_S_i"
.LC5:
	.string	"_Z11shared_scanPfS_ii"
.LC6:
	.string	"_Z10naive_scanPfS_ii"
	.section	.text.startup,"ax",@progbits
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB3671:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -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	.LC3(%rip), %rdx
	movq	%rax, %rdi
	movq	%rax, %rbx
	pushq	$0
	.cfi_def_cfa_offset 40
	movq	%rdx, %rcx
	leaq	_Z7computePfS_i(%rip), %rsi
	pushq	$0
	.cfi_def_cfa_offset 48
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	call	__cudaRegisterFunction@PLT
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	movq	%rbx, %rdi
	xorl	%r9d, %r9d
	pushq	$0
	.cfi_def_cfa_offset 24
	leaq	.LC4(%rip), %rdx
	orl	$-1, %r8d
	leaq	_Z7scatterPfS_S_S_i(%rip), %rsi
	pushq	$0
	.cfi_def_cfa_offset 32
	movq	%rdx, %rcx
	pushq	$0
	.cfi_def_cfa_offset 40
	pushq	$0
	.cfi_def_cfa_offset 48
	call	__cudaRegisterFunction@PLT
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	movq	%rbx, %rdi
	xorl	%r9d, %r9d
	pushq	$0
	.cfi_def_cfa_offset 24
	leaq	.LC5(%rip), %rdx
	orl	$-1, %r8d
	leaq	_Z11shared_scanPfS_ii(%rip), %rsi
	pushq	$0
	.cfi_def_cfa_offset 32
	movq	%rdx, %rcx
	pushq	$0
	.cfi_def_cfa_offset 40
	pushq	$0
	.cfi_def_cfa_offset 48
	call	__cudaRegisterFunction@PLT
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	movq	%rbx, %rdi
	xorl	%r9d, %r9d
	pushq	$0
	.cfi_def_cfa_offset 24
	leaq	.LC6(%rip), %rdx
	orl	$-1, %r8d
	leaq	_Z10naive_scanPfS_ii(%rip), %rsi
	pushq	$0
	.cfi_def_cfa_offset 32
	movq	%rdx, %rcx
	pushq	$0
	.cfi_def_cfa_offset 40
	pushq	$0
	.cfi_def_cfa_offset 48
	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	%rbx
	.cfi_def_cfa_offset 8
	jmp	atexit@PLT
	.cfi_endproc
.LFE3671:
	.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
	.globl	endEvent
	.bss
	.align 8
	.type	endEvent, @object
	.size	endEvent, 8
endEvent:
	.zero	8
	.globl	beginEvent
	.align 8
	.type	beginEvent, @object
	.size	beginEvent, 8
beginEvent:
	.zero	8
	.globl	maxThreadsPerBlock
	.data
	.align 4
	.type	maxThreadsPerBlock, @object
	.size	maxThreadsPerBlock, 4
maxThreadsPerBlock:
	.long	128
	.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:

#ifndef COMPACTION_H
#define COMPATION_H

void initCuda (int N);
/**
 * Calls an internal function to perform an exclusive prefix sum on the array
**/
void cudaScan (float* in_arr, float* out_arr, int size);
/**
 * Calls an internal function to perform a scatter on the array
**/
void cudaScatter (float* in_arr, float* out_arr, int size);

float* prefixSum (float* arr, int size);

#endif
	.text
	.file	"compaction.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
