Logo AND Algorithmique Numérique Distribuée

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