/**
 * GA Approximate: Try to approximate a simple function using Genetic Algorithm
 *
 **/

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

// For the CUDA runtime routines (prefixed with "cuda_")
#include <cuda_runtime.h>

/**
 * Macros to configure experiment
 */
#define POPULATION_SIZE 300
#define GENOTYPE_SIZE 3
#define MAX_NUMBER_OF_GENERATIONS 200
#define CROSSOVER_PROB 0.8
#define CROSSING_PROB 0.5
#define MUTATION_PROB 0.1
#define SAMPLING_POINTS 10
#define RAND_01 ((double)rand()/RAND_MAX)

void mutate(float* chromosome)
{
	for (int i = 0; i < GENOTYPE_SIZE; i++)
	{
		if (RAND_01 < MUTATION_PROB)
			chromosome[i] += 2 * RAND_01 - 1;
	}
}

void crossOver(float* chrom1, float* chrom2, float* newChrom1, float* newChrom2)
{
	for (int i = 0; i < GENOTYPE_SIZE; i++)
	{
		if (RAND_01 < CROSSING_PROB)
		{
			newChrom1[i] = chrom1[i];
			newChrom2[i] = chrom2[i];
		}
		else
		{
			newChrom1[i] = chrom2[i];
			newChrom2[i] = chrom1[i];
		}
	}
}

int rouletteWheel(float* fitness_values, double sum_fitness_values)
{
	double offsetSelection = 0;

	double chooseValue = RAND_01;

	for (int i = 0; i < POPULATION_SIZE; i++)
	{
		offsetSelection += (fitness_values[i] / sum_fitness_values);

		if (offsetSelection >= chooseValue)
		{
			return i;
		}
	}

	return POPULATION_SIZE-1;
}

__host__ __device__ float targetFunction(float value)
{
	// 10 * x^2 + 3 * x + 10
	return 10 * value * value + 3 * value - 10;
}

/**
 * CUDA Kernel Device code
 *
 * Run Genetic Algorithm experiments
 */
__global__ void RunGAIteration(float *population, float *fitness_values)
{
	// Get chromosome id
	int c_id = threadIdx.x;

	float *chromosome = population + (c_id * GENOTYPE_SIZE);
	float output, desiredOutput, testPoint;

	float fitness_value_of_instance = 0;

	for (int i = 0; i < SAMPLING_POINTS; i++)
	{
		testPoint = i * (20 / SAMPLING_POINTS) - 10;
		desiredOutput = targetFunction(testPoint);

		output = chromosome[0];

		for (int g = 1; g < GENOTYPE_SIZE; g++)
		{
			output += chromosome[g] * testPoint;
			testPoint *= testPoint;
		}

		fitness_value_of_instance += (desiredOutput - output) * (desiredOutput - output);
	}

	fitness_value_of_instance /= SAMPLING_POINTS;

	fitness_values[c_id] = fitness_value_of_instance;
}

/**
 * Host main routine
 */
int main(void)
{
	// Error code to check return values for CUDA calls
	cudaError_t err = cudaSuccess;

	float *population, *fitness_values;
	int POPULATION_MATRIX_SIZE = POPULATION_SIZE * GENOTYPE_SIZE;
	float FITNESS_VALUES_SIZE = POPULATION_SIZE;

	float *newGeneration = (float*) malloc(POPULATION_MATRIX_SIZE * sizeof(float));

	cudaMallocManaged(&population, POPULATION_MATRIX_SIZE * sizeof(float));
	cudaMallocManaged(&fitness_values, FITNESS_VALUES_SIZE * sizeof(float));

	// Generate a first random population
	for (int i = 0; i < POPULATION_SIZE; i++) {
		for (int g = 0; g < GENOTYPE_SIZE; g++) {
			population[i*GENOTYPE_SIZE + g] = (rand() % 100 - 50) / 10.0;
		}
	}

	// Run GA
	double fitness_values_sum;

	int c_id1, c_id2;

	float *chromosome1, *chromosome2, *newChromosome1, *newChromosome2;

	float bestFitness = FLT_MAX, bestFitnessOnGeneration = FLT_MAX;

	for (int gen = 0; gen < MAX_NUMBER_OF_GENERATIONS; gen++)
	{
		RunGAIteration<<<1, POPULATION_SIZE>>>(population, fitness_values);
		cudaDeviceSynchronize();

		err = cudaGetLastError();

		if (err != cudaSuccess)
		{
			fprintf(stderr, "Failed to launch RunGAIteration kernel (error code %s)!\n",
					cudaGetErrorString(err));
			exit(EXIT_FAILURE);
		}

		// Get best fitness value
		bestFitnessOnGeneration = FLT_MAX;
		for (int i = 0; i < POPULATION_SIZE; i++)
		{
			if (fitness_values[i] < bestFitness)
			{
				bestFitness = fitness_values[i];
			}

			if (fitness_values[i] < bestFitnessOnGeneration)
			{
				bestFitnessOnGeneration = fitness_values[i];
			}
		}
		printf("The best fitness value on generation %d is %.2f.\n", gen, bestFitnessOnGeneration);
		printf("The best fitness value until now is %.2f.\n", gen, bestFitness);

		/** Get new generation **/

		// Verify sum of fitness values
		fitness_values_sum = 0;

		for (int i = 0; i < POPULATION_SIZE; i++)
			fitness_values_sum += fitness_values[i];

		for (int i = 0; i < POPULATION_SIZE; i+=2)
		{
			// Select a pair chromosomes using RouletteWheel
			c_id1 = rouletteWheel(fitness_values, fitness_values_sum);
			c_id2 = rouletteWheel(fitness_values, fitness_values_sum);

			chromosome1 = population + c_id1;
			chromosome2 = population + c_id2;

			newChromosome1 = newGeneration + (i*GENOTYPE_SIZE);
			newChromosome2 = newGeneration + ((i+1)*GENOTYPE_SIZE);

			// Crossover
			if (RAND_01 <= CROSSOVER_PROB)
			{
				// Do Crossover
				crossOver(chromosome1, chromosome2, newChromosome1, newChromosome2);
			}
			else
			{
				// If not crossover, just copy the 2 chromosomes taken with Roulette Wheel method
				memcpy(newChromosome1, chromosome1, GENOTYPE_SIZE);
				memcpy(newChromosome2, chromosome2, GENOTYPE_SIZE);
			}

			// Mutate genes of generated pair of chromosomes
			mutate(newChromosome1);
			mutate(newChromosome2);
		}

		for (int i = 0; i < POPULATION_MATRIX_SIZE; i++)
		{
			population[i] = newGeneration[i];
		}
	}

	// Verify each chromosome
	//	for (int i = 0; i < POPULATION_SIZE; i++)
	//		printf("Chromosome %d got fitness value of %.2f.\n", i, fitness_values[i]);

	free(newGeneration);
	cudaFree(population);
	cudaFree(fitness_values);

	return 0;
}


	.file	"tmpxft_002fe95b_00000000-6_gaapproximate.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2034:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2034:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z6mutatePf
	.type	_Z6mutatePf, @function
_Z6mutatePf:
.LFB2027:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rdi, %rbp
	pushq	%rbx
	.cfi_def_cfa_offset 24
	.cfi_offset 3, -24
	xorl	%ebx, %ebx
	pushq	%rdx
	.cfi_def_cfa_offset 32
