Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <mpi/mpi.h>
- //mpicc -o main main.c
- //mpirun -np 2 main
- #define DATA_TAG 1
- #define RESULT_TAG 2
- const int imgWidth = 800;
- const int imgHeight = 800;
- const int maxN = 255;
- const double minR = -1.5;
- const double maxR = 0.7;
- const double minI = -1.0;
- const double maxI = 1.0;
- struct complejo{
- double real;
- double imaginario;
- };
- void master();
- void slave();
- int cal_pixel(struct complejo c);
- int main(int argc, char* argv[])
- {
- int rank;
- MPI_Init(&argc, &argv); // Inicializar MPI
- MPI_Comm_rank(MPI_COMM_WORLD, &rank); // Determina el rango del proceso
- if (rank == 0) {
- master();
- } else {
- slave();
- }
- MPI_Finalize(); // Finalizar MPI
- return 0;
- }
- void master(){
- int row = 0;
- int count = 0;
- int x,y, rank;
- int n =0;
- int n_procs;
- double start_time, end_time;
- MPI_Status status;
- MPI_Comm_rank(MPI_COMM_WORLD, &rank); // Determina el rango del proceso
- start_time = MPI_Wtime();
- // Generar ".ppm"
- FILE *archivo;
- archivo = fopen("imagen.ppm", "w");
- fprintf(archivo, "P3\n");
- fprintf(archivo, "%d %d\n", imgWidth, imgHeight);
- fprintf(archivo, "255\n");
- MPI_Comm_size(MPI_COMM_WORLD, &n_procs); // Determinar el número de procesos
- for(y = 0; y < imgWidth; y++){ // Filas
- for( x = 0; x < imgHeight; x++){ // Columnas
- struct complejo c;
- c.real = x * ((maxR - minR) / imgWidth) + minR;
- c.imaginario = y * ((maxI - minI) / imgHeight) + minI;
- MPI_Send(&c.real, sizeof(double), MPI_DOUBLE, 1, DATA_TAG, MPI_COMM_WORLD);
- MPI_Send(&c.imaginario, sizeof(double), MPI_DOUBLE, 1, DATA_TAG, MPI_COMM_WORLD);
- }
- }
- for(y = 0; y < imgWidth; y++){
- for(x = 0; x < imgHeight; x++){
- MPI_Recv(&n, sizeof(int), MPI_INT, 1, RESULT_TAG, MPI_COMM_WORLD, &status);
- int r = (n % 255); // Rojo
- int g = (n % 255); // Verde
- int b = (n % 255); // Azul
- // Guardar la imagen
- fprintf(archivo, "%d %d %d\n", r, g, b);
- }
- }
- end_time = MPI_Wtime();
- printf("Tiempo total: %f\n", end_time - start_time);
- fclose(archivo);
- }
- void slave(){
- int x,y, rank;
- MPI_Status status;
- MPI_Comm_rank(MPI_COMM_WORLD, &rank);
- for(y = 0; y < imgWidth; y++){
- for( x = 0; x < imgHeight; x++){
- struct complejo c;
- MPI_Recv(&c.real, sizeof(double), MPI_DOUBLE, 0, DATA_TAG, MPI_COMM_WORLD, &status);
- MPI_Recv(&c.imaginario, sizeof(double), MPI_DOUBLE, 0, DATA_TAG, MPI_COMM_WORLD, &status);
- // Encontrar el número de iteraciones en la formula de Mandelbrot
- int n = cal_pixel(c);
- MPI_Send(&n, sizeof(int), MPI_INT, 0, RESULT_TAG, MPI_COMM_WORLD);
- }
- }
- }
- int cal_pixel(struct complejo c){
- int count, max_iter;
- struct complejo z;
- double temp, lenghtsq;
- max_iter = 256;
- z.real = 0;
- z.imaginario = 0;
- count = 0;
- do {
- temp = z.real * z.real - z.imaginario * z.imaginario + c.real;
- z.imaginario = 2 * z.real * z.imaginario + c.imaginario;
- z.real = temp;
- lenghtsq = z.real * z.real + z.imaginario * z.imaginario;
- count++;
- }while((lenghtsq < 4.0) && (count < max_iter));
- return count;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement