YauhenMardan

Untitled

Oct 13th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.72 KB | None | 0 0
  1. //
  2. // main.cpp
  3. // CA-Lab2
  4. //
  5. // Created by Zhenya Mordan on 9/16/19.
  6. // Copyright © 2019 Zhenya Mordan. All rights reserved.
  7. //
  8.  
  9. #include "mpi.h"
  10. #include <iostream>
  11. #include <ctime>
  12. #include <fstream>
  13.  
  14. using namespace std;
  15.  
  16. void write(const char* ,int* , int );
  17. void merge(int *, int *, int, int, int);
  18. void mergeSort(int *, int *, int, int);
  19.  
  20. int main(int argc, char** argv) {
  21.  
  22. //read array
  23. const char* filename = argv[1];
  24.  
  25. ifstream ifile(filename);
  26. int n;
  27.  
  28. ifile>>n;
  29.  
  30. int *original_array = new int [n];
  31.  
  32. int i=0;
  33.  
  34. cout<< "Reading array...\n";
  35. if(ifile.is_open()){
  36. while (!ifile.eof()) {
  37. ifile>>original_array[i++];
  38. }
  39. }
  40. ifile.close();
  41.  
  42. ofstream ofile("output");
  43.  
  44. for(int i=0;i<n;i++){
  45. ofile<<original_array[i]<<" ";
  46. }
  47.  
  48. //init mpi
  49. int world_rank;
  50. int world_size;
  51.  
  52. MPI_Init(&argc, &argv);
  53. MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
  54. MPI_Comm_size(MPI_COMM_WORLD, &world_size);
  55.  
  56. //divide array
  57. int size = n/world_size;
  58.  
  59. //send each subarray to each process
  60. int *sub_array = new int [size];
  61. MPI_Scatter(original_array, size, MPI_INT, sub_array, size, MPI_INT, 0, MPI_COMM_WORLD);
  62.  
  63. //mergesort on each process
  64. int *tmp_array = new int [size];
  65. mergeSort(sub_array, tmp_array, 0, (size - 1));
  66.  
  67. //gather the sorted subarrays
  68. int *sorted = NULL;
  69. if(world_rank == 0) {
  70. sorted = new int [n];
  71. }
  72.  
  73. MPI_Gather(sub_array, size, MPI_INT, sorted, size, MPI_INT, 0, MPI_COMM_WORLD);
  74.  
  75. //make the final mergeSort call
  76. if(world_rank == 0) {
  77. int *other_array = new int [n];
  78. mergeSort(sorted, other_array, 0, (n - 1));
  79. //write the sorted array
  80. const char* ofilename = argv[2];
  81. write(ofilename, original_array, n);
  82. //clean up root
  83. delete [] sorted;
  84. delete [] other_array;
  85. }
  86. //clean up rest
  87. delete [] original_array;
  88. delete [] sub_array;
  89. delete [] tmp_array;
  90. //finalize MPI
  91. MPI_Barrier(MPI_COMM_WORLD);
  92. MPI_Finalize();
  93. //print runtime
  94. if(world_rank==0){
  95. cout << "Total time = " << clock()/1000.0 << endl;
  96. }
  97. return 0;
  98. }
  99.  
  100. void write(const char* filename,int* array, int n){
  101. ofstream ofile(filename);
  102.  
  103. cout<< "Writting array...\n";
  104. if(ofile.is_open()){
  105. for(int i=0;i<n;){
  106. ofile<<array[i++]<<"\n";
  107. }
  108. }
  109. ofile.close();
  110. }
  111.  
  112. void read(const char* filename,int* array, int &n){
  113.  
  114. }
  115.  
  116. //merge
  117. void merge(int *a, int *b, int l, int m, int r) {
  118.  
  119. int h, i, j, k;
  120. h = l;
  121. i = l;
  122. j = m + 1;
  123.  
  124. while((h <= m) && (j <= r)) {
  125.  
  126. if(a[h] <= a[j]) {
  127.  
  128. b[i] = a[h];
  129. h++;
  130.  
  131. }
  132.  
  133. else {
  134.  
  135. b[i] = a[j];
  136. j++;
  137.  
  138. }
  139.  
  140. i++;
  141.  
  142. }
  143.  
  144. if(m < h) {
  145.  
  146. for(k = j; k <= r; k++) {
  147.  
  148. b[i] = a[k];
  149. i++;
  150.  
  151. }
  152.  
  153. }
  154.  
  155. else {
  156.  
  157. for(k = h; k <= m; k++) {
  158.  
  159. b[i] = a[k];
  160. i++;
  161.  
  162. }
  163.  
  164. }
  165.  
  166. for(k = l; k <= r; k++) {
  167.  
  168. a[k] = b[k];
  169.  
  170. }
  171.  
  172. }
  173.  
  174. void mergeSort(int *a, int *b, int l, int r) {
  175.  
  176. int m;
  177.  
  178. if(l < r) {
  179.  
  180. m = (l + r)/2;
  181.  
  182. mergeSort(a, b, l, m);
  183. mergeSort(a, b, (m + 1), r);
  184. merge(a, b, l, m, r);
  185.  
  186. }
  187.  
  188. }
Advertisement
Add Comment
Please, Sign In to add comment