Input-dependent models now activated for all the operations

This commit is contained in:
Philippe Tillet
2014-10-04 08:58:11 +02:00
parent 044419f9f0
commit fc8b450a7c
5 changed files with 89 additions and 90 deletions

View File

@@ -6,71 +6,59 @@ import numpy as np
from sklearn.neighbors.kde import KernelDensity
from pyviennacl.atidlas import FetchingPolicy
def decode(y):
fetch = [FetchingPolicy.FETCH_FROM_LOCAL, FetchingPolicy.FETCH_FROM_GLOBAL_CONTIGUOUS, FetchingPolicy.FETCH_FROM_GLOBAL_STRIDED]
y[7] = fetch[y[7]]
y[8] = fetch[y[8]]
return y
def resample(X, tbincount, densities, step):
def resample(X, draw):
Xtuples = [tuple(x) for x in X]
r = random.random()
while(True):
if(len(tbincount)==0 or len(densities)==0 or r<=1.0/len(densities)):
x = np.array([step*random.randint(1,40), step*random.randint(1,40), step*random.randint(1,40)])
else:
probs = [1.0/x if x>0 else 0 for x in tbincount]
distr = np.random.choice(range(tbincount.size), p = probs/np.sum(probs))
x = densities[distr].sample()[0]
x = np.maximum(np.ones(x.shape),(x - step/2).astype(int)/step + 1)*step
x = draw()
if tuple(x) not in Xtuples:
break
return x.astype(int)
def generate_dataset(TemplateType, execution_handler):
I = 50
step = 64
path = "./data"
def generate_dataset(TemplateType, execution_handler, nTuning, nDataPoints, compute_perf, draw):
# print "Getting some good profiles..."
# X = np.empty((I, 3))
# t = np.empty(I)
# profiles = []
# for i in range(I):
# x = resample(X, [], [], step)
# y = execution_handler(x)
# if y not in profiles:
# profiles.append(y)
# idx = profiles.index(y)
# X[i,:] = x
# t[i] = idx
# densities = [KernelDensity(kernel='gaussian', bandwidth=2*step).fit(X[t==i,:]) for i in range(int(max(t))+1)];
#
# print "Generating the dataset..."
# N = 10000
# Y = np.empty((N, len(profiles)))
# X = np.empty((N,3))
# t = []
#
# for i in range(N):
# x = resample(X, [], [], step)
# for j,y in enumerate(profiles):
# T = execution_handler(x, os.devnull, decode(map(int, y)))
# Y[i,j] = 2*1e-9*x[0]*x[1]*x[2]/T
# idx = np.argmax(Y[i,:])
# X[i,:] = x
# t = np.argmax(Y[:i+1,], axis=1)
# densities[idx].fit(X[t==idx,:])
# if i%10==0:
# sys.stdout.write('%d data points generated\r'%i)
# sys.stdout.flush()
#
# np.savetxt(os.path.join(path,"profiles.csv"), profiles)
# np.savetxt(os.path.join(path,"X.csv"), X)
# np.savetxt(os.path.join(path,"Y.csv"), Y)
print "Getting some good profiles..."
nDim = draw().size
X = np.empty((nTuning, nDim))
t = np.empty(nTuning)
profiles = []
for i in range(nTuning):
x = resample(X, draw)
y = execution_handler(x)
if y not in profiles:
profiles.append(y)
idx = profiles.index(y)
X[i,:] = x
t[i] = idx
profiles = np.loadtxt(os.path.join(path,"profiles.csv"))
X = np.loadtxt(os.path.join(path,"X.csv"))
Y = np.loadtxt(os.path.join(path,"Y.csv"))
print "Generating the dataset..."
Y = np.empty((nDataPoints, len(profiles)))
X = np.empty((nDataPoints, nDim))
t = []
for i in range(nDataPoints):
x = resample(X, draw)
for j,y in enumerate(profiles):
T = execution_handler(x, os.devnull, y)
Y[i,j] = compute_perf(x, T)
idx = np.argmax(Y[i,:])
X[i,:] = x
t = np.argmax(Y[:i+1,], axis=1)
if i%10==0:
sys.stdout.write('%d data points generated\r'%i)
sys.stdout.flush()
template_name = TemplateType.__name__
dir = os.path.join("data", template_name)
if not os.path.exists(dir):
os.makedirs(dir)
np.savetxt(os.path.join(dir,"profiles.csv"), profiles)
np.savetxt(os.path.join(dir,"X.csv"), X)
np.savetxt(os.path.join(dir,"Y.csv"), Y)
profiles = np.loadtxt(os.path.join(dir, "profiles.csv"))
X = np.loadtxt(os.path.join(dir, "X.csv"),ndmin=2)
Y = np.loadtxt(os.path.join(dir, "Y.csv"),ndmin=2)
return X, Y, profiles