// to compile on cooley: nvcc  -arch sm_30 -o mandelbrot mandelbrot.cu -lm 
// to run on cooley:    ./mandelbrot 

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

#define MXITER 1000
#define NPOINTS 2048

// we will use these later to specify a 16x16 thread-block size
#define TX 16
#define TY 16

typedef struct {
  
  double r;
  double i;
  
}d_complex;

// return 1 if c is outside the mandelbrot set
// return 0 if c is inside the mandelbrot set

// TASK 1: annotate this as a device function 
__device__ int testpoint(d_complex c){
  
  d_complex z = c;
  
  for(int iter=0; iter<MXITER; iter++){
    
    double temp = (z.r*z.r) - (z.i*z.i) + c.r;
    
    z.i = z.r*z.i*2. + c.i;
    z.r = temp;
    
    if((z.r*z.r+z.i*z.i)>4.0){
      return 1;
    }
  }
  
  return 0;
  
}

// FREEBIE: partial reduction 
__device__ void partialReduction(int outside, int *outsideCounts){
  
  __shared__ int s_outside[TX*TY];
  int t = threadIdx.x + threadIdx.y*TX;
  s_outside[t] = outside;
  
  int alive = TX*TY;
  while(alive>1){
    
    __syncthreads();
    
    alive /= 2;
    if(t<alive && t+alive<TX*TY)
      s_outside[t] += s_outside[t+alive];
    
  }
  
  if(t==0){
    int b = blockIdx.x + gridDim.x*blockIdx.y;
    outsideCounts[b] = s_outside[0];
  }
}

// TASK 2: make this a kernel that processes 
// (i,j) \in   [blockIdx.x*blockDim.x,(blockIdx.x+1)*blockDim.x) 
//           x [blockIdx.y*blockDim.y,(blockIdx.y+1)*blockDim.y) 

// TASK 2a: annotate this to indicate it is a kernel and change return type to void
__global__ void mandeloutside(int * outsideCounts){

  double eps = 1e-5;

  d_complex c;

  // TASK 2b: replace loop structures with (i,j) defined from blockIdx, blockDim, threadIdx
  //  for(i=0;i<NPOINTS;i++){
  //    for(j=0;j<NPOINTS;j++){
  int i = threadIdx.x+blockIdx.x*TX; 
  int j = threadIdx.y+blockIdx.y*TY; 

  c.r = -2. + 2.5*((double)i)/(double)(NPOINTS)+eps;
  c.i =       1.125*((double)j)/(double)(NPOINTS)+eps;
  
  // TASK 2c: replace this with a partial sum reduction of numoutside in thread block
  int outside = 0; 
  if(i<NPOINTS && j<NPOINTS){
    outside = testpoint(c);
  }
  //   }
  // }

  // FREEBIE: reduction of TX*TY values to one value on each thread-block
  partialReduction(outside, outsideCounts);

}

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

  // TASK 3a: define dim3 variables for the grid size and thread-block size
  int GX = (NPOINTS+TX-1)/TX;
  int GY = (NPOINTS+TY-1)/TY;
  dim3 dimGrid(GX,GY,1);
  dim3 dimBlock(TX,TY,1);

  // TASK 3b: use cudaMalloc to create a DEVICE array that has one entry for each thread-block
  int *c_outsideCounts;
  cudaMalloc(&c_outsideCounts, GX*GY*sizeof(int));

  // FREEBIE: create CUDA events for timing
  cudaEvent_t start, end;

  cudaEventCreate(&start);
  cudaEventCreate(&end);
  
  cudaEventRecord(start);
  
  // TASK 3c: replace this with a kernel call
  mandeloutside <<< dimGrid, dimBlock >>> (c_outsideCounts);
  
  // FREEBIE: timing
  float elapsed;
  cudaEventRecord(end);
  cudaEventSynchronize(end);
  cudaEventElapsedTime(&elapsed, start, end);
  elapsed /= 1000;
  printf("elapsed = %g\n", elapsed);

  // TASK 3d: allocate a HOST array to receive the contents of the c_outsideCounts array
  int *h_outsideCounts = (int*) calloc(GX*GY, sizeof(int));
  
  // TASK 3e: use cudaMemcpy to copy the contents of the entries of c_outsideCounts to h_outsideCounts
  cudaMemcpy(h_outsideCounts, c_outsideCounts, GX*GY*sizeof(int), cudaMemcpyDeviceToHost);

  // TASK 3f: sum up the outsideCounts 
  int numoutside = 0;
  for(int n=0;n<GX*GY;++n){
    numoutside += h_outsideCounts[n];
  }

  printf("numoustide = %d\n", numoutside);

  double area = 2.*2.5*1.125*(NPOINTS*NPOINTS-numoutside)/(NPOINTS*NPOINTS);

  printf("area = %17.15lf\n", area);

  return 0;
}  

	.file	"tmpxft_00381623_00000000-6_mandelbrot.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2032:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2032:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z9testpoint9d_complex
	.type	_Z9testpoint9d_complex, @function
