/**
 * Assignment 06 Program - moving_average.cu
 * Sarah Helble
 * 10/06/17
 *
 * Calculates the average of each index and its neighbors
 *
 * Usage ./aout
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

// Execution Notes
// 512, 512 gives about equivalent times
// 512, 256 register is 2x faster
// 256, 256 register is slightly better
// 256, 128 register is slightly better
// 512, 128 register is slightly better
// 1024, 256 shared is slightly better
// 1024, 512 register is slightly better
//
// First run always seems to be bad (2x slower)

#define NUM_ELEMENTS 512
#define THREADS_PER_BLOCK 256

#define MAX_INT 30

/**
 * Returns the current time
 */
__host__ cudaEvent_t get_time(void)
{
	cudaEvent_t time;
	cudaEventCreate(&time);
	cudaEventRecord(time);
	return time;
}

/**
 * Kernel function that takes a moving average of the values in
 * @list and puts the results in @averages
 * Uses registers to store the calculations.
 */
__global__ void average_using_registers(unsigned int *list, float *averages)
{
  /* Calculate the current index */
  const unsigned int idx = (blockIdx.x * blockDim.x) + threadIdx.x;

	if(idx < NUM_ELEMENTS) {
		unsigned int sum = list[idx];
		unsigned int num = 1;

		// If there is a previous element, add it to sum
		if(idx > 0) {
			sum = sum + list[idx - 1];
			num = num + 1;
		}

		// If there is a next element, add it to sum
		if((idx + 1) < NUM_ELEMENTS) {
			sum = sum + list[idx + 1];
			num = num + 1;
		}

		averages[idx] = (float) sum / num;
	}
}

/**
 * Kernel function that takes a moving average of the values in
 * @list and puts the results in @averages
 * Uses shared memory to store the calculations.
 */
__global__ void average_using_shared(unsigned int *list, float *averages)
{
	__shared__ unsigned int sums[NUM_ELEMENTS];
	__shared__ unsigned int nums[NUM_ELEMENTS];

	const unsigned int idx = (blockIdx.x * blockDim.x) + threadIdx.x;

	if(idx < NUM_ELEMENTS) {
		sums[idx] = list[idx];
		nums[idx] = 1;

		// If there is a previous element, add it to sum
		if(idx > 0) {
			sums[idx] = sums[idx] + list[idx - 1];
			nums[idx] = nums[idx] + 1;
		}

		// If there is a next element, add it to sum
		if((idx + 1) < NUM_ELEMENTS) {
			sums[idx] = sums[idx] + list[idx + 1];
			nums[idx] = nums[idx] + 1;
		}

		// Calculate the average
		averages[idx] = (float) sums[idx] / nums[idx];
	}
}

/**
 * Fuction to handle the printing of results.
 * @list is the original array
 * @averages is the result
 */
void print_results(unsigned int *list, float *averages)
{
  int i = 0;

  printf("\n");
  for(i = 0; i < NUM_ELEMENTS; i++) {
    printf("Original value at index [%d]: %d, average: %f\n", i, list[i], averages[i]);
  }
  printf("\n");
}

/**
 * Function that sets up everything for the kernel function
 *
 * @array_size size of array (total number of threads)
 * @threads_per_block number of threads to put in each block
 * @use_registers is 1 if registers should be used. Otherwise, will call
 * kernel that uses shared memory
 */
void exec_kernel(bool use_registers)
{
  /* Calculate the size of the array */
  int array_size_in_bytes = (sizeof(unsigned int) * (NUM_ELEMENTS));
  int float_array_size_in_bytes = (sizeof(float) * (NUM_ELEMENTS));
  int i = 0;

  unsigned int *list;
  float *averages;

  //pin it
  cudaMallocHost((void **)&list, array_size_in_bytes);
  cudaMallocHost((void **)&averages, float_array_size_in_bytes);

	// Fill array with random numbers between 0 and MAX_INT
  for(i = 0; i < NUM_ELEMENTS; i++) {
  	list[i] = (unsigned int) rand() % MAX_INT;
  }

  /* Declare and allocate pointers for GPU based parameters */
  unsigned int *d_list;
  float *d_averages;

  cudaMalloc((void **)&d_list, array_size_in_bytes);
  cudaMalloc((void **)&d_averages, float_array_size_in_bytes);

  /* Copy the CPU memory to the GPU memory */
  cudaMemcpy(d_list, list, array_size_in_bytes, cudaMemcpyHostToDevice);

  /* Designate the number of blocks and threads */
  const unsigned int num_blocks = NUM_ELEMENTS/THREADS_PER_BLOCK;
  const unsigned int num_threads = NUM_ELEMENTS/num_blocks;

  /* Execute the kernel and keep track of start and end time for duration */
  float duration = 0;

  cudaEvent_t start_time = get_time();

	if(use_registers) {
		average_using_registers<<<num_blocks, num_threads>>>(d_list, d_averages);
	} else {
		average_using_shared<<<num_blocks, num_threads>>>(d_list, d_averages);
	}

  cudaEvent_t end_time = get_time();
  cudaEventSynchronize(end_time);

  cudaEventElapsedTime(&duration, start_time, end_time);

  /* Copy the changed GPU memory back to the CPU */
  cudaMemcpy( averages, d_averages, float_array_size_in_bytes, cudaMemcpyDeviceToHost);

  printf("\tDuration: %fmsn\n", duration);
  print_results(list, averages);

  /* Free the GPU memory */
  cudaFree(d_list);
  cudaFree(d_averages);

  /* Free the pinned CPU memory */
  cudaFreeHost(list);
  cudaFreeHost(averages);
}

/**
 * Entry point for execution. Checks command line arguments
 * then passes execution to subordinate function
 */
int main(int argc, char *argv[])
{

  printf("\n");

	/* Do the average with shared memory */
	printf("First Run of Averages Calculated using Shared Memory");
  exec_kernel(false);
	printf("-----------------------------------------------------------------\n");

	printf("Second Run of Averages Calculated using Shared Memory");
  exec_kernel(false);
	printf("-----------------------------------------------------------------\n");

  /* Do the average with registers*/
	printf("First Run of Averages Calculated using Register Memory");
  exec_kernel(true);
  printf("-----------------------------------------------------------------\n");

	printf("Second Run of Averages Calculated using Register Memory");
  exec_kernel(true);
  printf("-----------------------------------------------------------------\n");



  return EXIT_SUCCESS;
}

	.file	"tmpxft_00390ead_00000000-6_moving_average.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	_Z8get_timev
	.type	_Z8get_timev, @function
