/**
 * Array Search with CUDA (Fall 2016): 
 * 
 * Members:
 * Emanuelle Crespi, Tolga Keskinoglu
 *
 * This test implements an array search discussed in the methodology section
 * of Optimizing CPU-GPU Interactions.
 *
 * The following code makes use of the kernel call search(int n, char *data, char *out, char c)
 * to perform a parallel search for char c amongst a large vector of characters.
 *
 * The result is a one-one mapping of the data array with 1s and 0s in the out array at 
 * corresponding indices where the character has been found.  The output is written to 
 * the file 'parallel_array_search_result.txt' for validation with 'array_search_result.txt'
 * when running the executable for array_search.c
 *
 * While the overhead of executing 'cudaMemCpy(...)' slows down execution time,
 * the performance of the parallel search itself is significantly faster than it's
 * serial counterpart.
 * 
 * The output of the performance is displayed in seconds for verification.
 *
 * References:
 * NVIDIA CUDA C Programming Guide Version 3.2
 */

// System includes
#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <time.h>

// Jetson TK1 has device capability 1.x allowing 1024 threads/block
// We also indicate a threshold of 67108864 for vectored data
#define THREADS_PER_BLOCK 1024 
#define THRESHOLD 67108864 

//Identify failures
#define FILE_OPEN_FAIL -1
#define MALLOC_FAIL -2


/* 
 * Indicates the task search to be performed on the GPU
 * for char c in array data both of size n
 *
 * The output 1 or 0 is written in out to indicate 'found' and 'not-found' respectively
 * Results are written to device memory and must be fetched back from out for verification.
 * search( n, ['a','b','d','c','d','e',...], result, 'd') ==> result = ['0','0','1','0','1','0',...]
 */
__global__ void search(int n, char *data, char *out, char c){
	int i =  blockIdx.x*blockDim.x + threadIdx.x;
	if(i < n){
		if (data[i] == c){
			out[i] = '1';
		}else{
			out[i] = '0';
		}
	}
}

int main(){
	FILE *fp_data, *fp_out;
	char *data, c;
	char *d_data, *d_out;
	int s_data = 0, j = 0, i = 0;

	int flag = 0;
	cudaError_t error;
	//printf("Computing file size...\n");
	
	if (!(fp_data = fopen("../../file.txt", "r"))){
		perror("failed to open file.txt\n");
		return FILE_OPEN_FAIL;
	}
	
	while( fscanf(fp_data,"%c",&c) != EOF ){
		s_data++;
	}

	int rem = s_data % THRESHOLD;
	int sections = (THRESHOLD+s_data-1)/THRESHOLD;

	//printf("Mallocing %d bytes of data on CPU...\n", s_data);
	
	/* Allocate necessary space for host buffer */
	cudaMallocHost(&data, sizeof(char)*s_data);

	/* Allocate necessary space for device buffer */
	//printf("Mallocing %d bytes of data on GPU...\n", s_data);	
	cudaMalloc( (void **) &d_data, sizeof(char)*s_data);
	cudaMalloc( (void **) &d_out, sizeof(char)*s_data);

	fseek(fp_data, 0, 0);

	/* Read file into buffer */
	//printf("Reading data into buffer...\n");	
	for( j= 0; fscanf(fp_data,"%c",&data[j]) != EOF; j++ ){	}

	/* Identify our streams */
	cudaStream_t stream[sections];
	for (int j = 0; j < sections; j++){
		cudaStreamCreate(&stream[j]);
	}

	/* Time the search algorithm */
	//printf("Executing search on GPU...\n");


	if( rem == 0 ){
		flag = 0;
	}else{
		flag = 1;
	}
	
	/*******************************for testing purposes****************************************
	 *******************************************************************************************/
	cudaDeviceSynchronize();

    	// Allocate CUDA events that we'll use for timing
    	cudaEvent_t start;
    	error = cudaEventCreate(&start);

    	if (error != cudaSuccess)
    	{
        	fprintf(stderr, "Failed to create start event (error code %s)!\n", cudaGetErrorString(error));
        	exit(EXIT_FAILURE);
    	}

    	cudaEvent_t stop;
    	error = cudaEventCreate(&stop);

    	if (error != cudaSuccess)
    	{
        	fprintf(stderr, "Failed to create stop event (error code %s)!\n", cudaGetErrorString(error));
        	exit(EXIT_FAILURE);
    	}

    	// Record the start event
    	error = cudaEventRecord(start, NULL);

    	if (error != cudaSuccess)
    	{
        	fprintf(stderr, "Failed to record start event (error code %s)!\n", cudaGetErrorString(error));
        	exit(EXIT_FAILURE);
    	}
	printf("Running...\n");
    	// Execute the kernel
	for(j = 0; j < sections-flag; j++){
		cudaMemcpyAsync(d_data + j * THRESHOLD, data + j * THRESHOLD,
                    THRESHOLD, cudaMemcpyHostToDevice, stream[j]);
		search<<<(THRESHOLD+THREADS_PER_BLOCK-1)/THREADS_PER_BLOCK,THREADS_PER_BLOCK,0,stream[j]>>>(THRESHOLD, 
								data+(j*THRESHOLD), d_out+(j*THRESHOLD), 'D');
		cudaStreamSynchronize(stream[j]);	
		cudaMemcpyAsync( data + j * THRESHOLD, d_out + j * THRESHOLD,
                    THRESHOLD, cudaMemcpyDeviceToHost, stream[j]);
	}	
	
	
	/* Define and run stream for remainder */
	cudaMemcpyAsync(d_data + j * THRESHOLD, data + j * THRESHOLD,
                    rem, cudaMemcpyHostToDevice, stream[j]);

	search<<<(rem+THREADS_PER_BLOCK-1)/THREADS_PER_BLOCK,THREADS_PER_BLOCK,0,stream[j]>>>(rem, 
							data + j * THRESHOLD, d_out + j * THRESHOLD, 'D');
	cudaStreamSynchronize(stream[j]);	

	cudaMemcpyAsync( data + j * THRESHOLD, d_out + j * THRESHOLD,
                rem, cudaMemcpyDeviceToHost, stream[j]);

    	// Record the stop event
    	error = cudaEventRecord(stop, NULL);

    	if (error != cudaSuccess)
    	{
        	fprintf(stderr, "Failed to record stop event (error code %s)!\n", cudaGetErrorString(error));
        	exit(EXIT_FAILURE);
    	}

    	// Wait for the stop event to complete
    	error = cudaEventSynchronize(stop);

    	if (error != cudaSuccess)
    	{
        	fprintf(stderr, "Failed to synchronize on the stop event (error code %s)!\n", cudaGetErrorString(error));
        	exit(EXIT_FAILURE);
    	}

    	float msecTotal = 0.0f;
    	error = cudaEventElapsedTime(&msecTotal, start, stop);

    	if (error != cudaSuccess)
    	{
        	fprintf(stderr, "Failed to get time elapsed between events (error code %s)!\n", cudaGetErrorString(error));
        	exit(EXIT_FAILURE);
    	}

    	// Compute and print the performance
    	float msecPerisPrime = msecTotal / 1;

    	printf( "Performance= %.06f sec\n", msecPerisPrime/1000.0 );
	/*******************************************************************************************
	 ****************************** for testing purposes ***************************************/
	
	/* Destroy streams */
	for (int j = 0; j < sections; j++){
    		cudaStreamDestroy(stream[j]);
  	}

	/* Copy result back to host */
  	//printf("Writing result to 'parallel_array_search_result.txt'...\n");
 	
	if ( !(fp_out = fopen("parallel_array_search_result.txt", "w")) ){
		perror("failed to open results file\n");
		return FILE_OPEN_FAIL;
	}

	//output data to file
 	for (j = 0; j < s_data; j++){
		if( i == 32 ){
			fprintf(fp_out, "%c\n", data[j]);
			i = 0;	
		}else{
			fprintf(fp_out, "%c", data[j]);
			i++;
		}


	}

	/* Cleanup */
 	cudaFreeHost(data); 
	cudaFree(d_data); cudaFree(d_out);
 	fclose(fp_data); fclose(fp_out);

 	return 0;
}

	.file	"tmpxft_00312461_00000000-6_array_search.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2043:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2043:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z29__device_stub__Z6searchiPcS_ciPcS_c
	.type	_Z29__device_stub__Z6searchiPcS_ciPcS_c, @function
