-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
92 lines (69 loc) · 3.11 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include "Hungarian.h"
#include <iostream>
#include <unistd.h>
#include <sstream>
#include <fstream>
#include <vector>
#include <chrono>
// used this function for memory usage
void process_mem_usage(double& vm_usage, double& resident_set)
{
vm_usage = 0.0;
resident_set = 0.0;
std::ifstream stat_stream("/proc/self/stat",std::ios_base::in);
string pid, comm, state, ppid, pgrp, session, tty_nr;
string tpgid, flags, minflt, cminflt, majflt, cmajflt;
string utime, stime, cutime, cstime, priority, nice;
string O, itrealvalue, starttime;
unsigned long vsize;
long rss;
stat_stream >> pid >> comm >> state >> ppid >> pgrp >> session >> tty_nr>> tpgid >> flags >> minflt >> cminflt >> majflt >> cmajflt>> utime >> stime >> cutime >> cstime >> priority >> nice>> O >> itrealvalue >> starttime >> vsize >> rss;
stat_stream.close();
long page_size_kb = sysconf(_SC_PAGE_SIZE) / 1024; // in case x86-64 is configured to use 2MB pages
vm_usage = vsize / 1024.0;
resident_set = rss * page_size_kb;
}
//this main will be used to test our matrix
int main(void)
{
//"-std=c++11" for this initialization of vector.
vector<vector<double>> costMatrix; // declare vector of vectors
std::string tempstr;
int tempint;
char delimiter;
std::ifstream ifs("5x5mat.txt");
// this is where I read my vector matrix line by line.
//read line by line from a file into a string
while (std::getline(ifs, tempstr)) {
std::istringstream iss(tempstr);
// initialize the stringstream with that string
std::vector<double> tempv;
// extract the numbers from a stringstream
while (iss >> tempint) {
tempv.push_back(tempint); // push it onto our temporary vector
iss >> delimiter; // read the , delimiter
}
//push the vector onto vector of vectors
costMatrix.push_back(tempv);
}
HungarianAlgorithm HungAlgo;
vector<int> assignment;
using namespace std::chrono; //thanks to chrono library for time
auto start = high_resolution_clock::now(); // start the time in front of the function
double cost = HungAlgo.Solve(costMatrix, assignment);
auto stop = high_resolution_clock::now(); // until function's end
cout << "Assignment problem result: " << cost <<endl; // print the optimal result of the assignment problem
double vm, rss;
process_mem_usage(vm, rss); //call the function we got above for memory usage.
cout << "Virtual Memory: " << vm << " , Resident set size:" << rss << endl;
// convert nanoseconds to seconds I print in seconds
double time_taken = chrono::duration_cast<chrono::nanoseconds>(stop - start).count();
time_taken *= 1e-9;
cout << "Time " << fixed << time_taken << " seconds" << endl;
ofstream txt;
txt.open ("outputforcpp.txt"); // transfer the information we want to our output file.
txt << "Assignment problem result: " << cost << endl;
txt << "Virtual Memory: " << vm << " , Resident set size: " << rss << endl;
txt << "Time " << fixed << time_taken << " seconds" << endl;
return 0;
}