.L5:
	call	rand@PLT
	movsd	.LC1(%rip), %xmm1
	cvtsi2sdl	%eax, %xmm0
	divsd	.LC0(%rip), %xmm0
	comisd	%xmm0, %xmm1
	jbe	.L3
	call	rand@PLT
	cvtss2sd	0(%rbp,%rbx,4), %xmm1
	cvtsi2sdl	%eax, %xmm0
	divsd	.LC0(%rip), %xmm0
	addsd	%xmm0, %xmm0
	subsd	.LC2(%rip), %xmm0
	addsd	%xmm1, %xmm0
	cvtsd2ss	%xmm0, %xmm0
	movss	%xmm0, 0(%rbp,%rbx,4)
.L3:
	incq	%rbx
	cmpq	$3, %rbx
	jne	.L5
	popq	%rax
	.cfi_def_cfa_offset 24
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2027:
	.size	_Z6mutatePf, .-_Z6mutatePf
	.globl	_Z9crossOverPfS_S_S_
	.type	_Z9crossOverPfS_S_S_, @function
_Z9crossOverPfS_S_S_:
.LFB2028:
	.cfi_startproc
	endbr64
	pushq	%r14
	.cfi_def_cfa_offset 16
	.cfi_offset 14, -16
	movq	%rcx, %r14
	pushq	%r13
	.cfi_def_cfa_offset 24
	.cfi_offset 13, -24
	movq	%rdx, %r13
	pushq	%r12
	.cfi_def_cfa_offset 32
	.cfi_offset 12, -32
	movq	%rsi, %r12
	pushq	%rbp
	.cfi_def_cfa_offset 40
	.cfi_offset 6, -40
	movq	%rdi, %rbp
	pushq	%rbx
	.cfi_def_cfa_offset 48
	.cfi_offset 3, -48
	xorl	%ebx, %ebx
.L14:
	call	rand@PLT
	movsd	.LC3(%rip), %xmm1
	cvtsi2sdl	%eax, %xmm0
	divsd	.LC0(%rip), %xmm0
	comisd	%xmm0, %xmm1
	jbe	.L17
	movss	0(%rbp,%rbx), %xmm0
	movss	%xmm0, 0(%r13,%rbx)
	movss	(%r12,%rbx), %xmm0
	jmp	.L13
.L17:
	movss	(%r12,%rbx), %xmm0
	movss	%xmm0, 0(%r13,%rbx)
	movss	0(%rbp,%rbx), %xmm0
.L13:
	movss	%xmm0, (%r14,%rbx)
	addq	$4, %rbx
	cmpq	$12, %rbx
	jne	.L14
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%rbp
	.cfi_def_cfa_offset 32
	popq	%r12
	.cfi_def_cfa_offset 24
	popq	%r13
	.cfi_def_cfa_offset 16
	popq	%r14
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2028:
	.size	_Z9crossOverPfS_S_S_, .-_Z9crossOverPfS_S_S_
	.globl	_Z13rouletteWheelPfd
	.type	_Z13rouletteWheelPfd, @function
_Z13rouletteWheelPfd:
.LFB2029:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	movq	%rdi, %rbx
	subq	$16, %rsp
	.cfi_def_cfa_offset 32
	movsd	%xmm0, 8(%rsp)
	call	rand@PLT
	xorps	%xmm1, %xmm1
	cvtsi2sdl	%eax, %xmm0
	xorl	%eax, %eax
	divsd	.LC0(%rip), %xmm0
.L21:
	cvtss2sd	(%rbx,%rax,4), %xmm2
	divsd	8(%rsp), %xmm2
	addsd	%xmm2, %xmm1
	comisd	%xmm0, %xmm1
	jnb	.L19
	incq	%rax
	cmpq	$300, %rax
	jne	.L21
	movl	$299, %eax
.L19:
	addq	$16, %rsp
	.cfi_def_cfa_offset 16
	popq	%rbx
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2029:
	.size	_Z13rouletteWheelPfd, .-_Z13rouletteWheelPfd
	.globl	_Z14targetFunctionf
	.type	_Z14targetFunctionf, @function
_Z14targetFunctionf:
.LFB2030:
	.cfi_startproc
	endbr64
	movss	.LC5(%rip), %xmm2
	movaps	%xmm0, %xmm1
	mulss	%xmm2, %xmm1
	mulss	%xmm0, %xmm1
	mulss	.LC6(%rip), %xmm0
	addss	%xmm1, %xmm0
	subss	%xmm2, %xmm0
	ret
	.cfi_endproc
.LFE2030:
	.size	_Z14targetFunctionf, .-_Z14targetFunctionf
	.globl	_Z36__device_stub__Z14RunGAIterationPfS_PfS_
	.type	_Z36__device_stub__Z14RunGAIterationPfS_PfS_, @function
_Z36__device_stub__Z14RunGAIterationPfS_PfS_:
.LFB2056:
	.cfi_startproc
	endbr64
	subq	$120, %rsp
	.cfi_def_cfa_offset 128
	movq	%rdi, 8(%rsp)
	leaq	32(%rsp), %rcx
	leaq	24(%rsp), %rdx
	movq	%rsi, (%rsp)
	leaq	40(%rsp), %rdi
	leaq	52(%rsp), %rsi
	movq	%fs:40, %rax
	movq	%rax, 104(%rsp)
	xorl	%eax, %eax
	leaq	8(%rsp), %rax
	movl	$1, 48(%rsp)
	movq	%rax, 88(%rsp)
	movq	%rsp, %rax
	movq	%rax, 96(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 40(%rsp)
	movq	%rax, 52(%rsp)
	movl	$1, 60(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L26
	pushq	32(%rsp)
	.cfi_def_cfa_offset 136
	leaq	_Z14RunGAIterationPfS_(%rip), %rdi
	pushq	32(%rsp)
	.cfi_def_cfa_offset 144
	movq	68(%rsp), %rcx
	movl	76(%rsp), %r8d
	movq	56(%rsp), %rsi
	movl	64(%rsp), %edx
	leaq	104(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 136
	popq	%rdx
	.cfi_def_cfa_offset 128
.L26:
	movq	104(%rsp), %rax
	subq	%fs:40, %rax
	je	.L28
	call	__stack_chk_fail@PLT
.L28:
	addq	$120, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2056:
	.size	_Z36__device_stub__Z14RunGAIterationPfS_PfS_, .-_Z36__device_stub__Z14RunGAIterationPfS_PfS_
	.globl	_Z14RunGAIterationPfS_
	.type	_Z14RunGAIterationPfS_, @function
_Z14RunGAIterationPfS_:
.LFB2057:
	.cfi_startproc
	endbr64
	jmp	_Z36__device_stub__Z14RunGAIterationPfS_PfS_
	.cfi_endproc
.LFE2057:
	.size	_Z14RunGAIterationPfS_, .-_Z14RunGAIterationPfS_
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC9:
	.string	"Failed to launch RunGAIteration kernel (error code %s)!\n"
.LC10:
	.string	"The best fitness value on generation %d is %.2f.\n"
.LC11:
	.string	"The best fitness value until now is %.2f.\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2031:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	movl	$3600, %edi
	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
	xorl	%r12d, %r12d
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	subq	$88, %rsp
	.cfi_def_cfa_offset 144
	movq	%fs:40, %rax
	movq	%rax, 72(%rsp)
	xorl	%eax, %eax
	call	malloc@PLT
	leaq	32(%rsp), %rdi
	movl	$1, %edx
	movl	$3600, %esi
	movq	%rax, %rbp
	call	cudaMallocManaged@PLT
	leaq	40(%rsp), %rdi
	movl	$1, %edx
	movl	$1200, %esi
	call	cudaMallocManaged@PLT
.L32:
	xorl	%ebx, %ebx
.L33:
	call	rand@PLT
	movl	$100, %esi
	leaq	0(,%rbx,4), %rcx
	incq	%rbx
	cltd
	addq	32(%rsp), %rcx
	idivl	%esi
	subl	$50, %edx
	cvtsi2sdl	%edx, %xmm0
	divsd	.LC8(%rip), %xmm0
	cvtsd2ss	%xmm0, %xmm0
	movss	%xmm0, (%rcx,%r12)
	cmpq	$3, %rbx
	jne	.L33
	addq	$12, %r12
	cmpq	$3600, %r12
	jne	.L32
	movss	.LC7(%rip), %xmm3
	xorl	%r12d, %r12d
	movabsq	$4294967596, %r15
	movss	%xmm3, 12(%rsp)
.L34:
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movq	%r15, %rdx
	movl	$1, %ecx
	movabsq	$4294967297, %rdi
	movl	$1, %esi
	movq	%r15, 60(%rsp)
	movl	$1, 68(%rsp)
	movq	%rdi, 48(%rsp)
	movl	$1, 56(%rsp)
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L35
	movq	40(%rsp), %rsi
	movq	32(%rsp), %rdi
	call	_Z36__device_stub__Z14RunGAIterationPfS_PfS_
.L35:
	call	cudaDeviceSynchronize@PLT
	call	cudaGetLastError@PLT
	movl	%eax, %edi
	testl	%eax, %eax
	jne	.L36
	movq	40(%rsp), %rdx
	movss	.LC7(%rip), %xmm0
	xorl	%eax, %eax
	jmp	.L37
.L36:
	call	cudaGetErrorString@PLT
	movq	stderr(%rip), %rdi
	movl	$2, %esi
	leaq	.LC9(%rip), %rdx
	movq	%rax, %rcx
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	movl	$1, %edi
	call	exit@PLT
.L37:
	movss	(%rdx,%rax,4), %xmm1
	incq	%rax
	movaps	%xmm1, %xmm2
	minss	%xmm0, %xmm1
	minss	12(%rsp), %xmm2
	movaps	%xmm1, %xmm0
	movss	%xmm2, 12(%rsp)
	cmpq	$300, %rax
	jne	.L37
	movl	%r12d, %edx
	leaq	.LC10(%rip), %rsi
	movl	$2, %edi
	movb	$1, %al
	cvtss2sd	%xmm1, %xmm0
	call	__printf_chk@PLT
	movl	%r12d, %edx
	movl	$2, %edi
	leaq	.LC11(%rip), %rsi
	movb	$1, %al
	cvtss2sd	12(%rsp), %xmm0
	call	__printf_chk@PLT
	movq	$0x000000000, (%rsp)
	movq	40(%rsp), %rdx
	xorl	%eax, %eax
.L40:
	cvtss2sd	(%rdx,%rax,4), %xmm0
	addsd	(%rsp), %xmm0
	incq	%rax
	movsd	%xmm0, (%rsp)
	cmpq	$300, %rax
	jne	.L40
	movq	%rbp, %rbx
	movl	$3588, %r14d
.L44:
	movsd	(%rsp), %xmm0
	movq	40(%rsp), %rdi
	call	_Z13rouletteWheelPfd
	movsd	(%rsp), %xmm0
	movq	40(%rsp), %rdi
	movslq	%eax, %r13
	call	_Z13rouletteWheelPfd
	movq	32(%rsp), %rdx
	cltq
	leaq	(%rdx,%r13,4), %rdi
	leaq	(%rdx,%rax,4), %rsi
	movq	%rdi, 24(%rsp)
	leaq	12(%rbx), %r13
	movq	%rsi, 16(%rsp)
	call	rand@PLT
	movq	16(%rsp), %rsi
	movq	24(%rsp), %rdi
	movsd	.LC12(%rip), %xmm1
	cvtsi2sdl	%eax, %xmm0
	divsd	.LC0(%rip), %xmm0
	comisd	%xmm0, %xmm1
	jb	.L55
	movq	%r13, %rcx
	movq	%rbx, %rdx
	call	_Z9crossOverPfS_S_S_
	jmp	.L43
.L55:
	movw	(%rdi), %ax
	movq	%r14, %rcx
	movl	$3, %edx
	movw	%ax, (%rbx)
	movb	2(%rdi), %al
	movq	%r13, %rdi
	movb	%al, 2(%rbx)
	call	__memcpy_chk@PLT
.L43:
	movq	%rbx, %rdi
	subq	$24, %r14
	addq	$24, %rbx
	call	_Z6mutatePf
	movq	%r13, %rdi
	call	_Z6mutatePf
	cmpq	$-12, %r14
	jne	.L44
	movq	32(%rsp), %rbx
	incl	%r12d
	movl	$900, %ecx
	movq	%rbp, %rsi
	movq	%rbx, %rdi
	rep movsl
	cmpl	$200, %r12d
	jne	.L34
	movq	%rbp, %rdi
	call	free@PLT
	movq	%rbx, %rdi
	call	cudaFree@PLT
	movq	40(%rsp), %rdi
	call	cudaFree@PLT
	movq	72(%rsp), %rax
	subq	%fs:40, %rax
	je	.L45
	call	__stack_chk_fail@PLT
.L45:
	addq	$88, %rsp
	.cfi_def_cfa_offset 56
	xorl	%eax, %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
.LFE2031:
	.size	main, .-main
	.section	.rodata.str1.1
.LC13:
	.string	"_Z14RunGAIterationPfS_"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2059:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	leaq	_ZL15__fatDeviceText(%rip), %rdi
	call	__cudaRegisterFatBinary@PLT
	pushq	$0
	.cfi_def_cfa_offset 24
	xorl	%r9d, %r9d
	orl	$-1, %r8d
	pushq	$0
	.cfi_def_cfa_offset 32
	leaq	.LC13(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z14RunGAIterationPfS_(%rip), %rsi
	pushq	$0
	.cfi_def_cfa_offset 40
	movq	%rdx, %rcx
	pushq	$0
	.cfi_def_cfa_offset 48
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	call	__cudaRegisterFunction@PLT
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	call	__cudaRegisterFatBinaryEnd@PLT
	leaq	_ZL26__cudaUnregisterBinaryUtilv(%rip), %rdi
	popq	%rdx
	.cfi_def_cfa_offset 8
	jmp	atexit@PLT
	.cfi_endproc
.LFE2059:
	.size	_ZL24__sti____cudaRegisterAllv, .-_ZL24__sti____cudaRegisterAllv
	.section	.init_array,"aw"
	.align 8
	.quad	_ZL24__sti____cudaRegisterAllv
	.section	.nvFatBinSegment,"aw"
	.align 8
	.type	_ZL15__fatDeviceText, @object
	.size	_ZL15__fatDeviceText, 24
_ZL15__fatDeviceText:
	.long	1180844977
	.long	1
	.quad	fatbinData
	.quad	0
	.local	_ZL20__cudaFatCubinHandle
	.comm	_ZL20__cudaFatCubinHandle,8,8
	.section	.rodata.cst8,"aM",@progbits,8
	.align 8
.LC0:
	.long	-4194304
	.long	1105199103
	.align 8
.LC1:
	.long	-1717986918
	.long	1069128089
	.align 8
.LC2:
	.long	0
	.long	1072693248
	.align 8
.LC3:
	.long	0
	.long	1071644672
	.section	.rodata.cst4,"aM",@progbits,4
	.align 4
.LC5:
	.long	1092616192
	.align 4
.LC6:
	.long	1077936128
	.align 4
.LC7:
	.long	2139095039
	.section	.rodata.cst8
	.align 8
.LC8:
	.long	0
	.long	1076101120
	.align 8
.LC12:
	.long	-1717986918
	.long	1072273817
	.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:

/**
 * GA Approximate: Try to approximate a simple function using Genetic Algorithm
 *
 **/

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

// For the CUDA runtime routines (prefixed with "cuda_")
#include <hip/hip_runtime.h>

/**
 * Macros to configure experiment
 */
#define POPULATION_SIZE 300
#define GENOTYPE_SIZE 3
#define MAX_NUMBER_OF_GENERATIONS 200
#define CROSSOVER_PROB 0.8
#define CROSSING_PROB 0.5
#define MUTATION_PROB 0.1
#define SAMPLING_POINTS 10
#define RAND_01 ((double)rand()/RAND_MAX)

void mutate(float* chromosome)
{
	for (int i = 0; i < GENOTYPE_SIZE; i++)
	{
		if (RAND_01 < MUTATION_PROB)
			chromosome[i] += 2 * RAND_01 - 1;
	}
}

void crossOver(float* chrom1, float* chrom2, float* newChrom1, float* newChrom2)
{
	for (int i = 0; i < GENOTYPE_SIZE; i++)
	{
		if (RAND_01 < CROSSING_PROB)
		{
			newChrom1[i] = chrom1[i];
			newChrom2[i] = chrom2[i];
		}
		else
		{
			newChrom1[i] = chrom2[i];
			newChrom2[i] = chrom1[i];
		}
	}
}

int rouletteWheel(float* fitness_values, double sum_fitness_values)
{
	double offsetSelection = 0;

	double chooseValue = RAND_01;

	for (int i = 0; i < POPULATION_SIZE; i++)
	{
		offsetSelection += (fitness_values[i] / sum_fitness_values);

		if (offsetSelection >= chooseValue)
		{
			return i;
		}
	}

	return POPULATION_SIZE-1;
}

__host__ __device__ float targetFunction(float value)
{
	// 10 * x^2 + 3 * x + 10
	return 10 * value * value + 3 * value - 10;
}

/**
 * CUDA Kernel Device code
 *
 * Run Genetic Algorithm experiments
 */
__global__ void RunGAIteration(float *population, float *fitness_values)
{
	// Get chromosome id
	int c_id = threadIdx.x;

	float *chromosome = population + (c_id * GENOTYPE_SIZE);
	float output, desiredOutput, testPoint;

	float fitness_value_of_instance = 0;

	for (int i = 0; i < SAMPLING_POINTS; i++)
	{
		testPoint = i * (20 / SAMPLING_POINTS) - 10;
		desiredOutput = targetFunction(testPoint);

		output = chromosome[0];

		for (int g = 1; g < GENOTYPE_SIZE; g++)
		{
			output += chromosome[g] * testPoint;
			testPoint *= testPoint;
		}

		fitness_value_of_instance += (desiredOutput - output) * (desiredOutput - output);
	}

	fitness_value_of_instance /= SAMPLING_POINTS;

	fitness_values[c_id] = fitness_value_of_instance;
}

/**
 * Host main routine
 */
int main(void)
{
	// Error code to check return values for CUDA calls
	hipError_t err = hipSuccess;

	float *population, *fitness_values;
	int POPULATION_MATRIX_SIZE = POPULATION_SIZE * GENOTYPE_SIZE;
	float FITNESS_VALUES_SIZE = POPULATION_SIZE;

	float *newGeneration = (float*) malloc(POPULATION_MATRIX_SIZE * sizeof(float));

	hipMallocManaged(&population, POPULATION_MATRIX_SIZE * sizeof(float));
	hipMallocManaged(&fitness_values, FITNESS_VALUES_SIZE * sizeof(float));

	// Generate a first random population
	for (int i = 0; i < POPULATION_SIZE; i++) {
		for (int g = 0; g < GENOTYPE_SIZE; g++) {
			population[i*GENOTYPE_SIZE + g] = (rand() % 100 - 50) / 10.0;
		}
	}

	// Run GA
	double fitness_values_sum;

	int c_id1, c_id2;

	float *chromosome1, *chromosome2, *newChromosome1, *newChromosome2;

	float bestFitness = FLT_MAX, bestFitnessOnGeneration = FLT_MAX;

	for (int gen = 0; gen < MAX_NUMBER_OF_GENERATIONS; gen++)
	{
		RunGAIteration<<<1, POPULATION_SIZE>>>(population, fitness_values);
		hipDeviceSynchronize();

		err = hipGetLastError();

		if (err != hipSuccess)
		{
			fprintf(stderr, "Failed to launch RunGAIteration kernel (error code %s)!\n",
					hipGetErrorString(err));
			exit(EXIT_FAILURE);
		}

		// Get best fitness value
		bestFitnessOnGeneration = FLT_MAX;
		for (int i = 0; i < POPULATION_SIZE; i++)
		{
			if (fitness_values[i] < bestFitness)
			{
				bestFitness = fitness_values[i];
			}

			if (fitness_values[i] < bestFitnessOnGeneration)
			{
				bestFitnessOnGeneration = fitness_values[i];
			}
		}
		printf("The best fitness value on generation %d is %.2f.\n", gen, bestFitnessOnGeneration);
		printf("The best fitness value until now is %.2f.\n", gen, bestFitness);

		/** Get new generation **/

		// Verify sum of fitness values
		fitness_values_sum = 0;

		for (int i = 0; i < POPULATION_SIZE; i++)
			fitness_values_sum += fitness_values[i];

		for (int i = 0; i < POPULATION_SIZE; i+=2)
		{
			// Select a pair chromosomes using RouletteWheel
			c_id1 = rouletteWheel(fitness_values, fitness_values_sum);
			c_id2 = rouletteWheel(fitness_values, fitness_values_sum);

			chromosome1 = population + c_id1;
			chromosome2 = population + c_id2;

			newChromosome1 = newGeneration + (i*GENOTYPE_SIZE);
			newChromosome2 = newGeneration + ((i+1)*GENOTYPE_SIZE);

			// Crossover
			if (RAND_01 <= CROSSOVER_PROB)
			{
				// Do Crossover
				crossOver(chromosome1, chromosome2, newChromosome1, newChromosome2);
			}
			else
			{
				// If not crossover, just copy the 2 chromosomes taken with Roulette Wheel method
				memcpy(newChromosome1, chromosome1, GENOTYPE_SIZE);
				memcpy(newChromosome2, chromosome2, GENOTYPE_SIZE);
			}

			// Mutate genes of generated pair of chromosomes
			mutate(newChromosome1);
			mutate(newChromosome2);
		}

		for (int i = 0; i < POPULATION_MATRIX_SIZE; i++)
		{
			population[i] = newGeneration[i];
		}
	}

	// Verify each chromosome
	//	for (int i = 0; i < POPULATION_SIZE; i++)
	//		printf("Chromosome %d got fitness value of %.2f.\n", i, fitness_values[i]);

	free(newGeneration);
	hipFree(population);
	hipFree(fitness_values);

	return 0;
}


	.text
	.file	"gaapproximate.hip"
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function _Z6mutatePf
.LCPI0_0:
	.quad	0x41dfffffffc00000              # double 2147483647
.LCPI0_1:
	.quad	0x3fb999999999999a              # double 0.10000000000000001
.LCPI0_2:
	.quad	0xbff0000000000000              # double -1
	.text
	.globl	_Z6mutatePf
	.type	_Z6mutatePf,@function
_Z6mutatePf:                            # @_Z6mutatePf
	.cfi_startproc
# %bb.0:
	pushq	%r14
	.cfi_def_cfa_offset 16
	pushq	%rbx
	.cfi_def_cfa_offset 24
	pushq	%rax
	.cfi_def_cfa_offset 32
	.cfi_offset %rbx, -24
	.cfi_offset %r14, -16
	movq	%rdi, %rbx
	xorl	%r14d, %r14d
.LBB0_1:                                # =>This Inner Loop Header: Depth=1
	callq	rand
	xorps	%xmm0, %xmm0
	cvtsi2sd	%eax, %xmm0
	divsd	.LCPI0_0(%rip), %xmm0
	movsd	.LCPI0_1(%rip), %xmm1           # xmm1 = mem[0],zero
	ucomisd	%xmm0, %xmm1
	jbe	.LBB0_3
# %bb.2:                                #   in Loop: Header=BB0_1 Depth=1
	callq	rand
	xorps	%xmm0, %xmm0
	cvtsi2sd	%eax, %xmm0
	divsd	.LCPI0_0(%rip), %xmm0
	addsd	%xmm0, %xmm0
	addsd	.LCPI0_2(%rip), %xmm0
	xorps	%xmm1, %xmm1
	cvtss2sd	(%rbx,%r14,4), %xmm1
	addsd	%xmm0, %xmm1
	xorps	%xmm0, %xmm0
	cvtsd2ss	%xmm1, %xmm0
	movss	%xmm0, (%rbx,%r14,4)
.LBB0_3:                                #   in Loop: Header=BB0_1 Depth=1
	incq	%r14
	cmpq	$3, %r14
	jne	.LBB0_1
# %bb.4:
	addq	$8, %rsp
	.cfi_def_cfa_offset 24
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%r14
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end0:
	.size	_Z6mutatePf, .Lfunc_end0-_Z6mutatePf
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function _Z9crossOverPfS_S_S_
.LCPI1_0:
	.quad	0x41dfffffffc00000              # double 2147483647
.LCPI1_1:
	.quad	0x3fe0000000000000              # double 0.5
	.text
	.globl	_Z9crossOverPfS_S_S_
	.type	_Z9crossOverPfS_S_S_,@function
_Z9crossOverPfS_S_S_:                   # @_Z9crossOverPfS_S_S_
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%r13
	.cfi_def_cfa_offset 32
	pushq	%r12
	.cfi_def_cfa_offset 40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movq	%rcx, %rbx
	movq	%rdx, %r14
	movq	%rsi, %r15
	movq	%rdi, %r12
	xorl	%r13d, %r13d
.LBB1_1:                                # =>This Inner Loop Header: Depth=1
	callq	rand
	xorps	%xmm0, %xmm0
	cvtsi2sd	%eax, %xmm0
	divsd	.LCPI1_0(%rip), %xmm0
	movsd	.LCPI1_1(%rip), %xmm1           # xmm1 = mem[0],zero
	ucomisd	%xmm0, %xmm1
	movq	%r12, %rax
	movq	%r15, %rcx
	ja	.LBB1_3
# %bb.2:                                #   in Loop: Header=BB1_1 Depth=1
	movq	%r15, %rax
	movq	%r12, %rcx
.LBB1_3:                                #   in Loop: Header=BB1_1 Depth=1
	movss	(%rax,%r13,4), %xmm0            # xmm0 = mem[0],zero,zero,zero
	movss	%xmm0, (%r14,%r13,4)
	movss	(%rcx,%r13,4), %xmm0            # xmm0 = mem[0],zero,zero,zero
	movss	%xmm0, (%rbx,%r13,4)
	incq	%r13
	cmpq	$3, %r13
	jne	.LBB1_1
# %bb.4:
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end1:
	.size	_Z9crossOverPfS_S_S_, .Lfunc_end1-_Z9crossOverPfS_S_S_
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function _Z13rouletteWheelPfd
.LCPI2_0:
	.quad	0x41dfffffffc00000              # double 2147483647
	.text
	.globl	_Z13rouletteWheelPfd
	.type	_Z13rouletteWheelPfd,@function
_Z13rouletteWheelPfd:                   # @_Z13rouletteWheelPfd
	.cfi_startproc
# %bb.0:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	subq	$16, %rsp
	.cfi_def_cfa_offset 32
	.cfi_offset %rbx, -16
	movsd	%xmm0, 8(%rsp)                  # 8-byte Spill
	movq	%rdi, %rbx
	callq	rand
	movsd	8(%rsp), %xmm3                  # 8-byte Reload
                                        # xmm3 = mem[0],zero
	xorps	%xmm0, %xmm0
	cvtsi2sd	%eax, %xmm0
	divsd	.LCPI2_0(%rip), %xmm0
	xorpd	%xmm1, %xmm1
	xorl	%ecx, %ecx
.LBB2_1:                                # =>This Inner Loop Header: Depth=1
	movapd	%xmm1, %xmm2
	xorps	%xmm1, %xmm1
	cvtss2sd	(%rbx,%rcx,4), %xmm1
	divsd	%xmm3, %xmm1
	addsd	%xmm2, %xmm1
	ucomisd	%xmm0, %xmm1
	jae	.LBB2_4
# %bb.2:                                #   in Loop: Header=BB2_1 Depth=1
	incq	%rcx
	cmpq	$300, %rcx                      # imm = 0x12C
	jne	.LBB2_1
# %bb.3:
	movl	$300, %ecx                      # imm = 0x12C
.LBB2_4:                                # %.split.loop.exit17
	movl	$299, %eax                      # imm = 0x12B
	cmpl	%eax, %ecx
	cmovbl	%ecx, %eax
	addq	$16, %rsp
	.cfi_def_cfa_offset 16
	popq	%rbx
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end2:
	.size	_Z13rouletteWheelPfd, .Lfunc_end2-_Z13rouletteWheelPfd
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst4,"aM",@progbits,4
	.p2align	2, 0x0                          # -- Begin function _Z14targetFunctionf
.LCPI3_0:
	.long	0x41200000                      # float 10
.LCPI3_1:
	.long	0x40400000                      # float 3
.LCPI3_2:
	.long	0xc1200000                      # float -10
	.text
	.globl	_Z14targetFunctionf
	.type	_Z14targetFunctionf,@function
_Z14targetFunctionf:                    # @_Z14targetFunctionf
	.cfi_startproc
# %bb.0:
	movss	.LCPI3_0(%rip), %xmm1           # xmm1 = mem[0],zero,zero,zero
	mulss	%xmm0, %xmm1
	mulss	%xmm0, %xmm1
	mulss	.LCPI3_1(%rip), %xmm0
	addss	%xmm1, %xmm0
	addss	.LCPI3_2(%rip), %xmm0
	retq
.Lfunc_end3:
	.size	_Z14targetFunctionf, .Lfunc_end3-_Z14targetFunctionf
	.cfi_endproc
                                        # -- End function
	.globl	_Z29__device_stub__RunGAIterationPfS_ # -- Begin function _Z29__device_stub__RunGAIterationPfS_
	.type	_Z29__device_stub__RunGAIterationPfS_,@function
_Z29__device_stub__RunGAIterationPfS_:  # @_Z29__device_stub__RunGAIterationPfS_
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%r13
	.cfi_def_cfa_offset 32
	pushq	%r12
	.cfi_def_cfa_offset 40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	subq	$80, %rsp
	.cfi_def_cfa_offset 128
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	leaq	24(%rsp), %rax
	movq	%rdi, (%rax)
	leaq	16(%rsp), %rcx
	movq	%rsi, (%rcx)
	leaq	64(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rcx, 8(%rbx)
	leaq	48(%rsp), %r14
	leaq	32(%rsp), %r15
	leaq	8(%rsp), %r12
	movq	%rsp, %r13
	movq	%r14, %rdi
	movq	%r15, %rsi
	movq	%r12, %rdx
	movq	%r13, %rcx
	callq	__hipPopCallConfiguration
	movq	(%r14), %rsi
	movl	8(%r14), %edx
	movq	(%r15), %rcx
	movl	8(%r15), %r8d
	movl	$_Z14RunGAIterationPfS_, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$96, %rsp
	.cfi_adjust_cfa_offset -96
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end4:
	.size	_Z29__device_stub__RunGAIterationPfS_, .Lfunc_end4-_Z29__device_stub__RunGAIterationPfS_
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function main
.LCPI5_0:
	.quad	0x4024000000000000              # double 10
.LCPI5_2:
	.quad	0x41dfffffffc00000              # double 2147483647
.LCPI5_3:
	.quad	0x3fe999999999999a              # double 0.80000000000000004
.LCPI5_4:
	.quad	0x3fe0000000000000              # double 0.5
	.section	.rodata.cst4,"aM",@progbits,4
	.p2align	2, 0x0
.LCPI5_1:
	.long	0x7f7fffff                      # float 3.40282347E+38
	.text
	.globl	main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%r15
	.cfi_def_cfa_offset 24
	pushq	%r14
	.cfi_def_cfa_offset 32
	pushq	%r13
	.cfi_def_cfa_offset 40
	pushq	%r12
	.cfi_def_cfa_offset 48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	subq	$72, %rsp
	.cfi_def_cfa_offset 128
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movl	$3600, %edi                     # imm = 0xE10
	callq	malloc
	movq	%rax, 16(%rsp)                  # 8-byte Spill
	leaq	24(%rsp), %rdi
	movl	$3600, %esi                     # imm = 0xE10
	movl	$1, %edx
	callq	hipMallocManaged
	leaq	8(%rsp), %rdi
	movl	$1200, %esi                     # imm = 0x4B0
	movl	$1, %edx
	callq	hipMallocManaged
	xorl	%r14d, %r14d
	xorl	%r15d, %r15d
.LBB5_1:                                # %.preheader79
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB5_2 Depth 2
	xorl	%ebx, %ebx
.LBB5_2:                                #   Parent Loop BB5_1 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	callq	rand
	movsd	.LCPI5_0(%rip), %xmm1           # xmm1 = mem[0],zero
	cltq
	imulq	$1374389535, %rax, %rcx         # imm = 0x51EB851F
	movq	%rcx, %rdx
	shrq	$63, %rdx
	sarq	$37, %rcx
	addl	%edx, %ecx
	imull	$100, %ecx, %ecx
	negl	%ecx
	addl	%ecx, %eax
	addl	$-50, %eax
	xorps	%xmm0, %xmm0
	cvtsi2sd	%eax, %xmm0
	divsd	%xmm1, %xmm0
	movq	24(%rsp), %rax
	cvtsd2ss	%xmm0, %xmm0
	addq	%r14, %rax
	movss	%xmm0, (%rax,%rbx,4)
	incq	%rbx
	cmpq	$3, %rbx
	jne	.LBB5_2
# %bb.3:                                #   in Loop: Header=BB5_1 Depth=1
	incq	%r15
	addq	$12, %r14
	cmpq	$300, %r15                      # imm = 0x12C
	jne	.LBB5_1
# %bb.4:                                # %.preheader78.preheader
	movabsq	$4294967297, %rcx               # imm = 0x100000001
	movq	16(%rsp), %rax                  # 8-byte Reload
	addq	$12, %rax
	movq	%rax, 40(%rsp)                  # 8-byte Spill
	movss	.LCPI5_1(%rip), %xmm0           # xmm0 = mem[0],zero,zero,zero
	xorl	%ebx, %ebx
	leaq	299(%rcx), %rax
	movq	%rax, 48(%rsp)                  # 8-byte Spill
	movss	%xmm0, 4(%rsp)                  # 4-byte Spill
.LBB5_5:                                # %.preheader78
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB5_9 Depth 2
                                        #     Child Loop BB5_11 Depth 2
                                        #     Child Loop BB5_13 Depth 2
                                        #       Child Loop BB5_15 Depth 3
	movabsq	$4294967297, %rdi               # imm = 0x100000001
	movl	$1, %esi
	movq	48(%rsp), %rdx                  # 8-byte Reload
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB5_7
# %bb.6:                                #   in Loop: Header=BB5_5 Depth=1
	movq	24(%rsp), %rdi
	movq	8(%rsp), %rsi
	callq	_Z29__device_stub__RunGAIterationPfS_
.LBB5_7:                                #   in Loop: Header=BB5_5 Depth=1
	callq	hipDeviceSynchronize
	callq	hipGetLastError
	testl	%eax, %eax
	jne	.LBB5_22
# %bb.8:                                # %.preheader77
                                        #   in Loop: Header=BB5_5 Depth=1
	movq	8(%rsp), %rax
	xorl	%ecx, %ecx
	movss	.LCPI5_1(%rip), %xmm0           # xmm0 = mem[0],zero,zero,zero
	movss	4(%rsp), %xmm3                  # 4-byte Reload
                                        # xmm3 = mem[0],zero,zero,zero
.LBB5_9:                                #   Parent Loop BB5_5 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movaps	%xmm3, %xmm1
	movaps	%xmm0, %xmm2
	movss	(%rax,%rcx,4), %xmm0            # xmm0 = mem[0],zero,zero,zero
	movaps	%xmm0, %xmm3
	minss	%xmm1, %xmm3
	minss	%xmm2, %xmm0
	incq	%rcx
	cmpq	$300, %rcx                      # imm = 0x12C
	jne	.LBB5_9
# %bb.10:                               #   in Loop: Header=BB5_5 Depth=1
	cvtss2sd	%xmm0, %xmm0
	movl	$.L.str.1, %edi
	movl	%ebx, %esi
	movb	$1, %al
	movss	%xmm3, 4(%rsp)                  # 4-byte Spill
	callq	printf
	xorps	%xmm0, %xmm0
	cvtss2sd	4(%rsp), %xmm0                  # 4-byte Folded Reload
	movl	$.L.str.2, %edi
	movq	%rbx, 56(%rsp)                  # 8-byte Spill
	movl	%ebx, %esi
	movb	$1, %al
	callq	printf
	xorps	%xmm0, %xmm0
	movq	8(%rsp), %rax
	xorl	%ecx, %ecx
.LBB5_11:                               #   Parent Loop BB5_5 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	xorps	%xmm1, %xmm1
	cvtss2sd	(%rax,%rcx,4), %xmm1
	addsd	%xmm1, %xmm0
	incq	%rcx
	cmpq	$300, %rcx                      # imm = 0x12C
	jne	.LBB5_11
# %bb.12:                               # %.preheader76.preheader
                                        #   in Loop: Header=BB5_5 Depth=1
	movq	40(%rsp), %r14                  # 8-byte Reload
	xorl	%r12d, %r12d
	movsd	%xmm0, 32(%rsp)                 # 8-byte Spill
.LBB5_13:                               # %.preheader76
                                        #   Parent Loop BB5_5 Depth=1
                                        # =>  This Loop Header: Depth=2
                                        #       Child Loop BB5_15 Depth 3
	movq	8(%rsp), %rdi
	callq	_Z13rouletteWheelPfd
	movl	%eax, %ebp
	movq	8(%rsp), %rdi
	movsd	32(%rsp), %xmm0                 # 8-byte Reload
                                        # xmm0 = mem[0],zero
	callq	_Z13rouletteWheelPfd
	movq	24(%rsp), %rcx
	movslq	%ebp, %rdx
	leaq	(%rcx,%rdx,4), %rbx
	cltq
	leaq	(%rcx,%rax,4), %r15
	leaq	(%r12,%r12,2), %rax
	movq	16(%rsp), %rcx                  # 8-byte Reload
	leaq	(%rcx,%rax,4), %rbp
	leaq	(%rcx,%rax,4), %r13
	addq	$12, %r13
	callq	rand
	xorps	%xmm0, %xmm0
	cvtsi2sd	%eax, %xmm0
	divsd	.LCPI5_2(%rip), %xmm0
	movsd	.LCPI5_3(%rip), %xmm1           # xmm1 = mem[0],zero
	ucomisd	%xmm0, %xmm1
	movq	%r13, 64(%rsp)                  # 8-byte Spill
	jae	.LBB5_14
# %bb.20:                               #   in Loop: Header=BB5_13 Depth=2
	movb	2(%rbx), %al
	movb	%al, 2(%rbp)
	movzwl	(%rbx), %eax
	movw	%ax, (%rbp)
	movb	2(%r15), %al
	movb	%al, 2(%r13)
	movzwl	(%r15), %eax
	movw	%ax, (%r13)
	jmp	.LBB5_21
.LBB5_14:                               # %.preheader.preheader
                                        #   in Loop: Header=BB5_13 Depth=2
	xorl	%r13d, %r13d
.LBB5_15:                               # %.preheader
                                        #   Parent Loop BB5_5 Depth=1
                                        #     Parent Loop BB5_13 Depth=2
                                        # =>    This Inner Loop Header: Depth=3
	callq	rand
	xorps	%xmm0, %xmm0
	cvtsi2sd	%eax, %xmm0
	divsd	.LCPI5_2(%rip), %xmm0
	movsd	.LCPI5_4(%rip), %xmm1           # xmm1 = mem[0],zero
	ucomisd	%xmm0, %xmm1
	movq	%rbx, %rax
	movq	%r15, %rcx
	ja	.LBB5_17
# %bb.16:                               # %.preheader
                                        #   in Loop: Header=BB5_15 Depth=3
	movq	%r15, %rax
	movq	%rbx, %rcx
.LBB5_17:                               # %.preheader
                                        #   in Loop: Header=BB5_15 Depth=3
	movss	(%rax,%r13,4), %xmm0            # xmm0 = mem[0],zero,zero,zero
	movss	%xmm0, -12(%r14,%r13,4)
	movss	(%rcx,%r13,4), %xmm0            # xmm0 = mem[0],zero,zero,zero
	movss	%xmm0, (%r14,%r13,4)
	incq	%r13
	cmpq	$3, %r13
	jne	.LBB5_15
.LBB5_21:                               # %_Z9crossOverPfS_S_S_.exit
                                        #   in Loop: Header=BB5_13 Depth=2
	movq	%rbp, %rdi
	callq	_Z6mutatePf
	movq	64(%rsp), %rdi                  # 8-byte Reload
	callq	_Z6mutatePf
	leaq	2(%r12), %rax
	addq	$24, %r14
	cmpq	$298, %r12                      # imm = 0x12A
	movq	%rax, %r12
	movsd	32(%rsp), %xmm0                 # 8-byte Reload
                                        # xmm0 = mem[0],zero
	jb	.LBB5_13
# %bb.18:                               # %.preheader75
                                        #   in Loop: Header=BB5_5 Depth=1
	movq	24(%rsp), %r13
	movl	$3600, %edx                     # imm = 0xE10
	movq	%r13, %rdi
	movq	16(%rsp), %rsi                  # 8-byte Reload
	callq	memcpy@PLT
	movq	56(%rsp), %rbx                  # 8-byte Reload
	incl	%ebx
	cmpl	$200, %ebx
	jne	.LBB5_5
# %bb.19:
	movq	16(%rsp), %rdi                  # 8-byte Reload
	callq	free
	movq	%r13, %rdi
	callq	hipFree
	movq	8(%rsp), %rdi
	callq	hipFree
	xorl	%eax, %eax
	addq	$72, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%r12
	.cfi_def_cfa_offset 40
	popq	%r13
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.LBB5_22:
	.cfi_def_cfa_offset 128
	movq	stderr(%rip), %rbx
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str, %esi
	movq	%rbx, %rdi
	movq	%rax, %rdx
	xorl	%eax, %eax
	callq	fprintf
	movl	$1, %edi
	callq	exit
.Lfunc_end5:
	.size	main, .Lfunc_end5-main
	.cfi_endproc
                                        # -- End function
	.p2align	4, 0x90                         # -- Begin function __hip_module_ctor
	.type	__hip_module_ctor,@function
__hip_module_ctor:                      # @__hip_module_ctor
	.cfi_startproc
# %bb.0:
	subq	$40, %rsp
	.cfi_def_cfa_offset 48
	movq	__hip_gpubin_handle(%rip), %rdi
	testq	%rdi, %rdi
	jne	.LBB6_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rdi
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB6_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z14RunGAIterationPfS_, %esi
	movl	$.L__unnamed_1, %edx
	movl	$.L__unnamed_1, %ecx
	movl	$-1, %r8d
	xorl	%r9d, %r9d
	callq	__hipRegisterFunction
	movl	$__hip_module_dtor, %edi
	addq	$40, %rsp
	.cfi_def_cfa_offset 8
	jmp	atexit                          # TAILCALL
.Lfunc_end6:
	.size	__hip_module_ctor, .Lfunc_end6-__hip_module_ctor
	.cfi_endproc
                                        # -- End function
	.p2align	4, 0x90                         # -- Begin function __hip_module_dtor
	.type	__hip_module_dtor,@function
__hip_module_dtor:                      # @__hip_module_dtor
	.cfi_startproc
# %bb.0:
	movq	__hip_gpubin_handle(%rip), %rdi
	testq	%rdi, %rdi
	je	.LBB7_2
# %bb.1:
	pushq	%rax
	.cfi_def_cfa_offset 16
	callq	__hipUnregisterFatBinary
	movq	$0, __hip_gpubin_handle(%rip)
	addq	$8, %rsp
	.cfi_def_cfa_offset 8
.LBB7_2:
	retq
.Lfunc_end7:
	.size	__hip_module_dtor, .Lfunc_end7-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	_Z14RunGAIterationPfS_,@object  # @_Z14RunGAIterationPfS_
	.section	.rodata,"a",@progbits
	.globl	_Z14RunGAIterationPfS_
	.p2align	3, 0x0
_Z14RunGAIterationPfS_:
	.quad	_Z29__device_stub__RunGAIterationPfS_
	.size	_Z14RunGAIterationPfS_, 8

	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"Failed to launch RunGAIteration kernel (error code %s)!\n"
	.size	.L.str, 57

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"The best fitness value on generation %d is %.2f.\n"
	.size	.L.str.1, 50

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"The best fitness value until now is %.2f.\n"
	.size	.L.str.2, 43

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z14RunGAIterationPfS_"
	.size	.L__unnamed_1, 23

	.type	__hip_fatbin_wrapper,@object    # @__hip_fatbin_wrapper
	.section	.hipFatBinSegment,"a",@progbits
	.p2align	3, 0x0
__hip_fatbin_wrapper:
	.long	1212764230                      # 0x48495046
	.long	1                               # 0x1
	.quad	__hip_fatbin
	.quad	0
	.size	__hip_fatbin_wrapper, 24

	.type	__hip_gpubin_handle,@object     # @__hip_gpubin_handle
	.local	__hip_gpubin_handle
	.comm	__hip_gpubin_handle,8,8
	.section	.init_array,"aw",@init_array
	.p2align	3, 0x0
	.quad	__hip_module_ctor
	.type	__hip_cuid_,@object             # @__hip_cuid_
	.bss
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               # 0x0
	.size	__hip_cuid_, 1

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym _Z29__device_stub__RunGAIterationPfS_
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z14RunGAIterationPfS_
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z14RunGAIterationPfS_  ; -- Begin function _Z14RunGAIterationPfS_
	.globl	_Z14RunGAIterationPfS_
	.p2align	8
	.type	_Z14RunGAIterationPfS_,@function
_Z14RunGAIterationPfS_:                 ; @_Z14RunGAIterationPfS_
; %bb.0:
	s_load_b128 s[0:3], s[0:1], 0x0
	v_mul_u32_u24_e32 v1, 3, v0
	s_delay_alu instid0(VALU_DEP_1)
	v_dual_mov_b32 v4, 0 :: v_dual_lshlrev_b32 v1, 2, v1
	s_waitcnt lgkmcnt(0)
	global_load_b96 v[1:3], v1, s[0:1]
	s_mov_b32 s0, -10
.LBB0_1:                                ; =>This Inner Loop Header: Depth=1
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(SKIP_1) | instid1(SALU_CYCLE_1)
	v_cvt_f32_i32_e32 v5, s0
	s_add_i32 s0, s0, 2
	s_cmp_eq_u32 s0, 10
	s_waitcnt vmcnt(0)
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_1)
	v_fma_f32 v7, v2, v5, v1
	v_mul_f32_e32 v8, v5, v5
	v_dual_mul_f32 v6, 0x41200000, v5 :: v_dual_fmac_f32 v7, v8, v3
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mul_f32_e32 v6, v6, v5
	v_fmac_f32_e32 v6, 0x40400000, v5
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_add_f32_e32 v5, 0xc1200000, v6
	v_sub_f32_e32 v5, v5, v7
	s_delay_alu instid0(VALU_DEP_1)
	v_fmac_f32_e32 v4, v5, v5
	s_cbranch_scc0 .LBB0_1
; %bb.2:
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_2) | instid1(VALU_DEP_3)
	v_div_scale_f32 v1, null, 0x41200000, 0x41200000, v4
	v_div_scale_f32 v5, vcc_lo, v4, 0x41200000, v4
	v_lshlrev_b32_e32 v0, 2, v0
	v_rcp_f32_e32 v2, v1
	s_waitcnt_depctr 0xfff
	v_fma_f32 v3, -v1, v2, 1.0
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fmac_f32_e32 v2, v3, v2
	v_mul_f32_e32 v3, v5, v2
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fma_f32 v6, -v1, v3, v5
	v_fmac_f32_e32 v3, v6, v2
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fma_f32 v1, -v1, v3, v5
	v_div_fmas_f32 v1, v1, v2, v3
	s_delay_alu instid0(VALU_DEP_1)
	v_div_fixup_f32 v1, v1, 0x41200000, v4
	global_store_b32 v0, v1, s[2:3]
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z14RunGAIterationPfS_
		.amdhsa_group_segment_fixed_size 0
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 16
		.amdhsa_user_sgpr_count 15
		.amdhsa_user_sgpr_dispatch_ptr 0
		.amdhsa_user_sgpr_queue_ptr 0
		.amdhsa_user_sgpr_kernarg_segment_ptr 1
		.amdhsa_user_sgpr_dispatch_id 0
		.amdhsa_user_sgpr_private_segment_size 0
		.amdhsa_wavefront_size32 1
		.amdhsa_uses_dynamic_stack 0
		.amdhsa_enable_private_segment 0
		.amdhsa_system_sgpr_workgroup_id_x 1
		.amdhsa_system_sgpr_workgroup_id_y 0
		.amdhsa_system_sgpr_workgroup_id_z 0
		.amdhsa_system_sgpr_workgroup_info 0
		.amdhsa_system_vgpr_workitem_id 0
		.amdhsa_next_free_vgpr 9
		.amdhsa_next_free_sgpr 4
		.amdhsa_float_round_mode_32 0
		.amdhsa_float_round_mode_16_64 0
		.amdhsa_float_denorm_mode_32 3
		.amdhsa_float_denorm_mode_16_64 3
		.amdhsa_dx10_clamp 1
		.amdhsa_ieee_mode 1
		.amdhsa_fp16_overflow 0
		.amdhsa_workgroup_processor_mode 1
		.amdhsa_memory_ordered 1
		.amdhsa_forward_progress 0
		.amdhsa_shared_vgpr_count 0
		.amdhsa_exception_fp_ieee_invalid_op 0
		.amdhsa_exception_fp_denorm_src 0
		.amdhsa_exception_fp_ieee_div_zero 0
		.amdhsa_exception_fp_ieee_overflow 0
		.amdhsa_exception_fp_ieee_underflow 0
		.amdhsa_exception_fp_ieee_inexact 0
		.amdhsa_exception_int_div_zero 0
	.end_amdhsa_kernel
	.text
.Lfunc_end0:
	.size	_Z14RunGAIterationPfS_, .Lfunc_end0-_Z14RunGAIterationPfS_
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 264
; NumSgprs: 6
; NumVgprs: 9
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 0
; VGPRBlocks: 1
; NumSGPRsForWavesPerEU: 6
; NumVGPRsForWavesPerEU: 9
; Occupancy: 16
; WaveLimiterHint : 0
; COMPUTE_PGM_RSRC2:SCRATCH_EN: 0
; COMPUTE_PGM_RSRC2:USER_SGPR: 15
; COMPUTE_PGM_RSRC2:TRAP_HANDLER: 0
; COMPUTE_PGM_RSRC2:TGID_X_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Y_EN: 0
; COMPUTE_PGM_RSRC2:TGID_Z_EN: 0
; COMPUTE_PGM_RSRC2:TIDIG_COMP_CNT: 0
	.text
	.p2alignl 7, 3214868480
	.fill 96, 4, 3214868480
	.type	__hip_cuid_,@object             ; @__hip_cuid_
	.section	.bss,"aw",@nobits
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               ; 0x0
	.size	__hip_cuid_, 1

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym __hip_cuid_
	.amdgpu_metadata
---
amdhsa.kernels:
  - .args:
      - .address_space:  global
        .offset:         0
        .size:           8
        .value_kind:     global_buffer
      - .address_space:  global
        .offset:         8
        .size:           8
        .value_kind:     global_buffer
    .group_segment_fixed_size: 0
    .kernarg_segment_align: 8
    .kernarg_segment_size: 16
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z14RunGAIterationPfS_
    .private_segment_fixed_size: 0
    .sgpr_count:     6
    .sgpr_spill_count: 0
    .symbol:         _Z14RunGAIterationPfS_.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     9
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
