/* Pi - CUDA version 1 - uses integers for CUDA kernels
 * Author: Aaron Weeden, Shodor, May 2015
 */
#include <stdio.h> /* fprintf() */
#include <iostream>
#include <float.h> /* DBL_EPSILON() */
#include <math.h> /* sqrt() */

#if OPENMP_ENABLED
    #include <omp.h>
#endif

#define nthreads 1000

#if CUDA_ENABLED
    #define NUMBLOCKS(n) ceil(n/nthreads)
    #define KERNEL(n) <<<NUMBLOCKS(n), nthreads>>>
#else
    #define KERNEL(n)
#endif

#if CUDA_ENABLED
__global__ 
#endif
void calculateAreas(const int numRects, const double width, double *dev_areas) 
{
#if CUDA_ENABLED
    int threadId = threadIdx.x + (blockIdx.x * blockDim.x);
    if(threadId >= numRects)
    {
        return;
    }
#elif OPENMP_ENABLED
    #pragma omp parallel 
#endif

#if !CUDA_ENABLED
    for(int threadId = 0;threadId < numRects;threadId++)
#endif

    {
        double x = threadId * width;
        double heightSq = 1 - (x*x);
        double height = (heightSq < DBL_EPSILON) ? (0.0) : (sqrt(heightSq));
        dev_areas[threadId] = (width * height);   
    }
}

void calculateArea(const int numRects, double *area) {

    /* Allocate areas in host */
    double *areas = (double*)malloc(numRects * sizeof(double));
    double *dev_areas;
    int i = 0;
    cudaError_t err;

    /* Check for error in allocation*/
    if (areas == NULL) 
    {
        fprintf(stderr, "malloc failed!\n");
    }

    /* Allocate areas in device */
    err = cudaMalloc((void**)&dev_areas, (numRects * sizeof(double)));

    /* Check for error in allocation in device*/
    if (err != cudaSuccess) 
    {
        fprintf(stderr, "cudaMalloc failed: %s\n", cudaGetErrorString(err));
    }

#if CUDA_ENABLED
    calculateAreas KERNEL(numRects) (numRects, (1.0 / numRects), dev_areas);
    err = cudaMemcpy(areas, dev_areas, (numRects * sizeof(double)), cudaMemcpyDeviceToHost);
    if (err != cudaSuccess) 
    {
        fprintf(stderr, "cudaMalloc failed: %s\n", cudaGetErrorString(err));
    }
#else 
    calculateAreas KERNEL(numRects) (numRects, (1.0 / numRects), areas);
#endif

    (*area) = 0.0;
    for (i = 0; i < numRects; i++) 
    {
        (*area) += areas[i];
    }
    cudaFree(dev_areas);
    free(areas);
}

	.file	"tmpxft_0021314f_00000000-6_pi-cuda-1.cudafe1.cpp"
	.text
#APP
	.globl _ZSt21ios_base_library_initv

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB3639:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE3639:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z14calculateAreasidPd
	.type	_Z14calculateAreasidPd, @function
_Z14calculateAreasidPd:
.LFB3635:
	.cfi_startproc
	endbr64
	pushq	%r12
	.cfi_def_cfa_offset 16
	.cfi_offset 12, -16
	movaps	%xmm0, %xmm2
	movq	%rsi, %r12
	pushq	%rbp
	.cfi_def_cfa_offset 24
	.cfi_offset 6, -24
	movl	%edi, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset 3, -32
	xorl	%ebx, %ebx
	subq	$16, %rsp
	.cfi_def_cfa_offset 48
.L3:
	cmpl	%ebx, %ebp
	jle	.L9
	cvtsi2sdl	%ebx, %xmm1
	movsd	.LC1(%rip), %xmm3
	xorps	%xmm0, %xmm0
	mulsd	%xmm2, %xmm1
	mulsd	%xmm1, %xmm1
	subsd	%xmm1, %xmm3
	movsd	.LC2(%rip), %xmm1
	comisd	%xmm3, %xmm1
	ja	.L4
	movaps	%xmm3, %xmm0
	movsd	%xmm2, 8(%rsp)
	call	sqrt@PLT
	movsd	8(%rsp), %xmm2
.L4:
	mulsd	%xmm2, %xmm0
	movsd	%xmm0, (%r12,%rbx,8)
	incq	%rbx
	jmp	.L3
.L9:
	addq	$16, %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
.LFE3635:
	.size	_Z14calculateAreasidPd, .-_Z14calculateAreasidPd
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC3:
	.string	"malloc failed!\n"
.LC4:
	.string	"cudaMalloc failed: %s\n"
	.text
	.globl	_Z13calculateAreaiPd
	.type	_Z13calculateAreaiPd, @function
_Z13calculateAreaiPd:
.LFB3636:
	.cfi_startproc
	endbr64
	pushq	%r13
	.cfi_def_cfa_offset 16
	.cfi_offset 13, -16
	movslq	%edi, %r13
	pushq	%r12
	.cfi_def_cfa_offset 24
	.cfi_offset 12, -24
	movq	%rsi, %r12
	pushq	%rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	movq	%r13, %rbp
	salq	$3, %r13
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
	movq	%r13, %rdi
	subq	$24, %rsp
	.cfi_def_cfa_offset 64
	movq	%fs:40, %rax
	movq	%rax, 8(%rsp)
	xorl	%eax, %eax
	call	malloc@PLT
	movq	%rax, %rbx
	testq	%rax, %rax
	jne	.L11
	movq	stderr(%rip), %rdi
	leaq	.LC3(%rip), %rdx
	movl	$2, %esi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
.L11:
	movq	%rsp, %rdi
	movq	%r13, %rsi
	call	cudaMalloc@PLT
	movl	%eax, %edi
	testl	%eax, %eax
	je	.L12
	call	cudaGetErrorString@PLT
	movq	stderr(%rip), %rdi
	movl	$2, %esi
	leaq	.LC4(%rip), %rdx
	movq	%rax, %rcx
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
.L12:
	cvtsi2sdl	%ebp, %xmm1
	movsd	.LC1(%rip), %xmm0
	movq	%rbx, %rsi
	movl	%ebp, %edi
	divsd	%xmm1, %xmm0
	call	_Z14calculateAreasidPd
	xorl	%eax, %eax
	xorps	%xmm0, %xmm0
.L13:
	cmpl	%eax, %ebp
	jle	.L21
	addsd	(%rbx,%rax,8), %xmm0
	incq	%rax
	jmp	.L13
.L21:
	movq	(%rsp), %rdi
	movsd	%xmm0, (%r12)
	call	cudaFree@PLT
	movq	8(%rsp), %rax
	subq	%fs:40, %rax
	je	.L15
	call	__stack_chk_fail@PLT
.L15:
	addq	$24, %rsp
	.cfi_def_cfa_offset 40
	movq	%rbx, %rdi
	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
	jmp	free@PLT
	.cfi_endproc
.LFE3636:
	.size	_Z13calculateAreaiPd, .-_Z13calculateAreaiPd
	.section	.text.startup,"ax",@progbits
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB3662:
	.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
.LFE3662:
	.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
.LC1:
	.long	0
	.long	1072693248
	.align 8
.LC2:
	.long	0
	.long	1018167296
	.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>
void calculateArea(const int numRects, double *area);


	.text
	.file	"pi-cuda-1.hip"
	.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
