
/* Includes, system */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <sys/time.h>

/* Includes, cuda */
#include <cuda.h>
#include <cuda_runtime.h>

#define N 1000000
#define GRID 100000
#define BLOCK 100
#define M 1000

/* Main */
int main(int argc, char** argv)
{

    double *h_x,*h_y;
    double *d_x,*d_y;
    int n=N,num_err=0;
    int DeviceCount = 0;

    /* Initialize CUDA */
    printf("CUDA 0  ejemplo..\n");

    if (cuInit(0) != CUDA_SUCCESS){
        printf("ERROR de inicializacion\n");
        exit(0);
	}
    cuDeviceGetCount(&DeviceCount);
    if (DeviceCount == 0){
        printf("ERROR ningun dispositivo soporta CUDA\n");
        exit(0);
	}
    /* Allocate host memory for the matrices */
    h_x = (double*)malloc(n * sizeof(h_x[0]));
    h_y = (double*)malloc(n * sizeof(h_y[0]));

    memset(h_x,2,n * sizeof(h_x[0]));
    memset(h_y,4,n * sizeof(h_x[0]));

    if (cudaMalloc((void**)&d_x,n*sizeof(d_x[0])) != 0) num_err++;
    if (cudaMalloc((void**)&d_y,n*sizeof(d_y[0])) != 0) num_err++;

    if (num_err != 0) {
        printf ("ERROR reserva memoria device: %d\n",num_err);
        return EXIT_FAILURE;
    }

    if (cudaMemcpy(d_x,h_x,n*sizeof(d_x[0]),cudaMemcpyHostToDevice) != 0) printf("ERROR1") ;
    if (cudaMemcpy(d_y,h_y,n*sizeof(d_y[0]),cudaMemcpyHostToDevice) != 0) printf("ERROR2") ;

    //LLAMADA A KERNEL QUE MODIFICA MEMORIA DEVICE	

    if (cudaMemcpy(h_x,d_x,n*sizeof(h_x[0]),cudaMemcpyDeviceToHost) != 0) printf("ERROR3") ;
    if (cudaMemcpy(h_y,d_y,n*sizeof(h_y[0]),cudaMemcpyDeviceToHost) != 0) printf("ERROR4") ;

    free(h_x);
    free(h_y);

    cudaFree(d_x);
    cudaFree(d_y);

    return EXIT_SUCCESS;
}

	.file	"tmpxft_002c0673_00000000-6_cudaej0.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2030:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2030:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"CUDA 0  ejemplo..\n"
.LC1:
	.string	"ERROR de inicializacion\n"
.LC2:
	.string	"ERROR ningun dispositivo soporta CUDA\n"
.LC3:
	.string	"ERROR reserva memoria device: %d\n"
.LC4:
	.string	"ERROR1"
.LC5:
	.string	"ERROR2"
.LC6:
	.string	"ERROR3"
.LC7:
	.string	"ERROR4"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2027:
	.cfi_startproc
	endbr64
	pushq	%r12
	.cfi_def_cfa_offset 16
	.cfi_offset 12, -16
	xorl	%edx, %edx
	leaq	.LC0(%rip), %rsi
	movl	$2, %edi
	pushq	%rbp
	.cfi_def_cfa_offset 24
	.cfi_offset 6, -24
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset 3, -32
	subq	$32, %rsp
	.cfi_def_cfa_offset 64
	movq	%fs:40, %rax
	movq	%rax, 24(%rsp)
	xorl	%eax, %eax
	movl	%edx, 4(%rsp)
	call	__printf_chk@PLT
	xorl	%edi, %edi
	call	cuInit@PLT
	leaq	.LC1(%rip), %rsi
	testl	%eax, %eax
	jne	.L27
	leaq	4(%rsp), %rdi
	call	cuDeviceGetCount@PLT
	cmpl	$0, 4(%rsp)
	jne	.L4
	leaq	.LC2(%rip), %rsi
.L27:
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	xorl	%edi, %edi
	call	exit@PLT
.L4:
	movl	$8000000, %edi
	call	malloc@PLT
	movl	$8000000, %edi
	movq	%rax, %rbp
	call	malloc@PLT
	movl	$8000000, %ecx
	movq	%rbp, %rdi
	movl	$8000000, %esi
	movq	%rax, %rbx
	movb	$2, %al
	rep stosb
	movb	$4, %al
	movl	$8000000, %ecx
	movq	%rbx, %rdi
	rep stosb
	leaq	8(%rsp), %rdi
	call	cudaMalloc@PLT
	leaq	16(%rsp), %rdi
	movl	$8000000, %esi
	movl	%eax, %r12d
	call	cudaMalloc@PLT
	testl	%eax, %eax
	je	.L5
	cmpl	$1, %r12d
	movl	$1, %edx
	sbbl	$-1, %edx
	jmp	.L6
.L5:
	testl	%r12d, %r12d
	je	.L7
	movl	$1, %edx
.L6:
	leaq	.LC3(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	$1, %eax
	jmp	.L2
.L7:
	movq	8(%rsp), %rdi
	movl	$1, %ecx
	movl	$8000000, %edx
	movq	%rbp, %rsi
	call	cudaMemcpy@PLT
	testl	%eax, %eax
	je	.L9
	leaq	.LC4(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L9:
	movq	16(%rsp), %rdi
	movl	$1, %ecx
	movl	$8000000, %edx
	movq	%rbx, %rsi
	call	cudaMemcpy@PLT
	testl	%eax, %eax
	je	.L10
	leaq	.LC5(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L10:
	movq	8(%rsp), %rsi
	movl	$2, %ecx
	movl	$8000000, %edx
	movq	%rbp, %rdi
	call	cudaMemcpy@PLT
	testl	%eax, %eax
	je	.L11
	leaq	.LC6(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L11:
	movq	16(%rsp), %rsi
	movl	$2, %ecx
	movl	$8000000, %edx
	movq	%rbx, %rdi
	call	cudaMemcpy@PLT
	testl	%eax, %eax
	je	.L12
	leaq	.LC7(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L12:
	movq	%rbp, %rdi
	call	free@PLT
	movq	%rbx, %rdi
	call	free@PLT
	movq	8(%rsp), %rdi
	call	cudaFree@PLT
	movq	16(%rsp), %rdi
	call	cudaFree@PLT
	xorl	%eax, %eax
.L2:
	movq	24(%rsp), %rdx
	subq	%fs:40, %rdx
	je	.L13
	call	__stack_chk_fail@PLT
.L13:
	addq	$32, %rsp
	.cfi_def_cfa_offset 32
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%rbp
	.cfi_def_cfa_offset 16
	popq	%r12
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2027:
	.size	main, .-main
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2053:
	.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
.LFE2053:
	.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:


/* Includes, system */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <sys/time.h>

/* Includes, cuda */
#include <hip/hip_runtime.h>


#define N 1000000
#define GRID 100000
#define BLOCK 100
#define M 1000

/* Main */
int main(int argc, char** argv)
{

    double *h_x,*h_y;
    double *d_x,*d_y;
    int n=N,num_err=0;
    int DeviceCount = 0;

    /* Initialize CUDA */
    printf("CUDA 0  ejemplo..\n");

    if (hipInit(0) != hipSuccess){
        printf("ERROR de inicializacion\n");
        exit(0);
	}
    hipGetDeviceCount(&DeviceCount);
    if (DeviceCount == 0){
        printf("ERROR ningun dispositivo soporta CUDA\n");
        exit(0);
	}
    /* Allocate host memory for the matrices */
    h_x = (double*)malloc(n * sizeof(h_x[0]));
    h_y = (double*)malloc(n * sizeof(h_y[0]));

    memset(h_x,2,n * sizeof(h_x[0]));
    memset(h_y,4,n * sizeof(h_x[0]));

    if (hipMalloc((void**)&d_x,n*sizeof(d_x[0])) != 0) num_err++;
    if (hipMalloc((void**)&d_y,n*sizeof(d_y[0])) != 0) num_err++;

    if (num_err != 0) {
        printf ("ERROR reserva memoria device: %d\n",num_err);
        return EXIT_FAILURE;
    }

    if (hipMemcpy(d_x,h_x,n*sizeof(d_x[0]),hipMemcpyHostToDevice) != 0) printf("ERROR1") ;
    if (hipMemcpy(d_y,h_y,n*sizeof(d_y[0]),hipMemcpyHostToDevice) != 0) printf("ERROR2") ;

    //LLAMADA A KERNEL QUE MODIFICA MEMORIA DEVICE	

    if (hipMemcpy(h_x,d_x,n*sizeof(h_x[0]),hipMemcpyDeviceToHost) != 0) printf("ERROR3") ;
    if (hipMemcpy(h_y,d_y,n*sizeof(h_y[0]),hipMemcpyDeviceToHost) != 0) printf("ERROR4") ;

    free(h_x);
    free(h_y);

    hipFree(d_x);
    hipFree(d_y);

    return EXIT_SUCCESS;
}

	.text
	.file	"cudaej0.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	%rbx
	.cfi_def_cfa_offset 32
	subq	$32, %rsp
	.cfi_def_cfa_offset 64
	.cfi_offset %rbx, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movl	$0, 28(%rsp)
	movl	$.Lstr, %edi
	callq	puts@PLT
	xorl	%edi, %edi
	callq	hipInit
	testl	%eax, %eax
	jne	.LBB0_1
# %bb.3:
	leaq	28(%rsp), %rbx
	movq	%rbx, %rdi
	callq	hipGetDeviceCount
	cmpl	$0, (%rbx)
	je	.LBB0_4
# %bb.5:
	movl	$8000000, %edi                  # imm = 0x7A1200
	callq	malloc
	movq	%rax, %rbx
	movl	$8000000, %edi                  # imm = 0x7A1200
	callq	malloc
	movq	%rax, %r14
	movl	$8000000, %edx                  # imm = 0x7A1200
	movq	%rbx, %rdi
	movl	$2, %esi
	callq	memset@PLT
	movl	$8000000, %edx                  # imm = 0x7A1200
	movq	%r14, %rdi
	movl	$4, %esi
	callq	memset@PLT
	leaq	16(%rsp), %rdi
	movl	$8000000, %esi                  # imm = 0x7A1200
	callq	hipMalloc
	xorl	%r15d, %r15d
	testl	%eax, %eax
	setne	%r15b
	leaq	8(%rsp), %rdi
	movl	$8000000, %esi                  # imm = 0x7A1200
	callq	hipMalloc
	leal	1(%r15), %esi
	testl	%eax, %eax
	cmovel	%r15d, %esi
	testl	%esi, %esi
	je	.LBB0_7
# %bb.6:
	movl	$.L.str.3, %edi
	xorl	%eax, %eax
	callq	printf
	movl	$1, %eax
	jmp	.LBB0_16
.LBB0_7:
	movq	16(%rsp), %rdi
	movl	$8000000, %edx                  # imm = 0x7A1200
	movq	%rbx, %rsi
	movl	$1, %ecx
	callq	hipMemcpy
	testl	%eax, %eax
	je	.LBB0_9
# %bb.8:
	movl	$.L.str.4, %edi
	xorl	%eax, %eax
	callq	printf
.LBB0_9:
	movq	8(%rsp), %rdi
	movl	$8000000, %edx                  # imm = 0x7A1200
	movq	%r14, %rsi
	movl	$1, %ecx
	callq	hipMemcpy
	testl	%eax, %eax
	je	.LBB0_11
# %bb.10:
	movl	$.L.str.5, %edi
	xorl	%eax, %eax
	callq	printf
.LBB0_11:
	movq	16(%rsp), %rsi
	movl	$8000000, %edx                  # imm = 0x7A1200
	movq	%rbx, %rdi
	movl	$2, %ecx
	callq	hipMemcpy
	testl	%eax, %eax
	je	.LBB0_13
# %bb.12:
	movl	$.L.str.6, %edi
	xorl	%eax, %eax
	callq	printf
.LBB0_13:
	movq	8(%rsp), %rsi
	movl	$8000000, %edx                  # imm = 0x7A1200
	movq	%r14, %rdi
	movl	$2, %ecx
	callq	hipMemcpy
	testl	%eax, %eax
	je	.LBB0_15
# %bb.14:
	movl	$.L.str.7, %edi
	xorl	%eax, %eax
	callq	printf
.LBB0_15:
	movq	%rbx, %rdi
	callq	free
	movq	%r14, %rdi
	callq	free
	movq	16(%rsp), %rdi
	callq	hipFree
	movq	8(%rsp), %rdi
	callq	hipFree
	xorl	%eax, %eax
.LBB0_16:
	addq	$32, %rsp
	.cfi_def_cfa_offset 32
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.LBB0_1:
	.cfi_def_cfa_offset 64
	movl	$.Lstr.2, %edi
	jmp	.LBB0_2
.LBB0_4:
	movl	$.Lstr.1, %edi
.LBB0_2:
	callq	puts@PLT
	xorl	%edi, %edi
	callq	exit
.Lfunc_end0:
	.size	main, .Lfunc_end0-main
	.cfi_endproc
                                        # -- End function
	.type	.L.str.3,@object                # @.str.3
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str.3:
	.asciz	"ERROR reserva memoria device: %d\n"
	.size	.L.str.3, 34

	.type	.L.str.4,@object                # @.str.4
.L.str.4:
	.asciz	"ERROR1"
	.size	.L.str.4, 7

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"ERROR2"
	.size	.L.str.5, 7

	.type	.L.str.6,@object                # @.str.6
.L.str.6:
	.asciz	"ERROR3"
	.size	.L.str.6, 7

	.type	.L.str.7,@object                # @.str.7
.L.str.7:
	.asciz	"ERROR4"
	.size	.L.str.7, 7

	.type	__hip_cuid_,@object             # @__hip_cuid_
	.bss
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               # 0x0
	.size	__hip_cuid_, 1

	.type	.Lstr,@object                   # @str
	.section	.rodata.str1.1,"aMS",@progbits,1
.Lstr:
	.asciz	"CUDA 0  ejemplo.."
	.size	.Lstr, 18

	.type	.Lstr.1,@object                 # @str.1
.Lstr.1:
	.asciz	"ERROR ningun dispositivo soporta CUDA"
	.size	.Lstr.1, 38

	.type	.Lstr.2,@object                 # @str.2
.Lstr.2:
	.asciz	"ERROR de inicializacion"
	.size	.Lstr.2, 24

	.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
