#include "compare.cuh"
__global__ void forward_kernel(float *y, const float *x, const float *k, const int B, const int M, const int C, const int H, const int W, const int K)
{

	/*
	The goal here is to be correct AND fast.
	We have some nice #defs for you below to simplify indexing. Feel free to use them, or create your own.
	*/


	// An example use of these macros:
	// float a = y4d(0,0,0,0)
	// y4d(0,0,0,0) = a
#define y4d(i3, i2, i1, i0) y[(i3) * (M * H_out * W_out) + (i2) * (H_out * W_out) + (i1) * (W_out) + i0]
#define x4d(i3, i2, i1, i0) x[(i3) * (C * H * W) + (i2) * (H * W) + (i1) * (W) + i0]
#define k4d(i3, i2, i1, i0) k[(i3) * (C * K * K) + (i2) * (K * K) + (i1) * (K) + i0]

	int b = blockDim.x * blockIdx.x + threadIdx.x;
	const int H_out = H - K + 1;
	const int W_out = W - K + 1;

	if (b < B) // for each image in the batch
	{
		for (int m = 0; m < M; m++)         // for each output feature maps
			for (int h = 0; h < H_out; h++) // for each output element
				for (int w = 0; w < W_out; w++)
				{
					y4d(b, m, h, w) = 0;
					for (int c = 0; c < C; c++)     // sum over all input feature maps
						for (int p = 0; p < K; p++) // KxK filter
							for (int q = 0; q < K; q++)
								y4d(b, m, h, w) += x4d(b, c, h + p, w + q) * k4d(m, c, p, q);
				}
	}

#undef y4d
#undef x4d
#undef k4d
}

__global__ void unrollKernel(int C, int H, int W, int K, float* X, float* X_out)
{
	int tid = blockIdx.x*blockDim.x + threadIdx.x;
	int H_out = H - K + 1;
	int W_out = W - K + 1;
	int W_unroll = H_out*W_out;
	int c, s, h_out, w_out, h_unroll, w_unroll, w_base, k1, k2;


	if (tid < C*W_unroll) {
		c = tid / W_unroll;
		s = tid % W_unroll;
		h_out = s / W_out;
		w_out = s % W_out;
		h_unroll = h_out*W_out + w_out;
		w_base = c*K*K;
		for (k1 = 0; k1 < K; k1++) {
			for (k2 = 0; k2 < K; k2++) {
				w_unroll = w_base + k1*K + k2;
				X_out[w_unroll*W_unroll + h_unroll] = X[c*H*W + (h_out + k1)*W + w_out + k2];
			}
		}
	}

}

#define TILE_WIDTH 32
#define TILE_WIDTH_FLOAT 32.0

__global__ void matrixMultiplyShared(float *A, float *B, float *C,
	int numARows, int numAColumns,
	int numBRows, int numBColumns,
	int numCRows, int numCColumns) {

	__shared__ float subTileA[TILE_WIDTH][TILE_WIDTH];
	__shared__ float subTileB[TILE_WIDTH][TILE_WIDTH];

	int row = blockIdx.y * TILE_WIDTH + threadIdx.y;
	int col = blockIdx.x * TILE_WIDTH + threadIdx.x;

	float multVal = 0;

	//printf("%d %d\n", numAColumns, numAColumns/16);

	//if((row * numCColumns + col) < (numCRows*numCColumns)) {

	//printf("row %d col %d \n", row, col);

		//compute the number of tiles needed 
	for (int i = 0; i < ceilf(numAColumns / TILE_WIDTH_FLOAT); i++) {

		//each thread loads its bit
		if (row * numAColumns + i * TILE_WIDTH + threadIdx.x < numARows*numAColumns) {
			subTileA[threadIdx.y][threadIdx.x] = A[row * numAColumns + i * TILE_WIDTH + threadIdx.x];
			//subTileB[threadIdx.y][threadIdx.x] = B[(i * TILE_WIDTH + threadIdx.y) * numBColumns + col];
			//printf("%d, %d, %d, %d, %f\n", row, i, threadIdx.y, threadIdx.x, A[row * numAColumns + i * TILE_WIDTH + threadIdx.x]);
		}
		if ((i * TILE_WIDTH + threadIdx.y) * numBColumns + col < numBRows*numBColumns) {
			subTileB[threadIdx.y][threadIdx.x] = B[(i * TILE_WIDTH + threadIdx.y) * numBColumns + col];
		}

		__syncthreads();	
		if (row < numCRows && col < numCColumns) {
			for (int j = 0; j < TILE_WIDTH; j++) {
				if (i*TILE_WIDTH + j < numAColumns) {
					//printf("%d, %d, %f, %f\n", threadIdx.y, threadIdx.x, subTileA[threadIdx.y][j], subTileB[j][threadIdx.x]);
					multVal += subTileA[threadIdx.y][j] * subTileB[j][threadIdx.x];
				}
			}
			C[row*numCColumns + col] = multVal;
		}
		__syncthreads();

	}
}


void forwardWithMatmul(float *y, const float *x, const float *k, const int B, const int M, const int C, const int H, const int W, const int K) {
	float* dev_y;
	float* dev_x;
	float* dev_k;
	float* dev_X_out;

	int H_out = H - K + 1;
	int W_out = W - K + 1;

	cudaMalloc((void**)&dev_x, (size_t)(B*H*W*C * sizeof(float)));
	cudaMalloc((void**)&dev_X_out, (size_t)((H - K + 1)*(W - K + 1)*K*K*C * sizeof(float)));
	cudaMalloc((void**)&dev_y, (size_t)(B*M*H_out*W_out * sizeof(float)));
	cudaMalloc((void**)&dev_k, (size_t)(M*C*K*K * sizeof(float)));

	cudaMemcpy(dev_x, x, (size_t)(B*H*W*C * sizeof(float)), cudaMemcpyHostToDevice);
	cudaMemcpy(dev_k, k, (size_t)(M*C*K*K * sizeof(float)), cudaMemcpyHostToDevice);

	//eventually make this do it in one shot
	dim3 unrollBlockDim(H*W*C, 1, 1);
	dim3 unrollGridDim(1, 1, 1);

	const int inner_dim = K*K*C;

	dim3 mulBlockDim(TILE_WIDTH, TILE_WIDTH, 1);
	dim3 mulGridDim(ceilf((H_out*W_out)/TILE_WIDTH_FLOAT), ceilf(M/TILE_WIDTH_FLOAT), 1);
	
	//for everything in batch
	for (int b = 0; b < B; b++) {
		//unroll
		unrollKernel <<< unrollGridDim, unrollBlockDim >>> (C, H, W, K, dev_x+b*H*W*C, dev_X_out);

		//multiply first by second
		matrixMultiplyShared <<< mulGridDim, mulBlockDim >>> (dev_k, dev_X_out, dev_y+b*M*H_out*W_out, M, inner_dim, inner_dim, H_out*W_out, M, H_out*W_out);
	}

	cudaMemcpy(y, dev_y, (size_t)(B*M*H_out*W_out * sizeof(float)), cudaMemcpyDeviceToHost);

	cudaFree(dev_y);
	cudaFree(dev_x);
	cudaFree(dev_k);
	cudaFree(dev_X_out);
}

void forwardWithLoop(float *y, const float *x, const float *k, const int B, const int M, const int C, const int H, const int W, const int K) {
	float* dev_y;
	float* dev_x;
	float* dev_k;

	int H_out = H - K + 1;
	int W_out = W - K + 1;

	cudaMalloc((void**)&dev_x, (size_t)(B*H*W*C * sizeof(float)));
	cudaMalloc((void**)&dev_y, (size_t)(B*M*H_out*W_out * sizeof(float)));
	cudaMalloc((void**)&dev_k, (size_t)(M*C*K*K * sizeof(float)));

	cudaMemcpy(dev_x, x, (size_t)(B*H*W*C * sizeof(float)), cudaMemcpyHostToDevice);
	cudaMemcpy(dev_k, k, (size_t)(M*C*K*K * sizeof(float)), cudaMemcpyHostToDevice);

	dim3 gridDim((B + 511) / 512);
	dim3 blockDim(512);


	forward_kernel << < gridDim, blockDim >> > (dev_y, dev_x, dev_k, B, M, C, H, W, K);

	cudaMemcpy(y, dev_y, (size_t)(B*M*H_out*W_out * sizeof(float)), cudaMemcpyDeviceToHost);

	cudaFree(dev_y);
	cudaFree(dev_x);
	cudaFree(dev_k);
}

	.file	"tmpxft_002c26a6_00000000-6_compare.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2031:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2031:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z46__device_stub__Z14forward_kernelPfPKfS1_iiiiiiPfPKfS1_iiiiii
	.type	_Z46__device_stub__Z14forward_kernelPfPKfS1_iiiiiiPfPKfS1_iiiiii, @function
