fork download
  1. library(Rmpi)
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <math.h>
  6. #include <mpi.h>
  7.  
  8.  
  9. // Define a structure to hold data points with features and labels
  10. typedef struct {
  11. double features[2];
  12. int label;
  13. } DataPoint;
  14.  
  15. double euclidean_distance(double *x1, double *x2, int num_features) {
  16. // the Euclidean distance between two points is the square root of the sum of the squared
  17. // differences between the corresponding values
  18. double distance = 0.0;
  19. for (int i = 0; i < num_features; i++) {
  20. distance += pow(x1[i] - x2[i], 2);
  21. }
  22.  
  23. return sqrt(distance);
  24. }
  25.  
  26. void find_k_nearest_neighbours(DataPoint *train_data, int train_size, double *test_point, int num_features, int k, double *distances, int *labels) {
  27. for (int i = 0; i < train_data; i++) {
  28. distances[i] = euclidean_distance(train_data[i].features, test_point, num_features);
  29. labels[i] = train_data[i].label;
  30. }
  31. }
  32.  
  33. int main(int argc, char *argv[]) {
  34. // Initialize MPI
  35. MPI_Init(&argc, &argv);
  36.  
  37. int rank, size;
  38. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  39. MPI_Comm_size(MPI_COMM_WORLD, &size);
  40.  
  41. // Define the training data
  42. int num_features = 2;
  43. int train_size = 12;
  44.  
  45. DataPoint train_data[12] = {
  46. {{1.0, 1.0}, 0},
  47. {{1.0, 2.0}, 0},
  48. {{2.0, 1.0}, 0},
  49. {{2.0, 2.0}, 0},
  50. {{5.0, 5.0}, 1},
  51. {{5.0, 6.0}, 1},
  52. {{6.0, 5.0}, 1},
  53. {{6.0, 6.0}, 1},
  54. {{10.0, 10.0}, 2},
  55. {{10.0, 11.0}, 2},
  56. {{11.0, 10.0}, 2},
  57. {{11.0, 11.0}, 2}
  58. };
  59.  
  60. // Define the test data
  61. double test_point[2] = {3.0, 3.0};
  62. int k = 3;
  63.  
  64. // Allocate memory for arrays to hold distances and labels
  65. int local_size = train_size / size;
  66. int remainder = train_size % size;
  67.  
  68. int *sendcounts = (int *)malloc(size * sizeof(int));
  69. int *displs = (int *)malloc(size * sizeof(int));
  70. for (int i = 0; i < size; i++) {
  71. sendcounts[i] = (i < remainder) ? local_size + 1 : local_size;
  72. displs[i] = (i == 0) ? 0 : displs[i - 1] + sendcounts[i - 1];
  73. }
  74.  
  75. double *local_train_data = (DataPoint *)malloc(sendcounts[rank] * sizeof(DataPoint));
  76. MPI_Scatterv(train_data, sendcounts, displs, MPI_DOUBLE, local_train_data, sendcounts[rank], MPI_DOUBLE, 0, MPI_COMM_WORLD);
  77.  
  78. double *local_distances = (double *)malloc(sendcounts[rank] * sizeof(double));
  79. int *local_labels = (int *)malloc(sendcounts[rank] * sizeof(int));
  80.  
  81. find_k_nearest_neighbours(local_train_data, sendcounts[rank], test_point, num_features, k, local_distances, local_labels);
  82.  
  83. double *distances = NULL;
  84. int *labels = NULL;
  85.  
  86. if (rank == 0) {
  87. distances = (double *)malloc(train_size * sizeof(double));
  88. labels = (int *)malloc(train_size * sizeof(int));
  89. }
  90.  
  91. MPI_Gatherv(local_distances, sendcounts[rank], MPI_DOUBLE, distances, sendcounts, displs, MPI_DOUBLE, 0, MPI_COMM_WORLD);
  92. MPI_Gatherv(local_labels, sendcounts[rank], MPI_INT, labels, sendcounts, displs, MPI_INT, 0, MPI_COMM_WORLD);
  93.  
  94. if (rank == 0) {
  95. for (int i = 0; i < train_size - 1; i++) {
  96. for (int j = i + 1; j < train_size; j++) {
  97. if (distances[i] > distances[j]) {
  98. double temp_distance = distances[i];
  99. distances[i] = distances[j];
  100. distances[j] = temp_distance;
  101.  
  102. int temp_label = labels[i];
  103. labels[i] = labels[j];
  104. labels[j] = temp_label;
  105. }
  106. }
  107. }
  108.  
  109. // find the most common label among the k nearest neighbours
  110. int *label_count = (int *)calloc(train_size, sizeof(int));
  111. for (int i = 0; i < k; i++) {
  112. label_count[labels[i]]++;
  113. }
  114.  
  115. // Find the label with the maximum count
  116. int max_count = 0;
  117. int predicted_label = -1;
  118. for (int i = 0; i < train_size; i++) {
  119. if (label_count[i] > max_count) {
  120. max_count = label_count[i];
  121. predicted_label = i;
  122. }
  123. }
  124.  
  125. free(distances);
  126. free(labels);
  127. free(label_count);
  128.  
  129. printf("Predicted label: %d\n", predicted_label);
  130. }
  131.  
  132. free(local_train_data);
  133. free(local_distances);
  134. free(local_labels);
  135. free(sendcounts);
  136. free(displs);
  137.  
  138. // Finalize MPI
  139. MPI_Finalize();
  140.  
  141. return 0;
  142. }
Success #stdin #stdout #stderr 0.28s 41104KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Error in library(Rmpi) : there is no package called ‘Rmpi’
Execution halted