#include <time.h>
#include <stdio.h>
#include <stdlib.h>	//rand, atoi
#include <sys/time.h>
#include <unistd.h>

#define MAX_N 10000000

#define GPU_CHECKERROR(err) (gpuCheckError(err,__FILE__,__LINE__))

static void gpuCheckError(cudaError_t err, const char * file, int line){
	if(err != cudaSuccess ){
		printf("%s in file  %s at line %d\n",
				cudaGetErrorString(err), __FILE__, line);
		exit(EXIT_FAILURE);
	}
}

//Determines if A & B are co-prime using the Euclidean Algorithm.
unsigned int __cop(unsigned int A, unsigned int B){
	unsigned int T;
	//Swap 'em
	if(B>A){
		T=B;
		B=A;
		A=T;
		
	}
	while(B){
		T=B;
		B=A%B;
		A = T;
	}
	return A==1;
}

//Finds the number of co-prime  numbers in two vectors A and B
unsigned int copV(unsigned int N, unsigned int * A, unsigned int * B){
	unsigned int numCop = 0;
	for(int i = 0 ; i < N; i++){
		numCop += __cop(A[i], B[i]);
	}
	return numCop;
}

//Calculates if correspoding numbers in two arrays are coprime
//and stores the result in the first array.
__global__ void copV_GPU(unsigned int N, unsigned int * A, unsigned int * B){
	unsigned int n = blockDim.x * blockIdx.x + threadIdx.x;
	
	if(n >= N) return;
		
	int a = A[n];
	int b = B[n];
		
	unsigned int t;
	//Swap 'em
	if(b>a){
		t=b;
		b=a;
		a=t;
		
	}
	while(b){
		t=b;
		b=a%b;
		a = t;
	}
	A[n] = (a==1);
	
}

//Performs sum reduction of an array A. A must be aligned to blockDim.x
__global__ void reduce_GPU(unsigned int * A){
    unsigned int tid = threadIdx.x;
    unsigned int idx = blockDim.x*blockIdx.x + threadIdx.x;
   
    for( unsigned int S = blockDim.x/2; S > 0; S/=2 ){
        if( tid < S )
            A[idx] += A[idx+S];

        //Make sure all threads have finished before we continue
        __syncthreads();
    }
    
}

//Code to display device properties.
// http://gpucoder.livejournal.com/1064.html
void printDevProp(cudaDeviceProp devProp)
{
    printf("Major revision number:         %d\n",  devProp.major);
    printf("Minor revision number:         %d\n",  devProp.minor);
    printf("Name:                          %s\n",  devProp.name);
    printf("Total global memory:           %lu\n",  devProp.totalGlobalMem);
    printf("Total shared memory per block: %lu\n",  devProp.sharedMemPerBlock);
    printf("Total registers per block:     %d\n",  devProp.regsPerBlock);
    printf("Warp size:                     %d\n",  devProp.warpSize);
    printf("Maximum memory pitch:          %lu\n",  devProp.memPitch);
    printf("Maximum threads per block:     %d\n",  devProp.maxThreadsPerBlock);
    for (int i = 0; i < 3; ++i)
    printf("Maximum dimension %d of block:  %d\n", i, devProp.maxThreadsDim[i]);
    for (int i = 0; i < 3; ++i)
    printf("Maximum dimension %d of grid:   %d\n", i, devProp.maxGridSize[i]);
    printf("Clock rate:                    %d\n",  devProp.clockRate);
    printf("Total constant memory:         %lu\n",  devProp.totalConstMem);
    printf("Texture alignment:             %lu\n",  devProp.textureAlignment);
    printf("Concurrent copy and execution: %s\n",  (devProp.deviceOverlap ? "Yes" : "No"));
    printf("Number of multiprocessors:     %d\n",  devProp.multiProcessorCount);
    printf("Kernel execution timeout:      %s\n",  (devProp.kernelExecTimeoutEnabled ? "Yes" : "No"));
    return;
}

int main(int argc, char * argv[]){

	//Create an array on N
	unsigned int N;
	unsigned int N2;
	size_t sz;
    size_t sz2;
	unsigned int * h_A;
	unsigned int * h_B;

	unsigned int nThreads ;
	
	srand(time(NULL));
	
	//Query device properties.
	int dev_count, best_dev;
	cudaDeviceProp dev_prop_curr;
	cudaDeviceProp dev_prop_max_;
	cudaDeviceProp *dev_prop_max = &dev_prop_max_;
	dev_prop_max->maxThreadsPerBlock = 0;
	dev_prop_max->multiProcessorCount = 0;
	dev_prop_max->clockRate = 0;
	cudaGetDeviceCount( &dev_count );
	for( int i = 0; i < dev_count ; i++ ){
		cudaGetDeviceProperties( &dev_prop_curr , i);
		printf("\n-------------------Device %d----------------------\n",i);
		printDevProp( dev_prop_curr );
		//Select the best device
		if( 	(dev_prop_curr.maxThreadsPerBlock > dev_prop_max->maxThreadsPerBlock) &&
			(dev_prop_curr.multiProcessorCount > dev_prop_max->multiProcessorCount) && 
			(dev_prop_curr.clockRate > dev_prop_max->clockRate) ){
			best_dev = i;
			dev_prop_max = &dev_prop_curr;
			nThreads = dev_prop_curr.maxThreadsPerBlock;
			}
		
	}
	cudaSetDevice(best_dev);
	printf("\nBest Device found %d\n",best_dev);
	
	if(argc < 2){
		//Fill the arrays with 1000 random numbers
		N = 1000;
		N2=((N-1)/nThreads + 1)*nThreads;
        sz = sizeof( unsigned int ) * N;
		//Make sure array size is a multiple of blockDim;
        sz2 = sizeof( unsigned int ) * N2;
    
		h_A = (unsigned int *)malloc( sz2 );
		h_B = (unsigned int *)malloc( sz );
		
		printf("Argc=%d Filling array A & B with %d integers %d\n", argc-1, N,N2);
		for(unsigned int i = 0 ; i < N; i++){
			h_A[i] = rand() % MAX_N;
			h_B[i] = rand() % MAX_N;
			//printf("%d\n%d\n",h_A[i],h_B[i]);
		}	
	}else if(argc == 2){
		//Fill the arrays with random numbers
		N = (unsigned int) atoi( argv[1] );
		N2=((N-1)/nThreads + 1)*nThreads;
		sz = sizeof( unsigned int ) * N;
        //Make sure array size is a multiple of blockDim;
        sz2 = sizeof( unsigned int ) * N2;
    
		h_A = (unsigned int *)malloc( sz2 );
		h_B = (unsigned int *)malloc( sz );
	
		printf("\nArgc=%d Filling array A & B with %d integers and %d\n\n", argc-1, N,N2);
		for(unsigned int i = 0 ; i < N; i++){
			h_A[i] = rand() % MAX_N;
			h_B[i] = rand() % MAX_N;
			//printf("%d\n%d\n",h_A[i],h_B[i]);
		}
	}else if ( argc > 2){
	
		//Read from file
		printf( "Reading input from file %s\n",argv[2]);
		
		
		FILE *fr;
		fr = fopen(argv[2], "r");
		if( !fr ){
			fprintf(stderr, "Can't open input file!\n");
			exit(1);
		}
		
		char oneword[100];
		int count = 0;
		
		//First count the number of entries in the file
		while(fscanf(fr,"%s",oneword) != EOF ){
			count ++;
		}
		N = count/2;
		N2=((N-1)/nThreads + 1)*nThreads;
		rewind(fr);
		//Now re-allocate a new array
		sz = sizeof( unsigned int ) * N;
        //Make sure array size is a multiple of blockDim;
        sz2 = sizeof( unsigned int ) * N2;
		printf("Starting %d, %d\n", N,N2);

		h_A = (unsigned int *)malloc( sz2 );
		h_B = (unsigned int *)malloc( sz );
		
		count = 0;
		while(fscanf(fr,"%s",oneword) != EOF ){
		
			//sprintf("%s\n",oneword);
			if(count % 2){
				h_B[count/2] = atoi( oneword );
				//printf("%d\n",h_B[count/2]);
			}
			else{
				h_A[count/2] = atoi( oneword );
				//printf("%d ",h_A[count/2]);
			}
			count ++;
		}
		
		
		fclose(fr);
	}
	
	//--------------------------------------------------------------------------
	//Serial Version
	//
	//--------------------------------------------------------------------------
	struct timeval t0, t1, t2;
	gettimeofday(&t0,0);
	unsigned int numCoPrimes = copV(N, h_A, h_B);
	gettimeofday(&t1,0);
	float timdiff1 = (1000000.0*(t1.tv_sec - t0.tv_sec) + (t1.tv_usec - t0.tv_usec)) / 1000000.0;
	printf ("\nDone: time taken for serial version is %3.1f s\n", timdiff1);
	printf("Number co-primes found by CPU = %d\n\n", numCoPrimes);


	//--------------------------------------------------------------------------
	//Parallel Version
	//
	//--------------------------------------------------------------------------
	
	printf("Best device %d\n",best_dev);
	//Allocate vectors and count in device memory
	gettimeofday(&t1,0);
	unsigned int * d_A;
	GPU_CHECKERROR(  cudaMalloc(&d_A,sz2));
	unsigned int * d_B;
	GPU_CHECKERROR(  cudaMalloc(&d_B,sz));
	GPU_CHECKERROR(  cudaMemset((void *) &d_A[N],0,sz2-sz) );		//Pad with 0's
	
	//Copy vectors from host to device memory
	GPU_CHECKERROR(  cudaMemcpy(d_A,h_A,sz2,cudaMemcpyHostToDevice));
	GPU_CHECKERROR(  cudaMemcpy(d_B,h_B,sz,cudaMemcpyHostToDevice));
	
	unsigned int nBlocks = (N + nThreads - 1)/nThreads;

    copV_GPU<<< nBlocks, nThreads>>>(N,d_A,d_B);

	cudaDeviceSynchronize();	
	
	//Reduce 'em
	reduce_GPU<<< nBlocks, nThreads>>>(d_A);
	cudaDeviceSynchronize();
	
	//Fetch the results;
	GPU_CHECKERROR(  cudaMemcpy( (void *) h_A,
						(void *) d_A,
						sz2,
						cudaMemcpyDeviceToHost) );
				
	int sum = 0;
	for( int i = 0; i < N2 ; i++ ){
		if(!(i%nThreads)){
			sum += h_A[i];
			//printf("\n\n");
		}
		//printf("h_A[%d]=%u\t",i,h_A[i]);
	}
	printf("Number co-primes found by GPU = %d Num reduced %d\n", sum, N2);	
	
	gettimeofday(&t2,0);
    float timdiff2 = (1000000.0*(t2.tv_sec - t1.tv_sec) + (t2.tv_usec - t1.tv_usec)) / 1000000.0;
    printf ("done: time taken for parallel version is %3.1f s threads %d\n", timdiff2, 0);

	//Free device memory
	GPU_CHECKERROR( cudaFree(d_A) );
	GPU_CHECKERROR( cudaFree(d_B) );
	
	
	//Free Host memory
	free(h_A);
	free(h_B);
}


	.file	"tmpxft_00244fd1_00000000-6_copV.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2047:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2047:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"/home/ubuntu/Datasets/stackv2/train-structured/wakahiu/CUDA/master/hw0/copV.cu"
.LC1:
	.string	"%s in file  %s at line %d\n"
	.text
	.type	_ZL13gpuCheckError9cudaErrorPKci.constprop.0, @function
_ZL13gpuCheckError9cudaErrorPKci.constprop.0:
.LFB2192:
	.cfi_startproc
	testl	%edi, %edi
	je	.L2
	subq	$24, %rsp
	.cfi_def_cfa_offset 32
	movl	%esi, 12(%rsp)
	call	cudaGetErrorString@PLT
	movl	12(%rsp), %r8d
	movl	$2, %edi
	leaq	.LC0(%rip), %rcx
	movq	%rax, %rdx
	leaq	.LC1(%rip), %rsi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	$1, %edi
	call	exit@PLT
