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

typedef struct
{
    float posx;
    float posy;
    float range;
    float temp;
}
heatsrc_t;

typedef struct
{
    unsigned maxiter;       // maximum number of iterations
    unsigned resolution;    // spatial resolution
    int algorithm;          // 0=>Jacobi, 1=>Gauss

    unsigned visres;        // visualization resolution
  
    float *u, *uhelp;
    float *uvis;

    unsigned   numsrcs;     // number of heat sources
    heatsrc_t *heatsrcs;
}
algoparam_t;

// function declarations
int read_input( FILE *infile, algoparam_t *param );
void print_params( algoparam_t *param );
int initialize( algoparam_t *param );
int finalize( algoparam_t *param );
void write_image( FILE * f, float *u,
		  unsigned sizex, unsigned sizey );
int coarsen(float *uold, unsigned oldx, unsigned oldy ,
	    float *unew, unsigned newx, unsigned newy );


__global__ void gpu_Heat (float *h, float *g, float *d,int N);
__global__ void gpu_HeatReduction(float *res, float *res1);
#define NB 8
#define min(a,b) ( ((a) < (b)) ? (a) : (b) )

float cpu_residual (float *u, float *utmp, unsigned sizex, unsigned sizey)
{
    float diff, sum=0.0;
  
    for (int i=1; i<sizex-1; i++) 
        for (int j=1; j<sizey-1; j++) {
            diff = utmp[i*sizey+j] - u[i*sizey + j];
            sum += diff * diff;
	}
    return(sum);
}

float cpu_jacobi (float *u, float *utmp, unsigned sizex, unsigned sizey)
{
    float diff, sum=0.0;
    int nbx, bx, nby, by;
  
    nbx = NB;
    bx = sizex/nbx;
    nby = NB;
    by = sizey/nby;
    for (int ii=0; ii<nbx; ii++)
        for (int jj=0; jj<nby; jj++) 
            for (int i=1+ii*bx; i<=min((ii+1)*bx, sizex-2); i++) 
                for (int j=1+jj*by; j<=min((jj+1)*by, sizey-2); j++) {
	            utmp[i*sizey+j]= 0.25 * (u[ i*sizey     + (j-1) ]+  // left
					     u[ i*sizey     + (j+1) ]+  // right
				             u[ (i-1)*sizey + j     ]+  // top
				             u[ (i+1)*sizey + j     ]); // bottom
            	    diff = utmp[i*sizey+j] - u[i*sizey + j];
            	    sum += diff * diff;
	        }
    return(sum);
}

void usage( char *s )
{
    fprintf(stderr, "Usage: %s <input file> -t threads -b blocks\n", s);
    fprintf(stderr, "       -t number of threads per block in each dimension (e.g. 16)\n");
}

int main( int argc, char *argv[] ) {
    unsigned iter;
    FILE *infile, *resfile;
    char *resfilename;

    // algorithmic parameters
    algoparam_t param;
    int np;

    // check arguments
    if( argc < 4 ) {
	usage( argv[0] );
	return 1;
    }

    // check input file
    if( !(infile=fopen(argv[1], "r"))  ) {
	fprintf(stderr, 
		"\nError: Cannot open \"%s\" for reading.\n\n", argv[1]);
      
	usage(argv[0]);
	return 1;
    }

    // check result file
    resfilename="heat.ppm";

    if( !(resfile=fopen(resfilename, "w")) ) {
	fprintf(stderr, 
		"\nError: Cannot open \"%s\" for writing.\n\n", 
		resfilename);
	usage(argv[0]);
	return 1;
    }

    // check input
    if( !read_input(infile, &param) )
    {
	fprintf(stderr, "\nError: Error parsing input file.\n\n");
	usage(argv[0]);
	return 1;
    }

    // full size (param.resolution are only the inner points)
    np = param.resolution + 2;

    int Grid_Dim, Block_Dim;	// Grid and Block structure values
    if (strcmp(argv[2], "-t")==0) {
            Block_Dim = atoi(argv[3]);
            Grid_Dim = np/Block_Dim + ((np%Block_Dim)!=0);;
    	    if ((Block_Dim*Block_Dim) > 512) {
        	printf("Error -- too many threads in block, try again\n");
        	return 1;
    	    }
    }
    else {
    	    fprintf(stderr, "Usage: %s <input file> -t threads -b blocks\n", argv[0]);
            fprintf(stderr, "       -t number of threads per block in each dimension (e.g. 16)\n");
            return 0;
       	    }

    fprintf(stderr, "\nSolving Heat equation on the CPU and the GPU\n");
    fprintf(stderr, "--------------------------------------------\n");
    print_params(&param);

    fprintf(stdout, "\nExecution on CPU (sequential)\n-----------------------------\n");
    if( !initialize(&param) )
	{
	    fprintf(stderr, "Error in Solver initialization.\n\n");
            return 1;
	}

    // starting time
    float elapsed_time_ms;       	// which is applicable for asynchronous code also
    cudaEvent_t start, stop;     	// using cuda events to measure time
    cudaEventCreate( &start );     // instrument code to measure start time
    cudaEventCreate( &stop );

    cudaEventRecord( start, 0 );
    cudaEventSynchronize( start );

    iter = 0;
    float residual;
    while(1) {
	residual = cpu_jacobi(param.u, param.uhelp, np, np);
	float * tmp = param.u;
	param.u = param.uhelp;
	param.uhelp = tmp;
	//residual = cpu_residual (param.u, param.uhelp, np, np);
   //printf("residual: %.6f \n", residual);
        iter++;

        // solution good enough ?
        if (residual < 0.00005) break;

        // max. iteration reached ? (no limit with maxiter=0)
        if (iter>=param.maxiter) break;
    }

    cudaEventRecord( stop, 0 );     // instrument code to measue end time
    cudaEventSynchronize( stop );
    cudaEventElapsedTime( &elapsed_time_ms, start, stop );

    // Flop count after iter iterations
    float flop = iter * 11.0 * param.resolution * param.resolution;

    fprintf(stdout, "Time on CPU in ms.= %f ", elapsed_time_ms);
    fprintf(stdout, "(%3.3f GFlop => %6.2f MFlop/s)\n", 
	    flop/1000000000.0,
	    flop/elapsed_time_ms/1000);
    fprintf(stdout, "Convergence to residual=%f: %d iterations\n", residual, iter);

    finalize( &param );

    fprintf(stdout, "\nExecution on GPU\n----------------\n");
    fprintf(stderr, "Number of threads per block in each dimension = %d\n", Block_Dim);
    fprintf(stderr, "Number of blocks per grid in each dimension   = %d\n", Grid_Dim);

    if( !initialize(&param) )
	{
	    fprintf(stderr, "Error in Solver initialization.\n\n");
            return 1;
	}

    dim3 Grid(Grid_Dim, Grid_Dim);
    dim3 Block(Block_Dim, Block_Dim);
    
    // starting time
    cudaEventRecord( start, 0 );
    cudaEventSynchronize( start );

    float *dev_u, *dev_uhelp, *dev_res, *res, *result, *dev_result;
	 res = (float*)calloc(sizeof(float), np*np);
	 result = (float*)calloc(sizeof(float), np);
    // TODO: Allocation on GPU for matrices u and uhelp

    cudaMalloc( &dev_u, sizeof(float)*(np*np));
    cudaMalloc( &dev_uhelp, sizeof(float)*(np*np));
    cudaMalloc( &dev_res, sizeof(float)*(np*np));
	 cudaMalloc( &dev_result, sizeof(float)*np);
    // TODO: Copy initial values in u and uhelp from host to GPU
    

	 cudaMemcpy( dev_u,param.u, sizeof(float)*(np*np), cudaMemcpyHostToDevice);
	 cudaMemcpy( dev_uhelp, param.uhelp, sizeof(float)*(np*np), cudaMemcpyHostToDevice);
	 cudaMemcpy( dev_res, res, sizeof(float)*(np*np), cudaMemcpyHostToDevice);
	 cudaMemcpy( dev_result, result, sizeof(float)*(np), cudaMemcpyHostToDevice); 
    iter = 0;
  	 
    while(1) {
     gpu_Heat<<<Grid,Block>>>(dev_u, dev_uhelp, dev_res, np);
     cudaThreadSynchronize();                        // wait for all threads to complete

     // TODO: residual is computed on host, we need to get from GPU values computed in u and uhelp 
     cudaMemcpy( res, dev_res, sizeof(float)*(np*np), cudaMemcpyDeviceToHost);
	
	  //for(int i=0;i<np;i++) { printf("%.6f ", res[i*2]); }	  
	
	  gpu_HeatReduction<<<np,np,np*sizeof(float)>>>(dev_res, dev_result);
	  cudaThreadSynchronize();
   
	  cudaMemcpy( result, dev_result, sizeof(float)*np, cudaMemcpyDeviceToHost);

     
		//cudaMemcpy( param.uhelp, dev_uhelp, sizeof(float)*(np*np), cudaMemcpyDeviceToHost);
   	//residual = cpu_residual(param.u, param.uhelp, np, np);

	float * tmp = dev_u;
	dev_u = dev_uhelp;
	dev_uhelp = tmp;

   iter++;

	float sum =0.0;
	for(int i=0;i<np;i++) {
	//		printf("Result[%d]=%.6f\n",i,result[i]);	
			sum += result[i]; 		
	
	}
	residual = sum; 
	
        // solution good enough ?
       if (residual < 0.00005) break;

        // max. iteration reached ? (no limit with maxiter=0)
        if (iter>=param.maxiter) break;
    }

    // TODO: get result matrix from GPU
    cudaMemcpy(  param.u, dev_u, sizeof(float)*(np*np), cudaMemcpyDeviceToHost);
  
    // TODO: free memory used in GPU
    cudaFree( dev_u ); cudaFree( dev_uhelp);

    cudaEventRecord( stop, 0 );     // instrument code to measue end time
    cudaEventSynchronize( stop );
    cudaEventElapsedTime( &elapsed_time_ms, start, stop );

    fprintf(stdout, "\nTime on GPU in ms. = %f ", elapsed_time_ms);
    fprintf(stdout, "(%3.3f GFlop => %6.2f MFlop/s)\n", 
	    flop/1000000000.0,
	    flop/elapsed_time_ms/1000);
    fprintf(stdout, "Convergence to residual=%f: %d iterations\n", residual, iter);

    cudaEventDestroy(start);
    cudaEventDestroy(stop);

    // for plot...
    coarsen( param.u, np, np,
	     param.uvis, param.visres+2, param.visres+2 );
  
    write_image( resfile, param.uvis,  
		 param.visres+2, 
		 param.visres+2 );

    finalize( &param );

    return 0;
}


/*
 * Initialize the iterative solver
 * - allocate memory for matrices
 * - set boundary conditions according to configuration
 */
int initialize( algoparam_t *param )

{
    int i, j;
    float dist;

    // total number of points (including border)
    const int np = param->resolution + 2;
  
    //
    // allocate memory
    //
    (param->u)     = (float*)calloc( sizeof(float),np*np );
    (param->uhelp) = (float*)calloc( sizeof(float),np*np );
    (param->uvis)  = (float*)calloc( sizeof(float),
				      (param->visres+2) *
				      (param->visres+2) );
  

    if( !(param->u) || !(param->uhelp) || !(param->uvis) )
    {
	fprintf(stderr, "Error: Cannot allocate memory\n");
	return 0;
    }

    for( i=0; i<param->numsrcs; i++ )
    {
	/* top row */
	for( j=0; j<np; j++ )
	{
	    dist = sqrt( pow((float)j/(float)(np-1) - 
			     param->heatsrcs[i].posx, 2)+
			 pow(param->heatsrcs[i].posy, 2));
	  
	    if( dist <= param->heatsrcs[i].range )
	    {
		(param->u)[j] +=
		    (param->heatsrcs[i].range-dist) /
		    param->heatsrcs[i].range *
		    param->heatsrcs[i].temp;
	    }
	}
      
	/* bottom row */
	for( j=0; j<np; j++ )
	{
	    dist = sqrt( pow((float)j/(float)(np-1) - 
			     param->heatsrcs[i].posx, 2)+
			 pow(1-param->heatsrcs[i].posy, 2));
	  
	    if( dist <= param->heatsrcs[i].range )
	    {
		(param->u)[(np-1)*np+j]+=
		    (param->heatsrcs[i].range-dist) / 
		    param->heatsrcs[i].range * 
		    param->heatsrcs[i].temp;
	    }
	}
      
	/* leftmost column */
	for( j=1; j<np-1; j++ )
	{
	    dist = sqrt( pow(param->heatsrcs[i].posx, 2)+
			 pow((float)j/(float)(np-1) - 
			     param->heatsrcs[i].posy, 2)); 
	  
	    if( dist <= param->heatsrcs[i].range )
	    {
		(param->u)[ j*np ]+=
		    (param->heatsrcs[i].range-dist) / 
		    param->heatsrcs[i].range *
		    param->heatsrcs[i].temp;
	    }
	}
      
	/* rightmost column */
	for( j=1; j<np-1; j++ )
	{
	    dist = sqrt( pow(1-param->heatsrcs[i].posx, 2)+
			 pow((float)j/(float)(np-1) - 
			     param->heatsrcs[i].posy, 2)); 
	  
	    if( dist <= param->heatsrcs[i].range )
	    {
		(param->u)[ j*np+(np-1) ]+=
		    (param->heatsrcs[i].range-dist) /
		    param->heatsrcs[i].range *
		    param->heatsrcs[i].temp;
	    }
	}
    }

    // Copy u into uhelp
    float *putmp, *pu;
    pu = param->u;
    putmp = param->uhelp;
    for( j=0; j<np; j++ )
	for( i=0; i<np; i++ )
	    *putmp++ = *pu++;

    return 1;
}

/*
 * free used memory
 */
int finalize( algoparam_t *param )
{
    if( param->u ) {
	free(param->u);
	param->u = 0;
    }

    if( param->uhelp ) {
	free(param->uhelp);
	param->uhelp = 0;
    }

    if( param->uvis ) {
	free(param->uvis);
	param->uvis = 0;
    }

    return 1;
}


/*
 * write the given temperature u matrix to rgb values
 * and write the resulting image to file f
 */
void write_image( FILE * f, float *u,
		  unsigned sizex, unsigned sizey ) 
{
    // RGB table
    unsigned char r[1024], g[1024], b[1024];
    int i, j, k;
  
    float min, max;

    j=1023;

    // prepare RGB table
    for( i=0; i<256; i++ )
    {
	r[j]=255; g[j]=i; b[j]=0;
	j--;
    }
    for( i=0; i<256; i++ )
    {
	r[j]=255-i; g[j]=255; b[j]=0;
	j--;
    }
    for( i=0; i<256; i++ )
    {
	r[j]=0; g[j]=255; b[j]=i;
	j--;
    }
    for( i=0; i<256; i++ )
    {
	r[j]=0; g[j]=255-i; b[j]=255;
	j--;
    }

    min=DBL_MAX;
    max=-DBL_MAX;

    // find minimum and maximum 
    for( i=0; i<sizey; i++ )
    {
	for( j=0; j<sizex; j++ )
	{
	    if( u[i*sizex+j]>max )
		max=u[i*sizex+j];
	    if( u[i*sizex+j]<min )
		min=u[i*sizex+j];
	}
    }
  

    fprintf(f, "P3\n");
    fprintf(f, "%u %u\n", sizex, sizey);
    fprintf(f, "%u\n", 255);

    for( i=0; i<sizey; i++ )
    {
	for( j=0; j<sizex; j++ )
	{
	    k=(int)(1023.0*(u[i*sizex+j]-min)/(max-min));
	    fprintf(f, "%d %d %d  ", r[k], g[k], b[k]);
	}
	fprintf(f, "\n");
    }
}


int coarsen( float *uold, unsigned oldx, unsigned oldy ,
	     float *unew, unsigned newx, unsigned newy )
{
    int i, j;

    int stepx;
    int stepy;
    int stopx = newx;
    int stopy = newy;

    if (oldx>newx)
	stepx=oldx/newx;
    else {
	stepx=1;
	stopx=oldx;
    }
    if (oldy>newy)
	stepy=oldy/newy;
    else {
	stepy=1;
	stopy=oldy;
    }

    // NOTE: this only takes the top-left corner,
    // and doesnt' do any real coarsening 
    for( i=0; i<stopy-1; i++ )
    {
	for( j=0; j<stopx-1; j++ )
        {
	    unew[i*newx+j]=uold[i*oldx*stepy+j*stepx];
        }
    }

  return 1;
}

#define BUFSIZE 100
int read_input( FILE *infile, algoparam_t *param )
{
  int i, n;
  char buf[BUFSIZE];

  fgets(buf, BUFSIZE, infile);
  n = sscanf( buf, "%u", &(param->maxiter) );
  if( n!=1)
    return 0;

  fgets(buf, BUFSIZE, infile);
  n = sscanf( buf, "%u", &(param->resolution) );
  if( n!=1 )
    return 0;

  param->visres = param->resolution;

  fgets(buf, BUFSIZE, infile);
  n = sscanf(buf, "%u", &(param->numsrcs) );
  if( n!=1 )
    return 0;

  (param->heatsrcs) = 
    (heatsrc_t*) malloc( sizeof(heatsrc_t) * (param->numsrcs) );
  
  for( i=0; i<param->numsrcs; i++ )
    {
      fgets(buf, BUFSIZE, infile);
      n = sscanf( buf, "%f %f %f %f",
		  &(param->heatsrcs[i].posx),
		  &(param->heatsrcs[i].posy),
		  &(param->heatsrcs[i].range),
		  &(param->heatsrcs[i].temp) );

      if( n!=4 )
	return 0;
    }

  return 1;
}


void print_params( algoparam_t *param )
{
  int i;

  fprintf(stdout, "Iterations        : %u\n", param->maxiter);
  fprintf(stdout, "Resolution        : %u\n", param->resolution);
  fprintf(stdout, "Num. Heat sources : %u\n", param->numsrcs);

  for( i=0; i<param->numsrcs; i++ )
    {
      fprintf(stdout, "  %2d: (%2.2f, %2.2f) %2.2f %2.2f \n",
	     i+1,
	     param->heatsrcs[i].posx,
	     param->heatsrcs[i].posy,
	     param->heatsrcs[i].range,
	     param->heatsrcs[i].temp );
    }
}


	.file	"tmpxft_0022abbe_00000000-6_heatCUDA.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2039:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2039:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z12cpu_residualPfS_jj
	.type	_Z12cpu_residualPfS_jj, @function
_Z12cpu_residualPfS_jj:
.LFB2027:
	.cfi_startproc
	endbr64
	movl	%ecx, %r9d
	movl	$1, %r8d
	xorps	%xmm1, %xmm1
	decl	%edx
	leal	-1(%rcx), %r11d
.L3:
	cmpl	%edx, %r8d
	jnb	.L8
	movl	$1, %eax
.L5:
	cmpl	%r11d, %eax
	jnb	.L9
	leal	(%rax,%r9), %r10d
	incl	%eax
	movss	(%rsi,%r10,4), %xmm0
	subss	(%rdi,%r10,4), %xmm0
	mulss	%xmm0, %xmm0
	addss	%xmm0, %xmm1
	jmp	.L5
.L9:
	incl	%r8d
	addl	%ecx, %r9d
	jmp	.L3
.L8:
	movaps	%xmm1, %xmm0
	ret
	.cfi_endproc
.LFE2027:
	.size	_Z12cpu_residualPfS_jj, .-_Z12cpu_residualPfS_jj
	.globl	_Z10cpu_jacobiPfS_jj
	.type	_Z10cpu_jacobiPfS_jj, @function
_Z10cpu_jacobiPfS_jj:
.LFB2028:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	movl	%ecx, %eax
	movss	.LC1(%rip), %xmm2
	xorl	%r10d, %r10d
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	movl	%ecx, %r14d
	xorl	%r8d, %r8d
	xorps	%xmm1, %xmm1
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	shrl	$3, %r14d
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	movl	%edx, %r12d
	shrl	$3, %r12d
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	movl	%r12d, %ebx
	imull	%ecx, %ebx
	leal	-2(%rdx), %ecx
	movq	%rsi, -16(%rsp)
	movl	%ebx, -8(%rsp)
	leal	-2(%rax), %ebx
	movl	$8, -44(%rsp)
	movl	%ecx, -48(%rsp)
	movl	%ebx, -52(%rsp)
.L11:
	leal	1(%r8), %ecx
	movl	-48(%rsp), %edx
	addl	%r12d, %r8d
	cmpl	%r8d, -48(%rsp)
	movl	%ecx, -24(%rsp)
	leal	(%rax,%r10), %ecx
	movl	%eax, %r11d
	cmova	%r8d, %edx
	movl	$8, -40(%rsp)
	movl	%ecx, -4(%rsp)
	movl	%edx, -28(%rsp)
	xorl	%edx, %edx
.L16:
	leal	1(%rdx), %ecx
	movl	-52(%rsp), %ebx
	movl	%r10d, %ebp
	movl	%ecx, -20(%rsp)
	movl	%edx, %ecx
	addl	%r14d, %edx
	cmpl	%edx, -52(%rsp)
	cmova	%edx, %ebx
	movl	%ebx, -32(%rsp)
	movl	-4(%rsp), %ebx
	addl	%ebx, %ecx
	movl	-24(%rsp), %ebx
.L12:
	cmpl	%ebx, -28(%rsp)
	jb	.L20
	leal	(%rcx,%r11), %esi
	movl	-20(%rsp), %r9d
	movl	%esi, -36(%rsp)
	movl	%ecx, %esi
.L13:
	leal	1(%rsi), %r15d
	movl	%r15d, -56(%rsp)
	cmpl	%r9d, -32(%rsp)
	jb	.L21
	leal	2(%rsi), %r15d
	movl	-56(%rsp), %r13d
	movss	(%rdi,%r15,4), %xmm0
	addss	(%rdi,%rsi,4), %xmm0
	leal	(%r9,%rbp), %esi
	addss	(%rdi,%rsi,4), %xmm0
	movl	-36(%rsp), %esi
	addl	%r9d, %esi
	incl	%r9d
	movl	%esi, %esi
	addss	(%rdi,%rsi,4), %xmm0
	movq	-16(%rsp), %rsi
	mulss	%xmm2, %xmm0
	movss	%xmm0, (%rsi,%r13,4)
	subss	(%rdi,%r13,4), %xmm0
	movl	-56(%rsp), %esi
	mulss	%xmm0, %xmm0
	addss	%xmm0, %xmm1
	jmp	.L13
.L21:
	incl	%ebx
	addl	%eax, %ecx
	addl	%eax, %ebp
	jmp	.L12
.L20:
	subl	%r14d, %r11d
	decl	-40(%rsp)
	jne	.L16
	movl	-8(%rsp), %edx
	addl	%edx, %r10d
	decl	-44(%rsp)
	jne	.L11
	popq	%rbx
	.cfi_def_cfa_offset 48
	movaps	%xmm1, %xmm0
	popq	%rbp
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2028:
	.size	_Z10cpu_jacobiPfS_jj, .-_Z10cpu_jacobiPfS_jj
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC2:
	.string	"Usage: %s <input file> -t threads -b blocks\n"
.LC3:
	.string	"       -t number of threads per block in each dimension (e.g. 16)\n"
	.text
	.globl	_Z5usagePc
	.type	_Z5usagePc, @function
_Z5usagePc:
.LFB2029:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	movq	%rdi, %rcx
	movq	stderr(%rip), %rdi
	movl	$2, %esi
	leaq	.LC2(%rip), %rdx
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	movq	stderr(%rip), %rdi
	leaq	.LC3(%rip), %rdx
	xorl	%eax, %eax
	movl	$2, %esi
	popq	%rcx
	.cfi_def_cfa_offset 8
	jmp	__fprintf_chk@PLT
	.cfi_endproc
.LFE2029:
	.size	_Z5usagePc, .-_Z5usagePc
	.section	.rodata.str1.1
.LC4:
	.string	"Error: Cannot allocate memory\n"
	.text
	.globl	_Z10initializeP11algoparam_t
	.type	_Z10initializeP11algoparam_t, @function
_Z10initializeP11algoparam_t:
.LFB2031:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	movq	%rdi, %rbp
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	subq	$40, %rsp
	.cfi_def_cfa_offset 96
	movl	4(%rdi), %r14d
	movl	$4, %edi
	leal	2(%r14), %r15d
	movl	%r15d, %r12d
	imull	%r15d, %r12d
	movslq	%r12d, %r12
	movq	%r12, %rsi
	call	calloc@PLT
	movq	%r12, %rsi
	movl	$4, %edi
	movq	%rax, 16(%rbp)
	movq	%rax, %rbx
	call	calloc@PLT
	movl	$4, %edi
	movq	%rax, 24(%rbp)
	movq	%rax, %r12
	movl	12(%rbp), %eax
	leal	2(%rax), %esi
	imull	%esi, %esi
	call	calloc@PLT
	testq	%r12, %r12
	sete	%cl
	testq	%rbx, %rbx
	movq	%rax, 32(%rbp)
	sete	%sil
	orb	%sil, %cl
	jne	.L25
	testq	%rax, %rax
	je	.L25
	incl	%r14d
	movl	%r15d, %eax
	imull	%r14d, %eax
	movl	%eax, 24(%rsp)
	xorl	%eax, %eax
	movq	%rax, (%rsp)
	jmp	.L26
.L25:
	movq	stderr(%rip), %rdi
	leaq	.LC4(%rip), %rdx
	movl	$2, %esi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	xorl	%eax, %eax
	jmp	.L24
.L30:
	cvtsi2ssl	%ecx, %xmm0
	movq	48(%rbp), %rsi
	movq	%rcx, 16(%rsp)
	cvtsi2ssl	%r14d, %xmm1
	addq	%r13, %rsi
	movq	%rsi, 8(%rsp)
	divss	%xmm1, %xmm0
	cvtss2sd	4(%rsi), %xmm1
	mulsd	%xmm1, %xmm1
	subss	(%rsi), %xmm0
	cvtss2sd	%xmm0, %xmm0
	mulsd	%xmm0, %xmm0
	addsd	%xmm1, %xmm0
	call	sqrt@PLT
	movq	8(%rsp), %rsi
	movq	16(%rsp), %rcx
	cvtsd2ss	%xmm0, %xmm1
	movss	8(%rsi), %xmm2
	comiss	%xmm1, %xmm2
	jb	.L28
	movaps	%xmm2, %xmm0
	subss	%xmm1, %xmm0
	divss	%xmm2, %xmm0
	mulss	12(%rsi), %xmm0
	addss	(%rbx,%rcx,4), %xmm0
	movss	%xmm0, (%rbx,%rcx,4)
.L28:
	incq	%rcx
.L45:
	cmpl	%ecx, %r15d
	jg	.L30
	xorl	%ecx, %ecx
.L31:
	cmpl	%r15d, %ecx
	jl	.L34
	movl	%r15d, %esi
	movl	$1, %ecx
.L35:
	cmpl	%ecx, %r14d
	jg	.L38
	leal	(%r15,%r14), %esi
	movl	$1, %ecx
.L39:
	cmpl	%ecx, %r14d
	jg	.L42
	incq	(%rsp)
.L26:
	movl	(%rsp), %eax
	cmpl	40(%rbp), %eax
	jb	.L43
	movl	%r14d, %eax
	testl	%r15d, %r15d
	movl	$4, %ecx
	leaq	4(,%rax,4), %rax
	cmovle	%rcx, %rax
	xorl	%r8d, %r8d
	jmp	.L44
.L43:
	movq	(%rsp), %r13
	xorl	%ecx, %ecx
	salq	$4, %r13
	jmp	.L45
.L34:
	cvtsi2ssl	%ecx, %xmm0
	movq	48(%rbp), %rsi
	movl	%ecx, 16(%rsp)
	cvtsi2ssl	%r14d, %xmm1
	addq	%r13, %rsi
	movq	%rsi, 8(%rsp)
	divss	%xmm1, %xmm0
	movss	.LC5(%rip), %xmm1
	subss	4(%rsi), %xmm1
	cvtss2sd	%xmm1, %xmm1
	mulsd	%xmm1, %xmm1
	subss	(%rsi), %xmm0
	cvtss2sd	%xmm0, %xmm0
	mulsd	%xmm0, %xmm0
	addsd	%xmm1, %xmm0
	call	sqrt@PLT
	movq	8(%rsp), %rsi
	movl	16(%rsp), %ecx
	cvtsd2ss	%xmm0, %xmm1
	movss	8(%rsi), %xmm2
	comiss	%xmm1, %xmm2
	jb	.L32
	movaps	%xmm2, %xmm0
	movl	24(%rsp), %eax
	subss	%xmm1, %xmm0
	leal	(%rcx,%rax), %edi
	movslq	%edi, %rdi
	divss	%xmm2, %xmm0
	leaq	(%rbx,%rdi,4), %rdi
	mulss	12(%rsi), %xmm0
	addss	(%rdi), %xmm0
	movss	%xmm0, (%rdi)
.L32:
	incl	%ecx
	jmp	.L31
.L38:
	cvtsi2ssl	%ecx, %xmm1
	movq	48(%rbp), %rdi
	movl	%esi, 28(%rsp)
	cvtsi2ssl	%r14d, %xmm3
	movl	%ecx, 16(%rsp)
	addq	%r13, %rdi
	cvtss2sd	(%rdi), %xmm2
	mulsd	%xmm2, %xmm2
	movq	%rdi, 8(%rsp)
	movaps	%xmm1, %xmm0
	divss	%xmm3, %xmm0
	subss	4(%rdi), %xmm0
	cvtss2sd	%xmm0, %xmm0
	mulsd	%xmm0, %xmm0
	addsd	%xmm2, %xmm0
	call	sqrt@PLT
	movq	8(%rsp), %rdi
	movl	16(%rsp), %ecx
	cvtsd2ss	%xmm0, %xmm1
	movl	28(%rsp), %esi
	movss	8(%rdi), %xmm2
	comiss	%xmm1, %xmm2
	jb	.L36
	movaps	%xmm2, %xmm0
	movslq	%esi, %r8
	subss	%xmm1, %xmm0
	leaq	(%rbx,%r8,4), %r8
	divss	%xmm2, %xmm0
	mulss	12(%rdi), %xmm0
	addss	(%r8), %xmm0
	movss	%xmm0, (%r8)
.L36:
	incl	%ecx
	addl	%r15d, %esi
	jmp	.L35
.L42:
	cvtsi2ssl	%ecx, %xmm1
	movq	48(%rbp), %rdi
	movl	%esi, 28(%rsp)
	cvtsi2ssl	%r14d, %xmm3
	movss	.LC5(%rip), %xmm2
	movl	%ecx, 16(%rsp)
	addq	%r13, %rdi
	subss	(%rdi), %xmm2
	movq	%rdi, 8(%rsp)
	movaps	%xmm1, %xmm0
	divss	%xmm3, %xmm0
	cvtss2sd	%xmm2, %xmm2
	mulsd	%xmm2, %xmm2
	subss	4(%rdi), %xmm0
	cvtss2sd	%xmm0, %xmm0
	mulsd	%xmm0, %xmm0
	addsd	%xmm2, %xmm0
	call	sqrt@PLT
	movq	8(%rsp), %rdi
	movl	16(%rsp), %ecx
	cvtsd2ss	%xmm0, %xmm1
	movl	28(%rsp), %esi
	movss	8(%rdi), %xmm2
	comiss	%xmm1, %xmm2
	jb	.L40
	movaps	%xmm2, %xmm0
	movslq	%esi, %r8
	subss	%xmm1, %xmm0
	leaq	(%rbx,%r8,4), %r8
	divss	%xmm2, %xmm0
	mulss	12(%rdi), %xmm0
	addss	(%r8), %xmm0
	movss	%xmm0, (%r8)
.L40:
	incl	%ecx
	addl	%r15d, %esi
	jmp	.L39
.L44:
	cmpl	%r15d, %r8d
	jge	.L52
	movq	%r12, %rdi
	movq	%rbx, %rsi
	movq	%rax, %rcx
	incl	%r8d
	rep movsb
	movq	%rsi, %rbx
	movq	%rdi, %r12
	jmp	.L44
.L52:
	movl	$1, %eax
.L24:
	addq	$40, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%rbp
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2031:
	.size	_Z10initializeP11algoparam_t, .-_Z10initializeP11algoparam_t
	.globl	_Z8finalizeP11algoparam_t
	.type	_Z8finalizeP11algoparam_t, @function
_Z8finalizeP11algoparam_t:
.LFB2032:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	movq	%rdi, %rbx
	movq	16(%rdi), %rdi
	testq	%rdi, %rdi
	je	.L54
	call	free@PLT
	xorl	%ecx, %ecx
	movq	%rcx, 16(%rbx)
.L54:
	movq	24(%rbx), %rdi
	testq	%rdi, %rdi
	je	.L55
	call	free@PLT
	xorl	%edx, %edx
	movq	%rdx, 24(%rbx)
.L55:
	movq	32(%rbx), %rdi
	testq	%rdi, %rdi
	je	.L56
	call	free@PLT
	xorl	%eax, %eax
	movq	%rax, 32(%rbx)
.L56:
	movl	$1, %eax
	popq	%rbx
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2032:
	.size	_Z8finalizeP11algoparam_t, .-_Z8finalizeP11algoparam_t
	.section	.rodata.str1.1
.LC8:
	.string	"P3\n"
.LC9:
	.string	"%u %u\n"
.LC10:
	.string	"%u\n"
.LC12:
	.string	"%d %d %d  "
.LC13:
	.string	"\n"
	.text
	.globl	_Z11write_imageP8_IO_FILEPfjj
	.type	_Z11write_imageP8_IO_FILEPfjj, @function
_Z11write_imageP8_IO_FILEPfjj:
.LFB2033:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	movq	%rsi, %r13
	movl	%ecx, %esi
	movl	$256, %ecx
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	movl	%edx, %r12d
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	subq	$3112, %rsp
	.cfi_def_cfa_offset 3168
	movq	%rdi, (%rsp)
	leaq	792(%rsp), %rdx
	leaq	1048(%rsp), %r10
	movq	%rdx, %rdi
	movl	$1023, %edx
	movq	%fs:40, %rax
	movq	%rax, 3096(%rsp)
	xorl	%eax, %eax
	movb	$-1, %al
	rep stosb
.L68:
	movl	%edx, %eax
	decq	%rdx
	notl	%eax
	movb	%al, 1(%rdx,%r10)
	cmpq	$767, %rdx
	jne	.L68
	leaq	2840(%rsp), %r8
	movl	$64, %ecx
	xorl	%eax, %eax
	movq	%r8, %rdi
	leaq	24(%rsp), %r9
	rep stosl
.L69:
	movb	%dl, (%rdx,%r9)
	decq	%rdx
	cmpq	$511, %rdx
	jne	.L69
	movb	$-1, %r8b
	leaq	1560(%rsp), %r11
	movl	$256, %ecx
	movl	%r8d, %eax
	movq	%r11, %rdi
	leaq	2584(%rsp), %r11
	rep stosb
	xorl	%eax, %eax
	movq	%r11, %rdi
	movl	$64, %ecx
	rep stosl
	leaq	280(%rsp), %r11
	movl	$64, %ecx
	movq	%r11, %rdi
	leaq	1304(%rsp), %r11
	rep stosl
	movl	%r8d, %eax
	movl	$256, %ecx
	movq	%r11, %rdi
	rep stosb
	leaq	2072(%rsp), %r8
.L70:
	movl	%edx, %eax
	decq	%rdx
	notl	%eax
	movb	%al, 1(%rdx,%r8)
	cmpq	$255, %rdx
	jne	.L70
	movl	$64, %ecx
	xorl	%eax, %eax
	movq	%r9, %rdi
	rep stosl
.L71:
	movb	%dl, (%r10,%rdx)
	subq	$1, %rdx
	jnb	.L71
	movl	$256, %ecx
	movb	$-1, %al
	movq	%r8, %rdi
	xorl	%edx, %edx
	rep stosb
	movss	.LC6(%rip), %xmm3
	movss	.LC7(%rip), %xmm4
	xorl	%ebx, %ebx
	movss	%xmm3, 12(%rsp)
	movss	%xmm4, 8(%rsp)
.L72:
	cmpl	%esi, %ebx
	je	.L76
	xorl	%eax, %eax
.L77:
	cmpl	%r12d, %eax
	je	.L89
	leal	(%rax,%rdx), %ecx
	incl	%eax
	movss	0(%r13,%rcx,4), %xmm0
	movaps	%xmm0, %xmm2
	minss	8(%rsp), %xmm0
	maxss	12(%rsp), %xmm2
	movss	%xmm0, 8(%rsp)
	movss	%xmm2, 12(%rsp)
	jmp	.L77
.L89:
	incl	%ebx
	addl	%r12d, %edx
	jmp	.L72
.L76:
	movq	(%rsp), %rdi
	leaq	.LC8(%rip), %rdx
	xorl	%eax, %eax
	xorl	%ebp, %ebp
	movl	$2, %esi
	xorl	%r14d, %r14d
	call	__fprintf_chk@PLT
	movq	(%rsp), %rdi
	movl	%ebx, %r8d
	movl	%r12d, %ecx
	leaq	.LC9(%rip), %rdx
	movl	$2, %esi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	movq	(%rsp), %rdi
	movl	$255, %ecx
	xorl	%eax, %eax
	leaq	.LC10(%rip), %rdx
	movl	$2, %esi
	call	__fprintf_chk@PLT
.L78:
	cmpq	%rbx, %r14
	je	.L90
	movl	%ebp, %r15d
.L80:
	leal	(%r12,%rbp), %eax
	cmpl	%r15d, %eax
	je	.L91
	movl	%r15d, %eax
	movss	12(%rsp), %xmm1
	movq	(%rsp), %rdi
	leaq	.LC12(%rip), %rdx
	movss	0(%r13,%rax,4), %xmm0
	subss	8(%rsp), %xmm0
	movl	$2, %esi
	incl	%r15d
	subss	8(%rsp), %xmm1
	cvtss2sd	%xmm0, %xmm0
	mulsd	.LC11(%rip), %xmm0
	cvtss2sd	%xmm1, %xmm1
	divsd	%xmm1, %xmm0
	cvttsd2sil	%xmm0, %eax
	cltq
	movzbl	2072(%rsp,%rax), %r9d
	movzbl	24(%rsp,%rax), %ecx
	movzbl	1048(%rsp,%rax), %r8d
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	jmp	.L80
.L91:
	movq	(%rsp), %rdi
	leaq	.LC13(%rip), %rdx
	xorl	%eax, %eax
	incq	%r14
	movl	$2, %esi
	movl	%r15d, %ebp
	call	__fprintf_chk@PLT
	jmp	.L78
.L90:
	movq	3096(%rsp), %rax
	subq	%fs:40, %rax
	je	.L81
	call	__stack_chk_fail@PLT
.L81:
	addq	$3112, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%rbp
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2033:
	.size	_Z11write_imageP8_IO_FILEPfjj, .-_Z11write_imageP8_IO_FILEPfjj
	.globl	_Z7coarsenPfjjS_jj
	.type	_Z7coarsenPfjjS_jj, @function
_Z7coarsenPfjjS_jj:
.LFB2034:
	.cfi_startproc
	endbr64
	movl	%esi, %r11d
	pushq	%r13
	.cfi_def_cfa_offset 16
	.cfi_offset 13, -16
	movl	%edx, %esi
	movq	%rcx, %r10
	pushq	%r12
	.cfi_def_cfa_offset 24
	.cfi_offset 12, -24
	pushq	%rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
	cmpl	%r11d, %r8d
	jnb	.L93
	movl	%r11d, %eax
	xorl	%edx, %edx
	movl	%r8d, %ecx
	divl	%r8d
	movl	%eax, %ebx
	jmp	.L94
.L93:
	movl	%r11d, %ecx
	movl	$1, %ebx
.L94:
	cmpl	%esi, %r9d
	jnb	.L95
	movl	%esi, %eax
	xorl	%edx, %edx
	divl	%r9d
	jmp	.L96
.L95:
	movl	%esi, %r9d
	movl	$1, %eax
.L96:
	imull	%r11d, %eax
	xorl	%r12d, %r12d
	xorl	%r11d, %r11d
	xorl	%ebp, %ebp
	decl	%r9d
	decl	%ecx
.L97:
	cmpl	%ebp, %r9d
	jle	.L99
	movl	%r11d, %esi
	xorl	%edx, %edx
.L100:
	cmpl	%edx, %ecx
	jle	.L102
	movl	%esi, %r13d
	addl	%ebx, %esi
	movss	(%rdi,%r13,4), %xmm0
	leal	(%rdx,%r12), %r13d
	incl	%edx
	movss	%xmm0, (%r10,%r13,4)
	jmp	.L100
.L102:
	incl	%ebp
	addl	%eax, %r11d
	addl	%r8d, %r12d
	jmp	.L97
.L99:
	popq	%rbx
	.cfi_def_cfa_offset 32
	movl	$1, %eax
	popq	%rbp
	.cfi_def_cfa_offset 24
	popq	%r12
	.cfi_def_cfa_offset 16
	popq	%r13
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2034:
	.size	_Z7coarsenPfjjS_jj, .-_Z7coarsenPfjjS_jj
	.section	.rodata.str1.1
.LC14:
	.string	"%u"
.LC15:
	.string	"%f %f %f %f"
	.text
	.globl	_Z10read_inputP8_IO_FILEP11algoparam_t
	.type	_Z10read_inputP8_IO_FILEP11algoparam_t, @function
_Z10read_inputP8_IO_FILEP11algoparam_t:
.LFB2035:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	movq	%rdi, %rcx
	movl	$100, %edx
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	leaq	.LC14(%rip), %r13
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	movq	%rdi, %r12
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	movq	%rsi, %rbx
	movl	$100, %esi
	subq	$120, %rsp
	.cfi_def_cfa_offset 176
	movq	%fs:40, %rax
	movq	%rax, 104(%rsp)
	xorl	%eax, %eax
	leaq	4(%rsp), %rbp
	movq	%rbp, %rdi
	call	__fgets_chk@PLT
	xorl	%eax, %eax
	movq	%rbx, %rdx
	movq	%r13, %rsi
	movq	%rbp, %rdi
	call	__isoc23_sscanf@PLT
	decl	%eax
	je	.L104
.L106:
	xorl	%r13d, %r13d
	jmp	.L103
.L104:
	movq	%r12, %rcx
	movl	$100, %edx
	movl	$100, %esi
	movq	%rbp, %rdi
	call	__fgets_chk@PLT
	xorl	%eax, %eax
	leaq	4(%rbx), %rdx
	movq	%r13, %rsi
	movq	%rbp, %rdi
	call	__isoc23_sscanf@PLT
	decl	%eax
	jne	.L106
	movl	4(%rbx), %eax
	movq	%r12, %rcx
	movl	$100, %edx
	movl	$100, %esi
	movq	%rbp, %rdi
	movl	%eax, 12(%rbx)
	call	__fgets_chk@PLT
	movq	%r13, %rsi
	leaq	40(%rbx), %rdx
	movq	%rbp, %rdi
	xorl	%eax, %eax
	call	__isoc23_sscanf@PLT
	movl	%eax, %r13d
	cmpl	$1, %eax
	jne	.L106
	movl	40(%rbx), %edi
	xorl	%r14d, %r14d
	leaq	.LC15(%rip), %r15
	salq	$4, %rdi
	call	malloc@PLT
	movq	%rax, 48(%rbx)
.L107:
	cmpl	40(%rbx), %r14d
	jnb	.L103
	movq	%r12, %rcx
	movl	$100, %edx
	movl	$100, %esi
	movq	%rbp, %rdi
	call	__fgets_chk@PLT
	movq	%r14, %rdx
	movq	%r15, %rsi
	movq	%rbp, %rdi
	salq	$4, %rdx
	addq	48(%rbx), %rdx
	xorl	%eax, %eax
	incq	%r14
	leaq	4(%rdx), %rcx
	leaq	12(%rdx), %r9
	leaq	8(%rdx), %r8
	call	__isoc23_sscanf@PLT
	cmpl	$4, %eax
	je	.L107
	jmp	.L106
.L103:
	movq	104(%rsp), %rax
	subq	%fs:40, %rax
	je	.L109
	call	__stack_chk_fail@PLT
.L109:
	addq	$120, %rsp
	.cfi_def_cfa_offset 56
	movl	%r13d, %eax
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%rbp
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2035:
	.size	_Z10read_inputP8_IO_FILEP11algoparam_t, .-_Z10read_inputP8_IO_FILEP11algoparam_t
	.section	.rodata.str1.1
.LC16:
	.string	"Iterations        : %u\n"
.LC17:
	.string	"Resolution        : %u\n"
.LC18:
	.string	"Num. Heat sources : %u\n"
.LC19:
	.string	"  %2d: (%2.2f, %2.2f) %2.2f %2.2f \n"
	.text
	.globl	_Z12print_paramsP11algoparam_t
	.type	_Z12print_paramsP11algoparam_t, @function
_Z12print_paramsP11algoparam_t:
.LFB2036:
	.cfi_startproc
	endbr64
	pushq	%r12
	.cfi_def_cfa_offset 16
	.cfi_offset 12, -16
	leaq	.LC16(%rip), %rdx
	movl	$2, %esi
	xorl	%eax, %eax
	pushq	%rbp
	.cfi_def_cfa_offset 24
	.cfi_offset 6, -24
	leaq	.LC19(%rip), %r12
	xorl	%ebp, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset 3, -32
	movq	%rdi, %rbx
	movl	(%rdi), %ecx
	movq	stdout(%rip), %rdi
	call	__fprintf_chk@PLT
	movl	4(%rbx), %ecx
	movl	$2, %esi
	xorl	%eax, %eax
	movq	stdout(%rip), %rdi
	leaq	.LC17(%rip), %rdx
	call	__fprintf_chk@PLT
	movl	40(%rbx), %ecx
	movl	$2, %esi
	xorl	%eax, %eax
	movq	stdout(%rip), %rdi
	leaq	.LC18(%rip), %rdx
	call	__fprintf_chk@PLT
.L112:
	cmpl	40(%rbx), %ebp
	jnb	.L115
	movq	%rbp, %rax
	movq	stdout(%rip), %rdi
	leal	1(%rbp), %ecx
	movq	%r12, %rdx
	salq	$4, %rax
	addq	48(%rbx), %rax
	movl	$2, %esi
	incq	%rbp
	cvtss2sd	(%rax), %xmm0
	cvtss2sd	12(%rax), %xmm3
	cvtss2sd	8(%rax), %xmm2
	cvtss2sd	4(%rax), %xmm1
	movb	$4, %al
	call	__fprintf_chk@PLT
	jmp	.L112
.L115:
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%rbp
	.cfi_def_cfa_offset 16
	popq	%r12
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2036:
	.size	_Z12print_paramsP11algoparam_t, .-_Z12print_paramsP11algoparam_t
	.section	.rodata.str1.1
.LC20:
	.string	"r"
.LC21:
	.string	"\nError: Cannot open \"%s\" for reading.\n\n"
.LC22:
	.string	"w"
.LC23:
	.string	"heat.ppm"
.LC24:
	.string	"\nError: Cannot open \"%s\" for writing.\n\n"
.LC25:
	.string	"\nError: Error parsing input file.\n\n"
.LC26:
	.string	"-t"
.LC27:
	.string	"Error -- too many threads in block, try again\n"
.LC28:
	.string	"\nSolving Heat equation on the CPU and the GPU\n"
.LC29:
	.string	"--------------------------------------------\n"
.LC30:
	.string	"\nExecution on CPU (sequential)\n-----------------------------\n"
.LC31:
	.string	"Error in Solver initialization.\n\n"
.LC34:
	.string	"Time on CPU in ms.= %f "
.LC37:
	.string	"(%3.3f GFlop => %6.2f MFlop/s)\n"
.LC38:
	.string	"Convergence to residual=%f: %d iterations\n"
.LC39:
	.string	"\nExecution on GPU\n----------------\n"
.LC40:
	.string	"Number of threads per block in each dimension = %d\n"
.LC41:
	.string	"Number of blocks per grid in each dimension   = %d\n"
.LC42:
	.string	"\nTime on GPU in ms. = %f "
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2030:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	movq	%rsi, %rbp
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	subq	$232, %rsp
	.cfi_def_cfa_offset 288
	movq	%fs:40, %rax
	movq	%rax, 216(%rsp)
	xorl	%eax, %eax
	cmpl	$3, %edi
	jle	.L144
	movq	8(%rsi), %rdi
	leaq	.LC20(%rip), %rsi
	call	fopen@PLT
	movq	%rax, %rbx
	testq	%rax, %rax
	jne	.L119
	movq	8(%rbp), %rcx
	leaq	.LC21(%rip), %rdx
.L146:
	movq	stderr(%rip), %rdi
	movl	$2, %esi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
.L144:
	movq	0(%rbp), %rdi
	call	_Z5usagePc
.L118:
	movl	$1, %eax
	jmp	.L116
.L119:
	leaq	.LC23(%rip), %r12
	leaq	.LC22(%rip), %rsi
	movq	%r12, %rdi
	call	fopen@PLT
	movq	%r12, %rcx
	leaq	.LC24(%rip), %rdx
	movq	%rax, %r14
	testq	%rax, %rax
	je	.L146
	leaq	160(%rsp), %r13
	movq	%rbx, %rdi
	movq	%r13, %rsi
	call	_Z10read_inputP8_IO_FILEP11algoparam_t
	testl	%eax, %eax
	jne	.L122
	movq	stderr(%rip), %rdi
	leaq	.LC25(%rip), %rdx
	movl	$2, %esi
	call	__fprintf_chk@PLT
	jmp	.L144
