diff --git a/(p,t)_sparsification/main.cpp b/(p,t)_sparsification/main.cpp index b80c57585bcd3a3e33232246da8c86e52abda5e2..6e2c7ef7cef2906bab3659c5e2bf2c665baef2cd 100644 --- a/(p,t)_sparsification/main.cpp +++ b/(p,t)_sparsification/main.cpp @@ -74,37 +74,59 @@ int main(int argc, char *argv[]) { auto start = chrono::steady_clock::now(); string execMode=var["algorithm"].as<string>(); - for (int i=0; i<NUM_TRIALS; i++) { - pthread_create(&threads[i], NULL, handler, NULL); - if (execMode == "Random") { - g2 = compress_graph_basic(g, var["depth"].as<int>(), var["proportions"].as<vector<double>>(), +#include <omp.h> +#include <chrono> +#include <vector> +#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>()); - } else if (execMode == "LP") { - g2 = compress_graph_LP(g, e_s, var["depth"].as<int>(), var["proportions"].as<vector<double>>(), + } else if (var["algorithm"].as<std::string>() == "LP") { + g2 = compress_graph_LP(g, e_s, var["depth"].as<int>(), var["proportions"].as<std::vector<double>>(), 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>(), - var["proportions"].as<vector<double>>()); - }else if (execMode == "Greedy") { - g2 = compress_graph_greedy(g, var["depth"].as<int>(), var["proportions"].as<vector<double>>(), + var["proportions"].as<std::vector<double>>()); + } else if (var["algorithm"].as<std::string>() == "Greedy") { + g2 = compress_graph_greedy(g, var["depth"].as<int>(), var["proportions"].as<std::vector<double>>(), var["directed"].as<bool>()); } - auto finish = 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(); + auto finish = std::chrono::steady_clock::now(); + + // 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(); -// compression_rate = ((double) (edges_original.size() - edges_compressed.size()) / -// edges_original.size()); -// c.push_back(compression_rate); -// s.push_back(edges_compressed); + // Each thread should update its own local variables or use reduction clauses for summation + // 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); // cout << "compression rate: " << compression_rate << endl; cout <<endl << "compression time: " << elapsed_time/NUM_TRIALS << endl;