/////////////////////////
// streamedMult.cu     //
// Andrew Krepps       //
// Module 7 Assignment //
// 3/26/2018           //
/////////////////////////

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

///////////////////////////////////////////////////////////////////////////////
/// \brief perform element-wise array multiplication
/// 
/// \param [in] in1 the first input array
/// \param [in] in2 the second input array
/// \param [out] out the output array
/// \param [in] n the number of array elements
///////////////////////////////////////////////////////////////////////////////
__global__
void arrayMult(const float* in1, const float* in2, float* out, const unsigned int n)
{
	unsigned int dataIdx = blockIdx.x*blockDim.x + threadIdx.x;
	
	if (dataIdx < n) {
		out[dataIdx] = in1[dataIdx]*in2[dataIdx];
	}
}

///////////////////////////////////////////////////////////////////////////////
/// \brief initialize host array data
/// 
/// \param [out] a the data array
/// \param [in] n the number of array elements
///////////////////////////////////////////////////////////////////////////////
void initHostArray(float* a, const unsigned int n)
{
	for (unsigned int i = 0; i < n; ++i) {
		a[i] = (float)i;
	}
}

///////////////////////////////////////////////////////////////////////////////
/// \brief verify output data
/// 
/// This assumes both input arrays were initialized using initHostArray.
/// 
/// \param [in] a the output array
/// \param [in] n the number of array elements
///////////////////////////////////////////////////////////////////////////////
void verifyResult(float* a, const unsigned int n)
{
	for (unsigned int i = 0; i < n; ++i) {
		float expected = (float)i * (float)i;
		if (a[i] != expected) {
			printf("Error! a[%d] != %f\n", expected);
		}
	}
}

///////////////////////////////////////////////////////////////////////////////
/// \brief perform element-wise array multiplication while evenly splitting
/// work among concurrent streams
/// 
/// \param [in] numBlocks the number of thread blocks (total)
/// \param [in] blockSize the number of threads per block
/// \param [in] numStreams the number of streams
/// 
/// \returns the total GPU memory copy and execution time (in ms)
///////////////////////////////////////////////////////////////////////////////
float runStreamedArrayMult(const unsigned int numBlocks, const unsigned int blockSize, const unsigned int numStreams)
{
	// calculate data size
	const unsigned int n = numBlocks*blockSize;
	const unsigned int numBytes = n*sizeof(float);
	
	// evenly split blocks among streams
	const unsigned int blocksPerStream = numBlocks/numStreams;
	const unsigned int elementsPerStream = n/numStreams;
	const unsigned int bytesPerStream = elementsPerStream*sizeof(float);
	
	// allocate and initialize pinned host memory
	float* in1;
	float* in2;
	float* out;
	cudaMallocHost((void**)&in1, numBytes);
	cudaMallocHost((void**)&in2, numBytes);
	cudaMallocHost((void**)&out, numBytes);
	initHostArray(in1, n);
	initHostArray(in2, n);
	
	// allocate device memory
	float* d_in1;
	float* d_in2;
	float* d_out;
	cudaMalloc((void**)&d_in1, numBytes);
	cudaMalloc((void**)&d_in2, numBytes);
	cudaMalloc((void**)&d_out, numBytes);
	
	// create streams
	cudaStream_t* streams = (cudaStream_t*) malloc(numStreams*sizeof(cudaStream_t));
	for (unsigned int i = 0; i < numStreams; ++i) {
		cudaStreamCreate(&streams[i]);
	}
	
	// create timing events
	cudaEvent_t start, stop;
	cudaEventCreate(&start);
	cudaEventCreate(&stop);
	
	// start timer
	cudaEventRecord(start, 0);
	
	// execute kernels (data is split evenly among streams)
	for (unsigned int i = 0; i < numStreams; ++i) {
		const unsigned int startIdx = i*elementsPerStream;
		cudaMemcpyAsync(d_in1 + startIdx, in1 + startIdx, bytesPerStream, cudaMemcpyHostToDevice, streams[i]);
		cudaMemcpyAsync(d_in2 + startIdx, in2 + startIdx, bytesPerStream, cudaMemcpyHostToDevice, streams[i]);
		arrayMult<<<blocksPerStream, blockSize, 0, streams[i]>>>(d_in1 + startIdx, d_in2 + startIdx, d_out + startIdx, elementsPerStream);
		cudaMemcpyAsync(out + startIdx, d_out + startIdx, bytesPerStream, cudaMemcpyDeviceToHost, streams[i]);
	}
	
	// stop timer and wait for GPU to finish
	cudaEventRecord(stop, 0);
	cudaEventSynchronize(stop);
	
	// calculate execution time (in ms)
	float ms;
	cudaEventElapsedTime(&ms, start, stop);
	
	// verify output
	verifyResult(out, n);

	// free events
	cudaEventDestroy(start);
	cudaEventDestroy(stop);
	
	// free streams
	for (unsigned int i = 0; i < numStreams; ++i) {
		cudaStreamDestroy(streams[i]);
	}
	free(streams);
	
	// free device memory
	cudaFree(d_in1);
	cudaFree(d_in2);
	cudaFree(d_out);
	
	// free pinned host memory
	cudaFreeHost(in1);
	cudaFreeHost(in2);
	cudaFreeHost(out);
	
	// return execution time (in ms)
	return ms;
}

int main(int argc, char** argv)
{
	// configure run
	unsigned int numBlocks = 512;
	unsigned int blockSize = 256;
	unsigned int numStreams = 1;
	
	if (argc > 1) {
		numBlocks = atoi(argv[1]);
	}
	if (argc > 2) {
		blockSize = atoi(argv[2]);
	}
	if (argc > 3) {
		numStreams = atoi(argv[3]);
	}
	
	// dummy execution to avoid startup performance hits
	runStreamedArrayMult(numBlocks, blockSize, numStreams);
	
	// run experiment and display timing results
	float ms = runStreamedArrayMult(numBlocks, blockSize, numStreams);
	printf("GPU execution time (%d streams): %.3f ms\n", numStreams, ms);
	
	return EXIT_SUCCESS;
}

	.file	"tmpxft_0030c3d0_00000000-6_streamedMult.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	_Z13initHostArrayPfj
	.type	_Z13initHostArrayPfj, @function
_Z13initHostArrayPfj:
.LFB2027:
	.cfi_startproc
	endbr64
	xorl	%eax, %eax
.L3:
	cmpl	%esi, %eax
	jnb	.L8
	movl	%eax, %edx
	cvtsi2ssq	%rdx, %xmm0
	movss	%xmm0, (%rdi,%rax,4)
	incq	%rax
	jmp	.L3
.L8:
	ret
	.cfi_endproc
.LFE2027:
	.size	_Z13initHostArrayPfj, .-_Z13initHostArrayPfj
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"Error! a[%d] != %f\n"
	.text
	.globl	_Z12verifyResultPfj
	.type	_Z12verifyResultPfj, @function
_Z12verifyResultPfj:
.LFB2028:
	.cfi_startproc
	endbr64
	pushq	%r13
	.cfi_def_cfa_offset 16
	.cfi_offset 13, -16
	leaq	.LC0(%rip), %r13
	pushq	%r12
	.cfi_def_cfa_offset 24
	.cfi_offset 12, -24
	movq	%rdi, %r12
	pushq	%rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	movl	%esi, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
	xorl	%ebx, %ebx
	pushq	%rdx
	.cfi_def_cfa_offset 48
.L10:
	cmpq	%rbp, %rbx
	je	.L18
	movl	%ebx, %eax
	cvtsi2ssq	%rax, %xmm0
	mulss	%xmm0, %xmm0
	ucomiss	(%r12,%rbx,4), %xmm0
	jp	.L16
	je	.L13
.L16:
	movq	%r13, %rsi
	movl	$2, %edi
	movb	$1, %al
	cvtss2sd	%xmm0, %xmm0
	call	__printf_chk@PLT
.L13:
	incq	%rbx
	jmp	.L10
.L18:
	popq	%rax
	.cfi_def_cfa_offset 40
	popq	%rbx
	.cfi_def_cfa_offset 32
	popq	%rbp
	.cfi_def_cfa_offset 24
	popq	%r12
	.cfi_def_cfa_offset 16
	popq	%r13
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2028:
	.size	_Z12verifyResultPfj, .-_Z12verifyResultPfj
	.globl	_Z35__device_stub__Z9arrayMultPKfS0_PfjPKfS0_Pfj
	.type	_Z35__device_stub__Z9arrayMultPKfS0_PfjPKfS0_Pfj, @function
