fork download
  1. #include <iostream>
  2. #include "stdlib.h"
  3. #include "math.h"
  4. #include "time.h"
  5. #include "mpi.h"
  6.  
  7.  
  8. using namespace std;
  9.  
  10. int main(int argc,char *argv[])
  11. {
  12. int nPerNode,rank,size;
  13. MPI_Init(&argc,&argv);
  14. MPI_Comm_rank(MPI_COMM_WORLD,&rank);
  15. MPI_Comm_size(MPI_COMM_WORLD,&size);
  16.  
  17. int N,D,S;
  18. N=atoi(argv[1]);
  19. D=atoi(argv[2]);
  20. S=atoi(argv[3]);
  21. if(rank==0){
  22. cout<<"N:"<<N<<endl;
  23. cout<<"D:"<<D<<endl;
  24. cout<<"seed:"<<S<<endl;
  25. cout<<"The Distance data is using a simulated dynamic array"<<endl;
  26. cout<<"The Distance data is row-major"<<endl;
  27. }
  28.  
  29.  
  30. int MPIbegin,MPIend;
  31.  
  32. double Xsquare=0,Ysquare=0,Zsquare=0;//store to compute distance
  33. int temp2;
  34. double temp1;
  35. double Sign;
  36.  
  37. double **XYZ=new double *[N];
  38. for(int i=0;i<N;i++)
  39. {
  40. XYZ[i]=new double[3];
  41. }
  42.  
  43. double *Dist,*AllDist;
  44. Dist=new double[N*N]; //center to center distance
  45. AllDist=new double[N*N];
  46.  
  47. for(int i=0;i<N*N;i++){
  48. Dist[i]=0.0;
  49. AllDist[i]=0.0;}
  50.  
  51. if(rank==0){
  52. srand(S);//random seed
  53. }
  54.  
  55.  
  56. if(rank==0){
  57. for( int i=0;i<N;i++)
  58. {
  59. temp1=(rand());
  60. temp2=(rand()%2);
  61. Sign=pow(-1,temp2);//get sign
  62. XYZ[i][0]=temp1*D/RAND_MAX*Sign;// get x
  63.  
  64. temp1=(rand());
  65. temp2=(rand()%2);
  66. Sign=pow(-1,temp2);//get sign
  67. XYZ[i][1]=temp1*D/RAND_MAX*Sign;//get y
  68.  
  69. temp1=(rand());
  70. temp2=(rand()%2);
  71. Sign=pow(-1,temp2);//get sign
  72. XYZ[i][2]=temp1*D/RAND_MAX*Sign;//get z
  73.  
  74. }
  75. }
  76.  
  77. for(int i=0;i<N;i++)
  78. MPI_Bcast(&XYZ[i][0],3,MPI_DOUBLE,0,MPI_COMM_WORLD);
  79.  
  80.  
  81. MPI_Bcast(&Dist,N*N,MPI_DOUBLE,0,MPI_COMM_WORLD);
  82. MPI_Bcast(&AllDist,N*N,MPI_DOUBLE,0,MPI_COMM_WORLD);
  83. MPI_Bcast(&Xsquare,1,MPI_DOUBLE,0,MPI_COMM_WORLD);
  84. MPI_Bcast(&Ysquare,1,MPI_DOUBLE,0,MPI_COMM_WORLD);
  85. MPI_Bcast(&Zsquare,1,MPI_DOUBLE,0,MPI_COMM_WORLD);
  86.  
  87.  
  88. nPerNode=N/size;
  89. MPIbegin=rank*nPerNode+1;
  90. MPIend=MPIbegin+nPerNode-1;
  91. if(rank==(size-1)){
  92. MPIend=N-1;
  93. }
  94.  
  95. for(int j=MPIbegin;j<MPIend;j++)
  96. {
  97.  
  98. for(int i=0;i<=N;i++)//compute distance
  99. {
  100. Xsquare=(XYZ[j][0]*XYZ[j][0]+XYZ[i][0]*XYZ[i][0]-2*XYZ[i][0]*XYZ[j][0]); //x^2=(x1-x2)^2
  101. Ysquare=(XYZ[j][1]*XYZ[j][1]+XYZ[i][1]*XYZ[i][1]-2*XYZ[i][1]*XYZ[j][1]);
  102. Zsquare=(XYZ[j][2]*XYZ[j][2]+XYZ[i][2]*XYZ[i][2]-2*XYZ[i][2]*XYZ[j][2]);
  103. Dist[j*N+i]=sqrt(Xsquare+Ysquare+Zsquare);
  104.  
  105. }
  106. }
  107. for(int i=0;i<N*N ;i++)
  108. MPI_Reduce(&Dist[i],&AllDist[i],1,MPI_DOUBLE,MPI_SUM,0,MPI_COMM_WORLD);
  109.  
  110. MPI_Finalize();
  111.  
  112. return 0;
  113. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:5:17: error: mpi.h: No such file or directory
prog.cpp: In function ‘int main(int, char**)’:
prog.cpp:13: error: ‘MPI_Init’ was not declared in this scope
prog.cpp:14: error: ‘MPI_COMM_WORLD’ was not declared in this scope
prog.cpp:14: error: ‘MPI_Comm_rank’ was not declared in this scope
prog.cpp:15: error: ‘MPI_Comm_size’ was not declared in this scope
prog.cpp:78: error: ‘MPI_DOUBLE’ was not declared in this scope
prog.cpp:78: error: ‘MPI_Bcast’ was not declared in this scope
prog.cpp:81: error: ‘MPI_DOUBLE’ was not declared in this scope
prog.cpp:81: error: ‘MPI_Bcast’ was not declared in this scope
prog.cpp:108: error: ‘MPI_SUM’ was not declared in this scope
prog.cpp:108: error: ‘MPI_Reduce’ was not declared in this scope
prog.cpp:110: error: ‘MPI_Finalize’ was not declared in this scope
stdout
Standard output is empty