Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <mpi.h>
- #include <string.h>
- #include <stdlib.h>
- typedef struct {
- char red;
- char green;
- char blue;
- } rgb;
- typedef struct {
- int width;
- int height;
- int maxval;
- int type;
- rgb **pixels;
- char **gray;
- } image;
- void readInput(FILE *input, image *in) {
- char *readBuffer;
- fscanf(input, "%c %c", &readBuffer[0], readBuffer[1]);
- in->type = atoi(readBuffer[1]);
- fscanf(input, "%d %d", in->width, in->height);
- fscanf(input, "%d", in->maxval);
- if (in->type == 2) {
- for (int i = 0; i < in->height; ++i) {
- fread(in->gray[i], sizeof(char), in->width, input);
- }
- }
- if (in->type == 3) {
- for (int i = 0; i < in->height; ++i) {
- fread(in->pixels[i], sizeof(rgb), in->width, input);
- }
- }
- }
- void processImage(image *img, int rank, int nrProcesses) {
- if (rank == 0) {
- for (int i = 0; i < img->height; ++i) {
- if (img->type == 2) {
- MPI_Recv(img->gray[i], img->width, MPI_CHAR, )
- }
- }
- for (int x = 1; x < processes; ++x) {
- for (int j = x; j < output->height; j += processes) {
- if (input->type == COLOR) {
- MPI_Recv(output->color_image[j], output->width * 3, MPI_CHAR, x, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
- } else {
- MPI_Recv(output->gray_image[j], output->width, MPI_CHAR, x, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
- }
- }
- }
- }
- if (rank != 0) {
- for (int j = rank; j < output->height; j += processes) {
- if (input->type == COLOR) {
- MPI_Send(output->color_image[j], output->width * 3, MPI_CHAR, 0, 0, MPI_COMM_WORLD);
- } else {
- MPI_Send(output->gray_image[j], output->width, MPI_CHAR, 0, 0, MPI_COMM_WORLD);
- }
- }
- }
- }
- int main(int argc, char *argv[]) {
- int rank, nrProcesses;
- MPI_Init(&argc, &argv);
- MPI_Comm_size(MPI_COMM_WORLD, &nrProcesses);
- MPI_Comm_rank(MPI_COMM_WORLD, rank);
- FILE *input, *output;
- image in, out;
- input = fopen(argv[1], "r");
- if (input == NULL) {
- return;
- }
- readInput(input, in);
- processImage(in, rank, nrProcesses);
- MPI_Finalize();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement