Advertisement
Guest User

waf

a guest
Nov 19th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.43 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <mpi/mpi.h>
  4.  
  5. //mpicc -o main main.c
  6. //mpirun -np 2 main
  7.  
  8. #define DATA_TAG 1
  9. #define RESULT_TAG 2
  10.  
  11. const int imgWidth = 800;
  12. const int imgHeight = 800;
  13. const int maxN = 255;
  14. const double minR = -1.5;
  15. const double maxR = 0.7;
  16. const double minI = -1.0;
  17. const double maxI = 1.0;
  18.  
  19. struct complejo{
  20.     double real;
  21.     double imaginario;
  22. };
  23.  
  24. void master();
  25. void slave();
  26. int cal_pixel(struct complejo c);
  27.  
  28. int main(int argc, char* argv[])
  29. {
  30.     int rank;
  31.  
  32.     MPI_Init(&argc, &argv); // Inicializar MPI
  33.     MPI_Comm_rank(MPI_COMM_WORLD, &rank); // Determina el rango del proceso
  34.  
  35.     if (rank == 0) {
  36.         master();
  37.     } else {
  38.         slave();
  39.     }
  40.  
  41.     MPI_Finalize(); // Finalizar MPI
  42.     return 0;
  43. }
  44.  
  45. void master(){
  46.  
  47.     int row = 0;
  48.     int count = 0;
  49.     int x,y, rank;
  50.     int n =0;
  51.     int n_procs;
  52.     double start_time, end_time;
  53.     MPI_Status status;
  54.  
  55.     MPI_Comm_rank(MPI_COMM_WORLD, &rank);   // Determina el rango del proceso
  56.  
  57.     start_time = MPI_Wtime();
  58.  
  59.      // Generar ".ppm"
  60.     FILE *archivo;
  61.     archivo = fopen("imagen.ppm", "w");
  62.     fprintf(archivo, "P3\n");
  63.     fprintf(archivo, "%d %d\n", imgWidth, imgHeight);
  64.     fprintf(archivo, "255\n");
  65.  
  66.     MPI_Comm_size(MPI_COMM_WORLD, &n_procs);    // Determinar el número de procesos
  67.  
  68.     for(y = 0; y < imgWidth; y++){ // Filas
  69.         for( x = 0; x < imgHeight; x++){ // Columnas
  70.             struct complejo c;
  71.             c.real = x * ((maxR - minR) / imgWidth) + minR;
  72.             c.imaginario = y * ((maxI - minI) / imgHeight) + minI;
  73.  
  74.             MPI_Send(&c.real, sizeof(double), MPI_DOUBLE, 1, DATA_TAG, MPI_COMM_WORLD);
  75.             MPI_Send(&c.imaginario, sizeof(double), MPI_DOUBLE, 1, DATA_TAG, MPI_COMM_WORLD);
  76.          }
  77.     }
  78.  
  79.     for(y = 0; y < imgWidth; y++){
  80.         for(x = 0; x < imgHeight; x++){
  81.             MPI_Recv(&n, sizeof(int), MPI_INT, 1, RESULT_TAG, MPI_COMM_WORLD, &status);
  82.  
  83.             int r = (n % 255); // Rojo
  84.             int g = (n % 255); // Verde
  85.             int b = (n % 255); // Azul
  86.  
  87.             // Guardar la imagen
  88.             fprintf(archivo, "%d %d %d\n", r, g, b);
  89.         }
  90.     }
  91.     end_time = MPI_Wtime();
  92.     printf("Tiempo total: %f\n", end_time - start_time);
  93.     fclose(archivo);
  94. }
  95.  
  96. void slave(){
  97.  
  98.     int x,y, rank;
  99.     MPI_Status status;
  100.  
  101.     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  102.  
  103.     for(y = 0; y < imgWidth; y++){
  104.         for( x = 0; x < imgHeight; x++){
  105.             struct complejo c;
  106.             MPI_Recv(&c.real, sizeof(double), MPI_DOUBLE, 0, DATA_TAG, MPI_COMM_WORLD, &status);
  107.             MPI_Recv(&c.imaginario, sizeof(double), MPI_DOUBLE, 0, DATA_TAG, MPI_COMM_WORLD, &status);
  108.             // Encontrar el número de iteraciones en la formula de Mandelbrot
  109.             int n = cal_pixel(c);
  110.             MPI_Send(&n, sizeof(int), MPI_INT, 0, RESULT_TAG, MPI_COMM_WORLD);
  111.         }
  112.     }
  113. }
  114.  
  115. int cal_pixel(struct complejo c){
  116.     int count, max_iter;
  117.     struct complejo z;
  118.     double temp, lenghtsq;
  119.     max_iter = 256;
  120.     z.real = 0;
  121.     z.imaginario = 0;
  122.     count = 0;
  123.  
  124.  
  125.     do {
  126.         temp = z.real * z.real - z.imaginario * z.imaginario + c.real;
  127.         z.imaginario = 2 * z.real * z.imaginario + c.imaginario;
  128.         z.real = temp;
  129.         lenghtsq = z.real * z.real + z.imaginario * z.imaginario;
  130.         count++;
  131.     }while((lenghtsq < 4.0) && (count < max_iter));
  132.     return count;
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement