Skip to content

Commit

Permalink
Added some unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vihanshah98 committed Jul 11, 2024
1 parent ec79c53 commit 3211531
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 24 deletions.
18 changes: 9 additions & 9 deletions src/KEdgeConnect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,33 +65,33 @@ void KEdgeConnect::query() {

std::vector<std::pair<node_id_t, std::vector<node_id_t>>> temp_forest;
for(unsigned int i=0;i<num_forest-1;i++) {
std::cout << "SPANNING FOREST " << (i+1) << std::endl;
//std::cout << "SPANNING FOREST " << (i+1) << std::endl;
// getting the spanning forest from the i-th cc-alg
temp_forest = cc_alg[i]->calc_spanning_forest();
forests_collection.push_back(temp_forest);

for (unsigned int j = 0; j < temp_forest.size(); j++) {
std::cout << temp_forest[j].first << ":";
//std::cout << temp_forest[j].first << ":";
for (auto dst: temp_forest[j].second) {
std::cout << " " << dst;
//std::cout << " " << dst;
temp_edge.edge.src = temp_forest[j].first;
temp_edge.edge.dst = dst;
for (int l=i+1;l<num_forest;l++){
cc_alg[l]->update(temp_edge);
}
}
std::cout << std::endl;
//std::cout << std::endl;
}
}

std::cout << "THE LAST SPANNING FOREST" << std::endl;
//std::cout << "THE LAST SPANNING FOREST" << std::endl;
temp_forest = cc_alg[num_forest-1]->calc_spanning_forest();
forests_collection.push_back(temp_forest);
for (unsigned int j = 0; j < temp_forest.size(); j++) {
std::cout << temp_forest[j].first << ":";
//std::cout << temp_forest[j].first << ":";
for (auto dst: temp_forest[j].second) {
std::cout << " " << dst;
//std::cout << " " << dst;
}
std::cout << std::endl;
//std::cout << std::endl;
}
}
}
74 changes: 59 additions & 15 deletions tools/process_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
// TODO: make num_edge_connect an input argument;
// TODO: Daniel's concern: right now, the deletion of the edge to the stream makes the query only possible to be supported once -- fix this later

int ctr_x=0;
int ctr_y=0;

unsigned long long ctr_z=0;
unsigned long long ctr_o=0;
unsigned long long ctr_t=0;


static bool shutdown = false;

Expand All @@ -22,7 +29,7 @@ class MinCutSimple {
const node_id_t num_nodes;
unsigned int num_forest; // this value is k in the k-edge connectivity
unsigned int num_subgraphs;
const int my_prime = 100003; // Prime number for polynomial hashing
const int my_prime = 44497; // Prime number for polynomial hashing
std::vector<std::vector<int>> hash_coefficients;
std::vector<std::unique_ptr<KEdgeConnect>> k_edge_algs;
std::vector<unsigned int> mincut_values;
Expand All @@ -31,22 +38,28 @@ class MinCutSimple {
explicit MinCutSimple(node_id_t num_nodes, const std::vector<std::vector<CCAlgConfiguration>> &config_vec):
num_nodes(num_nodes) {
num_subgraphs = (unsigned int)(2*std::ceil(std::log2(num_nodes)));
std::cout<<"Number of subgraphs(constructor):"<<num_subgraphs<<std::endl;
// TODO: make the approximation factor tunable later
num_forest = 4*num_subgraphs;
num_forest = 2*num_subgraphs;
std::cout<<"Number of forests(k)(constructor):"<<num_forest<<std::endl;
for(unsigned int i=0;i<num_subgraphs;i++){
k_edge_algs.push_back(std::make_unique<KEdgeConnect>(num_nodes, num_forest, config_vec[i]));
}
// Initialize coefficients randomly
//std::cout<<"hash coeffs (constructor):"<<std::endl;
std::random_device rd_ind;
std::mt19937 gen_ind(rd_ind());
std::uniform_int_distribution<int> dist_coeff(1, my_prime - 1); // random numbers between 1 and p-1
for (int i =0; i<num_subgraphs; i++) {
std::vector<int> this_subgraph_coeff;
for (int j = 0; j < num_subgraphs; j++) {
this_subgraph_coeff.push_back(dist_coeff(gen_ind));
//std::cout<<this_subgraph_coeff[j]<<" ";
}
hash_coefficients.push_back(this_subgraph_coeff);
std::cout<<std::endl;
}
std::cout<<"end of constructor"<<std::endl;
}

void allocate_worker_memory(size_t num_workers){
Expand Down Expand Up @@ -78,14 +91,24 @@ class MinCutSimple {

// Function to generate k-wise independent hash
int k_wise_hash(const std::vector<int>& coefficients, unsigned int src_vertex, unsigned int dst_vertex) {
int hash_val = 0;
unsigned int hash_val = 0;
if (src_vertex>dst_vertex){
std::swap(src_vertex, dst_vertex);
}
unsigned int edge_id = src_vertex*num_nodes + dst_vertex;
for (int i = 0; i < coefficients.size(); ++i) {
hash_val = (hash_val + coefficients[i] * power(edge_id, i, my_prime)) % my_prime;
}
//std::cout<<"Hash val inside hash func: "<<(hash_val % 2)<<std::endl;
if(hash_val % 2==0)
{
ctr_x++;
}
else
{
ctr_y++;
}

return (hash_val % 2);
}

Expand Down Expand Up @@ -139,15 +162,30 @@ class MinCutSimple {
else{
position = -1;
}

//std::cout<<"Vertex: "<<src_vertex<<std::endl;
for(unsigned int i=0;i<num_subgraphs;i++) {
if (position>=0) {
k_edge_algs[i]->apply_update_batch(thr_id, src_vertex, input_dst_vertices);
//std::cout<<"Nbhd size in round "<<i<<" is: "<<input_dst_vertices.size()<<std::endl;
if(i==0)
{
ctr_z = ctr_z + input_dst_vertices.size();
}
else if(i==1)
{
ctr_o = ctr_o + input_dst_vertices.size();
}
else if(i==2)
{
ctr_t = ctr_t + input_dst_vertices.size();
}
// The following while loop: keep the position variable to be always aligned with the last vertex
// that has not been deleted yet
// If the vertex-corresponding end_index is at most the iteration number, it means it has already been
// accounted for in this iteration, and should not be accounted of at the next iteration; so we should
// remove
while (dst_end_index[position].second <= i && position>=0) {
while (dst_end_index[position].second <= i+1 && position>=0) {
input_dst_vertices.pop_back();
position--;
}
Expand All @@ -172,6 +210,7 @@ class MinCutSimple {
std::ofstream metis_file(file_name);

std::cout << "Writing METIS file...\n";
std::cout<<"no of vertices and edges "<< num_nodes << " " << num_edge << std::endl;

// could be a hidden bug later -- at the moment, num_nodes is taken from the class
metis_file << num_nodes << " " << num_edge << " 0" << "\n";
Expand All @@ -198,20 +237,21 @@ class MinCutSimple {
continue;
}
new_count++;
}}
std::cout<<"new count: "<<new_count<<std::endl;
std::cout<<"num edges:" <<num_edge<<std::endl;
}
}
std::cout<<"no of edges (going through the nodelist): "<<new_count<<std::endl;
}

void query(){
std::cout<<"We made it to the query function!"<<std::endl;
std::cout<<std::endl<<"We made it to the query function!"<<std::endl;
for(unsigned int i=0;i<num_subgraphs;i++) {
std::map<size_t, std::vector<size_t>> nodes_list;
std::vector<std::pair<node_id_t, std::vector<node_id_t>>> current_forest;
size_t subgraph_num_edge = 0;
// easy version: check from the i=0 to i=log n without using an additional layer of binary search
k_edge_algs[i]->query(); // This creates the k forests
unsigned int k = k_edge_algs[i]->forests_collection.size();
std::cout<<"query func, k= "<<k<<std::endl;
for (unsigned int j=0;j<k;j++) {
current_forest = k_edge_algs[i]->forests_collection[j];
for(auto v_neighbors_pair: current_forest){
Expand All @@ -226,9 +266,6 @@ class MinCutSimple {
// run the min-cut algorithm -- stolen from gpu min-cut
std::string file_name = "temp-graph-min-cut.metis";
std::string output_name = "mincut.txt";
// ************** test with a dummy cut value first ****************
//unsigned int cut_value = 5;
//mincut_values.push_back(cut_value);
std::string command = "./mincut_parallel " + file_name + " exact >" + output_name; // Run VieCut and store the output
std::system(command.data());

Expand Down Expand Up @@ -413,7 +450,9 @@ int main(int argc, char **argv) {
std::cout << std::endl;

num_subgraphs = (unsigned int)(2*std::ceil(std::log2(num_nodes)));
num_edge_connect = 10*num_subgraphs;
num_edge_connect = 2*num_subgraphs;
std::cout<<"Number of subgraphs:"<<num_subgraphs<<std::endl;
std::cout<<"Edges Connectivity Param (k):"<<num_edge_connect<<std::endl;

auto driver_config = DriverConfiguration().gutter_sys(CACHETREE).worker_threads(num_threads);
std::vector<std::vector<CCAlgConfiguration>> config_vec;
Expand All @@ -436,12 +475,9 @@ int main(int argc, char **argv) {

driver.process_stream_until(END_OF_STREAM);

std::cout<<"************* Checkpoint 5 *******************"<<std::endl;
auto cc_start = std::chrono::steady_clock::now();
std::cout<<"************* Checkpoint 6 *******************"<<std::endl;
driver.prep_query();

std::cout<<"************* Checkpoint 7 *******************"<<std::endl;
min_cut_alg.query();


Expand Down Expand Up @@ -519,4 +555,12 @@ int main(int argc, char **argv) {
// std::cout << "Number of connected Component in : " << i+1 << " is " << CC_nums[i] << std::endl;
// }
std::cout << "Maximum Memory Usage(MiB): " << get_max_mem_used() << std::endl;


std::cout<<"No of times hash func returns 0 (x)="<<ctr_x<<std::endl;
std::cout<<"No of times hash func returns 1 (y)="<<ctr_y<<std::endl;
std::cout<<"Counter to check numbers in sampling using concentration"<<std::endl;
std::cout<<"count of 0: "<<ctr_z<<std::endl;
std::cout<<"count of 1: "<<ctr_o<<std::endl;
std::cout<<"count of 2: "<<ctr_t<<std::endl;
}

0 comments on commit 3211531

Please sign in to comment.