fork download
  1. #include <mpi.h>
  2. #include <iostream>
  3. #include <vector>
  4. #include <string>
  5. #include <cmath>
  6.  
  7. class ParallelCalculator {
  8. private:
  9. int rank, size;
  10. std::vector<double> numbers;
  11.  
  12. enum Operation {
  13. ADD = 1,
  14. MULTIPLY = 2,
  15. POWER = 3,
  16. FACTORIAL = 4,
  17. AVERAGE = 5
  18. };
  19.  
  20. double calculateFactorial(int n) {
  21. if (n <= 1) return 1;
  22. double result = 1;
  23. for (int i = 2; i <= n; i++) {
  24. result *= i;
  25. }
  26. return result;
  27. }
  28.  
  29. public:
  30. ParallelCalculator(int r, int s) : rank(r), size(s) {}
  31.  
  32. void readNumbers() {
  33. if (rank == 0) {
  34. int count;
  35. std::cout << "How many numbers do you want to enter? ";
  36. std::cin >> count;
  37.  
  38. numbers.resize(count);
  39. std::cout << "Enter " << count << " numbers:\n";
  40. for (int i = 0; i < count; i++) {
  41. std::cout << "Number " << (i + 1) << ": ";
  42. std::cin >> numbers[i];
  43. }
  44. }
  45.  
  46. int count = numbers.size();
  47. MPI_Bcast(&count, 1, MPI_INT, 0, MPI_COMM_WORLD);
  48.  
  49. if (rank != 0) {
  50. numbers.resize(count);
  51. }
  52.  
  53. MPI_Bcast(numbers.data(), count, MPI_DOUBLE, 0, MPI_COMM_WORLD);
  54. }
  55.  
  56. void displayMenu() {
  57. if (rank == 0) {
  58. std::cout << "\nAvailable Operations:\n";
  59. std::cout << "1. Add Numbers\n";
  60. std::cout << "2. Multiply Numbers\n";
  61. std::cout << "3. Raise All Numbers to Power\n";
  62. std::cout << "4. Calculate Factorial of Numbers\n";
  63. std::cout << "5. Calculate Average\n";
  64. std::cout << "6. Enter New Numbers\n";
  65. std::cout << "7. Exit\n";
  66. std::cout << "Choose operation: ";
  67. }
  68. }
  69.  
  70. void processOperation(int operation) {
  71. int local_size = numbers.size() / size;
  72. int remainder = numbers.size() % size;
  73. int start_idx = rank * local_size + std::min(rank, remainder);
  74. if (rank < remainder) local_size++;
  75.  
  76. double local_result = 0;
  77.  
  78. if (operation == POWER) {
  79. double power_exponent = 2;
  80. if (rank == 0) {
  81. std::cout << "Enter power value: ";
  82. std::cin >> power_exponent;
  83. }
  84. MPI_Bcast(&power_exponent, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD);
  85.  
  86. std::vector<double> local_results;
  87. for (int i = 0; i < local_size && start_idx + i < numbers.size(); i++) {
  88. double result = std::pow(numbers[start_idx + i], power_exponent);
  89. local_results.push_back(result);
  90. }
  91.  
  92. if (rank == 0) {
  93. std::vector<int> recvcounts(size);
  94. std::vector<int> displs(size);
  95.  
  96. for (int i = 0; i < size; i++) {
  97. recvcounts[i] = numbers.size() / size;
  98. if (i < remainder) recvcounts[i]++;
  99. displs[i] = (i > 0) ? displs[i-1] + recvcounts[i-1] : 0;
  100. }
  101.  
  102. std::vector<double> all_results(numbers.size());
  103. MPI_Gatherv(local_results.data(), local_results.size(), MPI_DOUBLE,
  104. all_results.data(), recvcounts.data(), displs.data(),
  105. MPI_DOUBLE, 0, MPI_COMM_WORLD);
  106.  
  107. for (int i = 0; i < numbers.size(); i++) {
  108. std::cout << numbers[i] << " ^ " << power_exponent
  109. << " = " << all_results[i] << "\n";
  110. }
  111. } else {
  112. MPI_Gatherv(local_results.data(), local_results.size(), MPI_DOUBLE,
  113. nullptr, nullptr, nullptr, MPI_DOUBLE, 0, MPI_COMM_WORLD);
  114. }
  115. return;
  116. }
  117. else if (operation == FACTORIAL) {
  118. for (int i = 0; i < local_size && start_idx + i < numbers.size(); i++) {
  119. double result = calculateFactorial(static_cast<int>(numbers[start_idx + i]));
  120. if (rank == 0) {
  121. std::cout << numbers[start_idx + i] << "! = " << result << "\n";
  122. }
  123. }
  124. return;
  125. }
  126. else {
  127. switch (operation) {
  128. case ADD:
  129. local_result = 0;
  130. for (int i = 0; i < local_size && start_idx + i < numbers.size(); i++) {
  131. local_result += numbers[start_idx + i];
  132. }
  133. break;
  134.  
  135. case MULTIPLY:
  136. local_result = 1;
  137. for (int i = 0; i < local_size && start_idx + i < numbers.size(); i++) {
  138. local_result *= numbers[start_idx + i];
  139. }
  140. break;
  141.  
  142. case AVERAGE:
  143. local_result = 0;
  144. for (int i = 0; i < local_size && start_idx + i < numbers.size(); i++) {
  145. local_result += numbers[start_idx + i];
  146. }
  147. break;
  148. }
  149.  
  150. double global_result;
  151. MPI_Reduce(&local_result, &global_result, 1, MPI_DOUBLE,
  152. operation == MULTIPLY ? MPI_PROD : MPI_SUM, 0, MPI_COMM_WORLD);
  153.  
  154. if (rank == 0) {
  155. switch (operation) {
  156. case ADD:
  157. std::cout << "Sum of numbers = " << global_result << "\n";
  158. break;
  159. case MULTIPLY:
  160. std::cout << "Product of numbers = " << global_result << "\n";
  161. break;
  162. case AVERAGE:
  163. std::cout << "Average of numbers = " << global_result / numbers.size() << "\n";
  164. break;
  165. }
  166. }
  167. }
  168. }
  169.  
  170. void run() {
  171. readNumbers();
  172.  
  173. while (true) {
  174. int choice = 0;
  175.  
  176. if (rank == 0) {
  177. displayMenu();
  178. std::cin >> choice;
  179. }
  180.  
  181. MPI_Bcast(&choice, 1, MPI_INT, 0, MPI_COMM_WORLD);
  182.  
  183. if (choice == 7) break;
  184.  
  185. if (choice == 6) {
  186. readNumbers();
  187. continue;
  188. }
  189.  
  190. if (choice >= 1 && choice <= 5) {
  191. processOperation(choice);
  192. } else if (rank == 0) {
  193. std::cout << "Invalid choice!\n";
  194. }
  195. }
  196. }
  197. };
  198.  
  199. int main(int argc, char** argv) {
  200. MPI_Init(&argc, &argv);
  201.  
  202. int rank, size;
  203. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  204. MPI_Comm_size(MPI_COMM_WORLD, &size);
  205.  
  206. ParallelCalculator calculator(rank, size);
  207. calculator.run();
  208.  
  209. MPI_Finalize();
  210. return 0;
  211. }
Success #stdin #stdout #stderr 0.25s 40636KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Error: unexpected symbol in "class ParallelCalculator"
Execution halted