Advertisement
sepi0l

hpc_paradfs

Apr 25th, 2024
691
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.23 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <stack>
  4. #include <omp.h>
  5. #include <bits/stdc++.h>
  6.  
  7. using namespace std;
  8.  
  9. void dfs(int start, int* arr, int n, int visited[]) {
  10.  
  11.     #pragma omp parallel for ordered
  12.     for(int i = 0; i < n; i++) {
  13.         #pragma omp ordered
  14.         if( (*(arr + (start*n) + i) == 1) && (!visited[i]) )
  15.         {
  16.             visited[i] = 1;
  17.             cout<<i<<" ";
  18.             dfs(i, (int*)arr, n, visited);
  19.         }
  20.     }
  21. }
  22.  
  23. int main()
  24. {
  25.     // freopen("input.txt","r",stdin);
  26.     // freopen("output.txt","w",stdout);
  27.  
  28.     cout<<"Enter the number of vertices:";
  29.     int n;
  30.     cin>>n;
  31.    
  32.     int arr[n][n] = {0};
  33.  
  34.     cout<<"Enter the number of edges:";
  35.     int edges;
  36.     cin>>edges;
  37.    
  38.  
  39.     for(int j=0; j<edges; j++)
  40.     {
  41.         int a, b;
  42.         cout<<"Enter the two edges:"<<endl;
  43.         cin>>a>>b;
  44.         arr[a][b] = 1;
  45.         arr[b][a] = 1;
  46.     }
  47.  
  48.     int visited[n] = {0};
  49.  
  50.     cout<<"Enter the start vertex: ";
  51.     int start;
  52.     cin>>start;
  53.  
  54.     clock_t strt = clock();
  55.    
  56.     cout<<start<<" ";
  57.     visited[start] = 1;
  58.     dfs(start, (int *)arr, n, visited);
  59.    
  60.     clock_t stop = clock();
  61.  
  62.     cout<<"\nTime required : "<<(double)(stop-strt)<<" ms"<<endl;
  63.  
  64.  
  65.     return 0;
  66. }
  67.  
  68.  
  69. /*
  70.  
  71. "Parallel Execution"
  72. PS D:\C++> g++ -fopenmp parallel_dfs.cpp
  73. PS D:\C++> ./a out
  74. Enter the number of vertices:7
  75. Enter the number of edges:6
  76. Enter the two edges:
  77. 0 1
  78. Enter the two edges:
  79. 0 2
  80. Enter the two edges:
  81. 1 3        
  82. Enter the two edges:
  83. 1 4
  84. Enter the two edges:
  85. 2 5
  86. Enter the two edges:
  87. 2 6
  88. Enter the start vertex: 0
  89. 0 1 3 4 2 5 6
  90. Time required : 9 ms
  91.  
  92. "Serial Execution"
  93. PS D:\C++> g++ parallel_dfs.cpp
  94. PS D:\C++> ./a out
  95. Enter the number of vertices:7
  96. Enter the number of edges:6
  97. Enter the two edges:
  98. 0 1
  99. Enter the two edges:
  100. 0 2
  101. Enter the two edges:
  102. 1 3
  103. Enter the two edges:
  104. 1 4
  105. Enter the two edges:
  106. 2 5
  107. Enter the two edges:
  108. 2 6
  109. Enter the start vertex: 0
  110. 0 1 3 4 2 5 6
  111. Time required : 12 ms
  112.  
  113.  
  114. */
  115.  
  116. /*
  117. This code implements Depth-First Search (DFS) for a graph represented as an adjacency matrix. Let's break it down:
  118.  
  119. 1. **`dfs` Function:**
  120.    - This function performs a Depth-First Search traversal starting from a given vertex.
  121.    - It iterates over the vertices adjacent to the `start` vertex in parallel using OpenMP directives.
  122.    - Each thread checks if an adjacent vertex is reachable from the `start` vertex and if it has been visited before.
  123.    If not visited, it marks it as visited, prints the vertex, and recursively calls `dfs` with it as the starting vertex.
  124.  
  125. 2. **`main` Function:**
  126.    - It initializes the graph as an adjacency matrix `arr`, where `arr[i][j]` is 1 if there is an edge between vertices `i` and `j`.
  127.    - User input is taken for the number of vertices `n`, the number of edges `edges`, and the edges themselves.
  128.    - DFS traversal is initiated from a user-defined starting vertex.
  129.    - The time taken for the DFS traversal is measured using the `clock` function.
  130.  
  131. 3. **OpenMP Parallelization:**
  132.    - OpenMP directives (`#pragma omp parallel for ordered`) are used to parallelize the loop iterations.
  133.    - The `ordered` pragma ensures that the traversal order is maintained while still allowing parallel execution.
  134.  
  135. 4. **Output:**
  136.    - The DFS traversal sequence starting from the specified vertex is printed.
  137.    - The time taken for the DFS traversal is also printed in milliseconds.
  138.  
  139. 5. **Performance Comparison:**
  140.    - The code demonstrates the difference in execution time between parallel and serial DFS traversal.
  141.    - Parallel execution is expected to be faster than serial execution due to concurrent processing of vertices.
  142.  
  143. 6. **Sample Output:**
  144.    - For the given input graph with vertices `{0, 1, 2, 3, 4, 5, 6}` and edges `{(0, 1), (0, 2), (1, 3), (1, 4), (2, 5), (2, 6)}`,
  145.    starting from vertex 0, the DFS traversal sequence is `{0, 1, 3, 4, 2, 5, 6}`.
  146.    - The time required for parallel execution is 9 milliseconds, while for serial execution, it is 12 milliseconds.
  147.  
  148. Overall, this code showcases how Depth-First Search can be implemented for graph traversal using OpenMP parallelization,
  149. potentially improving performance by leveraging multiple threads to process vertices concurrently.
  150. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement