Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
comment
[simgrid.git] / contrib / network_model / calibrate_piecewise.py
1 #!/usr/bin/env python
2
3
4 import sys
5 from math import sqrt
6
7
8 if len(sys.argv) < 5:
9    print("Usage : %s datafile links latency bandwidth [size...]" % sys.argv[0])
10    print("where : links is the number of links between nodes")
11    print("        latency is the nominal latency given in the platform file")
12    print("        bandwidth is the nominal bandwidth given in the platform file")
13    print("        size are segments limites")
14    sys.exit(-1)
15
16 ##-----------------------------------------
17 ## avg : return average of a list of values
18 ## param l list of values
19 ##-----------------------------------------
20 def avg (l):
21    sum = 0
22    for e in l:
23       sum += float(e);
24    return sum / len(l)
25
26 ##-------------------------------------------------
27 ## cov : covariance
28 ## param X first data vector (..x_i..)
29 ## param Y second data vector (..x_i..)
30 ## = 1/n \Sum_{i=1}^n (x_i - avg(x)) * (y_i - avg(y))
31 ##--------------------------------------------------
32 def cov (X, Y):
33    assert len(X) == len(Y)
34    n = len(X)   #  n=len(X)=len(Y)
35    avg_X = avg(X)
36    avg_Y = avg(Y)
37    S_XY = 0.0
38    for i in xrange(n):
39       S_XY += (X[i] - avg_X) * (Y[i] - avg_Y)
40    return (S_XY / n)
41
42 ##----------------------------------
43 ## variance : variance
44 ## param X data vector ( ..x_i.. )
45 ## (S_X)^2 = (Sum ( x_i - avg(x) )^2 ) / n
46 ##----------------------------------
47 def variance (X):
48    n = len(X)
49    avg_X = avg (X)
50    S_X2 = 0.0
51    for i in xrange(n):
52       S_X2 += (X[i] - avg_X) ** 2
53    return (S_X2 / n)
54
55 def calibrate (links, latency, bandwidth, sizes, timings):
56    assert len(sizes) == len(timings)
57    if len(sizes) < 2:
58       return None
59    S_XY = cov(sizes, timings)
60    S_X2 = variance(sizes)
61    a = S_XY / S_X2
62    b = avg(timings) - a * avg(sizes)
63    return (b * 1e-6) / (latency * links), 1e6 / (a * bandwidth)
64
65 ##-----------------------------------------------------------------------------------------------
66 ## main
67 ##-----------------------------------------------------------------------------------------------
68 links = int(sys.argv[2])
69 latency = float(sys.argv[3])
70 bandwidth = float(sys.argv[4])
71 skampidat = open(sys.argv[1], "r")
72
73 timings = []
74 sizes = []
75 for line in skampidat:
76    l = line.split();
77    if line[0] != '#' and len(l) >= 3:   # is it a comment ?
78       ## expected format
79       ## ---------------
80       #count= 8388608  8388608  144916.1       7.6       32  144916.1  143262.0
81       #("%s %d %d %f %f %d %f %f\n" % (countlbl, count, countn, time, stddev, iter, mini, maxi)
82       timings.append(float(l[3]) / links)
83       sizes.append(int(l[1]))
84
85 ## adds message sizes of interest: if values are specified starting from the 6th command line arg 
86 ## and these values are found as message sizes in te log file, add it to the limits list.
87 ## Each of these value si considered a potential inflexion point between two segments.
88 ##
89 ## If no value specified, a single segment is considered from 1st to last message size logged.
90 limits = []
91 if len(sys.argv) > 5:
92    for i in xrange(5, len(sys.argv)):
93       limits += [idx for idx in xrange(len(sizes)) if sizes[idx] == int(sys.argv[i])]
94 limits.append(len(sizes) - 1)
95
96 low = 0
97 for lim in limits:
98    correc = calibrate(links, latency, bandwidth, sizes[low:lim + 1], timings[low:lim + 1])
99    if correc:
100       print("Segment [%d:%d] -- Latency factor=%g -- Bandwidth factor=%g" % (sizes[low], sizes[lim], correc[0], correc[1]))
101    low = lim + 1