Advertisement
bkit4s0

[mpi_mmc.c]

Mar 11th, 2015
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.69 KB | None | 0 0
  1. #include "mpi.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. #define NRA 62                 /* number of rows in matrix A */
  6. #define NCA 15                 /* number of columns in matrix A */
  7. #define NCB 7                  /* number of columns in matrix B */
  8. #define MASTER 0               /* taskid of first task */
  9. #define FROM_MASTER 1          /* setting a message type */
  10. #define FROM_WORKER 2          /* setting a message type */
  11.  
  12. int main (int argc, char *argv[])
  13. {
  14. int     numtasks,              /* number of tasks in partition */
  15.         taskid,                /* a task identifier */
  16.         numworkers,            /* number of worker tasks */
  17.         source,                /* task id of message source */
  18.         dest,                  /* task id of message destination */
  19.         mtype,                 /* message type */
  20.         rows,                  /* rows of matrix A sent to each worker */
  21.         averow, extra, offset, /* used to determine rows sent to each worker */
  22.         i, j, k, rc;           /* misc */
  23. double  a[NRA][NCA],           /* matrix A to be multiplied */
  24.         b[NCA][NCB],           /* matrix B to be multiplied */
  25.         c[NRA][NCB],           /* result matrix C */
  26. MPI_Status status;
  27.  
  28. MPI_Init(&argc,&argv);
  29. MPI_Comm_rank(MPI_COMM_WORLD,&taskid);
  30. MPI_Comm_size(MPI_COMM_WORLD,&numtasks);
  31. if (numtasks < 2 ) {
  32.   printf("Need at least two MPI tasks. Quitting...\n");
  33.   MPI_Abort(MPI_COMM_WORLD, rc);
  34.   exit(1);
  35.   }
  36. numworkers = numtasks-1;
  37.  
  38.  
  39. /**************************** master task ************************************/
  40.    if (taskid == MASTER)
  41.    {
  42.       printf("mpi_mm has started with %d tasks.\n",numtasks);
  43.       printf("Initializing arrays...\n");
  44.       for (i=0; i<NRA; i++)
  45.          for (j=0; j<NCA; j++)
  46.             a[i][j]= i+j;
  47.       for (i=0; i<NCA; i++)
  48.          for (j=0; j<NCB; j++)
  49.             b[i][j]= i*j;
  50.  
  51.       /* Send matrix data to the worker tasks */
  52.       averow = NRA/numworkers;
  53.       extra = NRA%numworkers;
  54.       offset = 0;
  55.       mtype = FROM_MASTER;
  56.       for (dest=1; dest<=numworkers; dest++)
  57.       {
  58.          rows = (dest <= extra) ? averow+1 : averow;
  59.          printf("Sending %d rows to task %d offset=%d\n",rows,dest,offset);
  60.        MPI_Bcast(&offset, 1, MPI_INT, dest, MPI_COMM_WORLD);
  61.          MPI_Bcast(&rows, 1, MPI_INT, dest, MPI_COMM_WORLD);
  62.         MPI_Scatter(&a[offset][0], rows*NCA, MPI_DOUBLE,/*irecv,count*/ dest,
  63.                    MPI_COMM_WORLD);
  64.         MPOI_Scatter(&b,NCA*NCB,MPI_DOUBLE,/*irecv,count*/,dest,MPI_COMM_WORLD);
  65.          // MPI_Send(&offset, 1, MPI_INT, dest, mtype, MPI_COMM_WORLD);
  66.         // MPI_Send(&rows, 1, MPI_INT, dest, mtype, MPI_COMM_WORLD);
  67.         // MPI_Send(&a[offset][0], rows*NCA, MPI_DOUBLE, dest, mtype,
  68.         //           MPI_COMM_WORLD);
  69.         // MPI_Send(&b, NCA*NCB, MPI_DOUBLE, dest, mtype, MPI_COMM_WORLD);
  70.          offset = offset + rows;
  71.       }
  72.  
  73.       /* Receive results from worker tasks */
  74.       mtype = FROM_WORKER;
  75.       for (i=1; i<=numworkers; i++)
  76.       {
  77.          source = i;
  78.         // MPI_Recv(&offset, 1, MPI_INT, source, mtype, MPI_COMM_WORLD, &status);
  79.         // MPI_Recv(&rows, 1, MPI_INT, source, mtype, MPI_COMM_WORLD, &status);
  80.         // MPI_Recv(&c[offset][0], rows*NCB, MPI_DOUBLE, source, mtype,
  81.          //         MPI_COMM_WORLD, &status);
  82.          printf("Received results from task %d\n",source);
  83.       }
  84.  
  85.       /* Print results */
  86.       printf("******************************************************\n");
  87.       printf("Result Matrix:\n");
  88.       for (i=0; i<NRA; i++)
  89.       {
  90.          printf("\n");
  91.          for (j=0; j<NCB; j++)
  92.             printf("%6.2f   ", c[i][j]);
  93.       }
  94.       printf("\n******************************************************\n");
  95.       printf ("Done.\n");
  96.    }
  97.  
  98.  
  99. /**************************** worker task ************************************/
  100.    if (taskid > MASTER)
  101.    {
  102.       mtype = FROM_MASTER;
  103.      // MPI_Recv(&offset, 1, MPI_INT, MASTER, mtype, MPI_COMM_WORLD, &status);
  104.      // MPI_Recv(&rows, 1, MPI_INT, MASTER, mtype, MPI_COMM_WORLD, &status);
  105.      // MPI_Recv(&a, rows*NCA, MPI_DOUBLE, MASTER, mtype, MPI_COMM_WORLD, &status);
  106.      // MPI_Recv(&b, NCA*NCB, MPI_DOUBLE, MASTER, mtype, MPI_COMM_WORLD, &status);
  107.       for (k=0; k<NCB; k++)
  108.          for (i=0; i<rows; i++)
  109.          {
  110.             c[i][k] = 0.0;
  111.             for (j=0; j<NCA; j++)
  112.                c[i][k] = c[i][k] + a[i][j] * b[j][k];
  113.          }
  114.       mtype = FROM_WORKER;
  115.       MPI_Send(&offset, 1, MPI_INT, MASTER, mtype, MPI_COMM_WORLD);
  116.       MPI_Send(&rows, 1, MPI_INT, MASTER, mtype, MPI_COMM_WORLD);
  117.       MPI_Send(&c, rows*NCB, MPI_DOUBLE, MASTER, mtype, MPI_COMM_WORLD);
  118.    }
  119.    MPI_Finalize();
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement