Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DRAFT] GR1: Additional Vectorization Pass supporting more fusion potentials. #870

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions _chain.daph
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
X = fill(1.0, 30000, 30000);
startProfiling();
v0 = t(X);
v1 = t(v0);
v2 = t(v1);
v3 = t(v2);
v4 = t(v3);
v5 = t(v4);
v6 = t(v5);
v7 = t(v6);
v8 = t(v7);
v9 = t(v8);
stopProfiling();
print(v9[0,0]);

133 changes: 133 additions & 0 deletions run_horz.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import sys
import numpy as np
import json
import datetime
import argparse
from tabulate import tabulate
import pandas as pd
import shared as sh

#------------------------------------------------------------------------------
# GLOBAL
#------------------------------------------------------------------------------

GENERATE_FUNCS = {
"ADD": lambda i, arg: [f"v{i} = {arg} + {i * 0.1};"],
"ADD_SUM": lambda i, arg: [f"i{i} = {arg} + {i * 0.1};", f"v{i} = sum(i{i});"]
}

GENERATE_PRINT_FUNCS = {
"ADD": lambda i: [f"print(v{i}[0,0]);"],
"ADD_SUM": lambda i: [f"print(v{i});"]
}

BASE_CWD = "./"
GLOBAL_ARGS = []
BASE_COMMAND = lambda th, bs, no_hf: [
"./run-daphne.sh",
"--timing",
"--vec",
"--vec-type=GREEDY_1",
f"--num-threads={th}",
f"--batchSize={bs}",
] + (["--no-hf"] if no_hf else []) + GLOBAL_ARGS + ["./_horz.daph"]

#------------------------------------------------------------------------------
# HELPER
#------------------------------------------------------------------------------

def generate_script(num_ops, tool, func, rows, cols):

script = []

script.append(f"X = fill(1.0, {rows}, {cols});")
script.append(sh.TOOLS[tool]["START_OP"])

for j in range(0, num_ops):
script += GENERATE_FUNCS[func](j, "X")
script.append(sh.TOOLS[tool]["STOP_OP"])

for j in range(0, num_ops):
script += GENERATE_PRINT_FUNCS[func](j)

script.append(sh.TOOLS[tool]["END_OP"])

return script

#------------------------------------------------------------------------------
# ARGS
#------------------------------------------------------------------------------

parser = argparse.ArgumentParser(description="Arguments")
parser.add_argument("--tool", type=str, choices=sh.TOOLS.keys(), help="", required=True)
parser.add_argument("--script", type=str, choices=GENERATE_FUNCS.keys(), help="", required=True)
parser.add_argument("--rows", type=int, default=10000, help="rows")
parser.add_argument("--cols", type=int, default=10000, help="rows")
parser.add_argument("--samples", type=int, default=3, help="")
parser.add_argument("--num-ops", type=int, default=12, help="")
parser.add_argument("--threads", type=int, default=1, help="")
parser.add_argument("--batchSize", type=int, default=0, help="")
parser.add_argument("--verbose-output", action="store_true")
parser.add_argument("--explain", action="store_true")

#------------------------------------------------------------------------------
# MAIN
#------------------------------------------------------------------------------

if __name__ == "__main__":

args = parser.parse_args()
exp_start = datetime.datetime.now().strftime("%Y-%m-%d_%H:%M:%S")

if args.explain:
GLOBAL_ARGS += ["--explain=vectorized"]

output = []
for no_hf in [False, True]:

cmd = BASE_COMMAND(args.threads, args.batchSize, no_hf)

command_output = {}
for ops in range(args.num_ops, args.num_ops+1):

script = generate_script(ops, args.tool, args.script, args.rows, args.cols)
with open("_horz.daph", "w") as f:
for line in script:
f.write(line + '\n')

timings = sh.runner(args, cmd, BASE_CWD)

#command_output[ops] = timings
command_output = timings

print()

output.append({
"cmd": cmd,
"timings": command_output,

})

with open(exp_start + "-horz_timings.json", "w+") as f:
_output = {
"settings": {
"num-ops": args.num_ops,
"rows": args.rows,
"cols": args.cols,
"type": args.script,
"tool": args.tool,
"threads": args.threads,
"samples": args.samples,
"batchSize": args.batchSize
},
"execs": output
}
json.dump(_output, f, indent=4)
f.close()

for i in output:
print(" ".join(i["cmd"]))
df = pd.json_normalize(i["timings"], sep=".")
tools_cols = [col for col in df.columns if col.startswith("tool")]
df[tools_cols] = df[tools_cols].astype(int)
print(tabulate(df.describe(), headers="keys", tablefmt="psql", showindex=True))
110 changes: 110 additions & 0 deletions shared.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import os
import subprocess
import json
import pandas as pd
from tabulate import tabulate

#------------------------------------------------------------------------------
# RUN COMMAND
#------------------------------------------------------------------------------

def run_command(cmd, cwd, env):

process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=cwd, env={**env, **os.environ})
stdout, stderr = process.communicate()

return stdout.decode(), stderr.decode()

def runner(args, cmd, cwd):

tool_env = TOOLS[args.tool]["ENV"]
env_str = " ".join(f"{k}=\"{v}\"" for k, v in tool_env.items())
cmd_str = " ".join(cmd)
print(f"Run: {env_str} {cmd_str} {cwd}")

timings = []
for i in range(0, args.samples):

stdout, stderr = run_command(cmd, cwd, tool_env)

if args.verbose_output:
print(stdout)
print(stderr)

timing = json.loads(stderr.split("\n")[-2])
timing["tool"] = TOOLS[args.tool]["GET_INFO"](stdout)

df = pd.json_normalize(timing, sep=".")
print(tabulate(df, headers="keys", tablefmt="psql", showindex=False))
timings.append(timing)

return timings

#------------------------------------------------------------------------------
# TOOLS
#------------------------------------------------------------------------------

def extract_f1xm3(stdout):
lines = stdout.split('\n')

for line in reversed(lines):
if "F1XM3" in line:
number = line.split("F1XM3:")[1]
return int(number)
return None

def extract_papi(stdout):
lines = stdout.split('\n')

offset = 0
for i, line in enumerate(lines):
if line.startswith("PAPI-HL Output:"):
offset = i
break
t = "".join(lines[offset+1:])
j = json.loads(t)
out = j["threads"]["0"]["regions"]["0"]
del out["name"]
del out["parent_region_id"]
return out

TOOLS = {
"PAPI_STD": {
"ENV": {
"PAPI_EVENTS": "perf::CYCLES,perf::INSTRUCTIONS,perf::CACHE-REFERENCES,perf::CACHE-MISSES,perf::BRANCHES,perf::BRANCH-MISSES",
"PAPI_REPORT": "1"
},
"START_OP": "startProfiling();",
"STOP_OP": "stopProfiling();",
"END_OP": "",
"GET_INFO": extract_papi
},
"PAPI_L1": {
"ENV": {
"PAPI_EVENTS": "perf::L1-dcache-load-misses,perf::L1-dcache-loads,perf::L1-dcache-prefetches,perf::L1-icache-load-misses,perf::L1-icache-loads",
"PAPI_REPORT": "1",
},
"START_OP": "startProfiling();",
"STOP_OP": "stopProfiling();",
"END_OP": "",
"GET_INFO": extract_papi
},
"PAPI_MPLX": {
"ENV": {
"PAPI_EVENTS": "perf::CYCLES,perf::INSTRUCTIONS,perf::CACHE-REFERENCES,perf::CACHE-MISSES,perf::BRANCHES,perf::BRANCH-MISSES,perf::L1-dcache-load-misses,perf::L1-dcache-loads,perf::L1-dcache-prefetches,perf::L1-icache-load-misses,perf::L1-icache-loads",
"PAPI_REPORT": "1",
"PAPI_MULTIPLEX": "1",
},
"START_OP": "startProfiling();",
"STOP_OP": "stopProfiling();",
"END_OP": "",
"GET_INFO": extract_papi
},
"NOW": {
"ENV": {},
"START_OP": "start = now();",
"STOP_OP": "end = now();",
"END_OP": "print(\"F1XM3:\"+ (end - start));",
"GET_INFO": extract_f1xm3
}
}
11 changes: 11 additions & 0 deletions sketch/bench/abs_t_exp.daph
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
X = rand($r, $c, 0.0, 1.0, 1, 12345);

