/*
 * This program uses the host CURAND API to generate 100 
 * pseudorandom floats.
 */
 #include <stdio.h>
 #include <stdlib.h>
 #include <cuda.h>
 #include <curand.h>
 #include <time.h>
 #include <sys/time.h>
 
 #define CUDA_CALL(x) do { if((x)!=cudaSuccess) { \
     printf("Error at %s:%d\n",__FILE__,__LINE__);\
     return EXIT_FAILURE;}} while(0)
 #define CURAND_CALL(x) do { if((x)!=CURAND_STATUS_SUCCESS) { \
     printf("Error at %s:%d\n",__FILE__,__LINE__);\
     return EXIT_FAILURE;}} while(0)
 
 int main(int argc, char *argv[])
 {

    struct timeval start_h2d, end_h2d, start_d2h, end_d2h;


     int n;
     n = 256 * atof(argv[1]);//256 floats per 1 KB
     size_t i;
     curandGenerator_t gen;
     float *devData, *hostData;
 
     /* Allocate n floats on host */
     hostData = (float *)calloc(n, sizeof(float));
 
     /* Allocate n floats on device */
     CUDA_CALL(cudaMalloc((void **)&devData, n*sizeof(float)));
 
     /* Create pseudo-random number generator */
     CURAND_CALL(curandCreateGenerator(&gen, 
                 CURAND_RNG_PSEUDO_DEFAULT));
     
     /* Set seed */
     CURAND_CALL(curandSetPseudoRandomGeneratorSeed(gen, 
                 1234ULL));
 
     /* Generate n floats on device */
     CURAND_CALL(curandGenerateUniform(gen, devData, n));
 
     /* Copy device memory to host */
     CUDA_CALL(cudaMemcpy(hostData, devData, n * sizeof(float),
         cudaMemcpyDeviceToHost));
 
     /* Show result 
     for(i = 0; i < n; i++) {
         printf("%1.4f ", hostData[i]);
     }
     printf("\n");
    */

    float* rando = (float*)malloc(n*sizeof(float));
    float* d_rand;
    cudaMalloc((void **)&d_rand,sizeof(float)*n);


    gettimeofday(&start_h2d, NULL);
        cudaMemcpy(d_rand, rando,sizeof(float)*n,cudaMemcpyHostToDevice);
    gettimeofday(&end_h2d, NULL);


    gettimeofday(&start_d2h, NULL);
        cudaMemcpy(rando, d_rand,sizeof(float)*n,cudaMemcpyDeviceToHost);
    gettimeofday(&end_d2h, NULL);

    printf("%ld, %ld\n", ((end_h2d.tv_sec * 1000000 + end_h2d.tv_usec) - (start_h2d.tv_sec * 1000000 + start_h2d.tv_usec)),((end_d2h.tv_sec * 1000000 + end_d2h.tv_usec) - (start_d2h.tv_sec * 1000000 + start_d2h.tv_usec)));
    
     /* Cleanup */
     CURAND_CALL(curandDestroyGenerator(gen));
     CUDA_CALL(cudaFree(devData));
     free(hostData);    
     return EXIT_SUCCESS;
 }
	.file	"tmpxft_00321b84_00000000-6_kilo.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
.LC1:
	.string	"/home/ubuntu/Datasets/stackv2/train-structured/Robert-M-Hughes/High-Preformance-Computing/master/Project05/Project05GPU/kilo.cu"
.LC2:
	.string	"Error at %s:%d\n"
.LC3:
	.string	"%ld, %ld\n"
	.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
	pushq	%rbp
	.cfi_def_cfa_offset 24
	.cfi_offset 6, -24
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset 3, -32
	subq	$96, %rsp
	.cfi_def_cfa_offset 128
	movq	8(%rsi), %rdi
	movq	%fs:40, %rax
	movq	%rax, 88(%rsp)
	xorl	%eax, %eax
	call	atof@PLT
	mulsd	.LC0(%rip), %xmm0
	movl	$4, %esi
	cvttsd2sil	%xmm0, %r12d
	movslq	%r12d, %r12
	movq	%r12, %rdi
	leaq	0(,%r12,4), %rbx
	call	calloc@PLT
	leaq	8(%rsp), %rdi
	movq	%rbx, %rsi
	movq	%rax, %rbp
	call	cudaMalloc@PLT
	movl	$35, %ecx
	testl	%eax, %eax
	jne	.L14
	movq	%rsp, %rdi
	movl	$100, %esi
	call	curandCreateGenerator@PLT
	testl	%eax, %eax
	je	.L5
	movl	$38, %ecx
.L14:
	leaq	.LC1(%rip), %rdx
	leaq	.LC2(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	$1, %eax
	jmp	.L2
.L5:
	movq	(%rsp), %rdi
	movl	$1234, %esi
	call	curandSetPseudoRandomGeneratorSeed@PLT
	movl	$42, %ecx
	testl	%eax, %eax
	jne	.L14
	movq	8(%rsp), %rsi
	movq	(%rsp), %rdi
	movq	%r12, %rdx
	call	curandGenerateUniform@PLT
	movl	$46, %ecx
	testl	%eax, %eax
	jne	.L14
	movq	8(%rsp), %rsi
	movl	$2, %ecx
	movq	%rbx, %rdx
	movq	%rbp, %rdi
	call	cudaMemcpy@PLT
	movl	$49, %ecx
	testl	%eax, %eax
	jne	.L14
	movq	%rbx, %rdi
	call	malloc@PLT
	leaq	16(%rsp), %rdi
	movq	%rbx, %rsi
	movq	%rax, %r12
	call	cudaMalloc@PLT
	leaq	24(%rsp), %rdi
	xorl	%esi, %esi
	call	gettimeofday@PLT
	movq	16(%rsp), %rdi
	movq	%rbx, %rdx
	movq	%r12, %rsi
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	leaq	40(%rsp), %rdi
	xorl	%esi, %esi
	call	gettimeofday@PLT
	leaq	56(%rsp), %rdi
	xorl	%esi, %esi
	call	gettimeofday@PLT
	movq	16(%rsp), %rsi
	movq	%rbx, %rdx
	movq	%r12, %rdi
	movl	$2, %ecx
	call	cudaMemcpy@PLT
	leaq	72(%rsp), %rdi
	xorl	%esi, %esi
	call	gettimeofday@PLT
	leaq	.LC3(%rip), %rsi
	movl	$2, %edi
	imulq	$1000000, 72(%rsp), %rcx
	addq	80(%rsp), %rcx
	imulq	$1000000, 56(%rsp), %rax
	addq	64(%rsp), %rax
	imulq	$1000000, 40(%rsp), %rdx
	subq	%rax, %rcx
	addq	48(%rsp), %rdx
	imulq	$1000000, 24(%rsp), %rax
	addq	32(%rsp), %rax
	subq	%rax, %rdx
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movq	(%rsp), %rdi
	call	curandDestroyGenerator@PLT
	movl	$76, %ecx
	testl	%eax, %eax
	jne	.L14
	movq	8(%rsp), %rdi
	call	cudaFree@PLT
	movl	$77, %ecx
	testl	%eax, %eax
	jne	.L14
	movq	%rbp, %rdi
	call	free@PLT
	xorl	%eax, %eax
.L2:
	movq	88(%rsp), %rdx
	subq	%fs:40, %rdx
	je	.L12
	call	__stack_chk_fail@PLT
.L12:
	addq	$96, %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
	.section	.rodata.cst8,"aM",@progbits,8
	.align 8
.LC0:
	.long	0
	.long	1081081856
	.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:

/*
 * This program uses the host CURAND API to generate 100 
 * pseudorandom floats.
 */
 #include <stdio.h>
 #include <stdlib.h>
 #include <hip/hip_runtime.h>
 #include <hiprand/hiprand.h>
 #include <time.h>
 #include <sys/time.h>
 
 #define CUDA_CALL(x) do { if((x)!=hipSuccess) { \
     printf("Error at %s:%d\n",__FILE__,__LINE__);\
     return EXIT_FAILURE;}} while(0)
 #define CURAND_CALL(x) do { if((x)!=HIPRAND_STATUS_SUCCESS) { \
     printf("Error at %s:%d\n",__FILE__,__LINE__);\
     return EXIT_FAILURE;}} while(0)
 
 int main(int argc, char *argv[])
 {

    struct timeval start_h2d, end_h2d, start_d2h, end_d2h;


     int n;
     n = 256 * atof(argv[1]);//256 floats per 1 KB
     size_t i;
     hiprandGenerator_t gen;
     float *devData, *hostData;
 
     /* Allocate n floats on host */
     hostData = (float *)calloc(n, sizeof(float));
 
     /* Allocate n floats on device */
     CUDA_CALL(hipMalloc((void **)&devData, n*sizeof(float)));
 
     /* Create pseudo-random number generator */
     CURAND_CALL(hiprandCreateGenerator(&gen, 
                 HIPRAND_RNG_PSEUDO_DEFAULT));
     
     /* Set seed */
     CURAND_CALL(hiprandSetPseudoRandomGeneratorSeed(gen, 
                 1234ULL));
 
     /* Generate n floats on device */
     CURAND_CALL(hiprandGenerateUniform(gen, devData, n));
 
     /* Copy device memory to host */
     CUDA_CALL(hipMemcpy(hostData, devData, n * sizeof(float),
         hipMemcpyDeviceToHost));
 
     /* Show result 
     for(i = 0; i < n; i++) {
         printf("%1.4f ", hostData[i]);
     }
     printf("\n");
    */

    float* rando = (float*)malloc(n*sizeof(float));
    float* d_rand;
    hipMalloc((void **)&d_rand,sizeof(float)*n);


    gettimeofday(&start_h2d, NULL);
        hipMemcpy(d_rand, rando,sizeof(float)*n,hipMemcpyHostToDevice);
    gettimeofday(&end_h2d, NULL);


    gettimeofday(&start_d2h, NULL);
        hipMemcpy(rando, d_rand,sizeof(float)*n,hipMemcpyDeviceToHost);
    gettimeofday(&end_d2h, NULL);

    printf("%ld, %ld\n", ((end_h2d.tv_sec * 1000000 + end_h2d.tv_usec) - (start_h2d.tv_sec * 1000000 + start_h2d.tv_usec)),((end_d2h.tv_sec * 1000000 + end_d2h.tv_usec) - (start_d2h.tv_sec * 1000000 + start_d2h.tv_usec)));
    
     /* Cleanup */
     CURAND_CALL(hiprandDestroyGenerator(gen));
     CUDA_CALL(hipFree(devData));
     free(hostData);    
     return EXIT_SUCCESS;
 }
	.text
	.file	"kilo.hip"
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function main
.LCPI0_0:
	.quad	0x4070000000000000              # double 256
	.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	$88, %rsp
	.cfi_def_cfa_offset 144
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movq	8(%rsi), %rdi
	callq	atof
	mulsd	.LCPI0_0(%rip), %xmm0
	cvttsd2si	%xmm0, %eax
	movslq	%eax, %r15
	movl	$4, %esi
	movq	%r15, %rdi
	callq	calloc
	movq	%rax, %rbx
	leaq	(,%r15,4), %r14
	movq	%rsp, %rdi
	movq	%r14, %rsi
	callq	hipMalloc
	testl	%eax, %eax
	je	.LBB0_3
# %bb.1:
	movl	$.L.str, %edi
	movl	$.L.str.1, %esi
	movl	$35, %edx
	jmp	.LBB0_2
.LBB0_3:
	leaq	8(%rsp), %rdi
	movl	$400, %esi                      # imm = 0x190
	callq	hiprandCreateGenerator
	testl	%eax, %eax
	je	.LBB0_5
# %bb.4:
	movl	$.L.str, %edi
	movl	$.L.str.1, %esi
	movl	$39, %edx
	jmp	.LBB0_2
.LBB0_5:
	movq	8(%rsp), %rdi
	movl	$1234, %esi                     # imm = 0x4D2
	callq	hiprandSetPseudoRandomGeneratorSeed
	testl	%eax, %eax
	je	.LBB0_7
# %bb.6:
	movl	$.L.str, %edi
	movl	$.L.str.1, %esi
	movl	$43, %edx
	jmp	.LBB0_2
.LBB0_7:
	movq	8(%rsp), %rdi
	movq	(%rsp), %rsi
	movq	%r15, %rdx
	callq	hiprandGenerateUniform
	testl	%eax, %eax
	je	.LBB0_9
# %bb.8:
	movl	$.L.str, %edi
	movl	$.L.str.1, %esi
	movl	$46, %edx
	jmp	.LBB0_2
.LBB0_9:
	movq	(%rsp), %rsi
	movq	%rbx, %rdi
	movq	%r14, %rdx
	movl	$2, %ecx
	callq	hipMemcpy
	testl	%eax, %eax
	je	.LBB0_11
# %bb.10:
	movl	$.L.str, %edi
	movl	$.L.str.1, %esi
	movl	$50, %edx
.LBB0_2:
	xorl	%eax, %eax
	callq	printf
	movl	$1, %ebp
.LBB0_17:
	movl	%ebp, %eax
	addq	$88, %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
.LBB0_11:
	.cfi_def_cfa_offset 144
	movq	%r14, %rdi
	callq	malloc
	movq	%rax, %r12
	leaq	16(%rsp), %r13
	movq	%r13, %rdi
	movq	%r14, %rsi
	callq	hipMalloc
	leaq	40(%rsp), %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	movq	(%r13), %rdi
	movl	$1, %ebp
	movq	%r12, %rsi
	movq	%r14, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	leaq	72(%rsp), %r15
	movq	%r15, %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	leaq	24(%rsp), %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	movq	(%r13), %rsi
	movq	%r12, %rdi
	movq	%r14, %rdx
	movl	$2, %ecx
	callq	hipMemcpy
	leaq	56(%rsp), %r14
	movq	%r14, %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	movq	(%r15), %rax
	movq	8(%r15), %rsi
	leaq	40(%rsp), %rcx
	subq	(%rcx), %rax
	imulq	$1000000, %rax, %rax            # imm = 0xF4240
	subq	8(%rcx), %rsi
	addq	%rax, %rsi
	movq	(%r14), %rax
	movq	8(%r14), %rdx
	leaq	24(%rsp), %rcx
	subq	(%rcx), %rax
	imulq	$1000000, %rax, %rax            # imm = 0xF4240
	subq	8(%rcx), %rdx
	addq	%rax, %rdx
	movl	$.L.str.2, %edi
	xorl	%eax, %eax
	callq	printf
	movq	8(%rsp), %rdi
	callq	hiprandDestroyGenerator
	testl	%eax, %eax
	je	.LBB0_14
# %bb.12:
	movl	$.L.str, %edi
	movl	$.L.str.1, %esi
	movl	$76, %edx
	jmp	.LBB0_13
.LBB0_14:
	movq	(%rsp), %rdi
	callq	hipFree
	testl	%eax, %eax
	je	.LBB0_16
# %bb.15:
	movl	$.L.str, %edi
	movl	$.L.str.1, %esi
	movl	$77, %edx
.LBB0_13:
	xorl	%eax, %eax
	callq	printf
	jmp	.LBB0_17
.LBB0_16:
	movq	%rbx, %rdi
	callq	free
	xorl	%ebp, %ebp
	jmp	.LBB0_17
.Lfunc_end0:
	.size	main, .Lfunc_end0-main
	.cfi_endproc
                                        # -- End function
	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"Error at %s:%d\n"
	.size	.L.str, 16

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"/home/ubuntu/Datasets/stackv2/train-structured-repos-hip-sm89/Robert-M-Hughes/High-Preformance-Computing/master/Project05/Project05GPU/kilo.hip"
	.size	.L.str.1, 144

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"%ld, %ld\n"
	.size	.L.str.2, 10

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

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym __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