_Z46__device_stub__Z14forward_kernelPfPKfS1_iiiiiiPfPKfS1_iiiiii:
.LFB2053:
	.cfi_startproc
	endbr64
	subq	$200, %rsp
	.cfi_def_cfa_offset 208
	movq	%rdi, 40(%rsp)
	leaq	64(%rsp), %rdi
	movq	%rsi, 32(%rsp)
	leaq	76(%rsp), %rsi
	movq	%rdx, 24(%rsp)
	leaq	48(%rsp), %rdx
	movl	%ecx, 20(%rsp)
	leaq	56(%rsp), %rcx
	movl	%r8d, 16(%rsp)
	movl	%r9d, 12(%rsp)
	movq	%fs:40, %rax
	movq	%rax, 184(%rsp)
	xorl	%eax, %eax
	leaq	40(%rsp), %rax
	movl	$1, 72(%rsp)
	movq	%rax, 112(%rsp)
	leaq	32(%rsp), %rax
	movq	%rax, 120(%rsp)
	leaq	24(%rsp), %rax
	movq	%rax, 128(%rsp)
	leaq	20(%rsp), %rax
	movq	%rax, 136(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 144(%rsp)
	leaq	12(%rsp), %rax
	movq	%rax, 152(%rsp)
	leaq	208(%rsp), %rax
	movq	%rax, 160(%rsp)
	leaq	216(%rsp), %rax
	movq	%rax, 168(%rsp)
	leaq	224(%rsp), %rax
	movq	%rax, 176(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 64(%rsp)
	movq	%rax, 76(%rsp)
	movl	$1, 84(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L2
	pushq	56(%rsp)
	.cfi_def_cfa_offset 216
	leaq	_Z14forward_kernelPfPKfS1_iiiiii(%rip), %rdi
	pushq	56(%rsp)
	.cfi_def_cfa_offset 224
	movq	92(%rsp), %rcx
	movl	100(%rsp), %r8d
	movq	80(%rsp), %rsi
	movl	88(%rsp), %edx
	leaq	128(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 216
	popq	%rdx
	.cfi_def_cfa_offset 208
.L2:
	movq	184(%rsp), %rax
	subq	%fs:40, %rax
	je	.L4
	call	__stack_chk_fail@PLT
.L4:
	addq	$200, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2053:
	.size	_Z46__device_stub__Z14forward_kernelPfPKfS1_iiiiiiPfPKfS1_iiiiii, .-_Z46__device_stub__Z14forward_kernelPfPKfS1_iiiiiiPfPKfS1_iiiiii
	.globl	_Z14forward_kernelPfPKfS1_iiiiii
	.type	_Z14forward_kernelPfPKfS1_iiiiii, @function
_Z14forward_kernelPfPKfS1_iiiiii:
.LFB2054:
	.cfi_startproc
	endbr64
	jmp	_Z46__device_stub__Z14forward_kernelPfPKfS1_iiiiiiPfPKfS1_iiiiii
	.cfi_endproc
.LFE2054:
	.size	_Z14forward_kernelPfPKfS1_iiiiii, .-_Z14forward_kernelPfPKfS1_iiiiii
	.globl	_Z15forwardWithLoopPfPKfS1_iiiiii
	.type	_Z15forwardWithLoopPfPKfS1_iiiiii, @function
_Z15forwardWithLoopPfPKfS1_iiiiii:
.LFB2028:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	movl	%r9d, %r12d
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	movl	%r8d, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	movl	%ecx, %ebx
	subq	$120, %rsp
	.cfi_def_cfa_offset 176
	movl	176(%rsp), %r14d
	movq	%rdx, 32(%rsp)
	movl	184(%rsp), %r15d
	movq	%rdi, 16(%rsp)
	leaq	64(%rsp), %rdi
	movl	%r14d, %edx
	movq	%rsi, 24(%rsp)
	movl	192(%rsp), %r13d
	imull	%ecx, %edx
	movq	%fs:40, %rax
	movq	%rax, 104(%rsp)
	xorl	%eax, %eax
	imull	%r15d, %edx
	imull	%r9d, %edx
	movslq	%edx, %rdx
	salq	$2, %rdx
	movq	%rdx, %rsi
	movq	%rdx, 40(%rsp)
	call	cudaMalloc@PLT
	movl	%ebx, %ecx
	movl	%r14d, %eax
	leaq	56(%rsp), %rdi
	imull	%ebp, %ecx
	subl	%r13d, %eax
	incl	%eax
	imull	%ecx, %eax
	movl	%r15d, %ecx
	subl	%r13d, %ecx
	incl	%ecx
	imull	%ecx, %eax
	cltq
	salq	$2, %rax
	movq	%rax, %rsi
	movq	%rax, (%rsp)
	call	cudaMalloc@PLT
	movl	%r12d, %eax
	leaq	72(%rsp), %rdi
	imull	%ebp, %eax
	imull	%r13d, %eax
	imull	%r13d, %eax
	cltq
	salq	$2, %rax
	movq	%rax, %rsi
	movq	%rax, 8(%rsp)
	call	cudaMalloc@PLT
	movq	40(%rsp), %rdx
	movq	24(%rsp), %rsi
	movl	$1, %ecx
	movq	64(%rsp), %rdi
	call	cudaMemcpy@PLT
	movq	8(%rsp), %rdx
	movq	32(%rsp), %rsi
	movl	$1, %ecx
	movq	72(%rsp), %rdi
	call	cudaMemcpy@PLT
	movl	$512, %ecx
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	leal	511(%rbx), %eax
	cltd
	idivl	%ecx
	movl	$8388609, %edx
	movl	$1, %ecx
	salq	$9, %rdx
	movl	%eax, 80(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 84(%rsp)
	movq	80(%rsp), %rdi
	movl	88(%rsp), %esi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L9
	pushq	%rax
	.cfi_def_cfa_offset 184
	movl	%r12d, %r9d
	movl	%ebp, %r8d
	movl	%ebx, %ecx
	pushq	%r13
	.cfi_def_cfa_offset 192
	pushq	%r15
	.cfi_def_cfa_offset 200
	pushq	%r14
	.cfi_def_cfa_offset 208
	movq	104(%rsp), %rdx
	movq	96(%rsp), %rsi
	movq	88(%rsp), %rdi
	call	_Z46__device_stub__Z14forward_kernelPfPKfS1_iiiiiiPfPKfS1_iiiiii
	addq	$32, %rsp
	.cfi_def_cfa_offset 176
.L9:
	movq	(%rsp), %rdx
	movq	56(%rsp), %rsi
	movl	$2, %ecx
	movq	16(%rsp), %rdi
	call	cudaMemcpy@PLT
	movq	56(%rsp), %rdi
	call	cudaFree@PLT
	movq	64(%rsp), %rdi
	call	cudaFree@PLT
	movq	72(%rsp), %rdi
	call	cudaFree@PLT
	movq	104(%rsp), %rax
	subq	%fs:40, %rax
	je	.L10
	call	__stack_chk_fail@PLT
.L10:
	addq	$120, %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
.LFE2028:
	.size	_Z15forwardWithLoopPfPKfS1_iiiiii, .-_Z15forwardWithLoopPfPKfS1_iiiiii
	.globl	_Z38__device_stub__Z12unrollKerneliiiiPfS_iiiiPfS_
	.type	_Z38__device_stub__Z12unrollKerneliiiiPfS_iiiiPfS_, @function
_Z38__device_stub__Z12unrollKerneliiiiPfS_iiiiPfS_:
.LFB2055:
	.cfi_startproc
	endbr64
	subq	$168, %rsp
	.cfi_def_cfa_offset 176
	movl	%edi, 28(%rsp)
	leaq	56(%rsp), %rdi
	movl	%esi, 24(%rsp)
	leaq	68(%rsp), %rsi
	movl	%edx, 20(%rsp)
	leaq	40(%rsp), %rdx
	movl	%ecx, 16(%rsp)
	leaq	48(%rsp), %rcx
	movq	%r8, 8(%rsp)
	movq	%r9, (%rsp)
	movq	%fs:40, %rax
	movq	%rax, 152(%rsp)
	xorl	%eax, %eax
	leaq	28(%rsp), %rax
	movl	$1, 64(%rsp)
	movq	%rax, 104(%rsp)
	leaq	24(%rsp), %rax
	movq	%rax, 112(%rsp)
	leaq	20(%rsp), %rax
	movq	%rax, 120(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 128(%rsp)
	leaq	8(%rsp), %rax
	movq	%rax, 136(%rsp)
	movq	%rsp, %rax
	movq	%rax, 144(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 56(%rsp)
	movq	%rax, 68(%rsp)
	movl	$1, 76(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L12
	pushq	48(%rsp)
	.cfi_def_cfa_offset 184
	leaq	_Z12unrollKerneliiiiPfS_(%rip), %rdi
	pushq	48(%rsp)
	.cfi_def_cfa_offset 192
	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 184
	popq	%rdx
	.cfi_def_cfa_offset 176
.L12:
	movq	152(%rsp), %rax
	subq	%fs:40, %rax
	je	.L14
	call	__stack_chk_fail@PLT
.L14:
	addq	$168, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2055:
	.size	_Z38__device_stub__Z12unrollKerneliiiiPfS_iiiiPfS_, .-_Z38__device_stub__Z12unrollKerneliiiiPfS_iiiiPfS_
	.globl	_Z12unrollKerneliiiiPfS_
	.type	_Z12unrollKerneliiiiPfS_, @function
_Z12unrollKerneliiiiPfS_:
.LFB2056:
	.cfi_startproc
	endbr64
	jmp	_Z38__device_stub__Z12unrollKerneliiiiPfS_iiiiPfS_
	.cfi_endproc
.LFE2056:
	.size	_Z12unrollKerneliiiiPfS_, .-_Z12unrollKerneliiiiPfS_
	.globl	_Z50__device_stub__Z20matrixMultiplySharedPfS_S_iiiiiiPfS_S_iiiiii
	.type	_Z50__device_stub__Z20matrixMultiplySharedPfS_S_iiiiiiPfS_S_iiiiii, @function
_Z50__device_stub__Z20matrixMultiplySharedPfS_S_iiiiiiPfS_S_iiiiii:
.LFB2057:
	.cfi_startproc
	endbr64
	subq	$200, %rsp
	.cfi_def_cfa_offset 208
	movq	%rdi, 40(%rsp)
	leaq	64(%rsp), %rdi
	movq	%rsi, 32(%rsp)
	leaq	76(%rsp), %rsi
	movq	%rdx, 24(%rsp)
	leaq	48(%rsp), %rdx
	movl	%ecx, 20(%rsp)
	leaq	56(%rsp), %rcx
	movl	%r8d, 16(%rsp)
	movl	%r9d, 12(%rsp)
	movq	%fs:40, %rax
	movq	%rax, 184(%rsp)
	xorl	%eax, %eax
	leaq	40(%rsp), %rax
	movl	$1, 72(%rsp)
	movq	%rax, 112(%rsp)
	leaq	32(%rsp), %rax
	movq	%rax, 120(%rsp)
	leaq	24(%rsp), %rax
	movq	%rax, 128(%rsp)
	leaq	20(%rsp), %rax
	movq	%rax, 136(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 144(%rsp)
	leaq	12(%rsp), %rax
	movq	%rax, 152(%rsp)
	leaq	208(%rsp), %rax
	movq	%rax, 160(%rsp)
	leaq	216(%rsp), %rax
	movq	%rax, 168(%rsp)
	leaq	224(%rsp), %rax
	movq	%rax, 176(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 64(%rsp)
	movq	%rax, 76(%rsp)
	movl	$1, 84(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L17
	pushq	56(%rsp)
	.cfi_def_cfa_offset 216
	leaq	_Z20matrixMultiplySharedPfS_S_iiiiii(%rip), %rdi
	pushq	56(%rsp)
	.cfi_def_cfa_offset 224
	movq	92(%rsp), %rcx
	movl	100(%rsp), %r8d
	movq	80(%rsp), %rsi
	movl	88(%rsp), %edx
	leaq	128(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 216
	popq	%rdx
	.cfi_def_cfa_offset 208
.L17:
	movq	184(%rsp), %rax
	subq	%fs:40, %rax
	je	.L19
	call	__stack_chk_fail@PLT
.L19:
	addq	$200, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2057:
	.size	_Z50__device_stub__Z20matrixMultiplySharedPfS_S_iiiiiiPfS_S_iiiiii, .-_Z50__device_stub__Z20matrixMultiplySharedPfS_S_iiiiiiPfS_S_iiiiii
	.globl	_Z20matrixMultiplySharedPfS_S_iiiiii
	.type	_Z20matrixMultiplySharedPfS_S_iiiiii, @function
_Z20matrixMultiplySharedPfS_S_iiiiii:
.LFB2058:
	.cfi_startproc
	endbr64
	jmp	_Z50__device_stub__Z20matrixMultiplySharedPfS_S_iiiiiiPfS_S_iiiiii
	.cfi_endproc
.LFE2058:
	.size	_Z20matrixMultiplySharedPfS_S_iiiiii, .-_Z20matrixMultiplySharedPfS_S_iiiiii
	.globl	_Z17forwardWithMatmulPfPKfS1_iiiiii
	.type	_Z17forwardWithMatmulPfPKfS1_iiiiii, @function
_Z17forwardWithMatmulPfPKfS1_iiiiii:
.LFB2027:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	movl	%r8d, %r12d
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	movl	%r9d, %ebx
	subq	$168, %rsp
	.cfi_def_cfa_offset 224
	movq	%rdx, 40(%rsp)
	movl	224(%rsp), %edx
	movl	240(%rsp), %ebp
	movl	224(%rsp), %r14d
	movl	%ecx, 12(%rsp)
	imull	%ecx, %edx
	movl	232(%rsp), %r13d
	movq	%rdi, 56(%rsp)
	leaq	80(%rsp), %rdi
	imull	232(%rsp), %edx
	subl	%ebp, %r14d
	movq	%rsi, 32(%rsp)
	incl	%r14d
	subl	%ebp, %r13d
	incl	%r13d
	movl	%r14d, %r15d
	movq	%fs:40, %rax
	movq	%rax, 152(%rsp)
	xorl	%eax, %eax
	imull	%r9d, %edx
	imull	%r13d, %r15d
	movslq	%edx, %rdx
	salq	$2, %rdx
	movq	%rdx, %rsi
	movq	%rdx, 48(%rsp)
	call	cudaMalloc@PLT
	movl	%r15d, %esi
	leaq	96(%rsp), %rdi
	imull	%ebp, %esi
	imull	%ebp, %esi
	imull	%ebx, %esi
	movslq	%esi, %rsi
	salq	$2, %rsi
	call	cudaMalloc@PLT
	movl	12(%rsp), %eax
	leaq	72(%rsp), %rdi
	imull	%r12d, %eax
	imull	%r14d, %eax
	imull	%r13d, %eax
	cltq
	salq	$2, %rax
	movq	%rax, %rsi
	movq	%rax, 16(%rsp)
	call	cudaMalloc@PLT
	movl	%ebx, %eax
	leaq	88(%rsp), %rdi
	imull	%r12d, %eax
	imull	%ebp, %eax
	imull	%ebp, %eax
	cltq
	salq	$2, %rax
	movq	%rax, %rsi
	movq	%rax, 24(%rsp)
	call	cudaMalloc@PLT
	movq	48(%rsp), %rdx
	movq	32(%rsp), %rsi
	movl	$1, %ecx
	movq	80(%rsp), %rdi
	call	cudaMemcpy@PLT
	movq	24(%rsp), %rdx
	movq	40(%rsp), %rsi
	movl	$1, %ecx
	movq	88(%rsp), %rdi
	call	cudaMemcpy@PLT
	movl	224(%rsp), %eax
	imull	232(%rsp), %eax
	movl	$1, 124(%rsp)
	movl	$1, 136(%rsp)
	cvtsi2sdl	%r12d, %xmm0
	mulsd	.LC0(%rip), %xmm0
	imull	%ebx, %eax
	movl	%eax, 104(%rsp)
	cvtsd2ss	%xmm0, %xmm0
	movabsq	$4294967297, %rax
	movq	%rax, 108(%rsp)
	movq	%rax, 116(%rsp)
	movl	%ebp, %eax
	imull	%ebp, %eax
	imull	%ebx, %eax
	imull	%r12d, %r13d
	movl	%eax, 32(%rsp)
	movabsq	$137438953504, %rax
	movq	%rax, 128(%rsp)
	imull	%r14d, %r13d
	xorl	%r14d, %r14d
	call	ceilf@PLT
	movss	%xmm0, 24(%rsp)
	cvtsi2sdl	%r15d, %xmm0
	mulsd	.LC0(%rip), %xmm0
	cvtsd2ss	%xmm0, %xmm0
	call	ceilf@PLT
	xorl	%edx, %edx
	xorl	%ecx, %ecx
	cvttss2siq	%xmm0, %rax
	movss	24(%rsp), %xmm1
	movl	%edx, 40(%rsp)
	movl	$1, 148(%rsp)
	movl	%ecx, 24(%rsp)
	movl	%eax, 140(%rsp)
	cvttss2siq	%xmm1, %rax
	movl	%eax, 144(%rsp)
	movl	232(%rsp), %eax
	imull	%ebx, %eax
	imull	224(%rsp), %eax
	movl	%eax, 48(%rsp)
.L23:
	movl	12(%rsp), %ecx
	cmpl	%ecx, 24(%rsp)
	jge	.L29
	movl	112(%rsp), %ecx
	movq	104(%rsp), %rdx
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movq	116(%rsp), %rdi
	movl	124(%rsp), %esi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L24
	movq	80(%rsp), %rdx
	movslq	40(%rsp), %rax
	movl	%ebp, %ecx
	movl	%ebx, %edi
	movq	96(%rsp), %r9
	movl	224(%rsp), %esi
	leaq	(%rdx,%rax,4), %r8
	movl	232(%rsp), %edx
	call	_Z38__device_stub__Z12unrollKerneliiiiPfS_iiiiPfS_
.L24:
	movl	136(%rsp), %ecx
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movq	128(%rsp), %rdx
	movq	140(%rsp), %rdi
	movl	148(%rsp), %esi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L25
	movq	72(%rsp), %rdx
	movslq	%r14d, %rax
	movl	%r12d, %ecx
	leaq	(%rdx,%rax,4), %rdx
	pushq	%rax
	.cfi_def_cfa_offset 232
	pushq	%r15
	.cfi_def_cfa_offset 240
	pushq	%r12
	.cfi_def_cfa_offset 248
	pushq	%r15
	.cfi_def_cfa_offset 256
	movl	64(%rsp), %r9d
	movq	128(%rsp), %rsi
	movq	120(%rsp), %rdi
	movl	%r9d, %r8d
	call	_Z50__device_stub__Z20matrixMultiplySharedPfS_S_iiiiiiPfS_S_iiiiii
	addq	$32, %rsp
	.cfi_def_cfa_offset 224
.L25:
	movl	48(%rsp), %ecx
	incl	24(%rsp)
	addl	%r13d, %r14d
	addl	%ecx, 40(%rsp)
	jmp	.L23
.L29:
	movq	16(%rsp), %rdx
	movq	72(%rsp), %rsi
	movl	$2, %ecx
	movq	56(%rsp), %rdi
	call	cudaMemcpy@PLT
	movq	72(%rsp), %rdi
	call	cudaFree@PLT
	movq	80(%rsp), %rdi
	call	cudaFree@PLT
	movq	88(%rsp), %rdi
	call	cudaFree@PLT
	movq	96(%rsp), %rdi
	call	cudaFree@PLT
	movq	152(%rsp), %rax
	subq	%fs:40, %rax
	je	.L27
	call	__stack_chk_fail@PLT
.L27:
	addq	$168, %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
.LFE2027:
	.size	_Z17forwardWithMatmulPfPKfS1_iiiiii, .-_Z17forwardWithMatmulPfPKfS1_iiiiii
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC1:
	.string	"_Z20matrixMultiplySharedPfS_S_iiiiii"
.LC2:
	.string	"_Z12unrollKerneliiiiPfS_"
.LC3:
	.string	"_Z14forward_kernelPfPKfS1_iiiiii"
	.section	.text.startup,"ax",@progbits
	.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	.LC1(%rip), %rdx
	movq	%rax, %rdi
	movq	%rax, %rbx
	pushq	$0
	.cfi_def_cfa_offset 40
	movq	%rdx, %rcx
	leaq	_Z20matrixMultiplySharedPfS_S_iiiiii(%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	.LC2(%rip), %rdx
	orl	$-1, %r8d
	leaq	_Z12unrollKerneliiiiPfS_(%rip), %rsi
	pushq	$0
	.cfi_def_cfa_offset 32
	movq	%rdx, %rcx
	pushq	$0
	.cfi_def_cfa_offset 40
	pushq	$0
	.cfi_def_cfa_offset 48
	call	__cudaRegisterFunction@PLT
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	movq	%rbx, %rdi
	xorl	%r9d, %r9d
	pushq	$0
	.cfi_def_cfa_offset 24
	leaq	.LC3(%rip), %rdx
	orl	$-1, %r8d
	leaq	_Z14forward_kernelPfPKfS1_iiiiii(%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
	.section	.rodata.cst8,"aM",@progbits,8
	.align 8
.LC0:
	.long	0
	.long	1067450368
	.ident	"GCC: (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:

#ifndef COMPARE_CUH
#define COMPARE_CUH

#include "hip/hip_runtime.h"

#include "hip/device_functions.h"
#include <math.h>

#include <stdio.h>

void forwardWithMatmul(float *y, const float *x, const float *k, const int B, const int M, const int C, const int H, const int W, const int K);
void forwardWithLoop(float *y, const float *x, const float *k, const int B, const int M, const int C, const int H, const int W, const int K);

#endif

	.text
	.file	"compare.hip"
	.type	__hip_cuid_,@object             # @__hip_cuid_
	.bss
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               # 0x0
	.size	__hip_cuid_, 1

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

	.text
	.p2alignl 7, 3214868480
	.fill 96, 4, 3214868480
	.type	__hip_cuid_,@object             ; @__hip_cuid_
	.section	.bss,"aw",@nobits
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               ; 0x0
	.size	__hip_cuid_, 1

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym __hip_cuid_
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.amdgpu_metadata
---
amdhsa.kernels:  []
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
