Advertisement
Guest User

Untitled

a guest
Dec 16th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.35 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <mpi.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5.  
  6. typedef struct {
  7.     char red;
  8.     char green;
  9.     char blue;
  10. } rgb;
  11. typedef struct {
  12.     int width;
  13.     int height;
  14.     int maxval;
  15.     int type;
  16.     rgb **pixels;
  17.     char **gray;
  18. } image;
  19.  
  20. void readInput(FILE *input, image *in) {
  21.     char *readBuffer;
  22.     fscanf(input, "%c %c", &readBuffer[0], readBuffer[1]);
  23.     in->type = atoi(readBuffer[1]);
  24.     fscanf(input, "%d %d", in->width, in->height);
  25.     fscanf(input, "%d", in->maxval);
  26.     if (in->type == 2) {
  27.         for (int i = 0; i < in->height; ++i) {
  28.             fread(in->gray[i], sizeof(char), in->width, input);
  29.         }
  30.     }
  31.     if (in->type == 3) {
  32.         for (int i = 0; i < in->height; ++i) {
  33.             fread(in->pixels[i], sizeof(rgb), in->width, input);
  34.         }
  35.     }
  36. }
  37.  
  38. void processImage(image *img, int rank, int nrProcesses) {
  39.     if (rank == 0) {
  40.         for (int i = 0; i < img->height; ++i) {
  41.             if (img->type == 2) {
  42.                 MPI_Recv(img->gray[i], img->width, MPI_CHAR, )
  43.             }
  44.         }
  45.         for (int x = 1; x < processes; ++x) {
  46.             for (int j = x; j < output->height; j += processes) {
  47.                 if (input->type == COLOR) {
  48.                     MPI_Recv(output->color_image[j], output->width * 3, MPI_CHAR, x, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  49.                 } else {
  50.                     MPI_Recv(output->gray_image[j], output->width, MPI_CHAR, x, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  51.                 }
  52.             }
  53.         }
  54.     }
  55.     if (rank != 0) {
  56.         for (int j = rank; j < output->height; j += processes) {
  57.             if (input->type == COLOR) {
  58.                 MPI_Send(output->color_image[j], output->width * 3, MPI_CHAR, 0, 0, MPI_COMM_WORLD);
  59.             } else {
  60.                 MPI_Send(output->gray_image[j], output->width, MPI_CHAR, 0, 0, MPI_COMM_WORLD);
  61.             }
  62.         }
  63.     }
  64. }
  65.  
  66. int main(int argc, char *argv[]) {
  67.     int rank, nrProcesses;
  68.     MPI_Init(&argc, &argv);
  69.     MPI_Comm_size(MPI_COMM_WORLD, &nrProcesses);
  70.     MPI_Comm_rank(MPI_COMM_WORLD, rank);
  71.     FILE *input, *output;
  72.     image in, out;
  73.     input = fopen(argv[1], "r");
  74.     if (input == NULL) {
  75.         return;
  76.     }
  77.     readInput(input, in);
  78.     processImage(in, rank, nrProcesses);
  79.     MPI_Finalize();
  80.     return 0;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement