-
Notifications
You must be signed in to change notification settings - Fork 0
/
opencl.py
170 lines (145 loc) · 5.02 KB
/
opencl.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
#!/usr/bin/env python
import numpy as np
import pyopencl as cl
def chazanMiranker(f, x0, N=int(1e3), eps=1e-6, solutionHistory=False, beta=1., q=0.9, threads=1):
"""
Parallel Gauss Seidel Method variant
"""
start_time = perf_counter()
P = x0
fn = f(P)
n = len(P)
if solutionHistory:
xHist = P.copy()
funHist = np.array([fn])
initialDirections = np.eye(n)
actualDirections = initialDirections.copy()
for it in range(N):
xPoints = np.tile(P, (n, 1)) + \
np.cumsum(actualDirections, axis=0)
alphas = np.array((n,),dtype='float32')
ctx = cl.create_some_context()
queue = cl.CommandQueue(ctx)
mf = cl.mem_flags
directions_g = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=actualDirections[0])
xpoints_g = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=xPoints)
size_g = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=n)
# This loop is parallelizable
prg = cl.Program(ctx, """
double f (double alpha, double xPoint[], double direction[], int length){
double arg[length];
for (int i=0; i < length; i++) {
arg[i] = xPoint[i] + alpha * direction[i];
}
return testFunc1(arg, length);
}
double testFunc1(double x[], int size){
double sum1 = 0;
double sum2 = 0;
for (int i = 0; i <= size - 1; i++){
sum1 += (2*(i+1) - 1) * pow((x[i] - (2 + (i+1))), 4);
}
for (int j = 0; j <= size - 2; j++){
for (int z = j + 1; z <= size - 1; z++){
sum2 += pow((x[j] - x[z] + ((z+1) - (j+1))), 4);
}
}
return sum1 + sum2;
}
__kernel void brent(
__global const float *directions_g, __global const float *xpoints_g, __global const int *size_g, __global float *alphas_g)
{
int gid = get_global_id(0);
double x, d, e, m, p, q, r, tol, t2, u, v, w, fu, fv, fw, fx;
double eps = sqrt(2.220446049250313E-016);
const double c = (3 - sqrt(5)) / 2;
v = w = x = a + c*(b - a);
e = 0;
fv = fw = fx = f(x, xpoints_g[gid], directions_g, size_g);
while (true)
{
m = 0.5*(a + b);
tol = eps*fabs(x) + t;
t2 = 2 * tol;
if (fabs(x - m) <= t2 - 0.5*(b - a)) break;
p = q = r = 0;
if (fabs(e) > tol)
{
r = (x - w)*(fx - fv);
q = (x - v)*(fx - fw);
p = (x - v)*q - (x - w)*r;
q = 2 * (q - r);
if (q > 0)p = -p; else q = -q;
r = e; e = d;
}
if (fabs(p) < fabs(0.5*q*r) && p > q*(a - x) && p < q*(b - x))
{
d = p / q;
u = x + d;
if (u - a < t2 || b - u < t2)
{
d = x < m ? tol : -tol;
}
}
else
{
e = (x < m ? b : a) - x;
d = c*e;
}
u = x + (fabs(d) >= tol ? d : (d > 0 ? tol : -tol));
fu = f(u, xpoints_g[gid], directions_g, size_g);
if (fu <= fx)
{
if (u < x)b = x; else a = x;
v = w; fv = fw; w = x; fw = fx; x = u; fx = fu;
}
else
{
if (u < x) a = u; else b = u;
if (fu<=fw || w==x)
{
v = w; fv = fw; w = u; fw = fu;
}
else if (fu <= fv || v == x || v == w)
{
v = u; fv = fu;
}
}
}
alphas_g[gid] = x
}
""").build()
alphas_g = cl.Buffer(ctx, mf.WRITE_ONLY, xPoints[0].nbytes)
knl = prg.sum
knl(queue, xPoints[0].shape, None, xpoints_g, directions_g, size_g, alphas_g)
cl.enqueue_copy(queue, alphas, alphas_g)
Pnew = xPoints[0] + alphas[0] * actualDirections[0]
# debug
# print(f"Chazan: {f(Pnew)}, it: {it+1}")
if f(Pnew) - 0 < eps:
P = Pnew
break
else:
if f(Pnew) < f(P):
P = Pnew
if solutionHistory:
xHist = np.vstack((xHist, P))
funHist = np.vstack((funHist, f(P)))
for dirInd in range(n - 1):
actualDirections[dirInd] = actualDirections[dirInd + 1] + \
(alphas[dirInd + 1] - alphas[dirInd]) * \
actualDirections[0]
newDir = beta * initialDirections[it % initialDirections.shape[-1]]
beta *= q
actualDirections[-1] = newDir
end_time = perf_counter()
retval = {
'x': P,
'fun': f(P),
'nit': it + 1,
'time': end_time - start_time
}
if solutionHistory:
retval['xHist'] = xHist
retval['funHist'] = funHist
return retval