#include <stdio.h>
#include <stdlib.h>
#include <math.h>

void Usage(char *prog_name);

#define ACCURACY 0.01

typedef float float_type;

#define KERNEL_ITERATIONS 1000

__device__ void warp_reduce(volatile float_type *sdata, const unsigned int thread_id)
{
    sdata[thread_id] += sdata[thread_id + 32];
    sdata[thread_id] += sdata[thread_id + 16];
    sdata[thread_id] += sdata[thread_id + 8];
    sdata[thread_id] += sdata[thread_id + 4];
    sdata[thread_id] += sdata[thread_id + 2];
    sdata[thread_id] += sdata[thread_id + 1];
}

__global__ void reduce_pi(float_type *gdata)
{
    extern __shared__ float_type sdata[];

    const unsigned int thread_id = threadIdx.x;
    const unsigned long long int i = (((unsigned long long int)blockIdx.x) * blockDim.x + threadIdx.x) * KERNEL_ITERATIONS;

    float_type current_thread_factor = 0.0f;
    for (int it = 0; it < KERNEL_ITERATIONS; it++)
    {
        const float factor = ((i + it) & 1) ? -1.0f : 1.0f;
        current_thread_factor += factor / (((i + it) << 1) + 1);
    }

    sdata[thread_id] = current_thread_factor;

    __syncthreads();

    // reduction in shared memory
    for (unsigned int stride = blockDim.x >> 1; stride > 32; stride >>= 1)
    {
        if (thread_id < stride) {
            sdata[thread_id] += sdata[thread_id + stride];
        }
        __syncthreads();
    }

    if (thread_id < 32)
        warp_reduce(sdata, thread_id);

    // write result for this block to global memory
    if (thread_id == 0)
        gdata[blockIdx.x] = sdata[0];
}

double sequential_solution(int argc, char *argv[])
{
    long long n, i;
    double factor = 0.0;
    double sum = 0.0;

    if (argc != 2)
        Usage(argv[0]);
    n = strtoll(argv[1], NULL, 10);
    if (n < 1)
        Usage(argv[0]);

    printf("Before for loop, factor = %f.\n", factor);
    for (i = 0; i < n; i++)
    {
        factor = (i % 2 == 0) ? 1.0 : -1.0;
        sum += factor / (2 * i + 1);
    }
    printf("After for loop, factor = %f.\n", factor);

    sum = 4.0 * sum;
    printf("With n = %lld terms\n", n);
    printf("   Our estimate of pi = %.14f\n", sum);
    printf("   Ref estimate of pi = %.14f\n", 4.0 * atan(1.0));
    return sum;
}

double parallel_solution(int argc, char *argv[])
{
    long long n, i;
    double factor = 0.0;
    double sum = 0.0;
    float_type *dev_sum;
    float_type *cpu_sum;

    if (argc != 2)
        Usage(argv[0]);
    n = strtoll(argv[1], NULL, 10);
    if (n < 1)
        Usage(argv[0]);

    printf("Before for loop, factor = %f.\n", factor);

    long long block_size = 1024;
    long long grid_size = ceil(((double)n / KERNEL_ITERATIONS) / block_size);

    cpu_sum = (float_type*)calloc(grid_size, sizeof(float_type));
    cudaMalloc(&dev_sum, grid_size * sizeof(float_type));

    reduce_pi<<< grid_size, block_size, block_size * sizeof(float_type) >>>(dev_sum);

    cudaMemcpy(cpu_sum, dev_sum, grid_size * sizeof(float_type), cudaMemcpyDeviceToHost);

    factor = ((n - 1) % 2 == 0) ? 1.0 : -1.0;
    for (i = 0; i < grid_size; i++)
        sum += cpu_sum[i];

    cudaFree(dev_sum);
    free(cpu_sum);

    printf("After for loop, factor = %f.\n", factor);

    sum = 4.0 * sum;
    printf("With n = %lld terms\n", n);
    printf("   Our estimate of pi = %.14f\n", sum);
    printf("   Ref estimate of pi = %.14f\n", 4.0 * atan(1.0));
    return sum;
}

int main(int argc, char *argv[])
{
    float elapsed_time_seq;
    cudaEvent_t start_time_seq, end_time_seq;
    cudaEventCreate(&start_time_seq);
    cudaEventCreate(&end_time_seq);
    float elapsed_time_parallel;
    cudaEvent_t start_time_parallel, end_time_parallel;
    cudaEventCreate(&start_time_parallel);
    cudaEventCreate(&end_time_parallel);

    printf("---------------------Sequential execution---------------------\n");
    cudaEventRecord(start_time_seq, 0);
    double sum_seq = sequential_solution(argc, argv);
    cudaEventRecord(end_time_seq, 0);
    cudaEventSynchronize(end_time_seq);
    cudaEventElapsedTime(&elapsed_time_seq, start_time_seq, end_time_seq);

    printf("----------------------Parallel execution----------------------\n");
    cudaEventRecord(start_time_parallel, 0);
    double sum_parallel = parallel_solution(argc, argv);
    cudaEventRecord(end_time_parallel, 0);
    cudaEventSynchronize(end_time_parallel);
    cudaEventElapsedTime(&elapsed_time_parallel, start_time_parallel, end_time_parallel);

    printf("\nSequential elapsed time: %fs\n", elapsed_time_seq / 1000.0);
    printf("Parallel elapsed time: %fs\n", elapsed_time_parallel / 1000.0);

    if (fabs(sum_seq - sum_parallel) < ACCURACY)
        printf("Test PASSED\n");
    else
        printf("Test FAILED\n");

    return 0;
}

void Usage(char *prog_name)
{
    fprintf(stderr, "usage: %s <n>\n", prog_name);
    fprintf(stderr, "   n is the number of terms and should be >= 1\n");
    exit(0);
}

	.file	"tmpxft_00240587_00000000-6_dz4z1.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2034:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2034:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z11warp_reducePVfj
	.type	_Z11warp_reducePVfj, @function
_Z11warp_reducePVfj:
.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	_Z11warp_reducePVfj, .-_Z11warp_reducePVfj
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"usage: %s <n>\n"
.LC1:
	.string	"   n is the number of terms and should be >= 1\n"
	.text
	.globl	_Z5UsagePc
	.type	_Z5UsagePc, @function
_Z5UsagePc:
.LFB2031:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	popq	%rax
	.cfi_def_cfa_offset 8
	leaq	.LC0(%rip), %rdx
	movl	$2, %esi
	pushq	%rax
	.cfi_def_cfa_offset 16
	movq	%rdi, %rcx
	movq	stderr(%rip), %rdi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	movq	stderr(%rip), %rdi
	leaq	.LC1(%rip), %rdx
	xorl	%eax, %eax
	movl	$2, %esi
	call	__fprintf_chk@PLT
	xorl	%edi, %edi
	call	exit@PLT
	.cfi_endproc
.LFE2031:
	.size	_Z5UsagePc, .-_Z5UsagePc
	.section	.rodata.str1.1
.LC5:
	.string	"Before for loop, factor = %f.\n"
.LC6:
	.string	"After for loop, factor = %f.\n"
.LC8:
	.string	"With n = %lld terms\n"
.LC9:
	.string	"   Our estimate of pi = %.14f\n"
.LC11:
	.string	"   Ref estimate of pi = %.14f\n"
	.text
	.globl	_Z19sequential_solutioniPPc
	.type	_Z19sequential_solutioniPPc, @function
_Z19sequential_solutioniPPc:
.LFB2028:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsi, %rbp
	pushq	%rbx
	.cfi_def_cfa_offset 24
	.cfi_offset 3, -24
	subq	$24, %rsp
	.cfi_def_cfa_offset 48
	cmpl	$2, %edi
	jne	.L14
	movq	8(%rsi), %rdi
	movl	$10, %edx
	xorl	%esi, %esi
	call	__isoc23_strtoll@PLT
	movq	%rax, %rbx
	testq	%rax, %rax
	jg	.L8
.L14:
	movq	0(%rbp), %rdi
	call	_Z5UsagePc
.L8:
	xorps	%xmm0, %xmm0
	leaq	.LC5(%rip), %rsi
	movl	$2, %edi
	movb	$1, %al
	call	__printf_chk@PLT
	xorps	%xmm1, %xmm1
	xorl	%eax, %eax
.L10:
	movsd	.LC4(%rip), %xmm0
	testb	$1, %al
	jne	.L9
	movsd	.LC3(%rip), %xmm0
.L9:
	leaq	(%rax,%rax), %rdx
	movaps	%xmm0, %xmm2
	incq	%rax
	incq	%rdx
	cvtsi2sdq	%rdx, %xmm3
	divsd	%xmm3, %xmm2
	addsd	%xmm2, %xmm1
	cmpq	%rax, %rbx
	jne	.L10
	leaq	.LC6(%rip), %rsi
	movl	$2, %edi
	movb	$1, %al
	movsd	%xmm1, 8(%rsp)
	call	__printf_chk@PLT
	movsd	8(%rsp), %xmm1
	movq	%rbx, %rdx
	xorl	%eax, %eax
	mulsd	.LC7(%rip), %xmm1
	leaq	.LC8(%rip), %rsi
	movl	$2, %edi
	movq	%xmm1, %rbp
	call	__printf_chk@PLT
	movq	%rbp, %xmm0
	leaq	.LC9(%rip), %rsi
	movb	$1, %al
	movl	$2, %edi
	call	__printf_chk@PLT
	movsd	.LC10(%rip), %xmm0
	leaq	.LC11(%rip), %rsi
	movb	$1, %al
	movl	$2, %edi
	call	__printf_chk@PLT
	addq	$24, %rsp
	.cfi_def_cfa_offset 24
	movq	%rbp, %xmm0
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2028:
	.size	_Z19sequential_solutioniPPc, .-_Z19sequential_solutioniPPc
	.globl	_Z28__device_stub__Z9reduce_piPfPf
	.type	_Z28__device_stub__Z9reduce_piPfPf, @function
_Z28__device_stub__Z9reduce_piPfPf:
.LFB2056:
	.cfi_startproc
	endbr64
	subq	$104, %rsp
	.cfi_def_cfa_offset 112
	movq	%rdi, 8(%rsp)
	leaq	24(%rsp), %rcx
	leaq	16(%rsp), %rdx
	leaq	44(%rsp), %rsi
	leaq	32(%rsp), %rdi
	movq	%fs:40, %rax
	movq	%rax, 88(%rsp)
	xorl	%eax, %eax
	leaq	8(%rsp), %rax
	movl	$1, 40(%rsp)
	movq	%rax, 80(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 32(%rsp)
	movq	%rax, 44(%rsp)
	movl	$1, 52(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L16
	pushq	24(%rsp)
	.cfi_def_cfa_offset 120
	leaq	_Z9reduce_piPf(%rip), %rdi
	pushq	24(%rsp)
	.cfi_def_cfa_offset 128
	movq	60(%rsp), %rcx
	movl	68(%rsp), %r8d
	movq	48(%rsp), %rsi
	movl	56(%rsp), %edx
	leaq	96(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 120
	popq	%rdx
	.cfi_def_cfa_offset 112
.L16:
	movq	88(%rsp), %rax
	subq	%fs:40, %rax
	je	.L18
	call	__stack_chk_fail@PLT
.L18:
	addq	$104, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2056:
	.size	_Z28__device_stub__Z9reduce_piPfPf, .-_Z28__device_stub__Z9reduce_piPfPf
	.globl	_Z9reduce_piPf
	.type	_Z9reduce_piPf, @function
_Z9reduce_piPf:
.LFB2057:
	.cfi_startproc
	endbr64
	jmp	_Z28__device_stub__Z9reduce_piPfPf
	.cfi_endproc
.LFE2057:
	.size	_Z9reduce_piPf, .-_Z9reduce_piPf
	.globl	_Z17parallel_solutioniPPc
	.type	_Z17parallel_solutioniPPc, @function
_Z17parallel_solutioniPPc:
.LFB2029:
	.cfi_startproc
	endbr64
	pushq	%r13
	.cfi_def_cfa_offset 16
	.cfi_offset 13, -16
	pushq	%r12
	.cfi_def_cfa_offset 24
	.cfi_offset 12, -24
	pushq	%rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	movq	%rsi, %rbp
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
	subq	$72, %rsp
	.cfi_def_cfa_offset 112
	movq	%fs:40, %rax
	movq	%rax, 56(%rsp)
	xorl	%eax, %eax
	cmpl	$2, %edi
	jne	.L35
	movq	8(%rsi), %rdi
	movl	$10, %edx
	xorl	%esi, %esi
	call	__isoc23_strtoll@PLT
	movq	%rax, %rbx
	testq	%rax, %rax
	jg	.L24
.L35:
	movq	56(%rsp), %rax
	subq	%fs:40, %rax
	jne	.L34
	movq	0(%rbp), %rdi
	call	_Z5UsagePc
.L24:
	leaq	.LC5(%rip), %rsi
	movl	$2, %edi
	xorps	%xmm0, %xmm0
	movb	$1, %al
	call	__printf_chk@PLT
	cvtsi2sdq	%rbx, %xmm0
	divsd	.LC12(%rip), %xmm0
	mulsd	.LC13(%rip), %xmm0
	call	ceil@PLT
	movl	$4, %esi
	cvttsd2siq	%xmm0, %r12
	movq	%r12, %rdi
	leaq	0(,%r12,4), %r13
	call	calloc@PLT
	leaq	24(%rsp), %rdi
	movq	%r13, %rsi
	movq	%rax, %rbp
	call	cudaMalloc@PLT
	movl	%r12d, 32(%rsp)
	movl	$4194305, %edx
	movabsq	$4294967297, %rax
	movq	%rax, 36(%rsp)
	movq	32(%rsp), %rdi
	xorl	%r9d, %r9d
	salq	$10, %rdx
	movl	40(%rsp), %esi
	movl	$4096, %r8d
	movl	$1, %ecx
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L26
	movq	24(%rsp), %rdi
	call	_Z28__device_stub__Z9reduce_piPfPf
.L26:
	movq	24(%rsp), %rsi
	movl	$2, %ecx
	movq	%r13, %rdx
	movq	%rbp, %rdi
	call	cudaMemcpy@PLT
	movsd	.LC4(%rip), %xmm0
	testb	$1, %bl
	je	.L27
	movsd	.LC3(%rip), %xmm0
.L27:
	xorps	%xmm1, %xmm1
	xorl	%eax, %eax
.L28:
	cmpq	%r12, %rax
	jge	.L36
	cvtss2sd	0(%rbp,%rax,4), %xmm2
	incq	%rax
	addsd	%xmm2, %xmm1
	jmp	.L28
.L36:
	movq	24(%rsp), %rdi
	movsd	%xmm1, (%rsp)
	movsd	%xmm0, 8(%rsp)
	call	cudaFree@PLT
	movq	%rbp, %rdi
	call	free@PLT
	movsd	8(%rsp), %xmm0
	leaq	.LC6(%rip), %rsi
	movb	$1, %al
	movl	$2, %edi
	call	__printf_chk@PLT
	movsd	(%rsp), %xmm1
	movq	%rbx, %rdx
	xorl	%eax, %eax
	mulsd	.LC7(%rip), %xmm1
	leaq	.LC8(%rip), %rsi
	movl	$2, %edi
	movq	%xmm1, %rbp
	call	__printf_chk@PLT
	movq	%rbp, %xmm0
	leaq	.LC9(%rip), %rsi
	movb	$1, %al
	movl	$2, %edi
	call	__printf_chk@PLT
	movsd	.LC10(%rip), %xmm0
	leaq	.LC11(%rip), %rsi
	movb	$1, %al
	movl	$2, %edi
	call	__printf_chk@PLT
	movq	56(%rsp), %rax
	subq	%fs:40, %rax
	je	.L30
.L34:
	call	__stack_chk_fail@PLT
.L30:
	addq	$72, %rsp
	.cfi_def_cfa_offset 40
	movq	%rbp, %xmm0
	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	_Z17parallel_solutioniPPc, .-_Z17parallel_solutioniPPc
	.section	.rodata.str1.1
.LC14:
	.string	"---------------------Sequential execution---------------------\n"
.LC15:
	.string	"----------------------Parallel execution----------------------\n"
.LC16:
	.string	"\nSequential elapsed time: %fs\n"
.LC17:
	.string	"Parallel elapsed time: %fs\n"
.LC20:
	.string	"Test PASSED\n"
.LC21:
	.string	"Test FAILED\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2030:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsi, %rbp
	pushq	%rbx
	.cfi_def_cfa_offset 24
	.cfi_offset 3, -24
	movl	%edi, %ebx
	subq	$72, %rsp
	.cfi_def_cfa_offset 96
	movq	%fs:40, %rax
	movq	%rax, 56(%rsp)
	xorl	%eax, %eax
	leaq	24(%rsp), %rdi
	call	cudaEventCreate@PLT
	leaq	32(%rsp), %rdi
	call	cudaEventCreate@PLT
	leaq	40(%rsp), %rdi
	call	cudaEventCreate@PLT
	leaq	48(%rsp), %rdi
	call	cudaEventCreate@PLT
	leaq	.LC14(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movq	24(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movq	%rbp, %rsi
	movl	%ebx, %edi
	call	_Z19sequential_solutioniPPc
	movq	32(%rsp), %rdi
	xorl	%esi, %esi
	movsd	%xmm0, 8(%rsp)
	call	cudaEventRecord@PLT
	movq	32(%rsp), %rdi
	call	cudaEventSynchronize@PLT
	movq	32(%rsp), %rdx
	movq	24(%rsp), %rsi
	leaq	16(%rsp), %rdi
	call	cudaEventElapsedTime@PLT
	leaq	.LC15(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movq	40(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movq	%rbp, %rsi
	movl	%ebx, %edi
	call	_Z17parallel_solutioniPPc
	movq	48(%rsp), %rdi
	xorl	%esi, %esi
	movsd	%xmm0, (%rsp)
	call	cudaEventRecord@PLT
	movq	48(%rsp), %rdi
	call	cudaEventSynchronize@PLT
	movq	48(%rsp), %rdx
	movq	40(%rsp), %rsi
	leaq	20(%rsp), %rdi
	call	cudaEventElapsedTime@PLT
	leaq	.LC16(%rip), %rsi
	movl	$2, %edi
	movb	$1, %al
	cvtss2sd	16(%rsp), %xmm0
	divsd	.LC12(%rip), %xmm0
	call	__printf_chk@PLT
	movb	$1, %al
	leaq	.LC17(%rip), %rsi
	movl	$2, %edi
	cvtss2sd	20(%rsp), %xmm0
	divsd	.LC12(%rip), %xmm0
	call	__printf_chk@PLT
	movsd	8(%rsp), %xmm1
	subsd	(%rsp), %xmm1
	leaq	.LC20(%rip), %rsi
	movaps	%xmm1, %xmm0
	movsd	.LC19(%rip), %xmm1
	andps	.LC18(%rip), %xmm0
	comisd	%xmm0, %xmm1
	ja	.L45
	leaq	.LC21(%rip), %rsi
.L45:
	xorl	%eax, %eax
	movl	$2, %edi
	call	__printf_chk@PLT
	movq	56(%rsp), %rax
	subq	%fs:40, %rax
	je	.L41
	call	__stack_chk_fail@PLT
.L41:
	addq	$72, %rsp
	.cfi_def_cfa_offset 24
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2030:
	.size	main, .-main
	.section	.rodata.str1.1
.LC22:
	.string	"_Z9reduce_piPf"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2059:
	.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	.LC22(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z9reduce_piPf(%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
.LFE2059:
	.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
.LC3:
	.long	0
	.long	1072693248
	.align 8
.LC4:
	.long	0
	.long	-1074790400
	.align 8
.LC7:
	.long	0
	.long	1074790400
	.align 8
.LC10:
	.long	1413754136
	.long	1074340347
	.align 8
.LC12:
	.long	0
	.long	1083129856
	.align 8
.LC13:
	.long	0
	.long	1062207488
	.section	.rodata.cst16,"aM",@progbits,16
	.align 16
.LC18:
	.long	-1
	.long	2147483647
	.long	0
	.long	0
	.section	.rodata.cst8
	.align 8
.LC19:
	.long	1202590843
	.long	1065646817
	.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 <math.h>

void Usage(char *prog_name);

#define ACCURACY 0.01

typedef float float_type;

#define KERNEL_ITERATIONS 1000

__device__ void warp_reduce(volatile float_type *sdata, const unsigned int thread_id)
{
    sdata[thread_id] += sdata[thread_id + 32];
    sdata[thread_id] += sdata[thread_id + 16];
    sdata[thread_id] += sdata[thread_id + 8];
    sdata[thread_id] += sdata[thread_id + 4];
    sdata[thread_id] += sdata[thread_id + 2];
    sdata[thread_id] += sdata[thread_id + 1];
}

__global__ void reduce_pi(float_type *gdata)
{
    extern __shared__ float_type sdata[];

    const unsigned int thread_id = threadIdx.x;
    const unsigned long long int i = (((unsigned long long int)blockIdx.x) * blockDim.x + threadIdx.x) * KERNEL_ITERATIONS;

    float_type current_thread_factor = 0.0f;
    for (int it = 0; it < KERNEL_ITERATIONS; it++)
    {
        const float factor = ((i + it) & 1) ? -1.0f : 1.0f;
        current_thread_factor += factor / (((i + it) << 1) + 1);
    }

    sdata[thread_id] = current_thread_factor;

    __syncthreads();

    // reduction in shared memory
    for (unsigned int stride = blockDim.x >> 1; stride > 32; stride >>= 1)
    {
        if (thread_id < stride) {
            sdata[thread_id] += sdata[thread_id + stride];
        }
        __syncthreads();
    }

    if (thread_id < 32)
        warp_reduce(sdata, thread_id);

    // write result for this block to global memory
    if (thread_id == 0)
        gdata[blockIdx.x] = sdata[0];
}

double sequential_solution(int argc, char *argv[])
{
    long long n, i;
    double factor = 0.0;
    double sum = 0.0;

    if (argc != 2)
        Usage(argv[0]);
    n = strtoll(argv[1], NULL, 10);
    if (n < 1)
        Usage(argv[0]);

    printf("Before for loop, factor = %f.\n", factor);
    for (i = 0; i < n; i++)
    {
        factor = (i % 2 == 0) ? 1.0 : -1.0;
        sum += factor / (2 * i + 1);
    }
    printf("After for loop, factor = %f.\n", factor);

    sum = 4.0 * sum;
    printf("With n = %lld terms\n", n);
    printf("   Our estimate of pi = %.14f\n", sum);
    printf("   Ref estimate of pi = %.14f\n", 4.0 * atan(1.0));
    return sum;
}

double parallel_solution(int argc, char *argv[])
{
    long long n, i;
    double factor = 0.0;
    double sum = 0.0;
    float_type *dev_sum;
    float_type *cpu_sum;

    if (argc != 2)
        Usage(argv[0]);
    n = strtoll(argv[1], NULL, 10);
    if (n < 1)
        Usage(argv[0]);

    printf("Before for loop, factor = %f.\n", factor);

    long long block_size = 1024;
    long long grid_size = ceil(((double)n / KERNEL_ITERATIONS) / block_size);

    cpu_sum = (float_type*)calloc(grid_size, sizeof(float_type));
    hipMalloc(&dev_sum, grid_size * sizeof(float_type));

    reduce_pi<<< grid_size, block_size, block_size * sizeof(float_type) >>>(dev_sum);

    hipMemcpy(cpu_sum, dev_sum, grid_size * sizeof(float_type), hipMemcpyDeviceToHost);

    factor = ((n - 1) % 2 == 0) ? 1.0 : -1.0;
    for (i = 0; i < grid_size; i++)
        sum += cpu_sum[i];

    hipFree(dev_sum);
    free(cpu_sum);

    printf("After for loop, factor = %f.\n", factor);

    sum = 4.0 * sum;
    printf("With n = %lld terms\n", n);
    printf("   Our estimate of pi = %.14f\n", sum);
    printf("   Ref estimate of pi = %.14f\n", 4.0 * atan(1.0));
    return sum;
}

int main(int argc, char *argv[])
{
    float elapsed_time_seq;
    hipEvent_t start_time_seq, end_time_seq;
    hipEventCreate(&start_time_seq);
    hipEventCreate(&end_time_seq);
    float elapsed_time_parallel;
    hipEvent_t start_time_parallel, end_time_parallel;
    hipEventCreate(&start_time_parallel);
    hipEventCreate(&end_time_parallel);

    printf("---------------------Sequential execution---------------------\n");
    hipEventRecord(start_time_seq, 0);
    double sum_seq = sequential_solution(argc, argv);
    hipEventRecord(end_time_seq, 0);
    hipEventSynchronize(end_time_seq);
    hipEventElapsedTime(&elapsed_time_seq, start_time_seq, end_time_seq);

    printf("----------------------Parallel execution----------------------\n");
    hipEventRecord(start_time_parallel, 0);
    double sum_parallel = parallel_solution(argc, argv);
    hipEventRecord(end_time_parallel, 0);
    hipEventSynchronize(end_time_parallel);
    hipEventElapsedTime(&elapsed_time_parallel, start_time_parallel, end_time_parallel);

    printf("\nSequential elapsed time: %fs\n", elapsed_time_seq / 1000.0);
    printf("Parallel elapsed time: %fs\n", elapsed_time_parallel / 1000.0);

    if (fabs(sum_seq - sum_parallel) < ACCURACY)
        printf("Test PASSED\n");
    else
        printf("Test FAILED\n");

    return 0;
}

void Usage(char *prog_name)
{
    fprintf(stderr, "usage: %s <n>\n", prog_name);
    fprintf(stderr, "   n is the number of terms and should be >= 1\n");
    exit(0);
}

	.text
	.file	"dz4z1.hip"
	.globl	_Z24__device_stub__reduce_piPf  # -- Begin function _Z24__device_stub__reduce_piPf
	.type	_Z24__device_stub__reduce_piPf,@function
_Z24__device_stub__reduce_piPf:         # @_Z24__device_stub__reduce_piPf
	.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	$64, %rsp
	.cfi_def_cfa_offset 112
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	leaq	24(%rsp), %rax
	movq	%rdi, (%rax)
	movq	%rsp, %rbx
	movq	%rax, (%rbx)
	leaq	48(%rsp), %r14
	leaq	32(%rsp), %r15
	leaq	16(%rsp), %r12
	leaq	8(%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	$_Z9reduce_piPf, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$80, %rsp
	.cfi_adjust_cfa_offset -80
	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_end0:
	.size	_Z24__device_stub__reduce_piPf, .Lfunc_end0-_Z24__device_stub__reduce_piPf
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst16,"aM",@progbits,16
	.p2align	3, 0x0                          # -- Begin function _Z19sequential_solutioniPPc
.LCPI1_0:
	.quad	0xbff0000000000000              # double -1
	.quad	0x3ff0000000000000              # double 1
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0
.LCPI1_1:
	.quad	0x4010000000000000              # double 4
.LCPI1_2:
	.quad	0x400921fb54442d18              # double 3.1415926535897931
	.text
	.globl	_Z19sequential_solutioniPPc
	.type	_Z19sequential_solutioniPPc,@function
_Z19sequential_solutioniPPc:            # @_Z19sequential_solutioniPPc
	.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	%rsi, %r14
	cmpl	$2, %edi
	jne	.LBB1_5
# %bb.1:
	movq	8(%r14), %rdi
	xorl	%esi, %esi
	movl	$10, %edx
	callq	__isoc23_strtoll
	testq	%rax, %rax
	jle	.LBB1_5
# %bb.2:
	movq	%rax, %rbx
	movl	$.L.str, %edi
	xorpd	%xmm0, %xmm0
	movb	$1, %al
	callq	printf
	xorpd	%xmm3, %xmm3
	movl	$1, %eax
	xorl	%ecx, %ecx
.LBB1_3:                                # =>This Inner Loop Header: Depth=1
	xorl	%edx, %edx
	testb	$1, %cl
	sete	%dl
	movsd	.LCPI1_0(,%rdx,8), %xmm0        # xmm0 = mem[0],zero
	xorps	%xmm1, %xmm1
	cvtsi2sd	%rax, %xmm1
	movapd	%xmm0, %xmm2
	divsd	%xmm1, %xmm2
	addsd	%xmm2, %xmm3
	incq	%rcx
	addq	$2, %rax
	cmpq	%rcx, %rbx
	jne	.LBB1_3
# %bb.4:
	movl	$.L.str.1, %edi
	movb	$1, %al
	movsd	%xmm3, (%rsp)                   # 8-byte Spill
	callq	printf
	movsd	(%rsp), %xmm0                   # 8-byte Reload
                                        # xmm0 = mem[0],zero
	mulsd	.LCPI1_1(%rip), %xmm0
	movsd	%xmm0, (%rsp)                   # 8-byte Spill
	movl	$.L.str.2, %edi
	movq	%rbx, %rsi
	xorl	%eax, %eax
	callq	printf
	movl	$.L.str.3, %edi
	movsd	(%rsp), %xmm0                   # 8-byte Reload
                                        # xmm0 = mem[0],zero
	movb	$1, %al
	callq	printf
	movsd	.LCPI1_2(%rip), %xmm0           # xmm0 = mem[0],zero
	movl	$.L.str.4, %edi
	movb	$1, %al
	callq	printf
	movsd	(%rsp), %xmm0                   # 8-byte Reload
                                        # xmm0 = mem[0],zero
	addq	$8, %rsp
	.cfi_def_cfa_offset 24
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%r14
	.cfi_def_cfa_offset 8
	retq
.LBB1_5:
	.cfi_def_cfa_offset 32
	movq	(%r14), %rdi
	callq	_Z5UsagePc
.Lfunc_end1:
	.size	_Z19sequential_solutioniPPc, .Lfunc_end1-_Z19sequential_solutioniPPc
	.cfi_endproc
                                        # -- End function
	.globl	_Z5UsagePc                      # -- Begin function _Z5UsagePc
	.type	_Z5UsagePc,@function
_Z5UsagePc:                             # @_Z5UsagePc
	.cfi_startproc
# %bb.0:
	pushq	%rax
	.cfi_def_cfa_offset 16
	movq	%rdi, %rdx
	movq	stderr(%rip), %rdi
	movl	$.L.str.11, %esi
	xorl	%eax, %eax
	callq	fprintf
	movq	stderr(%rip), %rcx
	movl	$.L.str.12, %edi
	movl	$47, %esi
	movl	$1, %edx
	callq	fwrite@PLT
	xorl	%edi, %edi
	callq	exit
.Lfunc_end2:
	.size	_Z5UsagePc, .Lfunc_end2-_Z5UsagePc
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function _Z17parallel_solutioniPPc
.LCPI3_0:
	.quad	0x408f400000000000              # double 1000
.LCPI3_1:
	.quad	0x3f50000000000000              # double 9.765625E-4
.LCPI3_2:
	.quad	0x4010000000000000              # double 4
.LCPI3_4:
	.quad	0x400921fb54442d18              # double 3.1415926535897931
	.section	.rodata.cst16,"aM",@progbits,16
	.p2align	3, 0x0
.LCPI3_3:
	.quad	0x3ff0000000000000              # double 1
	.quad	0xbff0000000000000              # double -1
	.text
	.globl	_Z17parallel_solutioniPPc
	.type	_Z17parallel_solutioniPPc,@function
_Z17parallel_solutioniPPc:              # @_Z17parallel_solutioniPPc
	.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
	subq	$24, %rsp
	.cfi_def_cfa_offset 64
	.cfi_offset %rbx, -40
	.cfi_offset %r12, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movq	%rsi, %r14
	cmpl	$2, %edi
	jne	.LBB3_9
# %bb.1:
	movq	8(%r14), %rdi
	xorl	%esi, %esi
	movl	$10, %edx
	callq	__isoc23_strtoll
	testq	%rax, %rax
	jle	.LBB3_9
# %bb.2:
	movq	%rax, %rbx
	xorps	%xmm0, %xmm0
	movsd	%xmm0, (%rsp)                   # 8-byte Spill
	movl	$.L.str, %edi
	movb	$1, %al
	callq	printf
	xorps	%xmm0, %xmm0
	cvtsi2sd	%rbx, %xmm0
	divsd	.LCPI3_0(%rip), %xmm0
	mulsd	.LCPI3_1(%rip), %xmm0
	callq	ceil@PLT
	cvttsd2si	%xmm0, %r15
	movl	$4, %esi
	movq	%r15, %rdi
	callq	calloc
	movq	%rax, %r14
	leaq	(,%r15,4), %r12
	leaq	8(%rsp), %rdi
	movq	%r12, %rsi
	callq	hipMalloc
	movl	%r15d, %edi
	btsq	$32, %rdi
	movabsq	$4294967296, %rdx               # imm = 0x100000000
	orq	$1024, %rdx                     # imm = 0x400
	movl	$4096, %r8d                     # imm = 0x1000
	movl	$1, %esi
	movl	$1, %ecx
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB3_4
# %bb.3:
	movq	8(%rsp), %rdi
	callq	_Z24__device_stub__reduce_piPf
.LBB3_4:
	movq	8(%rsp), %rsi
	movq	%r14, %rdi
	movq	%r12, %rdx
	movl	$2, %ecx
	callq	hipMemcpy
	testq	%r15, %r15
	jle	.LBB3_8
# %bb.5:                                # %.lr.ph.preheader
	xorpd	%xmm1, %xmm1
	xorl	%eax, %eax
.LBB3_6:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	xorps	%xmm0, %xmm0
	cvtss2sd	(%r14,%rax,4), %xmm0
	addsd	%xmm0, %xmm1
	incq	%rax
	cmpq	%rax, %r15
	jne	.LBB3_6
# %bb.7:                                # %._crit_edge.loopexit
	mulsd	.LCPI3_2(%rip), %xmm1
	movsd	%xmm1, (%rsp)                   # 8-byte Spill
.LBB3_8:                                # %._crit_edge
	xorl	%eax, %eax
	testb	$1, %bl
	sete	%al
	movsd	.LCPI3_3(,%rax,8), %xmm0        # xmm0 = mem[0],zero
	movsd	%xmm0, 16(%rsp)                 # 8-byte Spill
	movq	8(%rsp), %rdi
	callq	hipFree
	movq	%r14, %rdi
	callq	free
	movl	$.L.str.1, %edi
	movsd	16(%rsp), %xmm0                 # 8-byte Reload
                                        # xmm0 = mem[0],zero
	movb	$1, %al
	callq	printf
	movl	$.L.str.2, %edi
	movq	%rbx, %rsi
	xorl	%eax, %eax
	callq	printf
	movl	$.L.str.3, %edi
	movsd	(%rsp), %xmm0                   # 8-byte Reload
                                        # xmm0 = mem[0],zero
	movb	$1, %al
	callq	printf
	movsd	.LCPI3_4(%rip), %xmm0           # xmm0 = mem[0],zero
	movl	$.L.str.4, %edi
	movb	$1, %al
	callq	printf
	movsd	(%rsp), %xmm0                   # 8-byte Reload
                                        # xmm0 = mem[0],zero
	addq	$24, %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
.LBB3_9:
	.cfi_def_cfa_offset 64
	movq	(%r14), %rdi
	callq	_Z5UsagePc
.Lfunc_end3:
	.size	_Z17parallel_solutioniPPc, .Lfunc_end3-_Z17parallel_solutioniPPc
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function main
.LCPI4_0:
	.quad	0x408f400000000000              # double 1000
.LCPI4_2:
	.quad	0x3f847ae147ae147b              # double 0.01
	.section	.rodata.cst16,"aM",@progbits,16
	.p2align	4, 0x0
.LCPI4_1:
	.quad	0x7fffffffffffffff              # double NaN
	.quad	0x7fffffffffffffff              # double NaN
	.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	$72, %rsp
	.cfi_def_cfa_offset 128
	.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	%rsi, %r14
	movl	%edi, %ebp
	leaq	64(%rsp), %r12
	movq	%r12, %rdi
	callq	hipEventCreate
	leaq	56(%rsp), %r13
	movq	%r13, %rdi
	callq	hipEventCreate
	leaq	48(%rsp), %rbx
	movq	%rbx, %rdi
	callq	hipEventCreate
	leaq	40(%rsp), %r15
	movq	%r15, %rdi
	callq	hipEventCreate
	movl	$.Lstr, %edi
	callq	puts@PLT
	movq	(%r12), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	movl	%ebp, %edi
	movq	%r14, %rsi
	callq	_Z19sequential_solutioniPPc
	movaps	%xmm0, 16(%rsp)                 # 16-byte Spill
	movq	(%r13), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	movq	(%r13), %rdi
	callq	hipEventSynchronize
	movq	(%r12), %rsi
	movq	(%r13), %rdx
	leaq	12(%rsp), %r12
	movq	%r12, %rdi
	callq	hipEventElapsedTime
	movl	$.Lstr.1, %edi
	callq	puts@PLT
	movq	(%rbx), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	movl	%ebp, %edi
	movq	%r14, %rsi
	callq	_Z17parallel_solutioniPPc
	movsd	%xmm0, (%rsp)                   # 8-byte Spill
	movq	(%r15), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	movq	(%r15), %rdi
	callq	hipEventSynchronize
	movq	(%rbx), %rsi
	movq	(%r15), %rdx
	leaq	8(%rsp), %rbx
	movq	%rbx, %rdi
	callq	hipEventElapsedTime
	xorps	%xmm0, %xmm0
	cvtss2sd	(%r12), %xmm0
	divsd	.LCPI4_0(%rip), %xmm0
	movl	$.L.str.7, %edi
	movb	$1, %al
	callq	printf
	xorps	%xmm0, %xmm0
	cvtss2sd	(%rbx), %xmm0
	divsd	.LCPI4_0(%rip), %xmm0
	movl	$.L.str.8, %edi
	movb	$1, %al
	callq	printf
	movapd	16(%rsp), %xmm0                 # 16-byte Reload
	subsd	(%rsp), %xmm0                   # 8-byte Folded Reload
	andpd	.LCPI4_1(%rip), %xmm0
	movapd	%xmm0, %xmm1
	movsd	.LCPI4_2(%rip), %xmm0           # xmm0 = mem[0],zero
	ucomisd	%xmm1, %xmm0
	movl	$.Lstr.3, %eax
	movl	$.Lstr.2, %edi
	cmovaq	%rax, %rdi
	callq	puts@PLT
	xorl	%eax, %eax
	addq	$72, %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	$_Z9reduce_piPf, %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	_Z9reduce_piPf,@object          # @_Z9reduce_piPf
	.section	.rodata,"a",@progbits
	.globl	_Z9reduce_piPf
	.p2align	3, 0x0
_Z9reduce_piPf:
	.quad	_Z24__device_stub__reduce_piPf
	.size	_Z9reduce_piPf, 8

	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"Before for loop, factor = %f.\n"
	.size	.L.str, 31

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"After for loop, factor = %f.\n"
	.size	.L.str.1, 30

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"With n = %lld terms\n"
	.size	.L.str.2, 21

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"   Our estimate of pi = %.14f\n"
	.size	.L.str.3, 31

	.type	.L.str.4,@object                # @.str.4
.L.str.4:
	.asciz	"   Ref estimate of pi = %.14f\n"
	.size	.L.str.4, 31

	.type	.L.str.7,@object                # @.str.7
.L.str.7:
	.asciz	"\nSequential elapsed time: %fs\n"
	.size	.L.str.7, 31

	.type	.L.str.8,@object                # @.str.8
.L.str.8:
	.asciz	"Parallel elapsed time: %fs\n"
	.size	.L.str.8, 28

	.type	.L.str.11,@object               # @.str.11
.L.str.11:
	.asciz	"usage: %s <n>\n"
	.size	.L.str.11, 15

	.type	.L.str.12,@object               # @.str.12
.L.str.12:
	.asciz	"   n is the number of terms and should be >= 1\n"
	.size	.L.str.12, 48

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z9reduce_piPf"
	.size	.L__unnamed_1, 15

	.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	"---------------------Sequential execution---------------------"
	.size	.Lstr, 63

	.type	.Lstr.1,@object                 # @str.1
.Lstr.1:
	.asciz	"----------------------Parallel execution----------------------"
	.size	.Lstr.1, 63

	.type	.Lstr.2,@object                 # @str.2
.Lstr.2:
	.asciz	"Test FAILED"
	.size	.Lstr.2, 12

	.type	.Lstr.3,@object                 # @str.3
.Lstr.3:
	.asciz	"Test PASSED"
	.size	.Lstr.3, 12

	.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 _Z24__device_stub__reduce_piPf
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z9reduce_piPf
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z9reduce_piPf          ; -- Begin function _Z9reduce_piPf
	.globl	_Z9reduce_piPf
	.p2align	8
	.type	_Z9reduce_piPf,@function
_Z9reduce_piPf:                         ; @_Z9reduce_piPf
; %bb.0:
	s_load_b32 s9, s[0:1], 0x14
	v_mov_b32_e32 v1, 0
	s_mov_b32 s4, s15
	s_mov_b32 s3, 0
	s_mov_b64 s[6:7], 0
	s_mov_b32 s5, s3
	s_waitcnt lgkmcnt(0)
	s_and_b32 s8, s9, 0xffff
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mad_u64_u32 v[2:3], null, s8, s4, v[0:1]
	v_mad_u64_u32 v[4:5], null, 0x7d0, v2, 0
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mov_b32_e32 v2, v5
	v_mad_u64_u32 v[5:6], null, 0x7d0, v3, v[2:3]
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_or_b32_e32 v2, 1, v4
	v_mov_b32_e32 v3, v5
.LBB0_1:                                ; =>This Inner Loop Header: Depth=1
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(SALU_CYCLE_1)
	v_clz_i32_u32_e32 v4, v3
	s_and_b32 s2, s6, 1
	s_cmp_eq_u64 s[2:3], 0
	s_cselect_b32 s2, -1, 0
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_3) | instid1(VALU_DEP_1)
	v_min_u32_e32 v6, 32, v4
	s_add_u32 s6, s6, 1
	s_addc_u32 s7, s7, 0
	s_cmpk_eq_i32 s6, 0x3e8
	v_lshlrev_b64 v[4:5], v6, v[2:3]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_min_u32_e32 v4, 1, v4
	v_or_b32_e32 v4, v5, v4
	v_sub_nc_u32_e32 v5, 32, v6
	v_cndmask_b32_e64 v6, -1.0, 1.0, s2
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_cvt_f32_u32_e32 v4, v4
	v_ldexp_f32 v4, v4, v5
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_div_scale_f32 v5, null, v4, v4, v6
	v_div_scale_f32 v9, vcc_lo, v6, v4, v6
	v_rcp_f32_e32 v7, v5
	s_waitcnt_depctr 0xfff
	v_fma_f32 v8, -v5, v7, 1.0
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fmac_f32_e32 v7, v8, v7
	v_mul_f32_e32 v8, v9, v7
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fma_f32 v10, -v5, v8, v9
	v_fmac_f32_e32 v8, v10, v7
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fma_f32 v5, -v5, v8, v9
	v_div_fmas_f32 v5, v5, v7, v8
	v_add_co_u32 v2, vcc_lo, v2, 2
	v_add_co_ci_u32_e32 v3, vcc_lo, 0, v3, vcc_lo
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_div_fixup_f32 v4, v5, v4, v6
	v_add_f32_e32 v1, v1, v4
	s_cbranch_scc0 .LBB0_1
; %bb.2:
	v_cmp_gt_u16_e64 s2, 0x42, s9
	v_lshl_add_u32 v2, v0, 2, 0
	s_delay_alu instid0(VALU_DEP_2)
	s_and_b32 vcc_lo, exec_lo, s2
	ds_store_b32 v2, v1
	s_waitcnt lgkmcnt(0)
	s_barrier
	buffer_gl0_inv
	s_cbranch_vccnz .LBB0_7
; %bb.3:                                ; %.lr.ph.preheader
	s_lshr_b32 s2, s8, 1
.LBB0_4:                                ; %.lr.ph
                                        ; =>This Inner Loop Header: Depth=1
	s_mov_b32 s3, exec_lo
	v_cmpx_gt_u32_e64 s2, v0
	s_cbranch_execz .LBB0_6
; %bb.5:                                ;   in Loop: Header=BB0_4 Depth=1
	v_lshl_add_u32 v1, s2, 2, v2
	ds_load_b32 v1, v1
	ds_load_b32 v3, v2
	s_waitcnt lgkmcnt(0)
	v_add_f32_e32 v1, v1, v3
	ds_store_b32 v2, v1
.LBB0_6:                                ;   in Loop: Header=BB0_4 Depth=1
	s_or_b32 exec_lo, exec_lo, s3
	s_lshr_b32 s3, s2, 1
	s_cmpk_lt_u32 s2, 0x42
	s_mov_b32 s2, s3
	s_waitcnt lgkmcnt(0)
	s_barrier
	buffer_gl0_inv
	s_cbranch_scc0 .LBB0_4
.LBB0_7:                                ; %._crit_edge
	s_mov_b32 s3, exec_lo
	v_cmpx_gt_u32_e32 32, v0
	s_cbranch_execz .LBB0_9
; %bb.8:
	v_add_nc_u32_e32 v1, 0x80, v2
	v_cmp_ne_u32_e64 s2, -1, v2
	s_mov_b64 s[6:7], src_shared_base
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_cmp_ne_u32_e32 vcc_lo, -1, v1
	v_cndmask_b32_e64 v4, 0, s7, s2
	v_cndmask_b32_e64 v3, 0, v2, s2
	v_cndmask_b32_e32 v5, 0, v1, vcc_lo
	v_cndmask_b32_e64 v6, 0, s7, vcc_lo
	flat_load_b32 v1, v[5:6] glc dlc
	s_waitcnt vmcnt(0)
	flat_load_b32 v6, v[3:4] glc dlc
	s_waitcnt vmcnt(0)
	v_add_nc_u32_e32 v5, 64, v2
	s_delay_alu instid0(VALU_DEP_1)
	v_cmp_ne_u32_e32 vcc_lo, -1, v5
	v_cndmask_b32_e32 v5, 0, v5, vcc_lo
	s_waitcnt lgkmcnt(0)
	v_add_f32_e32 v1, v1, v6
	v_cndmask_b32_e64 v6, 0, s7, vcc_lo
	flat_store_b32 v[3:4], v1 dlc
	s_waitcnt_vscnt null, 0x0
	flat_load_b32 v1, v[5:6] glc dlc
	s_waitcnt vmcnt(0)
	flat_load_b32 v6, v[3:4] glc dlc
	s_waitcnt vmcnt(0)
	v_add_nc_u32_e32 v5, 32, v2
	s_delay_alu instid0(VALU_DEP_1)
	v_cmp_ne_u32_e32 vcc_lo, -1, v5
	v_cndmask_b32_e32 v5, 0, v5, vcc_lo
	s_waitcnt lgkmcnt(0)
	v_add_f32_e32 v1, v1, v6
	v_cndmask_b32_e64 v6, 0, s7, vcc_lo
	flat_store_b32 v[3:4], v1 dlc
	s_waitcnt_vscnt null, 0x0
	flat_load_b32 v1, v[5:6] glc dlc
	s_waitcnt vmcnt(0)
	flat_load_b32 v6, v[3:4] glc dlc
	s_waitcnt vmcnt(0)
	v_add_nc_u32_e32 v5, 16, v2
	s_delay_alu instid0(VALU_DEP_1)
	v_cmp_ne_u32_e32 vcc_lo, -1, v5
	v_cndmask_b32_e32 v5, 0, v5, vcc_lo
	s_waitcnt lgkmcnt(0)
	v_add_f32_e32 v1, v1, v6
	v_cndmask_b32_e64 v6, 0, s7, vcc_lo
	flat_store_b32 v[3:4], v1 dlc
	s_waitcnt_vscnt null, 0x0
	flat_load_b32 v1, v[5:6] glc dlc
	s_waitcnt vmcnt(0)
	flat_load_b32 v6, v[3:4] glc dlc
	s_waitcnt vmcnt(0)
	v_add_nc_u32_e32 v5, 8, v2
	s_delay_alu instid0(VALU_DEP_1)
	v_cmp_ne_u32_e32 vcc_lo, -1, v5
	v_cndmask_b32_e32 v5, 0, v5, vcc_lo
	s_waitcnt lgkmcnt(0)
	v_add_f32_e32 v1, v1, v6
	v_cndmask_b32_e64 v6, 0, s7, vcc_lo
	flat_store_b32 v[3:4], v1 dlc
	s_waitcnt_vscnt null, 0x0
	flat_load_b32 v5, v[5:6] glc dlc
	s_waitcnt vmcnt(0)
	flat_load_b32 v6, v[3:4] glc dlc
	s_waitcnt vmcnt(0)
	v_add_nc_u32_e32 v1, 4, v2
	s_delay_alu instid0(VALU_DEP_1)
	v_cmp_ne_u32_e32 vcc_lo, -1, v1
	v_cndmask_b32_e32 v1, 0, v1, vcc_lo
	v_cndmask_b32_e64 v2, 0, s7, vcc_lo
	s_waitcnt lgkmcnt(0)
	v_add_f32_e32 v5, v5, v6
	flat_store_b32 v[3:4], v5 dlc
	s_waitcnt_vscnt null, 0x0
	flat_load_b32 v1, v[1:2] glc dlc
	s_waitcnt vmcnt(0)
	flat_load_b32 v2, v[3:4] glc dlc
	s_waitcnt vmcnt(0) lgkmcnt(0)
	v_add_f32_e32 v1, v1, v2
	flat_store_b32 v[3:4], v1 dlc
	s_waitcnt_vscnt null, 0x0
.LBB0_9:
	s_or_b32 exec_lo, exec_lo, s3
	s_delay_alu instid0(SALU_CYCLE_1)
	s_mov_b32 s2, exec_lo
	v_cmpx_eq_u32_e32 0, v0
	s_cbranch_execz .LBB0_11
; %bb.10:
	v_mov_b32_e32 v0, 0
	s_load_b64 s[0:1], s[0:1], 0x0
	s_lshl_b64 s[2:3], s[4:5], 2
	ds_load_b32 v1, v0
	s_waitcnt lgkmcnt(0)
	s_add_u32 s0, s0, s2
	s_addc_u32 s1, s1, s3
	global_store_b32 v0, v1, s[0:1]
.LBB0_11:
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z9reduce_piPf
		.amdhsa_group_segment_fixed_size 0
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 264
		.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 11
		.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	_Z9reduce_piPf, .Lfunc_end0-_Z9reduce_piPf
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 968
; NumSgprs: 18
; NumVgprs: 11
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 1
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 11
; Occupancy: 16
; WaveLimiterHint : 1
; 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 sdata
	.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:     hidden_block_count_x
      - .offset:         12
        .size:           4
        .value_kind:     hidden_block_count_y
      - .offset:         16
        .size:           4
        .value_kind:     hidden_block_count_z
      - .offset:         20
        .size:           2
        .value_kind:     hidden_group_size_x
      - .offset:         22
        .size:           2
        .value_kind:     hidden_group_size_y
      - .offset:         24
        .size:           2
        .value_kind:     hidden_group_size_z
      - .offset:         26
        .size:           2
        .value_kind:     hidden_remainder_x
      - .offset:         28
        .size:           2
        .value_kind:     hidden_remainder_y
      - .offset:         30
        .size:           2
        .value_kind:     hidden_remainder_z
      - .offset:         48
        .size:           8
        .value_kind:     hidden_global_offset_x
      - .offset:         56
        .size:           8
        .value_kind:     hidden_global_offset_y
      - .offset:         64
        .size:           8
        .value_kind:     hidden_global_offset_z
      - .offset:         72
        .size:           2
        .value_kind:     hidden_grid_dims
      - .offset:         128
        .size:           4
        .value_kind:     hidden_dynamic_lds_size
    .group_segment_fixed_size: 0
    .kernarg_segment_align: 8
    .kernarg_segment_size: 264
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z9reduce_piPf
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z9reduce_piPf.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     11
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
