0% found this document useful (0 votes)
20 views5 pages

SWE2017 - Lab Assignment 1pages-7

program mpi code

Uploaded by

ccannavar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views5 pages

SWE2017 - Lab Assignment 1pages-7

program mpi code

Uploaded by

ccannavar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

SWE2017 - Parallel Programming Lab Assignment – 6

1 Division: Split the 1024x1024 image into 16 tiles of


256x256 pixels.

2 Edge Detection on Each Tile: Each processor runs edge


detection on its tile and then applies the Hough Transform.

3 Local Accumulation: Each processor accumulates detected


lines locally.

4 Global Combination: The main process combines the local accumulators into a final
accumulator array, detecting prominent lines across the image.

19.
Code:
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

#define POPULATION_SIZE 4
#define MAX_ITERATIONS 10
#define MUTATION_RATE 0.1
#define MIGRATION_INTERVAL 2
#define VALUE_RANGE 1000

typedef struct {
double x, y, z;
double fitness;
} Candidate;
SWE2017 - Parallel Programming Lab Assignment – 6

double fitness_function(double x, double y, double z) {


return -x * x + 1000000 * x - y * y - 40000 * y - z * z;
}

void initialize_population(Candidate population[], int size) {


for (int i = 0; i < size; i++) {
population[i].x = rand() % VALUE_RANGE;
population[i].y = rand() % VALUE_RANGE;
population[i].z = rand() % VALUE_RANGE;
population[i].fitness = fitness_function(population[i].x,
population[i].y, population[i].z);
}
}

Candidate select_best(Candidate population[], int size) {


Candidate best = population[0];
for (int i = 1; i < size; i++) {
if (population[i].fitness > best.fitness) {
best = population[i];
}
}
return best;
}

Candidate linear_crossover(Candidate parent1, Candidate parent2) {


Candidate offspring;
offspring.x = 0.5 * (parent1.x + parent2.x);
offspring.y = 0.5 * (parent1.y + parent2.y);
offspring.z = 0.5 * (parent1.z + parent2.z);
offspring.fitness = fitness_function(offspring.x, offspring.y,
offspring.z);
return offspring;
}

void mutate(Candidate *candidate) {


if ((rand() / (double)RAND_MAX) < MUTATION_RATE) {
candidate->x += rand() % VALUE_RANGE - VALUE_RANGE / 2;
candidate->y += rand() % VALUE_RANGE - VALUE_RANGE / 2;
candidate->z += rand() % VALUE_RANGE - VALUE_RANGE / 2;
candidate->fitness = fitness_function(candidate->x, candidate->y,
candidate->z);
}
}

void migrate_population(Candidate population[], int rank, int num_procs) {


MPI_Status status;
int neighbor = (rank + 1) % num_procs;
SWE2017 - Parallel Programming Lab Assignment – 6

if (rank % 2 == 0) {
MPI_Send(&population[0], sizeof(Candidate), MPI_BYTE, neighbor, 0,
MPI_COMM_WORLD);
MPI_Recv(&population[POPULATION_SIZE - 1], sizeof(Candidate),
MPI_BYTE, (rank - 1 + num_procs) % num_procs, 0, MPI_COMM_WORLD, &status);
} else {
MPI_Recv(&population[POPULATION_SIZE - 1], sizeof(Candidate),
MPI_BYTE, (rank - 1 + num_procs) % num_procs, 0, MPI_COMM_WORLD, &status);
MPI_Send(&population[0], sizeof(Candidate), MPI_BYTE, neighbor, 0,
MPI_COMM_WORLD);
}
}

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


int rank, num_procs;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &num_procs);

srand(time(NULL) + rank); // Seed random number generator

Candidate population[POPULATION_SIZE];
initialize_population(population, POPULATION_SIZE);

Candidate global_best;
global_best.fitness = -INFINITY;

for (int iter = 0; iter < MAX_ITERATIONS; iter++) {


// Selection, Crossover, and Mutation
for (int i = 0; i < POPULATION_SIZE; i++) {
int parent1_idx = rand() % POPULATION_SIZE;
int parent2_idx = rand() % POPULATION_SIZE;
Candidate offspring = linear_crossover(population[parent1_idx],
population[parent2_idx]);
mutate(&offspring);
if (offspring.fitness > population[i].fitness) {
population[i] = offspring;
}
}

// Find the best candidate in local population


Candidate local_best = select_best(population, POPULATION_SIZE);

// Migration step
if (iter % MIGRATION_INTERVAL == 0 && num_procs > 1) {
migrate_population(population, rank, num_procs);
}
SWE2017 - Parallel Programming Lab Assignment – 6

// Gather global best across all processors


Candidate candidates[num_procs];
MPI_Allgather(&local_best, sizeof(Candidate), MPI_BYTE, candidates,
sizeof(Candidate), MPI_BYTE, MPI_COMM_WORLD);

for (int i = 0; i < num_procs; i++) {


if (candidates[i].fitness > global_best.fitness) {
global_best = candidates[i];
}
}

// Print current best for each processor


printf("Processor %d, Iteration %d: Best solution so far - x = %.2f, y
= %.2f, z = %.2f, fitness = %.2f\n",
rank, iter, local_best.x, local_best.y, local_best.z,
local_best.fitness);
}

if (rank == 0) {
printf("\nGlobal Best Solution:\n");
printf("x = %.2f, y = %.2f, z = %.2f, fitness = %.2f\n",
global_best.x, global_best.y, global_best.z, global_best.fitness);
}

MPI_Finalize();
return 0;
}

Output:
SWE2017 - Parallel Programming Lab Assignment – 6

Parallel Genetic Algorithm Approach

To maximize the given function efficiently using a parallel genetic algorithm, we'll
divide the task across 4 processors, each managing its own population. The Island
Migration model will facilitate occasional sharing of solutions between processors,
which improves the genetic diversity and helps avoid local optima.

Key Steps in the Parallel Genetic Algorithm Implementation

1. Population Initialization:

o Each processor will start with a population of 4 candidate


solutions, where each candidate has values for x, y, and z
randomly initialized within the range [0,1000].

o The function value for each candidate, f(x,y,z), will be


calculated to initialize the fitness of each individual.

2. Selection:

o Each processor will apply a selection method, such as


tournament selection, to choose candidates based on their

You might also like