_Z9testpoint9d_complex:
.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	_Z9testpoint9d_complex, .-_Z9testpoint9d_complex
	.globl	_Z16partialReductioniPi
	.type	_Z16partialReductioniPi, @function
_Z16partialReductioniPi:
.LFB2028:
	.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
.LFE2028:
	.size	_Z16partialReductioniPi, .-_Z16partialReductioniPi
	.globl	_Z33__device_stub__Z13mandeloutsidePiPi
	.type	_Z33__device_stub__Z13mandeloutsidePiPi, @function
_Z33__device_stub__Z13mandeloutsidePiPi:
.LFB2054:
	.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	.L6
	pushq	24(%rsp)
	.cfi_def_cfa_offset 120
	leaq	_Z13mandeloutsidePi(%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
.L6:
	movq	88(%rsp), %rax
	subq	%fs:40, %rax
	je	.L8
	call	__stack_chk_fail@PLT
.L8:
	addq	$104, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2054:
	.size	_Z33__device_stub__Z13mandeloutsidePiPi, .-_Z33__device_stub__Z13mandeloutsidePiPi
	.globl	_Z13mandeloutsidePi
	.type	_Z13mandeloutsidePi, @function
_Z13mandeloutsidePi:
.LFB2055:
	.cfi_startproc
	endbr64
	jmp	_Z33__device_stub__Z13mandeloutsidePiPi
	.cfi_endproc
.LFE2055:
	.size	_Z13mandeloutsidePi, .-_Z13mandeloutsidePi
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC1:
	.string	"elapsed = %g\n"
.LC2:
	.string	"numoustide = %d\n"
.LC5:
	.string	"area = %17.15lf\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2029:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movl	$65536, %esi
	pushq	%rbx
	.cfi_def_cfa_offset 24
	.cfi_offset 3, -24
	subq	$72, %rsp
	.cfi_def_cfa_offset 96
	movq	%fs:40, %rax
	movq	%rax, 56(%rsp)
	movabsq	$549755814016, %rax
	leaq	8(%rsp), %rdi
	movl	$1, 40(%rsp)
	movl	$1, 52(%rsp)
	movq	%rax, 32(%rsp)
	movabsq	$68719476752, %rax
	movq	%rax, 44(%rsp)
	call	cudaMalloc@PLT
	leaq	16(%rsp), %rdi
	call	cudaEventCreate@PLT
	leaq	24(%rsp), %rdi
	call	cudaEventCreate@PLT
	movq	16(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movl	52(%rsp), %ecx
	movl	40(%rsp), %esi
	xorl	%r9d, %r9d
	movq	44(%rsp), %rdx
	movq	32(%rsp), %rdi
	xorl	%r8d, %r8d
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L13
	movq	8(%rsp), %rdi
	call	_Z33__device_stub__Z13mandeloutsidePiPi
.L13:
	movq	24(%rsp), %rdi
	xorl	%esi, %esi
	xorl	%ebx, %ebx
	call	cudaEventRecord@PLT
	movq	24(%rsp), %rdi
	call	cudaEventSynchronize@PLT
	movq	24(%rsp), %rdx
	movq	16(%rsp), %rsi
	leaq	4(%rsp), %rdi
	call	cudaEventElapsedTime@PLT
	leaq	.LC1(%rip), %rsi
	movl	$2, %edi
	movb	$1, %al
	movss	4(%rsp), %xmm0
	divss	.LC0(%rip), %xmm0
	movss	%xmm0, 4(%rsp)
	cvtss2sd	%xmm0, %xmm0
	call	__printf_chk@PLT
	movl	$4, %esi
	movl	$16384, %edi
	call	calloc@PLT
	movq	8(%rsp), %rsi
	movl	$65536, %edx
	movl	$2, %ecx
	movq	%rax, %rdi
	movq	%rax, %rbp
	call	cudaMemcpy@PLT
	xorl	%edx, %edx
.L14:
	addl	0(%rbp,%rdx,4), %ebx
	incq	%rdx
	cmpq	$16384, %rdx
	jne	.L14
	leaq	.LC2(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	movl	%ebx, %edx
	call	__printf_chk@PLT
	movl	$4194304, %eax
	movl	$2, %edi
	leaq	.LC5(%rip), %rsi
	subl	%ebx, %eax
	cvtsi2sdl	%eax, %xmm0
	mulsd	.LC3(%rip), %xmm0
	movb	$1, %al
	mulsd	.LC4(%rip), %xmm0
	call	__printf_chk@PLT
	movq	56(%rsp), %rax
	subq	%fs:40, %rax
	je	.L15
	call	__stack_chk_fail@PLT
.L15:
	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
.LFE2029:
	.size	main, .-main
	.section	.rodata.str1.1
.LC6:
	.string	"_Z13mandeloutsidePi"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2057:
	.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	.LC6(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z13mandeloutsidePi(%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
.LFE2057:
	.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.cst4,"aM",@progbits,4
	.align 4
.LC0:
	.long	1148846080
	.section	.rodata.cst8,"aM",@progbits,8
	.align 8
.LC3:
	.long	0
	.long	1075216384
	.align 8
.LC4:
	.long	0
	.long	1049624576
	.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:

// to compile on cooley: nvcc  -arch sm_30 -o mandelbrot mandelbrot.cu -lm 
// to run on cooley:    ./mandelbrot 


#include <hip/hip_runtime.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>

#define MXITER 1000
#define NPOINTS 2048

// we will use these later to specify a 16x16 thread-block size
#define TX 16
#define TY 16

typedef struct {
  
  double r;
  double i;
  
}d_complex;

// return 1 if c is outside the mandelbrot set
// return 0 if c is inside the mandelbrot set

// TASK 1: annotate this as a device function 
__device__ int testpoint(d_complex c){
  
  d_complex z = c;
  
  for(int iter=0; iter<MXITER; iter++){
    
    double temp = (z.r*z.r) - (z.i*z.i) + c.r;
    
    z.i = z.r*z.i*2. + c.i;
    z.r = temp;
    
    if((z.r*z.r+z.i*z.i)>4.0){
      return 1;
    }
  }
  
  return 0;
  
}

// FREEBIE: partial reduction 
__device__ void partialReduction(int outside, int *outsideCounts){
  
  __shared__ int s_outside[TX*TY];
  int t = threadIdx.x + threadIdx.y*TX;
  s_outside[t] = outside;
  
  int alive = TX*TY;
  while(alive>1){
    
    __syncthreads();
    
    alive /= 2;
    if(t<alive && t+alive<TX*TY)
      s_outside[t] += s_outside[t+alive];
    
  }
  
  if(t==0){
    int b = blockIdx.x + gridDim.x*blockIdx.y;
    outsideCounts[b] = s_outside[0];
  }
}

// TASK 2: make this a kernel that processes 
// (i,j) \in   [blockIdx.x*blockDim.x,(blockIdx.x+1)*blockDim.x) 
//           x [blockIdx.y*blockDim.y,(blockIdx.y+1)*blockDim.y) 

// TASK 2a: annotate this to indicate it is a kernel and change return type to void
__global__ void mandeloutside(int * outsideCounts){

  double eps = 1e-5;

  d_complex c;

  // TASK 2b: replace loop structures with (i,j) defined from blockIdx, blockDim, threadIdx
  //  for(i=0;i<NPOINTS;i++){
  //    for(j=0;j<NPOINTS;j++){
  int i = threadIdx.x+blockIdx.x*TX; 
  int j = threadIdx.y+blockIdx.y*TY; 

  c.r = -2. + 2.5*((double)i)/(double)(NPOINTS)+eps;
  c.i =       1.125*((double)j)/(double)(NPOINTS)+eps;
  
  // TASK 2c: replace this with a partial sum reduction of numoutside in thread block
  int outside = 0; 
  if(i<NPOINTS && j<NPOINTS){
    outside = testpoint(c);
  }
  //   }
  // }

  // FREEBIE: reduction of TX*TY values to one value on each thread-block
  partialReduction(outside, outsideCounts);

}

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

  // TASK 3a: define dim3 variables for the grid size and thread-block size
  int GX = (NPOINTS+TX-1)/TX;
  int GY = (NPOINTS+TY-1)/TY;
  dim3 dimGrid(GX,GY,1);
  dim3 dimBlock(TX,TY,1);

  // TASK 3b: use cudaMalloc to create a DEVICE array that has one entry for each thread-block
  int *c_outsideCounts;
  hipMalloc(&c_outsideCounts, GX*GY*sizeof(int));

  // FREEBIE: create CUDA events for timing
  hipEvent_t start, end;

  hipEventCreate(&start);
  hipEventCreate(&end);
  
  hipEventRecord(start);
  
  // TASK 3c: replace this with a kernel call
  mandeloutside <<< dimGrid, dimBlock >>> (c_outsideCounts);
  
  // FREEBIE: timing
  float elapsed;
  hipEventRecord(end);
  hipEventSynchronize(end);
  hipEventElapsedTime(&elapsed, start, end);
  elapsed /= 1000;
  printf("elapsed = %g\n", elapsed);

  // TASK 3d: allocate a HOST array to receive the contents of the c_outsideCounts array
  int *h_outsideCounts = (int*) calloc(GX*GY, sizeof(int));
  
  // TASK 3e: use cudaMemcpy to copy the contents of the entries of c_outsideCounts to h_outsideCounts
  hipMemcpy(h_outsideCounts, c_outsideCounts, GX*GY*sizeof(int), hipMemcpyDeviceToHost);

  // TASK 3f: sum up the outsideCounts 
  int numoutside = 0;
  for(int n=0;n<GX*GY;++n){
    numoutside += h_outsideCounts[n];
  }

  printf("numoustide = %d\n", numoutside);

  double area = 2.*2.5*1.125*(NPOINTS*NPOINTS-numoutside)/(NPOINTS*NPOINTS);

  printf("area = %17.15lf\n", area);

  return 0;
}  

	.text
	.file	"mandelbrot.hip"
	.globl	_Z28__device_stub__mandeloutsidePi # -- Begin function _Z28__device_stub__mandeloutsidePi
	.type	_Z28__device_stub__mandeloutsidePi,@function
_Z28__device_stub__mandeloutsidePi:     # @_Z28__device_stub__mandeloutsidePi
	.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	$_Z13mandeloutsidePi, %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	_Z28__device_stub__mandeloutsidePi, .Lfunc_end0-_Z28__device_stub__mandeloutsidePi
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst4,"aM",@progbits,4
	.p2align	2, 0x0                          # -- Begin function main
.LCPI1_0:
	.long	0x447a0000                      # float 1000
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0
.LCPI1_1:
	.quad	0x4016800000000000              # double 5.625
.LCPI1_2:
	.quad	0x3e90000000000000              # double 2.384185791015625E-7
	.text
	.globl	main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%r14
	.cfi_def_cfa_offset 16
	pushq	%rbx
	.cfi_def_cfa_offset 24
	subq	$40, %rsp
	.cfi_def_cfa_offset 64
	.cfi_offset %rbx, -24
	.cfi_offset %r14, -16
	leaq	16(%rsp), %rdi
	movl	$65536, %esi                    # imm = 0x10000
	callq	hipMalloc
	leaq	24(%rsp), %rbx
	movq	%rbx, %rdi
	callq	hipEventCreate
	leaq	8(%rsp), %rdi
	callq	hipEventCreate
	movq	(%rbx), %rdi
	xorl	%ebx, %ebx
	xorl	%esi, %esi
	callq	hipEventRecord
	movabsq	$549755814016, %rdi             # imm = 0x8000000080
	movabsq	$68719476752, %rdx              # imm = 0x1000000010
	movl	$1, %esi
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB1_2
# %bb.1:
	movq	16(%rsp), %rdi
	callq	_Z28__device_stub__mandeloutsidePi
.LBB1_2:
	movq	8(%rsp), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	movq	8(%rsp), %rdi
	callq	hipEventSynchronize
	movq	24(%rsp), %rsi
	movq	8(%rsp), %rdx
	leaq	36(%rsp), %r14
	movq	%r14, %rdi
	callq	hipEventElapsedTime
	movss	(%r14), %xmm0                   # xmm0 = mem[0],zero,zero,zero
	divss	.LCPI1_0(%rip), %xmm0
	movss	%xmm0, (%r14)
	cvtss2sd	%xmm0, %xmm0
	movl	$.L.str, %edi
	movb	$1, %al
	callq	printf
	movl	$16384, %edi                    # imm = 0x4000
	movl	$4, %esi
	callq	calloc
	movq	%rax, %r14
	movq	16(%rsp), %rsi
	movl	$65536, %edx                    # imm = 0x10000
	movq	%rax, %rdi
	movl	$2, %ecx
	callq	hipMemcpy
	xorl	%eax, %eax
.LBB1_3:                                # =>This Inner Loop Header: Depth=1
	addl	(%r14,%rax,4), %ebx
	incq	%rax
	cmpq	$16384, %rax                    # imm = 0x4000
	jne	.LBB1_3
# %bb.4:
	movl	$.L.str.1, %edi
	movl	%ebx, %esi
	xorl	%eax, %eax
	callq	printf
	movl	$4194304, %eax                  # imm = 0x400000
	subl	%ebx, %eax
	xorps	%xmm0, %xmm0
	cvtsi2sd	%eax, %xmm0
	mulsd	.LCPI1_1(%rip), %xmm0
	mulsd	.LCPI1_2(%rip), %xmm0
	movl	$.L.str.2, %edi
	movb	$1, %al
	callq	printf
	xorl	%eax, %eax
	addq	$40, %rsp
	.cfi_def_cfa_offset 24
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%r14
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end1:
	.size	main, .Lfunc_end1-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	.LBB2_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rdi
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB2_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z13mandeloutsidePi, %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_end2:
	.size	__hip_module_ctor, .Lfunc_end2-__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	.LBB3_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
.LBB3_2:
	retq
.Lfunc_end3:
	.size	__hip_module_dtor, .Lfunc_end3-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	_Z13mandeloutsidePi,@object     # @_Z13mandeloutsidePi
	.section	.rodata,"a",@progbits
	.globl	_Z13mandeloutsidePi
	.p2align	3, 0x0
_Z13mandeloutsidePi:
	.quad	_Z28__device_stub__mandeloutsidePi
	.size	_Z13mandeloutsidePi, 8

	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"elapsed = %g\n"
	.size	.L.str, 14

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"numoustide = %d\n"
	.size	.L.str.1, 17

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"area = %17.15lf\n"
	.size	.L.str.2, 17

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z13mandeloutsidePi"
	.size	.L__unnamed_1, 20

	.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

	.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 _Z28__device_stub__mandeloutsidePi
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z13mandeloutsidePi
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z13mandeloutsidePi     ; -- Begin function _Z13mandeloutsidePi
	.globl	_Z13mandeloutsidePi
	.p2align	8
	.type	_Z13mandeloutsidePi,@function
_Z13mandeloutsidePi:                    ; @_Z13mandeloutsidePi
; %bb.0:
	v_and_b32_e32 v8, 0x3ff, v0
	v_bfe_u32 v9, v0, 10, 10
	v_mov_b32_e32 v10, 0
	s_mov_b32 s2, exec_lo
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_lshl_add_u32 v0, s14, 4, v8
	v_lshl_add_u32 v1, s15, 4, v9
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_max_i32_e32 v2, v0, v1
	v_cmpx_gt_i32_e32 0x800, v2
	s_cbranch_execz .LBB0_9
; %bb.1:
	v_cvt_f64_i32_e32 v[2:3], v0
	v_cvt_f64_i32_e32 v[0:1], v1
	s_mov_b32 s4, 0x88e368f1
	s_mov_b32 s5, 0x3ee4f8b5
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_mul_f64 v[2:3], 0x40040000, v[2:3]
	v_mul_f64 v[0:1], 0x3ff20000, v[0:1]
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_fma_f64 v[2:3], 0x3f400000, v[2:3], -2.0
	v_fma_f64 v[0:1], 0x3f400000, v[0:1], s[4:5]
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_f64 v[2:3], v[2:3], s[4:5]
	v_mul_f64 v[4:5], v[0:1], v[0:1]
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_mul_f64 v[6:7], v[2:3], v[0:1]
	v_fma_f64 v[10:11], v[2:3], v[2:3], -v[4:5]
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_fma_f64 v[4:5], v[6:7], 2.0, v[0:1]
	v_add_f64 v[6:7], v[2:3], v[10:11]
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mul_f64 v[10:11], v[4:5], v[4:5]
	v_fma_f64 v[10:11], v[6:7], v[6:7], v[10:11]
	s_delay_alu instid0(VALU_DEP_1)
	v_cmp_nlt_f64_e32 vcc_lo, 4.0, v[10:11]
	v_mov_b32_e32 v10, 1
	s_and_saveexec_b32 s3, vcc_lo
	s_cbranch_execz .LBB0_8
; %bb.2:                                ; %.lr.ph.preheader
	s_mov_b32 s4, 0
	s_mov_b32 s5, 0
                                        ; implicit-def: $sgpr6
.LBB0_3:                                ; %.lr.ph
                                        ; =>This Inner Loop Header: Depth=1
	s_or_b32 s6, s6, exec_lo
	s_cmpk_eq_i32 s5, 0x3e7
	s_cbranch_scc1 .LBB0_5
; %bb.4:                                ;   in Loop: Header=BB0_3 Depth=1
	v_mul_f64 v[10:11], v[4:5], v[4:5]
	v_mul_f64 v[4:5], v[6:7], v[4:5]
	s_and_not1_b32 s6, s6, exec_lo
	s_add_i32 s7, s5, 1
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_fma_f64 v[6:7], v[6:7], v[6:7], -v[10:11]
	v_fma_f64 v[4:5], v[4:5], 2.0, v[0:1]
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_f64 v[6:7], v[2:3], v[6:7]
	v_mul_f64 v[10:11], v[4:5], v[4:5]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fma_f64 v[10:11], v[6:7], v[6:7], v[10:11]
	v_cmp_lt_f64_e32 vcc_lo, 4.0, v[10:11]
	s_and_b32 s8, vcc_lo, exec_lo
	s_delay_alu instid0(SALU_CYCLE_1)
	s_or_b32 s6, s6, s8
	s_branch .LBB0_6
.LBB0_5:                                ;   in Loop: Header=BB0_3 Depth=1
                                        ; implicit-def: $sgpr7
                                        ; implicit-def: $vgpr6_vgpr7
                                        ; implicit-def: $vgpr4_vgpr5
.LBB0_6:                                ; %Flow
                                        ;   in Loop: Header=BB0_3 Depth=1
	v_mov_b32_e32 v10, s5
	s_and_b32 s8, exec_lo, s6
	s_mov_b32 s5, s7
	s_or_b32 s4, s8, s4
	s_delay_alu instid0(SALU_CYCLE_1)
	s_and_not1_b32 exec_lo, exec_lo, s4
	s_cbranch_execnz .LBB0_3
; %bb.7:                                ; %_Z9testpoint9d_complex.exit.loopexit
	s_or_b32 exec_lo, exec_lo, s4
	v_cmp_gt_u32_e32 vcc_lo, 0x3e7, v10
	v_cndmask_b32_e64 v10, 0, 1, vcc_lo
.LBB0_8:                                ; %Flow16
	s_or_b32 exec_lo, exec_lo, s3
.LBB0_9:                                ; %_Z9testpoint9d_complex.exit
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(SKIP_2) | instid1(VALU_DEP_1)
	s_or_b32 exec_lo, exec_lo, s2
	v_lshl_add_u32 v0, v9, 4, v8
	s_movk_i32 s3, 0x100
	v_lshlrev_b32_e32 v1, 2, v0
	ds_store_b32 v1, v10
.LBB0_10:                               ; =>This Inner Loop Header: Depth=1
	s_lshr_b32 s4, s3, 1
	s_waitcnt lgkmcnt(0)
	v_add_nc_u32_e32 v2, s4, v0
	v_cmp_gt_u32_e32 vcc_lo, s4, v0
	s_barrier
	buffer_gl0_inv
	v_cmp_gt_u32_e64 s2, 0x100, v2
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_and_b32 s5, vcc_lo, s2
	s_and_saveexec_b32 s2, s5
	s_cbranch_execz .LBB0_12
; %bb.11:                               ;   in Loop: Header=BB0_10 Depth=1
	v_lshl_add_u32 v2, s4, 2, v1
	ds_load_b32 v2, v2
	ds_load_b32 v3, v1
	s_waitcnt lgkmcnt(0)
	v_add_nc_u32_e32 v2, v3, v2
	ds_store_b32 v1, v2
.LBB0_12:                               ;   in Loop: Header=BB0_10 Depth=1
	s_or_b32 exec_lo, exec_lo, s2
	s_cmp_gt_u32 s3, 3
	s_mov_b32 s3, s4
	s_cbranch_scc1 .LBB0_10
; %bb.13:
	s_mov_b32 s2, exec_lo
	v_cmpx_eq_u32_e32 0, v0
	s_cbranch_execz .LBB0_15
; %bb.14:
	s_load_b32 s2, s[0:1], 0x8
	v_mov_b32_e32 v0, 0
	s_load_b64 s[0:1], s[0:1], 0x0
	ds_load_b32 v1, v0
	s_waitcnt lgkmcnt(0)
	s_mul_i32 s2, s2, s15
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_add_i32 s2, s2, s14
	s_ashr_i32 s3, s2, 31
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_lshl_b64 s[2:3], s[2:3], 2
	s_add_u32 s0, s0, s2
	s_addc_u32 s1, s1, s3
	global_store_b32 v0, v1, s[0:1]
.LBB0_15:                               ; %_Z16partialReductioniPi.exit
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z13mandeloutsidePi
		.amdhsa_group_segment_fixed_size 1024
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 264
		.amdhsa_user_sgpr_count 14
		.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 1
		.amdhsa_system_sgpr_workgroup_id_z 0
		.amdhsa_system_sgpr_workgroup_info 0
		.amdhsa_system_vgpr_workitem_id 1
		.amdhsa_next_free_vgpr 12
		.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	_Z13mandeloutsidePi, .Lfunc_end0-_Z13mandeloutsidePi
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 652
; NumSgprs: 18
; NumVgprs: 12
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 1024 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 1
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 12
; Occupancy: 16
; WaveLimiterHint : 0
; COMPUTE_PGM_RSRC2:SCRATCH_EN: 0
; COMPUTE_PGM_RSRC2:USER_SGPR: 14
; COMPUTE_PGM_RSRC2:TRAP_HANDLER: 0
; COMPUTE_PGM_RSRC2:TGID_X_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Y_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Z_EN: 0
; COMPUTE_PGM_RSRC2:TIDIG_COMP_CNT: 1
	.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:     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
    .group_segment_fixed_size: 1024
    .kernarg_segment_align: 8
    .kernarg_segment_size: 264
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z13mandeloutsidePi
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z13mandeloutsidePi.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     12
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
