Guest User

Untitled

a guest
Dec 9th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.02 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <math.h>
  4. #include <algorithm>
  5. #include <mpi.h>
  6. #include <omp.h>
  7. #include <chrono>
  8. #include <time.h>
  9.  
  10. using namespace std;
  11.  
  12. typedef struct {
  13. int label;
  14. float x, y;
  15. } Point;
  16.  
  17. vector<Point> get_train_set(int N) {
  18. // Training set creation
  19. vector<Point> dataset;
  20. Point p;
  21. float LO = 0.0, HI = 50.0;
  22. for (int i = 0; i < N; i++) {
  23. p.x = LO + static_cast<float>(rand()) / (static_cast<float>(RAND_MAX / (HI - LO)));
  24. p.y = LO + static_cast<float>(rand()) / (static_cast<float>(RAND_MAX / (HI - LO)));
  25. p.label = 0 + (rand() % static_cast<int>(2));
  26. dataset.push_back(p);
  27. }
  28. return dataset;
  29. }
  30.  
  31. vector<Point> get_test_set(int N) {
  32. // Test set creation
  33. vector<Point> dataset;
  34. Point p;
  35. float LO = 0.0, HI = 50.0;
  36. for (int i = 0; i < N; i++) {
  37. p.x = LO + static_cast<float>(rand()) / (static_cast<float>(RAND_MAX / (HI - LO)));
  38. p.y = LO + static_cast<float>(rand()) / (static_cast<float>(RAND_MAX / (HI - LO)));
  39. dataset.push_back(p);
  40. }
  41. return dataset;
  42. }
  43.  
  44. void print_dataset(vector<Point> dataset) {
  45. for (auto p : dataset) {
  46. printf("(%f, %f) - %d\n", p.x, p.y, p.label);
  47. }
  48. return;
  49. }
  50.  
  51. float distance(Point p1, Point p2) {
  52. // Distance between two points
  53. return sqrt(pow(p1.x - p2.x, 2) + pow(p1.y - p2.y, 2));
  54. }
  55.  
  56. bool cmp(pair<Point, float> &a, pair<Point, float> &b) {
  57. // Сomparing two points by distance to them
  58. return a.second < b.second;
  59. }
  60.  
  61. vector<Point> knn_classify_dataset(vector<Point> train, vector<Point> test, int k) {
  62. // KNN classification
  63. vector<Point> result;
  64. for (Point test_point : test) {
  65. vector<pair<Point, float>> distances;
  66. #pragma omp parallel num_threads(4)
  67. {
  68. #pragma omp for schedule(static)
  69. for (int i = 0; i < train.size(); i++) {
  70. pair<Point, float> p(train[i], distance(train[i], test_point));
  71. #pragma omp critical
  72. {
  73. distances.push_back(p);
  74. }
  75. }
  76. }
  77.  
  78. sort(distances.begin(), distances.end(), cmp);
  79.  
  80. float s = 0;
  81. int counter = 0;
  82. for (vector<pair<Point, float>>::iterator it = distances.begin(); it != distances.end(); ++it) {
  83. s += it->first.label;
  84. counter++;
  85. if (counter >= k)
  86. break;
  87. }
  88.  
  89. Point p;
  90. p.x = test_point.x;
  91. p.y = test_point.y;
  92. p.label = (int)round(s / k);
  93. result.push_back(p);
  94. }
  95. return result;
  96. }
  97.  
  98. int main(int argc, char* argv[]) {
  99. // MPI initialization
  100. MPI_Init(&argc, &argv);
  101. int rank, size;
  102. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  103. MPI_Comm_size(MPI_COMM_WORLD, &size);
  104.  
  105. chrono::time_point<chrono::system_clock> start, end;
  106. if (rank == 0)
  107. start = chrono::system_clock::now();
  108.  
  109. // definition of a custom type Point
  110. MPI_Datatype MPI_POINT;
  111. int blocklengths[3] = {1, 1, 1};
  112. MPI_Datatype types[3] = {MPI_INT, MPI_FLOAT, MPI_FLOAT};
  113. MPI_Aint displacements[3];
  114. MPI_Aint intex, floatex;
  115. MPI_Type_extent(MPI_INT, &intex);
  116. MPI_Type_extent(MPI_FLOAT, &floatex);
  117. displacements[0] = (MPI_Aint)0;
  118. displacements[1] = intex;
  119. displacements[2] = intex + floatex;
  120. MPI_Type_struct(3, blocklengths, displacements, types, &MPI_POINT);
  121. MPI_Type_commit(&MPI_POINT);
  122.  
  123. int N_train = 1000, N_test = 200;
  124. vector<Point> X_train = vector<Point>(N_train);
  125. vector<Point> X_test = vector<Point>(N_test);
  126. if (rank == 0) {
  127. X_train = get_train_set(N_train);
  128. X_test = get_test_set(N_test);
  129. }
  130. MPI_Bcast(&X_train[0], N_train, MPI_POINT, 0, MPI_COMM_WORLD);
  131. MPI_Barrier(MPI_COMM_WORLD);
  132.  
  133. vector<Point> X_test_part = vector<Point>(N_test / size);
  134. MPI_Scatter(&X_test[0], N_test / size, MPI_POINT, &X_test_part[0], N_test / size, MPI_POINT, 0, MPI_COMM_WORLD);
  135. MPI_Barrier(MPI_COMM_WORLD);
  136.  
  137. X_test_part = knn_classify_dataset(X_train, X_test_part, 5);
  138. MPI_Barrier(MPI_COMM_WORLD);
  139.  
  140. MPI_Gather(&X_test_part[0], N_test / size, MPI_POINT, &X_test[0], N_test / size, MPI_POINT, 0, MPI_COMM_WORLD);
  141. MPI_Barrier(MPI_COMM_WORLD);
  142.  
  143. if (rank == 0) {
  144. print_dataset(X_test);
  145. }
  146.  
  147. if (rank == 0) {
  148. end = chrono::system_clock::now();
  149. int elapsed_seconds = chrono::duration_cast<chrono::milliseconds>(end - start).count();
  150. printf("Elapsed time: %d milliseconds\n", elapsed_seconds);
  151. }
  152.  
  153. MPI_Finalize();
  154. return 0;
  155. }
Add Comment
Please, Sign In to add comment