.L122:
	movq	16(%rbp), %rdi
	leaq	.LC26(%rip), %rsi
	movl	164(%rsp), %ebx
	call	strcmp@PLT
	testl	%eax, %eax
	jne	.L123
	movq	24(%rbp), %rdi
	call	atoi@PLT
	movl	%eax, %ebp
	imull	%eax, %eax
	cmpl	$512, %eax
	jle	.L124
	leaq	.LC27(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	jmp	.L118
.L123:
	movq	0(%rbp), %rcx
	movq	stderr(%rip), %rdi
	leaq	.LC2(%rip), %rdx
	xorl	%eax, %eax
	movl	$2, %esi
	call	__fprintf_chk@PLT
	movq	stderr(%rip), %rdi
	leaq	.LC3(%rip), %rdx
	xorl	%eax, %eax
	movl	$2, %esi
	call	__fprintf_chk@PLT
	jmp	.L125
.L124:
	movq	stderr(%rip), %rdi
	leaq	.LC28(%rip), %rdx
	movl	$2, %esi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	movq	stderr(%rip), %rdi
	leaq	.LC29(%rip), %rdx
	xorl	%eax, %eax
	movl	$2, %esi
	call	__fprintf_chk@PLT
	movq	%r13, %rdi
	call	_Z12print_paramsP11algoparam_t
	movq	stdout(%rip), %rdi
	xorl	%eax, %eax
	leaq	.LC30(%rip), %rdx
	movl	$2, %esi
	call	__fprintf_chk@PLT
	movq	%r13, %rdi
	call	_Z10initializeP11algoparam_t
	testl	%eax, %eax
	je	.L145
	leaq	64(%rsp), %rdi
	addl	$2, %ebx
	xorl	%r15d, %r15d
	call	cudaEventCreate@PLT
	leaq	72(%rsp), %rdi
	call	cudaEventCreate@PLT
	movq	64(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movq	64(%rsp), %rdi
	call	cudaEventSynchronize@PLT
	movsd	.LC32(%rip), %xmm3
.L128:
	movq	184(%rsp), %r12
	movl	%ebx, %ecx
	movl	%ebx, %edx
	incl	%r15d
	movq	176(%rsp), %rdi
	movq	%r12, %rsi
	call	_Z10cpu_jacobiPfS_jj
	movq	%r12, 176(%rsp)
	cvtss2sd	%xmm0, %xmm2
	comisd	%xmm2, %xmm3
	movsd	%xmm2, (%rsp)
	movq	%rdi, 184(%rsp)
	ja	.L127
	cmpl	160(%rsp), %r15d
	jb	.L128
.L127:
	movl	%ebx, %eax
	movq	72(%rsp), %rdi
	cltd
	idivl	%ebp
	cmpl	$1, %edx
	sbbl	$-1, %eax
	xorl	%esi, %esi
	movl	%eax, %r12d
	call	cudaEventRecord@PLT
	movq	72(%rsp), %rdi
	call	cudaEventSynchronize@PLT
	leaq	60(%rsp), %rax
	movq	72(%rsp), %rdx
	movq	64(%rsp), %rsi
	movq	%rax, %rdi
	movq	%rax, 40(%rsp)
	call	cudaEventElapsedTime@PLT
	movl	164(%rsp), %eax
	movl	$2, %esi
	movq	stdout(%rip), %rdi
	leaq	.LC34(%rip), %rdx
	cvtsi2sdq	%rax, %xmm1
	movl	%r15d, %eax
	cvtsi2sdq	%rax, %xmm0
	mulsd	.LC33(%rip), %xmm0
	movb	$1, %al
	mulsd	%xmm1, %xmm0
	mulsd	%xmm1, %xmm0
	cvtsd2ss	%xmm0, %xmm5
	cvtss2sd	60(%rsp), %xmm0
	movss	%xmm5, 20(%rsp)
	call	__fprintf_chk@PLT
	movq	stdout(%rip), %rdi
	leaq	.LC37(%rip), %rdx
	movb	$2, %al
	movl	$2, %esi
	movss	20(%rsp), %xmm1
	cvtss2sd	20(%rsp), %xmm0
	divss	60(%rsp), %xmm1
	divsd	.LC35(%rip), %xmm0
	divss	.LC36(%rip), %xmm1
	movsd	%xmm0, 24(%rsp)
	cvtss2sd	%xmm1, %xmm1
	call	__fprintf_chk@PLT
	movsd	(%rsp), %xmm0
	movl	%r15d, %ecx
	movb	$1, %al
	movq	stdout(%rip), %rdi
	leaq	.LC38(%rip), %rdx
	movl	$2, %esi
	call	__fprintf_chk@PLT
	movq	%r13, %rdi
	call	_Z8finalizeP11algoparam_t
	movq	stdout(%rip), %rdi
	leaq	.LC39(%rip), %rdx
	xorl	%eax, %eax
	movl	$2, %esi
	call	__fprintf_chk@PLT
	movq	stderr(%rip), %rdi
	movl	%ebp, %ecx
	xorl	%eax, %eax
	leaq	.LC40(%rip), %rdx
	movl	$2, %esi
	call	__fprintf_chk@PLT
	xorl	%eax, %eax
	movl	%r12d, %ecx
	movl	$2, %esi
	movq	stderr(%rip), %rdi
	leaq	.LC41(%rip), %rdx
	call	__fprintf_chk@PLT
	movq	%r13, %rdi
	call	_Z10initializeP11algoparam_t
	testl	%eax, %eax
	jne	.L133
.L145:
	movq	stderr(%rip), %rdi
	leaq	.LC31(%rip), %rdx
	movl	$2, %esi
	call	__fprintf_chk@PLT
	jmp	.L118
.L133:
	movl	%r12d, 112(%rsp)
	movq	64(%rsp), %rdi
	xorl	%esi, %esi
	movslq	%ebx, %r15
	movl	%r12d, 116(%rsp)
	movl	%ebx, %r12d
	imull	%ebx, %r12d
	movl	%ebp, 124(%rsp)
	movl	%ebp, 128(%rsp)
	movl	$1, 120(%rsp)
	movl	$1, 132(%rsp)
	movslq	%r12d, %r12
	call	cudaEventRecord@PLT
	movq	64(%rsp), %rdi
	call	cudaEventSynchronize@PLT
	movq	%r12, %rsi
	movl	$4, %edi
	call	calloc@PLT
	movq	%r15, %rsi
	movl	$4, %edi
	movq	%rax, %r13
	call	calloc@PLT
	leaq	80(%rsp), %rdi
	movq	%rax, %rbp
	leaq	0(,%r12,4), %rax
	xorl	%r12d, %r12d
	movq	%rax, %rsi
	movq	%rax, (%rsp)
	call	cudaMalloc@PLT
	movq	(%rsp), %rsi
	leaq	88(%rsp), %rdi
	call	cudaMalloc@PLT
	movq	(%rsp), %rsi
	leaq	96(%rsp), %rdi
	call	cudaMalloc@PLT
	leaq	0(,%r15,4), %rax
	leaq	104(%rsp), %rdi
	movabsq	$4294967297, %r15
	movq	%rax, %rsi
	movq	%rax, 8(%rsp)
	call	cudaMalloc@PLT
	movq	(%rsp), %rdx
	movq	80(%rsp), %rdi
	movl	$1, %ecx
	movq	176(%rsp), %rsi
	call	cudaMemcpy@PLT
	movq	(%rsp), %rdx
	movq	88(%rsp), %rdi
	movl	$1, %ecx
	movq	184(%rsp), %rsi
	call	cudaMemcpy@PLT
	movq	(%rsp), %rdx
	movq	96(%rsp), %rdi
	movq	%r13, %rsi
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	movq	8(%rsp), %rdx
	movq	104(%rsp), %rdi
	movq	%rbp, %rsi
	movl	$1, %ecx
	call	cudaMemcpy@PLT
.L139:
	movq	124(%rsp), %rdx
	movq	112(%rsp), %rdi
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movl	132(%rsp), %ecx
	movl	120(%rsp), %esi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L134
	movq	96(%rsp), %rdx
	movq	88(%rsp), %rsi
	movl	%ebx, %ecx
	movq	80(%rsp), %rdi
	call	_Z8gpu_HeatPfS_S_i@PLT
.L134:
	call	cudaThreadSynchronize@PLT
	movq	(%rsp), %rdx
	movq	96(%rsp), %rsi
	movq	%r13, %rdi
	movl	$2, %ecx
	call	cudaMemcpy@PLT
	movq	%r15, 152(%rsp)
	xorl	%r9d, %r9d
	movl	156(%rsp), %ecx
	movq	%r15, 140(%rsp)
	movq	8(%rsp), %r8
	movl	%ebx, 148(%rsp)
	movl	144(%rsp), %esi
	movl	%ebx, 136(%rsp)
	movq	148(%rsp), %rdx
	movq	136(%rsp), %rdi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L135
	movq	104(%rsp), %rsi
	movq	96(%rsp), %rdi
	call	_Z17gpu_HeatReductionPfS_@PLT
.L135:
	call	cudaThreadSynchronize@PLT
	movq	104(%rsp), %rsi
	movq	%rbp, %rdi
	incl	%r12d
	movq	8(%rsp), %rdx
	movl	$2, %ecx
	call	cudaMemcpy@PLT
	movq	88(%rsp), %rsi
	movq	80(%rsp), %rax
	xorps	%xmm0, %xmm0
	movq	%rsi, 80(%rsp)
	movq	%rax, 88(%rsp)
	xorl	%eax, %eax
.L136:
	cmpl	%eax, %ebx
	jle	.L147
	addss	0(%rbp,%rax,4), %xmm0
	incq	%rax
	jmp	.L136
.L147:
	cvtss2sd	%xmm0, %xmm6
	movsd	.LC32(%rip), %xmm0
	movsd	%xmm6, 32(%rsp)
	comisd	%xmm6, %xmm0
	ja	.L138
	cmpl	160(%rsp), %r12d
	jb	.L139
.L138:
	movq	(%rsp), %rdx
	movq	176(%rsp), %rdi
	movl	$2, %ecx
	call	cudaMemcpy@PLT
	movq	80(%rsp), %rdi
	call	cudaFree@PLT
	movq	88(%rsp), %rdi
	call	cudaFree@PLT
	movq	72(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movq	72(%rsp), %rdi
	call	cudaEventSynchronize@PLT
	movq	72(%rsp), %rdx
	movq	64(%rsp), %rsi
	movq	40(%rsp), %rdi
	call	cudaEventElapsedTime@PLT
	movq	stdout(%rip), %rdi
	leaq	.LC42(%rip), %rdx
	movb	$1, %al
	movl	$2, %esi
	cvtss2sd	60(%rsp), %xmm0
	call	__fprintf_chk@PLT
	movsd	24(%rsp), %xmm0
	movl	$2, %esi
	movq	stdout(%rip), %rdi
	leaq	.LC37(%rip), %rdx
	movb	$2, %al
	movss	20(%rsp), %xmm1
	divss	60(%rsp), %xmm1
	divss	.LC36(%rip), %xmm1
	cvtss2sd	%xmm1, %xmm1
	call	__fprintf_chk@PLT
	movl	%r12d, %ecx
	movsd	32(%rsp), %xmm0
	movq	stdout(%rip), %rdi
	leaq	.LC38(%rip), %rdx
	movb	$1, %al
	movl	$2, %esi
	call	__fprintf_chk@PLT
	movq	64(%rsp), %rdi
	call	cudaEventDestroy@PLT
	movq	72(%rsp), %rdi
	call	cudaEventDestroy@PLT
	movl	172(%rsp), %eax
	movl	%ebx, %edx
	movl	%ebx, %esi
	movq	192(%rsp), %rbp
	movq	176(%rsp), %rdi
	leal	2(%rax), %r8d
	movl	%r8d, %r9d
	movq	%rbp, %rcx
	call	_Z7coarsenPfjjS_jj
	movq	%r14, %rdi
	movl	%r8d, %ecx
	movl	%r8d, %edx
	movq	%rbp, %rsi
	call	_Z11write_imageP8_IO_FILEPfjj
	leaq	160(%rsp), %rdi
	call	_Z8finalizeP11algoparam_t
.L125:
	xorl	%eax, %eax
.L116:
	movq	216(%rsp), %rdx
	subq	%fs:40, %rdx
	je	.L140
	call	__stack_chk_fail@PLT
.L140:
	addq	$232, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%rbp
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2030:
	.size	main, .-main
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2062:
	.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
.LFE2062:
	.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
.LC1:
	.long	1048576000
	.align 4
.LC5:
	.long	1065353216
	.align 4
.LC6:
	.long	-8388608
	.align 4
.LC7:
	.long	2139095040
	.section	.rodata.cst8,"aM",@progbits,8
	.align 8
.LC11:
	.long	0
	.long	1083176960
	.align 8
.LC32:
	.long	-350469331
	.long	1057634018
	.align 8
.LC33:
	.long	0
	.long	1076232192
	.align 8
.LC35:
	.long	0
	.long	1104006501
	.section	.rodata.cst4
	.align 4
.LC36:
	.long	1148846080
	.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 <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <float.h>
#include <hip/hip_runtime.h>

typedef struct
{
    float posx;
    float posy;
    float range;
    float temp;
}
heatsrc_t;

typedef struct
{
    unsigned maxiter;       // maximum number of iterations
    unsigned resolution;    // spatial resolution
    int algorithm;          // 0=>Jacobi, 1=>Gauss

    unsigned visres;        // visualization resolution
  
    float *u, *uhelp;
    float *uvis;

    unsigned   numsrcs;     // number of heat sources
    heatsrc_t *heatsrcs;
}
algoparam_t;

// function declarations
int read_input( FILE *infile, algoparam_t *param );
void print_params( algoparam_t *param );
int initialize( algoparam_t *param );
int finalize( algoparam_t *param );
void write_image( FILE * f, float *u,
		  unsigned sizex, unsigned sizey );
int coarsen(float *uold, unsigned oldx, unsigned oldy ,
	    float *unew, unsigned newx, unsigned newy );


__global__ void gpu_Heat (float *h, float *g, float *d,int N);
__global__ void gpu_HeatReduction(float *res, float *res1);
#define NB 8
#define min(a,b) ( ((a) < (b)) ? (a) : (b) )

float cpu_residual (float *u, float *utmp, unsigned sizex, unsigned sizey)
{
    float diff, sum=0.0;
  
    for (int i=1; i<sizex-1; i++) 
        for (int j=1; j<sizey-1; j++) {
            diff = utmp[i*sizey+j] - u[i*sizey + j];
            sum += diff * diff;
	}
    return(sum);
}

float cpu_jacobi (float *u, float *utmp, unsigned sizex, unsigned sizey)
{
    float diff, sum=0.0;
    int nbx, bx, nby, by;
  
    nbx = NB;
    bx = sizex/nbx;
    nby = NB;
    by = sizey/nby;
    for (int ii=0; ii<nbx; ii++)
        for (int jj=0; jj<nby; jj++) 
            for (int i=1+ii*bx; i<=min((ii+1)*bx, sizex-2); i++) 
                for (int j=1+jj*by; j<=min((jj+1)*by, sizey-2); j++) {
	            utmp[i*sizey+j]= 0.25 * (u[ i*sizey     + (j-1) ]+  // left
					     u[ i*sizey     + (j+1) ]+  // right
				             u[ (i-1)*sizey + j     ]+  // top
				             u[ (i+1)*sizey + j     ]); // bottom
            	    diff = utmp[i*sizey+j] - u[i*sizey + j];
            	    sum += diff * diff;
	        }
    return(sum);
}

void usage( char *s )
{
    fprintf(stderr, "Usage: %s <input file> -t threads -b blocks\n", s);
    fprintf(stderr, "       -t number of threads per block in each dimension (e.g. 16)\n");
}

int main( int argc, char *argv[] ) {
    unsigned iter;
    FILE *infile, *resfile;
    char *resfilename;

    // algorithmic parameters
    algoparam_t param;
    int np;

    // check arguments
    if( argc < 4 ) {
	usage( argv[0] );
	return 1;
    }

    // check input file
    if( !(infile=fopen(argv[1], "r"))  ) {
	fprintf(stderr, 
		"\nError: Cannot open \"%s\" for reading.\n\n", argv[1]);
      
	usage(argv[0]);
	return 1;
    }

    // check result file
    resfilename="heat.ppm";

    if( !(resfile=fopen(resfilename, "w")) ) {
	fprintf(stderr, 
		"\nError: Cannot open \"%s\" for writing.\n\n", 
		resfilename);
	usage(argv[0]);
	return 1;
    }

    // check input
    if( !read_input(infile, &param) )
    {
	fprintf(stderr, "\nError: Error parsing input file.\n\n");
	usage(argv[0]);
	return 1;
    }

    // full size (param.resolution are only the inner points)
    np = param.resolution + 2;

    int Grid_Dim, Block_Dim;	// Grid and Block structure values
    if (strcmp(argv[2], "-t")==0) {
            Block_Dim = atoi(argv[3]);
            Grid_Dim = np/Block_Dim + ((np%Block_Dim)!=0);;
    	    if ((Block_Dim*Block_Dim) > 512) {
        	printf("Error -- too many threads in block, try again\n");
        	return 1;
    	    }
    }
    else {
    	    fprintf(stderr, "Usage: %s <input file> -t threads -b blocks\n", argv[0]);
            fprintf(stderr, "       -t number of threads per block in each dimension (e.g. 16)\n");
            return 0;
       	    }

    fprintf(stderr, "\nSolving Heat equation on the CPU and the GPU\n");
    fprintf(stderr, "--------------------------------------------\n");
    print_params(&param);

    fprintf(stdout, "\nExecution on CPU (sequential)\n-----------------------------\n");
    if( !initialize(&param) )
	{
	    fprintf(stderr, "Error in Solver initialization.\n\n");
            return 1;
	}

    // starting time
    float elapsed_time_ms;       	// which is applicable for asynchronous code also
    hipEvent_t start, stop;     	// using cuda events to measure time
    hipEventCreate( &start );     // instrument code to measure start time
    hipEventCreate( &stop );

    hipEventRecord( start, 0 );
    hipEventSynchronize( start );

    iter = 0;
    float residual;
    while(1) {
	residual = cpu_jacobi(param.u, param.uhelp, np, np);
	float * tmp = param.u;
	param.u = param.uhelp;
	param.uhelp = tmp;
	//residual = cpu_residual (param.u, param.uhelp, np, np);
   //printf("residual: %.6f \n", residual);
        iter++;

        // solution good enough ?
        if (residual < 0.00005) break;

        // max. iteration reached ? (no limit with maxiter=0)
        if (iter>=param.maxiter) break;
    }

    hipEventRecord( stop, 0 );     // instrument code to measue end time
    hipEventSynchronize( stop );
    hipEventElapsedTime( &elapsed_time_ms, start, stop );

    // Flop count after iter iterations
    float flop = iter * 11.0 * param.resolution * param.resolution;

    fprintf(stdout, "Time on CPU in ms.= %f ", elapsed_time_ms);
    fprintf(stdout, "(%3.3f GFlop => %6.2f MFlop/s)\n", 
	    flop/1000000000.0,
	    flop/elapsed_time_ms/1000);
    fprintf(stdout, "Convergence to residual=%f: %d iterations\n", residual, iter);

    finalize( &param );

    fprintf(stdout, "\nExecution on GPU\n----------------\n");
    fprintf(stderr, "Number of threads per block in each dimension = %d\n", Block_Dim);
    fprintf(stderr, "Number of blocks per grid in each dimension   = %d\n", Grid_Dim);

    if( !initialize(&param) )
	{
	    fprintf(stderr, "Error in Solver initialization.\n\n");
            return 1;
	}

    dim3 Grid(Grid_Dim, Grid_Dim);
    dim3 Block(Block_Dim, Block_Dim);
    
    // starting time
    hipEventRecord( start, 0 );
    hipEventSynchronize( start );

    float *dev_u, *dev_uhelp, *dev_res, *res, *result, *dev_result;
	 res = (float*)calloc(sizeof(float), np*np);
	 result = (float*)calloc(sizeof(float), np);
    // TODO: Allocation on GPU for matrices u and uhelp

    hipMalloc( &dev_u, sizeof(float)*(np*np));
    hipMalloc( &dev_uhelp, sizeof(float)*(np*np));
    hipMalloc( &dev_res, sizeof(float)*(np*np));
	 hipMalloc( &dev_result, sizeof(float)*np);
    // TODO: Copy initial values in u and uhelp from host to GPU
    

	 hipMemcpy( dev_u,param.u, sizeof(float)*(np*np), hipMemcpyHostToDevice);
	 hipMemcpy( dev_uhelp, param.uhelp, sizeof(float)*(np*np), hipMemcpyHostToDevice);
	 hipMemcpy( dev_res, res, sizeof(float)*(np*np), hipMemcpyHostToDevice);
	 hipMemcpy( dev_result, result, sizeof(float)*(np), hipMemcpyHostToDevice); 
    iter = 0;
  	 
    while(1) {
     gpu_Heat<<<Grid,Block>>>(dev_u, dev_uhelp, dev_res, np);
     hipDeviceSynchronize();                        // wait for all threads to complete

     // TODO: residual is computed on host, we need to get from GPU values computed in u and uhelp 
     hipMemcpy( res, dev_res, sizeof(float)*(np*np), hipMemcpyDeviceToHost);
	
	  //for(int i=0;i<np;i++) { printf("%.6f ", res[i*2]); }	  
	
	  gpu_HeatReduction<<<np,np,np*sizeof(float)>>>(dev_res, dev_result);
	  hipDeviceSynchronize();
   
	  hipMemcpy( result, dev_result, sizeof(float)*np, hipMemcpyDeviceToHost);

     
		//cudaMemcpy( param.uhelp, dev_uhelp, sizeof(float)*(np*np), cudaMemcpyDeviceToHost);
   	//residual = cpu_residual(param.u, param.uhelp, np, np);

	float * tmp = dev_u;
	dev_u = dev_uhelp;
	dev_uhelp = tmp;

   iter++;

	float sum =0.0;
	for(int i=0;i<np;i++) {
	//		printf("Result[%d]=%.6f\n",i,result[i]);	
			sum += result[i]; 		
	
	}
	residual = sum; 
	
        // solution good enough ?
       if (residual < 0.00005) break;

        // max. iteration reached ? (no limit with maxiter=0)
        if (iter>=param.maxiter) break;
    }

    // TODO: get result matrix from GPU
    hipMemcpy(  param.u, dev_u, sizeof(float)*(np*np), hipMemcpyDeviceToHost);
  
    // TODO: free memory used in GPU
    hipFree( dev_u ); hipFree( dev_uhelp);

    hipEventRecord( stop, 0 );     // instrument code to measue end time
    hipEventSynchronize( stop );
    hipEventElapsedTime( &elapsed_time_ms, start, stop );

    fprintf(stdout, "\nTime on GPU in ms. = %f ", elapsed_time_ms);
    fprintf(stdout, "(%3.3f GFlop => %6.2f MFlop/s)\n", 
	    flop/1000000000.0,
	    flop/elapsed_time_ms/1000);
    fprintf(stdout, "Convergence to residual=%f: %d iterations\n", residual, iter);

    hipEventDestroy(start);
    hipEventDestroy(stop);

    // for plot...
    coarsen( param.u, np, np,
	     param.uvis, param.visres+2, param.visres+2 );
  
    write_image( resfile, param.uvis,  
		 param.visres+2, 
		 param.visres+2 );

    finalize( &param );

    return 0;
}


/*
 * Initialize the iterative solver
 * - allocate memory for matrices
 * - set boundary conditions according to configuration
 */
int initialize( algoparam_t *param )

{
    int i, j;
    float dist;

    // total number of points (including border)
    const int np = param->resolution + 2;
  
    //
    // allocate memory
    //
    (param->u)     = (float*)calloc( sizeof(float),np*np );
    (param->uhelp) = (float*)calloc( sizeof(float),np*np );
    (param->uvis)  = (float*)calloc( sizeof(float),
				      (param->visres+2) *
				      (param->visres+2) );
  

    if( !(param->u) || !(param->uhelp) || !(param->uvis) )
    {
	fprintf(stderr, "Error: Cannot allocate memory\n");
	return 0;
    }

    for( i=0; i<param->numsrcs; i++ )
    {
	/* top row */
	for( j=0; j<np; j++ )
	{
	    dist = sqrt( pow((float)j/(float)(np-1) - 
			     param->heatsrcs[i].posx, 2)+
			 pow(param->heatsrcs[i].posy, 2));
	  
	    if( dist <= param->heatsrcs[i].range )
	    {
		(param->u)[j] +=
		    (param->heatsrcs[i].range-dist) /
		    param->heatsrcs[i].range *
		    param->heatsrcs[i].temp;
	    }
	}
      
	/* bottom row */
	for( j=0; j<np; j++ )
	{
	    dist = sqrt( pow((float)j/(float)(np-1) - 
			     param->heatsrcs[i].posx, 2)+
			 pow(1-param->heatsrcs[i].posy, 2));
	  
	    if( dist <= param->heatsrcs[i].range )
	    {
		(param->u)[(np-1)*np+j]+=
		    (param->heatsrcs[i].range-dist) / 
		    param->heatsrcs[i].range * 
		    param->heatsrcs[i].temp;
	    }
	}
      
	/* leftmost column */
	for( j=1; j<np-1; j++ )
	{
	    dist = sqrt( pow(param->heatsrcs[i].posx, 2)+
			 pow((float)j/(float)(np-1) - 
			     param->heatsrcs[i].posy, 2)); 
	  
	    if( dist <= param->heatsrcs[i].range )
	    {
		(param->u)[ j*np ]+=
		    (param->heatsrcs[i].range-dist) / 
		    param->heatsrcs[i].range *
		    param->heatsrcs[i].temp;
	    }
	}
      
	/* rightmost column */
	for( j=1; j<np-1; j++ )
	{
	    dist = sqrt( pow(1-param->heatsrcs[i].posx, 2)+
			 pow((float)j/(float)(np-1) - 
			     param->heatsrcs[i].posy, 2)); 
	  
	    if( dist <= param->heatsrcs[i].range )
	    {
		(param->u)[ j*np+(np-1) ]+=
		    (param->heatsrcs[i].range-dist) /
		    param->heatsrcs[i].range *
		    param->heatsrcs[i].temp;
	    }
	}
    }

    // Copy u into uhelp
    float *putmp, *pu;
    pu = param->u;
    putmp = param->uhelp;
    for( j=0; j<np; j++ )
	for( i=0; i<np; i++ )
	    *putmp++ = *pu++;

    return 1;
}

/*
 * free used memory
 */
int finalize( algoparam_t *param )
{
    if( param->u ) {
	free(param->u);
	param->u = 0;
    }

    if( param->uhelp ) {
	free(param->uhelp);
	param->uhelp = 0;
    }

    if( param->uvis ) {
	free(param->uvis);
	param->uvis = 0;
    }

    return 1;
}


/*
 * write the given temperature u matrix to rgb values
 * and write the resulting image to file f
 */
void write_image( FILE * f, float *u,
		  unsigned sizex, unsigned sizey ) 
{
    // RGB table
    unsigned char r[1024], g[1024], b[1024];
    int i, j, k;
  
    float min, max;

    j=1023;

    // prepare RGB table
    for( i=0; i<256; i++ )
    {
	r[j]=255; g[j]=i; b[j]=0;
	j--;
    }
    for( i=0; i<256; i++ )
    {
	r[j]=255-i; g[j]=255; b[j]=0;
	j--;
    }
    for( i=0; i<256; i++ )
    {
	r[j]=0; g[j]=255; b[j]=i;
	j--;
    }
    for( i=0; i<256; i++ )
    {
	r[j]=0; g[j]=255-i; b[j]=255;
	j--;
    }

    min=DBL_MAX;
    max=-DBL_MAX;

    // find minimum and maximum 
    for( i=0; i<sizey; i++ )
    {
	for( j=0; j<sizex; j++ )
	{
	    if( u[i*sizex+j]>max )
		max=u[i*sizex+j];
	    if( u[i*sizex+j]<min )
		min=u[i*sizex+j];
	}
    }
  

    fprintf(f, "P3\n");
    fprintf(f, "%u %u\n", sizex, sizey);
    fprintf(f, "%u\n", 255);

    for( i=0; i<sizey; i++ )
    {
	for( j=0; j<sizex; j++ )
	{
	    k=(int)(1023.0*(u[i*sizex+j]-min)/(max-min));
	    fprintf(f, "%d %d %d  ", r[k], g[k], b[k]);
	}
	fprintf(f, "\n");
    }
}


int coarsen( float *uold, unsigned oldx, unsigned oldy ,
	     float *unew, unsigned newx, unsigned newy )
{
    int i, j;

    int stepx;
    int stepy;
    int stopx = newx;
    int stopy = newy;

    if (oldx>newx)
	stepx=oldx/newx;
    else {
	stepx=1;
	stopx=oldx;
    }
    if (oldy>newy)
	stepy=oldy/newy;
    else {
	stepy=1;
	stopy=oldy;
    }

    // NOTE: this only takes the top-left corner,
    // and doesnt' do any real coarsening 
    for( i=0; i<stopy-1; i++ )
    {
	for( j=0; j<stopx-1; j++ )
        {
	    unew[i*newx+j]=uold[i*oldx*stepy+j*stepx];
        }
    }

  return 1;
}

#define BUFSIZE 100
int read_input( FILE *infile, algoparam_t *param )
{
  int i, n;
  char buf[BUFSIZE];

  fgets(buf, BUFSIZE, infile);
  n = sscanf( buf, "%u", &(param->maxiter) );
  if( n!=1)
    return 0;

  fgets(buf, BUFSIZE, infile);
  n = sscanf( buf, "%u", &(param->resolution) );
  if( n!=1 )
    return 0;

  param->visres = param->resolution;

  fgets(buf, BUFSIZE, infile);
  n = sscanf(buf, "%u", &(param->numsrcs) );
  if( n!=1 )
    return 0;

  (param->heatsrcs) = 
    (heatsrc_t*) malloc( sizeof(heatsrc_t) * (param->numsrcs) );
  
  for( i=0; i<param->numsrcs; i++ )
    {
      fgets(buf, BUFSIZE, infile);
      n = sscanf( buf, "%f %f %f %f",
		  &(param->heatsrcs[i].posx),
		  &(param->heatsrcs[i].posy),
		  &(param->heatsrcs[i].range),
		  &(param->heatsrcs[i].temp) );

      if( n!=4 )
	return 0;
    }

  return 1;
}


void print_params( algoparam_t *param )
{
  int i;

  fprintf(stdout, "Iterations        : %u\n", param->maxiter);
  fprintf(stdout, "Resolution        : %u\n", param->resolution);
  fprintf(stdout, "Num. Heat sources : %u\n", param->numsrcs);

  for( i=0; i<param->numsrcs; i++ )
    {
      fprintf(stdout, "  %2d: (%2.2f, %2.2f) %2.2f %2.2f \n",
	     i+1,
	     param->heatsrcs[i].posx,
	     param->heatsrcs[i].posy,
	     param->heatsrcs[i].range,
	     param->heatsrcs[i].temp );
    }
}


	.text
	.file	"heatCUDA.hip"
	.globl	_Z12cpu_residualPfS_jj          # -- Begin function _Z12cpu_residualPfS_jj
	.type	_Z12cpu_residualPfS_jj,@function
_Z12cpu_residualPfS_jj:                 # @_Z12cpu_residualPfS_jj
	.cfi_startproc
# %bb.0:
                                        # kill: def $ecx killed $ecx def $rcx
	decl	%edx
	xorps	%xmm0, %xmm0
	cmpl	$2, %edx
	jb	.LBB0_5
# %bb.1:                                # %.preheader.lr.ph
	pushq	%r14
	.cfi_def_cfa_offset 16
	pushq	%rbx
	.cfi_def_cfa_offset 24
	.cfi_offset %rbx, -24
	.cfi_offset %r14, -16
	leal	-1(%rcx), %eax
	leal	1(%rcx), %r8d
	leaq	-1(%rax), %r9
	xorps	%xmm0, %xmm0
	movl	$1, %r10d
.LBB0_2:                                # %.preheader
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB0_6 Depth 2
	movq	%r9, %r11
	movl	%r8d, %ebx
	cmpl	$2, %eax
	jb	.LBB0_3
.LBB0_6:                                #   Parent Loop BB0_2 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movl	%ebx, %r14d
	movss	(%rsi,%r14,4), %xmm1            # xmm1 = mem[0],zero,zero,zero
	subss	(%rdi,%r14,4), %xmm1
	mulss	%xmm1, %xmm1
	addss	%xmm1, %xmm0
	incl	%ebx
	decq	%r11
	jne	.LBB0_6
.LBB0_3:                                # %._crit_edge
                                        #   in Loop: Header=BB0_2 Depth=1
	incl	%r10d
	addl	%ecx, %r8d
	cmpl	%edx, %r10d
	jne	.LBB0_2
# %bb.4:
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%r14
	.cfi_def_cfa_offset 8
	.cfi_restore %rbx
	.cfi_restore %r14
.LBB0_5:                                # %._crit_edge23
	retq
.Lfunc_end0:
	.size	_Z12cpu_residualPfS_jj, .Lfunc_end0-_Z12cpu_residualPfS_jj
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst4,"aM",@progbits,4
	.p2align	2, 0x0                          # -- Begin function _Z10cpu_jacobiPfS_jj
.LCPI1_0:
	.long	0x3e800000                      # float 0.25
	.text
	.globl	_Z10cpu_jacobiPfS_jj
	.type	_Z10cpu_jacobiPfS_jj,@function
_Z10cpu_jacobiPfS_jj:                   # @_Z10cpu_jacobiPfS_jj
	.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
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
                                        # kill: def $ecx killed $ecx def $rcx
	movl	%edx, %eax
	shrl	$3, %eax
	movl	%ecx, %r8d
	shrl	$3, %r8d
	addl	$-2, %edx
	movl	%edx, -64(%rsp)                 # 4-byte Spill
	leal	-2(%rcx), %edx
	movl	%edx, -48(%rsp)                 # 4-byte Spill
	movl	%ecx, %edx
	movl	%eax, -68(%rsp)                 # 4-byte Spill
	imull	%eax, %edx
	movl	%edx, -72(%rsp)                 # 4-byte Spill
	leal	1(,%rcx,2), %edx
	xorps	%xmm0, %xmm0
	xorl	%r9d, %r9d
	movl	$1, %eax
	movss	.LCPI1_0(%rip), %xmm1           # xmm1 = mem[0],zero,zero,zero
	movl	%ecx, %r14d
	movq	%r8, -8(%rsp)                   # 8-byte Spill
.LBB1_1:                                # %.preheader
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB1_2 Depth 2
                                        #       Child Loop BB1_10 Depth 3
                                        #         Child Loop BB1_12 Depth 4
	movl	%r9d, %r15d
	movl	%eax, %r10d
	movl	-68(%rsp), %eax                 # 4-byte Reload
	imull	%eax, %r15d
	incl	%r9d
	movq	%r9, -16(%rsp)                  # 8-byte Spill
	movl	%r9d, %r12d
	imull	%eax, %r12d
	movl	-64(%rsp), %eax                 # 4-byte Reload
	cmpl	%eax, %r12d
	cmovael	%eax, %r12d
	movl	%r10d, -56(%rsp)                # 4-byte Spill
	movl	%edx, -52(%rsp)                 # 4-byte Spill
	movl	%edx, %r9d
	movl	%r14d, -60(%rsp)                # 4-byte Spill
	xorl	%eax, %eax
	movq	%rax, -24(%rsp)                 # 8-byte Spill
	xorl	%eax, %eax
	movl	%r15d, -44(%rsp)                # 4-byte Spill
.LBB1_2:                                #   Parent Loop BB1_1 Depth=1
                                        # =>  This Loop Header: Depth=2
                                        #       Child Loop BB1_10 Depth 3
                                        #         Child Loop BB1_12 Depth 4
	cmpl	%r12d, %r15d
	jae	.LBB1_3
# %bb.9:                                # %.lr.ph78
                                        #   in Loop: Header=BB1_2 Depth=2
	movl	%eax, %ebx
	imull	%r8d, %ebx
	incl	%eax
	movl	%eax, -40(%rsp)                 # 4-byte Spill
	movl	%eax, %ebp
	imull	%r8d, %ebp
	movl	-48(%rsp), %eax                 # 4-byte Reload
	cmpl	%eax, %ebp
	cmovael	%eax, %ebp
	movl	%r10d, -28(%rsp)                # 4-byte Spill
	movl	%r10d, %edx
	movl	%r9d, -32(%rsp)                 # 4-byte Spill
	movl	%r14d, -36(%rsp)                # 4-byte Spill
	movl	%r15d, %r8d
.LBB1_10:                               #   Parent Loop BB1_1 Depth=1
                                        #     Parent Loop BB1_2 Depth=2
                                        # =>    This Loop Header: Depth=3
                                        #         Child Loop BB1_12 Depth 4
	cmpl	%ebp, %ebx
	jae	.LBB1_6
# %bb.11:                               # %.lr.ph
                                        #   in Loop: Header=BB1_10 Depth=3
	movl	%edx, %r13d
	movl	%r9d, %r11d
	movl	%r14d, %eax
	movq	-24(%rsp), %r10                 # 8-byte Reload
.LBB1_12:                               #   Parent Loop BB1_1 Depth=1
                                        #     Parent Loop BB1_2 Depth=2
                                        #       Parent Loop BB1_10 Depth=3
                                        # =>      This Inner Loop Header: Depth=4
	movl	%eax, %r15d
	movss	(%rdi,%r15,4), %xmm2            # xmm2 = mem[0],zero,zero,zero
	leal	2(%rax), %r15d
	addss	(%rdi,%r15,4), %xmm2
	movl	%r13d, %r15d
	addss	(%rdi,%r15,4), %xmm2
	movl	%r11d, %r15d
	addss	(%rdi,%r15,4), %xmm2
	mulss	%xmm1, %xmm2
	incl	%eax
	movss	%xmm2, (%rsi,%rax,4)
	subss	(%rdi,%rax,4), %xmm2
	mulss	%xmm2, %xmm2
	addss	%xmm2, %xmm0
	incq	%r10
	incl	%r11d
	incl	%r13d
	cmpq	%rbp, %r10
	jb	.LBB1_12
.LBB1_6:                                # %.loopexit
                                        #   in Loop: Header=BB1_10 Depth=3
	incl	%r8d
	addl	%ecx, %r14d
	addl	%ecx, %r9d
	addl	%ecx, %edx
	cmpl	%r12d, %r8d
	jb	.LBB1_10
# %bb.7:                                #   in Loop: Header=BB1_2 Depth=2
	movq	-8(%rsp), %r8                   # 8-byte Reload
	movl	-44(%rsp), %r15d                # 4-byte Reload
	movl	-28(%rsp), %r10d                # 4-byte Reload
	movl	-32(%rsp), %r9d                 # 4-byte Reload
	movl	-36(%rsp), %r14d                # 4-byte Reload
	movl	-40(%rsp), %eax                 # 4-byte Reload
	jmp	.LBB1_8
.LBB1_3:                                # %.._crit_edge_crit_edge
                                        #   in Loop: Header=BB1_2 Depth=2
	incl	%eax
.LBB1_8:                                # %._crit_edge
                                        #   in Loop: Header=BB1_2 Depth=2
	addq	%r8, -24(%rsp)                  # 8-byte Folded Spill
	addl	%r8d, %r14d
	addl	%r8d, %r9d
	addl	%r8d, %r10d
	cmpl	$8, %eax
	jne	.LBB1_2
# %bb.4:                                #   in Loop: Header=BB1_1 Depth=1
	movl	-72(%rsp), %eax                 # 4-byte Reload
	movl	-60(%rsp), %r14d                # 4-byte Reload
	addl	%eax, %r14d
	movl	-52(%rsp), %edx                 # 4-byte Reload
	addl	%eax, %edx
	movl	-56(%rsp), %r9d                 # 4-byte Reload
	addl	%eax, %r9d
	movl	%r9d, %eax
	movq	-16(%rsp), %r9                  # 8-byte Reload
	cmpl	$8, %r9d
	jne	.LBB1_1
# %bb.5:
	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_end1:
	.size	_Z10cpu_jacobiPfS_jj, .Lfunc_end1-_Z10cpu_jacobiPfS_jj
	.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, %esi
	xorl	%eax, %eax
	callq	fprintf
	movq	stderr(%rip), %rcx
	movl	$.L.str.1, %edi
	movl	$66, %esi
	movl	$1, %edx
	popq	%rax
	.cfi_def_cfa_offset 8
	jmp	fwrite@PLT                      # TAILCALL
.Lfunc_end2:
	.size	_Z5usagePc, .Lfunc_end2-_Z5usagePc
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function main
.LCPI3_0:
	.quad	0x3f0a36e2eb1c432d              # double 5.0000000000000002E-5
.LCPI3_1:
	.quad	0x4026000000000000              # double 11
.LCPI3_2:
	.quad	0x41cdcd6500000000              # double 1.0E+9
	.section	.rodata.cst4,"aM",@progbits,4
	.p2align	2, 0x0
.LCPI3_3:
	.long	0x447a0000                      # float 1000
	.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	$168, %rsp
	.cfi_def_cfa_offset 224
	.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, %rbx
	cmpl	$3, %edi
	jle	.LBB3_11
# %bb.1:
	movq	8(%rbx), %rdi
	movl	$.L.str.2, %esi
	callq	fopen
	testq	%rax, %rax
	je	.LBB3_7
# %bb.2:
	movq	%rax, %r14
	movl	$.L.str.4, %edi
	movl	$.L.str.5, %esi
	callq	fopen
	testq	%rax, %rax
	je	.LBB3_8
# %bb.3:
	movq	%rax, %r13
	leaq	72(%rsp), %rsi
	movq	%r14, %rdi
	callq	_Z10read_inputP8_IO_FILEP11algoparam_t
	testl	%eax, %eax
	je	.LBB3_10
# %bb.4:
	movl	76(%rsp), %ebp
	movq	16(%rbx), %rdi
	movl	$.L.str.8, %esi
	callq	strcmp
	testl	%eax, %eax
	jne	.LBB3_15
# %bb.5:
	addl	$2, %ebp
	movq	24(%rbx), %rdi
	callq	atoi
	movl	%eax, %r15d
	movl	%ebp, %eax
	cltd
	idivl	%r15d
	movl	%eax, %r12d
	cmpl	$1, %edx
	sbbl	$-1, %r12d
	movl	%r15d, %eax
	imull	%r15d, %eax
	cmpl	$513, %eax                      # imm = 0x201
	jb	.LBB3_16
# %bb.6:
	movl	$.Lstr, %edi
	callq	puts@PLT
	jmp	.LBB3_13
.LBB3_7:
	movq	stderr(%rip), %rdi
	movq	8(%rbx), %rdx
	movl	$.L.str.3, %esi
	jmp	.LBB3_9
.LBB3_8:
	movq	stderr(%rip), %rdi
	movl	$.L.str.6, %esi
	movl	$.L.str.4, %edx
.LBB3_9:
	xorl	%eax, %eax
	callq	fprintf
	jmp	.LBB3_11
.LBB3_10:
	movq	stderr(%rip), %rcx
	movl	$.L.str.7, %edi
	movl	$35, %esi
	movl	$1, %edx
	callq	fwrite@PLT
.LBB3_11:
	movq	(%rbx), %rdx
	movq	stderr(%rip), %rdi
	movl	$.L.str, %esi
	xorl	%eax, %eax
	callq	fprintf
	movq	stderr(%rip), %rcx
	movl	$.L.str.1, %edi
	movl	$66, %esi
.LBB3_12:
	movl	$1, %edx
	callq	fwrite@PLT
.LBB3_13:
	movl	$1, %ebx
.LBB3_14:
	movl	%ebx, %eax
	addq	$168, %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
.LBB3_15:
	.cfi_def_cfa_offset 224
	movq	stderr(%rip), %rdi
	movq	(%rbx), %rdx
	xorl	%ebx, %ebx
	movl	$.L.str, %esi
	xorl	%eax, %eax
	callq	fprintf
	movq	stderr(%rip), %rcx
	movl	$.L.str.1, %edi
	movl	$66, %esi
	movl	$1, %edx
	callq	fwrite@PLT
	jmp	.LBB3_14
.LBB3_16:
	movq	stderr(%rip), %rcx
	movl	$.L.str.10, %edi
	movl	$46, %esi
	movl	$1, %edx
	callq	fwrite@PLT
	movq	stderr(%rip), %rcx
	movl	$.L.str.11, %edi
	movl	$45, %esi
	movl	$1, %edx
	callq	fwrite@PLT
	leaq	72(%rsp), %rbx
	movq	%rbx, %rdi
	callq	_Z12print_paramsP11algoparam_t
	movq	stdout(%rip), %rcx
	movl	$.L.str.12, %edi
	movl	$61, %esi
	movl	$1, %edx
	callq	fwrite@PLT
	movq	%rbx, %rdi
	callq	_Z10initializeP11algoparam_t
	testl	%eax, %eax
	jne	.LBB3_18
.LBB3_17:
	movq	stderr(%rip), %rcx
	movl	$.L.str.13, %edi
	movl	$33, %esi
	jmp	.LBB3_12
.LBB3_18:
	movq	%r13, 136(%rsp)                 # 8-byte Spill
	leaq	32(%rsp), %r14
	movq	%r14, %rdi
	callq	hipEventCreate
	movq	%rsp, %rdi
	callq	hipEventCreate
	movq	(%r14), %rdi
	xorl	%ebx, %ebx
	xorl	%esi, %esi
	callq	hipEventRecord
	movq	(%r14), %rdi
	callq	hipEventSynchronize
	movq	88(%rsp), %rdi
	movq	96(%rsp), %rsi
.LBB3_19:                               # =>This Inner Loop Header: Depth=1
	movl	%ebp, %edx
	movl	%ebp, %ecx
	callq	_Z10cpu_jacobiPfS_jj
	movq	88(%rsp), %rsi
	movq	96(%rsp), %rdi
	movq	%rdi, 88(%rsp)
	movq	%rsi, 96(%rsp)
	incl	%ebx
	xorps	%xmm1, %xmm1
	cvtss2sd	%xmm0, %xmm1
	movsd	.LCPI3_0(%rip), %xmm0           # xmm0 = mem[0],zero
	ucomisd	%xmm1, %xmm0
	ja	.LBB3_21
# %bb.20:                               #   in Loop: Header=BB3_19 Depth=1
	cmpl	72(%rsp), %ebx
	jb	.LBB3_19
.LBB3_21:
	movq	(%rsp), %rdi
	xorl	%r14d, %r14d
	xorl	%esi, %esi
	movsd	%xmm1, 8(%rsp)                  # 8-byte Spill
	callq	hipEventRecord
	movq	(%rsp), %rdi
	callq	hipEventSynchronize
	movq	32(%rsp), %rsi
	movq	(%rsp), %rdx
	leaq	68(%rsp), %r13
	movq	%r13, %rdi
	callq	hipEventElapsedTime
	movl	%ebx, %eax
	xorps	%xmm0, %xmm0
	cvtsi2sd	%rax, %xmm0
	mulsd	.LCPI3_1(%rip), %xmm0
	leaq	72(%rsp), %rax
	movl	4(%rax), %eax
	xorps	%xmm1, %xmm1
	cvtsi2sd	%rax, %xmm1
	mulsd	%xmm1, %xmm0
	mulsd	%xmm1, %xmm0
	cvtsd2ss	%xmm0, %xmm0
	movss	%xmm0, 44(%rsp)                 # 4-byte Spill
	movq	stdout(%rip), %rdi
	xorps	%xmm0, %xmm0
	cvtss2sd	(%r13), %xmm0
	movl	$.L.str.14, %esi
	movb	$1, %al
	callq	fprintf
	movss	44(%rsp), %xmm1                 # 4-byte Reload
                                        # xmm1 = mem[0],zero,zero,zero
	xorps	%xmm0, %xmm0
	cvtss2sd	%xmm1, %xmm0
	divsd	.LCPI3_2(%rip), %xmm0
	divss	(%r13), %xmm1
	movq	stdout(%rip), %rdi
	divss	.LCPI3_3(%rip), %xmm1
	cvtss2sd	%xmm1, %xmm1
	movl	$.L.str.15, %esi
	movsd	%xmm0, 128(%rsp)                # 8-byte Spill
	movb	$2, %al
	callq	fprintf
	movq	stdout(%rip), %rdi
	movl	$.L.str.16, %esi
	movsd	8(%rsp), %xmm0                  # 8-byte Reload
                                        # xmm0 = mem[0],zero
	movl	%ebx, %edx
	movb	$1, %al
	callq	fprintf
	leaq	72(%rsp), %rbx
	movq	%rbx, %rdi
	callq	_Z8finalizeP11algoparam_t
	movq	stdout(%rip), %rcx
	movl	$.L.str.17, %edi
	movl	$35, %esi
	movl	$1, %edx
	callq	fwrite@PLT
	movq	stderr(%rip), %rdi
	movl	$.L.str.18, %esi
	movl	%r15d, %edx
	xorl	%eax, %eax
	callq	fprintf
	movq	stderr(%rip), %rdi
	movl	$.L.str.19, %esi
	movl	%r12d, %edx
	xorl	%eax, %eax
	callq	fprintf
	movq	%rbx, %rdi
	callq	_Z10initializeP11algoparam_t
	testl	%eax, %eax
	je	.LBB3_17
# %bb.22:
	movl	%r12d, %eax
	movq	%rax, %rcx
	shlq	$32, %rcx
	orq	%rax, %rcx
	movq	%rcx, 160(%rsp)                 # 8-byte Spill
	movl	%r15d, %eax
	movq	%rax, %rcx
	shlq	$32, %rcx
	orq	%rax, %rcx
	movq	%rcx, 152(%rsp)                 # 8-byte Spill
	movq	32(%rsp), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	movq	32(%rsp), %rdi
	callq	hipEventSynchronize
	movl	%ebp, %r13d
	imull	%r13d, %r13d
	movl	$4, %edi
	movq	%r13, %rsi
	callq	calloc
	movq	%rax, %rbx
	movslq	%ebp, %r12
	movl	$4, %edi
	movq	%r12, %rsi
	callq	calloc
	movq	%rax, %r15
	shlq	$2, %r13
	leaq	24(%rsp), %rdi
	movq	%r13, %rsi
	callq	hipMalloc
	leaq	16(%rsp), %rdi
	movq	%r13, %rsi
	callq	hipMalloc
	leaq	48(%rsp), %rdi
	movq	%r13, %rsi
	callq	hipMalloc
	shlq	$2, %r12
	leaq	56(%rsp), %rdi
	movq	%r12, %rsi
	callq	hipMalloc
	leaq	24(%rsp), %rax
	movq	(%rax), %rdi
	movq	88(%rsp), %rsi
	movq	%r13, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	leaq	16(%rsp), %rax
	movq	(%rax), %rdi
	movq	96(%rsp), %rsi
	movq	%r13, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	leaq	48(%rsp), %rax
	movq	(%rax), %rdi
	movq	%rbx, 144(%rsp)                 # 8-byte Spill
	movq	%rbx, %rsi
	movq	%r13, 8(%rsp)                   # 8-byte Spill
	movq	%r13, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	leaq	56(%rsp), %rax
	movq	(%rax), %rdi
	movq	%r15, %rsi
	movq	%r12, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	movl	%ebp, %r13d
	movabsq	$4294967297, %rax               # imm = 0x100000001
	leaq	(%rax,%r13), %rbx
	decq	%rbx
.LBB3_23:                               # =>This Loop Header: Depth=1
                                        #     Child Loop BB3_29 Depth 2
	movq	160(%rsp), %rdi                 # 8-byte Reload
	movl	$1, %esi
	movq	152(%rsp), %rdx                 # 8-byte Reload
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB3_25
# %bb.24:                               #   in Loop: Header=BB3_23 Depth=1
	movq	24(%rsp), %rdi
	movq	16(%rsp), %rsi
	movq	48(%rsp), %rdx
	movl	%ebp, %ecx
	callq	_Z23__device_stub__gpu_HeatPfS_S_i
.LBB3_25:                               #   in Loop: Header=BB3_23 Depth=1
	callq	hipDeviceSynchronize
	movq	48(%rsp), %rsi
	movq	144(%rsp), %rdi                 # 8-byte Reload
	movq	8(%rsp), %rdx                   # 8-byte Reload
	movl	$2, %ecx
	callq	hipMemcpy
	movq	%rbx, %rdi
	movl	$1, %esi
	movq	%rbx, %rdx
	movl	$1, %ecx
	movq	%r12, %r8
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB3_27
# %bb.26:                               #   in Loop: Header=BB3_23 Depth=1
	movq	48(%rsp), %rdi
	movq	56(%rsp), %rsi
	callq	_Z32__device_stub__gpu_HeatReductionPfS_
.LBB3_27:                               #   in Loop: Header=BB3_23 Depth=1
	callq	hipDeviceSynchronize
	movq	56(%rsp), %rsi
	movq	%r15, %rdi
	movq	%r12, %rdx
	movl	$2, %ecx
	callq	hipMemcpy
	movq	24(%rsp), %rax
	movq	16(%rsp), %rsi
	movq	%rsi, 24(%rsp)
	movq	%rax, 16(%rsp)
	xorps	%xmm0, %xmm0
	testl	%ebp, %ebp
	jle	.LBB3_30
# %bb.28:                               # %.lr.ph.preheader
                                        #   in Loop: Header=BB3_23 Depth=1
	xorl	%eax, %eax
.LBB3_29:                               # %.lr.ph
                                        #   Parent Loop BB3_23 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	addss	(%r15,%rax,4), %xmm0
	incq	%rax
	cmpq	%rax, %r13
	jne	.LBB3_29
.LBB3_30:                               # %._crit_edge
                                        #   in Loop: Header=BB3_23 Depth=1
	incl	%r14d
	xorps	%xmm1, %xmm1
	cvtss2sd	%xmm0, %xmm1
	movsd	.LCPI3_0(%rip), %xmm0           # xmm0 = mem[0],zero
	ucomisd	%xmm1, %xmm0
	ja	.LBB3_32
# %bb.31:                               # %._crit_edge
                                        #   in Loop: Header=BB3_23 Depth=1
	cmpl	72(%rsp), %r14d
	jb	.LBB3_23
.LBB3_32:
	leaq	72(%rsp), %r15
	movq	16(%r15), %rdi
	movq	8(%rsp), %rdx                   # 8-byte Reload
	movl	$2, %ecx
	movsd	%xmm1, 8(%rsp)                  # 8-byte Spill
	callq	hipMemcpy
	movq	24(%rsp), %rdi
	callq	hipFree
	movq	16(%rsp), %rdi
	callq	hipFree
	movq	(%rsp), %rdi
	xorl	%ebx, %ebx
	xorl	%esi, %esi
	callq	hipEventRecord
	movq	(%rsp), %rdi
	callq	hipEventSynchronize
	movq	32(%rsp), %rsi
	movq	(%rsp), %rdx
	leaq	68(%rsp), %r12
	movq	%r12, %rdi
	callq	hipEventElapsedTime
	movq	stdout(%rip), %rdi
	xorps	%xmm0, %xmm0
	cvtss2sd	(%r12), %xmm0
	movl	$.L.str.20, %esi
	movb	$1, %al
	callq	fprintf
	movss	44(%rsp), %xmm0                 # 4-byte Reload
                                        # xmm0 = mem[0],zero,zero,zero
	divss	(%r12), %xmm0
	movq	stdout(%rip), %rdi
	divss	.LCPI3_3(%rip), %xmm0
	xorps	%xmm1, %xmm1
	cvtss2sd	%xmm0, %xmm1
	movl	$.L.str.15, %esi
	movsd	128(%rsp), %xmm0                # 8-byte Reload
                                        # xmm0 = mem[0],zero
	movb	$2, %al
	callq	fprintf
	movq	stdout(%rip), %rdi
	movl	$.L.str.16, %esi
	movsd	8(%rsp), %xmm0                  # 8-byte Reload
                                        # xmm0 = mem[0],zero
	movl	%r14d, %edx
	movb	$1, %al
	callq	fprintf
	movq	32(%rsp), %rdi
	callq	hipEventDestroy
	movq	(%rsp), %rdi
	callq	hipEventDestroy
	movq	16(%r15), %rdi
	movq	32(%r15), %rcx
	movl	12(%r15), %r8d
	addl	$2, %r8d
	movl	%ebp, %esi
	movl	%ebp, %edx
	movl	%r8d, %r9d
	callq	_Z7coarsenPfjjS_jj
	movq	32(%r15), %rsi
	movl	12(%r15), %ecx
	addl	$2, %ecx
	movq	136(%rsp), %rdi                 # 8-byte Reload
	movl	%ecx, %edx
	callq	_Z11write_imageP8_IO_FILEPfjj
	movq	%r15, %rdi
	callq	_Z8finalizeP11algoparam_t
	jmp	.LBB3_14
.Lfunc_end3:
	.size	main, .Lfunc_end3-main
	.cfi_endproc
                                        # -- End function
	.globl	_Z10read_inputP8_IO_FILEP11algoparam_t # -- Begin function _Z10read_inputP8_IO_FILEP11algoparam_t
	.type	_Z10read_inputP8_IO_FILEP11algoparam_t,@function
_Z10read_inputP8_IO_FILEP11algoparam_t: # @_Z10read_inputP8_IO_FILEP11algoparam_t
	.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	$120, %rsp
	.cfi_def_cfa_offset 176
	.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, %rbx
	movq	%rdi, %r14
	leaq	16(%rsp), %r15
	movq	%r15, %rdi
	movl	$100, %esi
	movq	%r14, %rdx
	callq	fgets
	xorl	%ebp, %ebp
	movl	$.L.str.27, %esi
	movq	%r15, %rdi
	movq	%rbx, %rdx
	xorl	%eax, %eax
	callq	__isoc23_sscanf
	cmpl	$1, %eax
	jne	.LBB4_8
# %bb.1:
	leaq	16(%rsp), %r15
	movq	%r15, %rdi
	movl	$100, %esi
	movq	%r14, %rdx
	callq	fgets
	leaq	4(%rbx), %rdx
	xorl	%ebp, %ebp
	movl	$.L.str.27, %esi
	movq	%r15, %rdi
	xorl	%eax, %eax
	callq	__isoc23_sscanf
	cmpl	$1, %eax
	jne	.LBB4_8
# %bb.2:
	movl	4(%rbx), %eax
	movl	%eax, 12(%rbx)
	leaq	16(%rsp), %r12
	movq	%r12, %rdi
	movl	$100, %esi
	movq	%r14, %rdx
	callq	fgets
	leaq	40(%rbx), %rdx
	xorl	%ebp, %ebp
	movl	$.L.str.27, %esi
	movq	%r12, %rdi
	movq	%rdx, 8(%rsp)                   # 8-byte Spill
	xorl	%eax, %eax
	callq	__isoc23_sscanf
	cmpl	$1, %eax
	jne	.LBB4_8
# %bb.3:
	movl	40(%rbx), %r15d
	movq	%r15, %rdi
	shlq	$4, %rdi
	callq	malloc
	movq	%rax, 48(%rbx)
	movl	$1, %ebp
	testq	%r15, %r15
	je	.LBB4_8
# %bb.4:                                # %.lr.ph.preheader
	leaq	16(%rsp), %r12
	xorl	%r13d, %r13d
	xorl	%r15d, %r15d
.LBB4_6:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movq	%r12, %rdi
	movl	$100, %esi
	movq	%r14, %rdx
	callq	fgets
	movq	48(%rbx), %rax
	leaq	(%rax,%r13), %rdx
	leaq	(%rax,%r13), %rcx
	addq	$4, %rcx
	leaq	8(%rax,%r13), %r8
	leaq	(%rax,%r13), %r9
	addq	$12, %r9
	movl	$.L.str.28, %esi
	movq	%r12, %rdi
	xorl	%eax, %eax
	callq	__isoc23_sscanf
	cmpl	$4, %eax
	jne	.LBB4_7
# %bb.5:                                #   in Loop: Header=BB4_6 Depth=1
	incq	%r15
	movq	8(%rsp), %rax                   # 8-byte Reload
	movl	(%rax), %eax
	addq	$16, %r13
	cmpq	%rax, %r15
	jb	.LBB4_6
	jmp	.LBB4_8
.LBB4_7:
	xorl	%ebp, %ebp
.LBB4_8:                                # %.loopexit
	movl	%ebp, %eax
	addq	$120, %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	_Z10read_inputP8_IO_FILEP11algoparam_t, .Lfunc_end4-_Z10read_inputP8_IO_FILEP11algoparam_t
	.cfi_endproc
                                        # -- End function
	.globl	_Z12print_paramsP11algoparam_t  # -- Begin function _Z12print_paramsP11algoparam_t
	.type	_Z12print_paramsP11algoparam_t,@function
_Z12print_paramsP11algoparam_t:         # @_Z12print_paramsP11algoparam_t
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset %rbx, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movq	%rdi, %rbx
	movq	stdout(%rip), %rdi
	movl	(%rbx), %edx
	movl	$.L.str.29, %esi
	xorl	%eax, %eax
	callq	fprintf
	movq	stdout(%rip), %rdi
	movl	4(%rbx), %edx
	movl	$.L.str.30, %esi
	xorl	%eax, %eax
	callq	fprintf
	movq	stdout(%rip), %rdi
	movl	40(%rbx), %edx
	movl	$.L.str.31, %esi
	xorl	%eax, %eax
	callq	fprintf
	cmpl	$0, 40(%rbx)
	je	.LBB5_3
# %bb.1:                                # %.lr.ph
	movl	$12, %r15d
	xorl	%r14d, %r14d
.LBB5_2:                                # =>This Inner Loop Header: Depth=1
	movq	stdout(%rip), %rdi
	incq	%r14
	movq	48(%rbx), %rax
	xorps	%xmm0, %xmm0
	cvtss2sd	-12(%rax,%r15), %xmm0
	xorps	%xmm1, %xmm1
	cvtss2sd	-8(%rax,%r15), %xmm1
	xorps	%xmm2, %xmm2
	cvtss2sd	-4(%rax,%r15), %xmm2
	xorps	%xmm3, %xmm3
	cvtss2sd	(%rax,%r15), %xmm3
	movl	$.L.str.32, %esi
	movl	%r14d, %edx
	movb	$4, %al
	callq	fprintf
	movl	40(%rbx), %eax
	addq	$16, %r15
	cmpq	%rax, %r14
	jb	.LBB5_2
.LBB5_3:                                # %._crit_edge
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end5:
	.size	_Z12print_paramsP11algoparam_t, .Lfunc_end5-_Z12print_paramsP11algoparam_t
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst4,"aM",@progbits,4
	.p2align	2, 0x0                          # -- Begin function _Z10initializeP11algoparam_t
.LCPI6_0:
	.long	0x3f800000                      # float 1
	.text
	.globl	_Z10initializeP11algoparam_t
	.type	_Z10initializeP11algoparam_t,@function
_Z10initializeP11algoparam_t:           # @_Z10initializeP11algoparam_t
	.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	$24, %rsp
	.cfi_def_cfa_offset 80
	.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	%rdi, %r15
	movl	4(%rdi), %r12d
	leal	2(%r12), %ebp
	movl	%ebp, %r14d
	imull	%r14d, %r14d
	movl	$4, %edi
	movq	%r14, %rsi
	callq	calloc
	movq	%rax, %rbx
	movq	%rax, 16(%r15)
	movl	$4, %edi
	movq	%r14, %rsi
	callq	calloc
	movq	%rax, %r14
	movq	%rax, 24(%r15)
	movl	12(%r15), %esi
	addl	$2, %esi
	imull	%esi, %esi
	movl	$4, %edi
	callq	calloc
	movq	%rax, 32(%r15)
	testq	%rbx, %rbx
	je	.LBB6_32
# %bb.1:
	testq	%r14, %r14
	je	.LBB6_32
# %bb.2:
	testq	%rax, %rax
	je	.LBB6_32
# %bb.3:                                # %.preheader151
	movl	40(%r15), %eax
	movl	%ebp, %ecx
	testq	%rax, %rax
	je	.LBB6_26
# %bb.4:                                # %.preheader150.lr.ph
	incl	%r12d
	cvtsi2ss	%r12d, %xmm0
	movl	%r12d, %edx
	imull	%ebp, %edx
	movslq	%edx, %rdx
	movslq	%ebp, %rdi
	movslq	%r12d, %r8
	leaq	(%rbx,%rdx,4), %rdx
	leaq	(,%rdi,4), %rsi
	leaq	(%rbx,%rdi,4), %r9
	movq	%r9, 16(%rsp)                   # 8-byte Spill
	shlq	$2, %r8
	leaq	(%r8,%rdi,4), %rdi
	addq	%rbx, %rdi
	movq	%rdi, 8(%rsp)                   # 8-byte Spill
	xorl	%r9d, %r9d
	movss	.LCPI6_0(%rip), %xmm1           # xmm1 = mem[0],zero,zero,zero
.LBB6_5:                                # %.preheader150
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB6_7 Depth 2
                                        #     Child Loop BB6_12 Depth 2
                                        #     Child Loop BB6_17 Depth 2
                                        #     Child Loop BB6_22 Depth 2
	testl	%ebp, %ebp
	jle	.LBB6_15
# %bb.6:                                # %.lr.ph
                                        #   in Loop: Header=BB6_5 Depth=1
	movq	48(%r15), %r11
	movq	%r9, %r10
	shlq	$4, %r10
	leaq	(%r11,%r10), %rdi
	addq	$4, %rdi
	addq	%r10, %r11
	xorl	%r8d, %r8d
.LBB6_7:                                #   Parent Loop BB6_5 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	xorps	%xmm2, %xmm2
	cvtsi2ss	%r8d, %xmm2
	divss	%xmm0, %xmm2
	subss	(%r11), %xmm2
	cvtss2sd	%xmm2, %xmm2
	mulsd	%xmm2, %xmm2
	xorps	%xmm3, %xmm3
	cvtss2sd	(%rdi), %xmm3
	mulsd	%xmm3, %xmm3
	addsd	%xmm2, %xmm3
	xorps	%xmm2, %xmm2
	sqrtsd	%xmm3, %xmm2
	xorps	%xmm3, %xmm3
	cvtsd2ss	%xmm2, %xmm3
	movss	4(%rdi), %xmm2                  # xmm2 = mem[0],zero,zero,zero
	ucomiss	%xmm3, %xmm2
	jb	.LBB6_9
# %bb.8:                                #   in Loop: Header=BB6_7 Depth=2
	movaps	%xmm2, %xmm4
	subss	%xmm3, %xmm4
	divss	%xmm2, %xmm4
	mulss	8(%rdi), %xmm4
	addss	(%rbx,%r8,4), %xmm4
	movss	%xmm4, (%rbx,%r8,4)
.LBB6_9:                                #   in Loop: Header=BB6_7 Depth=2
	incq	%r8
	cmpq	%r8, %rcx
	jne	.LBB6_7
# %bb.10:                               # %.preheader149
                                        #   in Loop: Header=BB6_5 Depth=1
	testl	%ebp, %ebp
	jle	.LBB6_15
# %bb.11:                               # %.lr.ph154
                                        #   in Loop: Header=BB6_5 Depth=1
	movq	48(%r15), %rdi
	leaq	(%rdi,%r10), %r8
	addq	%r10, %rdi
	addq	$4, %rdi
	xorl	%r10d, %r10d
.LBB6_12:                               #   Parent Loop BB6_5 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	xorps	%xmm2, %xmm2
	cvtsi2ss	%r10d, %xmm2
	divss	%xmm0, %xmm2
	subss	(%r8), %xmm2
	cvtss2sd	%xmm2, %xmm2
	mulsd	%xmm2, %xmm2
	movaps	%xmm1, %xmm3
	subss	(%rdi), %xmm3
	cvtss2sd	%xmm3, %xmm3
	mulsd	%xmm3, %xmm3
	addsd	%xmm2, %xmm3
	xorps	%xmm2, %xmm2
	sqrtsd	%xmm3, %xmm2
	xorps	%xmm3, %xmm3
	cvtsd2ss	%xmm2, %xmm3
	movss	4(%rdi), %xmm2                  # xmm2 = mem[0],zero,zero,zero
	ucomiss	%xmm3, %xmm2
	jb	.LBB6_14
# %bb.13:                               #   in Loop: Header=BB6_12 Depth=2
	movaps	%xmm2, %xmm4
	subss	%xmm3, %xmm4
	divss	%xmm2, %xmm4
	mulss	8(%rdi), %xmm4
	addss	(%rdx,%r10,4), %xmm4
	movss	%xmm4, (%rdx,%r10,4)
.LBB6_14:                               #   in Loop: Header=BB6_12 Depth=2
	incq	%r10
	cmpq	%r10, %rcx
	jne	.LBB6_12
.LBB6_15:                               # %.preheader148
                                        #   in Loop: Header=BB6_5 Depth=1
	cmpl	$2, %r12d
	jl	.LBB6_25
# %bb.16:                               # %.lr.ph156
                                        #   in Loop: Header=BB6_5 Depth=1
	movq	48(%r15), %r11
	movq	%r9, %r10
	shlq	$4, %r10
	leaq	(%r11,%r10), %r13
	addq	$4, %r13
	addq	%r10, %r11
	movl	$1, %r8d
	movq	16(%rsp), %rdi                  # 8-byte Reload
.LBB6_17:                               #   Parent Loop BB6_5 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	xorps	%xmm2, %xmm2
	cvtss2sd	(%r11), %xmm2
	mulsd	%xmm2, %xmm2
	xorps	%xmm3, %xmm3
	cvtsi2ss	%r8d, %xmm3
	divss	%xmm0, %xmm3
	subss	(%r13), %xmm3
	cvtss2sd	%xmm3, %xmm3
	mulsd	%xmm3, %xmm3
	addsd	%xmm2, %xmm3
	xorps	%xmm2, %xmm2
	sqrtsd	%xmm3, %xmm2
	xorps	%xmm3, %xmm3
	cvtsd2ss	%xmm2, %xmm3
	movss	4(%r13), %xmm2                  # xmm2 = mem[0],zero,zero,zero
	ucomiss	%xmm3, %xmm2
	jb	.LBB6_19
# %bb.18:                               #   in Loop: Header=BB6_17 Depth=2
	movaps	%xmm2, %xmm4
	subss	%xmm3, %xmm4
	divss	%xmm2, %xmm4
	mulss	8(%r13), %xmm4
	addss	(%rdi), %xmm4
	movss	%xmm4, (%rdi)
.LBB6_19:                               #   in Loop: Header=BB6_17 Depth=2
	incq	%r8
	addq	%rsi, %rdi
	cmpq	%r8, %r12
	jne	.LBB6_17
# %bb.20:                               # %.preheader147
                                        #   in Loop: Header=BB6_5 Depth=1
	cmpl	$2, %r12d
	jl	.LBB6_25
# %bb.21:                               # %.lr.ph158
                                        #   in Loop: Header=BB6_5 Depth=1
	movq	48(%r15), %rdi
	leaq	(%rdi,%r10), %r11
	addq	%rdi, %r10
	addq	$4, %r10
	movl	$1, %r13d
	movq	8(%rsp), %r8                    # 8-byte Reload
.LBB6_22:                               #   Parent Loop BB6_5 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movaps	%xmm1, %xmm2
	subss	(%r11), %xmm2
	cvtss2sd	%xmm2, %xmm2
	mulsd	%xmm2, %xmm2
	xorps	%xmm3, %xmm3
	cvtsi2ss	%r13d, %xmm3
	divss	%xmm0, %xmm3
	subss	(%r10), %xmm3
	cvtss2sd	%xmm3, %xmm3
	mulsd	%xmm3, %xmm3
	addsd	%xmm2, %xmm3
	xorps	%xmm2, %xmm2
	sqrtsd	%xmm3, %xmm2
	xorps	%xmm3, %xmm3
	cvtsd2ss	%xmm2, %xmm3
	movss	4(%r10), %xmm2                  # xmm2 = mem[0],zero,zero,zero
	ucomiss	%xmm3, %xmm2
	jb	.LBB6_24
# %bb.23:                               #   in Loop: Header=BB6_22 Depth=2
	movaps	%xmm2, %xmm4
	subss	%xmm3, %xmm4
	divss	%xmm2, %xmm4
	mulss	8(%r10), %xmm4
	addss	(%r8), %xmm4
	movss	%xmm4, (%r8)
.LBB6_24:                               #   in Loop: Header=BB6_22 Depth=2
	incq	%r13
	addq	%rsi, %r8
	cmpq	%r13, %r12
	jne	.LBB6_22
.LBB6_25:                               # %._crit_edge
                                        #   in Loop: Header=BB6_5 Depth=1
	incq	%r9
	cmpq	%rax, %r9
	jne	.LBB6_5
.LBB6_26:                               # %._crit_edge160
	movl	$1, %eax
	testl	%ebp, %ebp
	jle	.LBB6_31
# %bb.27:                               # %.lr.ph164.preheader.preheader
	xorl	%edx, %edx
.LBB6_28:                               # %.lr.ph164.preheader
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB6_29 Depth 2
	xorl	%edi, %edi
	xorl	%esi, %esi
.LBB6_29:                               # %.lr.ph164
                                        #   Parent Loop BB6_28 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movss	(%rbx,%rdi,4), %xmm0            # xmm0 = mem[0],zero,zero,zero
	movss	%xmm0, (%r14,%rdi,4)
	addq	$-4, %rsi
	incq	%rdi
	cmpl	%edi, %ecx
	jne	.LBB6_29
# %bb.30:                               # %._crit_edge165
                                        #   in Loop: Header=BB6_28 Depth=1
	incl	%edx
	subq	%rsi, %rbx
	subq	%rsi, %r14
	cmpl	%ebp, %edx
	jne	.LBB6_28
.LBB6_31:                               # %.loopexit
	addq	$24, %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
.LBB6_32:
	.cfi_def_cfa_offset 80
	movq	stderr(%rip), %rcx
	movl	$.L.str.21, %edi
	movl	$30, %esi
	movl	$1, %edx
	callq	fwrite@PLT
	xorl	%eax, %eax
	jmp	.LBB6_31
.Lfunc_end6:
	.size	_Z10initializeP11algoparam_t, .Lfunc_end6-_Z10initializeP11algoparam_t
	.cfi_endproc
                                        # -- End function
	.globl	_Z8finalizeP11algoparam_t       # -- Begin function _Z8finalizeP11algoparam_t
	.type	_Z8finalizeP11algoparam_t,@function
_Z8finalizeP11algoparam_t:              # @_Z8finalizeP11algoparam_t
	.cfi_startproc
# %bb.0:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset %rbx, -16
	movq	%rdi, %rbx
	movq	16(%rdi), %rdi
	testq	%rdi, %rdi
	je	.LBB7_2
# %bb.1:
	callq	free
	movq	$0, 16(%rbx)
.LBB7_2:
	movq	24(%rbx), %rdi
	testq	%rdi, %rdi
	je	.LBB7_4
# %bb.3:
	callq	free
	movq	$0, 24(%rbx)
.LBB7_4:
	movq	32(%rbx), %rdi
	testq	%rdi, %rdi
	je	.LBB7_6
# %bb.5:
	callq	free
	movq	$0, 32(%rbx)
.LBB7_6:
	movl	$1, %eax
	popq	%rbx
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end7:
	.size	_Z8finalizeP11algoparam_t, .Lfunc_end7-_Z8finalizeP11algoparam_t
	.cfi_endproc
                                        # -- End function
	.globl	_Z7coarsenPfjjS_jj              # -- Begin function _Z7coarsenPfjjS_jj
	.type	_Z7coarsenPfjjS_jj,@function
_Z7coarsenPfjjS_jj:                     # @_Z7coarsenPfjjS_jj
	.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
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movl	%edx, %r11d
	movl	$1, %r10d
	movl	%esi, %ebx
	cmpl	%r8d, %esi
	jbe	.LBB8_2
# %bb.1:
	movl	%esi, %eax
	xorl	%edx, %edx
	divl	%r8d
	movl	%eax, %r10d
	movl	%r8d, %ebx
.LBB8_2:
	movl	$1, %eax
	cmpl	%r9d, %r11d
	jbe	.LBB8_3
# %bb.4:
	movl	%r11d, %eax
	xorl	%edx, %edx
	divl	%r9d
	jmp	.LBB8_5
.LBB8_3:
	movl	%r11d, %r9d
.LBB8_5:
	cmpl	$2, %r9d
	jl	.LBB8_11
# %bb.6:                                # %.preheader.lr.ph
	leal	-1(%rbx), %edx
	imull	%esi, %eax
	addl	$-2, %r9d
	xorl	%esi, %esi
	xorl	%r11d, %r11d
	xorl	%r14d, %r14d
.LBB8_7:                                # %.preheader
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB8_9 Depth 2
	cmpl	$2, %ebx
	jl	.LBB8_10
# %bb.8:                                # %.lr.ph
                                        #   in Loop: Header=BB8_7 Depth=1
	movq	%rdx, %r15
	movl	%esi, %ebp
	movl	%r11d, %r12d
.LBB8_9:                                #   Parent Loop BB8_7 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movl	%ebp, %r13d
	movss	(%rdi,%r13,4), %xmm0            # xmm0 = mem[0],zero,zero,zero
	movl	%r12d, %r13d
	movss	%xmm0, (%rcx,%r13,4)
	incl	%r12d
	addl	%r10d, %ebp
	decq	%r15
	jne	.LBB8_9
.LBB8_10:                               # %._crit_edge
                                        #   in Loop: Header=BB8_7 Depth=1
	leal	1(%r14), %ebp
	addl	%r8d, %r11d
	addl	%eax, %esi
	cmpl	%r9d, %r14d
	movl	%ebp, %r14d
	jne	.LBB8_7
.LBB8_11:                               # %._crit_edge39
	movl	$1, %eax
	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_end8:
	.size	_Z7coarsenPfjjS_jj, .Lfunc_end8-_Z7coarsenPfjjS_jj
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst4,"aM",@progbits,4
	.p2align	2, 0x0                          # -- Begin function _Z11write_imageP8_IO_FILEPfjj
.LCPI9_0:
	.long	0xff800000                      # float -Inf
.LCPI9_1:
	.long	0x7f800000                      # float +Inf
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0
.LCPI9_2:
	.quad	0x408ff80000000000              # double 1023
	.text
	.globl	_Z11write_imageP8_IO_FILEPfjj
	.type	_Z11write_imageP8_IO_FILEPfjj,@function
_Z11write_imageP8_IO_FILEPfjj:          # @_Z11write_imageP8_IO_FILEPfjj
	.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	$3112, %rsp                     # imm = 0xC28
	.cfi_def_cfa_offset 3168
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movl	%ecx, %ebp
	movl	%edx, 4(%rsp)                   # 4-byte Spill
	movq	%rsi, %r14
	movq	%rdi, %r15
	movl	$768, %r13d                     # imm = 0x300
	leaq	32(%rsp), %rax
	leaq	(%rax,%r13), %rdi
	movl	$256, %edx                      # imm = 0x100
	movl	$255, %esi
	callq	memset@PLT
	leaq	1056(%rsp), %r12
	addq	%r12, %r13
	xorl	%ebx, %ebx
	movl	$256, %edx                      # imm = 0x100
	movq	%r13, %rdi
	xorl	%esi, %esi
	callq	memset@PLT
	leaq	3103(%rsp), %rax
.LBB9_1:                                # =>This Inner Loop Header: Depth=1
	movb	%bl, (%rax)
	incl	%ebx
	decq	%rax
	cmpl	$256, %ebx                      # imm = 0x100
	jne	.LBB9_1
# %bb.2:                                # %.preheader95.preheader
	movl	$512, %ebx                      # imm = 0x200
	leaq	2080(%rsp), %r13
	leaq	(%rbx,%r13), %rdi
	movl	$256, %edx                      # imm = 0x100
	movl	$255, %esi
	callq	memset@PLT
	addq	%rbx, %r12
	movl	$256, %edx                      # imm = 0x100
	movq	%r12, %rdi
	xorl	%esi, %esi
	callq	memset@PLT
	movl	$256, %eax                      # imm = 0x100
	leaq	799(%rsp), %rcx
.LBB9_3:                                # %.preheader95
                                        # =>This Inner Loop Header: Depth=1
	decl	%eax
	movb	%al, (%rcx)
	decq	%rcx
	testl	%eax, %eax
	jne	.LBB9_3
# %bb.4:                                # %.preheader94.preheader
	movl	$256, %r12d                     # imm = 0x100
	leaq	32(%rsp), %rdi
	addq	%r12, %rdi
	xorl	%ebx, %ebx
	movl	$256, %edx                      # imm = 0x100
	xorl	%esi, %esi
	callq	memset@PLT
	addq	%r12, %r13
	movl	$256, %edx                      # imm = 0x100
	movq	%r13, %rdi
	movl	$255, %esi
	callq	memset@PLT
	leaq	1567(%rsp), %rax
.LBB9_5:                                # %.preheader94
                                        # =>This Inner Loop Header: Depth=1
	movb	%bl, (%rax)
	incl	%ebx
	decq	%rax
	cmpl	$256, %ebx                      # imm = 0x100
	jne	.LBB9_5
# %bb.6:                                # %.preheader93.preheader
	leaq	32(%rsp), %rdi
	movl	$256, %edx                      # imm = 0x100
	xorl	%esi, %esi
	callq	memset@PLT
	leaq	1056(%rsp), %rdi
	movl	$256, %edx                      # imm = 0x100
	movl	$255, %esi
	callq	memset@PLT
	movl	$255, %eax
	movl	$256, %ecx                      # imm = 0x100
	movl	4(%rsp), %r12d                  # 4-byte Reload
.LBB9_7:                                # %.preheader93
                                        # =>This Inner Loop Header: Depth=1
	movb	%al, 2080(%rsp,%rax)
	decq	%rax
	decl	%ecx
	jne	.LBB9_7
# %bb.8:                                # %.preheader92
	movl	%r12d, %eax
	movq	%rax, 24(%rsp)                  # 8-byte Spill
	testl	%ebp, %ebp
	je	.LBB9_9
# %bb.10:                               # %.preheader91.lr.ph
	movss	.LCPI9_1(%rip), %xmm1           # xmm1 = mem[0],zero,zero,zero
	movss	.LCPI9_0(%rip), %xmm0           # xmm0 = mem[0],zero,zero,zero
	xorl	%eax, %eax
	xorl	%ecx, %ecx
.LBB9_11:                               # %.preheader91
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB9_14 Depth 2
	testl	%r12d, %r12d
	je	.LBB9_12
# %bb.13:                               # %.lr.ph
                                        #   in Loop: Header=BB9_11 Depth=1
	movq	24(%rsp), %rdx                  # 8-byte Reload
	movl	%eax, %esi
.LBB9_14:                               #   Parent Loop BB9_11 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movl	%esi, %edi
	movss	(%r14,%rdi,4), %xmm2            # xmm2 = mem[0],zero,zero,zero
	movaps	%xmm2, %xmm3
	maxss	%xmm0, %xmm3
	minss	%xmm1, %xmm2
	incl	%esi
	movaps	%xmm3, %xmm0
	movaps	%xmm2, %xmm1
	decq	%rdx
	jne	.LBB9_14
	jmp	.LBB9_15
.LBB9_12:                               #   in Loop: Header=BB9_11 Depth=1
	movaps	%xmm1, %xmm2
	movaps	%xmm0, %xmm3
.LBB9_15:                               # %._crit_edge
                                        #   in Loop: Header=BB9_11 Depth=1
	incl	%ecx
	addl	%r12d, %eax
	movaps	%xmm3, %xmm0
	movaps	%xmm2, %xmm1
	cmpl	%ebp, %ecx
	jne	.LBB9_11
	jmp	.LBB9_16
.LBB9_9:
	movss	.LCPI9_0(%rip), %xmm3           # xmm3 = mem[0],zero,zero,zero
	movss	.LCPI9_1(%rip), %xmm2           # xmm2 = mem[0],zero,zero,zero
.LBB9_16:                               # %._crit_edge111
	movss	%xmm3, 8(%rsp)                  # 4-byte Spill
	movss	%xmm2, (%rsp)                   # 4-byte Spill
	movl	$.L.str.22, %edi
	movl	$3, %esi
	movl	$1, %edx
	movq	%r15, %rcx
	callq	fwrite@PLT
	movl	$.L.str.23, %esi
	movq	%r15, %rdi
	movl	%r12d, %edx
	movl	%ebp, %ecx
	xorl	%eax, %eax
	callq	fprintf
	movl	$.L.str.24, %esi
	movq	%r15, %rdi
	movl	$255, %edx
	xorl	%eax, %eax
	callq	fprintf
	movl	%ebp, 20(%rsp)                  # 4-byte Spill
	testl	%ebp, %ebp
	je	.LBB9_22
# %bb.17:                               # %.preheader.lr.ph
	movss	8(%rsp), %xmm0                  # 4-byte Reload
                                        # xmm0 = mem[0],zero,zero,zero
	subss	(%rsp), %xmm0                   # 4-byte Folded Reload
	cvtss2sd	%xmm0, %xmm0
	movsd	%xmm0, 8(%rsp)                  # 8-byte Spill
	xorl	%ebp, %ebp
	xorl	%ebx, %ebx
.LBB9_18:                               # %.preheader
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB9_20 Depth 2
	testl	%r12d, %r12d
	movss	(%rsp), %xmm1                   # 4-byte Reload
                                        # xmm1 = mem[0],zero,zero,zero
	movsd	8(%rsp), %xmm2                  # 8-byte Reload
                                        # xmm2 = mem[0],zero
	movsd	.LCPI9_2(%rip), %xmm3           # xmm3 = mem[0],zero
	je	.LBB9_21
# %bb.19:                               # %.lr.ph115
                                        #   in Loop: Header=BB9_18 Depth=1
	movq	24(%rsp), %r12                  # 8-byte Reload
	movl	%ebp, %r13d
.LBB9_20:                               #   Parent Loop BB9_18 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movl	%r13d, %eax
	movss	(%r14,%rax,4), %xmm0            # xmm0 = mem[0],zero,zero,zero
	subss	%xmm1, %xmm0
	cvtss2sd	%xmm0, %xmm0
	mulsd	%xmm3, %xmm0
	divsd	%xmm2, %xmm0
	cvttsd2si	%xmm0, %eax
	cltq
	movzbl	32(%rsp,%rax), %edx
	movzbl	2080(%rsp,%rax), %ecx
	movzbl	1056(%rsp,%rax), %r8d
	movl	$.L.str.25, %esi
	movq	%r15, %rdi
	xorl	%eax, %eax
	callq	fprintf
	movsd	.LCPI9_2(%rip), %xmm3           # xmm3 = mem[0],zero
	movsd	8(%rsp), %xmm2                  # 8-byte Reload
                                        # xmm2 = mem[0],zero
	movss	(%rsp), %xmm1                   # 4-byte Reload
                                        # xmm1 = mem[0],zero,zero,zero
	incl	%r13d
	decq	%r12
	jne	.LBB9_20
.LBB9_21:                               # %._crit_edge116
                                        #   in Loop: Header=BB9_18 Depth=1
	movl	$10, %edi
	movq	%r15, %rsi
	callq	fputc@PLT
	incl	%ebx
	movl	4(%rsp), %r12d                  # 4-byte Reload
	addl	%r12d, %ebp
	cmpl	20(%rsp), %ebx                  # 4-byte Folded Reload
	jne	.LBB9_18
.LBB9_22:                               # %._crit_edge118
	addq	$3112, %rsp                     # imm = 0xC28
	.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_end9:
	.size	_Z11write_imageP8_IO_FILEPfjj, .Lfunc_end9-_Z11write_imageP8_IO_FILEPfjj
	.cfi_endproc
                                        # -- End function
	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"Usage: %s <input file> -t threads -b blocks\n"
	.size	.L.str, 45

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"       -t number of threads per block in each dimension (e.g. 16)\n"
	.size	.L.str.1, 67

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"r"
	.size	.L.str.2, 2

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"\nError: Cannot open \"%s\" for reading.\n\n"
	.size	.L.str.3, 40

	.type	.L.str.4,@object                # @.str.4
.L.str.4:
	.asciz	"heat.ppm"
	.size	.L.str.4, 9

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"w"
	.size	.L.str.5, 2

	.type	.L.str.6,@object                # @.str.6
.L.str.6:
	.asciz	"\nError: Cannot open \"%s\" for writing.\n\n"
	.size	.L.str.6, 40

	.type	.L.str.7,@object                # @.str.7
.L.str.7:
	.asciz	"\nError: Error parsing input file.\n\n"
	.size	.L.str.7, 36

	.type	.L.str.8,@object                # @.str.8
.L.str.8:
	.asciz	"-t"
	.size	.L.str.8, 3

	.type	.L.str.10,@object               # @.str.10
.L.str.10:
	.asciz	"\nSolving Heat equation on the CPU and the GPU\n"
	.size	.L.str.10, 47

	.type	.L.str.11,@object               # @.str.11
.L.str.11:
	.asciz	"--------------------------------------------\n"
	.size	.L.str.11, 46

	.type	.L.str.12,@object               # @.str.12
.L.str.12:
	.asciz	"\nExecution on CPU (sequential)\n-----------------------------\n"
	.size	.L.str.12, 62

	.type	.L.str.13,@object               # @.str.13
.L.str.13:
	.asciz	"Error in Solver initialization.\n\n"
	.size	.L.str.13, 34

	.type	.L.str.14,@object               # @.str.14
.L.str.14:
	.asciz	"Time on CPU in ms.= %f "
	.size	.L.str.14, 24

	.type	.L.str.15,@object               # @.str.15
.L.str.15:
	.asciz	"(%3.3f GFlop => %6.2f MFlop/s)\n"
	.size	.L.str.15, 32

	.type	.L.str.16,@object               # @.str.16
.L.str.16:
	.asciz	"Convergence to residual=%f: %d iterations\n"
	.size	.L.str.16, 43

	.type	.L.str.17,@object               # @.str.17
.L.str.17:
	.asciz	"\nExecution on GPU\n----------------\n"
	.size	.L.str.17, 36

	.type	.L.str.18,@object               # @.str.18
.L.str.18:
	.asciz	"Number of threads per block in each dimension = %d\n"
	.size	.L.str.18, 52

	.type	.L.str.19,@object               # @.str.19
.L.str.19:
	.asciz	"Number of blocks per grid in each dimension   = %d\n"
	.size	.L.str.19, 52

	.type	.L.str.20,@object               # @.str.20
.L.str.20:
	.asciz	"\nTime on GPU in ms. = %f "
	.size	.L.str.20, 26

	.type	.L.str.21,@object               # @.str.21
.L.str.21:
	.asciz	"Error: Cannot allocate memory\n"
	.size	.L.str.21, 31

	.type	.L.str.22,@object               # @.str.22
.L.str.22:
	.asciz	"P3\n"
	.size	.L.str.22, 4

	.type	.L.str.23,@object               # @.str.23
.L.str.23:
	.asciz	"%u %u\n"
	.size	.L.str.23, 7

	.type	.L.str.24,@object               # @.str.24
.L.str.24:
	.asciz	"%u\n"
	.size	.L.str.24, 4

	.type	.L.str.25,@object               # @.str.25
.L.str.25:
	.asciz	"%d %d %d  "
	.size	.L.str.25, 11

	.type	.L.str.27,@object               # @.str.27
.L.str.27:
	.asciz	"%u"
	.size	.L.str.27, 3

	.type	.L.str.28,@object               # @.str.28
.L.str.28:
	.asciz	"%f %f %f %f"
	.size	.L.str.28, 12

	.type	.L.str.29,@object               # @.str.29
.L.str.29:
	.asciz	"Iterations        : %u\n"
	.size	.L.str.29, 24

	.type	.L.str.30,@object               # @.str.30
.L.str.30:
	.asciz	"Resolution        : %u\n"
	.size	.L.str.30, 24

	.type	.L.str.31,@object               # @.str.31
.L.str.31:
	.asciz	"Num. Heat sources : %u\n"
	.size	.L.str.31, 24

	.type	.L.str.32,@object               # @.str.32
.L.str.32:
	.asciz	"  %2d: (%2.2f, %2.2f) %2.2f %2.2f \n"
	.size	.L.str.32, 36

	.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	"Error -- too many threads in block, try again"
	.size	.Lstr, 46

	.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
