Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.17 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <cmath>
  3. #include <mpi.h>
  4. #include <fstream>
  5. #include <iostream>
  6.  
  7.  
  8.  
  9. long double xZeroFunction()
  10. {
  11. return 0.0;
  12. }
  13. long double xOneFunction(long double t)
  14. {
  15. return pow(2.0, 3.0 / 4.0)*exp(4.0*t);
  16. }
  17. long double CorrectXTFunction(long double x, long double t)
  18. {
  19. return exp(4.0*t)*pow(2.0*x, 3.0 / 4.0);
  20. }
  21. long double StepXFunction(long double wiPlus1, long double wiMinus1, long double wi, long double tau, long double h)
  22. {
  23. return wi + tau * (4.0*wi + ((3.0*wi*((wiPlus1 - 2.0*wi + wiMinus1) / pow(h, 2.0)) + pow((wiPlus1 - wi) / h, 2.0)) / (3.0*pow(wi, 2.0 / 3.0))));
  24. }
  25. long double ZeroZFunction(long double x)
  26. {
  27. return pow(2.0*x, 3.0 / 4.0);
  28. }
  29.  
  30.  
  31.  
  32. int main(int argc, char* argv[])
  33. {
  34. const int numberOfStepsX = 20;
  35. const int numberOfStepsT = 2000;
  36.  
  37. const long double stepX = 1.0 / numberOfStepsX;
  38. const long double stepT = 1.0 / numberOfStepsT;
  39.  
  40. MPI_Init(&argc, &argv);
  41.  
  42. int myid, numprocs;
  43. MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
  44. MPI_Comm_rank(MPI_COMM_WORLD, &myid);
  45.  
  46. long double matrixOfCorrectAnswers[numberOfStepsT][numberOfStepsX] = { 0 };
  47. long double matrixOfApproximateAnswers[numberOfStepsT][numberOfStepsX] = { 0 };
  48.  
  49. long double currentX = 0.0;
  50. long double currentT = 0.0;
  51. double t1, t2;
  52. if (myid == 0)
  53. {
  54. for (int i = 0; i < numberOfStepsT; i++)
  55. {
  56. currentX = 0.0;
  57. for (int j = 0; j < numberOfStepsX; j++)
  58. {
  59. matrixOfCorrectAnswers[i][j] = CorrectXTFunction(currentX, currentT);
  60. currentX += stepX;
  61. }
  62. currentT += stepT;
  63. }
  64. // Algorithm starts
  65. t1 = MPI_Wtime();
  66. currentX = 0.0;
  67. for (int i = 0; i < numberOfStepsX; i++)
  68. {
  69. matrixOfApproximateAnswers[0][i] = ZeroZFunction(currentX);
  70. currentX += stepX;
  71. }
  72.  
  73. currentT = 0.0;
  74. for (int i = 0; i < numberOfStepsT; i++)
  75. {
  76. matrixOfApproximateAnswers[i][0] = xZeroFunction();
  77. matrixOfApproximateAnswers[i][numberOfStepsX - 1] = xOneFunction(currentT);
  78. currentT += stepT;
  79. }
  80. }
  81. for (int i = 1; i < numberOfStepsT; i++)
  82. {
  83. MPI_Bcast(matrixOfApproximateAnswers[i - 1], numberOfStepsX, MPI_LONG_DOUBLE, 0, MPI_COMM_WORLD);
  84. for (int j = myid + 1; j < numberOfStepsX - 1; j += numprocs)
  85. {
  86. matrixOfApproximateAnswers[i][j] = StepXFunction(matrixOfApproximateAnswers[i - 1][j + 1],
  87. matrixOfApproximateAnswers[i - 1][j - 1], matrixOfApproximateAnswers[i - 1][j], stepT, stepX);
  88. }
  89. if (myid == 0)
  90. {
  91. for (int k = 1; k < numprocs; k++)
  92. {
  93. long double temp[numberOfStepsX];
  94. MPI_Recv(temp, numberOfStepsX, MPI_LONG_DOUBLE, k, MPI_ANY_TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  95. for (int j = k; j < numberOfStepsX - 1; j += numprocs)
  96. {
  97. matrixOfApproximateAnswers[i][j] = temp[j];
  98. }
  99. }
  100. }
  101. else
  102. {
  103. MPI_Send(&matrixOfApproximateAnswers[i], numberOfStepsX, MPI_LONG_DOUBLE, 0, 0, MPI_COMM_WORLD);
  104. }
  105. }
  106.  
  107. if (myid == 0)
  108. {
  109. // Algorithm finishes
  110. t2 = MPI_Wtime();
  111. printf("Elapsed time is %f\n", t2 - t1);
  112. // Saving results to file
  113. std::ofstream fout1("/Users/Nazar Shevchuk/Kursova/results.txt");
  114. std::ofstream fout2("/Users/Nazar Shevchuk/Kursova/results_yavno.txt");
  115. for (int i = 0; i < numberOfStepsT; i++)
  116. {
  117. for (int j = 0; j < numberOfStepsX; j++)
  118. {
  119. //printf("%3.20Lf - %3.20Lf\n", matrixOfCorrectAnswers[i][j], matrixOfApproximateAnswers[i][j]);
  120. fout1 << matrixOfCorrectAnswers[i][j] << '\t';
  121. fout2 << matrixOfApproximateAnswers[i][j] << '\t';
  122. }
  123. fout1 << std::endl;
  124. fout2 << std::endl;
  125. }
  126. fout1.close();
  127. fout2.close();
  128. }
  129. MPI_Finalize();
  130. System ("pause");
  131. return 0;
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement