#include <string.h>
#include <stdlib.h>
#include <stdio.h>
//CUDA RunTime API
#include <cuda_runtime.h>
#include <time.h>

#define THREAD_NUM 1024
#define BLOCK_NUM 16
#define DATA_SIZE 1048576

// __global__ 函数（GPU上执行） 计算立方和
__global__ static void sumOfSquares(int *num, int* result, clock_t* time)
{
	extern __shared__ int shared[];

	const int tid = threadIdx.x;

	const int bid = blockIdx.x;

	shared[tid] = 0;

	int i;

	if(tid == 0)
		time[bid] = clock();

	for(i = bid * THREAD_NUM + tid; i < DATA_SIZE; i+= BLOCK_NUM * THREAD_NUM){
		
		shared[tid] += num[i] * num[i] * num[i];

	}
	//第一个同步的意思是确保所有的线程完成了立方和的操作
	__syncthreads();

	int offset = 1, mask = 1;

	while(offset < THREAD_NUM){
		//由于为二进制，逢2进一，因此进行当两个相差2的数进行&操作时势必为0
		if((tid & mask) == 0){
			shared[tid] = shared[tid + offset];
		}

		offset += offset;
		mask = offset + mask;
		//第二个同步的意思是确保每次的累加完成，这样才能进入下一次的累加操作
		__syncthreads();
	}

	if(tid == 0){
		result[bid] = shared[0];
		time[bid + BLOCK_NUM] = clock();
	}
}

void printDeviceProp(const cudaDeviceProp &prop)
{
	printf("Device Name : %s.\n", prop.name);
	printf("totalGlobalMem : %d.\n", prop.totalGlobalMem);
	printf("shareMemPredBLock : %d.\n", prop.sharedMemPerBlock);
	printf("regsPerBlock : %d.\n", prop.regsPerBlock);
	printf("warpSize : %d.\n", prop.warpSize);
	printf("memPitch : %d.\n", prop.memPitch);
	printf("maxThreadsPerBlock : %d.\n", prop.maxThreadsPerBlock);
	printf("maxThreadsDim[0 - 2] : %d %d %d.\n", prop.maxThreadsDim[0], prop.maxThreadsDim[1], prop.maxThreadsDim[2]);
	printf("maxGridSize[0 - 2] : %d %d %d.\n", prop.maxGridSize[0], prop.maxGridSize[1], prop.maxGridSize[2]);
	printf("totalConstMem : %d.\n", prop.totalConstMem);
	printf("major.minor : %d.%d\n", prop.major, prop.minor);
	printf("clockRate : %d.\n", prop.clockRate);
	printf("textureAlignment : %d\n", prop.textureAlignment);
	printf("deviceOverlap : %d.\n", prop.deviceOverlap);
	printf("multiProcessorCount : %d.\n", prop.multiProcessorCount);
}

int data[DATA_SIZE];

//产生大量0-9之间的随机数
void GenerateNumbers(int *number, int size)
{
	for(int i = 0; i < size; i++){
		number[i] = rand() % 10;
	}
}

//CUDA初始化
bool InitCUDA()
{
	int count;

	//取得cuda的装置的数目
	cudaGetDeviceCount(&count);

	if(count == 0) {
		fprintf(stderr, "There is no device.\n");
		return false;
	}
	
	int i;
	
	for(i = 0; i < count; i++){
		cudaDeviceProp prop;
		cudaGetDeviceProperties(&prop, i);
		//打印设备信息
		printDeviceProp(prop);
		if(cudaGetDeviceProperties(&prop, i) == cudaSuccess){
			if(prop.major >= 1){
				break;
			}
		}
	}
	if(i == count){
		fprintf(stderr, "There is no device supporting CUDA 1.x.\n");
		return false;
	}

	cudaSetDevice(i);
	return true;
}



int main()
{
	if (!InitCUDA()){
		return 0;
	}

	printf("CUDA initialized.\n");
	//生成随机数
	GenerateNumbers(data, DATA_SIZE);

	/*吧数据复制到显卡内部*/

	int* gpudata, *result;
	clock_t* time;

	//cudaMalloc取得一块显存内存（其中result存储结果)
	cudaMalloc((void**)&gpudata, sizeof(int) * DATA_SIZE);
	cudaMalloc((void**)&result, sizeof(int) * BLOCK_NUM);
	cudaMalloc((void**)&time, sizeof(clock_t) * BLOCK_NUM * 2);

	//cudaMemcpy 将产生的随机数复制到显存内存中
	//cudaMemcpyHostToDevice - 从内存复制到显存
	//cudaMemcpyDeviceToHost - 从显存复制到内存
	cudaMemcpy(gpudata, data, sizeof(int) * DATA_SIZE, cudaMemcpyHostToDevice);

	sumOfSquares<<<BLOCK_NUM, THREAD_NUM, THREAD_NUM * sizeof(int)>>>(gpudata, result, time);

	int sum[BLOCK_NUM];
	clock_t time_used[BLOCK_NUM * 2];

	//cudaMemcpy 将结果从显存中复制回内存
	cudaMemcpy(&sum, result, sizeof(int) * BLOCK_NUM, cudaMemcpyDeviceToHost);
	cudaMemcpy(&time_used, time, sizeof(clock_t) * BLOCK_NUM * 2 ,cudaMemcpyDeviceToHost);
	int final_sum = 0;
	for(int i = 0; i < BLOCK_NUM; i++){
		final_sum += sum[i];
	}
	//Free
	cudaFree(gpudata);
	cudaFree(result);
	cudaFree(time);
	clock_t min_start, max_end;
	min_start = time_used[0];
	max_end = time_used[BLOCK_NUM];

	for(int i = 1; i < BLOCK_NUM; i++){
		if(min_start > time_used[i])
			min_start = time_used[i];
		if(max_end < time_used[i + BLOCK_NUM])
			max_end = time_used[i + BLOCK_NUM];
	}
	printf("GPUsum: %d gputime: %d\n", final_sum, max_end - min_start) ;
	final_sum = 0;
	for(int i = 0; i < DATA_SIZE; i++){
		final_sum += data[i] * data[i] * data[i];
	}

	printf("CPUsum: %d \n", final_sum);
	return 0;
}

	.file	"tmpxft_00354906_00000000-6_initCuda.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
	.type	_ZL36__device_stub__Z12sumOfSquaresPiS_PlPiS_Pl, @function