_Z8get_timev:
.LFB2027:
	.cfi_startproc
	endbr64
	subq	$24, %rsp
	.cfi_def_cfa_offset 32
	movq	%fs:40, %rax
	movq	%rax, 8(%rsp)
	xorl	%eax, %eax
	movq	%rsp, %rdi
	call	cudaEventCreate@PLT
	movq	(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movq	(%rsp), %rax
	movq	8(%rsp), %rdx
	subq	%fs:40, %rdx
	je	.L3
	call	__stack_chk_fail@PLT
.L3:
	addq	$24, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2027:
	.size	_Z8get_timev, .-_Z8get_timev
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"\n"
.LC1:
	.string	"Original value at index [%d]: %d, average: %f\n"
	.text
	.globl	_Z13print_resultsPjPf
	.type	_Z13print_resultsPjPf, @function
_Z13print_resultsPjPf:
.LFB2028:
	.cfi_startproc
	endbr64
	pushq	%r14
	.cfi_def_cfa_offset 16
	.cfi_offset 14, -16
	xorl	%eax, %eax
	leaq	.LC1(%rip), %r14
	pushq	%r13
	.cfi_def_cfa_offset 24
	.cfi_offset 13, -24
	leaq	.LC0(%rip), %r13
	pushq	%r12
	.cfi_def_cfa_offset 32
	.cfi_offset 12, -32
	movq	%rsi, %r12
	movq	%r13, %rsi
	pushq	%rbp
	.cfi_def_cfa_offset 40
	.cfi_offset 6, -40
	movq	%rdi, %rbp
	movl	$2, %edi
	pushq	%rbx
	.cfi_def_cfa_offset 48
	.cfi_offset 3, -48
	xorl	%ebx, %ebx
	call	__printf_chk@PLT
.L7:
	movl	0(%rbp,%rbx,4), %ecx
	movl	%ebx, %edx
	cvtss2sd	(%r12,%rbx,4), %xmm0
	movq	%r14, %rsi
	movl	$2, %edi
	movb	$1, %al
	incq	%rbx
	call	__printf_chk@PLT
	cmpq	$512, %rbx
	jne	.L7
	popq	%rbx
	.cfi_def_cfa_offset 40
	movq	%r13, %rsi
	popq	%rbp
	.cfi_def_cfa_offset 32
	movl	$2, %edi
	popq	%r12
	.cfi_def_cfa_offset 24
	xorl	%eax, %eax
	popq	%r13
	.cfi_def_cfa_offset 16
	popq	%r14
	.cfi_def_cfa_offset 8
	jmp	__printf_chk@PLT
	.cfi_endproc
.LFE2028:
	.size	_Z13print_resultsPjPf, .-_Z13print_resultsPjPf
	.globl	_Z45__device_stub__Z23average_using_registersPjPfPjPf
	.type	_Z45__device_stub__Z23average_using_registersPjPfPjPf, @function
_Z45__device_stub__Z23average_using_registersPjPfPjPf:
.LFB2055:
	.cfi_startproc
	endbr64
	subq	$120, %rsp
	.cfi_def_cfa_offset 128
	movq	%rdi, 8(%rsp)
	leaq	32(%rsp), %rcx
	leaq	24(%rsp), %rdx
	movq	%rsi, (%rsp)
	leaq	40(%rsp), %rdi
	leaq	52(%rsp), %rsi
	movq	%fs:40, %rax
	movq	%rax, 104(%rsp)
	xorl	%eax, %eax
	leaq	8(%rsp), %rax
	movl	$1, 48(%rsp)
	movq	%rax, 88(%rsp)
	movq	%rsp, %rax
	movq	%rax, 96(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 40(%rsp)
	movq	%rax, 52(%rsp)
	movl	$1, 60(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L10
	pushq	32(%rsp)
	.cfi_def_cfa_offset 136
	leaq	_Z23average_using_registersPjPf(%rip), %rdi
	pushq	32(%rsp)
	.cfi_def_cfa_offset 144
	movq	68(%rsp), %rcx
	movl	76(%rsp), %r8d
	movq	56(%rsp), %rsi
	movl	64(%rsp), %edx
	leaq	104(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 136
	popq	%rdx
	.cfi_def_cfa_offset 128
.L10:
	movq	104(%rsp), %rax
	subq	%fs:40, %rax
	je	.L12
	call	__stack_chk_fail@PLT
.L12:
	addq	$120, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2055:
	.size	_Z45__device_stub__Z23average_using_registersPjPfPjPf, .-_Z45__device_stub__Z23average_using_registersPjPfPjPf
	.globl	_Z23average_using_registersPjPf
	.type	_Z23average_using_registersPjPf, @function
_Z23average_using_registersPjPf:
.LFB2056:
	.cfi_startproc
	endbr64
	jmp	_Z45__device_stub__Z23average_using_registersPjPfPjPf
	.cfi_endproc
.LFE2056:
	.size	_Z23average_using_registersPjPf, .-_Z23average_using_registersPjPf
	.globl	_Z42__device_stub__Z20average_using_sharedPjPfPjPf
	.type	_Z42__device_stub__Z20average_using_sharedPjPfPjPf, @function
_Z42__device_stub__Z20average_using_sharedPjPfPjPf:
.LFB2057:
	.cfi_startproc
	endbr64
	subq	$120, %rsp
	.cfi_def_cfa_offset 128
	movq	%rdi, 8(%rsp)
	leaq	32(%rsp), %rcx
	leaq	24(%rsp), %rdx
	movq	%rsi, (%rsp)
	leaq	40(%rsp), %rdi
	leaq	52(%rsp), %rsi
	movq	%fs:40, %rax
	movq	%rax, 104(%rsp)
	xorl	%eax, %eax
	leaq	8(%rsp), %rax
	movl	$1, 48(%rsp)
	movq	%rax, 88(%rsp)
	movq	%rsp, %rax
	movq	%rax, 96(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 40(%rsp)
	movq	%rax, 52(%rsp)
	movl	$1, 60(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L15
	pushq	32(%rsp)
	.cfi_def_cfa_offset 136
	leaq	_Z20average_using_sharedPjPf(%rip), %rdi
	pushq	32(%rsp)
	.cfi_def_cfa_offset 144
	movq	68(%rsp), %rcx
	movl	76(%rsp), %r8d
	movq	56(%rsp), %rsi
	movl	64(%rsp), %edx
	leaq	104(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 136
	popq	%rdx
	.cfi_def_cfa_offset 128
.L15:
	movq	104(%rsp), %rax
	subq	%fs:40, %rax
	je	.L17
	call	__stack_chk_fail@PLT
.L17:
	addq	$120, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2057:
	.size	_Z42__device_stub__Z20average_using_sharedPjPfPjPf, .-_Z42__device_stub__Z20average_using_sharedPjPfPjPf
	.globl	_Z20average_using_sharedPjPf
	.type	_Z20average_using_sharedPjPf, @function
_Z20average_using_sharedPjPf:
.LFB2058:
	.cfi_startproc
	endbr64
	jmp	_Z42__device_stub__Z20average_using_sharedPjPfPjPf
	.cfi_endproc
.LFE2058:
	.size	_Z20average_using_sharedPjPf, .-_Z20average_using_sharedPjPf
	.section	.rodata.str1.1
.LC3:
	.string	"\tDuration: %fmsn\n"
	.text
	.globl	_Z11exec_kernelb
	.type	_Z11exec_kernelb, @function
_Z11exec_kernelb:
.LFB2029:
	.cfi_startproc
	endbr64
	pushq	%r12
	.cfi_def_cfa_offset 16
	.cfi_offset 12, -16
	movl	$2048, %esi
	movl	%edi, %r12d
	pushq	%rbp
	.cfi_def_cfa_offset 24
	.cfi_offset 6, -24
	movl	$30, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset 3, -32
	xorl	%ebx, %ebx
	subq	$80, %rsp
	.cfi_def_cfa_offset 112
	movq	%fs:40, %rax
	movq	%rax, 72(%rsp)
	xorl	%eax, %eax
	leaq	16(%rsp), %rdi
	call	cudaMallocHost@PLT
	leaq	24(%rsp), %rdi
	movl	$2048, %esi
	call	cudaMallocHost@PLT
.L21:
	call	rand@PLT
	xorl	%edx, %edx
	divl	%ebp
	movq	16(%rsp), %rax
	movl	%edx, (%rax,%rbx)
	addq	$4, %rbx
	cmpq	$2048, %rbx
	jne	.L21
	leaq	32(%rsp), %rdi
	movl	$2048, %esi
	call	cudaMalloc@PLT
	leaq	40(%rsp), %rdi
	movl	$2048, %esi
	call	cudaMalloc@PLT
	movq	16(%rsp), %rsi
	movq	32(%rsp), %rdi
	movl	$1, %ecx
	movl	$2048, %edx
	call	cudaMemcpy@PLT
	movl	$0x00000000, 12(%rsp)
	call	_Z8get_timev
	testb	%r12b, %r12b
	movl	$1, %ecx
	movabsq	$4294967552, %rdx
	movq	%rdx, 60(%rsp)
	movq	%rax, %rbp
	movabsq	$4294967298, %rdi
	movl	$0, %r9d
	movq	%rdi, 48(%rsp)
	movl	$0, %r8d
	movl	$1, %esi
	movl	$1, 68(%rsp)
	movl	$1, 56(%rsp)
	je	.L22
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L24
	movq	40(%rsp), %rsi
	movq	32(%rsp), %rdi
	call	_Z45__device_stub__Z23average_using_registersPjPfPjPf
	jmp	.L24
.L22:
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L24
	movq	40(%rsp), %rsi
	movq	32(%rsp), %rdi
	call	_Z42__device_stub__Z20average_using_sharedPjPfPjPf
.L24:
	call	_Z8get_timev
	movq	%rax, %rbx
	movq	%rax, %rdi
	call	cudaEventSynchronize@PLT
	leaq	12(%rsp), %rdi
	movq	%rbx, %rdx
	movq	%rbp, %rsi
	call	cudaEventElapsedTime@PLT
	movq	40(%rsp), %rsi
	movq	24(%rsp), %rdi
	movl	$2, %ecx
	movl	$2048, %edx
	call	cudaMemcpy@PLT
	leaq	.LC3(%rip), %rsi
	movl	$2, %edi
	movb	$1, %al
	cvtss2sd	12(%rsp), %xmm0
	call	__printf_chk@PLT
	movq	24(%rsp), %rsi
	movq	16(%rsp), %rdi
	call	_Z13print_resultsPjPf
	movq	32(%rsp), %rdi
	call	cudaFree@PLT
	movq	40(%rsp), %rdi
	call	cudaFree@PLT
	movq	16(%rsp), %rdi
	call	cudaFreeHost@PLT
	movq	24(%rsp), %rdi
	call	cudaFreeHost@PLT
	movq	72(%rsp), %rax
	subq	%fs:40, %rax
	je	.L26
	call	__stack_chk_fail@PLT
.L26:
	addq	$80, %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
.LFE2029:
	.size	_Z11exec_kernelb, .-_Z11exec_kernelb
	.section	.rodata.str1.1
.LC4:
	.string	"First Run of Averages Calculated using Shared Memory"
.LC5:
	.string	"-----------------------------------------------------------------\n"
.LC6:
	.string	"Second Run of Averages Calculated using Shared Memory"
.LC7:
	.string	"First Run of Averages Calculated using Register Memory"
.LC8:
	.string	"Second Run of Averages Calculated using Register Memory"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2030:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	leaq	.LC0(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	leaq	.LC5(%rip), %rbx
	call	__printf_chk@PLT
	leaq	.LC4(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	xorl	%edi, %edi
	call	_Z11exec_kernelb
	movq	%rbx, %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	leaq	.LC6(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	xorl	%edi, %edi
	call	_Z11exec_kernelb
	movq	%rbx, %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	leaq	.LC7(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	$1, %edi
	call	_Z11exec_kernelb
	movq	%rbx, %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	leaq	.LC8(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	$1, %edi
	call	_Z11exec_kernelb
	movq	%rbx, %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2030:
	.size	main, .-main
	.section	.rodata.str1.1
.LC9:
	.string	"_Z20average_using_sharedPjPf"
.LC10:
	.string	"_Z23average_using_registersPjPf"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2060:
	.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	.LC9(%rip), %rdx
	movq	%rax, %rdi
	movq	%rax, %rbx
	pushq	$0
	.cfi_def_cfa_offset 40
	movq	%rdx, %rcx
	leaq	_Z20average_using_sharedPjPf(%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	.LC10(%rip), %rdx
	orl	$-1, %r8d
	leaq	_Z23average_using_registersPjPf(%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
.LFE2060:
	.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
	.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:

/**
 * Assignment 06 Program - moving_average.cu
 * Sarah Helble
 * 10/06/17
 *
 * Calculates the average of each index and its neighbors
 *
 * Usage ./aout
 *
 */


#include <hip/hip_runtime.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

// Execution Notes
// 512, 512 gives about equivalent times
// 512, 256 register is 2x faster
// 256, 256 register is slightly better
// 256, 128 register is slightly better
// 512, 128 register is slightly better
// 1024, 256 shared is slightly better
// 1024, 512 register is slightly better
//
// First run always seems to be bad (2x slower)

#define NUM_ELEMENTS 512
#define THREADS_PER_BLOCK 256

#define MAX_INT 30

/**
 * Returns the current time
 */
__host__ hipEvent_t get_time(void)
{
	hipEvent_t time;
	hipEventCreate(&time);
	hipEventRecord(time);
	return time;
}

/**
 * Kernel function that takes a moving average of the values in
 * @list and puts the results in @averages
 * Uses registers to store the calculations.
 */
__global__ void average_using_registers(unsigned int *list, float *averages)
{
  /* Calculate the current index */
  const unsigned int idx = (blockIdx.x * blockDim.x) + threadIdx.x;

	if(idx < NUM_ELEMENTS) {
		unsigned int sum = list[idx];
		unsigned int num = 1;

		// If there is a previous element, add it to sum
		if(idx > 0) {
			sum = sum + list[idx - 1];
			num = num + 1;
		}

		// If there is a next element, add it to sum
		if((idx + 1) < NUM_ELEMENTS) {
			sum = sum + list[idx + 1];
			num = num + 1;
		}

		averages[idx] = (float) sum / num;
	}
}

/**
 * Kernel function that takes a moving average of the values in
 * @list and puts the results in @averages
 * Uses shared memory to store the calculations.
 */
__global__ void average_using_shared(unsigned int *list, float *averages)
{
	__shared__ unsigned int sums[NUM_ELEMENTS];
	__shared__ unsigned int nums[NUM_ELEMENTS];

	const unsigned int idx = (blockIdx.x * blockDim.x) + threadIdx.x;

	if(idx < NUM_ELEMENTS) {
		sums[idx] = list[idx];
		nums[idx] = 1;

		// If there is a previous element, add it to sum
		if(idx > 0) {
			sums[idx] = sums[idx] + list[idx - 1];
			nums[idx] = nums[idx] + 1;
		}

		// If there is a next element, add it to sum
		if((idx + 1) < NUM_ELEMENTS) {
			sums[idx] = sums[idx] + list[idx + 1];
			nums[idx] = nums[idx] + 1;
		}

		// Calculate the average
		averages[idx] = (float) sums[idx] / nums[idx];
	}
}

/**
 * Fuction to handle the printing of results.
 * @list is the original array
 * @averages is the result
 */
void print_results(unsigned int *list, float *averages)
{
  int i = 0;

  printf("\n");
  for(i = 0; i < NUM_ELEMENTS; i++) {
    printf("Original value at index [%d]: %d, average: %f\n", i, list[i], averages[i]);
  }
  printf("\n");
}

/**
 * Function that sets up everything for the kernel function
 *
 * @array_size size of array (total number of threads)
 * @threads_per_block number of threads to put in each block
 * @use_registers is 1 if registers should be used. Otherwise, will call
 * kernel that uses shared memory
 */
void exec_kernel(bool use_registers)
{
  /* Calculate the size of the array */
  int array_size_in_bytes = (sizeof(unsigned int) * (NUM_ELEMENTS));
  int float_array_size_in_bytes = (sizeof(float) * (NUM_ELEMENTS));
  int i = 0;

  unsigned int *list;
  float *averages;

  //pin it
  hipHostMalloc((void **)&list, array_size_in_bytes, hipHostMallocDefault);
  hipHostMalloc((void **)&averages, float_array_size_in_bytes, hipHostMallocDefault);

	// Fill array with random numbers between 0 and MAX_INT
  for(i = 0; i < NUM_ELEMENTS; i++) {
  	list[i] = (unsigned int) rand() % MAX_INT;
  }

  /* Declare and allocate pointers for GPU based parameters */
  unsigned int *d_list;
  float *d_averages;

  hipMalloc((void **)&d_list, array_size_in_bytes);
  hipMalloc((void **)&d_averages, float_array_size_in_bytes);

  /* Copy the CPU memory to the GPU memory */
  hipMemcpy(d_list, list, array_size_in_bytes, hipMemcpyHostToDevice);

  /* Designate the number of blocks and threads */
  const unsigned int num_blocks = NUM_ELEMENTS/THREADS_PER_BLOCK;
  const unsigned int num_threads = NUM_ELEMENTS/num_blocks;

  /* Execute the kernel and keep track of start and end time for duration */
  float duration = 0;

  hipEvent_t start_time = get_time();

	if(use_registers) {
		average_using_registers<<<num_blocks, num_threads>>>(d_list, d_averages);
	} else {
		average_using_shared<<<num_blocks, num_threads>>>(d_list, d_averages);
	}

  hipEvent_t end_time = get_time();
  hipEventSynchronize(end_time);

  hipEventElapsedTime(&duration, start_time, end_time);

  /* Copy the changed GPU memory back to the CPU */
  hipMemcpy( averages, d_averages, float_array_size_in_bytes, hipMemcpyDeviceToHost);

  printf("\tDuration: %fmsn\n", duration);
  print_results(list, averages);

  /* Free the GPU memory */
  hipFree(d_list);
  hipFree(d_averages);

  /* Free the pinned CPU memory */
  hipHostFree(list);
  hipHostFree(averages);
}

/**
 * Entry point for execution. Checks command line arguments
 * then passes execution to subordinate function
 */
int main(int argc, char *argv[])
{

  printf("\n");

	/* Do the average with shared memory */
	printf("First Run of Averages Calculated using Shared Memory");
  exec_kernel(false);
	printf("-----------------------------------------------------------------\n");

	printf("Second Run of Averages Calculated using Shared Memory");
  exec_kernel(false);
	printf("-----------------------------------------------------------------\n");

  /* Do the average with registers*/
	printf("First Run of Averages Calculated using Register Memory");
  exec_kernel(true);
  printf("-----------------------------------------------------------------\n");

	printf("Second Run of Averages Calculated using Register Memory");
  exec_kernel(true);
  printf("-----------------------------------------------------------------\n");



  return EXIT_SUCCESS;
}

	.text
	.file	"moving_average.hip"
	.globl	_Z8get_timev                    # -- Begin function _Z8get_timev
	.type	_Z8get_timev,@function
_Z8get_timev:                           # @_Z8get_timev
	.cfi_startproc
# %bb.0:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	subq	$16, %rsp
	.cfi_def_cfa_offset 32
	.cfi_offset %rbx, -16
	leaq	8(%rsp), %rbx
	movq	%rbx, %rdi
	callq	hipEventCreate
	movq	(%rbx), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	movq	(%rbx), %rax
	addq	$16, %rsp
	.cfi_def_cfa_offset 16
	popq	%rbx
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end0:
	.size	_Z8get_timev, .Lfunc_end0-_Z8get_timev
	.cfi_endproc
                                        # -- End function
	.globl	_Z38__device_stub__average_using_registersPjPf # -- Begin function _Z38__device_stub__average_using_registersPjPf
	.type	_Z38__device_stub__average_using_registersPjPf,@function
_Z38__device_stub__average_using_registersPjPf: # @_Z38__device_stub__average_using_registersPjPf
	.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	$80, %rsp
	.cfi_def_cfa_offset 128
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	leaq	24(%rsp), %rax
	movq	%rdi, (%rax)
	leaq	16(%rsp), %rcx
	movq	%rsi, (%rcx)
	leaq	64(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rcx, 8(%rbx)
	leaq	48(%rsp), %r14
	leaq	32(%rsp), %r15
	leaq	8(%rsp), %r12
	movq	%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	$_Z23average_using_registersPjPf, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$96, %rsp
	.cfi_adjust_cfa_offset -96
	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_end1:
	.size	_Z38__device_stub__average_using_registersPjPf, .Lfunc_end1-_Z38__device_stub__average_using_registersPjPf
	.cfi_endproc
                                        # -- End function
	.globl	_Z35__device_stub__average_using_sharedPjPf # -- Begin function _Z35__device_stub__average_using_sharedPjPf
	.type	_Z35__device_stub__average_using_sharedPjPf,@function
_Z35__device_stub__average_using_sharedPjPf: # @_Z35__device_stub__average_using_sharedPjPf
	.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	$80, %rsp
	.cfi_def_cfa_offset 128
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	leaq	24(%rsp), %rax
	movq	%rdi, (%rax)
	leaq	16(%rsp), %rcx
	movq	%rsi, (%rcx)
	leaq	64(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rcx, 8(%rbx)
	leaq	48(%rsp), %r14
	leaq	32(%rsp), %r15
	leaq	8(%rsp), %r12
	movq	%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	$_Z20average_using_sharedPjPf, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$96, %rsp
	.cfi_adjust_cfa_offset -96
	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_end2:
	.size	_Z35__device_stub__average_using_sharedPjPf, .Lfunc_end2-_Z35__device_stub__average_using_sharedPjPf
	.cfi_endproc
                                        # -- End function
	.globl	_Z13print_resultsPjPf           # -- Begin function _Z13print_resultsPjPf
	.type	_Z13print_resultsPjPf,@function
_Z13print_resultsPjPf:                  # @_Z13print_resultsPjPf
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset %rbx, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movq	%rsi, %rbx
	movq	%rdi, %r14
	movl	$10, %edi
	callq	putchar@PLT
	xorl	%r15d, %r15d
.LBB3_1:                                # =>This Inner Loop Header: Depth=1
	movl	(%r14,%r15,4), %edx
	xorps	%xmm0, %xmm0
	cvtss2sd	(%rbx,%r15,4), %xmm0
	movl	$.L.str.1, %edi
	movl	%r15d, %esi
	movb	$1, %al
	callq	printf
	incq	%r15
	cmpq	$512, %r15                      # imm = 0x200
	jne	.LBB3_1
# %bb.2:
	movl	$10, %edi
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	jmp	putchar@PLT                     # TAILCALL
.Lfunc_end3:
	.size	_Z13print_resultsPjPf, .Lfunc_end3-_Z13print_resultsPjPf
	.cfi_endproc
                                        # -- End function
	.globl	_Z11exec_kernelb                # -- Begin function _Z11exec_kernelb
	.type	_Z11exec_kernelb,@function
_Z11exec_kernelb:                       # @_Z11exec_kernelb
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%rbx
	.cfi_def_cfa_offset 32
	subq	$48, %rsp
	.cfi_def_cfa_offset 80
	.cfi_offset %rbx, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movl	%edi, %ebx
	leaq	8(%rsp), %rdi
	xorl	%r14d, %r14d
	movl	$2048, %esi                     # imm = 0x800
	xorl	%edx, %edx
	callq	hipHostMalloc
	leaq	24(%rsp), %rdi
	movl	$2048, %esi                     # imm = 0x800
	xorl	%edx, %edx
	callq	hipHostMalloc
	movl	$2290649225, %r15d              # imm = 0x88888889
.LBB4_1:                                # =>This Inner Loop Header: Depth=1
	callq	rand
	movl	%eax, %ecx
	imulq	%r15, %rcx
	shrq	$36, %rcx
	movl	%ecx, %edx
	shll	$5, %edx
	addl	%ecx, %ecx
	subl	%edx, %ecx
	addl	%eax, %ecx
	movq	8(%rsp), %rax
	movl	%ecx, (%rax,%r14,4)
	incq	%r14
	cmpq	$512, %r14                      # imm = 0x200
	jne	.LBB4_1
# %bb.2:
	leaq	16(%rsp), %r14
	movl	$2048, %esi                     # imm = 0x800
	movq	%r14, %rdi
	callq	hipMalloc
	movq	%rsp, %rdi
	movl	$2048, %esi                     # imm = 0x800
	callq	hipMalloc
	movq	(%r14), %rdi
	movq	8(%rsp), %rsi
	movl	$2048, %edx                     # imm = 0x800
	movl	$1, %ecx
	callq	hipMemcpy
	movl	$0, 36(%rsp)
	leaq	40(%rsp), %r14
	movq	%r14, %rdi
	callq	hipEventCreate
	movq	(%r14), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	movq	(%r14), %r14
	movabsq	$4294967298, %rdi               # imm = 0x100000002
	leaq	254(%rdi), %rdx
	movl	$1, %esi
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testb	%bl, %bl
	je	.LBB4_5
# %bb.3:
	testl	%eax, %eax
	jne	.LBB4_7
# %bb.4:
	movq	16(%rsp), %rdi
	movq	(%rsp), %rsi
	callq	_Z38__device_stub__average_using_registersPjPf
	jmp	.LBB4_7
.LBB4_5:
	testl	%eax, %eax
	jne	.LBB4_7
# %bb.6:
	movq	16(%rsp), %rdi
	movq	(%rsp), %rsi
	callq	_Z35__device_stub__average_using_sharedPjPf
.LBB4_7:
	leaq	40(%rsp), %rbx
	movq	%rbx, %rdi
	callq	hipEventCreate
	movq	(%rbx), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	movq	(%rbx), %rbx
	movq	%rbx, %rdi
	callq	hipEventSynchronize
	leaq	36(%rsp), %r15
	movq	%r15, %rdi
	movq	%r14, %rsi
	movq	%rbx, %rdx
	callq	hipEventElapsedTime
	movq	24(%rsp), %rdi
	movq	(%rsp), %rsi
	movl	$2048, %edx                     # imm = 0x800
	movl	$2, %ecx
	callq	hipMemcpy
	cvtss2sd	(%r15), %xmm0
	movl	$.L.str.2, %edi
	movb	$1, %al
	callq	printf
	movq	8(%rsp), %rdi
	movq	24(%rsp), %rsi
	callq	_Z13print_resultsPjPf
	movq	16(%rsp), %rdi
	callq	hipFree
	movq	(%rsp), %rdi
	callq	hipFree
	movq	8(%rsp), %rdi
	callq	hipHostFree
	movq	24(%rsp), %rdi
	callq	hipHostFree
	addq	$48, %rsp
	.cfi_def_cfa_offset 32
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end4:
	.size	_Z11exec_kernelb, .Lfunc_end4-_Z11exec_kernelb
	.cfi_endproc
                                        # -- End function
	.globl	main                            # -- Begin function main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%rax
	.cfi_def_cfa_offset 16
	movl	$10, %edi
	callq	putchar@PLT
	movl	$.L.str.3, %edi
	xorl	%eax, %eax
	callq	printf
	xorl	%edi, %edi
	callq	_Z11exec_kernelb
	movl	$.Lstr.3, %edi
	callq	puts@PLT
	movl	$.L.str.5, %edi
	xorl	%eax, %eax
	callq	printf
	xorl	%edi, %edi
	callq	_Z11exec_kernelb
	movl	$.Lstr.3, %edi
	callq	puts@PLT
	movl	$.L.str.6, %edi
	xorl	%eax, %eax
	callq	printf
	movl	$1, %edi
	callq	_Z11exec_kernelb
	movl	$.Lstr.3, %edi
	callq	puts@PLT
	movl	$.L.str.7, %edi
	xorl	%eax, %eax
	callq	printf
	movl	$1, %edi
	callq	_Z11exec_kernelb
	movl	$.Lstr.3, %edi
	callq	puts@PLT
	xorl	%eax, %eax
	popq	%rcx
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end5:
	.size	main, .Lfunc_end5-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:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	subq	$32, %rsp
	.cfi_def_cfa_offset 48
	.cfi_offset %rbx, -16
	movq	__hip_gpubin_handle(%rip), %rbx
	testq	%rbx, %rbx
	jne	.LBB6_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rbx
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB6_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z23average_using_registersPjPf, %esi
	movl	$.L__unnamed_1, %edx
	movl	$.L__unnamed_1, %ecx
	movq	%rbx, %rdi
	movl	$-1, %r8d
	xorl	%r9d, %r9d
	callq	__hipRegisterFunction
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z20average_using_sharedPjPf, %esi
	movl	$.L__unnamed_2, %edx
	movl	$.L__unnamed_2, %ecx
	movq	%rbx, %rdi
	movl	$-1, %r8d
	xorl	%r9d, %r9d
	callq	__hipRegisterFunction
	movl	$__hip_module_dtor, %edi
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	popq	%rbx
	.cfi_def_cfa_offset 8
	jmp	atexit                          # TAILCALL
.Lfunc_end6:
	.size	__hip_module_ctor, .Lfunc_end6-__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	.LBB7_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
.LBB7_2:
	retq
.Lfunc_end7:
	.size	__hip_module_dtor, .Lfunc_end7-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	_Z23average_using_registersPjPf,@object # @_Z23average_using_registersPjPf
	.section	.rodata,"a",@progbits
	.globl	_Z23average_using_registersPjPf
	.p2align	3, 0x0
_Z23average_using_registersPjPf:
	.quad	_Z38__device_stub__average_using_registersPjPf
	.size	_Z23average_using_registersPjPf, 8

	.type	_Z20average_using_sharedPjPf,@object # @_Z20average_using_sharedPjPf
	.globl	_Z20average_using_sharedPjPf
	.p2align	3, 0x0
_Z20average_using_sharedPjPf:
	.quad	_Z35__device_stub__average_using_sharedPjPf
	.size	_Z20average_using_sharedPjPf, 8

	.type	.L.str.1,@object                # @.str.1
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str.1:
	.asciz	"Original value at index [%d]: %d, average: %f\n"
	.size	.L.str.1, 47

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"\tDuration: %fmsn\n"
	.size	.L.str.2, 18

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"First Run of Averages Calculated using Shared Memory"
	.size	.L.str.3, 53

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"Second Run of Averages Calculated using Shared Memory"
	.size	.L.str.5, 54

	.type	.L.str.6,@object                # @.str.6
.L.str.6:
	.asciz	"First Run of Averages Calculated using Register Memory"
	.size	.L.str.6, 55

	.type	.L.str.7,@object                # @.str.7
.L.str.7:
	.asciz	"Second Run of Averages Calculated using Register Memory"
	.size	.L.str.7, 56

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z23average_using_registersPjPf"
	.size	.L__unnamed_1, 32

	.type	.L__unnamed_2,@object           # @1
.L__unnamed_2:
	.asciz	"_Z20average_using_sharedPjPf"
	.size	.L__unnamed_2, 29

	.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.3,@object                 # @str.3
	.section	.rodata.str1.1,"aMS",@progbits,1
.Lstr.3:
	.asciz	"-----------------------------------------------------------------"
	.size	.Lstr.3, 66

	.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 _Z38__device_stub__average_using_registersPjPf
	.addrsig_sym _Z35__device_stub__average_using_sharedPjPf
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z23average_using_registersPjPf
	.addrsig_sym _Z20average_using_sharedPjPf
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z23average_using_registersPjPf ; -- Begin function _Z23average_using_registersPjPf
	.globl	_Z23average_using_registersPjPf
	.p2align	8
	.type	_Z23average_using_registersPjPf,@function
_Z23average_using_registersPjPf:        ; @_Z23average_using_registersPjPf
; %bb.0:
	s_load_b32 s2, s[0:1], 0x1c
	s_waitcnt lgkmcnt(0)
	s_and_b32 s2, s2, 0xffff
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(SKIP_1) | instid1(VALU_DEP_1)
	v_mad_u64_u32 v[2:3], null, s15, s2, v[0:1]
	s_mov_b32 s2, exec_lo
	v_cmpx_gt_u32_e32 0x200, v2
	s_cbranch_execz .LBB0_6
; %bb.1:
	s_load_b128 s[0:3], s[0:1], 0x0
	v_dual_mov_b32 v3, 0 :: v_dual_mov_b32 v6, 1
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_1)
	v_lshlrev_b64 v[0:1], 2, v[2:3]
	s_waitcnt lgkmcnt(0)
	v_add_co_u32 v3, vcc_lo, s0, v0
	s_delay_alu instid0(VALU_DEP_2)
	v_add_co_ci_u32_e32 v4, vcc_lo, s1, v1, vcc_lo
	s_mov_b32 s0, exec_lo
	global_load_b32 v5, v[3:4], off
	v_cmpx_ne_u32_e32 0, v2
	s_cbranch_execz .LBB0_3
; %bb.2:
	global_load_b32 v6, v[3:4], off offset:-4
	s_waitcnt vmcnt(0)
	v_dual_mov_b32 v6, 2 :: v_dual_add_nc_u32 v5, v6, v5
.LBB0_3:
	s_or_b32 exec_lo, exec_lo, s0
	s_delay_alu instid0(SALU_CYCLE_1)
	s_mov_b32 s0, exec_lo
	v_cmpx_ne_u32_e32 0x1ff, v2
	s_cbranch_execz .LBB0_5
; %bb.4:
	global_load_b32 v2, v[3:4], off offset:4
	v_add_nc_u32_e32 v6, 1, v6
	s_waitcnt vmcnt(0)
	v_add_nc_u32_e32 v5, v2, v5
.LBB0_5:
	s_or_b32 exec_lo, exec_lo, s0
	s_waitcnt vmcnt(0)
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_1)
	v_cvt_f32_u32_e32 v2, v5
	v_cvt_f32_ubyte0_e32 v3, v6
	v_div_scale_f32 v4, null, v3, v3, v2
	v_div_scale_f32 v7, vcc_lo, v2, v3, v2
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_2) | instid1(VALU_DEP_1)
	v_rcp_f32_e32 v5, v4
	s_waitcnt_depctr 0xfff
	v_fma_f32 v6, -v4, v5, 1.0
	v_fmac_f32_e32 v5, v6, v5
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mul_f32_e32 v6, v7, v5
	v_fma_f32 v8, -v4, v6, v7
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fmac_f32_e32 v6, v8, v5
	v_fma_f32 v4, -v4, v6, v7
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_2) | instid1(VALU_DEP_3)
	v_div_fmas_f32 v4, v4, v5, v6
	v_add_co_u32 v0, vcc_lo, s2, v0
	v_add_co_ci_u32_e32 v1, vcc_lo, s3, v1, vcc_lo
	v_div_fixup_f32 v2, v4, v3, v2
	global_store_b32 v[0:1], v2, off
.LBB0_6:
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z23average_using_registersPjPf
		.amdhsa_group_segment_fixed_size 0
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 272
		.amdhsa_user_sgpr_count 15
		.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 0
		.amdhsa_system_sgpr_workgroup_id_z 0
		.amdhsa_system_sgpr_workgroup_info 0
		.amdhsa_system_vgpr_workitem_id 0
		.amdhsa_next_free_vgpr 9
		.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	_Z23average_using_registersPjPf, .Lfunc_end0-_Z23average_using_registersPjPf
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 324
; NumSgprs: 18
; NumVgprs: 9
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 1
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 9
; Occupancy: 16
; WaveLimiterHint : 0
; COMPUTE_PGM_RSRC2:SCRATCH_EN: 0
; COMPUTE_PGM_RSRC2:USER_SGPR: 15
; COMPUTE_PGM_RSRC2:TRAP_HANDLER: 0
; COMPUTE_PGM_RSRC2:TGID_X_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Y_EN: 0
; COMPUTE_PGM_RSRC2:TGID_Z_EN: 0
; COMPUTE_PGM_RSRC2:TIDIG_COMP_CNT: 0
	.text
	.protected	_Z20average_using_sharedPjPf ; -- Begin function _Z20average_using_sharedPjPf
	.globl	_Z20average_using_sharedPjPf
	.p2align	8
	.type	_Z20average_using_sharedPjPf,@function
_Z20average_using_sharedPjPf:           ; @_Z20average_using_sharedPjPf
; %bb.0:
	s_load_b32 s2, s[0:1], 0x1c
	s_waitcnt lgkmcnt(0)
	s_and_b32 s2, s2, 0xffff
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(SKIP_1) | instid1(VALU_DEP_1)
	v_mad_u64_u32 v[4:5], null, s15, s2, v[0:1]
	s_mov_b32 s2, exec_lo
	v_cmpx_gt_u32_e32 0x200, v4
	s_cbranch_execz .LBB1_6
; %bb.1:
	s_load_b128 s[0:3], s[0:1], 0x0
	v_dual_mov_b32 v5, 0 :: v_dual_mov_b32 v6, 2.0
                                        ; implicit-def: $sgpr4
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_1)
	v_lshlrev_b64 v[0:1], 2, v[4:5]
	s_waitcnt lgkmcnt(0)
	v_add_co_u32 v2, vcc_lo, s0, v0
	s_delay_alu instid0(VALU_DEP_2)
	v_add_co_ci_u32_e32 v3, vcc_lo, s1, v1, vcc_lo
	s_mov_b32 s0, -1
	s_mov_b32 s1, exec_lo
	global_load_b32 v5, v[2:3], off
	v_cmpx_ne_u32_e32 0, v4
	s_cbranch_execz .LBB1_3
; %bb.2:
	global_load_b32 v6, v[2:3], off offset:-4
	v_cmp_ne_u32_e32 vcc_lo, 0x1ff, v4
	s_mov_b32 s4, 2.0
	s_or_not1_b32 s0, vcc_lo, exec_lo
	s_waitcnt vmcnt(0)
	v_dual_mov_b32 v6, 0x40400000 :: v_dual_add_nc_u32 v5, v6, v5
.LBB1_3:                                ; %Flow
	s_or_b32 exec_lo, exec_lo, s1
	v_mov_b32_e32 v4, s4
	s_and_saveexec_b32 s1, s0
	s_cbranch_execz .LBB1_5
; %bb.4:
	global_load_b32 v2, v[2:3], off offset:4
	v_mov_b32_e32 v4, v6
	s_waitcnt vmcnt(0)
	v_add_nc_u32_e32 v5, v2, v5
.LBB1_5:
	s_or_b32 exec_lo, exec_lo, s1
	s_waitcnt vmcnt(0)
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_cvt_f32_u32_e32 v2, v5
	v_div_scale_f32 v3, null, v4, v4, v2
	v_div_scale_f32 v7, vcc_lo, v2, v4, v2
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_2) | instid1(VALU_DEP_1)
	v_rcp_f32_e32 v5, v3
	s_waitcnt_depctr 0xfff
	v_fma_f32 v6, -v3, v5, 1.0
	v_fmac_f32_e32 v5, v6, v5
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mul_f32_e32 v6, v7, v5
	v_fma_f32 v8, -v3, v6, v7
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fmac_f32_e32 v6, v8, v5
	v_fma_f32 v3, -v3, v6, v7
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_2) | instid1(VALU_DEP_3)
	v_div_fmas_f32 v3, v3, v5, v6
	v_add_co_u32 v0, vcc_lo, s2, v0
	v_add_co_ci_u32_e32 v1, vcc_lo, s3, v1, vcc_lo
	v_div_fixup_f32 v2, v3, v4, v2
	global_store_b32 v[0:1], v2, off
.LBB1_6:
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z20average_using_sharedPjPf
		.amdhsa_group_segment_fixed_size 0
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 272
		.amdhsa_user_sgpr_count 15
		.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 0
		.amdhsa_system_sgpr_workgroup_id_z 0
		.amdhsa_system_sgpr_workgroup_info 0
		.amdhsa_system_vgpr_workitem_id 0
		.amdhsa_next_free_vgpr 9
		.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_end1:
	.size	_Z20average_using_sharedPjPf, .Lfunc_end1-_Z20average_using_sharedPjPf
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 336
; NumSgprs: 18
; NumVgprs: 9
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 1
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 9
; Occupancy: 16
; WaveLimiterHint : 0
; COMPUTE_PGM_RSRC2:SCRATCH_EN: 0
; COMPUTE_PGM_RSRC2:USER_SGPR: 15
; COMPUTE_PGM_RSRC2:TRAP_HANDLER: 0
; COMPUTE_PGM_RSRC2:TGID_X_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Y_EN: 0
; COMPUTE_PGM_RSRC2:TGID_Z_EN: 0
; COMPUTE_PGM_RSRC2:TIDIG_COMP_CNT: 0
	.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:     hidden_block_count_x
      - .offset:         20
        .size:           4
        .value_kind:     hidden_block_count_y
      - .offset:         24
        .size:           4
        .value_kind:     hidden_block_count_z
      - .offset:         28
        .size:           2
        .value_kind:     hidden_group_size_x
      - .offset:         30
        .size:           2
        .value_kind:     hidden_group_size_y
      - .offset:         32
        .size:           2
        .value_kind:     hidden_group_size_z
      - .offset:         34
        .size:           2
        .value_kind:     hidden_remainder_x
      - .offset:         36
        .size:           2
        .value_kind:     hidden_remainder_y
      - .offset:         38
        .size:           2
        .value_kind:     hidden_remainder_z
      - .offset:         56
        .size:           8
        .value_kind:     hidden_global_offset_x
      - .offset:         64
        .size:           8
        .value_kind:     hidden_global_offset_y
      - .offset:         72
        .size:           8
        .value_kind:     hidden_global_offset_z
      - .offset:         80
        .size:           2
        .value_kind:     hidden_grid_dims
    .group_segment_fixed_size: 0
    .kernarg_segment_align: 8
    .kernarg_segment_size: 272
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z23average_using_registersPjPf
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z23average_using_registersPjPf.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     9
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
  - .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:     hidden_block_count_x
      - .offset:         20
        .size:           4
        .value_kind:     hidden_block_count_y
      - .offset:         24
        .size:           4
        .value_kind:     hidden_block_count_z
      - .offset:         28
        .size:           2
        .value_kind:     hidden_group_size_x
      - .offset:         30
        .size:           2
        .value_kind:     hidden_group_size_y
      - .offset:         32
        .size:           2
        .value_kind:     hidden_group_size_z
      - .offset:         34
        .size:           2
        .value_kind:     hidden_remainder_x
      - .offset:         36
        .size:           2
        .value_kind:     hidden_remainder_y
      - .offset:         38
        .size:           2
        .value_kind:     hidden_remainder_z
      - .offset:         56
        .size:           8
        .value_kind:     hidden_global_offset_x
      - .offset:         64
        .size:           8
        .value_kind:     hidden_global_offset_y
      - .offset:         72
        .size:           8
        .value_kind:     hidden_global_offset_z
      - .offset:         80
        .size:           2
        .value_kind:     hidden_grid_dims
    .group_segment_fixed_size: 0
    .kernarg_segment_align: 8
    .kernarg_segment_size: 272
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z20average_using_sharedPjPf
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z20average_using_sharedPjPf.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     9
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
