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

#include "config.cuh"

using namespace std;

/*
 * Mapping function to be run for each input. The input must be read from memory
 * and the the key/value output must be stored in memory at pairs. Multiple
 * pairs may be stored at the next postiion in pairs, but the maximum number of
 * key/value pairs stored must not exceed NUM_KEYS.
 */
__device__ void mapper(input_type *input, KeyValuePair *pairs) 
{
    // We set the key of each input to 0.
    pairs->key = 0;
    char ch = input->ch;	
	//pairs->key = ch;
    // We check if the input array has a space or a new line and set the value accordingly.
	//If so this will count the number of words in a file.
    if (ch == ' '||ch == '\n') 
	{ 
		pairs->value = 1;
	} 
     
	else 
	{
        pairs->value = 0;
    }
}

/*
 * Reducing function to be run for each set of key/value pairs that share the
 * same key. len key/value pairs may be read from memory, and the output
 * generated from these pairs must be stored at output in memory.
 */
__device__ void reducer(KeyValuePair *pairs, int len, output_type *output) 
{
    int wordCount = 0;
    for (KeyValuePair *pair = pairs; pair != pairs + len; pair++) 
	{
        if(pair->value == 1) 
		{
            wordCount++;
        }
    }
    // After calculating number of words in an input file, we will move the wordCount into the output variable.
    *output = wordCount;
}


/*
 * Main function that runs a map reduce job.
 */
int main(int argc, char const *argv[]) 
{
    printf("\n My first Program. I am here\n");
	
    // Allocate host memory
    size_t input_size = NUM_INPUT * sizeof(input_type);
    size_t output_size = NUM_OUTPUT * sizeof(output_type);
    input_type *input = (input_type *) malloc(input_size);
    output_type *output = (output_type *) malloc(output_size);

    
	
	//Reading an input file and copying it into an input array
	FILE *f;
    char c;
	//char input[1000000];
    f=fopen("test.txt","rt");
	
	int i=0;
    while((c=fgetc(f))!=EOF)
	{
        //printf("%c",c);
		input[i].ch = c;
		i++;
    }
	
	printf("\n The array is:  %s", input);
	 fclose(f);

    // Run the Map Reduce Job
    runMapReduce(input, output);

    // Iterate through the output array
    for (size_t i = 0; i < NUM_OUTPUT; i++) 
	{
        printf("The total number of words in the file are: %d\n", output[i]);
    }

    // Free host memory
    free(input);
    free(output);

    return 0;
}

	.file	"tmpxft_003679ab_00000000-6_gpu_demo.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2032:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2032:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z6mapperP10Word_CountP12KeyValuePair
	.type	_Z6mapperP10Word_CountP12KeyValuePair, @function
_Z6mapperP10Word_CountP12KeyValuePair:
.LFB2027:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	popq	%rax
	.cfi_def_cfa_offset 8
	subq	$24, %rsp
	.cfi_def_cfa_offset 32
	movl	$1, 12(%rsp)
	movl	12(%rsp), %edi
	call	exit@PLT
	.cfi_endproc
.LFE2027:
	.size	_Z6mapperP10Word_CountP12KeyValuePair, .-_Z6mapperP10Word_CountP12KeyValuePair
	.globl	_Z7reducerP12KeyValuePairiPi
	.type	_Z7reducerP12KeyValuePairiPi, @function
_Z7reducerP12KeyValuePairiPi:
.LFB2028:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	popq	%rax
	.cfi_def_cfa_offset 8
	subq	$24, %rsp
	.cfi_def_cfa_offset 32
	movl	$1, 12(%rsp)
	movl	12(%rsp), %edi
	call	exit@PLT
	.cfi_endproc
.LFE2028:
	.size	_Z7reducerP12KeyValuePairiPi, .-_Z7reducerP12KeyValuePairiPi
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"\n My first Program. I am here\n"
.LC1:
	.string	"rt"
.LC2:
	.string	"test.txt"
.LC3:
	.string	"\n The array is:  %s"
