Skip to content

Commit c573ee8

Browse files
committed
Add files
1 parent fc0bdbe commit c573ee8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+50696
-0
lines changed

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# build folders
2+
bin/
3+
build/
4+
cython/build/
5+
16
# Prerequisites
27
*.d
38

Workbook.xlsx

42.8 KB
Binary file not shown.

bench.py

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
2+
import json
3+
import os
4+
import subprocess
5+
6+
7+
RESULTS_FILE = "results.txt"
8+
9+
RUN_SCRIPTS = {
10+
"cpp": "./cpp/run.sh",
11+
"python": "./python/run.sh",
12+
"python3": "./python/run_python3.sh",
13+
"kotlin": "./kotlin/run.sh",
14+
"kotlin_jit": "./kotlin/run_jit.sh",
15+
"kotlin_jit_5": "./kotlin/run_jit_5.sh",
16+
"cython_bfs": "./cython/run.sh",
17+
"cython_full": "./cython/run_full.sh",
18+
}
19+
20+
21+
def extract_tags(lines):
22+
result = {}
23+
for line in lines.split('\n'):
24+
if "=" in line:
25+
k, v = line.split("=")
26+
result[k] = v
27+
return result
28+
29+
30+
def run_benchmark(name, map_name):
31+
path = 'txt-maps/{}'.format(map_name)
32+
script = RUN_SCRIPTS[name]
33+
stdout = subprocess.check_output([script, path])
34+
tags = extract_tags(stdout)
35+
tags['map'] = map_name
36+
tags['name'] = name
37+
print(tags)
38+
return tags
39+
40+
41+
def write_to_file(data):
42+
f = open(RESULTS_FILE, 'a')
43+
f.write(json.dumps(data) + '\n')
44+
f.close()
45+
46+
47+
def get_map_tags(map_name):
48+
f = open('maps/{}'.format(map_name.replace(".txt", ".json")))
49+
data = json.load(f)
50+
return {
51+
'nodes': len(data['sites']),
52+
'edges': len(data['rivers']),
53+
}
54+
55+
56+
def all_benchmarks():
57+
# clean the file
58+
f = open(RESULTS_FILE, 'w')
59+
f.close()
60+
61+
files = os.listdir('txt-maps/')
62+
# files = [
63+
# 'edinburgh-10000.txt',
64+
# 'icfp-coauthors-pj.txt',
65+
# 'vancouver.txt',
66+
# 'oxford-10000.txt',
67+
# ]
68+
for f in reversed(files):
69+
print('')
70+
map_tags = get_map_tags(f)
71+
print('{} -> {}'.format(f, map_tags))
72+
for name in sorted(RUN_SCRIPTS.keys()):
73+
tags = run_benchmark(name, f)
74+
tags.update(map_tags)
75+
write_to_file(tags)
76+
77+
78+
all_benchmarks()
79+
# run_benchmark('kotlin_jit', 'gothenburg-sparse.txt')

compile_all.sh

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/bash -x
2+
3+
./cpp/compile.sh
4+
./kotlin/compile.sh
5+
./cython/compile.sh

convert.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
import json
3+
import os
4+
5+
6+
def proces_file(file_name):
7+
f = open('maps/{}'.format(file_name))
8+
g = open('txt-maps/{}'.format(file_name.replace(".json", ".txt")), 'wt')
9+
10+
data = json.load(f)
11+
12+
max_city = 0
13+
for city in data['sites']:
14+
max_city = max(max_city, city['id'])
15+
16+
# g.write('{}\n'.format(len(data['sites'])))
17+
g.write('{}\n'.format(max_city + 1))
18+
19+
mines = data['mines']
20+
g.write('{}\n'.format(len(mines)))
21+
g.write('{}\n'.format(' '.join(map(str, mines))))
22+
g.write('{}\n'.format(len(data['rivers'])))
23+
for river in data['rivers']:
24+
s = river['source']
25+
t = river['target']
26+
g.write('{} {}\n'.format(s, t))
27+
g.close()
28+
29+
30+
files = os.listdir('maps/')
31+
for f in files:
32+
proces_file(f)
33+
# break

copy-results.txt

+144
Large diffs are not rendered by default.

cpp/compile.sh

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/bash
2+
ROOT=$(dirname ${BASH_SOURCE[0]})
3+
4+
mkdir -p ${ROOT}/../build
5+
g++ -std=c++14 -O3 ${ROOT}/solver.cpp -o ${ROOT}/../build/cpp_solver.out

cpp/run.sh

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
3+
./build/cpp_solver.out $1

cpp/solver.cpp

+135
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
#include <stdio.h>
2+
#include <iostream>
3+
#include <vector>
4+
#include <string>
5+
#include <math.h>
6+
#include <algorithm>
7+
#include <bitset>
8+
#include <set>
9+
#include <sstream>
10+
#include <stdlib.h>
11+
#include <map>
12+
#include <queue>
13+
#include <assert.h>
14+
#include <deque>
15+
#include <string.h>
16+
#include <unordered_map>
17+
18+
#include <ctime>
19+
20+
using namespace std;
21+
22+
#define sz(x) ((int)x.size())
23+
#define all(x) (x).begin(), (x).end()
24+
#define pb(x) push_back(x)
25+
#define mp(x, y) make_pair(x, y)
26+
27+
typedef long long int64;
28+
29+
typedef vector <int> vi;
30+
typedef vector <vi> vvi;
31+
32+
33+
typedef vector <string> vs;
34+
35+
36+
constexpr int NOT_PROCESSED = -1;
37+
38+
struct Solver {
39+
vvi graph;
40+
vs mines;
41+
int num_cities;
42+
43+
void init() {
44+
cin >> num_cities;
45+
graph.resize(num_cities);
46+
47+
int num_mines;
48+
cin >> num_mines;
49+
for (int i = 0; i < num_mines; ++i) {
50+
string m;
51+
cin >> m;
52+
mines.push_back(m);
53+
}
54+
55+
int n_edges;
56+
cin >> n_edges;
57+
for (int i = 0; i < n_edges; ++i) {
58+
int s;
59+
int t;
60+
cin >> s >> t;
61+
62+
graph[s].push_back(t);
63+
graph[t].push_back(s);
64+
}
65+
}
66+
67+
void compute_hash() {
68+
clock_t tbegin = clock();
69+
70+
vvi distances(num_cities);
71+
for (int i = 0; i < num_cities; ++i) {
72+
vi scores;
73+
run_bfs(i, scores);
74+
distances[i] = std::move(scores);
75+
}
76+
77+
int total_score = 0;
78+
for (int i = 0; i < num_cities; ++i) {
79+
int current_score = 0;
80+
for (const auto& d : distances[i]) {
81+
if (d == NOT_PROCESSED) {
82+
continue;
83+
}
84+
int d2 = d * d;
85+
current_score ^= d2;
86+
}
87+
total_score += current_score;
88+
}
89+
cout << "graph_hash=" << total_score << endl;
90+
91+
clock_t tend = clock();
92+
double elapsed_msecs = double(tend - tbegin) / CLOCKS_PER_SEC * 1000;
93+
cout << "time=" << elapsed_msecs << endl;
94+
// cout << "name=cpp" << endl;
95+
96+
}
97+
98+
void run_bfs(int start, vi& scores) {
99+
scores.assign(num_cities, NOT_PROCESSED);
100+
scores[start] = 0;
101+
102+
deque<int> queue;
103+
queue.push_back(start);
104+
105+
while (!queue.empty()) {
106+
int now = queue.front();
107+
queue.pop_front();
108+
int value = scores[now];
109+
110+
for (const auto& next : graph[now]) {
111+
if (scores[next] == NOT_PROCESSED) {
112+
scores[next] = value + 1;
113+
queue.push_back(next);
114+
}
115+
}
116+
}
117+
}
118+
};
119+
120+
121+
122+
int main (int argc, char *argv[]) {
123+
if (argc < 2) {
124+
cout << "map name not specified" << endl;
125+
return 1;
126+
}
127+
freopen(argv[1], "rt", stdin);
128+
//freopen("", "wt", stdout);
129+
//std::ios::sync_with_stdio(false);
130+
Solver s;
131+
s.init();
132+
s.compute_hash();
133+
134+
return 0;
135+
}

cython/compile.sh

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/bash
2+
ROOT=$(dirname ${BASH_SOURCE[0]})
3+
pushd ${ROOT}
4+
python setup.py build_ext --inplace
5+
popd

cython/cpp_solver.cpp

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#include "cpp_solver.h"
2+
3+
const int NOT_PROCESSED = -1;
4+
5+
6+
CppSolver::CppSolver(vvi* _graph)
7+
: graph(_graph)
8+
{
9+
10+
}
11+
12+
void CppSolver::run_bfs(int start, vi* _scores) {
13+
vi& scores = *_scores;
14+
auto num_cities = graph->size();
15+
scores.assign(num_cities, NOT_PROCESSED);
16+
scores[start] = 0;
17+
18+
deque<int> queue;
19+
queue.push_back(start);
20+
21+
while (!queue.empty()) {
22+
int now = queue.front();
23+
queue.pop_front();
24+
int value = scores[now];
25+
26+
for (const auto& next : (*graph)[now]) {
27+
if (scores[next] == NOT_PROCESSED) {
28+
scores[next] = value + 1;
29+
queue.push_back(next);
30+
}
31+
}
32+
}
33+
}
34+
35+
void CppSolver::compute_hash() {
36+
clock_t tbegin = clock();
37+
38+
auto num_cities = graph->size();
39+
vvi distances(num_cities);
40+
for (int i = 0; i < num_cities; ++i) {
41+
vi scores;
42+
run_bfs(i, &scores);
43+
distances[i] = std::move(scores);
44+
}
45+
46+
int total_score = 0;
47+
for (int i = 0; i < num_cities; ++i) {
48+
int current_score = 0;
49+
for (const auto& d : distances[i]) {
50+
if (d == NOT_PROCESSED) {
51+
continue;
52+
}
53+
int d2 = d * d;
54+
current_score ^= d2;
55+
}
56+
total_score += current_score;
57+
}
58+
cout << "graph_hash=" << total_score << endl;
59+
60+
clock_t tend = clock();
61+
double elapsed_msecs = double(tend - tbegin) / CLOCKS_PER_SEC * 1000;
62+
cout << "time=" << elapsed_msecs << endl;
63+
}

cython/cpp_solver.h

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#pragma once
2+
3+
#include <stdio.h>
4+
#include <iostream>
5+
#include <vector>
6+
#include <string>
7+
#include <math.h>
8+
#include <algorithm>
9+
#include <bitset>
10+
#include <set>
11+
#include <sstream>
12+
#include <stdlib.h>
13+
#include <map>
14+
#include <queue>
15+
#include <assert.h>
16+
#include <deque>
17+
#include <string.h>
18+
#include <unordered_map>
19+
20+
#include <ctime>
21+
22+
using namespace std;
23+
24+
typedef vector <int> vi;
25+
typedef vector <vi> vvi;
26+
typedef vector <string> vs;
27+
28+
29+
struct CppSolver {
30+
CppSolver(vvi* _graph);
31+
32+
void run_bfs(int start, vi* scores);
33+
void compute_hash();
34+
private:
35+
vvi* graph;
36+
};

0 commit comments

Comments
 (0)