_Z29__device_stub__Z6searchiPcS_ciPcS_c:
.LFB2065:
	.cfi_startproc
	endbr64
	subq	$152, %rsp
	.cfi_def_cfa_offset 160
	movl	%edi, 28(%rsp)
	leaq	56(%rsp), %rdi
	movq	%rsi, 16(%rsp)
	leaq	68(%rsp), %rsi
	movq	%rdx, 8(%rsp)
	leaq	40(%rsp), %rdx
	movb	%cl, 24(%rsp)
	leaq	48(%rsp), %rcx
	movq	%fs:40, %rax
	movq	%rax, 136(%rsp)
	xorl	%eax, %eax
	leaq	28(%rsp), %rax
	movl	$1, 64(%rsp)
	movq	%rax, 104(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 112(%rsp)
	leaq	8(%rsp), %rax
	movq	%rax, 120(%rsp)
	leaq	24(%rsp), %rax
	movq	%rax, 128(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 56(%rsp)
	movq	%rax, 68(%rsp)
	movl	$1, 76(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L2
	pushq	48(%rsp)
	.cfi_def_cfa_offset 168
	leaq	_Z6searchiPcS_c(%rip), %rdi
	pushq	48(%rsp)
	.cfi_def_cfa_offset 176
	movq	84(%rsp), %rcx
	movl	92(%rsp), %r8d
	movq	72(%rsp), %rsi
	movl	80(%rsp), %edx
	leaq	120(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 168
	popq	%rdx
	.cfi_def_cfa_offset 160
.L2:
	movq	136(%rsp), %rax
	subq	%fs:40, %rax
	je	.L4
	call	__stack_chk_fail@PLT
.L4:
	addq	$152, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2065:
	.size	_Z29__device_stub__Z6searchiPcS_ciPcS_c, .-_Z29__device_stub__Z6searchiPcS_ciPcS_c
	.globl	_Z6searchiPcS_c
	.type	_Z6searchiPcS_c, @function
_Z6searchiPcS_c:
.LFB2066:
	.cfi_startproc
	endbr64
	movsbl	%cl, %ecx
	jmp	_Z29__device_stub__Z6searchiPcS_ciPcS_c
	.cfi_endproc
.LFE2066:
	.size	_Z6searchiPcS_c, .-_Z6searchiPcS_c
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"r"
.LC1:
	.string	"../../file.txt"
.LC2:
	.string	"failed to open file.txt\n"
.LC3:
	.string	"%c"
.LC4:
	.string	"Failed to create start event (error code %s)!\n"
.LC5:
	.string	"Failed to create stop event (error code %s)!\n"
.LC6:
	.string	"Failed to record start event (error code %s)!\n"
.LC7:
	.string	"Running...\n"
.LC8:
	.string	"Failed to record stop event (error code %s)!\n"
.LC9:
	.string	"Failed to synchronize on the stop event (error code %s)!\n"
.LC11:
	.string	"Failed to get time elapsed between events (error code %s)!\n"
.LC13:
	.string	"Performance= %.06f sec\n"
.LC14:
	.string	"w"
.LC15:
	.string	"parallel_array_search_result.txt"
.LC16:
	.string	"failed to open results file\n"
.LC17:
	.string	"%c\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2040:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	leaq	.LC1(%rip), %rdi
	leaq	.LC0(%rip), %rsi
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	pushq	%r15
	pushq	%r14
	pushq	%r13
	pushq	%r12
	pushq	%rbx
	subq	$120, %rsp
	.cfi_offset 15, -24
	.cfi_offset 14, -32
	.cfi_offset 13, -40
	.cfi_offset 12, -48
	.cfi_offset 3, -56
	movq	%fs:40, %rax
	movq	%rax, -56(%rbp)
	xorl	%eax, %eax
	call	fopen@PLT
	leaq	.LC2(%rip), %rdi
	movq	%rax, -136(%rbp)
	testq	%rax, %rax
	je	.L46
	xorl	%r12d, %r12d
	leaq	.LC3(%rip), %r13
.L9:
	movq	-136(%rbp), %rdi
	xorl	%eax, %eax
	leaq	-121(%rbp), %rdx
	movq	%r13, %rsi
	call	__isoc23_fscanf@PLT
	incl	%eax
	je	.L48
	incl	%r12d
	jmp	.L9
.L48:
	movl	%r12d, %eax
	movslq	%r12d, %rbx
	leaq	-120(%rbp), %rdi
	xorl	%edx, %edx
	andl	$67108863, %eax
	movq	%rbx, %rsi
	movl	%eax, -140(%rbp)
	leal	67108863(%r12), %eax
	sarl	$26, %eax
	movl	%eax, -152(%rbp)
	call	cudaHostAlloc@PLT
	movq	%rbx, %rsi
	leaq	-112(%rbp), %rdi
	call	cudaMalloc@PLT
	movq	%rbx, %rsi
	leaq	-104(%rbp), %rdi
	xorl	%ebx, %ebx
	call	cudaMalloc@PLT
	movq	-136(%rbp), %rdi
	xorl	%edx, %edx
	xorl	%esi, %esi
	call	fseek@PLT
.L12:
	movq	-120(%rbp), %rdx
	movq	-136(%rbp), %rdi
	xorl	%eax, %eax
	movq	%r13, %rsi
	addq	%rbx, %rdx
	incq	%rbx
	call	__isoc23_fscanf@PLT
	incl	%eax
	jne	.L12
	movslq	-152(%rbp), %rcx
	movq	%rsp, %rsi
	leaq	15(,%rcx,8), %rdx
	movq	%rdx, %rax
	andq	$-4096, %rdx
	andq	$-16, %rax
	subq	%rdx, %rsi
.L13:
	cmpq	%rsi, %rsp
	je	.L14
	subq	$4096, %rsp
	orq	$0, 4088(%rsp)
	jmp	.L13
.L14:
	andl	$4095, %eax
	subq	%rax, %rsp
	testq	%rax, %rax
	je	.L15
	orq	$0, -8(%rsp,%rax)
.L15:
	movq	%rsp, %rbx
	leaq	(%rbx,%rcx,8), %rax
	movq	%rbx, %r14
	movq	%rbx, %r13
	movq	%rax, -160(%rbp)
.L16:
	movq	-160(%rbp), %rax
	cmpq	%rax, %r13
	je	.L49
	movq	%r13, %rdi
	addq	$8, %r13
	call	cudaStreamCreate@PLT
	jmp	.L16
.L49:
	xorl	%eax, %eax
	cmpl	$0, -140(%rbp)
	setne	%al
	movl	%eax, -144(%rbp)
	call	cudaDeviceSynchronize@PLT
	leaq	-96(%rbp), %rdi
	call	cudaEventCreate@PLT
	movl	%eax, %edi
	testl	%eax, %eax
	je	.L18
	call	cudaGetErrorString@PLT
	leaq	.LC4(%rip), %rdx
	movq	%rax, %rcx
	jmp	.L47
.L18:
	leaq	-88(%rbp), %rdi
	call	cudaEventCreate@PLT
	movl	%eax, %edi
	testl	%eax, %eax
	je	.L19
	call	cudaGetErrorString@PLT
	leaq	.LC5(%rip), %rdx
	movq	%rax, %rcx
.L47:
	movq	stderr(%rip), %rdi
	movl	$2, %esi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	movl	$1, %edi
	call	exit@PLT
.L19:
	movq	-96(%rbp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movl	%eax, %edi
	testl	%eax, %eax
	je	.L20
	call	cudaGetErrorString@PLT
	leaq	.LC6(%rip), %rdx
	movq	%rax, %rcx
	jmp	.L47
.L20:
	leaq	.LC7(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	xorl	%r13d, %r13d
	call	__printf_chk@PLT
.L21:
	movl	-152(%rbp), %eax
	movl	-144(%rbp), %ecx
	movq	%r13, %r15
	movq	-120(%rbp), %rsi
	movq	-112(%rbp), %rdi
	salq	$26, %r15
	subl	%ecx, %eax
	movq	(%rbx,%r13,8), %r8
	addq	%r15, %rsi
	addq	%r15, %rdi
	cmpl	%r13d, %eax
	jle	.L50
	movl	$1, %ecx
	movl	$67108864, %edx
	call	cudaMemcpyAsync@PLT
	movl	$4194305, %eax
	movl	$65537, %edi
	xorl	%r8d, %r8d
	salq	$10, %rax
	movq	(%rbx,%r13,8), %r9
	salq	$16, %rdi
	movl	$1, %ecx
	movq	%rax, %rdx
	movl	$1, %esi
	movq	%rax, -68(%rbp)
	movl	$1, -60(%rbp)
	movq	%rdi, -80(%rbp)
	movl	$1, -72(%rbp)
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L22
	movq	-104(%rbp), %rdx
	movq	-120(%rbp), %rsi
	movl	$68, %ecx
	movl	$67108864, %edi
	addq	%r15, %rdx
	addq	%r15, %rsi
	call	_Z29__device_stub__Z6searchiPcS_ciPcS_c
.L22:
	movq	(%rbx,%r13,8), %rdi
	call	cudaStreamSynchronize@PLT
	movq	-104(%rbp), %rsi
	movq	-120(%rbp), %rdi
	movl	$2, %ecx
	movq	(%rbx,%r13,8), %r8
	movl	$67108864, %edx
	incq	%r13
	addq	%r15, %rsi
	addq	%r15, %rdi
	call	cudaMemcpyAsync@PLT
	jmp	.L21
.L50:
	xorl	%edx, %edx
	testl	%eax, %eax
	movl	$1, %ecx
	cmovs	%edx, %eax
	movslq	%eax, %r15
	movslq	-140(%rbp), %rax
	leaq	(%rbx,%r15,8), %rbx
	movq	%r15, %r13
	movq	%rax, %rdx
	movq	%rax, -152(%rbp)
	salq	$26, %r13
	call	cudaMemcpyAsync@PLT
	movl	$4194305, %edx
	movq	(%rbx), %r9
	xorl	%r8d, %r8d
	movl	-140(%rbp), %eax
	salq	$10, %rdx
	movl	$1, %ecx
	movl	$1, -60(%rbp)
	movq	%rdx, -68(%rbp)
	addl	$1023, %eax
	sarl	$10, %eax
	movl	%eax, -80(%rbp)
	movabsq	$4294967297, %rax
	movq	%rax, -76(%rbp)
	movq	-80(%rbp), %rdi
	movl	-72(%rbp), %esi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L24
	movq	-104(%rbp), %rdx
	movq	-120(%rbp), %rsi
	movl	$68, %ecx
	movl	-140(%rbp), %edi
	addq	%r13, %rdx
	addq	%r13, %rsi
	call	_Z29__device_stub__Z6searchiPcS_ciPcS_c
.L24:
	movq	(%rbx), %rdi
	call	cudaStreamSynchronize@PLT
	movq	-104(%rbp), %rsi
	movq	-120(%rbp), %rdi
	movl	$2, %ecx
	movq	(%rbx), %r8
	movq	-152(%rbp), %rdx
	addq	%r13, %rsi
	addq	%r13, %rdi
	call	cudaMemcpyAsync@PLT
	movq	-88(%rbp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movl	%eax, %edi
	testl	%eax, %eax
	je	.L25
	call	cudaGetErrorString@PLT
	leaq	.LC8(%rip), %rdx
	movq	%rax, %rcx
	jmp	.L47
.L25:
	movq	-88(%rbp), %rdi
	call	cudaEventSynchronize@PLT
	movl	%eax, %edi
	testl	%eax, %eax
	je	.L26
	call	cudaGetErrorString@PLT
	leaq	.LC9(%rip), %rdx
	movq	%rax, %rcx
	jmp	.L47
.L26:
	movq	-88(%rbp), %rdx
	movq	-96(%rbp), %rsi
	leaq	-68(%rbp), %rdi
	movl	$0x00000000, -68(%rbp)
	call	cudaEventElapsedTime@PLT
	movl	%eax, %edi
	testl	%eax, %eax
	je	.L27
	call	cudaGetErrorString@PLT
	leaq	.LC11(%rip), %rdx
	movq	%rax, %rcx
	jmp	.L47
.L27:
	leaq	.LC13(%rip), %rsi
	movl	$2, %edi
	movb	$1, %al
	cvtss2sd	-68(%rbp), %xmm0
	divsd	.LC12(%rip), %xmm0
	call	__printf_chk@PLT
.L28:
	movq	-160(%rbp), %rax
	cmpq	%rax, %r14
	je	.L51
	movq	(%r14), %rdi
	addq	$8, %r14
	call	cudaStreamDestroy@PLT
	jmp	.L28
.L51:
	leaq	.LC14(%rip), %rsi
	leaq	.LC15(%rip), %rdi
	xorl	%r13d, %r13d
	xorl	%r15d, %r15d
	call	fopen@PLT
	leaq	.LC3(%rip), %r14
	movq	%rax, %rbx
	testq	%rax, %rax
	jne	.L30
	leaq	.LC16(%rip), %rdi
.L46:
	call	perror@PLT
	orl	$-1, %eax
	jmp	.L8
.L34:
	movsbl	(%rdi,%r13), %ecx
	cmpl	$32, %r15d
	jne	.L32
	leaq	.LC17(%rip), %rdx
	movq	%rbx, %rdi
	xorl	%eax, %eax
	xorl	%r15d, %r15d
	movl	$2, %esi
	call	__fprintf_chk@PLT
.L33:
	incq	%r13
.L30:
	movq	-120(%rbp), %rdi
	cmpl	%r13d, %r12d
	jg	.L34
	jmp	.L52
.L32:
	movq	%r14, %rdx
	movl	$2, %esi
	movq	%rbx, %rdi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	incl	%r15d
	jmp	.L33
.L52:
	call	cudaFreeHost@PLT
	movq	-112(%rbp), %rdi
	call	cudaFree@PLT
	movq	-104(%rbp), %rdi
	call	cudaFree@PLT
	movq	-136(%rbp), %rdi
	call	fclose@PLT
	movq	%rbx, %rdi
	call	fclose@PLT
	xorl	%eax, %eax
.L8:
	movq	-56(%rbp), %rdx
	subq	%fs:40, %rdx
	je	.L35
	call	__stack_chk_fail@PLT
.L35:
	leaq	-40(%rbp), %rsp
	popq	%rbx
	popq	%r12
	popq	%r13
	popq	%r14
	popq	%r15
	popq	%rbp
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE2040:
	.size	main, .-main
	.section	.rodata.str1.1
.LC18:
	.string	"_Z6searchiPcS_c"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2068:
	.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	.LC18(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z6searchiPcS_c(%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
.LFE2068:
	.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
.LC12:
	.long	0
	.long	1083129856
	.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:

/**
 * Array Search with CUDA (Fall 2016): 
 * 
 * Members:
 * Emanuelle Crespi, Tolga Keskinoglu
 *
 * This test implements an array search discussed in the methodology section
 * of Optimizing CPU-GPU Interactions.
 *
 * The following code makes use of the kernel call search(int n, char *data, char *out, char c)
 * to perform a parallel search for char c amongst a large vector of characters.
 *
 * The result is a one-one mapping of the data array with 1s and 0s in the out array at 
 * corresponding indices where the character has been found.  The output is written to 
 * the file 'parallel_array_search_result.txt' for validation with 'array_search_result.txt'
 * when running the executable for array_search.c
 *
 * While the overhead of executing 'cudaMemCpy(...)' slows down execution time,
 * the performance of the parallel search itself is significantly faster than it's
 * serial counterpart.
 * 
 * The output of the performance is displayed in seconds for verification.
 *
 * References:
 * NVIDIA CUDA C Programming Guide Version 3.2
 */

// System includes

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

// Jetson TK1 has device capability 1.x allowing 1024 threads/block
// We also indicate a threshold of 67108864 for vectored data
#define THREADS_PER_BLOCK 1024 
#define THRESHOLD 67108864 

//Identify failures
#define FILE_OPEN_FAIL -1
#define MALLOC_FAIL -2


/* 
 * Indicates the task search to be performed on the GPU
 * for char c in array data both of size n
 *
 * The output 1 or 0 is written in out to indicate 'found' and 'not-found' respectively
 * Results are written to device memory and must be fetched back from out for verification.
 * search( n, ['a','b','d','c','d','e',...], result, 'd') ==> result = ['0','0','1','0','1','0',...]
 */
__global__ void search(int n, char *data, char *out, char c){
	int i =  blockIdx.x*blockDim.x + threadIdx.x;
	if(i < n){
		if (data[i] == c){
			out[i] = '1';
		}else{
			out[i] = '0';
		}
	}
}

int main(){
	FILE *fp_data, *fp_out;
	char *data, c;
	char *d_data, *d_out;
	int s_data = 0, j = 0, i = 0;

	int flag = 0;
	hipError_t error;
	//printf("Computing file size...\n");
	
	if (!(fp_data = fopen("../../file.txt", "r"))){
		perror("failed to open file.txt\n");
		return FILE_OPEN_FAIL;
	}
	
	while( fscanf(fp_data,"%c",&c) != EOF ){
		s_data++;
	}

	int rem = s_data % THRESHOLD;
	int sections = (THRESHOLD+s_data-1)/THRESHOLD;

	//printf("Mallocing %d bytes of data on CPU...\n", s_data);
	
	/* Allocate necessary space for host buffer */
	hipHostMalloc(&data, sizeof(char)*s_data, hipHostMallocDefault);

	/* Allocate necessary space for device buffer */
	//printf("Mallocing %d bytes of data on GPU...\n", s_data);	
	hipMalloc( (void **) &d_data, sizeof(char)*s_data);
	hipMalloc( (void **) &d_out, sizeof(char)*s_data);

	fseek(fp_data, 0, 0);

	/* Read file into buffer */
	//printf("Reading data into buffer...\n");	
	for( j= 0; fscanf(fp_data,"%c",&data[j]) != EOF; j++ ){	}

	/* Identify our streams */
	hipStream_t stream[sections];
	for (int j = 0; j < sections; j++){
		hipStreamCreate(&stream[j]);
	}

	/* Time the search algorithm */
	//printf("Executing search on GPU...\n");


	if( rem == 0 ){
		flag = 0;
	}else{
		flag = 1;
	}
	
	/*******************************for testing purposes****************************************
	 *******************************************************************************************/
	hipDeviceSynchronize();

    	// Allocate CUDA events that we'll use for timing
    	hipEvent_t start;
    	error = hipEventCreate(&start);

    	if (error != hipSuccess)
    	{
        	fprintf(stderr, "Failed to create start event (error code %s)!\n", hipGetErrorString(error));
        	exit(EXIT_FAILURE);
    	}

    	hipEvent_t stop;
    	error = hipEventCreate(&stop);

    	if (error != hipSuccess)
    	{
        	fprintf(stderr, "Failed to create stop event (error code %s)!\n", hipGetErrorString(error));
        	exit(EXIT_FAILURE);
    	}

    	// Record the start event
    	error = hipEventRecord(start, NULL);

    	if (error != hipSuccess)
    	{
        	fprintf(stderr, "Failed to record start event (error code %s)!\n", hipGetErrorString(error));
        	exit(EXIT_FAILURE);
    	}
	printf("Running...\n");
    	// Execute the kernel
	for(j = 0; j < sections-flag; j++){
		hipMemcpyAsync(d_data + j * THRESHOLD, data + j * THRESHOLD,
                    THRESHOLD, hipMemcpyHostToDevice, stream[j]);
		search<<<(THRESHOLD+THREADS_PER_BLOCK-1)/THREADS_PER_BLOCK,THREADS_PER_BLOCK,0,stream[j]>>>(THRESHOLD, 
								data+(j*THRESHOLD), d_out+(j*THRESHOLD), 'D');
		hipStreamSynchronize(stream[j]);	
		hipMemcpyAsync( data + j * THRESHOLD, d_out + j * THRESHOLD,
                    THRESHOLD, hipMemcpyDeviceToHost, stream[j]);
	}	
	
	
	/* Define and run stream for remainder */
	hipMemcpyAsync(d_data + j * THRESHOLD, data + j * THRESHOLD,
                    rem, hipMemcpyHostToDevice, stream[j]);

	search<<<(rem+THREADS_PER_BLOCK-1)/THREADS_PER_BLOCK,THREADS_PER_BLOCK,0,stream[j]>>>(rem, 
							data + j * THRESHOLD, d_out + j * THRESHOLD, 'D');
	hipStreamSynchronize(stream[j]);	

	hipMemcpyAsync( data + j * THRESHOLD, d_out + j * THRESHOLD,
                rem, hipMemcpyDeviceToHost, stream[j]);

    	// Record the stop event
    	error = hipEventRecord(stop, NULL);

    	if (error != hipSuccess)
    	{
        	fprintf(stderr, "Failed to record stop event (error code %s)!\n", hipGetErrorString(error));
        	exit(EXIT_FAILURE);
    	}

    	// Wait for the stop event to complete
    	error = hipEventSynchronize(stop);

    	if (error != hipSuccess)
    	{
        	fprintf(stderr, "Failed to synchronize on the stop event (error code %s)!\n", hipGetErrorString(error));
        	exit(EXIT_FAILURE);
    	}

    	float msecTotal = 0.0f;
    	error = hipEventElapsedTime(&msecTotal, start, stop);

    	if (error != hipSuccess)
    	{
        	fprintf(stderr, "Failed to get time elapsed between events (error code %s)!\n", hipGetErrorString(error));
        	exit(EXIT_FAILURE);
    	}

    	// Compute and print the performance
    	float msecPerisPrime = msecTotal / 1;

    	printf( "Performance= %.06f sec\n", msecPerisPrime/1000.0 );
	/*******************************************************************************************
	 ****************************** for testing purposes ***************************************/
	
	/* Destroy streams */
	for (int j = 0; j < sections; j++){
    		hipStreamDestroy(stream[j]);
  	}

	/* Copy result back to host */
  	//printf("Writing result to 'parallel_array_search_result.txt'...\n");
 	
	if ( !(fp_out = fopen("parallel_array_search_result.txt", "w")) ){
		perror("failed to open results file\n");
		return FILE_OPEN_FAIL;
	}

	//output data to file
 	for (j = 0; j < s_data; j++){
		if( i == 32 ){
			fprintf(fp_out, "%c\n", data[j]);
			i = 0;	
		}else{
			fprintf(fp_out, "%c", data[j]);
			i++;
		}


	}

	/* Cleanup */
 	hipHostFree(data); 
	hipFree(d_data); hipFree(d_out);
 	fclose(fp_data); fclose(fp_out);

 	return 0;
}

	.text
	.file	"array_search.hip"
	.globl	_Z21__device_stub__searchiPcS_c # -- Begin function _Z21__device_stub__searchiPcS_c
	.type	_Z21__device_stub__searchiPcS_c,@function
_Z21__device_stub__searchiPcS_c:        # @_Z21__device_stub__searchiPcS_c
	.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), %rdi
	movq	%rsi, (%rdi)
	leaq	32(%rsp), %rsi
	movq	%rdx, (%rsi)
	leaq	11(%rsp), %rdx
	movb	%cl, (%rdx)
	leaq	80(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rdi, 8(%rbx)
	movq	%rsi, 16(%rbx)
	movq	%rdx, 24(%rbx)
	leaq	64(%rsp), %r14
	leaq	48(%rsp), %r15
	leaq	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	$_Z6searchiPcS_c, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$128, %rsp
	.cfi_adjust_cfa_offset -128
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end0:
	.size	_Z21__device_stub__searchiPcS_c, .Lfunc_end0-_Z21__device_stub__searchiPcS_c
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function main
.LCPI1_0:
	.quad	0x408f400000000000              # double 1000
	.text
	.globl	main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset %rbp, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register %rbp
	pushq	%r15
	pushq	%r14
	pushq	%r13
	pushq	%r12
	pushq	%rbx
	subq	$104, %rsp
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	movl	$.L.str, %edi
	movl	$.L.str.1, %esi
	callq	fopen
	testq	%rax, %rax
	je	.LBB1_44
# %bb.1:                                # %.preheader116.preheader
	movq	%rax, %r12
	movq	$-1, %r14
	leaq	-49(%rbp), %r15
.LBB1_2:                                # %.preheader116
                                        # =>This Inner Loop Header: Depth=1
	movl	$.L.str.3, %esi
	movq	%r12, %rdi
	movq	%r15, %rdx
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	incq	%r14
	cmpl	$-1, %eax
	jne	.LBB1_2
# %bb.3:
	leaq	67108863(%r14), %r15
	leaq	-48(%rbp), %rdi
	movq	%r14, %rsi
	xorl	%edx, %edx
	callq	hipHostMalloc
	leaq	-96(%rbp), %rdi
	movq	%r14, %rsi
	callq	hipMalloc
	leaq	-64(%rbp), %rdi
	movq	%r14, %rsi
	callq	hipMalloc
	movq	%r12, %rdi
	xorl	%esi, %esi
	xorl	%edx, %edx
	callq	fseek
	xorl	%ebx, %ebx
.LBB1_4:                                # =>This Inner Loop Header: Depth=1
	movq	-48(%rbp), %rdx
	addq	%rbx, %rdx
	movl	$.L.str.3, %esi
	movq	%r12, %rdi
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	incq	%rbx
	cmpl	$-1, %eax
	jne	.LBB1_4
# %bb.5:
	movq	%r12, -112(%rbp)                # 8-byte Spill
	movq	%rsp, -144(%rbp)                # 8-byte Spill
	movl	%r14d, %eax
	andl	$67108863, %eax                 # imm = 0x3FFFFFF
	movq	%rax, -72(%rbp)                 # 8-byte Spill
	movq	%r15, %r12
	shrl	$26, %r12d
	leal	15(,%r12,8), %eax
	andl	$-16, %eax
	movq	%rsp, %r13
	subq	%rax, %r13
	movq	%r13, %rsp
	testl	%r14d, %r14d
	je	.LBB1_8
# %bb.6:                                # %.lr.ph.preheader
	xorl	%ebx, %ebx
	movq	%r13, %r15
.LBB1_7:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movq	%r15, %rdi
	callq	hipStreamCreate
	incq	%rbx
	addq	$8, %r15
	cmpq	%r12, %rbx
	jb	.LBB1_7
.LBB1_8:                                # %._crit_edge
	xorl	%ebx, %ebx
	movq	-72(%rbp), %rax                 # 8-byte Reload
                                        # kill: def $eax killed $eax killed $rax
	negl	%eax
	sbbl	%ebx, %ebx
	callq	hipDeviceSynchronize
	leaq	-128(%rbp), %rdi
	callq	hipEventCreate
	testl	%eax, %eax
	jne	.LBB1_9
# %bb.11:
	movq	%r12, -120(%rbp)                # 8-byte Spill
	leaq	-88(%rbp), %rdi
	callq	hipEventCreate
	testl	%eax, %eax
	jne	.LBB1_12
# %bb.13:
	movq	-128(%rbp), %rdi
	xorl	%r12d, %r12d
	xorl	%esi, %esi
	callq	hipEventRecord
	testl	%eax, %eax
	jne	.LBB1_14
# %bb.15:
	movabsq	$4294968320, %r15               # imm = 0x100000400
	movl	$.Lstr, %edi
	callq	puts@PLT
	addl	-120(%rbp), %ebx                # 4-byte Folded Reload
	movq	-72(%rbp), %rax                 # 8-byte Reload
	jle	.LBB1_21
# %bb.16:                               # %.lr.ph120.preheader
	movl	%ebx, %eax
	movq	%rax, -80(%rbp)                 # 8-byte Spill
	xorl	%ebx, %ebx
	addq	$64512, %r15                    # imm = 0xFC00
	xorl	%r12d, %r12d
.LBB1_17:                               # %.lr.ph120
                                        # =>This Inner Loop Header: Depth=1
	movq	-96(%rbp), %rdi
	addq	%rbx, %rdi
	movq	-48(%rbp), %rsi
	addq	%rbx, %rsi
	movq	(%r13,%r12,8), %r8
	movl	$67108864, %edx                 # imm = 0x4000000
	movl	$1, %ecx
	callq	hipMemcpyAsync
	movq	(%r13,%r12,8), %r9
	movq	%r15, %rdi
	movl	$1, %esi
	movabsq	$4294968320, %rdx               # imm = 0x100000400
	movl	$1, %ecx
	xorl	%r8d, %r8d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB1_19
# %bb.18:                               #   in Loop: Header=BB1_17 Depth=1
	movq	-48(%rbp), %rsi
	addq	%rbx, %rsi
	movq	-64(%rbp), %rdx
	addq	%rbx, %rdx
	movl	$67108864, %edi                 # imm = 0x4000000
	movl	$68, %ecx
	callq	_Z21__device_stub__searchiPcS_c
.LBB1_19:                               #   in Loop: Header=BB1_17 Depth=1
	movq	(%r13,%r12,8), %rdi
	callq	hipStreamSynchronize
	movq	-48(%rbp), %rdi
	addq	%rbx, %rdi
	movq	-64(%rbp), %rsi
	addq	%rbx, %rsi
	movq	(%r13,%r12,8), %r8
	movl	$67108864, %edx                 # imm = 0x4000000
	movl	$2, %ecx
	callq	hipMemcpyAsync
	incq	%r12
	addq	$67108864, %rbx                 # imm = 0x4000000
	cmpq	-80(%rbp), %r12                 # 8-byte Folded Reload
	jb	.LBB1_17
# %bb.20:                               # %._crit_edge121.loopexit
	movq	-72(%rbp), %rax                 # 8-byte Reload
	movabsq	$4294968320, %r15               # imm = 0x100000400
.LBB1_21:                               # %._crit_edge121
	movl	%r12d, %ecx
	movq	%rcx, -136(%rbp)                # 8-byte Spill
	shll	$26, %r12d
	movq	-96(%rbp), %rdi
	addq	%r12, %rdi
	movq	-48(%rbp), %rsi
	addq	%r12, %rsi
	movq	%rax, %rbx
	movl	%ebx, %edx
	movq	(%r13,%rcx,8), %r8
	movq	%rdx, -80(%rbp)                 # 8-byte Spill
	movl	$1, %ecx
	callq	hipMemcpyAsync
	leal	1023(%rbx), %eax
	movq	-136(%rbp), %rbx                # 8-byte Reload
	shrl	$10, %eax
	movq	(%r13,%rbx,8), %r9
	leaq	(%r15,%rax), %rdi
	addq	$-1024, %rdi                    # imm = 0xFC00
	movl	$1, %esi
	movq	%r15, %rdx
	movl	$1, %ecx
	xorl	%r8d, %r8d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB1_23
# %bb.22:
	movq	-48(%rbp), %rsi
	addq	%r12, %rsi
	movq	-64(%rbp), %rdx
	addq	%r12, %rdx
	movq	-72(%rbp), %rdi                 # 8-byte Reload
                                        # kill: def $edi killed $edi killed $rdi
	movl	$68, %ecx
	callq	_Z21__device_stub__searchiPcS_c
.LBB1_23:
	movq	(%r13,%rbx,8), %rdi
	callq	hipStreamSynchronize
	movq	-48(%rbp), %rdi
	addq	%r12, %rdi
	addq	-64(%rbp), %r12
	movq	(%r13,%rbx,8), %r8
	movq	%r12, %rsi
	movq	-80(%rbp), %rdx                 # 8-byte Reload
	movl	$2, %ecx
	callq	hipMemcpyAsync
	movq	-88(%rbp), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	testl	%eax, %eax
	jne	.LBB1_24
# %bb.25:
	movq	-88(%rbp), %rdi
	callq	hipEventSynchronize
	testl	%eax, %eax
	movq	-120(%rbp), %r15                # 8-byte Reload
	jne	.LBB1_26
# %bb.27:
	leaq	-100(%rbp), %rdi
	movl	$0, (%rdi)
	movq	-128(%rbp), %rsi
	movq	-88(%rbp), %rdx
	callq	hipEventElapsedTime
	testl	%eax, %eax
	jne	.LBB1_28
# %bb.29:
	cvtss2sd	-100(%rbp), %xmm0
	divsd	.LCPI1_0(%rip), %xmm0
	movl	$.L.str.11, %edi
	movb	$1, %al
	callq	printf
	testl	%r14d, %r14d
	je	.LBB1_34
# %bb.30:                               # %.lr.ph124.preheader
	xorl	%ebx, %ebx
.LBB1_31:                               # %.lr.ph124
                                        # =>This Inner Loop Header: Depth=1
	movq	(%r13,%rbx,8), %rdi
	callq	hipStreamDestroy
	incq	%rbx
	cmpq	%r15, %rbx
	jb	.LBB1_31
# %bb.32:                               # %._crit_edge125
	movl	$.L.str.12, %edi
	movl	$.L.str.13, %esi
	callq	fopen
	testq	%rax, %rax
	movq	-112(%rbp), %r13                # 8-byte Reload
	je	.LBB1_33
# %bb.35:                               # %.preheader
	movq	%rax, %r15
	testl	%r14d, %r14d
	je	.LBB1_41
# %bb.36:                               # %.lr.ph128.preheader
	xorl	%ebx, %ebx
	xorl	%r12d, %r12d
.LBB1_37:                               # %.lr.ph128
                                        # =>This Inner Loop Header: Depth=1
	movq	-48(%rbp), %rax
	movsbl	(%rax,%rbx), %edx
	cmpl	$32, %r12d
	je	.LBB1_38
# %bb.39:                               #   in Loop: Header=BB1_37 Depth=1
	movl	%edx, %edi
	movq	%r15, %rsi
	callq	fputc@PLT
	incl	%r12d
	jmp	.LBB1_40
.LBB1_38:                               #   in Loop: Header=BB1_37 Depth=1
	xorl	%r12d, %r12d
	movl	$.L.str.15, %esi
	movq	%r15, %rdi
	xorl	%eax, %eax
	callq	fprintf
.LBB1_40:                               #   in Loop: Header=BB1_37 Depth=1
	incq	%rbx
	decq	%r14
	jne	.LBB1_37
	jmp	.LBB1_41
.LBB1_34:                               # %._crit_edge125.thread
	movl	$.L.str.12, %edi
	movl	$.L.str.13, %esi
	callq	fopen
	movq	%rax, %r15
	testq	%rax, %rax
	movq	-112(%rbp), %r13                # 8-byte Reload
	je	.LBB1_33
.LBB1_41:                               # %._crit_edge129
	movq	-48(%rbp), %rdi
	callq	hipHostFree
	movq	-96(%rbp), %rdi
	callq	hipFree
	movq	-64(%rbp), %rdi
	callq	hipFree
	movq	%r13, %rdi
	callq	fclose
	movq	%r15, %rdi
	callq	fclose
	xorl	%eax, %eax
.LBB1_42:
	movq	-144(%rbp), %rsp                # 8-byte Reload
.LBB1_43:
	leaq	-40(%rbp), %rsp
	popq	%rbx
	popq	%r12
	popq	%r13
	popq	%r14
	popq	%r15
	popq	%rbp
	.cfi_def_cfa %rsp, 8
	retq
.LBB1_44:
	.cfi_def_cfa %rbp, 16
	movl	$.L.str.2, %edi
	callq	perror
	movl	$-1, %eax
	jmp	.LBB1_43
.LBB1_33:
	movl	$.L.str.14, %edi
	callq	perror
	movl	$-1, %eax
	jmp	.LBB1_42
.LBB1_9:
	movq	stderr(%rip), %rbx
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str.4, %esi
	jmp	.LBB1_10
.LBB1_12:
	movq	stderr(%rip), %rbx
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str.5, %esi
	jmp	.LBB1_10
.LBB1_14:
	movq	stderr(%rip), %rbx
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str.6, %esi
	jmp	.LBB1_10
.LBB1_24:
	movq	stderr(%rip), %rbx
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str.8, %esi
	jmp	.LBB1_10
.LBB1_26:
	movq	stderr(%rip), %rbx
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str.9, %esi
	jmp	.LBB1_10
.LBB1_28:
	movq	stderr(%rip), %rbx
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str.10, %esi
.LBB1_10:
	movq	%rbx, %rdi
	movq	%rax, %rdx
	xorl	%eax, %eax
	callq	fprintf
	movl	$1, %edi
	callq	exit
.Lfunc_end1:
	.size	main, .Lfunc_end1-main
	.cfi_endproc
                                        # -- End function
	.p2align	4, 0x90                         # -- Begin function __hip_module_ctor
	.type	__hip_module_ctor,@function
__hip_module_ctor:                      # @__hip_module_ctor
	.cfi_startproc
# %bb.0:
	subq	$40, %rsp
	.cfi_def_cfa_offset 48
	movq	__hip_gpubin_handle(%rip), %rdi
	testq	%rdi, %rdi
	jne	.LBB2_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rdi
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB2_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z6searchiPcS_c, %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_end2:
	.size	__hip_module_ctor, .Lfunc_end2-__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	.LBB3_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
.LBB3_2:
	retq
.Lfunc_end3:
	.size	__hip_module_dtor, .Lfunc_end3-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	_Z6searchiPcS_c,@object         # @_Z6searchiPcS_c
	.section	.rodata,"a",@progbits
	.globl	_Z6searchiPcS_c
	.p2align	3, 0x0
_Z6searchiPcS_c:
	.quad	_Z21__device_stub__searchiPcS_c
	.size	_Z6searchiPcS_c, 8

	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"../../file.txt"
	.size	.L.str, 15

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

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"failed to open file.txt\n"
	.size	.L.str.2, 25

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

	.type	.L.str.4,@object                # @.str.4
.L.str.4:
	.asciz	"Failed to create start event (error code %s)!\n"
	.size	.L.str.4, 47

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"Failed to create stop event (error code %s)!\n"
	.size	.L.str.5, 46

	.type	.L.str.6,@object                # @.str.6
.L.str.6:
	.asciz	"Failed to record start event (error code %s)!\n"
	.size	.L.str.6, 47

	.type	.L.str.8,@object                # @.str.8
.L.str.8:
	.asciz	"Failed to record stop event (error code %s)!\n"
	.size	.L.str.8, 46

	.type	.L.str.9,@object                # @.str.9
.L.str.9:
	.asciz	"Failed to synchronize on the stop event (error code %s)!\n"
	.size	.L.str.9, 58

	.type	.L.str.10,@object               # @.str.10
.L.str.10:
	.asciz	"Failed to get time elapsed between events (error code %s)!\n"
	.size	.L.str.10, 60

	.type	.L.str.11,@object               # @.str.11
.L.str.11:
	.asciz	"Performance= %.06f sec\n"
	.size	.L.str.11, 24

	.type	.L.str.12,@object               # @.str.12
.L.str.12:
	.asciz	"parallel_array_search_result.txt"
	.size	.L.str.12, 33

	.type	.L.str.13,@object               # @.str.13
.L.str.13:
	.asciz	"w"
	.size	.L.str.13, 2

	.type	.L.str.14,@object               # @.str.14
.L.str.14:
	.asciz	"failed to open results file\n"
	.size	.L.str.14, 29

	.type	.L.str.15,@object               # @.str.15
.L.str.15:
	.asciz	"%c\n"
	.size	.L.str.15, 4

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z6searchiPcS_c"
	.size	.L__unnamed_1, 16

	.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	"Running..."
	.size	.Lstr, 11

	.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 _Z21__device_stub__searchiPcS_c
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z6searchiPcS_c
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z6searchiPcS_c         ; -- Begin function _Z6searchiPcS_c
	.globl	_Z6searchiPcS_c
	.p2align	8
	.type	_Z6searchiPcS_c,@function
_Z6searchiPcS_c:                        ; @_Z6searchiPcS_c
; %bb.0:
	s_clause 0x1
	s_load_b32 s2, s[0:1], 0x2c
	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_i32_e64 s3, v1
	s_cbranch_execz .LBB0_2
; %bb.1:                                ; %.sink.split
	s_load_b128 s[4:7], s[0:1], 0x8
	v_ashrrev_i32_e32 v4, 31, v1
	s_load_b32 s0, s[0:1], 0x18
	s_waitcnt lgkmcnt(0)
	v_add_co_u32 v2, vcc_lo, s4, v1
	s_delay_alu instid0(VALU_DEP_2)
	v_add_co_ci_u32_e32 v3, vcc_lo, s5, v4, vcc_lo
	v_add_co_u32 v0, vcc_lo, s6, v1
	v_add_co_ci_u32_e32 v1, vcc_lo, s7, v4, vcc_lo
	global_load_u8 v2, v[2:3], off
	v_and_b32_e64 v3, 0xff, s0
	s_waitcnt vmcnt(0)
	s_delay_alu instid0(VALU_DEP_1)
	v_cmp_eq_u16_e32 vcc_lo, v2, v3
	v_cndmask_b32_e64 v2, 48, 49, vcc_lo
	global_store_b8 v[0:1], v2, off
.LBB0_2:
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z6searchiPcS_c
		.amdhsa_group_segment_fixed_size 0
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 288
		.amdhsa_user_sgpr_count 15
		.amdhsa_user_sgpr_dispatch_ptr 0
		.amdhsa_user_sgpr_queue_ptr 0
		.amdhsa_user_sgpr_kernarg_segment_ptr 1
		.amdhsa_user_sgpr_dispatch_id 0
		.amdhsa_user_sgpr_private_segment_size 0
		.amdhsa_wavefront_size32 1
		.amdhsa_uses_dynamic_stack 0
		.amdhsa_enable_private_segment 0
		.amdhsa_system_sgpr_workgroup_id_x 1
		.amdhsa_system_sgpr_workgroup_id_y 0
		.amdhsa_system_sgpr_workgroup_id_z 0
		.amdhsa_system_sgpr_workgroup_info 0
		.amdhsa_system_vgpr_workitem_id 0
		.amdhsa_next_free_vgpr 5
		.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	_Z6searchiPcS_c, .Lfunc_end0-_Z6searchiPcS_c
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 172
; NumSgprs: 18
; NumVgprs: 5
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 0
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 5
; 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:           1
        .value_kind:     by_value
      - .offset:         32
        .size:           4
        .value_kind:     hidden_block_count_x
      - .offset:         36
        .size:           4
        .value_kind:     hidden_block_count_y
      - .offset:         40
        .size:           4
        .value_kind:     hidden_block_count_z
      - .offset:         44
        .size:           2
        .value_kind:     hidden_group_size_x
      - .offset:         46
        .size:           2
        .value_kind:     hidden_group_size_y
      - .offset:         48
        .size:           2
        .value_kind:     hidden_group_size_z
      - .offset:         50
        .size:           2
        .value_kind:     hidden_remainder_x
      - .offset:         52
        .size:           2
        .value_kind:     hidden_remainder_y
      - .offset:         54
        .size:           2
        .value_kind:     hidden_remainder_z
      - .offset:         72
        .size:           8
        .value_kind:     hidden_global_offset_x
      - .offset:         80
        .size:           8
        .value_kind:     hidden_global_offset_y
      - .offset:         88
        .size:           8
        .value_kind:     hidden_global_offset_z
      - .offset:         96
        .size:           2
        .value_kind:     hidden_grid_dims
    .group_segment_fixed_size: 0
    .kernarg_segment_align: 8
    .kernarg_segment_size: 288
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z6searchiPcS_c
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z6searchiPcS_c.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     5
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