_Z35__device_stub__Z9arrayMultPKfS0_PfjPKfS0_Pfj:
.LFB2055:
	.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
	movq	%rdx, 8(%rsp)
	leaq	40(%rsp), %rdx
	movl	%ecx, 4(%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	8(%rsp), %rax
	movq	%rax, 120(%rsp)
	leaq	4(%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	.L19
	pushq	48(%rsp)
	.cfi_def_cfa_offset 168
	leaq	_Z9arrayMultPKfS0_Pfj(%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
.L19:
	movq	136(%rsp), %rax
	subq	%fs:40, %rax
	je	.L21
	call	__stack_chk_fail@PLT
.L21:
	addq	$152, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2055:
	.size	_Z35__device_stub__Z9arrayMultPKfS0_PfjPKfS0_Pfj, .-_Z35__device_stub__Z9arrayMultPKfS0_PfjPKfS0_Pfj
	.globl	_Z9arrayMultPKfS0_Pfj
	.type	_Z9arrayMultPKfS0_Pfj, @function
_Z9arrayMultPKfS0_Pfj:
.LFB2056:
	.cfi_startproc
	endbr64
	jmp	_Z35__device_stub__Z9arrayMultPKfS0_PfjPKfS0_Pfj
	.cfi_endproc
.LFE2056:
	.size	_Z9arrayMultPKfS0_Pfj, .-_Z9arrayMultPKfS0_Pfj
	.globl	_Z20runStreamedArrayMultjjj
	.type	_Z20runStreamedArrayMultjjj, @function
_Z20runStreamedArrayMultjjj:
.LFB2029:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	movl	%edi, %eax
	movl	%esi, %ecx
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	imull	%edi, %ecx
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	movl	%edx, %ebx
	subq	$136, %rsp
	.cfi_def_cfa_offset 192
	movl	%esi, 20(%rsp)
	leaq	32(%rsp), %rdi
	movq	%fs:40, %rdx
	movq	%rdx, 120(%rsp)
	xorl	%edx, %edx
	xorl	%edx, %edx
	movl	%ecx, 8(%rsp)
	divl	%ebx
	xorl	%edx, %edx
	movl	%eax, 28(%rsp)
	movl	%ecx, %eax
	divl	%ebx
	salq	$3, %rbx
	movl	%eax, 12(%rsp)
	sall	$2, %eax
	movl	%eax, 24(%rsp)
	movl	%ecx, %eax
	leal	0(,%rax,4), %ebp
	movq	%rbp, %rsi
	call	cudaMallocHost@PLT
	movq	%rbp, %rsi
	leaq	40(%rsp), %rdi
	call	cudaMallocHost@PLT
	movq	%rbp, %rsi
	leaq	48(%rsp), %rdi
	call	cudaMallocHost@PLT
	movl	8(%rsp), %esi
	movq	32(%rsp), %rdi
	call	_Z13initHostArrayPfj
	movl	8(%rsp), %esi
	movq	40(%rsp), %rdi
	call	_Z13initHostArrayPfj
	movq	%rbp, %rsi
	leaq	56(%rsp), %rdi
	call	cudaMalloc@PLT
	movq	%rbp, %rsi
	leaq	64(%rsp), %rdi
	call	cudaMalloc@PLT
	movq	%rbp, %rsi
	leaq	72(%rsp), %rdi
	call	cudaMalloc@PLT
	movq	%rbx, %rdi
	call	malloc@PLT
	movq	%rax, %rbp
	movq	%rax, %r13
	addq	%rax, %rbx
	movq	%rax, %r12
.L25:
	cmpq	%rbx, %r12
	je	.L34
	movq	%r12, %rdi
	addq	$8, %r12
	call	cudaStreamCreate@PLT
	jmp	.L25
.L34:
	leaq	80(%rsp), %rdi
	movq	%rbp, %r12
	movabsq	$4294967297, %r15
	call	cudaEventCreate@PLT
	leaq	88(%rsp), %rdi
	call	cudaEventCreate@PLT
	movq	80(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	xorl	%eax, %eax
	movl	%eax, 16(%rsp)
.L27:
	cmpq	%rbx, %r12
	je	.L35
	movl	16(%rsp), %r10d
	movl	24(%rsp), %eax
	movl	$1, %ecx
	movq	32(%rsp), %rsi
	movq	56(%rsp), %rdi
	leaq	0(,%r10,4), %r14
	movq	(%r12), %r8
	movq	%rax, %rdx
	movq	%rax, (%rsp)
	addq	%r14, %rsi
	addq	%r14, %rdi
	call	cudaMemcpyAsync@PLT
	movq	40(%rsp), %rsi
	movq	64(%rsp), %rdi
	movl	$1, %ecx
	movq	(%r12), %r8
	movq	(%rsp), %rdx
	addq	%r14, %rsi
	addq	%r14, %rdi
	call	cudaMemcpyAsync@PLT
	movl	20(%rsp), %eax
	movq	(%r12), %r9
	xorl	%r8d, %r8d
	movq	%r15, 112(%rsp)
	movl	116(%rsp), %ecx
	movl	%eax, 108(%rsp)
	movl	28(%rsp), %eax
	movq	%r15, 100(%rsp)
	movq	108(%rsp), %rdx
	movl	%eax, 96(%rsp)
	movl	104(%rsp), %esi
	movq	96(%rsp), %rdi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L28
	movq	72(%rsp), %rdx
	movq	64(%rsp), %rsi
	movq	56(%rsp), %rdi
	movl	12(%rsp), %ecx
	addq	%r14, %rdx
	addq	%r14, %rsi
	addq	%r14, %rdi
	call	_Z35__device_stub__Z9arrayMultPKfS0_PfjPKfS0_Pfj
.L28:
	movq	72(%rsp), %rsi
	movq	48(%rsp), %rdi
	movl	$2, %ecx
	addq	$8, %r12
	movq	-8(%r12), %r8
	movq	(%rsp), %rdx
	addq	%r14, %rsi
	addq	%r14, %rdi
	call	cudaMemcpyAsync@PLT
	movl	12(%rsp), %ecx
	addl	%ecx, 16(%rsp)
	jmp	.L27
.L35:
	movq	88(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movq	88(%rsp), %rdi
	call	cudaEventSynchronize@PLT
	movq	88(%rsp), %rdx
	movq	80(%rsp), %rsi
	leaq	108(%rsp), %rdi
	call	cudaEventElapsedTime@PLT
	movl	8(%rsp), %esi
	movq	48(%rsp), %rdi
	call	_Z12verifyResultPfj
	movq	80(%rsp), %rdi
	call	cudaEventDestroy@PLT
	movq	88(%rsp), %rdi
	call	cudaEventDestroy@PLT
.L30:
	cmpq	%rbx, %r13
	je	.L36
	movq	0(%r13), %rdi
	addq	$8, %r13
	call	cudaStreamDestroy@PLT
	jmp	.L30
.L36:
	movq	%rbp, %rdi
	call	free@PLT
	movq	56(%rsp), %rdi
	call	cudaFree@PLT
	movq	64(%rsp), %rdi
	call	cudaFree@PLT
	movq	72(%rsp), %rdi
	call	cudaFree@PLT
	movq	32(%rsp), %rdi
	call	cudaFreeHost@PLT
	movq	40(%rsp), %rdi
	call	cudaFreeHost@PLT
	movq	48(%rsp), %rdi
	call	cudaFreeHost@PLT
	movss	108(%rsp), %xmm0
	movq	120(%rsp), %rax
	subq	%fs:40, %rax
	je	.L32
	call	__stack_chk_fail@PLT
.L32:
	addq	$136, %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
.LFE2029:
	.size	_Z20runStreamedArrayMultjjj, .-_Z20runStreamedArrayMultjjj
	.section	.rodata.str1.1
.LC1:
	.string	"GPU execution time (%d streams): %.3f ms\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2030:
	.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
	pushq	%r12
	.cfi_def_cfa_offset 32
	.cfi_offset 12, -32
	pushq	%rbp
	.cfi_def_cfa_offset 40
	.cfi_offset 6, -40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	.cfi_offset 3, -48
	cmpl	$1, %edi
	jle	.L39
	movl	%edi, %r14d
	movq	8(%rsi), %rdi
	movq	%rsi, %r13
	call	atoi@PLT
	movl	%eax, %r12d
	cmpl	$2, %r14d
	je	.L40
	movq	16(%r13), %rdi
	movl	$1, %ebx
	call	atoi@PLT
	movl	%eax, %ebp
	cmpl	$3, %r14d
	je	.L38
	movq	24(%r13), %rdi
	call	atoi@PLT
	movl	%eax, %ebx
	jmp	.L38
.L39:
	movl	$256, %ebp
	movl	$512, %r12d
	jmp	.L43
.L40:
	movl	$256, %ebp
.L43:
	movl	$1, %ebx
.L38:
	movl	%ebx, %edx
	movl	%ebp, %esi
	movl	%r12d, %edi
	call	_Z20runStreamedArrayMultjjj
	movl	%ebx, %edx
	movl	%ebp, %esi
	movl	%r12d, %edi
	call	_Z20runStreamedArrayMultjjj
	movl	%ebx, %edx
	leaq	.LC1(%rip), %rsi
	movb	$1, %al
	movl	$2, %edi
	cvtss2sd	%xmm0, %xmm0
	call	__printf_chk@PLT
	popq	%rbx
	.cfi_def_cfa_offset 40
	xorl	%eax, %eax
	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
.LFE2030:
	.size	main, .-main
	.section	.rodata.str1.1
.LC2:
	.string	"_Z9arrayMultPKfS0_Pfj"
	.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	.LC2(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z9arrayMultPKfS0_Pfj(%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
	.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:

/////////////////////////
// streamedMult.cu     //
// Andrew Krepps       //
// Module 7 Assignment //
// 3/26/2018           //
/////////////////////////


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

///////////////////////////////////////////////////////////////////////////////
/// \brief perform element-wise array multiplication
/// 
/// \param [in] in1 the first input array
/// \param [in] in2 the second input array
/// \param [out] out the output array
/// \param [in] n the number of array elements
///////////////////////////////////////////////////////////////////////////////
__global__
void arrayMult(const float* in1, const float* in2, float* out, const unsigned int n)
{
	unsigned int dataIdx = blockIdx.x*blockDim.x + threadIdx.x;
	
	if (dataIdx < n) {
		out[dataIdx] = in1[dataIdx]*in2[dataIdx];
	}
}

///////////////////////////////////////////////////////////////////////////////
/// \brief initialize host array data
/// 
/// \param [out] a the data array
/// \param [in] n the number of array elements
///////////////////////////////////////////////////////////////////////////////
void initHostArray(float* a, const unsigned int n)
{
	for (unsigned int i = 0; i < n; ++i) {
		a[i] = (float)i;
	}
}

///////////////////////////////////////////////////////////////////////////////
/// \brief verify output data
/// 
/// This assumes both input arrays were initialized using initHostArray.
/// 
/// \param [in] a the output array
/// \param [in] n the number of array elements
///////////////////////////////////////////////////////////////////////////////
void verifyResult(float* a, const unsigned int n)
{
	for (unsigned int i = 0; i < n; ++i) {
		float expected = (float)i * (float)i;
		if (a[i] != expected) {
			printf("Error! a[%d] != %f\n", expected);
		}
	}
}

///////////////////////////////////////////////////////////////////////////////
/// \brief perform element-wise array multiplication while evenly splitting
/// work among concurrent streams
/// 
/// \param [in] numBlocks the number of thread blocks (total)
/// \param [in] blockSize the number of threads per block
/// \param [in] numStreams the number of streams
/// 
/// \returns the total GPU memory copy and execution time (in ms)
///////////////////////////////////////////////////////////////////////////////
float runStreamedArrayMult(const unsigned int numBlocks, const unsigned int blockSize, const unsigned int numStreams)
{
	// calculate data size
	const unsigned int n = numBlocks*blockSize;
	const unsigned int numBytes = n*sizeof(float);
	
	// evenly split blocks among streams
	const unsigned int blocksPerStream = numBlocks/numStreams;
	const unsigned int elementsPerStream = n/numStreams;
	const unsigned int bytesPerStream = elementsPerStream*sizeof(float);
	
	// allocate and initialize pinned host memory
	float* in1;
	float* in2;
	float* out;
	hipHostMalloc((void**)&in1, numBytes, hipHostMallocDefault);
	hipHostMalloc((void**)&in2, numBytes, hipHostMallocDefault);
	hipHostMalloc((void**)&out, numBytes, hipHostMallocDefault);
	initHostArray(in1, n);
	initHostArray(in2, n);
	
	// allocate device memory
	float* d_in1;
	float* d_in2;
	float* d_out;
	hipMalloc((void**)&d_in1, numBytes);
	hipMalloc((void**)&d_in2, numBytes);
	hipMalloc((void**)&d_out, numBytes);
	
	// create streams
	hipStream_t* streams = (hipStream_t*) malloc(numStreams*sizeof(hipStream_t));
	for (unsigned int i = 0; i < numStreams; ++i) {
		hipStreamCreate(&streams[i]);
	}
	
	// create timing events
	hipEvent_t start, stop;
	hipEventCreate(&start);
	hipEventCreate(&stop);
	
	// start timer
	hipEventRecord(start, 0);
	
	// execute kernels (data is split evenly among streams)
	for (unsigned int i = 0; i < numStreams; ++i) {
		const unsigned int startIdx = i*elementsPerStream;
		hipMemcpyAsync(d_in1 + startIdx, in1 + startIdx, bytesPerStream, hipMemcpyHostToDevice, streams[i]);
		hipMemcpyAsync(d_in2 + startIdx, in2 + startIdx, bytesPerStream, hipMemcpyHostToDevice, streams[i]);
		arrayMult<<<blocksPerStream, blockSize, 0, streams[i]>>>(d_in1 + startIdx, d_in2 + startIdx, d_out + startIdx, elementsPerStream);
		hipMemcpyAsync(out + startIdx, d_out + startIdx, bytesPerStream, hipMemcpyDeviceToHost, streams[i]);
	}
	
	// stop timer and wait for GPU to finish
	hipEventRecord(stop, 0);
	hipEventSynchronize(stop);
	
	// calculate execution time (in ms)
	float ms;
	hipEventElapsedTime(&ms, start, stop);
	
	// verify output
	verifyResult(out, n);

	// free events
	hipEventDestroy(start);
	hipEventDestroy(stop);
	
	// free streams
	for (unsigned int i = 0; i < numStreams; ++i) {
		hipStreamDestroy(streams[i]);
	}
	free(streams);
	
	// free device memory
	hipFree(d_in1);
	hipFree(d_in2);
	hipFree(d_out);
	
	// free pinned host memory
	hipHostFree(in1);
	hipHostFree(in2);
	hipHostFree(out);
	
	// return execution time (in ms)
	return ms;
}

int main(int argc, char** argv)
{
	// configure run
	unsigned int numBlocks = 512;
	unsigned int blockSize = 256;
	unsigned int numStreams = 1;
	
	if (argc > 1) {
		numBlocks = atoi(argv[1]);
	}
	if (argc > 2) {
		blockSize = atoi(argv[2]);
	}
	if (argc > 3) {
		numStreams = atoi(argv[3]);
	}
	
	// dummy execution to avoid startup performance hits
	runStreamedArrayMult(numBlocks, blockSize, numStreams);
	
	// run experiment and display timing results
	float ms = runStreamedArrayMult(numBlocks, blockSize, numStreams);
	printf("GPU execution time (%d streams): %.3f ms\n", numStreams, ms);
	
	return EXIT_SUCCESS;
}

	.text
	.file	"streamedMult.hip"
	.globl	_Z24__device_stub__arrayMultPKfS0_Pfj # -- Begin function _Z24__device_stub__arrayMultPKfS0_Pfj
	.type	_Z24__device_stub__arrayMultPKfS0_Pfj,@function
_Z24__device_stub__arrayMultPKfS0_Pfj:  # @_Z24__device_stub__arrayMultPKfS0_Pfj
	.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), %rdi
	movq	%rsi, (%rdi)
	leaq	24(%rsp), %rsi
	movq	%rdx, (%rsi)
	leaq	4(%rsp), %rdx
	movl	%ecx, (%rdx)
	leaq	80(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rdi, 8(%rbx)
	movq	%rsi, 16(%rbx)
	movq	%rdx, 24(%rbx)
	leaq	64(%rsp), %r14
	leaq	48(%rsp), %r15
	leaq	16(%rsp), %r12
	leaq	8(%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	$_Z9arrayMultPKfS0_Pfj, %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	_Z24__device_stub__arrayMultPKfS0_Pfj, .Lfunc_end0-_Z24__device_stub__arrayMultPKfS0_Pfj
	.cfi_endproc
                                        # -- End function
	.globl	_Z13initHostArrayPfj            # -- Begin function _Z13initHostArrayPfj
	.type	_Z13initHostArrayPfj,@function
_Z13initHostArrayPfj:                   # @_Z13initHostArrayPfj
	.cfi_startproc
# %bb.0:
	testl	%esi, %esi
	je	.LBB1_3
# %bb.1:                                # %.lr.ph.preheader
	movl	%esi, %eax
	xorl	%ecx, %ecx
.LBB1_2:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movl	%ecx, %edx
	xorps	%xmm0, %xmm0
	cvtsi2ss	%rdx, %xmm0
	movss	%xmm0, (%rdi,%rcx,4)
	incq	%rcx
	cmpq	%rcx, %rax
	jne	.LBB1_2
.LBB1_3:                                # %._crit_edge
	retq
.Lfunc_end1:
	.size	_Z13initHostArrayPfj, .Lfunc_end1-_Z13initHostArrayPfj
	.cfi_endproc
                                        # -- End function
	.globl	_Z12verifyResultPfj             # -- Begin function _Z12verifyResultPfj
	.type	_Z12verifyResultPfj,@function
_Z12verifyResultPfj:                    # @_Z12verifyResultPfj
	.cfi_startproc
# %bb.0:
	testl	%esi, %esi
	je	.LBB2_6
# %bb.1:                                # %.lr.ph.preheader
	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	%rdi, %rbx
	movl	%esi, %r14d
	xorl	%r15d, %r15d
.LBB2_2:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movl	%r15d, %eax
	xorps	%xmm0, %xmm0
	cvtsi2ss	%rax, %xmm0
	mulss	%xmm0, %xmm0
	movss	(%rbx,%r15,4), %xmm1            # xmm1 = mem[0],zero,zero,zero
	ucomiss	%xmm0, %xmm1
	jne	.LBB2_3
	jnp	.LBB2_4
.LBB2_3:                                #   in Loop: Header=BB2_2 Depth=1
	cvtss2sd	%xmm0, %xmm0
	movl	$.L.str, %edi
	movb	$1, %al
	callq	printf
.LBB2_4:                                #   in Loop: Header=BB2_2 Depth=1
	incq	%r15
	cmpq	%r15, %r14
	jne	.LBB2_2
# %bb.5:
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	.cfi_restore %rbx
	.cfi_restore %r14
	.cfi_restore %r15
.LBB2_6:                                # %._crit_edge
	retq
.Lfunc_end2:
	.size	_Z12verifyResultPfj, .Lfunc_end2-_Z12verifyResultPfj
	.cfi_endproc
                                        # -- End function
	.globl	_Z20runStreamedArrayMultjjj     # -- Begin function _Z20runStreamedArrayMultjjj
	.type	_Z20runStreamedArrayMultjjj,@function
_Z20runStreamedArrayMultjjj:            # @_Z20runStreamedArrayMultjjj
	.cfi_startproc
# %bb.0:
	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	$104, %rsp
	.cfi_def_cfa_offset 160
	.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	%edx, %r15d
	movl	%edi, %eax
	movl	%esi, 80(%rsp)                  # 4-byte Spill
	movl	%esi, %ebx
	imull	%edi, %ebx
	leal	(,%rbx,4), %r14d
	xorl	%edx, %edx
	divl	%r15d
	movl	%eax, 16(%rsp)                  # 4-byte Spill
	movl	%ebx, %eax
	xorl	%edx, %edx
	divl	%r15d
                                        # kill: def $eax killed $eax def $rax
	movq	%rax, 24(%rsp)                  # 8-byte Spill
	leaq	72(%rsp), %rdi
	movq	%r14, %rsi
	xorl	%edx, %edx
	callq	hipHostMalloc
	leaq	64(%rsp), %rdi
	movq	%r14, %rsi
	xorl	%edx, %edx
	callq	hipHostMalloc
	leaq	56(%rsp), %rdi
	movq	%r14, %rsi
	xorl	%edx, %edx
	callq	hipHostMalloc
	testl	%ebx, %ebx
	je	.LBB3_5
# %bb.1:                                # %.lr.ph.preheader.i
	movq	72(%rsp), %rcx
	movl	%ebx, %eax
	xorl	%edx, %edx
.LBB3_2:                                # %.lr.ph.i
                                        # =>This Inner Loop Header: Depth=1
	movl	%edx, %esi
	xorps	%xmm0, %xmm0
	cvtsi2ss	%rsi, %xmm0
	movss	%xmm0, (%rcx,%rdx,4)
	incq	%rdx
	cmpq	%rdx, %rax
	jne	.LBB3_2
# %bb.3:                                # %_Z13initHostArrayPfj.exit
	movq	64(%rsp), %rcx
	xorl	%edx, %edx
.LBB3_4:                                # %.lr.ph.i64
                                        # =>This Inner Loop Header: Depth=1
	movl	%edx, %esi
	xorps	%xmm0, %xmm0
	cvtsi2ss	%rsi, %xmm0
	movss	%xmm0, (%rcx,%rdx,4)
	incq	%rdx
	cmpq	%rdx, %rax
	jne	.LBB3_4
.LBB3_5:                                # %_Z13initHostArrayPfj.exit68
	movq	%rbx, 96(%rsp)                  # 8-byte Spill
	movq	24(%rsp), %rax                  # 8-byte Reload
	leal	(,%rax,4), %ebp
	leaq	48(%rsp), %rdi
	movq	%r14, %rsi
	callq	hipMalloc
	leaq	40(%rsp), %rdi
	movq	%r14, %rsi
	callq	hipMalloc
	leaq	32(%rsp), %rdi
	movq	%r14, %rsi
	callq	hipMalloc
	movl	%r15d, %edi
	shlq	$3, %rdi
	callq	malloc
	movq	%rax, %r12
	cmpl	$1, %r15d
	adcl	$0, %r15d
	leaq	(,%r15,8), %rbx
	xorl	%r14d, %r14d
.LBB3_6:                                # =>This Inner Loop Header: Depth=1
	leaq	(%r12,%r14), %rdi
	callq	hipStreamCreate
	addq	$8, %r14
	cmpq	%r14, %rbx
	jne	.LBB3_6
# %bb.7:
	leaq	88(%rsp), %r14
	movq	%r14, %rdi
	callq	hipEventCreate
	leaq	8(%rsp), %rdi
	callq	hipEventCreate
	movq	(%r14), %rdi
	xorl	%ebx, %ebx
	xorl	%esi, %esi
	callq	hipEventRecord
	movl	%ebp, %r13d
	movl	16(%rsp), %eax                  # 4-byte Reload
	btsq	$32, %rax
	movq	%rax, 16(%rsp)                  # 8-byte Spill
	movl	80(%rsp), %r14d                 # 4-byte Reload
	btsq	$32, %r14
.LBB3_8:                                # =>This Inner Loop Header: Depth=1
	movq	%r15, %rbp
	movq	24(%rsp), %r15                  # 8-byte Reload
                                        # kill: def $r15d killed $r15d killed $r15 def $r15
	imull	%ebx, %r15d
	shlq	$2, %r15
	movq	48(%rsp), %rdi
	addq	%r15, %rdi
	movq	72(%rsp), %rsi
	addq	%r15, %rsi
	movq	(%r12,%rbx,8), %r8
	movq	%r13, %rdx
	movl	$1, %ecx
	callq	hipMemcpyAsync
	movq	40(%rsp), %rdi
	addq	%r15, %rdi
	movq	64(%rsp), %rsi
	addq	%r15, %rsi
	movq	(%r12,%rbx,8), %r8
	movq	%r13, %rdx
	movl	$1, %ecx
	callq	hipMemcpyAsync
	movq	(%r12,%rbx,8), %r9
	movq	16(%rsp), %rdi                  # 8-byte Reload
	movl	$1, %esi
	movq	%r14, %rdx
	movl	$1, %ecx
	xorl	%r8d, %r8d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB3_10
# %bb.9:                                #   in Loop: Header=BB3_8 Depth=1
	movq	48(%rsp), %rdi
	addq	%r15, %rdi
	movq	40(%rsp), %rsi
	addq	%r15, %rsi
	movq	32(%rsp), %rdx
	addq	%r15, %rdx
	movq	24(%rsp), %rcx                  # 8-byte Reload
                                        # kill: def $ecx killed $ecx killed $rcx
	callq	_Z24__device_stub__arrayMultPKfS0_Pfj
.LBB3_10:                               #   in Loop: Header=BB3_8 Depth=1
	movq	56(%rsp), %rdi
	addq	%r15, %rdi
	addq	32(%rsp), %r15
	movq	(%r12,%rbx,8), %r8
	movq	%r15, %rsi
	movq	%r13, %rdx
	movl	$2, %ecx
	callq	hipMemcpyAsync
	incq	%rbx
	movq	%rbp, %r15
	cmpq	%rbx, %rbp
	jne	.LBB3_8
# %bb.11:
	movq	8(%rsp), %rdi
	xorl	%ebx, %ebx
	xorl	%esi, %esi
	callq	hipEventRecord
	movq	8(%rsp), %rdi
	callq	hipEventSynchronize
	movq	88(%rsp), %rsi
	movq	8(%rsp), %rdx
	leaq	84(%rsp), %rdi
	callq	hipEventElapsedTime
	movq	56(%rsp), %rdi
	movq	96(%rsp), %rsi                  # 8-byte Reload
                                        # kill: def $esi killed $esi killed $rsi
	callq	_Z12verifyResultPfj
	movq	88(%rsp), %rdi
	callq	hipEventDestroy
	movq	8(%rsp), %rdi
	callq	hipEventDestroy
.LBB3_12:                               # =>This Inner Loop Header: Depth=1
	movq	(%r12,%rbx,8), %rdi
	callq	hipStreamDestroy
	incq	%rbx
	cmpq	%rbx, %r15
	jne	.LBB3_12
# %bb.13:
	movq	%r12, %rdi
	callq	free
	movq	48(%rsp), %rdi
	callq	hipFree
	movq	40(%rsp), %rdi
	callq	hipFree
	movq	32(%rsp), %rdi
	callq	hipFree
	movq	72(%rsp), %rdi
	callq	hipHostFree
	movq	64(%rsp), %rdi
	callq	hipHostFree
	movq	56(%rsp), %rdi
	callq	hipHostFree
	movss	84(%rsp), %xmm0                 # xmm0 = mem[0],zero,zero,zero
	addq	$104, %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
.Lfunc_end3:
	.size	_Z20runStreamedArrayMultjjj, .Lfunc_end3-_Z20runStreamedArrayMultjjj
	.cfi_endproc
                                        # -- End function
	.globl	main                            # -- Begin function main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%r15
	.cfi_def_cfa_offset 24
	pushq	%r14
	.cfi_def_cfa_offset 32
	pushq	%r12
	.cfi_def_cfa_offset 40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movl	$1, %ebx
	movl	$512, %r14d                     # imm = 0x200
	movl	$256, %ebp                      # imm = 0x100
	cmpl	$2, %edi
	jl	.LBB4_4
# %bb.1:
	movq	%rsi, %r15
	movl	%edi, %r12d
	movq	8(%rsi), %rdi
	callq	atoi
	movl	%eax, %r14d
	cmpl	$2, %r12d
	je	.LBB4_4
# %bb.2:
	movq	16(%r15), %rdi
	callq	atoi
	movl	%eax, %ebp
	cmpl	$4, %r12d
	jb	.LBB4_4
# %bb.3:
	movq	24(%r15), %rdi
	callq	atoi
	movl	%eax, %ebx
.LBB4_4:                                # %.thread17
	movl	%r14d, %edi
	movl	%ebp, %esi
	movl	%ebx, %edx
	callq	_Z20runStreamedArrayMultjjj
	movl	%r14d, %edi
	movl	%ebp, %esi
	movl	%ebx, %edx
	callq	_Z20runStreamedArrayMultjjj
	cvtss2sd	%xmm0, %xmm0
	movl	$.L.str.1, %edi
	movl	%ebx, %esi
	movb	$1, %al
	callq	printf
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%r12
	.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
.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	$_Z9arrayMultPKfS0_Pfj, %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	_Z9arrayMultPKfS0_Pfj,@object   # @_Z9arrayMultPKfS0_Pfj
	.section	.rodata,"a",@progbits
	.globl	_Z9arrayMultPKfS0_Pfj
	.p2align	3, 0x0
_Z9arrayMultPKfS0_Pfj:
	.quad	_Z24__device_stub__arrayMultPKfS0_Pfj
	.size	_Z9arrayMultPKfS0_Pfj, 8

	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"Error! a[%d] != %f\n"
	.size	.L.str, 20

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"GPU execution time (%d streams): %.3f ms\n"
	.size	.L.str.1, 42

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z9arrayMultPKfS0_Pfj"
	.size	.L__unnamed_1, 22

	.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

	.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 _Z24__device_stub__arrayMultPKfS0_Pfj
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z9arrayMultPKfS0_Pfj
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z9arrayMultPKfS0_Pfj   ; -- Begin function _Z9arrayMultPKfS0_Pfj
	.globl	_Z9arrayMultPKfS0_Pfj
	.p2align	8
	.type	_Z9arrayMultPKfS0_Pfj,@function
_Z9arrayMultPKfS0_Pfj:                  ; @_Z9arrayMultPKfS0_Pfj
; %bb.0:
	s_clause 0x1
	s_load_b32 s2, s[0:1], 0x2c
	s_load_b32 s3, s[0:1], 0x18
	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[1:2], null, s15, s2, v[0:1]
	s_mov_b32 s2, exec_lo
	v_cmpx_gt_u32_e64 s3, v1
	s_cbranch_execz .LBB0_2
; %bb.1:
	s_load_b128 s[4:7], s[0:1], 0x0
	v_mov_b32_e32 v2, 0
	s_load_b64 s[0:1], s[0:1], 0x10
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_1)
	v_lshlrev_b64 v[0:1], 2, v[1:2]
	s_waitcnt lgkmcnt(0)
	v_add_co_u32 v2, vcc_lo, s4, v0
	s_delay_alu instid0(VALU_DEP_2)
	v_add_co_ci_u32_e32 v3, vcc_lo, s5, v1, vcc_lo
	v_add_co_u32 v4, vcc_lo, s6, v0
	v_add_co_ci_u32_e32 v5, vcc_lo, s7, v1, vcc_lo
	v_add_co_u32 v0, vcc_lo, s0, v0
	global_load_b32 v2, v[2:3], off
	global_load_b32 v3, v[4:5], off
	v_add_co_ci_u32_e32 v1, vcc_lo, s1, v1, vcc_lo
	s_waitcnt vmcnt(0)
	v_mul_f32_e32 v2, v2, v3
	global_store_b32 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 _Z9arrayMultPKfS0_Pfj
		.amdhsa_group_segment_fixed_size 0
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 288
		.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 6
		.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	_Z9arrayMultPKfS0_Pfj, .Lfunc_end0-_Z9arrayMultPKfS0_Pfj
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 180
; NumSgprs: 18
; NumVgprs: 6
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 0
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 6
; 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
      - .address_space:  global
        .offset:         16
        .size:           8
        .value_kind:     global_buffer
      - .offset:         24
        .size:           4
        .value_kind:     by_value
      - .offset:         32
        .size:           4
        .value_kind:     hidden_block_count_x
      - .offset:         36
        .size:           4
        .value_kind:     hidden_block_count_y
      - .offset:         40
        .size:           4
        .value_kind:     hidden_block_count_z
      - .offset:         44
        .size:           2
        .value_kind:     hidden_group_size_x
      - .offset:         46
        .size:           2
        .value_kind:     hidden_group_size_y
      - .offset:         48
        .size:           2
        .value_kind:     hidden_group_size_z
      - .offset:         50
        .size:           2
        .value_kind:     hidden_remainder_x
      - .offset:         52
        .size:           2
        .value_kind:     hidden_remainder_y
      - .offset:         54
        .size:           2
        .value_kind:     hidden_remainder_z
      - .offset:         72
        .size:           8
        .value_kind:     hidden_global_offset_x
      - .offset:         80
        .size:           8
        .value_kind:     hidden_global_offset_y
      - .offset:         88
        .size:           8
        .value_kind:     hidden_global_offset_z
      - .offset:         96
        .size:           2
        .value_kind:     hidden_grid_dims
    .group_segment_fixed_size: 0
    .kernarg_segment_align: 8
    .kernarg_segment_size: 288
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z9arrayMultPKfS0_Pfj
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z9arrayMultPKfS0_Pfj.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     6
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
