0

I am trying to implement a master/slave relationship which solves the mandelbrot set and prints it into a ppm file. This is what I have so far:

    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <mpi/mpi.h>


    int calculateMan (double , double ); //calculateMandelbrotSet

    MPI_Status status;

    struct Number {
        double R;
        double i;
    } Z,C;

    const int color;
    int colorTemp; //color value
    const int max = 1000;  //max iteration value
    const int ResHeight = 800; //Resolution
    const int ResWidth  = 800;
    double CRMax = 1.5;
    double CIMax = 2.0;
    double CRMin = -2.5;
    double CIMin = -2.0; //Constant values
    double colorWidth;
    double colorHeight;


    int main (int argc, char** argv) {
        int rank, size = 0;
        int nodos, source, dest;
        double startTime, endTime;

         //Rank = current process ID
         //Size = amount of processes

          MPI_Init (&argc, &argv);      // starts MPI

          startTime = MPI_Wtime();

          MPI_Comm_size (MPI_COMM_WORLD, &size);   // get number of processes
          MPI_Comm_rank (MPI_COMM_WORLD, &rank);  // get current process

        nodos = size - 1;

        if (rank == 0) { // MASTER --------------------------------------

            colorHeight = (CIMax - CIMin) / ResHeight;
            colorWidth = (CRMax - CRMin) / ResWidth;
            FILE *fp;

            fp = fopen("Mandelbrot.ppm","w");

            fprintf(fp,"P3\n %d\n %d\n %d\n",ResWidth,ResHeight,255); //Magic Number & Header

            for (int row = 0; row < ResHeight; row++) {
                C.i= CIMin + row*colorHeight;
                for (int column = 0; column < ResWidth; column++) {
                    C.R = CRMin + column*colorWidth;

                    //data sends
                     for (dest = 1; dest <= nodos; dest++) {
                     MPI_Send(&C.R, sizeof(double), MPI_DOUBLE, dest, column, MPI_COMM_WORLD);
                     MPI_Send(&C.i, sizeof(double), MPI_DOUBLE, dest, column, MPI_COMM_WORLD);
                         }
                    }
                }

           for (int row = 0; row < ResHeight; row++) {
               for (int column = 0; column < ResWidth; column++) {
                //Recv and print
                    MPI_Recv(&colorTemp, sizeof(int), MPI_DOUBLE, source, 0, MPI_COMM_WORLD, &status);
                   fprintf(fp, "%d %d %d\n", colorTemp, 1,3);
                    }
               }
           fclose(fp);
           } //------------------------- END MASTER

             if (rank > 0) // START SLAVE --------------------------------------
                {
                    for (int row = 0; row < ResHeight; row++) {
                        for (int column = 0; column < ResWidth; column++) {
                          MPI_Recv(&C.R, sizeof(double), MPI_DOUBLE, 0, column, MPI_COMM_WORLD, &status);
                          MPI_Recv(&C.i, sizeof(double), MPI_DOUBLE, 0, column, MPI_COMM_WORLD, &status);

                            colorTemp = calculateMan(C.R, C.i);
                            MPI_Send(&colorTemp, sizeof(int), MPI_INT, 0, 0, MPI_COMM_WORLD);
                            }
                    }
                } // SLAVE END---------------------------------


            endTime = MPI_Wtime(); //stop timer
            MPI_Finalize(); //end MPI
            printf("Time: %.6f\n", endTime-startTime);
            exit(0); //end program

    }

    int calculateMan (double CReal, double CImaginary) {
        int i = 0;
        Z.R = 0.0;
        Z.i = 0.0;

        while (((i < max) && (Z.R*Z.R) + (Z.i * Z.i) < 4))
        {
            double temp = (Z.R * Z.R) - (Z.i * Z.i) + CReal;
            Z.i = 2.0 * Z.R * Z.i + CImaginary;
            Z.R = temp;
            i++;
        }

        if (i == max)
            return 0; //interior is black
        else
            return 255; //exterior white
    }

I am trying to run my program but I cannot figure out why the RECV and print have an infinite iteration. Also, can anyone have a look at the code and tell me any sort of other issues or things I should look out for, for future reference?

Thanks!

4
  • Using global variables so cavalierly is bad practice. Nov 17, 2017 at 12:14
  • I didn't know that @DavidBowling, do you happen to have more info on it?
    – tiger123
    Nov 17, 2017 at 12:18
  • That is just a basic design principle; it is bad to use global variables unless absolutely necessary. Instead you should limit variable scopes as much as possible. Code does not have to be very large before it becomes easy to introduce duplicate identifiers, which can lead to difficult to find bugs. Not the problem here, just an observation. Nov 17, 2017 at 12:23
  • The second argument to MPI_Send / MPI_Recv is the number of elements, not the size in bytes! The size is already encoded in the MPI datatype, e.g., MPI knows that MPI_DOUBLE is sizeof double bytes long. You only need to tell it how many doubles there are and not how many bytes those take. Nov 17, 2017 at 12:31

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.