_ZL36__device_stub__Z12sumOfSquaresPiS_PlPiS_Pl:
.LFB2055:
	.cfi_startproc
	subq	$136, %rsp
	.cfi_def_cfa_offset 144
	movq	%rdi, 24(%rsp)
	leaq	40(%rsp), %rcx
	leaq	48(%rsp), %rdi
	movq	%rsi, 16(%rsp)
	leaq	60(%rsp), %rsi
	movq	%rdx, 8(%rsp)
	leaq	32(%rsp), %rdx
	movq	%fs:40, %rax
	movq	%rax, 120(%rsp)
	xorl	%eax, %eax
	leaq	24(%rsp), %rax
	movl	$1, 56(%rsp)
	movq	%rax, 96(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 104(%rsp)
	leaq	8(%rsp), %rax
	movq	%rax, 112(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 48(%rsp)
	movq	%rax, 60(%rsp)
	movl	$1, 68(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L2
	pushq	40(%rsp)
	.cfi_def_cfa_offset 152
	leaq	_ZL12sumOfSquaresPiS_Pl(%rip), %rdi
	pushq	40(%rsp)
	.cfi_def_cfa_offset 160
	movq	76(%rsp), %rcx
	movl	84(%rsp), %r8d
	movq	64(%rsp), %rsi
	movl	72(%rsp), %edx
	leaq	112(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 152
	popq	%rdx
	.cfi_def_cfa_offset 144
.L2:
	movq	120(%rsp), %rax
	subq	%fs:40, %rax
	je	.L4
	call	__stack_chk_fail@PLT
.L4:
	addq	$136, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2055:
	.size	_ZL36__device_stub__Z12sumOfSquaresPiS_PlPiS_Pl, .-_ZL36__device_stub__Z12sumOfSquaresPiS_PlPiS_Pl
	.type	_ZL12sumOfSquaresPiS_Pl, @function
_ZL12sumOfSquaresPiS_Pl:
.LFB2056:
	.cfi_startproc
	endbr64
	jmp	_ZL36__device_stub__Z12sumOfSquaresPiS_PlPiS_Pl
	.cfi_endproc
.LFE2056:
	.size	_ZL12sumOfSquaresPiS_Pl, .-_ZL12sumOfSquaresPiS_Pl
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"Device Name : %s.\n"
.LC1:
	.string	"totalGlobalMem : %d.\n"
.LC2:
	.string	"shareMemPredBLock : %d.\n"
.LC3:
	.string	"regsPerBlock : %d.\n"
.LC4:
	.string	"warpSize : %d.\n"
.LC5:
	.string	"memPitch : %d.\n"
.LC6:
	.string	"maxThreadsPerBlock : %d.\n"
.LC7:
	.string	"maxThreadsDim[0 - 2] : %d %d %d.\n"
.LC8:
	.string	"maxGridSize[0 - 2] : %d %d %d.\n"
.LC9:
	.string	"totalConstMem : %d.\n"
.LC10:
	.string	"major.minor : %d.%d\n"
.LC11:
	.string	"clockRate : %d.\n"
.LC12:
	.string	"textureAlignment : %d\n"
.LC13:
	.string	"deviceOverlap : %d.\n"
.LC14:
	.string	"multiProcessorCount : %d.\n"
	.text
	.globl	_Z15printDevicePropRK14cudaDeviceProp
	.type	_Z15printDevicePropRK14cudaDeviceProp, @function
_Z15printDevicePropRK14cudaDeviceProp:
.LFB2027:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	movq	%rdi, %rdx
	movq	%rdi, %rbx
	leaq	.LC0(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movq	288(%rbx), %rdx
	leaq	.LC1(%rip), %rsi
	xorl	%eax, %eax
	movl	$2, %edi
	call	__printf_chk@PLT
	movq	296(%rbx), %rdx
	leaq	.LC2(%rip), %rsi
	xorl	%eax, %eax
	movl	$2, %edi
	call	__printf_chk@PLT
	movl	304(%rbx), %edx
	leaq	.LC3(%rip), %rsi
	xorl	%eax, %eax
	movl	$2, %edi
	call	__printf_chk@PLT
	movl	308(%rbx), %edx
	leaq	.LC4(%rip), %rsi
	xorl	%eax, %eax
	movl	$2, %edi
	call	__printf_chk@PLT
	movq	312(%rbx), %rdx
	leaq	.LC5(%rip), %rsi
	xorl	%eax, %eax
	movl	$2, %edi
	call	__printf_chk@PLT
	movl	320(%rbx), %edx
	leaq	.LC6(%rip), %rsi
	xorl	%eax, %eax
	movl	$2, %edi
	call	__printf_chk@PLT
	movl	328(%rbx), %ecx
	xorl	%eax, %eax
	movl	324(%rbx), %edx
	movl	332(%rbx), %r8d
	leaq	.LC7(%rip), %rsi
	movl	$2, %edi
	call	__printf_chk@PLT
	movl	344(%rbx), %r8d
	xorl	%eax, %eax
	movl	340(%rbx), %ecx
	movl	336(%rbx), %edx
	leaq	.LC8(%rip), %rsi
	movl	$2, %edi
	call	__printf_chk@PLT
	movq	352(%rbx), %rdx
	leaq	.LC9(%rip), %rsi
	xorl	%eax, %eax
	movl	$2, %edi
	call	__printf_chk@PLT
	movl	364(%rbx), %ecx
	movl	360(%rbx), %edx
	xorl	%eax, %eax
	leaq	.LC10(%rip), %rsi
	movl	$2, %edi
	call	__printf_chk@PLT
	movl	348(%rbx), %edx
	leaq	.LC11(%rip), %rsi
	xorl	%eax, %eax
	movl	$2, %edi
	call	__printf_chk@PLT
	movq	368(%rbx), %rdx
	leaq	.LC12(%rip), %rsi
	xorl	%eax, %eax
	movl	$2, %edi
	call	__printf_chk@PLT
	movl	384(%rbx), %edx
	leaq	.LC13(%rip), %rsi
	xorl	%eax, %eax
	movl	$2, %edi
	call	__printf_chk@PLT
	movl	388(%rbx), %edx
	leaq	.LC14(%rip), %rsi
	xorl	%eax, %eax
	movl	$2, %edi
	popq	%rbx
	.cfi_def_cfa_offset 8
	jmp	__printf_chk@PLT
	.cfi_endproc
.LFE2027:
	.size	_Z15printDevicePropRK14cudaDeviceProp, .-_Z15printDevicePropRK14cudaDeviceProp
	.globl	_Z15GenerateNumbersPii
	.type	_Z15GenerateNumbersPii, @function
_Z15GenerateNumbersPii:
.LFB2028:
	.cfi_startproc
	endbr64
	pushq	%r13
	.cfi_def_cfa_offset 16
	.cfi_offset 13, -16
	movl	$10, %r13d
	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
.L11:
	cmpl	%ebx, %ebp
	jle	.L14
	call	rand@PLT
	cltd
	idivl	%r13d
	movl	%edx, (%r12,%rbx,4)
	incq	%rbx
	jmp	.L11
.L14:
	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	_Z15GenerateNumbersPii, .-_Z15GenerateNumbersPii
	.section	.rodata.str1.1
.LC15:
	.string	"There is no device.\n"
.LC16:
	.string	"There is no device supporting CUDA 1.x.\n"
	.text
	.globl	_Z8InitCUDAv
	.type	_Z8InitCUDAv, @function
_Z8InitCUDAv:
.LFB2029:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	pushq	%rbx
	.cfi_def_cfa_offset 24
	.cfi_offset 3, -24
	subq	$1064, %rsp
	.cfi_def_cfa_offset 1088
	movq	%fs:40, %rax
	movq	%rax, 1048(%rsp)
	xorl	%eax, %eax
	leaq	12(%rsp), %rdi
	call	cudaGetDeviceCount@PLT
	cmpl	$0, 12(%rsp)
	leaq	.LC15(%rip), %rdx
	je	.L29
	xorl	%ebx, %ebx
	leaq	16(%rsp), %rbp
.L16:
	cmpl	%ebx, 12(%rsp)
	jle	.L20
	movl	%ebx, %esi
	movq	%rbp, %rdi
	call	cudaGetDeviceProperties_v2@PLT
	movq	%rbp, %rdi
	call	_Z15printDevicePropRK14cudaDeviceProp
	movl	%ebx, %esi
	movq	%rbp, %rdi
	call	cudaGetDeviceProperties_v2@PLT
	testl	%eax, %eax
	jne	.L18
	cmpl	$0, 376(%rsp)
	jg	.L20
.L18:
	incl	%ebx
	jmp	.L16
.L20:
	cmpl	%ebx, 12(%rsp)
	jne	.L22
	leaq	.LC16(%rip), %rdx
.L29:
	movq	stderr(%rip), %rdi
	movl	$2, %esi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	xorl	%eax, %eax
	jmp	.L15
.L22:
	movl	%ebx, %edi
	call	cudaSetDevice@PLT
	movb	$1, %al
.L15:
	movq	1048(%rsp), %rdx
	subq	%fs:40, %rdx
	je	.L24
	call	__stack_chk_fail@PLT
.L24:
	addq	$1064, %rsp
	.cfi_def_cfa_offset 24
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2029:
	.size	_Z8InitCUDAv, .-_Z8InitCUDAv
	.section	.rodata.str1.1
.LC17:
	.string	"CUDA initialized.\n"
.LC18:
	.string	"GPUsum: %d gputime: %d\n"
.LC19:
	.string	"CPUsum: %d \n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2030:
	.cfi_startproc
	endbr64
	pushq	%r12
	.cfi_def_cfa_offset 16
	.cfi_offset 12, -16
	pushq	%rbp
	.cfi_def_cfa_offset 24
	.cfi_offset 6, -24
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset 3, -32
	subq	$384, %rsp
	.cfi_def_cfa_offset 416
	movq	%fs:40, %rax
	movq	%rax, 376(%rsp)
	xorl	%eax, %eax
	call	_Z8InitCUDAv
	testb	%al, %al
	je	.L31
	leaq	.LC17(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	leaq	data(%rip), %rbx
	movl	$1048576, %esi
	movq	%rbx, %rdi
	call	_Z15GenerateNumbersPii
	leaq	8(%rsp), %rdi
	movl	$4194304, %esi
	call	cudaMalloc@PLT
	leaq	16(%rsp), %rdi
	movl	$64, %esi
	call	cudaMalloc@PLT
	leaq	24(%rsp), %rdi
	movl	$256, %esi
	call	cudaMalloc@PLT
	movq	8(%rsp), %rdi
	movl	$1, %ecx
	movq	%rbx, %rsi
	movl	$4194304, %edx
	call	cudaMemcpy@PLT
	movl	$4194305, %edx
	movl	$268435457, %edi
	xorl	%r9d, %r9d
	salq	$10, %rdx
	salq	$4, %rdi
	movl	$4096, %r8d
	movl	$1, %ecx
	movl	$1, %esi
	movq	%rdx, 44(%rsp)
	movl	$1, 52(%rsp)
	movq	%rdi, 32(%rsp)
	movl	$1, 40(%rsp)
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L32
	movq	24(%rsp), %rdx
	movq	16(%rsp), %rsi
	movq	8(%rsp), %rdi
	call	_ZL36__device_stub__Z12sumOfSquaresPiS_PlPiS_Pl
.L32:
	movq	16(%rsp), %rsi
	leaq	56(%rsp), %r12
	movl	$2, %ecx
	movl	$64, %edx
	movq	%r12, %rdi
	leaq	120(%rsp), %rbx
	call	cudaMemcpy@PLT
	movq	24(%rsp), %rsi
	movl	$256, %edx
	movq	%rbx, %rdi
	movl	$2, %ecx
	call	cudaMemcpy@PLT
	xorl	%eax, %eax
	xorl	%edx, %edx
.L33:
	addl	(%r12,%rax,4), %edx
	incq	%rax
	movl	%edx, %ebp
	cmpq	$16, %rax
	jne	.L33
	movq	8(%rsp), %rdi
	call	cudaFree@PLT
	movq	16(%rsp), %rdi
	call	cudaFree@PLT
	movq	24(%rsp), %rdi
	call	cudaFree@PLT
	movq	120(%rsp), %rdx
	movq	%rbx, %rax
	movq	248(%rsp), %rcx
	leaq	240(%rsp), %rdi
.L34:
	movq	8(%rax), %rsi
	cmpq	%rsi, %rdx
	cmovg	%rsi, %rdx
	movq	136(%rax), %rsi
	cmpq	%rsi, %rcx
	cmovl	%rsi, %rcx
	addq	$8, %rax
	cmpq	%rdi, %rax
	jne	.L34
	subq	%rdx, %rcx
	movl	$2, %edi
	movl	%ebp, %edx
	xorl	%eax, %eax
	leaq	.LC18(%rip), %rsi
	call	__printf_chk@PLT
	leaq	data(%rip), %rax
	xorl	%edx, %edx
	leaq	4194304(%rax), %rdi
.L35:
	movl	(%rax), %esi
	addq	$4, %rax
	movl	%esi, %ecx
	imull	%esi, %ecx
	imull	%esi, %ecx
	addl	%ecx, %edx
	cmpq	%rax, %rdi
	jne	.L35
	leaq	.LC19(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L31:
	movq	376(%rsp), %rax
	subq	%fs:40, %rax
	je	.L36
	call	__stack_chk_fail@PLT
.L36:
	addq	$384, %rsp
	.cfi_def_cfa_offset 32
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%rbp
	.cfi_def_cfa_offset 16
	popq	%r12
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2030:
	.size	main, .-main
	.section	.rodata.str1.1
.LC20:
	.string	"_Z12sumOfSquaresPiS_Pl"
	.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	.LC20(%rip), %rdx
	movq	%rax, %rdi
	leaq	_ZL12sumOfSquaresPiS_Pl(%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
	.globl	data
	.bss
	.align 32
	.type	data, @object
	.size	data, 4194304
data:
	.zero	4194304
	.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:

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
//CUDA RunTime API
#include <hip/hip_runtime.h>
#include <time.h>

#define THREAD_NUM 1024
#define BLOCK_NUM 16
#define DATA_SIZE 1048576

// __global__ 函数（GPU上执行） 计算立方和
__global__ static void sumOfSquares(int *num, int* result, clock_t* time)
{
	extern __shared__ int shared[];

	const int tid = threadIdx.x;

	const int bid = blockIdx.x;

	shared[tid] = 0;

	int i;

	if(tid == 0)
		time[bid] = clock();

	for(i = bid * THREAD_NUM + tid; i < DATA_SIZE; i+= BLOCK_NUM * THREAD_NUM){
		
		shared[tid] += num[i] * num[i] * num[i];

	}
	//第一个同步的意思是确保所有的线程完成了立方和的操作
	__syncthreads();

	int offset = 1, mask = 1;

	while(offset < THREAD_NUM){
		//由于为二进制，逢2进一，因此进行当两个相差2的数进行&操作时势必为0
		if((tid & mask) == 0){
			shared[tid] = shared[tid + offset];
		}

		offset += offset;
		mask = offset + mask;
		//第二个同步的意思是确保每次的累加完成，这样才能进入下一次的累加操作
		__syncthreads();
	}

	if(tid == 0){
		result[bid] = shared[0];
		time[bid + BLOCK_NUM] = clock();
	}
}

void printDeviceProp(const hipDeviceProp_t &prop)
{
	printf("Device Name : %s.\n", prop.name);
	printf("totalGlobalMem : %d.\n", prop.totalGlobalMem);
	printf("shareMemPredBLock : %d.\n", prop.sharedMemPerBlock);
	printf("regsPerBlock : %d.\n", prop.regsPerBlock);
	printf("warpSize : %d.\n", prop.warpSize);
	printf("memPitch : %d.\n", prop.memPitch);
	printf("maxThreadsPerBlock : %d.\n", prop.maxThreadsPerBlock);
	printf("maxThreadsDim[0 - 2] : %d %d %d.\n", prop.maxThreadsDim[0], prop.maxThreadsDim[1], prop.maxThreadsDim[2]);
	printf("maxGridSize[0 - 2] : %d %d %d.\n", prop.maxGridSize[0], prop.maxGridSize[1], prop.maxGridSize[2]);
	printf("totalConstMem : %d.\n", prop.totalConstMem);
	printf("major.minor : %d.%d\n", prop.major, prop.minor);
	printf("clockRate : %d.\n", prop.clockRate);
	printf("textureAlignment : %d\n", prop.textureAlignment);
	printf("deviceOverlap : %d.\n", prop.deviceOverlap);
	printf("multiProcessorCount : %d.\n", prop.multiProcessorCount);
}

int data[DATA_SIZE];

//产生大量0-9之间的随机数
void GenerateNumbers(int *number, int size)
{
	for(int i = 0; i < size; i++){
		number[i] = rand() % 10;
	}
}

//CUDA初始化
bool InitCUDA()
{
	int count;

	//取得cuda的装置的数目
	hipGetDeviceCount(&count);

	if(count == 0) {
		fprintf(stderr, "There is no device.\n");
		return false;
	}
	
	int i;
	
	for(i = 0; i < count; i++){
		hipDeviceProp_t prop;
		hipGetDeviceProperties(&prop, i);
		//打印设备信息
		printDeviceProp(prop);
		if(hipGetDeviceProperties(&prop, i) == hipSuccess){
			if(prop.major >= 1){
				break;
			}
		}
	}
	if(i == count){
		fprintf(stderr, "There is no device supporting CUDA 1.x.\n");
		return false;
	}

	hipSetDevice(i);
	return true;
}



int main()
{
	if (!InitCUDA()){
		return 0;
	}

	printf("CUDA initialized.\n");
	//生成随机数
	GenerateNumbers(data, DATA_SIZE);

	/*吧数据复制到显卡内部*/

	int* gpudata, *result;
	clock_t* time;

	//cudaMalloc取得一块显存内存（其中result存储结果)
	hipMalloc((void**)&gpudata, sizeof(int) * DATA_SIZE);
	hipMalloc((void**)&result, sizeof(int) * BLOCK_NUM);
	hipMalloc((void**)&time, sizeof(clock_t) * BLOCK_NUM * 2);

	//cudaMemcpy 将产生的随机数复制到显存内存中
	//cudaMemcpyHostToDevice - 从内存复制到显存
	//cudaMemcpyDeviceToHost - 从显存复制到内存
	hipMemcpy(gpudata, data, sizeof(int) * DATA_SIZE, hipMemcpyHostToDevice);

	sumOfSquares<<<BLOCK_NUM, THREAD_NUM, THREAD_NUM * sizeof(int)>>>(gpudata, result, time);

	int sum[BLOCK_NUM];
	clock_t time_used[BLOCK_NUM * 2];

	//cudaMemcpy 将结果从显存中复制回内存
	hipMemcpy(&sum, result, sizeof(int) * BLOCK_NUM, hipMemcpyDeviceToHost);
	hipMemcpy(&time_used, time, sizeof(clock_t) * BLOCK_NUM * 2 ,hipMemcpyDeviceToHost);
	int final_sum = 0;
	for(int i = 0; i < BLOCK_NUM; i++){
		final_sum += sum[i];
	}
	//Free
	hipFree(gpudata);
	hipFree(result);
	hipFree(time);
	clock_t min_start, max_end;
	min_start = time_used[0];
	max_end = time_used[BLOCK_NUM];

	for(int i = 1; i < BLOCK_NUM; i++){
		if(min_start > time_used[i])
			min_start = time_used[i];
		if(max_end < time_used[i + BLOCK_NUM])
			max_end = time_used[i + BLOCK_NUM];
	}
	printf("GPUsum: %d gputime: %d\n", final_sum, max_end - min_start) ;
	final_sum = 0;
	for(int i = 0; i < DATA_SIZE; i++){
		final_sum += data[i] * data[i] * data[i];
	}

	printf("CPUsum: %d \n", final_sum);
	return 0;
}

	.text
	.file	"initCuda.hip"
	.globl	_Z15printDevicePropRK20hipDeviceProp_tR0600 # -- Begin function _Z15printDevicePropRK20hipDeviceProp_tR0600
	.type	_Z15printDevicePropRK20hipDeviceProp_tR0600,@function
_Z15printDevicePropRK20hipDeviceProp_tR0600: # @_Z15printDevicePropRK20hipDeviceProp_tR0600
	.cfi_startproc
# %bb.0:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset %rbx, -16
	movq	%rdi, %rbx
	movl	$.L.str, %edi
	movq	%rbx, %rsi
	xorl	%eax, %eax
	callq	printf
	movq	288(%rbx), %rsi
	movl	$.L.str.1, %edi
	xorl	%eax, %eax
	callq	printf
	movq	296(%rbx), %rsi
	movl	$.L.str.2, %edi
	xorl	%eax, %eax
	callq	printf
	movl	304(%rbx), %esi
	movl	$.L.str.3, %edi
	xorl	%eax, %eax
	callq	printf
	movl	308(%rbx), %esi
	movl	$.L.str.4, %edi
	xorl	%eax, %eax
	callq	printf
	movq	312(%rbx), %rsi
	movl	$.L.str.5, %edi
	xorl	%eax, %eax
	callq	printf
	movl	320(%rbx), %esi
	movl	$.L.str.6, %edi
	xorl	%eax, %eax
	callq	printf
	movl	324(%rbx), %esi
	movl	328(%rbx), %edx
	movl	332(%rbx), %ecx
	movl	$.L.str.7, %edi
	xorl	%eax, %eax
	callq	printf
	movl	336(%rbx), %esi
	movl	340(%rbx), %edx
	movl	344(%rbx), %ecx
	movl	$.L.str.8, %edi
	xorl	%eax, %eax
	callq	printf
	movq	352(%rbx), %rsi
	movl	$.L.str.9, %edi
	xorl	%eax, %eax
	callq	printf
	movl	360(%rbx), %esi
	movl	364(%rbx), %edx
	movl	$.L.str.10, %edi
	xorl	%eax, %eax
	callq	printf
	movl	348(%rbx), %esi
	movl	$.L.str.11, %edi
	xorl	%eax, %eax
	callq	printf
	movq	368(%rbx), %rsi
	movl	$.L.str.12, %edi
	xorl	%eax, %eax
	callq	printf
	movl	384(%rbx), %esi
	movl	$.L.str.13, %edi
	xorl	%eax, %eax
	callq	printf
	movl	388(%rbx), %esi
	movl	$.L.str.14, %edi
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 8
	jmp	printf                          # TAILCALL
.Lfunc_end0:
	.size	_Z15printDevicePropRK20hipDeviceProp_tR0600, .Lfunc_end0-_Z15printDevicePropRK20hipDeviceProp_tR0600
	.cfi_endproc
                                        # -- End function
	.globl	_Z15GenerateNumbersPii          # -- Begin function _Z15GenerateNumbersPii
	.type	_Z15GenerateNumbersPii,@function
_Z15GenerateNumbersPii:                 # @_Z15GenerateNumbersPii
	.cfi_startproc
# %bb.0:
	testl	%esi, %esi
	jle	.LBB1_4
# %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
.LBB1_2:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	callq	rand
	cltq
	imulq	$1717986919, %rax, %rcx         # imm = 0x66666667
	movq	%rcx, %rdx
	shrq	$63, %rdx
	sarq	$34, %rcx
	addl	%edx, %ecx
	addl	%ecx, %ecx
	leal	(%rcx,%rcx,4), %ecx
	subl	%ecx, %eax
	movl	%eax, (%rbx,%r15,4)
	incq	%r15
	cmpq	%r15, %r14
	jne	.LBB1_2
# %bb.3:
	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
.LBB1_4:                                # %._crit_edge
	retq
.Lfunc_end1:
	.size	_Z15GenerateNumbersPii, .Lfunc_end1-_Z15GenerateNumbersPii
	.cfi_endproc
                                        # -- End function
	.globl	_Z8InitCUDAv                    # -- Begin function _Z8InitCUDAv
	.type	_Z8InitCUDAv,@function
_Z8InitCUDAv:                           # @_Z8InitCUDAv
	.cfi_startproc
# %bb.0:
	pushq	%r14
	.cfi_def_cfa_offset 16
	pushq	%rbx
	.cfi_def_cfa_offset 24
	subq	$1480, %rsp                     # imm = 0x5C8
	.cfi_def_cfa_offset 1504
	.cfi_offset %rbx, -24
	.cfi_offset %r14, -16
	leaq	4(%rsp), %rbx
	movq	%rbx, %rdi
	callq	hipGetDeviceCount
	movl	(%rbx), %ecx
	testl	%ecx, %ecx
	je	.LBB2_3
# %bb.1:                                # %.preheader
	jle	.LBB2_2
# %bb.5:                                # %.lr.ph
	xorl	%ebx, %ebx
	leaq	8(%rsp), %r14
.LBB2_6:                                # =>This Inner Loop Header: Depth=1
	movq	%r14, %rdi
	movl	%ebx, %esi
	callq	hipGetDevicePropertiesR0600
	movq	%r14, %rdi
	callq	_Z15printDevicePropRK20hipDeviceProp_tR0600
	movq	%r14, %rdi
	movl	%ebx, %esi
	callq	hipGetDevicePropertiesR0600
	movl	4(%rsp), %ecx
	testl	%eax, %eax
	jne	.LBB2_8
# %bb.7:                                #   in Loop: Header=BB2_6 Depth=1
	cmpl	$0, 368(%rsp)
	jg	.LBB2_9
.LBB2_8:                                #   in Loop: Header=BB2_6 Depth=1
	incl	%ebx
	cmpl	%ecx, %ebx
	jl	.LBB2_6
	jmp	.LBB2_9
.LBB2_2:
	xorl	%ebx, %ebx
.LBB2_9:                                # %._crit_edge
	cmpl	%ecx, %ebx
	je	.LBB2_10
# %bb.11:
	movl	%ebx, %edi
	callq	hipSetDevice
	movb	$1, %al
.LBB2_12:
                                        # kill: def $al killed $al killed $eax
	addq	$1480, %rsp                     # imm = 0x5C8
	.cfi_def_cfa_offset 24
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%r14
	.cfi_def_cfa_offset 8
	retq
.LBB2_3:
	.cfi_def_cfa_offset 1504
	movq	stderr(%rip), %rcx
	movl	$.L.str.15, %edi
	movl	$20, %esi
	jmp	.LBB2_4
.LBB2_10:
	movq	stderr(%rip), %rcx
	movl	$.L.str.16, %edi
	movl	$40, %esi
.LBB2_4:
	movl	$1, %edx
	callq	fwrite@PLT
	xorl	%eax, %eax
	jmp	.LBB2_12
.Lfunc_end2:
	.size	_Z8InitCUDAv, .Lfunc_end2-_Z8InitCUDAv
	.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	%rbx
	.cfi_def_cfa_offset 24
	subq	$360, %rsp                      # imm = 0x168
	.cfi_def_cfa_offset 384
	.cfi_offset %rbx, -24
	.cfi_offset %rbp, -16
	callq	_Z8InitCUDAv
	testb	%al, %al
	je	.LBB3_12
# %bb.1:
	movl	$.Lstr, %edi
	callq	puts@PLT
	movq	$-4194304, %rbx                 # imm = 0xFFC00000
.LBB3_2:                                # %.lr.ph.i
                                        # =>This Inner Loop Header: Depth=1
	callq	rand
	cltq
	imulq	$1717986919, %rax, %rcx         # imm = 0x66666667
	movq	%rcx, %rdx
	shrq	$63, %rdx
	sarq	$34, %rcx
	addl	%edx, %ecx
	addl	%ecx, %ecx
	leal	(%rcx,%rcx,4), %ecx
	subl	%ecx, %eax
	movl	%eax, data+4194304(%rbx)
	addq	$4, %rbx
	jne	.LBB3_2
# %bb.3:                                # %_Z15GenerateNumbersPii.exit
	leaq	24(%rsp), %rbx
	movl	$4194304, %esi                  # imm = 0x400000
	movq	%rbx, %rdi
	callq	hipMalloc
	leaq	16(%rsp), %rdi
	movl	$64, %esi
	callq	hipMalloc
	leaq	8(%rsp), %rdi
	movl	$256, %esi                      # imm = 0x100
	callq	hipMalloc
	movq	(%rbx), %rdi
	movl	$data, %esi
	movl	$4194304, %edx                  # imm = 0x400000
	movl	$1, %ecx
	callq	hipMemcpy
	movabsq	$4294967312, %rdi               # imm = 0x100000010
	leaq	1008(%rdi), %rdx
	xorl	%ebx, %ebx
	movl	$4096, %r8d                     # imm = 0x1000
	movl	$1, %esi
	movl	$1, %ecx
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB3_5
# %bb.4:
	movq	24(%rsp), %rdi
	movq	16(%rsp), %rsi
	movq	8(%rsp), %rdx
	callq	_ZL27__device_stub__sumOfSquaresPiS_Pl
.LBB3_5:
	movq	16(%rsp), %rsi
	leaq	32(%rsp), %rdi
	movl	$64, %edx
	movl	$2, %ecx
	callq	hipMemcpy
	movq	8(%rsp), %rsi
	leaq	96(%rsp), %rdi
	movl	$256, %edx                      # imm = 0x100
	movl	$2, %ecx
	callq	hipMemcpy
	xorl	%eax, %eax
.LBB3_6:                                # =>This Inner Loop Header: Depth=1
	addl	32(%rsp,%rax,4), %ebx
	incq	%rax
	cmpq	$16, %rax
	jne	.LBB3_6
# %bb.7:
	movq	24(%rsp), %rdi
	callq	hipFree
	movq	16(%rsp), %rdi
	callq	hipFree
	movq	8(%rsp), %rdi
	callq	hipFree
	movq	96(%rsp), %rax
	movq	224(%rsp), %rdx
	movl	$1, %ecx
.LBB3_8:                                # =>This Inner Loop Header: Depth=1
	movq	%rax, %rsi
	movq	%rdx, %rdi
	movq	96(%rsp,%rcx,8), %rax
	movq	224(%rsp,%rcx,8), %rdx
	cmpq	%rax, %rsi
	cmovlq	%rsi, %rax
	cmpq	%rdx, %rdi
	cmovgq	%rdi, %rdx
	incq	%rcx
	cmpq	$16, %rcx
	jne	.LBB3_8
# %bb.9:
	subq	%rax, %rdx
	xorl	%ebp, %ebp
	movl	$.L.str.18, %edi
	movl	%ebx, %esi
	xorl	%eax, %eax
	callq	printf
	movq	$-4194304, %rax                 # imm = 0xFFC00000
.LBB3_10:                               # =>This Inner Loop Header: Depth=1
	movl	data+4194304(%rax), %ecx
	movl	%ecx, %edx
	imull	%ecx, %edx
	imull	%ecx, %edx
	addl	%edx, %ebp
	addq	$4, %rax
	jne	.LBB3_10
# %bb.11:
	movl	$.L.str.19, %edi
	movl	%ebp, %esi
	xorl	%eax, %eax
	callq	printf
.LBB3_12:
	xorl	%eax, %eax
	addq	$360, %rsp                      # imm = 0x168
	.cfi_def_cfa_offset 24
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end3:
	.size	main, .Lfunc_end3-main
	.cfi_endproc
                                        # -- End function
	.type	_ZL27__device_stub__sumOfSquaresPiS_Pl,@function # -- Begin function _ZL27__device_stub__sumOfSquaresPiS_Pl
_ZL27__device_stub__sumOfSquaresPiS_Pl: # @_ZL27__device_stub__sumOfSquaresPiS_Pl
	.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), %rcx
	movq	%rsi, (%rcx)
	leaq	24(%rsp), %rsi
	movq	%rdx, (%rsi)
	leaq	80(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rcx, 8(%rbx)
	movq	%rsi, 16(%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	$_ZL12sumOfSquaresPiS_Pl, %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_end4:
	.size	_ZL27__device_stub__sumOfSquaresPiS_Pl, .Lfunc_end4-_ZL27__device_stub__sumOfSquaresPiS_Pl
	.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	$_ZL12sumOfSquaresPiS_Pl, %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	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"Device Name : %s.\n"
	.size	.L.str, 19

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"totalGlobalMem : %d.\n"
	.size	.L.str.1, 22

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"shareMemPredBLock : %d.\n"
	.size	.L.str.2, 25

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"regsPerBlock : %d.\n"
	.size	.L.str.3, 20

	.type	.L.str.4,@object                # @.str.4
.L.str.4:
	.asciz	"warpSize : %d.\n"
	.size	.L.str.4, 16

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"memPitch : %d.\n"
	.size	.L.str.5, 16

	.type	.L.str.6,@object                # @.str.6
.L.str.6:
	.asciz	"maxThreadsPerBlock : %d.\n"
	.size	.L.str.6, 26

	.type	.L.str.7,@object                # @.str.7
.L.str.7:
	.asciz	"maxThreadsDim[0 - 2] : %d %d %d.\n"
	.size	.L.str.7, 34

	.type	.L.str.8,@object                # @.str.8
.L.str.8:
	.asciz	"maxGridSize[0 - 2] : %d %d %d.\n"
	.size	.L.str.8, 32

	.type	.L.str.9,@object                # @.str.9
.L.str.9:
	.asciz	"totalConstMem : %d.\n"
	.size	.L.str.9, 21

	.type	.L.str.10,@object               # @.str.10
.L.str.10:
	.asciz	"major.minor : %d.%d\n"
	.size	.L.str.10, 21

	.type	.L.str.11,@object               # @.str.11
.L.str.11:
	.asciz	"clockRate : %d.\n"
	.size	.L.str.11, 17

	.type	.L.str.12,@object               # @.str.12
.L.str.12:
	.asciz	"textureAlignment : %d\n"
	.size	.L.str.12, 23

	.type	.L.str.13,@object               # @.str.13
.L.str.13:
	.asciz	"deviceOverlap : %d.\n"
	.size	.L.str.13, 21

	.type	.L.str.14,@object               # @.str.14
.L.str.14:
	.asciz	"multiProcessorCount : %d.\n"
	.size	.L.str.14, 27

	.type	data,@object                    # @data
	.bss
	.globl	data
	.p2align	4, 0x0
data:
	.zero	4194304
	.size	data, 4194304

	.type	.L.str.15,@object               # @.str.15
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str.15:
	.asciz	"There is no device.\n"
	.size	.L.str.15, 21

	.type	.L.str.16,@object               # @.str.16
.L.str.16:
	.asciz	"There is no device supporting CUDA 1.x.\n"
	.size	.L.str.16, 41

	.type	_ZL12sumOfSquaresPiS_Pl,@object # @_ZL12sumOfSquaresPiS_Pl
	.section	.rodata,"a",@progbits
	.p2align	3, 0x0
_ZL12sumOfSquaresPiS_Pl:
	.quad	_ZL27__device_stub__sumOfSquaresPiS_Pl
	.size	_ZL12sumOfSquaresPiS_Pl, 8

	.type	.L.str.18,@object               # @.str.18
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str.18:
	.asciz	"GPUsum: %d gputime: %d\n"
	.size	.L.str.18, 24

	.type	.L.str.19,@object               # @.str.19
.L.str.19:
	.asciz	"CPUsum: %d \n"
	.size	.L.str.19, 13

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_ZL12sumOfSquaresPiS_Pl"
	.size	.L__unnamed_1, 24

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

	.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 _ZL27__device_stub__sumOfSquaresPiS_Pl
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym data
	.addrsig_sym _ZL12sumOfSquaresPiS_Pl
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.section	.text._ZL12sumOfSquaresPiS_Pl,"axG",@progbits,_ZL12sumOfSquaresPiS_Pl,comdat
	.globl	_ZL12sumOfSquaresPiS_Pl         ; -- Begin function _ZL12sumOfSquaresPiS_Pl
	.p2align	8
	.type	_ZL12sumOfSquaresPiS_Pl,@function
_ZL12sumOfSquaresPiS_Pl:                ; @_ZL12sumOfSquaresPiS_Pl
; %bb.0:
	s_clause 0x1
	s_load_b128 s[4:7], s[0:1], 0x0
	s_load_b64 s[8:9], s[0:1], 0x10
	v_lshl_add_u32 v6, v0, 2, 0
	v_mov_b32_e32 v2, 0
	v_cmp_eq_u32_e32 vcc_lo, 0, v0
	s_mov_b32 s2, s15
	ds_store_b32 v6, v2
	s_and_saveexec_b32 s0, vcc_lo
	s_cbranch_execz .LBB0_2
; %bb.1:
	s_ashr_i32 s3, s2, 31
	s_getreg_b32 s1, hwreg(HW_REG_SHADER_CYCLES, 0, 20)
	s_lshl_b64 s[10:11], s[2:3], 3
	v_mov_b32_e32 v1, s1
	s_waitcnt lgkmcnt(0)
	s_add_u32 s10, s8, s10
	s_addc_u32 s11, s9, s11
	global_store_b64 v2, v[1:2], s[10:11]
.LBB0_2:
	s_or_b32 exec_lo, exec_lo, s0
	s_lshl_b32 s1, s2, 10
	s_mov_b32 s3, exec_lo
	v_or_b32_e32 v1, s1, v0
	s_delay_alu instid0(VALU_DEP_1)
	v_cmpx_gt_i32_e32 0x100000, v1
	s_cbranch_execz .LBB0_6
; %bb.3:                                ; %.lr.ph
	ds_load_b32 v3, v6
	v_ashrrev_i32_e32 v2, 31, v1
	v_add3_u32 v7, v0, s1, 0xffffc000
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_1) | instid1(VALU_DEP_1)
	v_lshlrev_b64 v[1:2], 2, v[1:2]
	s_waitcnt lgkmcnt(0)
	v_add_co_u32 v1, s0, s4, v1
	s_delay_alu instid0(VALU_DEP_1)
	v_add_co_ci_u32_e64 v2, s0, s5, v2, s0
	s_mov_b32 s4, 0
.LBB0_4:                                ; =>This Inner Loop Header: Depth=1
	global_load_b32 v8, v[1:2], off
	v_add_nc_u32_e32 v7, 0x4000, v7
	v_add_co_u32 v1, s0, 0x10000, v1
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_add_co_ci_u32_e64 v2, s0, 0, v2, s0
	v_cmp_lt_i32_e64 s1, 0xfbfff, v7
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_2) | instid1(VALU_DEP_1)
	s_or_b32 s4, s1, s4
	s_waitcnt vmcnt(0)
	v_mul_lo_u32 v9, v8, v8
	v_mad_u64_u32 v[4:5], null, v9, v8, v[3:4]
	s_delay_alu instid0(VALU_DEP_1)
	v_mov_b32_e32 v3, v4
	s_and_not1_b32 exec_lo, exec_lo, s4
	s_cbranch_execnz .LBB0_4
; %bb.5:                                ; %._crit_edge
	s_or_b32 exec_lo, exec_lo, s4
	ds_store_b32 v6, v4
.LBB0_6:                                ; %Flow
	s_or_b32 exec_lo, exec_lo, s3
	s_mov_b32 s1, 1
	s_mov_b32 s3, 1
	s_waitcnt lgkmcnt(0)
	s_waitcnt_vscnt null, 0x0
	s_barrier
	buffer_gl0_inv
.LBB0_7:                                ; =>This Inner Loop Header: Depth=1
	v_and_b32_e32 v1, s1, v0
	s_mov_b32 s4, exec_lo
	s_delay_alu instid0(VALU_DEP_1)
	v_cmpx_eq_u32_e32 0, v1
	s_cbranch_execz .LBB0_9
; %bb.8:                                ;   in Loop: Header=BB0_7 Depth=1
	v_lshl_add_u32 v1, s3, 2, v6
	ds_load_b32 v1, v1
	s_waitcnt lgkmcnt(0)
	ds_store_b32 v6, v1
.LBB0_9:                                ;   in Loop: Header=BB0_7 Depth=1
	s_or_b32 exec_lo, exec_lo, s4
	s_lshl_b32 s0, s3, 1
	s_waitcnt lgkmcnt(0)
	s_add_i32 s1, s0, s1
	s_cmpk_lt_u32 s3, 0x200
	s_mov_b32 s3, s0
	s_barrier
	buffer_gl0_inv
	s_cbranch_scc1 .LBB0_7
; %bb.10:
	s_and_saveexec_b32 s0, vcc_lo
	s_cbranch_execz .LBB0_12
; %bb.11:
	v_mov_b32_e32 v1, 0
	s_ashr_i32 s3, s2, 31
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_lshl_b64 s[0:1], s[2:3], 3
	s_add_u32 s0, s8, s0
	ds_load_b32 v0, v1
	s_addc_u32 s1, s9, s1
	s_lshl_b64 s[2:3], s[2:3], 2
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(SKIP_4) | instid1(SALU_CYCLE_1)
	s_add_u32 s2, s6, s2
	s_addc_u32 s3, s7, s3
	s_waitcnt lgkmcnt(0)
	global_store_b32 v1, v0, s[2:3]
	s_getreg_b32 s2, hwreg(HW_REG_SHADER_CYCLES, 0, 20)
	v_mov_b32_e32 v0, s2
	global_store_b64 v1, v[0:1], s[0:1] offset:128
.LBB0_12:
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _ZL12sumOfSquaresPiS_Pl
		.amdhsa_group_segment_fixed_size 0
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 24
		.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 10
		.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
	.section	.text._ZL12sumOfSquaresPiS_Pl,"axG",@progbits,_ZL12sumOfSquaresPiS_Pl,comdat
.Lfunc_end0:
	.size	_ZL12sumOfSquaresPiS_Pl, .Lfunc_end0-_ZL12sumOfSquaresPiS_Pl
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 512
; NumSgprs: 18
; NumVgprs: 10
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 1
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 10
; 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
    .group_segment_fixed_size: 0
    .kernarg_segment_align: 8
    .kernarg_segment_size: 24
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _ZL12sumOfSquaresPiS_Pl
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _ZL12sumOfSquaresPiS_Pl.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     10
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