.LC4:
	.string	"The total number of words in the file are: %d\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2029:
	.cfi_startproc
	endbr64
	pushq	%r13
	.cfi_def_cfa_offset 16
	.cfi_offset 13, -16
	leaq	.LC0(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	pushq	%r12
	.cfi_def_cfa_offset 24
	.cfi_offset 12, -24
	pushq	%rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
	pushq	%rcx
	.cfi_def_cfa_offset 48
	call	__printf_chk@PLT
	movl	$100000, %edi
	call	malloc@PLT
	movl	$4, %edi
	movq	%rax, %rbx
	call	malloc@PLT
	leaq	.LC1(%rip), %rsi
	leaq	.LC2(%rip), %rdi
	movq	%rbx, %r13
	movq	%rax, %rbp
	call	fopen@PLT
	movq	%rax, %r12
.L7:
	movq	%r12, %rdi
	call	fgetc@PLT
	cmpb	$-1, %al
	je	.L11
	movb	%al, 0(%r13)
	incq	%r13
	jmp	.L7
.L11:
	movq	%rbx, %rdx
	leaq	.LC3(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movq	%r12, %rdi
	call	fclose@PLT
	movq	%rbp, %rsi
	movq	%rbx, %rdi
	call	_Z12runMapReduceP10Word_CountPi@PLT
	movl	0(%rbp), %edx
	movl	$2, %edi
	xorl	%eax, %eax
	leaq	.LC4(%rip), %rsi
	call	__printf_chk@PLT
	movq	%rbx, %rdi
	call	free@PLT
	movq	%rbp, %rdi
	call	free@PLT
	popq	%rdx
	.cfi_def_cfa_offset 40
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 32
	popq	%rbp
	.cfi_def_cfa_offset 24
	popq	%r12
	.cfi_def_cfa_offset 16
	popq	%r13
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2029:
	.size	main, .-main
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2055:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	leaq	_ZL15__fatDeviceText(%rip), %rdi
	call	__cudaRegisterFatBinary@PLT
	movq	%rax, %rdi
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	call	__cudaRegisterFatBinaryEnd@PLT
	leaq	_ZL26__cudaUnregisterBinaryUtilv(%rip), %rdi
	popq	%rdx
	.cfi_def_cfa_offset 8
	jmp	atexit@PLT
	.cfi_endproc
.LFE2055:
	.size	_ZL24__sti____cudaRegisterAllv, .-_ZL24__sti____cudaRegisterAllv
	.section	.init_array,"aw"
	.align 8
	.quad	_ZL24__sti____cudaRegisterAllv
	.section	.nvFatBinSegment,"aw"
	.align 8
	.type	_ZL15__fatDeviceText, @object
	.size	_ZL15__fatDeviceText, 24
_ZL15__fatDeviceText:
	.long	1180844977
	.long	1
	.quad	fatbinData
	.quad	0
	.local	_ZL20__cudaFatCubinHandle
	.comm	_ZL20__cudaFatCubinHandle,8,8
	.ident	"GCC: (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:


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

#include "config.cuh"

using namespace std;

/*
 * Mapping function to be run for each input. The input must be read from memory
 * and the the key/value output must be stored in memory at pairs. Multiple
 * pairs may be stored at the next postiion in pairs, but the maximum number of
 * key/value pairs stored must not exceed NUM_KEYS.
 */
__device__ void mapper(input_type *input, KeyValuePair *pairs) 
{
    // We set the key of each input to 0.
    pairs->key = 0;
    char ch = input->ch;	
	//pairs->key = ch;
    // We check if the input array has a space or a new line and set the value accordingly.
	//If so this will count the number of words in a file.
    if (ch == ' '||ch == '\n') 
	{ 
		pairs->value = 1;
	} 
     
	else 
	{
        pairs->value = 0;
    }
}

/*
 * Reducing function to be run for each set of key/value pairs that share the
 * same key. len key/value pairs may be read from memory, and the output
 * generated from these pairs must be stored at output in memory.
 */
__device__ void reducer(KeyValuePair *pairs, int len, output_type *output) 
{
    int wordCount = 0;
    for (KeyValuePair *pair = pairs; pair != pairs + len; pair++) 
	{
        if(pair->value == 1) 
		{
            wordCount++;
        }
    }
    // After calculating number of words in an input file, we will move the wordCount into the output variable.
    *output = wordCount;
}


/*
 * Main function that runs a map reduce job.
 */
int main(int argc, char const *argv[]) 
{
    printf("\n My first Program. I am here\n");
	
    // Allocate host memory
    size_t input_size = NUM_INPUT * sizeof(input_type);
    size_t output_size = NUM_OUTPUT * sizeof(output_type);
    input_type *input = (input_type *) malloc(input_size);
    output_type *output = (output_type *) malloc(output_size);

    
	
	//Reading an input file and copying it into an input array
	FILE *f;
    char c;
	//char input[1000000];
    f=fopen("test.txt","rt");
	
	int i=0;
    while((c=fgetc(f))!=EOF)
	{
        //printf("%c",c);
		input[i].ch = c;
		i++;
    }
	
	printf("\n The array is:  %s", input);
	 fclose(f);

    // Run the Map Reduce Job
    runMapReduce(input, output);

    // Iterate through the output array
    for (size_t i = 0; i < NUM_OUTPUT; i++) 
	{
        printf("The total number of words in the file are: %d\n", output[i]);
    }

    // Free host memory
    free(input);
    free(output);

    return 0;
}

	.text
	.file	"gpu_demo.hip"
	.globl	main                            # -- Begin function main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%r12
	.cfi_def_cfa_offset 32
	pushq	%rbx
	.cfi_def_cfa_offset 40
	pushq	%rax
	.cfi_def_cfa_offset 48
	.cfi_offset %rbx, -40
	.cfi_offset %r12, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movl	$.Lstr, %edi
	callq	puts@PLT
	movl	$100000, %edi                   # imm = 0x186A0
	callq	malloc
	movq	%rax, %rbx
	movl	$4, %edi
	callq	malloc
	movq	%rax, %r14
	movl	$.L.str.1, %edi
	movl	$.L.str.2, %esi
	callq	fopen
	movq	%rax, %r15
	movq	%rax, %rdi
	callq	fgetc
	cmpb	$-1, %al
	je	.LBB0_3
# %bb.1:                                # %.lr.ph.preheader
	movq	%rbx, %r12
.LBB0_2:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movb	%al, (%r12)
	movq	%r15, %rdi
	callq	fgetc
	incq	%r12
	cmpb	$-1, %al
	jne	.LBB0_2
.LBB0_3:                                # %.critedge
	movl	$.L.str.3, %edi
	movq	%rbx, %rsi
	xorl	%eax, %eax
	callq	printf
	movq	%r15, %rdi
	callq	fclose
	movq	%rbx, %rdi
	movq	%r14, %rsi
	callq	_Z12runMapReduceP10Word_CountPi
	movl	(%r14), %esi
	movl	$.L.str.4, %edi
	xorl	%eax, %eax
	callq	printf
	movq	%rbx, %rdi
	callq	free
	movq	%r14, %rdi
	callq	free
	xorl	%eax, %eax
	addq	$8, %rsp
	.cfi_def_cfa_offset 40
	popq	%rbx
	.cfi_def_cfa_offset 32
	popq	%r12
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end0:
	.size	main, .Lfunc_end0-main
	.cfi_endproc
                                        # -- End function
	.type	.L.str.1,@object                # @.str.1
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str.1:
	.asciz	"test.txt"
	.size	.L.str.1, 9

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"rt"
	.size	.L.str.2, 3

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"\n The array is:  %s"
	.size	.L.str.3, 20

	.type	.L.str.4,@object                # @.str.4
.L.str.4:
	.asciz	"The total number of words in the file are: %d\n"
	.size	.L.str.4, 47

	.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	"\n My first Program. I am here"
	.size	.Lstr, 30

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

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

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

	.end_amdgpu_metadata
