forked from ffevotte/libeft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qual.py
executable file
·217 lines (172 loc) · 5.15 KB
/
qual.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#!/usr/bin/python -u
# -*- coding: utf-8 -*-
# This file is part of LibEFT, an error-free transformations library
#
# Copyright (C) 2017
# F. Févotte <[email protected]>
# B. Lathuilière <[email protected]>
# EDF SA
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
# 02111-1307, USA.
#
# The GNU General Public License is contained in the file COPYING.
import math
from numpy import linspace
from numpy.random import shuffle
from fractions import Fraction
from random import random
def dot (x, y, f=lambda x: x):
"""Dot product between x and y.
If argument f is given, it is a function applied to each coefficient of each
vector before it is used in computations.
In particular, f = Fraction allows computing an accurate result using
rational numbers.
"""
acc = f(0)
for i in xrange(len(x)):
acc += f(x[i]) * f(y[i])
return acc
def genDot (n, c):
"""Generate two vectors whose dot product is ill-conditioned
Arguments:
n -- vectors size
c -- target condition number
Results:
x, y -- vectors of size n
d -- accurate dot product, rounded to nearest
C -- actual condition number of the dot product
"""
# Initialization
x = [0] * n
y = [0] * n
# First half of the vectors:
# random numbers within a large exponent range
n2 = int(n / 2)
b = math.log(c) / math.log(2)
e = [random() * b/2 for i in xrange(n2)]
e[0] = b/2 + 1 # Make sure exponents b/2
e[n2-1] = 0 # and 0 actually occur
for i in xrange(n2):
x[i] = (2*random()-1) * 2**(e[i])
y[i] = (2*random()-1) * 2**(e[i])
# Second half of the vectors such that
# (*) log2( dot (x[1:i], y[1:i]) ) decreases from b/2 to 0
e = linspace (b/2, 0, n-n2)
for i in xrange(n-n2):
# Random x[i]
cx = (2*random()-1) * 2**(e[i])
x[i+n2] = cx
# y[i] chosen according to (*)
cy = (2*random()-1) * 2**(e[i])
y[i+n2] = (cy - float (dot (x, y, Fraction))) / cx
# Random permutation of x and y
perm = range(n); shuffle (perm)
X = [0] * n
Y = [0] * n
for i in xrange(n):
X[i] = x[perm[i]]
Y[i] = y[perm[i]]
# Dot product, rounded to nearest
d = float (dot (X, Y, Fraction))
# Actual condition number
C = 2 * dot (X, Y, abs) / abs(d)
return [X,Y,d,C]
# Helper functions for standalone Test
def split (a):
factor = 2**27 + 1
c = factor * a
x = c - (c - a)
y = a - x
return (x, y)
def twoProd (a, b):
x = a*b
a1, a2 = split (a)
b1, b2 = split (b)
y = a2*b2 - (((x-a1*b1) - a2*b1) - a1*b2)
return (x, y)
def twoSum (a, b):
x = a + b
if abs(b) > abs(a):
(a,b) = (b,a)
z = x - a
y = b - z
return (x, y)
def dot1 (x,y):
acc = 0
accErr = 0
for i in xrange(len(x)):
acc, e = twoSum (acc, x[i] * y[i])
accErr += e
return acc + accErr
def dot2 (x,y):
acc = 0
accErr = 0
for i in xrange(len(x)):
p, e = twoProd (x[i], y[i])
acc += p
accErr += e
return acc + accErr
def dot3 (x,y):
acc = 0
accErr = 0
for i in xrange(len(x)):
p, ep = twoProd (x[i], y[i])
acc, es = twoSum (acc, p)
accErr += ep + es
return acc + accErr
def err (res, ref):
return min(1, max(1e-16, abs((res-ref)/ref)))
def send (cmd, x, y):
p = subprocess.Popen(cmd,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE)
p.stdin.write("%d\n" % len(x))
for i in xrange(len(x)):
p.stdin.write ("%.18e %.18e\n" % (x[i], y[i]))
return float(p.stdout.read())
def output (f,x):
out = "%.5e " % x
print out,
f.write(out)
if __name__ == "__main__":
import sys
import subprocess
n = 100
c1 = 1
c2 = 1e35
step = 2
try:
n = int(sys.argv[1])
c1 = float(sys.argv[2])
c2 = float(sys.argv[3])
step = float(sys.argv[4])
except:
pass
with open ("qual.dat", "w") as f:
c = c1
while c < 1e33:
(x,y,d,C) = genDot (n, c)
output (f, C)
r = dot(x,y)
output (f, err(r,d))
for test in ["./testCxx", "./testC", "./testF"]:
for version in ["std", "comp"]:
r = send ([test, "dot", version], x, y)
output (f, err(r,d))
print "\n",
f.write ("\n")
c *= step
subprocess.call (["gnuplot", "qual.gp"])