<start>
i1 = abs(X);
i2 = t(i1);
i3 = exp(i2);
i4 = i3 + 2;
<stop>

print(i4[0,0]);
<end>
29 changes: 29 additions & 0 deletions sketch/bench/kmeans.daphne
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// K-means clustering.

// Arguments:
// - r ... number of records
// - c ... number of centroids
// - f ... number of features
// - i ... number of iterations

// Data generation.
X = rand($r, $f, 0.0, 1.0, 1, 12345);
C = rand($c, $f, 0.0, 1.0, 1, 67890);

// K-means clustering (decisive part).
<start>
for(i in 1:$i) {
D = (X @ t(C)) * -2 + t(sum(C ^ 2, 0));
minD = aggMin(D, 0);
P = D <= minD;
P = P / sum(P, 0);
P_denom = sum(P, 1);
C = (t(P) @ X) / t(P_denom);
}
<stop>

// Result output.
print(C[0,0]);
print(C[1,1]);
print(C[2,2]);
<end>
12 changes: 12 additions & 0 deletions sketch/bench/outerAdd_exp.daph
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
X = rand($r, 1, 0.0, 1.0, 1, 12345);
Y = rand(1, $c, 0.0, 1.0, 1, 67890);

<start>
i1 = outerAdd(X,Y);
i2 = exp(i1);
i3 = i2 + 2;
<stop>


print(i3[0,0]);
<end>
19 changes: 19 additions & 0 deletions sketch/bench/outerAdd_sumCol_exp.daph
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

//r=40000, c=40000
//NoVec: Killed
//GR1: Killed
//GR2: ~33s
X = rand($r, 1, 0.0, 1.0, 1, 12345);
Y = rand(1, $c, 0.0, 1.0, 1, 67890);

<start>
i1 = outerAdd(X,Y);
i2 = sum(i1, 1);
i3 = outerAdd(X,i2);
i4 = sqrt(i3);
i5 = i4 + 2;
<stop>


print(i5[0,0]);
<end>
12 changes: 12 additions & 0 deletions sketch/bench/outerAdd_t.daph
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
X = rand($r, 1, 0.0, 1.0, 1, 12345);
Y = rand(1, $c, 0.0, 1.0, 1, 67890);

<start>
i1 = outerAdd(X,Y);
i2 = t(i1);
i3 = i2 + 2;
<stop>


print(i3[0,0]);
<end>
18 changes: 18 additions & 0 deletions sketch/bench/outerAdd_t_exp.daph
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

//r=40000, c=40000
//NoVec: Killed
//GR1: Killed
//GR2: ~33s
X = rand($r, 1, 0.0, 1.0, 1, 12345);
Y = rand(1, $c, 0.0, 1.0, 1, 67890);

<start>
i1 = outerAdd(X,Y);
i2 = t(i1);
i3 = exp(i2);
i4 = i3 + 2;
<stop>


print(i4[0,0]);
<end>
9 changes: 9 additions & 0 deletions sketch/bench/sqrt_sum.daph
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
X = rand($r, $c, 0.0, 1.0, 1, 12345);

<start>
i1 = sqrt(X);
i2 = sum(i1);
<stop>

print(i2);
<end>
9 changes: 9 additions & 0 deletions sketch/bench/transpose_sum.daph
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
X = rand($r, $c, 0.0, 1.0, 1, 12345);

<start>
t = t(X);
s = sum(t);
<stop>

print(s);
<end>
Loading
Loading