fork download
  1.  
  2. #include <stdio.h>
  3. #include "mpi.h"
  4. #include <dirent.h> // For opendir(), readdir(), and closedir()
  5.  
  6. int directoryExists(const char *path) {
  7. DIR *dir = opendir(path);
  8. if (dir) {
  9. closedir(dir);
  10. return 1;
  11. } else {
  12. return 0;
  13. }
  14. }
  15.  
  16. int CountOccurrences(int target, const char *FileName) {
  17. FILE *file = fopen(FileName, "r");
  18. if (file == NULL) {
  19. perror("Error opening file");
  20. return -1;
  21. }
  22.  
  23. int count = 0;
  24. int num;
  25. while (fscanf(file, "%d", &num) == 1) {
  26. if (num == target) {
  27. count++;
  28. }
  29. }
  30.  
  31. fclose(file);
  32. return count;
  33. }
  34.  
  35. int main( int argc, char **argv )
  36. {
  37. const int Files = 100;
  38. const int NumPerFile = 100;
  39. int rank,size;
  40. int x;
  41. char FileNames[Files][30];
  42. char recievedFileNames[Files][30];
  43. int localCount = 0, totalCount = 0;
  44. MPI_Status status;
  45.  
  46. MPI_Init(&argc, &argv);
  47. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  48. MPI_Comm_size(MPI_COMM_WORLD, &size);
  49.  
  50. if(rank ==0){
  51. char directory[30];
  52. // take directory
  53. do{
  54. printf("Enter the directory path: ");
  55. scanf("%s", directory);
  56. }
  57. while (!directoryExists(directory));
  58.  
  59. // create Files with random numbers
  60. for (int i = 0; i < Files; i++) {
  61. sprintf(FileNames[i], "%s/file_%d.txt", directory, i);
  62. FILE *file = fopen(FileNames[i], "w");
  63. if (file == NULL) {
  64. perror("Error creating file");
  65. MPI_Finalize();
  66. return 1;
  67. }
  68. for (int j = 0; j < NumPerFile; j++) {
  69. fprintf(file, "%d\n", rand() % 100 + 1);
  70. }
  71. fclose(file);
  72. }
  73.  
  74. printf("Enter number x: \n");
  75. scanf("%d", &x);
  76.  
  77.  
  78. MPI_Scatter(FileNames, Files/size *30, MPI_CHAR, recievedFileNames, Files/size *30, MPI_CHAR, 0, MPI_COMM_WORLD);
  79.  
  80. // handle remaining files
  81. for(int i = Files - Files% size ; i < Files; i++){
  82. localCount += CountOccurrences(x,FileNames[i]);
  83. }
  84. }
  85.  
  86. localCount = 0;
  87. MPI_Bcast(&x,1,MPI_INT,0,MPI_COMM_WORLD);
  88.  
  89. MPI_Scatter(FileNames, Files/size * 30, MPI_CHAR, recievedFileNames, Files/size * 30, MPI_CHAR, 0, MPI_COMM_WORLD);
  90.  
  91.  
  92. for (int i = 0; i < Files/size; i++)
  93. {
  94. printf("%s\n",recievedFileNames[i]);
  95. localCount += CountOccurrences(x,recievedFileNames[i]);
  96. }
  97.  
  98. MPI_Reduce(&localCount, &totalCount, 1, MPI_INT, MPI_SUM, 0,MPI_COMM_WORLD);
  99.  
  100. printf("P%d Total number of occurrences = %d\n",rank,localCount);
  101.  
  102. if (rank == 0)
  103. {
  104. printf("Total number of occurrences in all 100 files = %d\n",totalCount);
  105. }
  106.  
  107.  
  108. MPI_Finalize();
  109. return 0;
  110. }
Success #stdin #stdout #stderr 0.32s 40324KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Error: unexpected symbol in "int directoryExists"
Execution halted