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

#define error 1e-6

#define NUM_ITERATIONS 1000
#define NUM_PARTICLES 100000
#define BLOCK_SIZE 256
#define NSTREAMS 4
#define SIZE_BATCH 


struct particle {
    float position[3];
    float velocity[3];
};

double cpuSecond() {
    struct timeval tp;
    gettimeofday(&tp,NULL);
    return ((double)tp.tv_sec + (double)tp.tv_usec*1.e-6);
}

__host__ __device__ void uptdateParticle(particle *particula, int iter, int id, int num_p){
    //update the velocity:
    particula[id].velocity[0] = (3*id + iter) % num_p;
    particula[id].velocity[1] = (4*id + iter) % num_p;
    particula[id].velocity[2] = (5*id + iter) % num_p;

    //update the position:
    particula[id].position[0] = particula[id].position[0] + particula[id].velocity[0]; 
    particula[id].position[1] = particula[id].position[1] + particula[id].velocity[1]; 
    particula[id].position[2] = particula[id].position[2] + particula[id].velocity[2]; 
}

__global__ void timeStep(particle *particles, int iter, int num_p, int offset, int s, int streamSize){
    const int id = offset + threadIdx.x + blockIdx.x*blockDim.x;
    if(id < num_p && id < ((s+1)*streamSize)){
        uptdateParticle(particles, iter, id, num_p);
    }
}

void init_Array(particle *particulas){
    for(int i = 0; i < NUM_PARTICLES; i++){
        particulas[i].position[0] = rand() % 1000;
        particulas[i].position[1] = rand() % 1000;
        particulas[i].position[2] = rand() % 1000;

        particulas[i].velocity[0] = rand() % 1000;
        particulas[i].velocity[1] = rand() % 1000;
        particulas[i].velocity[2] = rand() % 1000;
    }
}

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

    
    bool bien = true;
    


    double start_GPU, stop_GPU;
    double start_CPU, stop_CPU;
    double diferencia_CPU, diferencia_GPU;


    particle *particlesCPU = (particle*)malloc(NUM_PARTICLES * sizeof(particle));
    particle *particlesGPU;
    particle *resCPU;

    cudaMallocHost(&resCPU, NUM_PARTICLES * sizeof(particle));

    cudaMalloc(&particlesGPU, NUM_PARTICLES * sizeof(particle));

    init_Array(particlesCPU);

    cudaMemcpy(resCPU, particlesCPU, NUM_PARTICLES*sizeof(particle), cudaMemcpyHostToHost);

    const int streamSize = NUM_PARTICLES / NSTREAMS;
    const int StreamBytes = streamSize * sizeof(particle);
    cudaStream_t stream[NSTREAMS];
    for(int i = 0; i < NSTREAMS; i++){
        cudaStreamCreate(&stream[i]);
    }
    int GRID = (streamSize + BLOCK_SIZE - 1)/BLOCK_SIZE;

    // CPU part//

    start_CPU = cpuSecond();

    for(int i = 0; i < NUM_ITERATIONS; i++){
        for(int j = 0; j < NUM_PARTICLES; j++){
            uptdateParticle(particlesCPU, i, j, NUM_PARTICLES);
        }
    };


    stop_CPU = cpuSecond();
    diferencia_CPU = stop_CPU - start_CPU;

    // Finish CPU part

    //Start GPU part

    start_GPU = cpuSecond();

    

    for(int s = 0; s < NSTREAMS; s++){
        
        int offset = s * streamSize;

        cudaMemcpyAsync(&particlesGPU[offset], &resCPU[offset], StreamBytes, cudaMemcpyHostToDevice, stream[s]);

        for(int i = 0; i < NUM_ITERATIONS; i++){
            
            timeStep<<<GRID, BLOCK_SIZE, 0, stream[s]>>>(particlesGPU, i, NUM_PARTICLES, offset, s, streamSize);
        
        }

        cudaMemcpyAsync(&resCPU[offset], &particlesGPU[offset], StreamBytes, cudaMemcpyDeviceToHost, stream[s]);

        cudaStreamSynchronize(stream[s]);
    }
        


    stop_GPU = cpuSecond();

    diferencia_GPU = stop_GPU - start_GPU;

    for(int i = 0; i < NUM_PARTICLES && bien; i++){
        for(int dim = 0; dim < 3; dim++){
            if(fabs(particlesCPU[i].position[dim] - resCPU[i].position[dim]) > error ){
                bien = false;
                break;
            }
        }
    }

    printf("NUM_ITERATIONS: %d\n", NUM_ITERATIONS);
    printf("NUM_PARTICLES: %d\n", NUM_PARTICLES);
    printf("BLOCK_SIZE: %d\n", BLOCK_SIZE);
    if(bien){
        printf("datos correctos\n");
    }else{
        printf("datos incorrectos\n");
    }
        
    cudaFree(particlesGPU);
    cudaFreeHost(resCPU);
    delete[] particlesCPU;
    
    for(int i = 0; i < NSTREAMS; i++){
        cudaStreamDestroy(stream[i]);
    }
    

    printf("Duration of the CPU: %f\n", diferencia_CPU);
    printf("Duration of the GPU: %f\n", diferencia_GPU);
    printf("--------------------------------------------\n");
    
    return 0;
}

	.file	"tmpxft_002fab69_00000000-6_exercise_3.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2033:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2033:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z9cpuSecondv
	.type	_Z9cpuSecondv, @function
_Z9cpuSecondv:
.LFB2027:
	.cfi_startproc
	endbr64
	subq	$40, %rsp
	.cfi_def_cfa_offset 48
	xorl	%esi, %esi
	movq	%fs:40, %rax
	movq	%rax, 24(%rsp)
	xorl	%eax, %eax
	leaq	8(%rsp), %rdi
	call	gettimeofday@PLT
	cvtsi2sdq	16(%rsp), %xmm0
	mulsd	.LC0(%rip), %xmm0
	cvtsi2sdq	8(%rsp), %xmm1
	addsd	%xmm1, %xmm0
	movq	24(%rsp), %rax
	subq	%fs:40, %rax
	je	.L3
	call	__stack_chk_fail@PLT
.L3:
	addq	$40, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2027:
	.size	_Z9cpuSecondv, .-_Z9cpuSecondv
	.globl	_Z15uptdateParticleP8particleiii
	.type	_Z15uptdateParticleP8particleiii, @function
_Z15uptdateParticleP8particleiii:
.LFB2028:
	.cfi_startproc
	endbr64
	movslq	%edx, %rax
	movq	%rdi, %r9
	movl	%ecx, %edi
	movq	%rax, %r8
	leal	(%rax,%rax,2), %ecx
	imulq	$24, %rax, %rax
	addl	%esi, %ecx
	leaq	(%r9,%rax), %rsi
	movl	%ecx, %eax
	addl	%r8d, %ecx
	cltd
	idivl	%edi
	movl	%ecx, %eax
	cvtsi2ssl	%edx, %xmm2
	cltd
	idivl	%edi
	movss	%xmm2, 12(%rsi)
	leal	(%rcx,%r8), %eax
	addss	(%rsi), %xmm2
	movss	%xmm2, (%rsi)
	cvtsi2ssl	%edx, %xmm1
	cltd
	idivl	%edi
	movss	%xmm1, 16(%rsi)
	addss	4(%rsi), %xmm1
	movss	%xmm1, 4(%rsi)
	cvtsi2ssl	%edx, %xmm0
	movss	%xmm0, 20(%rsi)
	addss	8(%rsi), %xmm0
	movss	%xmm0, 8(%rsi)
	ret
	.cfi_endproc
.LFE2028:
	.size	_Z15uptdateParticleP8particleiii, .-_Z15uptdateParticleP8particleiii
	.globl	_Z10init_ArrayP8particle
	.type	_Z10init_ArrayP8particle, @function
_Z10init_ArrayP8particle:
.LFB2029:
	.cfi_startproc
	endbr64
	pushq	%r12
	.cfi_def_cfa_offset 16
	.cfi_offset 12, -16
	leaq	2400000(%rdi), %r12
	pushq	%rbp
	.cfi_def_cfa_offset 24
	.cfi_offset 6, -24
	movl	$1000, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset 3, -32
	movq	%rdi, %rbx
.L8:
	call	rand@PLT
	addq	$24, %rbx
	cltd
	idivl	%ebp
	cvtsi2ssl	%edx, %xmm0
	movss	%xmm0, -24(%rbx)
	call	rand@PLT
	cltd
	idivl	%ebp
	cvtsi2ssl	%edx, %xmm0
	movss	%xmm0, -20(%rbx)
	call	rand@PLT
	cltd
	idivl	%ebp
	cvtsi2ssl	%edx, %xmm0
	movss	%xmm0, -16(%rbx)
	call	rand@PLT
	cltd
	idivl	%ebp
	cvtsi2ssl	%edx, %xmm0
	movss	%xmm0, -12(%rbx)
	call	rand@PLT
	cltd
	idivl	%ebp
	cvtsi2ssl	%edx, %xmm0
	movss	%xmm0, -8(%rbx)
	call	rand@PLT
	cltd
	idivl	%ebp
	cvtsi2ssl	%edx, %xmm0
	movss	%xmm0, -4(%rbx)
	cmpq	%r12, %rbx
	jne	.L8
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%rbp
	.cfi_def_cfa_offset 16
	popq	%r12
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2029:
	.size	_Z10init_ArrayP8particle, .-_Z10init_ArrayP8particle
	.globl	_Z40__device_stub__Z8timeStepP8particleiiiiiP8particleiiiii
	.type	_Z40__device_stub__Z8timeStepP8particleiiiiiP8particleiiiii, @function
_Z40__device_stub__Z8timeStepP8particleiiiiiP8particleiiiii:
.LFB2055:
	.cfi_startproc
	endbr64
	subq	$168, %rsp
	.cfi_def_cfa_offset 176
	movq	%rdi, 24(%rsp)
	leaq	56(%rsp), %rdi
	movl	%esi, 20(%rsp)
	leaq	68(%rsp), %rsi
	movl	%edx, 16(%rsp)
	leaq	40(%rsp), %rdx
	movl	%ecx, 12(%rsp)
	leaq	48(%rsp), %rcx
	movl	%r8d, 8(%rsp)
	movl	%r9d, 4(%rsp)
	movq	%fs:40, %rax
	movq	%rax, 152(%rsp)
	xorl	%eax, %eax
	leaq	24(%rsp), %rax
	movl	$1, 64(%rsp)
	movq	%rax, 104(%rsp)
	leaq	20(%rsp), %rax
	movq	%rax, 112(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 120(%rsp)
	leaq	12(%rsp), %rax
	movq	%rax, 128(%rsp)
	leaq	8(%rsp), %rax
	movq	%rax, 136(%rsp)
	leaq	4(%rsp), %rax
	movq	%rax, 144(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 56(%rsp)
	movq	%rax, 68(%rsp)
	movl	$1, 76(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L11
	pushq	48(%rsp)
	.cfi_def_cfa_offset 184
	leaq	_Z8timeStepP8particleiiiii(%rip), %rdi
	pushq	48(%rsp)
	.cfi_def_cfa_offset 192
	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 184
	popq	%rdx
	.cfi_def_cfa_offset 176
.L11:
	movq	152(%rsp), %rax
	subq	%fs:40, %rax
	je	.L13
	call	__stack_chk_fail@PLT
.L13:
	addq	$168, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2055:
	.size	_Z40__device_stub__Z8timeStepP8particleiiiiiP8particleiiiii, .-_Z40__device_stub__Z8timeStepP8particleiiiiiP8particleiiiii
	.globl	_Z8timeStepP8particleiiiii
	.type	_Z8timeStepP8particleiiiii, @function
_Z8timeStepP8particleiiiii:
.LFB2056:
	.cfi_startproc
	endbr64
	jmp	_Z40__device_stub__Z8timeStepP8particleiiiiiP8particleiiiii
	.cfi_endproc
.LFE2056:
	.size	_Z8timeStepP8particleiiiii, .-_Z8timeStepP8particleiiiii
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC2:
	.string	"NUM_ITERATIONS: %d\n"
.LC3:
	.string	"NUM_PARTICLES: %d\n"
.LC4:
	.string	"BLOCK_SIZE: %d\n"
.LC5:
	.string	"datos correctos\n"
.LC6:
	.string	"datos incorrectos\n"
.LC7:
	.string	"Duration of the CPU: %f\n"
.LC8:
	.string	"Duration of the GPU: %f\n"
.LC9:
	.string	"--------------------------------------------\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2030:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	movl	$2400000, %edi
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	subq	$120, %rsp
	.cfi_def_cfa_offset 176
	movq	%fs:40, %rax
	movq	%rax, 104(%rsp)
	xorl	%eax, %eax
	leaq	72(%rsp), %r15
	call	malloc@PLT
	xorl	%edx, %edx
	leaq	40(%rsp), %rdi
	movl	$2400000, %esi
	movq	%rax, %rbx
	call	cudaHostAlloc@PLT
	movl	$2400000, %esi
	leaq	32(%rsp), %rdi
	call	cudaMalloc@PLT
	movq	%rbx, %rdi
	call	_Z10init_ArrayP8particle
	movq	40(%rsp), %rdi
	xorl	%ecx, %ecx
	movq	%rbx, %rsi
	movl	$2400000, %edx
	call	cudaMemcpy@PLT
	movq	%r15, %rdi
	call	cudaStreamCreate@PLT
	leaq	80(%rsp), %rdi
	call	cudaStreamCreate@PLT
	leaq	88(%rsp), %rdi
	call	cudaStreamCreate@PLT
	leaq	96(%rsp), %rdi
	call	cudaStreamCreate@PLT
	call	_Z9cpuSecondv
	xorl	%r11d, %r11d
	movsd	%xmm0, 8(%rsp)
.L17:
	xorl	%r10d, %r10d
.L18:
	movl	%r10d, %edx
	movl	$100000, %ecx
	movl	%r11d, %esi
	movq	%rbx, %rdi
	call	_Z15uptdateParticleP8particleiii
	incl	%r10d
	cmpl	$100000, %r10d
	jne	.L18
	incl	%r11d
	cmpl	$1000, %r11d
	jne	.L17
	call	_Z9cpuSecondv
	subsd	8(%rsp), %xmm0
	movq	%r15, %r12
	xorl	%ebp, %ebp
	movsd	%xmm0, 8(%rsp)
	call	_Z9cpuSecondv
	movsd	%xmm0, 16(%rsp)
.L22:
	imulq	$600000, %rbp, %r13
	movq	40(%rsp), %rsi
	movq	32(%rsp), %rdi
	movl	$1, %ecx
	movq	(%r12), %r8
	movl	$600000, %edx
	movl	%ebp, 28(%rsp)
	xorl	%r14d, %r14d
	imull	$25000, %ebp, %eax
	addq	%r13, %rsi
	addq	%r13, %rdi
	movl	%eax, 24(%rsp)
	call	cudaMemcpyAsync@PLT
.L21:
	movl	$16777217, %eax
	movl	$2147483697, %edi
	movq	(%r12), %r9
	xorl	%r8d, %r8d
	salq	$8, %rax
	addq	%rdi, %rdi
	movl	$1, %ecx
	movl	$1, %esi
	movq	%rax, %rdx
	movq	%rax, 60(%rsp)
	movl	$1, 68(%rsp)
	movq	%rdi, 48(%rsp)
	movl	$1, 56(%rsp)
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L20
	movl	28(%rsp), %r8d
	movl	24(%rsp), %ecx
	movl	$100000, %edx
	movl	$25000, %r9d
	movq	32(%rsp), %rdi
	movl	%r14d, %esi
	call	_Z40__device_stub__Z8timeStepP8particleiiiiiP8particleiiiii
.L20:
	incl	%r14d
	cmpl	$1000, %r14d
	jne	.L21
	movq	32(%rsp), %rsi
	movq	(%r12), %r8
	incq	%rbp
	addq	$8, %r12
	movq	40(%rsp), %rdi
	movl	$2, %ecx
	movl	$600000, %edx
	addq	%r13, %rsi
	addq	%r13, %rdi
	call	cudaMemcpyAsync@PLT
	movq	-8(%r12), %rdi
	call	cudaStreamSynchronize@PLT
	cmpq	$4, %rbp
	jne	.L22
	call	_Z9cpuSecondv
	subsd	16(%rsp), %xmm0
	xorl	%edx, %edx
	xorl	%ecx, %ecx
	movq	40(%rsp), %rdi
	movsd	.LC0(%rip), %xmm2
	movss	.LC1(%rip), %xmm1
	movq	%xmm0, %rbp
.L23:
	leaq	(%rbx,%rdx), %r8
	leaq	(%rdi,%rdx), %rsi
	xorl	%eax, %eax
.L25:
	movss	(%r8,%rax), %xmm0
	subss	(%rsi,%rax), %xmm0
	andps	%xmm1, %xmm0
	cvtss2sd	%xmm0, %xmm0
	comisd	%xmm2, %xmm0
	ja	.L32
	addq	$4, %rax
	cmpq	$12, %rax
	jne	.L25
	movb	$1, %r12b
	jmp	.L24
.L32:
	xorl	%r12d, %r12d
.L24:
	incl	%ecx
	addq	$24, %rdx
	cmpl	$99999, %ecx
	jg	.L26
	testb	%r12b, %r12b
	jne	.L23
.L26:
	movl	$1000, %edx
	leaq	.LC2(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	$100000, %edx
	leaq	.LC3(%rip), %rsi
	xorl	%eax, %eax
	movl	$2, %edi
	call	__printf_chk@PLT
	leaq	.LC4(%rip), %rsi
	movl	$256, %edx
	xorl	%eax, %eax
	movl	$2, %edi
	call	__printf_chk@PLT
	leaq	.LC5(%rip), %rsi
	testb	%r12b, %r12b
	jne	.L48
	leaq	.LC6(%rip), %rsi
.L48:
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movq	32(%rsp), %rdi
	call	cudaFree@PLT
	movq	40(%rsp), %rdi
	call	cudaFreeHost@PLT
	testq	%rbx, %rbx
	je	.L29
	movq	%rbx, %rdi
	call	_ZdaPv@PLT
.L29:
	xorl	%ebx, %ebx
.L30:
	movq	(%r15,%rbx,8), %rdi
	incq	%rbx
	call	cudaStreamDestroy@PLT
	cmpq	$4, %rbx
	jne	.L30
	movsd	8(%rsp), %xmm0
	leaq	.LC7(%rip), %rsi
	movl	$2, %edi
	movb	$1, %al
	call	__printf_chk@PLT
	leaq	.LC8(%rip), %rsi
	movl	$2, %edi
	movb	$1, %al
	movq	%rbp, %xmm0
	call	__printf_chk@PLT
	xorl	%eax, %eax
	movl	$2, %edi
	leaq	.LC9(%rip), %rsi
	call	__printf_chk@PLT
	movq	104(%rsp), %rax
	subq	%fs:40, %rax
	je	.L31
	call	__stack_chk_fail@PLT
.L31:
	addq	$120, %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
.LFE2030:
	.size	main, .-main
	.section	.rodata.str1.1
.LC10:
	.string	"_Z8timeStepP8particleiiiii"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2058:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	leaq	_ZL15__fatDeviceText(%rip), %rdi
	call	__cudaRegisterFatBinary@PLT
	pushq	$0
	.cfi_def_cfa_offset 24
	xorl	%r9d, %r9d
	orl	$-1, %r8d
	pushq	$0
	.cfi_def_cfa_offset 32
	leaq	.LC10(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z8timeStepP8particleiiiii(%rip), %rsi
	pushq	$0
	.cfi_def_cfa_offset 40
	movq	%rdx, %rcx
	pushq	$0
	.cfi_def_cfa_offset 48
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	call	__cudaRegisterFunction@PLT
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	call	__cudaRegisterFatBinaryEnd@PLT
	leaq	_ZL26__cudaUnregisterBinaryUtilv(%rip), %rdi
	popq	%rdx
	.cfi_def_cfa_offset 8
	jmp	atexit@PLT
	.cfi_endproc
.LFE2058:
	.size	_ZL24__sti____cudaRegisterAllv, .-_ZL24__sti____cudaRegisterAllv
	.section	.init_array,"aw"
	.align 8
	.quad	_ZL24__sti____cudaRegisterAllv
	.section	.nvFatBinSegment,"aw"
	.align 8
	.type	_ZL15__fatDeviceText, @object
	.size	_ZL15__fatDeviceText, 24
_ZL15__fatDeviceText:
	.long	1180844977
	.long	1
	.quad	fatbinData
	.quad	0
	.local	_ZL20__cudaFatCubinHandle
	.comm	_ZL20__cudaFatCubinHandle,8,8
	.section	.rodata.cst8,"aM",@progbits,8
	.align 8
.LC0:
	.long	-1598689907
	.long	1051772663
	.section	.rodata.cst16,"aM",@progbits,16
	.align 16
.LC1:
	.long	2147483647
	.long	0
	.long	0
	.long	0
	.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 <sys/time.h>

#define error 1e-6

#define NUM_ITERATIONS 1000
#define NUM_PARTICLES 100000
#define BLOCK_SIZE 256
#define NSTREAMS 4
#define SIZE_BATCH 


struct particle {
    float position[3];
    float velocity[3];
};

double cpuSecond() {
    struct timeval tp;
    gettimeofday(&tp,NULL);
    return ((double)tp.tv_sec + (double)tp.tv_usec*1.e-6);
}

__host__ __device__ void uptdateParticle(particle *particula, int iter, int id, int num_p){
    //update the velocity:
    particula[id].velocity[0] = (3*id + iter) % num_p;
    particula[id].velocity[1] = (4*id + iter) % num_p;
    particula[id].velocity[2] = (5*id + iter) % num_p;

    //update the position:
    particula[id].position[0] = particula[id].position[0] + particula[id].velocity[0]; 
    particula[id].position[1] = particula[id].position[1] + particula[id].velocity[1]; 
    particula[id].position[2] = particula[id].position[2] + particula[id].velocity[2]; 
}

__global__ void timeStep(particle *particles, int iter, int num_p, int offset, int s, int streamSize){
    const int id = offset + threadIdx.x + blockIdx.x*blockDim.x;
    if(id < num_p && id < ((s+1)*streamSize)){
        uptdateParticle(particles, iter, id, num_p);
    }
}

void init_Array(particle *particulas){
    for(int i = 0; i < NUM_PARTICLES; i++){
        particulas[i].position[0] = rand() % 1000;
        particulas[i].position[1] = rand() % 1000;
        particulas[i].position[2] = rand() % 1000;

        particulas[i].velocity[0] = rand() % 1000;
        particulas[i].velocity[1] = rand() % 1000;
        particulas[i].velocity[2] = rand() % 1000;
    }
}

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

    
    bool bien = true;
    


    double start_GPU, stop_GPU;
    double start_CPU, stop_CPU;
    double diferencia_CPU, diferencia_GPU;


    particle *particlesCPU = (particle*)malloc(NUM_PARTICLES * sizeof(particle));
    particle *particlesGPU;
    particle *resCPU;

    hipHostMalloc(&resCPU, NUM_PARTICLES * sizeof(particle), hipHostMallocDefault);

    hipMalloc(&particlesGPU, NUM_PARTICLES * sizeof(particle));

    init_Array(particlesCPU);

    hipMemcpy(resCPU, particlesCPU, NUM_PARTICLES*sizeof(particle), hipMemcpyHostToHost);

    const int streamSize = NUM_PARTICLES / NSTREAMS;
    const int StreamBytes = streamSize * sizeof(particle);
    hipStream_t stream[NSTREAMS];
    for(int i = 0; i < NSTREAMS; i++){
        hipStreamCreate(&stream[i]);
    }
    int GRID = (streamSize + BLOCK_SIZE - 1)/BLOCK_SIZE;

    // CPU part//

    start_CPU = cpuSecond();

    for(int i = 0; i < NUM_ITERATIONS; i++){
        for(int j = 0; j < NUM_PARTICLES; j++){
            uptdateParticle(particlesCPU, i, j, NUM_PARTICLES);
        }
    };


    stop_CPU = cpuSecond();
    diferencia_CPU = stop_CPU - start_CPU;

    // Finish CPU part

    //Start GPU part

    start_GPU = cpuSecond();

    

    for(int s = 0; s < NSTREAMS; s++){
        
        int offset = s * streamSize;

        hipMemcpyAsync(&particlesGPU[offset], &resCPU[offset], StreamBytes, hipMemcpyHostToDevice, stream[s]);

        for(int i = 0; i < NUM_ITERATIONS; i++){
            
            timeStep<<<GRID, BLOCK_SIZE, 0, stream[s]>>>(particlesGPU, i, NUM_PARTICLES, offset, s, streamSize);
        
        }

        hipMemcpyAsync(&resCPU[offset], &particlesGPU[offset], StreamBytes, hipMemcpyDeviceToHost, stream[s]);

        hipStreamSynchronize(stream[s]);
    }
        


    stop_GPU = cpuSecond();

    diferencia_GPU = stop_GPU - start_GPU;

    for(int i = 0; i < NUM_PARTICLES && bien; i++){
        for(int dim = 0; dim < 3; dim++){
            if(fabs(particlesCPU[i].position[dim] - resCPU[i].position[dim]) > error ){
                bien = false;
                break;
            }
        }
    }

    printf("NUM_ITERATIONS: %d\n", NUM_ITERATIONS);
    printf("NUM_PARTICLES: %d\n", NUM_PARTICLES);
    printf("BLOCK_SIZE: %d\n", BLOCK_SIZE);
    if(bien){
        printf("datos correctos\n");
    }else{
        printf("datos incorrectos\n");
    }
        
    hipFree(particlesGPU);
    hipHostFree(resCPU);
    delete[] particlesCPU;
    
    for(int i = 0; i < NSTREAMS; i++){
        hipStreamDestroy(stream[i]);
    }
    

    printf("Duration of the CPU: %f\n", diferencia_CPU);
    printf("Duration of the GPU: %f\n", diferencia_GPU);
    printf("--------------------------------------------\n");
    
    return 0;
}

	.text
	.file	"exercise_3.hip"
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function _Z9cpuSecondv
.LCPI0_0:
	.quad	0x3eb0c6f7a0b5ed8d              # double 9.9999999999999995E-7
	.text
	.globl	_Z9cpuSecondv
	.type	_Z9cpuSecondv,@function
_Z9cpuSecondv:                          # @_Z9cpuSecondv
	.cfi_startproc
# %bb.0:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	subq	$16, %rsp
	.cfi_def_cfa_offset 32
	.cfi_offset %rbx, -16
	movq	%rsp, %rbx
	movq	%rbx, %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	cvtsi2sdq	(%rbx), %xmm1
	cvtsi2sdq	8(%rbx), %xmm0
	mulsd	.LCPI0_0(%rip), %xmm0
	addsd	%xmm1, %xmm0
	addq	$16, %rsp
	.cfi_def_cfa_offset 16
	popq	%rbx
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end0:
	.size	_Z9cpuSecondv, .Lfunc_end0-_Z9cpuSecondv
	.cfi_endproc
                                        # -- End function
	.globl	_Z15uptdateParticleP8particleiii # -- Begin function _Z15uptdateParticleP8particleiii
	.type	_Z15uptdateParticleP8particleiii,@function
_Z15uptdateParticleP8particleiii:       # @_Z15uptdateParticleP8particleiii
	.cfi_startproc
# %bb.0:
	movl	%edx, %r8d
                                        # kill: def $esi killed $esi def $rsi
	leal	(%r8,%r8,2), %eax
	addl	%esi, %eax
	cltd
	idivl	%ecx
	cvtsi2ss	%edx, %xmm0
	movslq	%r8d, %rax
	leaq	(%rax,%rax,2), %r9
	movss	%xmm0, 12(%rdi,%r9,8)
	leal	(%rsi,%r8,4), %eax
	cltd
	idivl	%ecx
	cvtsi2ss	%edx, %xmm1
	movss	%xmm1, 16(%rdi,%r9,8)
	leal	(%r8,%r8,4), %eax
	addl	%esi, %eax
	cltd
	idivl	%ecx
	cvtsi2ss	%edx, %xmm2
	movss	%xmm2, 20(%rdi,%r9,8)
	addss	(%rdi,%r9,8), %xmm0
	movss	%xmm0, (%rdi,%r9,8)
	addss	4(%rdi,%r9,8), %xmm1
	movss	%xmm1, 4(%rdi,%r9,8)
	addss	8(%rdi,%r9,8), %xmm2
	movss	%xmm2, 8(%rdi,%r9,8)
	retq
.Lfunc_end1:
	.size	_Z15uptdateParticleP8particleiii, .Lfunc_end1-_Z15uptdateParticleP8particleiii
	.cfi_endproc
                                        # -- End function
	.globl	_Z23__device_stub__timeStepP8particleiiiii # -- Begin function _Z23__device_stub__timeStepP8particleiiiii
	.type	_Z23__device_stub__timeStepP8particleiiiii,@function
_Z23__device_stub__timeStepP8particleiiiii: # @_Z23__device_stub__timeStepP8particleiiiii
	.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	$128, %rsp
	.cfi_def_cfa_offset 176
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	leaq	40(%rsp), %rax
	movq	%rdi, (%rax)
	leaq	20(%rsp), %rdi
	movl	%esi, (%rdi)
	leaq	16(%rsp), %rsi
	movl	%edx, (%rsi)
	leaq	12(%rsp), %rdx
	movl	%ecx, (%rdx)
	leaq	8(%rsp), %rcx
	movl	%r8d, (%rcx)
	leaq	4(%rsp), %r8
	movl	%r9d, (%r8)
	leaq	80(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rdi, 8(%rbx)
	movq	%rsi, 16(%rbx)
	movq	%rdx, 24(%rbx)
	movq	%rcx, 32(%rbx)
	movq	%r8, 40(%rbx)
	leaq	64(%rsp), %r14
	leaq	48(%rsp), %r15
	leaq	32(%rsp), %r12
	leaq	24(%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	$_Z8timeStepP8particleiiiii, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$144, %rsp
	.cfi_adjust_cfa_offset -144
	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__timeStepP8particleiiiii, .Lfunc_end2-_Z23__device_stub__timeStepP8particleiiiii
	.cfi_endproc
                                        # -- End function
	.globl	_Z10init_ArrayP8particle        # -- Begin function _Z10init_ArrayP8particle
	.type	_Z10init_ArrayP8particle,@function
_Z10init_ArrayP8particle:               # @_Z10init_ArrayP8particle
	.cfi_startproc
# %bb.0:
	pushq	%r14
	.cfi_def_cfa_offset 16
	pushq	%rbx
	.cfi_def_cfa_offset 24
	pushq	%rax
	.cfi_def_cfa_offset 32
	.cfi_offset %rbx, -24
	.cfi_offset %r14, -16
	movq	%rdi, %rbx
	movl	$20, %r14d
.LBB3_1:                                # =>This Inner Loop Header: Depth=1
	callq	rand
	cltq
	imulq	$274877907, %rax, %rcx          # imm = 0x10624DD3
	movq	%rcx, %rdx
	shrq	$63, %rdx
	sarq	$38, %rcx
	addl	%edx, %ecx
	imull	$1000, %ecx, %ecx               # imm = 0x3E8
	subl	%ecx, %eax
	xorps	%xmm0, %xmm0
	cvtsi2ss	%eax, %xmm0
	movss	%xmm0, -20(%rbx,%r14)
	callq	rand
	cltq
	imulq	$274877907, %rax, %rcx          # imm = 0x10624DD3
	movq	%rcx, %rdx
	shrq	$63, %rdx
	sarq	$38, %rcx
	addl	%edx, %ecx
	imull	$1000, %ecx, %ecx               # imm = 0x3E8
	subl	%ecx, %eax
	xorps	%xmm0, %xmm0
	cvtsi2ss	%eax, %xmm0
	movss	%xmm0, -16(%rbx,%r14)
	callq	rand
	cltq
	imulq	$274877907, %rax, %rcx          # imm = 0x10624DD3
	movq	%rcx, %rdx
	shrq	$63, %rdx
	sarq	$38, %rcx
	addl	%edx, %ecx
	imull	$1000, %ecx, %ecx               # imm = 0x3E8
	subl	%ecx, %eax
	xorps	%xmm0, %xmm0
	cvtsi2ss	%eax, %xmm0
	movss	%xmm0, -12(%rbx,%r14)
	callq	rand
	cltq
	imulq	$274877907, %rax, %rcx          # imm = 0x10624DD3
	movq	%rcx, %rdx
	shrq	$63, %rdx
	sarq	$38, %rcx
	addl	%edx, %ecx
	imull	$1000, %ecx, %ecx               # imm = 0x3E8
	subl	%ecx, %eax
	xorps	%xmm0, %xmm0
	cvtsi2ss	%eax, %xmm0
	movss	%xmm0, -8(%rbx,%r14)
	callq	rand
	cltq
	imulq	$274877907, %rax, %rcx          # imm = 0x10624DD3
	movq	%rcx, %rdx
	shrq	$63, %rdx
	sarq	$38, %rcx
	addl	%edx, %ecx
	imull	$1000, %ecx, %ecx               # imm = 0x3E8
	subl	%ecx, %eax
	xorps	%xmm0, %xmm0
	cvtsi2ss	%eax, %xmm0
	movss	%xmm0, -4(%rbx,%r14)
	callq	rand
	cltq
	imulq	$274877907, %rax, %rcx          # imm = 0x10624DD3
	movq	%rcx, %rdx
	shrq	$63, %rdx
	sarq	$38, %rcx
	addl	%edx, %ecx
	imull	$1000, %ecx, %ecx               # imm = 0x3E8
	subl	%ecx, %eax
	xorps	%xmm0, %xmm0
	cvtsi2ss	%eax, %xmm0
	movss	%xmm0, (%rbx,%r14)
	addq	$24, %r14
	cmpq	$2400020, %r14                  # imm = 0x249F14
	jne	.LBB3_1
# %bb.2:
	addq	$8, %rsp
	.cfi_def_cfa_offset 24
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%r14
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end3:
	.size	_Z10init_ArrayP8particle, .Lfunc_end3-_Z10init_ArrayP8particle
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst16,"aM",@progbits,16
	.p2align	4, 0x0                          # -- Begin function main
.LCPI4_0:
	.long	0x7fffffff                      # float NaN
	.long	0x7fffffff                      # float NaN
	.long	0x7fffffff                      # float NaN
	.long	0x7fffffff                      # float NaN
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0
.LCPI4_1:
	.quad	0x3eb0c6f7a0b5ed8d              # double 9.9999999999999995E-7
	.text
	.globl	main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:
	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	$136, %rsp
	.cfi_def_cfa_offset 192
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movl	$2400000, %edi                  # imm = 0x249F00
	callq	malloc
	movq	%rax, %rbx
	leaq	8(%rsp), %r14
	xorl	%r15d, %r15d
	movl	$2400000, %esi                  # imm = 0x249F00
	movq	%r14, %rdi
	xorl	%edx, %edx
	callq	hipHostMalloc
	leaq	16(%rsp), %rdi
	movl	$2400000, %esi                  # imm = 0x249F00
	callq	hipMalloc
	movq	%rbx, %rdi
	callq	_Z10init_ArrayP8particle
	movq	(%r14), %rdi
	movl	$2400000, %edx                  # imm = 0x249F00
	movq	%rbx, %rsi
	xorl	%ecx, %ecx
	callq	hipMemcpy
.LBB4_1:                                # =>This Inner Loop Header: Depth=1
	leaq	(%rsp,%r15), %rdi
	addq	$96, %rdi
	callq	hipStreamCreate
	addq	$8, %r15
	cmpq	$32, %r15
	jne	.LBB4_1
# %bb.2:
	xorl	%r14d, %r14d
	leaq	80(%rsp), %r15
	movq	%r15, %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	movq	(%r15), %rax
	movq	%rax, 72(%rsp)                  # 8-byte Spill
	movq	8(%r15), %rax
	movq	%rax, 64(%rsp)                  # 8-byte Spill
.LBB4_3:                                # %.preheader66
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB4_4 Depth 2
	xorl	%ebp, %ebp
.LBB4_4:                                #   Parent Loop BB4_3 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movq	%rbx, %rdi
	movl	%r14d, %esi
	movl	%ebp, %edx
	movl	$100000, %ecx                   # imm = 0x186A0
	callq	_Z15uptdateParticleP8particleiii
	incl	%ebp
	cmpl	$100000, %ebp                   # imm = 0x186A0
	jne	.LBB4_4
# %bb.5:                                #   in Loop: Header=BB4_3 Depth=1
	incl	%r14d
	cmpl	$1000, %r14d                    # imm = 0x3E8
	jne	.LBB4_3
# %bb.6:
	movabsq	$4294967394, %r14               # imm = 0x100000062
	xorl	%r15d, %r15d
	leaq	80(%rsp), %r12
	movq	%r12, %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	movq	(%r12), %rax
	movq	%rax, 56(%rsp)                  # 8-byte Spill
	movq	8(%r12), %rax
	movq	%rax, 48(%rsp)                  # 8-byte Spill
	leaq	80(%rsp), %r12
	movq	%r12, %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	movq	(%r12), %rax
	movq	%rax, 40(%rsp)                  # 8-byte Spill
	movq	8(%r12), %rax
	movq	%rax, 32(%rsp)                  # 8-byte Spill
	leaq	158(%r14), %r12
.LBB4_7:                                # =>This Loop Header: Depth=1
                                        #     Child Loop BB4_8 Depth 2
	imulq	$25000, %r15, %rax              # imm = 0x61A8
	movq	%rax, 24(%rsp)                  # 8-byte Spill
	imull	$25000, %r15d, %ebp             # imm = 0x61A8
	imulq	$600000, %r15, %rsi             # imm = 0x927C0
	movq	16(%rsp), %rdi
	addq	%rsi, %rdi
	addq	8(%rsp), %rsi
	movq	96(%rsp,%r15,8), %r8
	movl	$600000, %edx                   # imm = 0x927C0
	movl	$1, %ecx
	callq	hipMemcpyAsync
	xorl	%r13d, %r13d
.LBB4_8:                                #   Parent Loop BB4_7 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movq	96(%rsp,%r15,8), %r9
	movq	%r14, %rdi
	movl	$1, %esi
	movq	%r12, %rdx
	movl	$1, %ecx
	xorl	%r8d, %r8d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB4_10
# %bb.9:                                #   in Loop: Header=BB4_8 Depth=2
	movq	16(%rsp), %rdi
	movl	%r13d, %esi
	movl	$100000, %edx                   # imm = 0x186A0
	movl	%ebp, %ecx
	movl	%r15d, %r8d
	movl	$25000, %r9d                    # imm = 0x61A8
	callq	_Z23__device_stub__timeStepP8particleiiiii
.LBB4_10:                               #   in Loop: Header=BB4_8 Depth=2
	incl	%r13d
	cmpl	$1000, %r13d                    # imm = 0x3E8
	jne	.LBB4_8
# %bb.11:                               #   in Loop: Header=BB4_7 Depth=1
	movq	24(%rsp), %rax                  # 8-byte Reload
	shlq	$3, %rax
	leaq	(%rax,%rax,2), %rsi
	movq	8(%rsp), %rdi
	addq	%rsi, %rdi
	addq	16(%rsp), %rsi
	movq	96(%rsp,%r15,8), %r8
	movl	$600000, %edx                   # imm = 0x927C0
	movl	$2, %ecx
	callq	hipMemcpyAsync
	movq	96(%rsp,%r15,8), %rdi
	callq	hipStreamSynchronize
	incq	%r15
	cmpq	$4, %r15
	jne	.LBB4_7
# %bb.12:
	xorl	%r13d, %r13d
	leaq	80(%rsp), %r14
	movq	%r14, %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	movq	(%r14), %r15
	movq	8(%r14), %r12
	movq	8(%rsp), %rax
	movaps	.LCPI4_0(%rip), %xmm0           # xmm0 = [NaN,NaN,NaN,NaN]
	movq	%rbx, %rcx
.LBB4_13:                               # %.preheader
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB4_15 Depth 2
	xorl	%edx, %edx
.LBB4_15:                               #   Parent Loop BB4_13 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movss	(%rcx,%rdx,4), %xmm1            # xmm1 = mem[0],zero,zero,zero
	subss	(%rax,%rdx,4), %xmm1
	andps	%xmm0, %xmm1
	cvtss2sd	%xmm1, %xmm1
	ucomisd	.LCPI4_1(%rip), %xmm1
	ja	.LBB4_16
# %bb.14:                               #   in Loop: Header=BB4_15 Depth=2
	incq	%rdx
	cmpq	$3, %rdx
	jne	.LBB4_15
# %bb.17:                               #   in Loop: Header=BB4_13 Depth=1
	leaq	1(%r13), %rdx
	addq	$24, %rax
	addq	$24, %rcx
	cmpq	$99999, %r13                    # imm = 0x1869F
	movq	%rdx, %r13
	jb	.LBB4_13
# %bb.18:
	movl	$.Lstr.1, %r14d
	jmp	.LBB4_19
.LBB4_16:
	movl	$.Lstr, %r14d
.LBB4_19:                               # %.thread
	xorl	%r13d, %r13d
	movl	$.L.str, %edi
	movl	$1000, %esi                     # imm = 0x3E8
	xorl	%eax, %eax
	callq	printf
	movl	$.L.str.1, %edi
	movl	$100000, %esi                   # imm = 0x186A0
	xorl	%eax, %eax
	callq	printf
	movl	$.L.str.2, %edi
	movl	$256, %esi                      # imm = 0x100
	xorl	%eax, %eax
	callq	printf
	movq	%r14, %rdi
	callq	puts@PLT
	movq	16(%rsp), %rdi
	callq	hipFree
	movq	8(%rsp), %rdi
	callq	hipHostFree
	movq	%rbx, %rdi
	callq	_ZdaPv
.LBB4_20:                               # =>This Inner Loop Header: Depth=1
	movq	96(%rsp,%r13,8), %rdi
	callq	hipStreamDestroy
	incq	%r13
	cmpq	$4, %r13
	jne	.LBB4_20
# %bb.21:
	xorps	%xmm0, %xmm0
	cvtsi2sd	%r12, %xmm0
	movsd	.LCPI4_1(%rip), %xmm3           # xmm3 = mem[0],zero
	mulsd	%xmm3, %xmm0
	xorps	%xmm1, %xmm1
	cvtsi2sd	%r15, %xmm1
	addsd	%xmm0, %xmm1
	movapd	%xmm1, %xmm4
	xorps	%xmm0, %xmm0
	cvtsi2sdq	32(%rsp), %xmm0         # 8-byte Folded Reload
	xorps	%xmm1, %xmm1
	cvtsi2sdq	40(%rsp), %xmm1         # 8-byte Folded Reload
	mulsd	%xmm3, %xmm0
	addsd	%xmm0, %xmm1
	cvtsi2sdq	48(%rsp), %xmm2         # 8-byte Folded Reload
	subsd	%xmm1, %xmm4
	movsd	%xmm4, 24(%rsp)                 # 8-byte Spill
	mulsd	%xmm3, %xmm2
	xorps	%xmm0, %xmm0
	cvtsi2sdq	56(%rsp), %xmm0         # 8-byte Folded Reload
	addsd	%xmm2, %xmm0
	xorps	%xmm1, %xmm1
	cvtsi2sdq	64(%rsp), %xmm1         # 8-byte Folded Reload
	mulsd	%xmm3, %xmm1
	xorps	%xmm2, %xmm2
	cvtsi2sdq	72(%rsp), %xmm2         # 8-byte Folded Reload
	addsd	%xmm1, %xmm2
	subsd	%xmm2, %xmm0
	movl	$.L.str.5, %edi
	movb	$1, %al
	callq	printf
	movl	$.L.str.6, %edi
	movsd	24(%rsp), %xmm0                 # 8-byte Reload
                                        # xmm0 = mem[0],zero
	movb	$1, %al
	callq	printf
	movl	$.Lstr.2, %edi
	callq	puts@PLT
	xorl	%eax, %eax
	addq	$136, %rsp
	.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
.Lfunc_end4:
	.size	main, .Lfunc_end4-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	.LBB5_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rdi
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB5_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z8timeStepP8particleiiiii, %esi
	movl	$.L__unnamed_1, %edx
	movl	$.L__unnamed_1, %ecx
	movl	$-1, %r8d
	xorl	%r9d, %r9d
	callq	__hipRegisterFunction
	movl	$__hip_module_dtor, %edi
	addq	$40, %rsp
	.cfi_def_cfa_offset 8
	jmp	atexit                          # TAILCALL
.Lfunc_end5:
	.size	__hip_module_ctor, .Lfunc_end5-__hip_module_ctor
	.cfi_endproc
                                        # -- End function
	.p2align	4, 0x90                         # -- Begin function __hip_module_dtor
	.type	__hip_module_dtor,@function
__hip_module_dtor:                      # @__hip_module_dtor
	.cfi_startproc
# %bb.0:
	movq	__hip_gpubin_handle(%rip), %rdi
	testq	%rdi, %rdi
	je	.LBB6_2
# %bb.1:
	pushq	%rax
	.cfi_def_cfa_offset 16
	callq	__hipUnregisterFatBinary
	movq	$0, __hip_gpubin_handle(%rip)
	addq	$8, %rsp
	.cfi_def_cfa_offset 8
.LBB6_2:
	retq
.Lfunc_end6:
	.size	__hip_module_dtor, .Lfunc_end6-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	_Z8timeStepP8particleiiiii,@object # @_Z8timeStepP8particleiiiii
	.section	.rodata,"a",@progbits
	.globl	_Z8timeStepP8particleiiiii
	.p2align	3, 0x0
_Z8timeStepP8particleiiiii:
	.quad	_Z23__device_stub__timeStepP8particleiiiii
	.size	_Z8timeStepP8particleiiiii, 8

	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"NUM_ITERATIONS: %d\n"
	.size	.L.str, 20

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

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

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"Duration of the CPU: %f\n"
	.size	.L.str.5, 25

	.type	.L.str.6,@object                # @.str.6
.L.str.6:
	.asciz	"Duration of the GPU: %f\n"
	.size	.L.str.6, 25

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z8timeStepP8particleiiiii"
	.size	.L__unnamed_1, 27

	.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	"datos incorrectos"
	.size	.Lstr, 18

	.type	.Lstr.1,@object                 # @str.1
.Lstr.1:
	.asciz	"datos correctos"
	.size	.Lstr.1, 16

	.type	.Lstr.2,@object                 # @str.2
.Lstr.2:
	.asciz	"--------------------------------------------"
	.size	.Lstr.2, 45

	.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__timeStepP8particleiiiii
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z8timeStepP8particleiiiii
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z8timeStepP8particleiiiii ; -- Begin function _Z8timeStepP8particleiiiii
	.globl	_Z8timeStepP8particleiiiii
	.p2align	8
	.type	_Z8timeStepP8particleiiiii,@function
_Z8timeStepP8particleiiiii:             ; @_Z8timeStepP8particleiiiii
; %bb.0:
	s_clause 0x2
	s_load_b32 s2, s[0:1], 0x2c
	s_load_b128 s[4:7], s[0:1], 0x8
	s_load_b32 s3, s[0:1], 0x18
	s_waitcnt lgkmcnt(0)
	s_and_b32 s2, s2, 0xffff
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(SKIP_3) | instid1(SALU_CYCLE_1)
	s_mul_i32 s15, s15, s2
	s_add_i32 s2, s7, 1
	v_add3_u32 v0, s15, s6, v0
	s_mul_i32 s2, s2, s3
	s_min_i32 s2, s5, s2
	s_delay_alu instid0(VALU_DEP_1) | instid1(SALU_CYCLE_1)
	v_cmp_gt_i32_e32 vcc_lo, s2, v0
	s_and_saveexec_b32 s2, vcc_lo
	s_cbranch_execz .LBB0_2
; %bb.1:
	s_load_b64 s[0:1], s[0:1], 0x0
	s_waitcnt lgkmcnt(0)
	v_mad_i64_i32 v[7:8], null, v0, 24, s[0:1]
	s_ashr_i32 s0, s5, 31
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_add_i32 s1, s5, s0
	s_xor_b32 s0, s1, s0
	s_delay_alu instid0(SALU_CYCLE_1)
	v_cvt_f32_u32_e32 v1, s0
	global_load_b96 v[4:6], v[7:8], off
	s_sub_i32 s1, 0, s0
	v_rcp_iflag_f32_e32 v1, v1
	s_waitcnt_depctr 0xfff
	v_mul_f32_e32 v3, 0x4f7ffffe, v1
	v_mad_u64_u32 v[1:2], null, v0, 3, s[4:5]
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_cvt_u32_f32_e32 v2, v3
	v_ashrrev_i32_e32 v10, 31, v1
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_1) | instid1(VALU_DEP_3)
	v_mul_lo_u32 v9, s1, v2
	v_add_nc_u32_e32 v3, v1, v0
	v_add_nc_u32_e32 v1, v1, v10
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_4)
	v_ashrrev_i32_e32 v11, 31, v3
	v_mul_hi_u32 v9, v2, v9
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_xor_b32_e32 v1, v1, v10
	v_add_nc_u32_e32 v2, v2, v9
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mul_hi_u32 v9, v1, v2
	v_mul_lo_u32 v9, v9, s0
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_sub_nc_u32_e32 v1, v1, v9
	v_cmp_le_u32_e32 vcc_lo, s0, v1
	v_add_nc_u32_e32 v0, v3, v0
	v_add_nc_u32_e32 v3, v3, v11
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_ashrrev_i32_e32 v12, 31, v0
	v_xor_b32_e32 v3, v3, v11
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_nc_u32_e32 v0, v0, v12
	v_mul_hi_u32 v13, v3, v2
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_xor_b32_e32 v0, v0, v12
	v_mul_lo_u32 v13, v13, s0
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_mul_hi_u32 v2, v0, v2
	v_sub_nc_u32_e32 v3, v3, v13
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_mul_lo_u32 v2, v2, s0
	v_subrev_nc_u32_e32 v9, s0, v3
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_sub_nc_u32_e32 v0, v0, v2
	v_subrev_nc_u32_e32 v2, s0, v1
	v_subrev_nc_u32_e32 v13, s0, v0
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_3) | instid1(VALU_DEP_4)
	v_cndmask_b32_e32 v1, v1, v2, vcc_lo
	v_cmp_le_u32_e32 vcc_lo, s0, v3
	v_cndmask_b32_e32 v2, v3, v9, vcc_lo
	v_cmp_le_u32_e32 vcc_lo, s0, v0
	v_subrev_nc_u32_e32 v3, s0, v1
	s_delay_alu instid0(VALU_DEP_3) | instskip(SKIP_2) | instid1(VALU_DEP_2)
	v_subrev_nc_u32_e32 v9, s0, v2
	v_cndmask_b32_e32 v0, v0, v13, vcc_lo
	v_cmp_le_u32_e32 vcc_lo, s0, v1
	v_subrev_nc_u32_e32 v13, s0, v0
	v_cndmask_b32_e32 v1, v1, v3, vcc_lo
	v_cmp_le_u32_e32 vcc_lo, s0, v2
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_2) | instid1(VALU_DEP_3)
	v_xor_b32_e32 v1, v1, v10
	v_cndmask_b32_e32 v2, v2, v9, vcc_lo
	v_cmp_le_u32_e32 vcc_lo, s0, v0
	v_sub_nc_u32_e32 v1, v1, v10
	s_delay_alu instid0(VALU_DEP_3) | instskip(SKIP_1) | instid1(VALU_DEP_3)
	v_xor_b32_e32 v2, v2, v11
	v_cndmask_b32_e32 v0, v0, v13, vcc_lo
	v_cvt_f32_i32_e32 v3, v1
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_sub_nc_u32_e32 v2, v2, v11
	v_xor_b32_e32 v0, v0, v12
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_cvt_f32_i32_e32 v9, v2
	v_sub_nc_u32_e32 v0, v0, v12
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_3)
	v_cvt_f32_i32_e32 v10, v0
	s_waitcnt vmcnt(0)
	v_dual_add_f32 v0, v4, v3 :: v_dual_add_f32 v1, v5, v9
	s_delay_alu instid0(VALU_DEP_2)
	v_add_f32_e32 v2, v6, v10
	s_clause 0x1
	global_store_b64 v[7:8], v[9:10], off offset:16
	global_store_b128 v[7:8], v[0:3], off
.LBB0_2:
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z8timeStepP8particleiiiii
		.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 14
		.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	_Z8timeStepP8particleiiiii, .Lfunc_end0-_Z8timeStepP8particleiiiii
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 528
; NumSgprs: 18
; NumVgprs: 14
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 1
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 14
; Occupancy: 16
; WaveLimiterHint : 0
; COMPUTE_PGM_RSRC2:SCRATCH_EN: 0
; COMPUTE_PGM_RSRC2:USER_SGPR: 15
; COMPUTE_PGM_RSRC2:TRAP_HANDLER: 0
; COMPUTE_PGM_RSRC2:TGID_X_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Y_EN: 0
; COMPUTE_PGM_RSRC2:TGID_Z_EN: 0
; COMPUTE_PGM_RSRC2:TIDIG_COMP_CNT: 0
	.text
	.p2alignl 7, 3214868480
	.fill 96, 4, 3214868480
	.type	__hip_cuid_,@object             ; @__hip_cuid_
	.section	.bss,"aw",@nobits
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               ; 0x0
	.size	__hip_cuid_, 1

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym __hip_cuid_
	.amdgpu_metadata
---
amdhsa.kernels:
  - .args:
      - .address_space:  global
        .offset:         0
        .size:           8
        .value_kind:     global_buffer
      - .offset:         8
        .size:           4
        .value_kind:     by_value
      - .offset:         12
        .size:           4
        .value_kind:     by_value
      - .offset:         16
        .size:           4
        .value_kind:     by_value
      - .offset:         20
        .size:           4
        .value_kind:     by_value
      - .offset:         24
        .size:           4
        .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:           _Z8timeStepP8particleiiiii
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z8timeStepP8particleiiiii.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     14
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
