fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <mpi.h>
  4. #define ntotal 200
  5.  
  6. main ( argc, argv)
  7. int argc;
  8. char **argv;
  9. {
  10. double amax, gmax, a[ntotal], b[ntotal], c[ntotal], d[ntotal];
  11. int i, j, k;
  12. FILE *fp;
  13. int nproc, myid, istart, iend, icount, r_nbr, l_nbr, lastp;
  14. int itag, isrc, idest, istart1,icount1, istart2, iend1, istartm1, iendp1;
  15. int gstart[16], gend[16], gcount[16];
  16. MPI_Status istat[8];
  17. MPI_Comm comm;
  18. extern double max(double, double);
  19.  
  20. MPI_Init (&argc, &argv);
  21. MPI_Comm_size (MPI_COMM_WORLD, &nproc);
  22. MPI_Comm_rank (MPI_COMM_WORLD, &myid);
  23. comm=MPI_COMM_WORLD;
  24.  
  25. startend (nproc, 0, ntotal-1, gstart, gend, gcount);
  26. istart=gstart[myid];
  27. iend=gend[myid];
  28. icount=gcount[myid];
  29. lastp=nproc-1;
  30. printf( "NPROC,MYID,ISTART,IEND=%d\t%d\t%d\t%d\n",nproc,myid,istart,iend);
  31.  
  32. istartm1=istart-1;
  33. iendp1=iend+1;
  34.  
  35. istart2=istart;
  36. if (myid == 0) istart2=istart+1;
  37. iend1=iend;
  38. if(myid == lastp ) iend1=iend-1;
  39.  
  40. l_nbr = myid - 1;
  41. r_nbr = myid + 1;
  42. if (myid == 0) l_nbr=MPI_PROC_NULL;
  43. if (myid == lastp) r_nbr=MPI_PROC_NULL;
  44.  
  45. /* READ 'input.dat', and distribute input data */
  46.  
  47. if ( myid==0) {
  48. fp = fopen( "input.dat", "r");
  49. fread( (void *)&b, sizeof(b), 1, fp );
  50. fread( (void *)&c, sizeof(c), 1, fp );
  51. fread( (void *)&d, sizeof(d), 1, fp );
  52. fclose( fp );
  53.  
  54. for (idest = 1; idest < nproc; idest++) {
  55. istart1=gstart[idest];
  56. icount1=gcount[idest];
  57. itag=10;
  58. MPI_Send ((void *)&b[istart1], icount1, MPI_DOUBLE, idest, itag, comm);
  59. itag=20;
  60. MPI_Send ((void *)&c[istart1], icount1, MPI_DOUBLE, idest, itag, comm);
  61. itag=30;
  62. MPI_Send ((void *)&d[istart1], icount1, MPI_DOUBLE, idest, itag, comm);
  63. }
  64. }
  65. else {
  66. isrc=0;
  67. itag=10;
  68. MPI_Recv ((void *)&b[istart], icount, MPI_DOUBLE, isrc, itag, comm, istat);
  69. itag=20;
  70. MPI_Recv ((void *)&c[istart], icount, MPI_DOUBLE, isrc, itag, comm, istat);
  71. itag=30;
  72. MPI_Recv ((void *)&d[istart], icount, MPI_DOUBLE, isrc, itag, comm, istat);
  73. }
  74. /*
  75.   Exchange data outside the territory
  76. */
  77. itag=110;
  78. MPI_Sendrecv((void *)&b[iend], 1, MPI_DOUBLE, r_nbr, itag,
  79. (void *)&b[istartm1],1, MPI_DOUBLE, l_nbr, itag, comm, istat);
  80. itag=120;
  81. MPI_Sendrecv((void *)&b[istart], 1, MPI_DOUBLE, l_nbr, itag,
  82. (void *)&b[iendp1],1, MPI_DOUBLE, r_nbr, itag, comm, istat);
  83. /*
  84.   Compute, gather and write out the computed result
  85. */
  86. amax= -1.0e12;
  87. for (i=istart2; i<=iend1; i++) {
  88. a[i]=c[i]*d[i]+(b[i-1]+2.0*b[i]+b[i+1])*0.25;
  89. amax=max(amax,a[i]);
  90. }
  91. itag=130;
  92. if (myid > 0) {
  93. idest=0;
  94. MPI_Send((void *)&a[istart], icount, MPI_DOUBLE, idest, itag, icomm);
  95. }
  96. else {
  97. for (isrc=1; isrc<nproc; isrc++) {
  98. istart1=gstart[isrc];
  99. icount1=gcount[isrc];
  100. MPI_Recv((void *)&a[istart1], icount1, MPI_DOUBLE, isrc, itag, comm, istat);
  101. }
  102. }
  103. MPI_Allreduce((void *)&amax, (void *)&gmax, 1, MPI_DOUBLE, MPI_MAX, comm);
  104. amax=gmax;
  105. if( myid == 0) {
  106. for (i = 0; i < ntotal; i+=40) {
  107. printf( "%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\n",
  108. a[i],a[i+5],a[i+10],a[i+15],a[i+20],a[i+25],a[i+30],a[i+35]);
  109. }
  110. printf ("MAXIMUM VALUE OF ARRAY A is %f\n", amax);
  111. }
  112. MPI_Finalize();
  113. return 0;
  114. }
  115. double max(double a, double b)
  116. {
  117. if(a >= b)
  118. return a;
  119. else
  120. return b; }
  121.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c:3:17: error: mpi.h: No such file or directory
prog.c:7: warning: return type defaults to ‘int’
prog.c: In function ‘main’:
prog.c:16: error: ‘MPI_Status’ undeclared (first use in this function)
prog.c:16: error: (Each undeclared identifier is reported only once
prog.c:16: error: for each function it appears in.)
prog.c:16: error: expected ‘;’ before ‘istat’
prog.c:17: error: ‘MPI_Comm’ undeclared (first use in this function)
prog.c:17: error: expected ‘;’ before ‘comm’
prog.c:20: warning: implicit declaration of function ‘MPI_Init’
prog.c:21: warning: implicit declaration of function ‘MPI_Comm_size’
prog.c:21: error: ‘MPI_COMM_WORLD’ undeclared (first use in this function)
prog.c:22: warning: implicit declaration of function ‘MPI_Comm_rank’
prog.c:23: error: ‘comm’ undeclared (first use in this function)
prog.c:25: warning: implicit declaration of function ‘startend’
prog.c:42: error: ‘MPI_PROC_NULL’ undeclared (first use in this function)
prog.c:58: warning: implicit declaration of function ‘MPI_Send’
prog.c:58: error: ‘MPI_DOUBLE’ undeclared (first use in this function)
prog.c:68: warning: implicit declaration of function ‘MPI_Recv’
prog.c:68: error: ‘istat’ undeclared (first use in this function)
prog.c:78: warning: implicit declaration of function ‘MPI_Sendrecv’
prog.c:94: error: ‘icomm’ undeclared (first use in this function)
prog.c:103: warning: implicit declaration of function ‘MPI_Allreduce’
prog.c:103: error: ‘MPI_MAX’ undeclared (first use in this function)
prog.c:112: warning: implicit declaration of function ‘MPI_Finalize’
prog.c:11: warning: unused variable ‘k’
prog.c:11: warning: unused variable ‘j’
prog.c:49: warning: ignoring return value of ‘fread’, declared with attribute warn_unused_result
prog.c:50: warning: ignoring return value of ‘fread’, declared with attribute warn_unused_result
prog.c:51: warning: ignoring return value of ‘fread’, declared with attribute warn_unused_result
stdout
Standard output is empty