.L2:
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2192:
	.size	_ZL13gpuCheckError9cudaErrorPKci.constprop.0, .-_ZL13gpuCheckError9cudaErrorPKci.constprop.0
	.globl	_Z5__copjj
	.type	_Z5__copjj, @function
_Z5__copjj:
.LFB2041:
	.cfi_startproc
	endbr64
	movl	%esi, %eax
	movl	%esi, %edx
	cmpl	%esi, %edi
	jb	.L10
.L12:
	movl	%edi, %eax
	movl	%edx, %edi
.L10:
	testl	%edi, %edi
	je	.L13
	xorl	%edx, %edx
	divl	%edi
	jmp	.L12
.L13:
	decl	%eax
	sete	%al
	movzbl	%al, %eax
	ret
	.cfi_endproc
.LFE2041:
	.size	_Z5__copjj, .-_Z5__copjj
	.globl	_Z4copVjPjS_
	.type	_Z4copVjPjS_, @function
_Z4copVjPjS_:
.LFB2042:
	.cfi_startproc
	endbr64
	movq	%rsi, %r10
	movq	%rdx, %r11
	movl	%edi, %r9d
	xorl	%ecx, %ecx
	xorl	%r8d, %r8d
.L15:
	cmpq	%r9, %rcx
	je	.L17
	movl	(%r11,%rcx,4), %esi
	movl	(%r10,%rcx,4), %edi
	incq	%rcx
	call	_Z5__copjj
	addl	%eax, %r8d
	jmp	.L15
.L17:
	movl	%r8d, %eax
	ret
	.cfi_endproc
.LFE2042:
	.size	_Z4copVjPjS_, .-_Z4copVjPjS_
	.section	.rodata.str1.1
.LC2:
	.string	"Yes"
.LC3:
	.string	"No"
.LC4:
	.string	"Major revision number:         %d\n"
.LC5:
	.string	"Minor revision number:         %d\n"
.LC6:
	.string	"Name:                          %s\n"
.LC7:
	.string	"Total global memory:           %lu\n"
.LC8:
	.string	"Total shared memory per block: %lu\n"
.LC9:
	.string	"Total registers per block:     %d\n"
.LC10:
	.string	"Warp size:                     %d\n"
.LC11:
	.string	"Maximum memory pitch:          %lu\n"
.LC12:
	.string	"Maximum threads per block:     %d\n"
.LC13:
	.string	"Maximum dimension %d of block:  %d\n"
.LC14:
	.string	"Maximum dimension %d of grid:   %d\n"
.LC15:
	.string	"Clock rate:                    %d\n"
.LC16:
	.string	"Total constant memory:         %lu\n"
.LC17:
	.string	"Texture alignment:             %lu\n"
.LC18:
	.string	"Concurrent copy and execution: %s\n"
.LC19:
	.string	"Number of multiprocessors:     %d\n"
.LC20:
	.string	"Kernel execution timeout:      %s\n"
	.text
	.globl	_Z12printDevProp14cudaDeviceProp
	.type	_Z12printDevProp14cudaDeviceProp, @function
_Z12printDevProp14cudaDeviceProp:
.LFB2043:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	leaq	.LC4(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	pushq	%rbx
	.cfi_def_cfa_offset 24
	.cfi_offset 3, -24
	leaq	.LC13(%rip), %rbp
	xorl	%ebx, %ebx
	pushq	%rdx
	.cfi_def_cfa_offset 32
	movl	392(%rsp), %edx
	call	__printf_chk@PLT
	movl	396(%rsp), %edx
	leaq	.LC5(%rip), %rsi
	xorl	%eax, %eax
	movl	$2, %edi
	call	__printf_chk@PLT
	leaq	32(%rsp), %rdx
	leaq	.LC6(%rip), %rsi
	xorl	%eax, %eax
	movl	$2, %edi
	call	__printf_chk@PLT
	movq	320(%rsp), %rdx
	leaq	.LC7(%rip), %rsi
	xorl	%eax, %eax
	movl	$2, %edi
	call	__printf_chk@PLT
	movq	328(%rsp), %rdx
	leaq	.LC8(%rip), %rsi
	xorl	%eax, %eax
	movl	$2, %edi
	call	__printf_chk@PLT
	movl	336(%rsp), %edx
	leaq	.LC9(%rip), %rsi
	xorl	%eax, %eax
	movl	$2, %edi
	call	__printf_chk@PLT
	movl	340(%rsp), %edx
	leaq	.LC10(%rip), %rsi
	xorl	%eax, %eax
	movl	$2, %edi
	call	__printf_chk@PLT
	movq	344(%rsp), %rdx
	leaq	.LC11(%rip), %rsi
	xorl	%eax, %eax
	movl	$2, %edi
	call	__printf_chk@PLT
	movl	352(%rsp), %edx
	leaq	.LC12(%rip), %rsi
	xorl	%eax, %eax
	movl	$2, %edi
	call	__printf_chk@PLT
.L19:
	movl	356(%rsp,%rbx,4), %ecx
	movl	%ebx, %edx
	movq	%rbp, %rsi
	xorl	%eax, %eax
	movl	$2, %edi
	incq	%rbx
	call	__printf_chk@PLT
	cmpq	$3, %rbx
	jne	.L19
	xorl	%ebx, %ebx
	leaq	.LC14(%rip), %rbp
.L20:
	movl	368(%rsp,%rbx,4), %ecx
	movl	%ebx, %edx
	movq	%rbp, %rsi
	xorl	%eax, %eax
	movl	$2, %edi
	incq	%rbx
	call	__printf_chk@PLT
	cmpq	$3, %rbx
	jne	.L20
	movl	380(%rsp), %edx
	leaq	.LC15(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	leaq	.LC2(%rip), %rbp
	leaq	.LC3(%rip), %rbx
	call	__printf_chk@PLT
	movq	384(%rsp), %rdx
	leaq	.LC16(%rip), %rsi
	xorl	%eax, %eax
	movl	$2, %edi
	call	__printf_chk@PLT
	movq	400(%rsp), %rdx
	leaq	.LC17(%rip), %rsi
	xorl	%eax, %eax
	movl	$2, %edi
	call	__printf_chk@PLT
	cmpl	$0, 416(%rsp)
	movq	%rbp, %rdx
	leaq	.LC18(%rip), %rsi
	cmove	%rbx, %rdx
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	420(%rsp), %edx
	leaq	.LC19(%rip), %rsi
	xorl	%eax, %eax
	movl	$2, %edi
	call	__printf_chk@PLT
	cmpl	$0, 424(%rsp)
	movq	%rbp, %rdx
	leaq	.LC20(%rip), %rsi
	cmove	%rbx, %rdx
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	popq	%rax
	.cfi_def_cfa_offset 24
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2043:
	.size	_Z12printDevProp14cudaDeviceProp, .-_Z12printDevProp14cudaDeviceProp
	.globl	_Z30__device_stub__Z8copV_GPUjPjS_jPjS_
	.type	_Z30__device_stub__Z8copV_GPUjPjS_jPjS_, @function
_Z30__device_stub__Z8copV_GPUjPjS_jPjS_:
.LFB2069:
	.cfi_startproc
	endbr64
	subq	$136, %rsp
	.cfi_def_cfa_offset 144
	movl	%edi, 28(%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	28(%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	.L28
	pushq	40(%rsp)
	.cfi_def_cfa_offset 152
	leaq	_Z8copV_GPUjPjS_(%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
.L28:
	movq	120(%rsp), %rax
	subq	%fs:40, %rax
	je	.L30
	call	__stack_chk_fail@PLT
.L30:
	addq	$136, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2069:
	.size	_Z30__device_stub__Z8copV_GPUjPjS_jPjS_, .-_Z30__device_stub__Z8copV_GPUjPjS_jPjS_
	.globl	_Z8copV_GPUjPjS_
	.type	_Z8copV_GPUjPjS_, @function
_Z8copV_GPUjPjS_:
.LFB2070:
	.cfi_startproc
	endbr64
	jmp	_Z30__device_stub__Z8copV_GPUjPjS_jPjS_
	.cfi_endproc
.LFE2070:
	.size	_Z8copV_GPUjPjS_, .-_Z8copV_GPUjPjS_
	.globl	_Z30__device_stub__Z10reduce_GPUPjPj
	.type	_Z30__device_stub__Z10reduce_GPUPjPj, @function
_Z30__device_stub__Z10reduce_GPUPjPj:
.LFB2071:
	.cfi_startproc
	endbr64
	subq	$104, %rsp
	.cfi_def_cfa_offset 112
	movq	%rdi, 8(%rsp)
	leaq	24(%rsp), %rcx
	leaq	16(%rsp), %rdx
	leaq	44(%rsp), %rsi
	leaq	32(%rsp), %rdi
	movq	%fs:40, %rax
	movq	%rax, 88(%rsp)
	xorl	%eax, %eax
	leaq	8(%rsp), %rax
	movl	$1, 40(%rsp)
	movq	%rax, 80(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 32(%rsp)
	movq	%rax, 44(%rsp)
	movl	$1, 52(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L33
	pushq	24(%rsp)
	.cfi_def_cfa_offset 120
	leaq	_Z10reduce_GPUPj(%rip), %rdi
	pushq	24(%rsp)
	.cfi_def_cfa_offset 128
	movq	60(%rsp), %rcx
	movl	68(%rsp), %r8d
	movq	48(%rsp), %rsi
	movl	56(%rsp), %edx
	leaq	96(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 120
	popq	%rdx
	.cfi_def_cfa_offset 112
.L33:
	movq	88(%rsp), %rax
	subq	%fs:40, %rax
	je	.L35
	call	__stack_chk_fail@PLT
.L35:
	addq	$104, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2071:
	.size	_Z30__device_stub__Z10reduce_GPUPjPj, .-_Z30__device_stub__Z10reduce_GPUPjPj
	.globl	_Z10reduce_GPUPj
	.type	_Z10reduce_GPUPj, @function
_Z10reduce_GPUPj:
.LFB2072:
	.cfi_startproc
	endbr64
	jmp	_Z30__device_stub__Z10reduce_GPUPjPj
	.cfi_endproc
.LFE2072:
	.size	_Z10reduce_GPUPj, .-_Z10reduce_GPUPj
	.section	.rodata.str1.1
.LC21:
	.string	"\n-------------------Device %d----------------------\n"
.LC22:
	.string	"\nBest Device found %d\n"
.LC23:
	.string	"Argc=%d Filling array A & B with %d integers %d\n"
.LC24:
	.string	"\nArgc=%d Filling array A & B with %d integers and %d\n\n"
.LC25:
	.string	"Reading input from file %s\n"
.LC26:
	.string	"r"
.LC27:
	.string	"Can't open input file!\n"
.LC28:
	.string	"%s"
.LC29:
	.string	"Starting %d, %d\n"
.LC31:
	.string	"\nDone: time taken for serial version is %3.1f s\n"
.LC32:
	.string	"Number co-primes found by CPU = %d\n\n"
.LC33:
	.string	"Best device %d\n"
.LC34:
	.string	"Number co-primes found by GPU = %d Num reduced %d\n"
.LC35:
	.string	"done: time taken for parallel version is %3.1f s threads %d\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2044:
	.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
	movq	%rsi, %r13
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	xorl	%ebp, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	movl	%edi, %ebx
	xorl	%edi, %edi
	subq	$2312, %rsp
	.cfi_def_cfa_offset 2368
	movq	%fs:40, %rax
	movq	%rax, 2296(%rsp)
	xorl	%eax, %eax
	leaq	1160(%rsp), %r15
	call	time@PLT
	movl	%eax, %edi
	call	srand@PLT
	xorl	%eax, %eax
	leaq	44(%rsp), %rdi
	movl	%eax, 1480(%rsp)
	movl	%eax, 1548(%rsp)
	movl	%eax, 1508(%rsp)
	call	cudaGetDeviceCount@PLT
.L39:
	cmpl	%ebp, 44(%rsp)
	jle	.L64
	leaq	128(%rsp), %r14
	movl	%ebp, %esi
	movq	%r14, %rdi
	call	cudaGetDeviceProperties_v2@PLT
	movl	%ebp, %edx
	leaq	.LC21(%rip), %rsi
	xorl	%eax, %eax
	movl	$2, %edi
	call	__printf_chk@PLT
	subq	$1040, %rsp
	.cfi_def_cfa_offset 3408
	movl	$258, %ecx
	movq	%r14, %rsi
	movq	%rsp, %rdi
	rep movsl
	call	_Z12printDevProp14cudaDeviceProp
	movl	1488(%rsp), %eax
	addq	$1040, %rsp
	.cfi_def_cfa_offset 2368
	cmpl	320(%r15), %eax
	jle	.L40
	movl	388(%r15), %ecx
	cmpl	%ecx, 516(%rsp)
	jle	.L40
	movl	348(%r15), %ecx
	cmpl	%ecx, 476(%rsp)
	jle	.L40
	movl	%ebp, 20(%rsp)
	movl	%eax, %r12d
	movq	%r14, %r15
.L40:
	incl	%ebp
	jmp	.L39
.L64:
	movl	20(%rsp), %edi
	call	cudaSetDevice@PLT
	movl	20(%rsp), %edx
	xorl	%eax, %eax
	movl	$2, %edi
	leaq	.LC22(%rip), %rsi
	call	__printf_chk@PLT
	cmpl	$1, %ebx
	jg	.L42
	movl	$999, %eax
	xorl	%edx, %edx
	xorl	%r13d, %r13d
	divl	%r12d
	incl	%eax
	imull	%r12d, %eax
	movl	%eax, 16(%rsp)
	salq	$2, %rax
	movq	%rax, %rdi
	movq	%rax, (%rsp)
	call	malloc@PLT
	movl	$4000, %edi
	movq	%rax, %rbp
	call	malloc@PLT
	movl	16(%rsp), %r8d
	leal	-1(%rbx), %edx
	movl	$1000, %ecx
	movq	%rax, %r14
	leaq	.LC23(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	$10000000, %ebx
.L43:
	call	rand@PLT
	cltd
	idivl	%ebx
	movl	%edx, 0(%rbp,%r13)
	call	rand@PLT
	cltd
	idivl	%ebx
	movl	%edx, (%r14,%r13)
	addq	$4, %r13
	cmpq	$4000, %r13
	jne	.L43
	movl	$1000, %ebx
	jmp	.L44
.L42:
	cmpl	$2, %ebx
	jne	.L45
	movq	8(%r13), %rdi
	xorl	%r15d, %r15d
	call	atoi@PLT
	xorl	%edx, %edx
	movl	%eax, %r13d
	leal	-1(%r13), %eax
	movq	%r13, %rbx
	salq	$2, %r13
	divl	%r12d
	incl	%eax
	imull	%r12d, %eax
	movl	%eax, 16(%rsp)
	salq	$2, %rax
	movq	%rax, %rdi
	movq	%rax, (%rsp)
	call	malloc@PLT
	movq	%r13, %rdi
	movq	%rax, %rbp
	call	malloc@PLT
	movl	16(%rsp), %r8d
	movl	%ebx, %ecx
	movl	$1, %edx
	movq	%rax, %r14
	leaq	.LC24(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L46:
	cmpl	%ebx, %r15d
	jnb	.L44
	call	rand@PLT
	movl	$10000000, %ecx
	cltd
	idivl	%ecx
	movl	%edx, 0(%rbp,%r15,4)
	call	rand@PLT
	movl	$10000000, %ecx
	cltd
	idivl	%ecx
	movl	%edx, (%r14,%r15,4)
	incq	%r15
	jmp	.L46
.L45:
	movq	16(%r13), %rdx
	leaq	.LC25(%rip), %rsi
	xorl	%eax, %eax
	xorl	%ebx, %ebx
	movl	$2, %edi
	leaq	.LC28(%rip), %rbp
	call	__printf_chk@PLT
	movq	16(%r13), %rdi
	leaq	.LC26(%rip), %rsi
	call	fopen@PLT
	movq	%rax, 8(%rsp)
	testq	%rax, %rax
	jne	.L48
	movq	stderr(%rip), %rdi
	leaq	.LC27(%rip), %rdx
	movl	$2, %esi
	call	__fprintf_chk@PLT
	movl	$1, %edi
	call	exit@PLT
.L48:
	movq	8(%rsp), %rdi
	xorl	%eax, %eax
	leaq	2196(%rsp), %rdx
	movq	%rbp, %rsi
	call	__isoc23_fscanf@PLT
	incl	%eax
	je	.L65
	incl	%ebx
	jmp	.L48
.L65:
	sarl	%ebx
	xorl	%edx, %edx
	movq	8(%rsp), %rdi
	xorl	%r15d, %r15d
	leal	-1(%rbx), %eax
	movslq	%ebx, %r13
	divl	%r12d
	salq	$2, %r13
	incl	%eax
	imull	%r12d, %eax
	movl	%eax, 16(%rsp)
	call	rewind@PLT
	movl	16(%rsp), %eax
	movl	16(%rsp), %ecx
	movl	%ebx, %edx
	leaq	.LC29(%rip), %rsi
	movl	$2, %edi
	salq	$2, %rax
	movq	%rax, (%rsp)
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movq	(%rsp), %rdi
	call	malloc@PLT
	movq	%r13, %rdi
	movq	%rax, %rbp
	call	malloc@PLT
	movq	%rax, %r14
.L50:
	leaq	2196(%rsp), %rax
	movq	8(%rsp), %rdi
	leaq	.LC28(%rip), %rsi
	movq	%rax, 24(%rsp)
	movq	%rax, %rdx
	xorl	%eax, %eax
	call	__isoc23_fscanf@PLT
	incl	%eax
	je	.L66
	movq	24(%rsp), %rdi
	call	atoi@PLT
	movl	%r15d, %edx
	sarl	%edx
	movslq	%edx, %rdx
	testl	$1, %r15d
	je	.L51
	movl	%eax, (%r14,%rdx,4)
	jmp	.L52
.L51:
	movl	%eax, 0(%rbp,%rdx,4)
.L52:
	incl	%r15d
	jmp	.L50
.L66:
	movq	8(%rsp), %rdi
	call	fclose@PLT
.L44:
	xorl	%esi, %esi
	leaq	80(%rsp), %rdi
	leaq	96(%rsp), %r15
	call	gettimeofday@PLT
	movq	%r14, %rdx
	movq	%rbp, %rsi
	movl	%ebx, %edi
	call	_Z4copVjPjS_
	xorl	%esi, %esi
	movq	%r15, %rdi
	movl	%eax, 8(%rsp)
	call	gettimeofday@PLT
	movq	96(%rsp), %rax
	subq	80(%rsp), %rax
	leaq	.LC31(%rip), %rsi
	cvtsi2sdq	%rax, %xmm0
	movq	104(%rsp), %rax
	subq	88(%rsp), %rax
	movl	$2, %edi
	mulsd	.LC30(%rip), %xmm0
	cvtsi2sdq	%rax, %xmm1
	movb	$1, %al
	addsd	%xmm1, %xmm0
	divsd	.LC30(%rip), %xmm0
	cvtsd2ss	%xmm0, %xmm0
	cvtss2sd	%xmm0, %xmm0
	call	__printf_chk@PLT
	movl	8(%rsp), %edx
	movl	$2, %edi
	xorl	%eax, %eax
	leaq	.LC32(%rip), %rsi
	call	__printf_chk@PLT
	movl	20(%rsp), %edx
	movl	$2, %edi
	xorl	%eax, %eax
	leaq	.LC33(%rip), %rsi
	call	__printf_chk@PLT
	xorl	%esi, %esi
	movq	%r15, %rdi
	call	gettimeofday@PLT
	movq	(%rsp), %rsi
	leaq	48(%rsp), %rdi
	call	cudaMalloc@PLT
	movl	$261, %esi
	movl	%eax, %edi
	call	_ZL13gpuCheckError9cudaErrorPKci.constprop.0
	movq	%r13, %rsi
	leaq	56(%rsp), %rdi
	call	cudaMalloc@PLT
	movl	$263, %esi
	movl	%eax, %edi
	call	_ZL13gpuCheckError9cudaErrorPKci.constprop.0
	movq	48(%rsp), %rcx
	movq	(%rsp), %rdx
	movl	%ebx, %eax
	xorl	%esi, %esi
	subq	%r13, %rdx
	leaq	(%rcx,%rax,4), %rdi
	call	cudaMemset@PLT
	movl	$264, %esi
	movl	%eax, %edi
	call	_ZL13gpuCheckError9cudaErrorPKci.constprop.0
	movq	(%rsp), %rdx
	movq	48(%rsp), %rdi
	movq	%rbp, %rsi
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	movl	$267, %esi
	movl	%eax, %edi
	call	_ZL13gpuCheckError9cudaErrorPKci.constprop.0
	movq	56(%rsp), %rdi
	movq	%r13, %rdx
	movq	%r14, %rsi
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	movl	$268, %esi
	movl	%eax, %edi
	call	_ZL13gpuCheckError9cudaErrorPKci.constprop.0
	xorl	%edx, %edx
	leal	-1(%rbx,%r12), %eax
	xorl	%r9d, %r9d
	divl	%r12d
	movl	%r12d, 112(%rsp)
	xorl	%r8d, %r8d
	movl	%eax, %r13d
	movabsq	$4294967297, %rax
	movq	%rax, 116(%rsp)
	movl	120(%rsp), %ecx
	movq	%rax, 72(%rsp)
	movq	112(%rsp), %rdx
	movl	%r13d, 68(%rsp)
	movl	76(%rsp), %esi
	movq	68(%rsp), %rdi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L54
	movq	56(%rsp), %rdx
	movq	48(%rsp), %rsi
	movl	%ebx, %edi
	call	_Z30__device_stub__Z8copV_GPUjPjS_jPjS_
.L54:
	call	cudaDeviceSynchronize@PLT
	movl	%r12d, 112(%rsp)
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movabsq	$4294967297, %rax
	movl	%r13d, 68(%rsp)
	movq	%rax, 116(%rsp)
	movl	120(%rsp), %ecx
	movq	%rax, 72(%rsp)
	movq	112(%rsp), %rdx
	movq	68(%rsp), %rdi
	movl	76(%rsp), %esi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L55
	movq	48(%rsp), %rdi
	call	_Z30__device_stub__Z10reduce_GPUPjPj
.L55:
	call	cudaDeviceSynchronize@PLT
	movq	(%rsp), %rdx
	movq	48(%rsp), %rsi
	movq	%rbp, %rdi
	movl	$2, %ecx
	call	cudaMemcpy@PLT
	movl	$281, %esi
	movl	%eax, %edi
	call	_ZL13gpuCheckError9cudaErrorPKci.constprop.0
	xorl	%ecx, %ecx
	xorl	%esi, %esi
.L56:
	movl	16(%rsp), %ebx
	movl	%ecx, %eax
	cmpl	%ebx, %ecx
	jnb	.L67
	xorl	%edx, %edx
	divl	%r12d
	testl	%edx, %edx
	jne	.L57
	addl	0(%rbp,%rcx,4), %esi
.L57:
	incq	%rcx
	jmp	.L56
.L67:
	movl	%ebx, %ecx
	movl	%esi, %edx
	movl	$2, %edi
	xorl	%eax, %eax
	leaq	.LC34(%rip), %rsi
	call	__printf_chk@PLT
	xorl	%esi, %esi
	leaq	112(%rsp), %rdi
	call	gettimeofday@PLT
	movq	112(%rsp), %rax
	subq	96(%rsp), %rax
	xorl	%edx, %edx
	cvtsi2sdq	%rax, %xmm0
	movq	120(%rsp), %rax
	subq	104(%rsp), %rax
	leaq	.LC35(%rip), %rsi
	mulsd	.LC30(%rip), %xmm0
	cvtsi2sdq	%rax, %xmm1
	movl	$2, %edi
	movb	$1, %al
	addsd	%xmm1, %xmm0
	divsd	.LC30(%rip), %xmm0
	cvtsd2ss	%xmm0, %xmm0
	cvtss2sd	%xmm0, %xmm0
	call	__printf_chk@PLT
	movq	48(%rsp), %rdi
	call	cudaFree@PLT
	movl	$301, %esi
	movl	%eax, %edi
	call	_ZL13gpuCheckError9cudaErrorPKci.constprop.0
	movq	56(%rsp), %rdi
	call	cudaFree@PLT
	movl	$302, %esi
	movl	%eax, %edi
	call	_ZL13gpuCheckError9cudaErrorPKci.constprop.0
	movq	%rbp, %rdi
	call	free@PLT
	movq	%r14, %rdi
	call	free@PLT
	movq	2296(%rsp), %rax
	subq	%fs:40, %rax
	je	.L59
	call	__stack_chk_fail@PLT
.L59:
	addq	$2312, %rsp
	.cfi_def_cfa_offset 56
	xorl	%eax, %eax
	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
.LFE2044:
	.size	main, .-main
	.section	.rodata.str1.1
.LC36:
	.string	"_Z10reduce_GPUPj"
.LC37:
	.string	"_Z8copV_GPUjPjS_"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2074:
	.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	.LC36(%rip), %rdx
	movq	%rax, %rdi
	movq	%rax, %rbx
	pushq	$0
	.cfi_def_cfa_offset 40
	movq	%rdx, %rcx
	leaq	_Z10reduce_GPUPj(%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	.LC37(%rip), %rdx
	orl	$-1, %r8d
	leaq	_Z8copV_GPUjPjS_(%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
.LFE2074:
	.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
.LC30:
	.long	0
	.long	1093567616
	.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 <hip/hip_runtime.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>	//rand, atoi
#include <sys/time.h>
#include <unistd.h>

#define MAX_N 10000000

#define GPU_CHECKERROR(err) (gpuCheckError(err,__FILE__,__LINE__))

static void gpuCheckError(hipError_t err, const char * file, int line){
	if(err != hipSuccess ){
		printf("%s in file  %s at line %d\n",
				hipGetErrorString(err), __FILE__, line);
		exit(EXIT_FAILURE);
	}
}

//Determines if A & B are co-prime using the Euclidean Algorithm.
unsigned int __cop(unsigned int A, unsigned int B){
	unsigned int T;
	//Swap 'em
	if(B>A){
		T=B;
		B=A;
		A=T;
		
	}
	while(B){
		T=B;
		B=A%B;
		A = T;
	}
	return A==1;
}

//Finds the number of co-prime  numbers in two vectors A and B
unsigned int copV(unsigned int N, unsigned int * A, unsigned int * B){
	unsigned int numCop = 0;
	for(int i = 0 ; i < N; i++){
		numCop += __cop(A[i], B[i]);
	}
	return numCop;
}

//Calculates if correspoding numbers in two arrays are coprime
//and stores the result in the first array.
__global__ void copV_GPU(unsigned int N, unsigned int * A, unsigned int * B){
	unsigned int n = blockDim.x * blockIdx.x + threadIdx.x;
	
	if(n >= N) return;
		
	int a = A[n];
	int b = B[n];
		
	unsigned int t;
	//Swap 'em
	if(b>a){
		t=b;
		b=a;
		a=t;
		
	}
	while(b){
		t=b;
		b=a%b;
		a = t;
	}
	A[n] = (a==1);
	
}

//Performs sum reduction of an array A. A must be aligned to blockDim.x
__global__ void reduce_GPU(unsigned int * A){
    unsigned int tid = threadIdx.x;
    unsigned int idx = blockDim.x*blockIdx.x + threadIdx.x;
   
    for( unsigned int S = blockDim.x/2; S > 0; S/=2 ){
        if( tid < S )
            A[idx] += A[idx+S];

        //Make sure all threads have finished before we continue
        __syncthreads();
    }
    
}

//Code to display device properties.
// http://gpucoder.livejournal.com/1064.html
void printDevProp(hipDeviceProp_t devProp)
{
    printf("Major revision number:         %d\n",  devProp.major);
    printf("Minor revision number:         %d\n",  devProp.minor);
    printf("Name:                          %s\n",  devProp.name);
    printf("Total global memory:           %lu\n",  devProp.totalGlobalMem);
    printf("Total shared memory per block: %lu\n",  devProp.sharedMemPerBlock);
    printf("Total registers per block:     %d\n",  devProp.regsPerBlock);
    printf("Warp size:                     %d\n",  devProp.warpSize);
    printf("Maximum memory pitch:          %lu\n",  devProp.memPitch);
    printf("Maximum threads per block:     %d\n",  devProp.maxThreadsPerBlock);
    for (int i = 0; i < 3; ++i)
    printf("Maximum dimension %d of block:  %d\n", i, devProp.maxThreadsDim[i]);
    for (int i = 0; i < 3; ++i)
    printf("Maximum dimension %d of grid:   %d\n", i, devProp.maxGridSize[i]);
    printf("Clock rate:                    %d\n",  devProp.clockRate);
    printf("Total constant memory:         %lu\n",  devProp.totalConstMem);
    printf("Texture alignment:             %lu\n",  devProp.textureAlignment);
    printf("Concurrent copy and execution: %s\n",  (devProp.deviceOverlap ? "Yes" : "No"));
    printf("Number of multiprocessors:     %d\n",  devProp.multiProcessorCount);
    printf("Kernel execution timeout:      %s\n",  (devProp.kernelExecTimeoutEnabled ? "Yes" : "No"));
    return;
}

int main(int argc, char * argv[]){

	//Create an array on N
	unsigned int N;
	unsigned int N2;
	size_t sz;
    size_t sz2;
	unsigned int * h_A;
	unsigned int * h_B;

	unsigned int nThreads ;
	
	srand(time(NULL));
	
	//Query device properties.
	int dev_count, best_dev;
	hipDeviceProp_t dev_prop_curr;
	hipDeviceProp_t dev_prop_max_;
	hipDeviceProp_t *dev_prop_max = &dev_prop_max_;
	dev_prop_max->maxThreadsPerBlock = 0;
	dev_prop_max->multiProcessorCount = 0;
	dev_prop_max->clockRate = 0;
	hipGetDeviceCount( &dev_count );
	for( int i = 0; i < dev_count ; i++ ){
		hipGetDeviceProperties( &dev_prop_curr , i);
		printf("\n-------------------Device %d----------------------\n",i);
		printDevProp( dev_prop_curr );
		//Select the best device
		if( 	(dev_prop_curr.maxThreadsPerBlock > dev_prop_max->maxThreadsPerBlock) &&
			(dev_prop_curr.multiProcessorCount > dev_prop_max->multiProcessorCount) && 
			(dev_prop_curr.clockRate > dev_prop_max->clockRate) ){
			best_dev = i;
			dev_prop_max = &dev_prop_curr;
			nThreads = dev_prop_curr.maxThreadsPerBlock;
			}
		
	}
	hipSetDevice(best_dev);
	printf("\nBest Device found %d\n",best_dev);
	
	if(argc < 2){
		//Fill the arrays with 1000 random numbers
		N = 1000;
		N2=((N-1)/nThreads + 1)*nThreads;
        sz = sizeof( unsigned int ) * N;
		//Make sure array size is a multiple of blockDim;
        sz2 = sizeof( unsigned int ) * N2;
    
		h_A = (unsigned int *)malloc( sz2 );
		h_B = (unsigned int *)malloc( sz );
		
		printf("Argc=%d Filling array A & B with %d integers %d\n", argc-1, N,N2);
		for(unsigned int i = 0 ; i < N; i++){
			h_A[i] = rand() % MAX_N;
			h_B[i] = rand() % MAX_N;
			//printf("%d\n%d\n",h_A[i],h_B[i]);
		}	
	}else if(argc == 2){
		//Fill the arrays with random numbers
		N = (unsigned int) atoi( argv[1] );
		N2=((N-1)/nThreads + 1)*nThreads;
		sz = sizeof( unsigned int ) * N;
        //Make sure array size is a multiple of blockDim;
        sz2 = sizeof( unsigned int ) * N2;
    
		h_A = (unsigned int *)malloc( sz2 );
		h_B = (unsigned int *)malloc( sz );
	
		printf("\nArgc=%d Filling array A & B with %d integers and %d\n\n", argc-1, N,N2);
		for(unsigned int i = 0 ; i < N; i++){
			h_A[i] = rand() % MAX_N;
			h_B[i] = rand() % MAX_N;
			//printf("%d\n%d\n",h_A[i],h_B[i]);
		}
	}else if ( argc > 2){
	
		//Read from file
		printf( "Reading input from file %s\n",argv[2]);
		
		
		FILE *fr;
		fr = fopen(argv[2], "r");
		if( !fr ){
			fprintf(stderr, "Can't open input file!\n");
			exit(1);
		}
		
		char oneword[100];
		int count = 0;
		
		//First count the number of entries in the file
		while(fscanf(fr,"%s",oneword) != EOF ){
			count ++;
		}
		N = count/2;
		N2=((N-1)/nThreads + 1)*nThreads;
		rewind(fr);
		//Now re-allocate a new array
		sz = sizeof( unsigned int ) * N;
        //Make sure array size is a multiple of blockDim;
        sz2 = sizeof( unsigned int ) * N2;
		printf("Starting %d, %d\n", N,N2);

		h_A = (unsigned int *)malloc( sz2 );
		h_B = (unsigned int *)malloc( sz );
		
		count = 0;
		while(fscanf(fr,"%s",oneword) != EOF ){
		
			//sprintf("%s\n",oneword);
			if(count % 2){
				h_B[count/2] = atoi( oneword );
				//printf("%d\n",h_B[count/2]);
			}
			else{
				h_A[count/2] = atoi( oneword );
				//printf("%d ",h_A[count/2]);
			}
			count ++;
		}
		
		
		fclose(fr);
	}
	
	//--------------------------------------------------------------------------
	//Serial Version
	//
	//--------------------------------------------------------------------------
	struct timeval t0, t1, t2;
	gettimeofday(&t0,0);
	unsigned int numCoPrimes = copV(N, h_A, h_B);
	gettimeofday(&t1,0);
	float timdiff1 = (1000000.0*(t1.tv_sec - t0.tv_sec) + (t1.tv_usec - t0.tv_usec)) / 1000000.0;
	printf ("\nDone: time taken for serial version is %3.1f s\n", timdiff1);
	printf("Number co-primes found by CPU = %d\n\n", numCoPrimes);


	//--------------------------------------------------------------------------
	//Parallel Version
	//
	//--------------------------------------------------------------------------
	
	printf("Best device %d\n",best_dev);
	//Allocate vectors and count in device memory
	gettimeofday(&t1,0);
	unsigned int * d_A;
	GPU_CHECKERROR(  hipMalloc(&d_A,sz2));
	unsigned int * d_B;
	GPU_CHECKERROR(  hipMalloc(&d_B,sz));
	GPU_CHECKERROR(  hipMemset((void *) &d_A[N],0,sz2-sz) );		//Pad with 0's
	
	//Copy vectors from host to device memory
	GPU_CHECKERROR(  hipMemcpy(d_A,h_A,sz2,hipMemcpyHostToDevice));
	GPU_CHECKERROR(  hipMemcpy(d_B,h_B,sz,hipMemcpyHostToDevice));
	
	unsigned int nBlocks = (N + nThreads - 1)/nThreads;

    copV_GPU<<< nBlocks, nThreads>>>(N,d_A,d_B);

	hipDeviceSynchronize();	
	
	//Reduce 'em
	reduce_GPU<<< nBlocks, nThreads>>>(d_A);
	hipDeviceSynchronize();
	
	//Fetch the results;
	GPU_CHECKERROR(  hipMemcpy( (void *) h_A,
						(void *) d_A,
						sz2,
						hipMemcpyDeviceToHost) );
				
	int sum = 0;
	for( int i = 0; i < N2 ; i++ ){
		if(!(i%nThreads)){
			sum += h_A[i];
			//printf("\n\n");
		}
		//printf("h_A[%d]=%u\t",i,h_A[i]);
	}
	printf("Number co-primes found by GPU = %d Num reduced %d\n", sum, N2);	
	
	gettimeofday(&t2,0);
    float timdiff2 = (1000000.0*(t2.tv_sec - t1.tv_sec) + (t2.tv_usec - t1.tv_usec)) / 1000000.0;
    printf ("done: time taken for parallel version is %3.1f s threads %d\n", timdiff2, 0);

	//Free device memory
	GPU_CHECKERROR( hipFree(d_A) );
	GPU_CHECKERROR( hipFree(d_B) );
	
	
	//Free Host memory
	free(h_A);
	free(h_B);
}


	.text
	.file	"copV.hip"
	.globl	_Z5__copjj                      # -- Begin function _Z5__copjj
	.type	_Z5__copjj,@function
_Z5__copjj:                             # @_Z5__copjj
	.cfi_startproc
# %bb.0:
	movl	%edi, %edx
	cmpl	%edi, %esi
	movl	%edi, %eax
	cmoval	%esi, %eax
	cmovbl	%esi, %edx
	testl	%edx, %edx
	je	.LBB0_1
.LBB0_2:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movl	%edx, %ecx
	xorl	%edx, %edx
	divl	%ecx
	movl	%ecx, %eax
	testl	%edx, %edx
	jne	.LBB0_2
	jmp	.LBB0_3
.LBB0_1:
	movl	%eax, %ecx
.LBB0_3:                                # %._crit_edge
	xorl	%eax, %eax
	cmpl	$1, %ecx
	sete	%al
	retq
.Lfunc_end0:
	.size	_Z5__copjj, .Lfunc_end0-_Z5__copjj
	.cfi_endproc
                                        # -- End function
	.globl	_Z4copVjPjS_                    # -- Begin function _Z4copVjPjS_
	.type	_Z4copVjPjS_,@function
_Z4copVjPjS_:                           # @_Z4copVjPjS_
	.cfi_startproc
# %bb.0:
	testl	%edi, %edi
	je	.LBB1_1
# %bb.3:                                # %.lr.ph.preheader
	movq	%rdx, %rcx
	movl	%edi, %r8d
	xorl	%r9d, %r9d
	xorl	%edi, %edi
.LBB1_4:                                # %.lr.ph
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB1_6 Depth 2
	movl	(%rsi,%r9,4), %edx
	movl	(%rcx,%r9,4), %r10d
	cmpl	%edx, %r10d
	movl	%edx, %eax
	cmoval	%r10d, %eax
	cmovbl	%r10d, %edx
	testl	%edx, %edx
	je	.LBB1_5
.LBB1_6:                                # %.lr.ph.i
                                        #   Parent Loop BB1_4 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movl	%edx, %r10d
	xorl	%edx, %edx
	divl	%r10d
	movl	%r10d, %eax
	testl	%edx, %edx
	jne	.LBB1_6
	jmp	.LBB1_7
.LBB1_5:                                #   in Loop: Header=BB1_4 Depth=1
	movl	%eax, %r10d
.LBB1_7:                                # %_Z5__copjj.exit
                                        #   in Loop: Header=BB1_4 Depth=1
	xorl	%eax, %eax
	cmpl	$1, %r10d
	sete	%al
	addl	%eax, %edi
	incq	%r9
	cmpq	%r8, %r9
	jne	.LBB1_4
	jmp	.LBB1_2
.LBB1_1:
	xorl	%edi, %edi
.LBB1_2:                                # %._crit_edge
	movl	%edi, %eax
	retq
.Lfunc_end1:
	.size	_Z4copVjPjS_, .Lfunc_end1-_Z4copVjPjS_
	.cfi_endproc
                                        # -- End function
	.globl	_Z23__device_stub__copV_GPUjPjS_ # -- Begin function _Z23__device_stub__copV_GPUjPjS_
	.type	_Z23__device_stub__copV_GPUjPjS_,@function
_Z23__device_stub__copV_GPUjPjS_:       # @_Z23__device_stub__copV_GPUjPjS_
	.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	12(%rsp), %rax
	movl	%edi, (%rax)
	leaq	40(%rsp), %rcx
	movq	%rsi, (%rcx)
	leaq	32(%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	24(%rsp), %r12
	leaq	16(%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	$_Z8copV_GPUjPjS_, %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_end2:
	.size	_Z23__device_stub__copV_GPUjPjS_, .Lfunc_end2-_Z23__device_stub__copV_GPUjPjS_
	.cfi_endproc
                                        # -- End function
	.globl	_Z25__device_stub__reduce_GPUPj # -- Begin function _Z25__device_stub__reduce_GPUPj
	.type	_Z25__device_stub__reduce_GPUPj,@function
_Z25__device_stub__reduce_GPUPj:        # @_Z25__device_stub__reduce_GPUPj
	.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	$64, %rsp
	.cfi_def_cfa_offset 112
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	leaq	24(%rsp), %rax
	movq	%rdi, (%rax)
	movq	%rsp, %rbx
	movq	%rax, (%rbx)
	leaq	48(%rsp), %r14
	leaq	32(%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	$_Z10reduce_GPUPj, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$80, %rsp
	.cfi_adjust_cfa_offset -80
	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_end3:
	.size	_Z25__device_stub__reduce_GPUPj, .Lfunc_end3-_Z25__device_stub__reduce_GPUPj
	.cfi_endproc
                                        # -- End function
	.globl	_Z12printDevProp20hipDeviceProp_tR0600 # -- Begin function _Z12printDevProp20hipDeviceProp_tR0600
	.type	_Z12printDevProp20hipDeviceProp_tR0600,@function
_Z12printDevProp20hipDeviceProp_tR0600: # @_Z12printDevProp20hipDeviceProp_tR0600
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset %rbx, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	leaq	32(%rsp), %rbx
	movl	360(%rbx), %esi
	movl	$.L.str, %edi
	xorl	%eax, %eax
	callq	printf
	movl	364(%rbx), %esi
	movl	$.L.str.1, %edi
	xorl	%eax, %eax
	callq	printf
	movl	$.L.str.2, %edi
	movq	%rbx, %rsi
	xorl	%eax, %eax
	callq	printf
	movq	288(%rbx), %rsi
	movl	$.L.str.3, %edi
	xorl	%eax, %eax
	callq	printf
	movq	296(%rbx), %rsi
	movl	$.L.str.4, %edi
	xorl	%eax, %eax
	callq	printf
	movl	304(%rbx), %esi
	movl	$.L.str.5, %edi
	xorl	%eax, %eax
	callq	printf
	movl	308(%rbx), %esi
	movl	$.L.str.6, %edi
	xorl	%eax, %eax
	callq	printf
	movq	312(%rbx), %rsi
	movl	$.L.str.7, %edi
	xorl	%eax, %eax
	callq	printf
	movl	320(%rbx), %esi
	movl	$.L.str.8, %edi
	xorl	%eax, %eax
	callq	printf
	xorl	%r14d, %r14d
.LBB4_1:                                # =>This Inner Loop Header: Depth=1
	movl	324(%rbx,%r14,4), %edx
	movl	$.L.str.9, %edi
	movl	%r14d, %esi
	xorl	%eax, %eax
	callq	printf
	incq	%r14
	cmpq	$3, %r14
	jne	.LBB4_1
# %bb.2:                                # %.preheader.preheader
	xorl	%r14d, %r14d
.LBB4_3:                                # %.preheader
                                        # =>This Inner Loop Header: Depth=1
	movl	336(%rbx,%r14,4), %edx
	movl	$.L.str.10, %edi
	movl	%r14d, %esi
	xorl	%eax, %eax
	callq	printf
	incq	%r14
	cmpq	$3, %r14
	jne	.LBB4_3
# %bb.4:
	movl	348(%rbx), %esi
	movl	$.L.str.11, %edi
	xorl	%eax, %eax
	callq	printf
	movq	352(%rbx), %rsi
	movl	$.L.str.12, %edi
	xorl	%eax, %eax
	callq	printf
	movq	368(%rbx), %rsi
	movl	$.L.str.13, %edi
	xorl	%eax, %eax
	callq	printf
	cmpl	$0, 384(%rbx)
	movl	$.L.str.16, %r15d
	movl	$.L.str.15, %r14d
	movl	$.L.str.15, %esi
	cmoveq	%r15, %rsi
	movl	$.L.str.14, %edi
	xorl	%eax, %eax
	callq	printf
	movl	388(%rbx), %esi
	movl	$.L.str.17, %edi
	xorl	%eax, %eax
	callq	printf
	cmpl	$0, 392(%rbx)
	cmoveq	%r15, %r14
	movl	$.L.str.18, %edi
	movq	%r14, %rsi
	xorl	%eax, %eax
	callq	printf
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end4:
	.size	_Z12printDevProp20hipDeviceProp_tR0600, .Lfunc_end4-_Z12printDevProp20hipDeviceProp_tR0600
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function main
.LCPI5_0:
	.quad	0x412e848000000000              # double 1.0E+6
	.text
	.globl	main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:                                # %.lr.ph
	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	$4616, %rsp                     # imm = 0x1208
	.cfi_def_cfa_offset 4672
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movq	%rsi, %rbx
	movl	%edi, %r12d
	xorl	%r14d, %r14d
	xorl	%edi, %edi
	callq	time
	movl	%eax, %edi
	callq	srand
	leaq	3144(%rsp), %r15
	movl	%r14d, 320(%r15)
	movl	%r14d, 388(%r15)
	movl	%r14d, 348(%r15)
	leaq	1532(%rsp), %rdi
	callq	hipGetDeviceCount
	leaq	1672(%rsp), %r14
                                        # implicit-def: $eax
	movq	%rax, 1480(%rsp)                # 8-byte Spill
                                        # implicit-def: $r13d
	xorl	%ebp, %ebp
.LBB5_1:                                # =>This Inner Loop Header: Depth=1
	movq	%r14, %rdi
	movl	%ebp, %esi
	callq	hipGetDevicePropertiesR0600
	movl	$.L.str.19, %edi
	movl	%ebp, %esi
	xorl	%eax, %eax
	callq	printf
	movl	$184, %ecx
	movq	%rsp, %rdi
	movq	%r14, %rsi
	rep;movsq (%rsi), %es:(%rdi)
	callq	_Z12printDevProp20hipDeviceProp_tR0600
	movl	1992(%rsp), %eax
	cmpl	320(%r15), %eax
	jle	.LBB5_5
# %bb.2:                                #   in Loop: Header=BB5_1 Depth=1
	movl	2060(%rsp), %ecx
	cmpl	388(%r15), %ecx
	jle	.LBB5_5
# %bb.3:                                #   in Loop: Header=BB5_1 Depth=1
	movl	2020(%rsp), %ecx
	cmpl	348(%r15), %ecx
	jle	.LBB5_5
# %bb.4:                                #   in Loop: Header=BB5_1 Depth=1
	movq	%r14, %r15
	movl	%ebp, %r13d
                                        # kill: def $eax killed $eax def $rax
	movq	%rax, 1480(%rsp)                # 8-byte Spill
.LBB5_5:                                #   in Loop: Header=BB5_1 Depth=1
	incl	%ebp
	cmpl	1532(%rsp), %ebp
	jl	.LBB5_1
# %bb.6:                                # %._crit_edge
	movl	%r13d, %edi
	callq	hipSetDevice
	movl	$.L.str.20, %edi
	movl	%r13d, %esi
	xorl	%eax, %eax
	callq	printf
	cmpl	$2, %r12d
	jge	.LBB5_10
# %bb.7:
	movl	%r13d, 1476(%rsp)               # 4-byte Spill
	xorl	%r13d, %r13d
	movl	$999, %eax                      # imm = 0x3E7
	xorl	%edx, %edx
	movq	1480(%rsp), %rcx                # 8-byte Reload
	divl	%ecx
	movl	%eax, %ebp
	incl	%ebp
	movq	%rcx, 1480(%rsp)                # 8-byte Spill
	imull	%ecx, %ebp
	leaq	(,%rbp,4), %rdi
	movq	%rdi, 1488(%rsp)                # 8-byte Spill
	callq	malloc
	movq	%rax, %rbx
	movl	$4000, %edi                     # imm = 0xFA0
	callq	malloc
	movq	%rax, %r15
	decl	%r12d
	movl	$.L.str.21, %edi
	movl	%r12d, %esi
	movl	$1000, %edx                     # imm = 0x3E8
	movq	%rbp, 1512(%rsp)                # 8-byte Spill
	movl	%ebp, %ecx
	xorl	%eax, %eax
	callq	printf
.LBB5_8:                                # =>This Inner Loop Header: Depth=1
	callq	rand
	cltq
	imulq	$1801439851, %rax, %rcx         # imm = 0x6B5FCA6B
	movq	%rcx, %rdx
	shrq	$63, %rdx
	sarq	$54, %rcx
	addl	%edx, %ecx
	imull	$10000000, %ecx, %ecx           # imm = 0x989680
	subl	%ecx, %eax
	movl	%eax, (%rbx,%r13,4)
	callq	rand
	cltq
	imulq	$1801439851, %rax, %rcx         # imm = 0x6B5FCA6B
	movq	%rcx, %rdx
	shrq	$63, %rdx
	sarq	$54, %rcx
	addl	%edx, %ecx
	imull	$10000000, %ecx, %ecx           # imm = 0x989680
	subl	%ecx, %eax
	movl	%eax, (%r15,%r13,4)
	incq	%r13
	cmpq	$1000, %r13                     # imm = 0x3E8
	jne	.LBB5_8
# %bb.9:                                # %.thread
	leaq	1552(%rsp), %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	movl	$4000, %eax                     # imm = 0xFA0
	movq	%rax, 1504(%rsp)                # 8-byte Spill
	movl	$1000, %r12d                    # imm = 0x3E8
	movl	1476(%rsp), %r13d               # 4-byte Reload
	jmp	.LBB5_22
.LBB5_10:
	movl	%r13d, 1476(%rsp)               # 4-byte Spill
	movq	1480(%rsp), %r14                # 8-byte Reload
	jne	.LBB5_14
# %bb.11:
	movq	8(%rbx), %rdi
	callq	atoi
	movl	%eax, %r12d
	leal	-1(%r12), %eax
	xorl	%edx, %edx
	divl	%r14d
	movl	%eax, %ebp
	incl	%ebp
	imull	%r14d, %ebp
	movl	%r12d, %r13d
	leaq	(,%r13,4), %r15
	leaq	(,%rbp,4), %rdi
	movq	%rdi, 1488(%rsp)                # 8-byte Spill
	callq	malloc
	movq	%rax, %rbx
	movq	%r15, 1504(%rsp)                # 8-byte Spill
	movq	%r15, %rdi
	callq	malloc
	movq	%rax, %r15
	movl	$.L.str.22, %edi
	movl	$1, %esi
	movl	%r12d, %edx
	movq	%rbp, 1512(%rsp)                # 8-byte Spill
	movl	%ebp, %ecx
	xorl	%eax, %eax
	callq	printf
	cmpl	$1, %r12d
	movq	%r14, 1480(%rsp)                # 8-byte Spill
	jae	.LBB5_12
# %bb.38:                               # %.loopexit.thread
	leaq	1552(%rsp), %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	xorl	%ebp, %ebp
	movl	1476(%rsp), %r13d               # 4-byte Reload
	xorl	%r14d, %r14d
	jmp	.LBB5_28
.LBB5_14:
	movq	16(%rbx), %rsi
	movl	$.L.str.23, %edi
	xorl	%eax, %eax
	callq	printf
	movq	16(%rbx), %rdi
	movl	$.L.str.24, %esi
	callq	fopen
	testq	%rax, %rax
	je	.LBB5_39
# %bb.15:
	movq	%rax, %r13
	movl	$-1, %r12d
	leaq	1552(%rsp), %rbx
.LBB5_16:                               # =>This Inner Loop Header: Depth=1
	movl	$.L.str.26, %esi
	movq	%r13, %rdi
	movq	%rbx, %rdx
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	incl	%r12d
	cmpl	$-1, %eax
	jne	.LBB5_16
# %bb.17:
	shrl	%r12d
	leal	-1(%r12), %eax
	xorl	%edx, %edx
	divl	%r14d
	movl	%eax, %ebx
	incl	%ebx
	imull	%r14d, %ebx
	movq	%r13, %rdi
	callq	rewind
	leal	(,%r12,4), %r15d
	leaq	(,%rbx,4), %r14
	movl	$.L.str.27, %edi
	movl	%r12d, %esi
	movq	%rbx, 1512(%rsp)                # 8-byte Spill
	movl	%ebx, %edx
	xorl	%eax, %eax
	callq	printf
	movq	%r14, 1488(%rsp)                # 8-byte Spill
	movq	%r14, %rdi
	callq	malloc
	movq	%rax, %rbx
	movq	%r15, 1504(%rsp)                # 8-byte Spill
	movq	%r15, %rdi
	callq	malloc
	movq	%rax, %r15
	leaq	1552(%rsp), %rdx
	movl	$.L.str.26, %esi
	movq	%r13, %rdi
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	cmpl	$-1, %eax
	je	.LBB5_20
# %bb.18:                               # %.lr.ph184.preheader
	leaq	1552(%rsp), %rbp
	xorl	%r14d, %r14d
.LBB5_19:                               # %.lr.ph184
                                        # =>This Inner Loop Header: Depth=1
	movq	%rbp, %rdi
	callq	atoi
	movl	%r14d, %ecx
	shrl	%ecx
	testb	$1, %r14b
	movq	%r15, %rdx
	cmoveq	%rbx, %rdx
	movl	%eax, (%rdx,%rcx,4)
	incl	%r14d
	movl	$.L.str.26, %esi
	movq	%r13, %rdi
	movq	%rbp, %rdx
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	cmpl	$-1, %eax
	jne	.LBB5_19
.LBB5_20:                               # %._crit_edge185
	movq	%r13, %rdi
	callq	fclose
	jmp	.LBB5_21
.LBB5_12:                               # %.lr.ph188.preheader
	xorl	%ebp, %ebp
.LBB5_13:                               # %.lr.ph188
                                        # =>This Inner Loop Header: Depth=1
	callq	rand
	cltq
	imulq	$1801439851, %rax, %rcx         # imm = 0x6B5FCA6B
	movq	%rcx, %rdx
	shrq	$63, %rdx
	sarq	$54, %rcx
	addl	%edx, %ecx
	imull	$10000000, %ecx, %ecx           # imm = 0x989680
	subl	%ecx, %eax
	movl	%eax, (%rbx,%rbp,4)
	callq	rand
	cltq
	imulq	$1801439851, %rax, %rcx         # imm = 0x6B5FCA6B
	movq	%rcx, %rdx
	shrq	$63, %rdx
	sarq	$54, %rcx
	addl	%edx, %ecx
	imull	$10000000, %ecx, %ecx           # imm = 0x989680
	subl	%ecx, %eax
	movl	%eax, (%r15,%rbp,4)
	incq	%rbp
	cmpq	%rbp, %r13
	jne	.LBB5_13
.LBB5_21:                               # %.loopexit
	xorl	%r14d, %r14d
	leaq	1552(%rsp), %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	movl	$0, %ebp
	testl	%r12d, %r12d
	movl	1476(%rsp), %r13d               # 4-byte Reload
	je	.LBB5_28
.LBB5_22:                               # %.lr.ph.preheader.i
	movl	%r12d, %ecx
	xorl	%esi, %esi
	xorl	%ebp, %ebp
.LBB5_23:                               # %.lr.ph.i
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB5_25 Depth 2
	movl	(%rbx,%rsi,4), %edx
	movl	(%r15,%rsi,4), %edi
	cmpl	%edx, %edi
	movl	%edx, %eax
	cmoval	%edi, %eax
	cmovbl	%edi, %edx
	testl	%edx, %edx
	je	.LBB5_24
.LBB5_25:                               # %.lr.ph.i.i
                                        #   Parent Loop BB5_23 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movl	%edx, %edi
	xorl	%edx, %edx
	divl	%edi
	movl	%edi, %eax
	testl	%edx, %edx
	jne	.LBB5_25
	jmp	.LBB5_26
.LBB5_24:                               #   in Loop: Header=BB5_23 Depth=1
	movl	%eax, %edi
.LBB5_26:                               # %_Z5__copjj.exit.i
                                        #   in Loop: Header=BB5_23 Depth=1
	xorl	%eax, %eax
	cmpl	$1, %edi
	sete	%al
	addl	%eax, %ebp
	incq	%rsi
	cmpq	%rcx, %rsi
	jne	.LBB5_23
# %bb.27:
	movl	%r12d, %r14d
.LBB5_28:                               # %_Z4copVjPjS_.exit
	leaq	1536(%rsp), %r12
	movq	%r12, %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	movq	(%r12), %rax
	subq	1552(%rsp), %rax
	cvtsi2sd	%rax, %xmm0
	movq	8(%r12), %rax
	movsd	.LCPI5_0(%rip), %xmm2           # xmm2 = mem[0],zero
	subq	1560(%rsp), %rax
	mulsd	%xmm2, %xmm0
	cvtsi2sd	%rax, %xmm1
	addsd	%xmm0, %xmm1
	divsd	%xmm2, %xmm1
	xorps	%xmm0, %xmm0
	cvtsd2ss	%xmm1, %xmm0
	cvtss2sd	%xmm0, %xmm0
	movl	$.L.str.28, %edi
	movb	$1, %al
	callq	printf
	movl	$.L.str.29, %edi
	movl	%ebp, %esi
	xorl	%eax, %eax
	callq	printf
	movl	$.L.str.30, %edi
	movl	%r13d, %esi
	xorl	%eax, %eax
	callq	printf
	movq	%r12, %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	leaq	1496(%rsp), %rbp
	movq	%rbp, %rdi
	movq	1488(%rsp), %r13                # 8-byte Reload
	movq	%r13, %rsi
	callq	hipMalloc
	movl	%eax, %edi
	movl	$263, %esi                      # imm = 0x107
	callq	_ZL13gpuCheckError10hipError_tPKci
	leaq	1520(%rsp), %rdi
	movq	1504(%rsp), %r12                # 8-byte Reload
	movq	%r12, %rsi
	callq	hipMalloc
	movl	%eax, %edi
	movl	$265, %esi                      # imm = 0x109
	callq	_ZL13gpuCheckError10hipError_tPKci
	movl	%r14d, %edi
	shlq	$2, %rdi
	addq	(%rbp), %rdi
	movq	%r13, %rdx
	subq	%r12, %rdx
	xorl	%esi, %esi
	callq	hipMemset
	movl	%eax, %edi
	movl	$266, %esi                      # imm = 0x10A
	callq	_ZL13gpuCheckError10hipError_tPKci
	movq	(%rbp), %rdi
	movq	%rbx, %rsi
	movq	%r13, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	movl	%eax, %edi
	movl	$269, %esi                      # imm = 0x10D
	callq	_ZL13gpuCheckError10hipError_tPKci
	leaq	1520(%rsp), %rax
	movq	(%rax), %rdi
	movq	%r15, %rsi
	movq	%r12, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	movl	%eax, %edi
	movl	$270, %esi                      # imm = 0x10E
	callq	_ZL13gpuCheckError10hipError_tPKci
	movq	1480(%rsp), %rcx                # 8-byte Reload
	leal	(%rcx,%r14), %eax
	decl	%eax
	xorl	%edx, %edx
	divl	%ecx
	movl	%eax, %ebp
	btsq	$32, %rbp
	movl	%ecx, %r13d
	btsq	$32, %r13
	movq	%rbp, %rdi
	movl	$1, %esi
	movq	%r13, %rdx
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB5_30
# %bb.29:
	movq	1496(%rsp), %rsi
	movq	1520(%rsp), %rdx
	movl	%r14d, %edi
	callq	_Z23__device_stub__copV_GPUjPjS_
.LBB5_30:
	callq	hipDeviceSynchronize
	xorl	%r12d, %r12d
	movq	%rbp, %rdi
	movl	$1, %esi
	movq	%r13, %rdx
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB5_32
# %bb.31:
	movq	1496(%rsp), %rdi
	callq	_Z25__device_stub__reduce_GPUPj
.LBB5_32:
	callq	hipDeviceSynchronize
	movq	1496(%rsp), %rsi
	movq	%rbx, %rdi
	movq	1488(%rsp), %rdx                # 8-byte Reload
	movl	$2, %ecx
	callq	hipMemcpy
	movl	%eax, %edi
	movl	$286, %esi                      # imm = 0x11E
	callq	_ZL13gpuCheckError10hipError_tPKci
	movq	1512(%rsp), %r8                 # 8-byte Reload
	testl	%r8d, %r8d
	movq	1480(%rsp), %rdi                # 8-byte Reload
	je	.LBB5_37
# %bb.33:                               # %.lr.ph193.preheader
	movl	%r8d, %esi
	xorl	%ecx, %ecx
	xorl	%r12d, %r12d
.LBB5_34:                               # %.lr.ph193
                                        # =>This Inner Loop Header: Depth=1
	movl	%ecx, %eax
	xorl	%edx, %edx
	divl	%edi
	testl	%edx, %edx
	jne	.LBB5_36
# %bb.35:                               #   in Loop: Header=BB5_34 Depth=1
	addl	(%rbx,%rcx,4), %r12d
.LBB5_36:                               #   in Loop: Header=BB5_34 Depth=1
	incq	%rcx
	cmpq	%rcx, %rsi
	jne	.LBB5_34
.LBB5_37:                               # %._crit_edge194
	movl	$.L.str.32, %edi
	movl	%r12d, %esi
	movl	%r8d, %edx
	xorl	%eax, %eax
	callq	printf
	leaq	1656(%rsp), %r14
	movq	%r14, %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	movq	(%r14), %rax
	movq	8(%r14), %rcx
	subq	1536(%rsp), %rax
	cvtsi2sd	%rax, %xmm0
	subq	1544(%rsp), %rcx
	movsd	.LCPI5_0(%rip), %xmm2           # xmm2 = mem[0],zero
	mulsd	%xmm2, %xmm0
	cvtsi2sd	%rcx, %xmm1
	addsd	%xmm0, %xmm1
	divsd	%xmm2, %xmm1
	xorps	%xmm0, %xmm0
	cvtsd2ss	%xmm1, %xmm0
	cvtss2sd	%xmm0, %xmm0
	movl	$.L.str.33, %edi
	xorl	%esi, %esi
	movb	$1, %al
	callq	printf
	movq	1496(%rsp), %rdi
	callq	hipFree
	movl	%eax, %edi
	movl	$303, %esi                      # imm = 0x12F
	callq	_ZL13gpuCheckError10hipError_tPKci
	movq	1520(%rsp), %rdi
	callq	hipFree
	movl	%eax, %edi
	movl	$304, %esi                      # imm = 0x130
	callq	_ZL13gpuCheckError10hipError_tPKci
	movq	%rbx, %rdi
	callq	free
	movq	%r15, %rdi
	callq	free
	xorl	%eax, %eax
	addq	$4616, %rsp                     # imm = 0x1208
	.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
.LBB5_39:
	.cfi_def_cfa_offset 4672
	movq	stderr(%rip), %rcx
	movl	$.L.str.25, %edi
	movl	$23, %esi
	movl	$1, %edx
	callq	fwrite@PLT
	movl	$1, %edi
	callq	exit
.Lfunc_end5:
	.size	main, .Lfunc_end5-main
	.cfi_endproc
                                        # -- End function
	.type	_ZL13gpuCheckError10hipError_tPKci,@function # -- Begin function _ZL13gpuCheckError10hipError_tPKci
_ZL13gpuCheckError10hipError_tPKci:     # @_ZL13gpuCheckError10hipError_tPKci
	.cfi_startproc
# %bb.0:
	testl	%edi, %edi
	jne	.LBB6_2
# %bb.1:
	retq
.LBB6_2:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset %rbx, -16
	movl	%esi, %ebx
	callq	hipGetErrorString
	movl	$.L.str.34, %edi
	movl	$.L.str.31, %edx
	movq	%rax, %rsi
	movl	%ebx, %ecx
	xorl	%eax, %eax
	callq	printf
	movl	$1, %edi
	callq	exit
.Lfunc_end6:
	.size	_ZL13gpuCheckError10hipError_tPKci, .Lfunc_end6-_ZL13gpuCheckError10hipError_tPKci
	.cfi_endproc
                                        # -- End function
	.p2align	4, 0x90                         # -- Begin function __hip_module_ctor
	.type	__hip_module_ctor,@function
__hip_module_ctor:                      # @__hip_module_ctor
	.cfi_startproc
# %bb.0:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	subq	$32, %rsp
	.cfi_def_cfa_offset 48
	.cfi_offset %rbx, -16
	movq	__hip_gpubin_handle(%rip), %rbx
	testq	%rbx, %rbx
	jne	.LBB7_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rbx
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB7_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z8copV_GPUjPjS_, %esi
	movl	$.L__unnamed_1, %edx
	movl	$.L__unnamed_1, %ecx
	movq	%rbx, %rdi
	movl	$-1, %r8d
	xorl	%r9d, %r9d
	callq	__hipRegisterFunction
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z10reduce_GPUPj, %esi
	movl	$.L__unnamed_2, %edx
	movl	$.L__unnamed_2, %ecx
	movq	%rbx, %rdi
	movl	$-1, %r8d
	xorl	%r9d, %r9d
	callq	__hipRegisterFunction
	movl	$__hip_module_dtor, %edi
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	popq	%rbx
	.cfi_def_cfa_offset 8
	jmp	atexit                          # TAILCALL
.Lfunc_end7:
	.size	__hip_module_ctor, .Lfunc_end7-__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	.LBB8_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
.LBB8_2:
	retq
.Lfunc_end8:
	.size	__hip_module_dtor, .Lfunc_end8-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	_Z8copV_GPUjPjS_,@object        # @_Z8copV_GPUjPjS_
	.section	.rodata,"a",@progbits
	.globl	_Z8copV_GPUjPjS_
	.p2align	3, 0x0
_Z8copV_GPUjPjS_:
	.quad	_Z23__device_stub__copV_GPUjPjS_
	.size	_Z8copV_GPUjPjS_, 8

	.type	_Z10reduce_GPUPj,@object        # @_Z10reduce_GPUPj
	.globl	_Z10reduce_GPUPj
	.p2align	3, 0x0
_Z10reduce_GPUPj:
	.quad	_Z25__device_stub__reduce_GPUPj
	.size	_Z10reduce_GPUPj, 8

	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"Major revision number:         %d\n"
	.size	.L.str, 35

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"Minor revision number:         %d\n"
	.size	.L.str.1, 35

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"Name:                          %s\n"
	.size	.L.str.2, 35

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"Total global memory:           %lu\n"
	.size	.L.str.3, 36

	.type	.L.str.4,@object                # @.str.4
.L.str.4:
	.asciz	"Total shared memory per block: %lu\n"
	.size	.L.str.4, 36

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"Total registers per block:     %d\n"
	.size	.L.str.5, 35

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

	.type	.L.str.7,@object                # @.str.7
.L.str.7:
	.asciz	"Maximum memory pitch:          %lu\n"
	.size	.L.str.7, 36

	.type	.L.str.8,@object                # @.str.8
.L.str.8:
	.asciz	"Maximum threads per block:     %d\n"
	.size	.L.str.8, 35

	.type	.L.str.9,@object                # @.str.9
.L.str.9:
	.asciz	"Maximum dimension %d of block:  %d\n"
	.size	.L.str.9, 36

	.type	.L.str.10,@object               # @.str.10
.L.str.10:
	.asciz	"Maximum dimension %d of grid:   %d\n"
	.size	.L.str.10, 36

	.type	.L.str.11,@object               # @.str.11
.L.str.11:
	.asciz	"Clock rate:                    %d\n"
	.size	.L.str.11, 35

	.type	.L.str.12,@object               # @.str.12
.L.str.12:
	.asciz	"Total constant memory:         %lu\n"
	.size	.L.str.12, 36

	.type	.L.str.13,@object               # @.str.13
.L.str.13:
	.asciz	"Texture alignment:             %lu\n"
	.size	.L.str.13, 36

	.type	.L.str.14,@object               # @.str.14
.L.str.14:
	.asciz	"Concurrent copy and execution: %s\n"
	.size	.L.str.14, 35

	.type	.L.str.15,@object               # @.str.15
.L.str.15:
	.asciz	"Yes"
	.size	.L.str.15, 4

	.type	.L.str.16,@object               # @.str.16
.L.str.16:
	.asciz	"No"
	.size	.L.str.16, 3

	.type	.L.str.17,@object               # @.str.17
.L.str.17:
	.asciz	"Number of multiprocessors:     %d\n"
	.size	.L.str.17, 35

	.type	.L.str.18,@object               # @.str.18
.L.str.18:
	.asciz	"Kernel execution timeout:      %s\n"
	.size	.L.str.18, 35

	.type	.L.str.19,@object               # @.str.19
.L.str.19:
	.asciz	"\n-------------------Device %d----------------------\n"
	.size	.L.str.19, 53

	.type	.L.str.20,@object               # @.str.20
.L.str.20:
	.asciz	"\nBest Device found %d\n"
	.size	.L.str.20, 23

	.type	.L.str.21,@object               # @.str.21
.L.str.21:
	.asciz	"Argc=%d Filling array A & B with %d integers %d\n"
	.size	.L.str.21, 49

	.type	.L.str.22,@object               # @.str.22
.L.str.22:
	.asciz	"\nArgc=%d Filling array A & B with %d integers and %d\n\n"
	.size	.L.str.22, 55

	.type	.L.str.23,@object               # @.str.23
.L.str.23:
	.asciz	"Reading input from file %s\n"
	.size	.L.str.23, 28

	.type	.L.str.24,@object               # @.str.24
.L.str.24:
	.asciz	"r"
	.size	.L.str.24, 2

	.type	.L.str.25,@object               # @.str.25
.L.str.25:
	.asciz	"Can't open input file!\n"
	.size	.L.str.25, 24

	.type	.L.str.26,@object               # @.str.26
.L.str.26:
	.asciz	"%s"
	.size	.L.str.26, 3

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

	.type	.L.str.28,@object               # @.str.28
.L.str.28:
	.asciz	"\nDone: time taken for serial version is %3.1f s\n"
	.size	.L.str.28, 49

	.type	.L.str.29,@object               # @.str.29
.L.str.29:
	.asciz	"Number co-primes found by CPU = %d\n\n"
	.size	.L.str.29, 37

	.type	.L.str.30,@object               # @.str.30
.L.str.30:
	.asciz	"Best device %d\n"
	.size	.L.str.30, 16

	.type	.L.str.31,@object               # @.str.31
.L.str.31:
	.asciz	"/home/ubuntu/Datasets/stackv2/train-structured-repos-hip-sm89/wakahiu/CUDA/master/hw0/copV.hip"
	.size	.L.str.31, 95

	.type	.L.str.32,@object               # @.str.32
.L.str.32:
	.asciz	"Number co-primes found by GPU = %d Num reduced %d\n"
	.size	.L.str.32, 51

	.type	.L.str.33,@object               # @.str.33
.L.str.33:
	.asciz	"done: time taken for parallel version is %3.1f s threads %d\n"
	.size	.L.str.33, 61

	.type	.L.str.34,@object               # @.str.34
.L.str.34:
	.asciz	"%s in file  %s at line %d\n"
	.size	.L.str.34, 27

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z8copV_GPUjPjS_"
	.size	.L__unnamed_1, 17

	.type	.L__unnamed_2,@object           # @1
.L__unnamed_2:
	.asciz	"_Z10reduce_GPUPj"
	.size	.L__unnamed_2, 17

	.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 _Z23__device_stub__copV_GPUjPjS_
	.addrsig_sym _Z25__device_stub__reduce_GPUPj
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z8copV_GPUjPjS_
	.addrsig_sym _Z10reduce_GPUPj
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z8copV_GPUjPjS_        ; -- Begin function _Z8copV_GPUjPjS_
	.globl	_Z8copV_GPUjPjS_
	.p2align	8
	.type	_Z8copV_GPUjPjS_,@function
_Z8copV_GPUjPjS_:                       ; @_Z8copV_GPUjPjS_
; %bb.0:
	s_clause 0x1
	s_load_b32 s2, s[0:1], 0x24
	s_load_b32 s3, s[0:1], 0x0
	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_5
; %bb.1:
	s_load_b128 s[0:3], s[0:1], 0x8
	v_mov_b32_e32 v2, 0
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_1)
	v_lshlrev_b64 v[2:3], 2, v[1:2]
	s_waitcnt lgkmcnt(0)
	v_add_co_u32 v0, vcc_lo, s0, v2
	s_delay_alu instid0(VALU_DEP_2)
	v_add_co_ci_u32_e32 v1, vcc_lo, s1, v3, vcc_lo
	v_add_co_u32 v2, vcc_lo, s2, v2
	v_add_co_ci_u32_e32 v3, vcc_lo, s3, v3, vcc_lo
	s_mov_b32 s1, 0
	global_load_b32 v5, v[0:1], off
	global_load_b32 v2, v[2:3], off
	s_mov_b32 s0, exec_lo
	s_waitcnt vmcnt(0)
	v_min_i32_e32 v4, v2, v5
	v_max_i32_e32 v2, v2, v5
	s_delay_alu instid0(VALU_DEP_2)
	v_cmpx_ne_u32_e32 0, v4
	s_cbranch_execz .LBB0_4
.LBB0_2:                                ; %.lr.ph
                                        ; =>This Inner Loop Header: Depth=1
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_ashrrev_i32_e32 v7, 31, v2
	v_dual_mov_b32 v3, v4 :: v_dual_add_nc_u32 v2, v2, v7
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_ashrrev_i32_e32 v4, 31, v3
	v_xor_b32_e32 v2, v2, v7
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_add_nc_u32_e32 v5, v3, v4
	v_xor_b32_e32 v4, v5, v4
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_cvt_f32_u32_e32 v5, v4
	v_sub_nc_u32_e32 v6, 0, v4
	v_rcp_iflag_f32_e32 v5, v5
	s_waitcnt_depctr 0xfff
	v_mul_f32_e32 v5, 0x4f7ffffe, v5
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_cvt_u32_f32_e32 v5, v5
	v_mul_lo_u32 v6, v6, v5
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mul_hi_u32 v6, v5, v6
	v_add_nc_u32_e32 v5, v5, v6
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mul_hi_u32 v5, v2, v5
	v_mul_lo_u32 v5, v5, v4
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_sub_nc_u32_e32 v2, v2, v5
	v_sub_nc_u32_e32 v5, v2, v4
	v_cmp_ge_u32_e32 vcc_lo, v2, v4
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_cndmask_b32_e32 v2, v2, v5, vcc_lo
	v_sub_nc_u32_e32 v5, v2, v4
	v_cmp_ge_u32_e32 vcc_lo, v2, v4
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_cndmask_b32_e32 v2, v2, v5, vcc_lo
	v_xor_b32_e32 v2, v2, v7
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_sub_nc_u32_e32 v4, v2, v7
	v_mov_b32_e32 v2, v3
	v_cmp_eq_u32_e32 vcc_lo, 0, v4
	s_or_b32 s1, vcc_lo, s1
	s_delay_alu instid0(SALU_CYCLE_1)
	s_and_not1_b32 exec_lo, exec_lo, s1
	s_cbranch_execnz .LBB0_2
; %bb.3:                                ; %Flow
	s_or_b32 exec_lo, exec_lo, s1
	v_mov_b32_e32 v2, v3
.LBB0_4:                                ; %Flow39
	s_or_b32 exec_lo, exec_lo, s0
	s_delay_alu instid0(VALU_DEP_1)
	v_cmp_eq_u32_e32 vcc_lo, 1, v2
	v_cndmask_b32_e64 v2, 0, 1, vcc_lo
	global_store_b32 v[0:1], v2, off
.LBB0_5:
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z8copV_GPUjPjS_
		.amdhsa_group_segment_fixed_size 0
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 280
		.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 8
		.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	_Z8copV_GPUjPjS_, .Lfunc_end0-_Z8copV_GPUjPjS_
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 408
; NumSgprs: 18
; NumVgprs: 8
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 0
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 8
; Occupancy: 16
; WaveLimiterHint : 0
; COMPUTE_PGM_RSRC2:SCRATCH_EN: 0
; COMPUTE_PGM_RSRC2:USER_SGPR: 15
; COMPUTE_PGM_RSRC2:TRAP_HANDLER: 0
; COMPUTE_PGM_RSRC2:TGID_X_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Y_EN: 0
; COMPUTE_PGM_RSRC2:TGID_Z_EN: 0
; COMPUTE_PGM_RSRC2:TIDIG_COMP_CNT: 0
	.text
	.protected	_Z10reduce_GPUPj        ; -- Begin function _Z10reduce_GPUPj
	.globl	_Z10reduce_GPUPj
	.p2align	8
	.type	_Z10reduce_GPUPj,@function
_Z10reduce_GPUPj:                       ; @_Z10reduce_GPUPj
; %bb.0:
	s_load_b32 s2, s[0:1], 0x14
	s_waitcnt lgkmcnt(0)
	s_and_b32 s2, s2, 0xffff
	s_delay_alu instid0(SALU_CYCLE_1)
	s_cmp_lt_u32 s2, 2
	s_cbranch_scc1 .LBB1_5
; %bb.1:                                ; %.lr.ph
	s_load_b64 s[0:1], s[0:1], 0x0
	v_mov_b32_e32 v2, 0
	v_mad_u64_u32 v[3:4], null, s15, s2, v[0:1]
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mov_b32_e32 v4, v2
	v_lshlrev_b64 v[4:5], 2, v[3:4]
	s_waitcnt lgkmcnt(0)
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_co_u32 v4, vcc_lo, s0, v4
	v_add_co_ci_u32_e32 v5, vcc_lo, s1, v5, vcc_lo
.LBB1_2:                                ; =>This Inner Loop Header: Depth=1
	s_lshr_b32 s3, s2, 1
	s_mov_b32 s4, exec_lo
	v_cmpx_gt_u32_e64 s3, v0
	s_cbranch_execz .LBB1_4
; %bb.3:                                ;   in Loop: Header=BB1_2 Depth=1
	v_add_nc_u32_e32 v1, s3, v3
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_lshlrev_b64 v[6:7], 2, v[1:2]
	v_add_co_u32 v6, vcc_lo, s0, v6
	s_delay_alu instid0(VALU_DEP_2)
	v_add_co_ci_u32_e32 v7, vcc_lo, s1, v7, vcc_lo
	s_clause 0x1
	global_load_b32 v1, v[6:7], off
	global_load_b32 v6, v[4:5], off
	s_waitcnt vmcnt(0)
	v_add_nc_u32_e32 v1, v6, v1
	global_store_b32 v[4:5], v1, off
.LBB1_4:                                ;   in Loop: Header=BB1_2 Depth=1
	s_or_b32 exec_lo, exec_lo, s4
	s_cmp_lt_u32 s2, 4
	s_mov_b32 s2, s3
	s_waitcnt_vscnt null, 0x0
	s_barrier
	buffer_gl0_inv
	s_cbranch_scc0 .LBB1_2
.LBB1_5:                                ; %._crit_edge
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z10reduce_GPUPj
		.amdhsa_group_segment_fixed_size 0
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 264
		.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 8
		.amdhsa_next_free_sgpr 16
		.amdhsa_float_round_mode_32 0
		.amdhsa_float_round_mode_16_64 0
		.amdhsa_float_denorm_mode_32 3
		.amdhsa_float_denorm_mode_16_64 3
		.amdhsa_dx10_clamp 1
		.amdhsa_ieee_mode 1
		.amdhsa_fp16_overflow 0
		.amdhsa_workgroup_processor_mode 1
		.amdhsa_memory_ordered 1
		.amdhsa_forward_progress 0
		.amdhsa_shared_vgpr_count 0
		.amdhsa_exception_fp_ieee_invalid_op 0
		.amdhsa_exception_fp_denorm_src 0
		.amdhsa_exception_fp_ieee_div_zero 0
		.amdhsa_exception_fp_ieee_overflow 0
		.amdhsa_exception_fp_ieee_underflow 0
		.amdhsa_exception_fp_ieee_inexact 0
		.amdhsa_exception_int_div_zero 0
	.end_amdhsa_kernel
	.text
.Lfunc_end1:
	.size	_Z10reduce_GPUPj, .Lfunc_end1-_Z10reduce_GPUPj
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 212
; NumSgprs: 18
; NumVgprs: 8
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 0
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 8
; 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:
      - .offset:         0
        .size:           4
        .value_kind:     by_value
      - .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:     hidden_block_count_x
      - .offset:         28
        .size:           4
        .value_kind:     hidden_block_count_y
      - .offset:         32
        .size:           4
        .value_kind:     hidden_block_count_z
      - .offset:         36
        .size:           2
        .value_kind:     hidden_group_size_x
      - .offset:         38
        .size:           2
        .value_kind:     hidden_group_size_y
      - .offset:         40
        .size:           2
        .value_kind:     hidden_group_size_z
      - .offset:         42
        .size:           2
        .value_kind:     hidden_remainder_x
      - .offset:         44
        .size:           2
        .value_kind:     hidden_remainder_y
      - .offset:         46
        .size:           2
        .value_kind:     hidden_remainder_z
      - .offset:         64
        .size:           8
        .value_kind:     hidden_global_offset_x
      - .offset:         72
        .size:           8
        .value_kind:     hidden_global_offset_y
      - .offset:         80
        .size:           8
        .value_kind:     hidden_global_offset_z
      - .offset:         88
        .size:           2
        .value_kind:     hidden_grid_dims
    .group_segment_fixed_size: 0
    .kernarg_segment_align: 8
    .kernarg_segment_size: 280
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z8copV_GPUjPjS_
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z8copV_GPUjPjS_.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     8
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
  - .args:
      - .address_space:  global
        .offset:         0
        .size:           8
        .value_kind:     global_buffer
      - .offset:         8
        .size:           4
        .value_kind:     hidden_block_count_x
      - .offset:         12
        .size:           4
        .value_kind:     hidden_block_count_y
      - .offset:         16
        .size:           4
        .value_kind:     hidden_block_count_z
      - .offset:         20
        .size:           2
        .value_kind:     hidden_group_size_x
      - .offset:         22
        .size:           2
        .value_kind:     hidden_group_size_y
      - .offset:         24
        .size:           2
        .value_kind:     hidden_group_size_z
      - .offset:         26
        .size:           2
        .value_kind:     hidden_remainder_x
      - .offset:         28
        .size:           2
        .value_kind:     hidden_remainder_y
      - .offset:         30
        .size:           2
        .value_kind:     hidden_remainder_z
      - .offset:         48
        .size:           8
        .value_kind:     hidden_global_offset_x
      - .offset:         56
        .size:           8
        .value_kind:     hidden_global_offset_y
      - .offset:         64
        .size:           8
        .value_kind:     hidden_global_offset_z
      - .offset:         72
        .size:           2
        .value_kind:     hidden_grid_dims
    .group_segment_fixed_size: 0
    .kernarg_segment_align: 8
    .kernarg_segment_size: 264
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z10reduce_GPUPj
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z10reduce_GPUPj.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     8
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
