Skip to content
Snippets Groups Projects
Commit a730de6b authored by gianlucarossi15's avatar gianlucarossi15
Browse files

openMP implementation

parent 6f06be83
No related branches found
No related tags found
1 merge request!1New code version
...@@ -74,37 +74,59 @@ int main(int argc, char *argv[]) { ...@@ -74,37 +74,59 @@ int main(int argc, char *argv[]) {
auto start = chrono::steady_clock::now(); auto start = chrono::steady_clock::now();
string execMode=var["algorithm"].as<string>(); string execMode=var["algorithm"].as<string>();
for (int i=0; i<NUM_TRIALS; i++) { #include <omp.h>
pthread_create(&threads[i], NULL, handler, NULL); #include <chrono>
if (execMode == "Random") { #include <vector>
g2 = compress_graph_basic(g, var["depth"].as<int>(), var["proportions"].as<vector<double>>(), #include <string>
// Assuming NUM_TRIALS and other variables are defined somewhere
#pragma omp parallel for
for (int i = 0; i < NUM_TRIALS; i++) {
// Inside the loop, each thread will execute one iteration
// Each thread will create a separate graph instance, so make sure g2 is declared inside the loop
Graph g2;
// Other variables like edges_original and edges_compressed need to be declared thread-safe or local to each thread
auto start = std::chrono::steady_clock::now();
if (var["algorithm"].as<std::string>() == "Random") {
g2 = compress_graph_basic(g, var["depth"].as<int>(), var["proportions"].as<std::vector<double>>(),
var["directed"].as<bool>()); var["directed"].as<bool>());
} else if (execMode == "LP") { } else if (var["algorithm"].as<std::string>() == "LP") {
g2 = compress_graph_LP(g, e_s, var["depth"].as<int>(), var["proportions"].as<vector<double>>(), g2 = compress_graph_LP(g, e_s, var["depth"].as<int>(), var["proportions"].as<std::vector<double>>(),
var["directed"].as<bool>()); var["directed"].as<bool>());
} else if (execMode == "SA") { } else if (var["algorithm"].as<std::string>() == "SA") {
g2 = Simulated_annealing(1000, 10, 0.99, g, var["directed"].as<bool>(), var["depth"].as<int>(), g2 = Simulated_annealing(1000, 10, 0.99, g, var["directed"].as<bool>(), var["depth"].as<int>(),
var["proportions"].as<vector<double>>()); var["proportions"].as<std::vector<double>>());
}else if (execMode == "Greedy") { } else if (var["algorithm"].as<std::string>() == "Greedy") {
g2 = compress_graph_greedy(g, var["depth"].as<int>(), var["proportions"].as<vector<double>>(), g2 = compress_graph_greedy(g, var["depth"].as<int>(), var["proportions"].as<std::vector<double>>(),
var["directed"].as<bool>()); var["directed"].as<bool>());
} }
auto finish = chrono::steady_clock::now(); auto finish = std::chrono::steady_clock::now();
vector<edge> edges_original = get_edges(g, var["directed"].as<bool>());
edges_compressed += get_edges(g2, var["directed"].as<bool>()).size(); // It's important to calculate and update edges_compressed and elapsed_time inside the loop in a thread-safe manner
elapsed_time += chrono::duration_cast<chrono::duration<double>>(finish - start).count(); // Each thread should update its own local variables or use reduction clauses for summation
// compression_rate = ((double) (edges_original.size() - edges_compressed.size()) /
// edges_original.size());
// c.push_back(compression_rate);
// s.push_back(edges_compressed);
// Update edges_compressed and elapsed_time in a thread-safe manner
#pragma omp critical
{
vector<edge> edges_original = get_edges(g, var["directed"].as<bool>());
edges_compressed += get_edges(g2, var["directed"].as<bool>()).size();
elapsed_time += std::chrono::duration_cast<std::chrono::duration<double>>(finish - start).count();
}
// You may also need to handle compression_rate, c, and s variables inside the loop, ensuring thread safety.
} }
//graph_to_file(var, elapsed_time, compression_rate, edges_compressed); //graph_to_file(var, elapsed_time, compression_rate, edges_compressed);
// cout << "compression rate: " << compression_rate << endl; // cout << "compression rate: " << compression_rate << endl;
cout <<endl << "compression time: " << elapsed_time/NUM_TRIALS << endl; cout <<endl << "compression time: " << elapsed_time/NUM_TRIALS << endl;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment