root / Epidevomath / vector8.py @ 175
Historique | Voir | Annoter | Télécharger (8,22 ko)
1 |
#!/usr/bin/env python
|
---|---|
2 |
# -*- coding: utf-8 -*-
|
3 |
"""
|
4 |
Demonstrateur OpenCL pour l'ANR Epidevomath
|
5 |
|
6 |
Emmanuel QUEMENER <emmanuel.quemener@ens-lyon.fr> CeCILLv2
|
7 |
"""
|
8 |
import getopt |
9 |
import sys |
10 |
import time |
11 |
import numpy as np |
12 |
import pyopencl as cl |
13 |
import pyopencl.array as cl_array |
14 |
from numpy.random import randint as nprnd |
15 |
|
16 |
|
17 |
BlobOpenCL= """
|
18 |
#define znew ((z=36969*(z&65535)+(z>>16))<<16)
|
19 |
#define wnew ((w=18000*(w&65535)+(w>>16))&65535)
|
20 |
#define MWC (znew+wnew)
|
21 |
#define SHR3 (jsr=(jsr=(jsr=jsr^(jsr<<17))^(jsr>>13))^(jsr<<5))
|
22 |
#define CONG (jcong=69069*jcong+1234567)
|
23 |
#define KISS ((MWC^CONG)+SHR3)
|
24 |
|
25 |
#define MWCfp MWC * 2.328306435454494e-10f
|
26 |
#define KISSfp KISS * 2.328306435454494e-10f
|
27 |
#define SHR3fp SHR3 * 2.328306435454494e-10f
|
28 |
#define CONGfp CONG * 2.328306435454494e-10f
|
29 |
|
30 |
#define LENGTH 1.
|
31 |
|
32 |
#define PI 3.14159265359
|
33 |
|
34 |
#define SMALL_NUM 0.000000001
|
35 |
|
36 |
__kernel void SplutterPoints(__global float8* clData, float box,
|
37 |
uint seed_z,uint seed_w)
|
38 |
{
|
39 |
int gid = get_global_id(0);
|
40 |
uint z=seed_z+(uint)gid;
|
41 |
uint w=seed_w-(uint)gid;
|
42 |
|
43 |
clData[gid].s01234567 = (float8) (box*MWCfp,box*MWCfp,box*MWCfp,0.,0.,0.,0.,0.);
|
44 |
}
|
45 |
|
46 |
__kernel void ExtendSegment(__global float8* clData, float length,
|
47 |
uint seed_z,uint seed_w)
|
48 |
{
|
49 |
int gid = get_global_id(0);
|
50 |
uint z=seed_z+(uint)gid;
|
51 |
uint w=seed_w-(uint)gid;
|
52 |
|
53 |
float theta=MWCfp*PI;
|
54 |
float phi=MWCfp*PI*2.;
|
55 |
float sinTheta=sin(theta);
|
56 |
clData[gid].s4=clData[gid].s0+length*sinTheta*cos(phi);
|
57 |
clData[gid].s5=clData[gid].s1+length*sinTheta*sin(phi);
|
58 |
clData[gid].s6=clData[gid].s2+length*cos(theta);
|
59 |
|
60 |
}
|
61 |
|
62 |
__kernel void EstimateLength(__global float8* clData,__global float* clSize)
|
63 |
{
|
64 |
int gid = get_global_id(0);
|
65 |
|
66 |
clSize[gid]=distance(clData[gid].lo,clData[gid].hi);
|
67 |
}
|
68 |
|
69 |
// Get from http://geomalgorithms.com/a07-_distance.html
|
70 |
|
71 |
__kernel void ShortestDistance(__global float8* clData,__global float* clDistance)
|
72 |
{
|
73 |
int gidx = get_global_id(0);
|
74 |
int ggsz = get_global_size(0);
|
75 |
int gidy = get_global_id(1);
|
76 |
|
77 |
float4 u = clData[gidx].hi - clData[gidx].lo;
|
78 |
float4 v = clData[gidy].hi - clData[gidy].lo;
|
79 |
float4 w = clData[gidx].lo - clData[gidy].lo;
|
80 |
|
81 |
float a = dot(u,u); // always >= 0
|
82 |
float b = dot(u,v);
|
83 |
float c = dot(v,v); // always >= 0
|
84 |
float d = dot(u,w);
|
85 |
float e = dot(v,w);
|
86 |
|
87 |
float D = a*c - b*b; // always >= 0
|
88 |
float sc, sN, sD = D; // sc = sN / sD, default sD = D >= 0
|
89 |
float tc, tN, tD = D; // tc = tN / tD, default tD = D >= 0
|
90 |
|
91 |
// compute the line parameters of the two closest points
|
92 |
if (D < SMALL_NUM) { // the lines are almost parallel
|
93 |
sN = 0.0; // force using point P0 on segment S1
|
94 |
sD = 1.0; // to prevent possible division by 0.0 later
|
95 |
tN = e;
|
96 |
tD = c;
|
97 |
}
|
98 |
else { // get the closest points on the infinite lines
|
99 |
sN = (b*e - c*d);
|
100 |
tN = (a*e - b*d);
|
101 |
if (sN < 0.0) { // sc < 0 => the s=0 edge is visible
|
102 |
sN = 0.0;
|
103 |
tN = e;
|
104 |
tD = c;
|
105 |
}
|
106 |
else if (sN > sD) { // sc > 1 => the s=1 edge is visible
|
107 |
sN = sD;
|
108 |
tN = e + b;
|
109 |
tD = c;
|
110 |
}
|
111 |
}
|
112 |
|
113 |
if (tN < 0.0) { // tc < 0 => the t=0 edge is visible
|
114 |
tN = 0.0;
|
115 |
// recompute sc for this edge
|
116 |
if (-d < 0.0)
|
117 |
sN = 0.0;
|
118 |
else if (-d > a)
|
119 |
sN = sD;
|
120 |
else {
|
121 |
sN = -d;
|
122 |
sD = a;
|
123 |
}
|
124 |
}
|
125 |
else if (tN > tD) { // tc > 1 => the t=1 edge is visible
|
126 |
tN = tD;
|
127 |
// recompute sc for this edge
|
128 |
if ((-d + b) < 0.0)
|
129 |
sN = 0;
|
130 |
else if ((-d + b) > a)
|
131 |
sN = sD;
|
132 |
else {
|
133 |
sN = (-d + b);
|
134 |
sD = a;
|
135 |
}
|
136 |
}
|
137 |
// finally do the division to get sc and tc
|
138 |
sc = (fabs(sN) < SMALL_NUM ? 0.0 : sN / sD);
|
139 |
tc = (fabs(tN) < SMALL_NUM ? 0.0 : tN / tD);
|
140 |
|
141 |
// get the difference of the two closest points
|
142 |
float4 dP = w + (sc * u) - (tc * v); // = S1(sc) - S2(tc)
|
143 |
|
144 |
clDistance[ggsz*gidy+gidx]=length(dP); // return the closest distance
|
145 |
}
|
146 |
|
147 |
"""
|
148 |
|
149 |
if __name__=='__main__': |
150 |
|
151 |
# Set defaults values
|
152 |
|
153 |
# Id of Device : 1 is for first find !
|
154 |
Device=1
|
155 |
# Iterations is integer
|
156 |
Number=16384
|
157 |
# Size of box
|
158 |
SizeOfBox=1000.
|
159 |
# Size of segment
|
160 |
LengthOfSegment=1.
|
161 |
# Redo the last process
|
162 |
Redo=1
|
163 |
|
164 |
HowToUse='%s -d <DeviceId> -n <NumberOfSegments> -s <SizeOfBox> -l <LengthOfSegment>'
|
165 |
|
166 |
try:
|
167 |
opts, args = getopt.getopt(sys.argv[1:],"hd:n:s:l:r:",["device=","number=","size=","length=","redo="]) |
168 |
except getopt.GetoptError:
|
169 |
print HowToUse % sys.argv[0] |
170 |
sys.exit(2)
|
171 |
|
172 |
for opt, arg in opts: |
173 |
if opt == '-h': |
174 |
print HowToUse % sys.argv[0] |
175 |
|
176 |
print "\nInformations about devices detected under OpenCL:" |
177 |
try:
|
178 |
Id=1
|
179 |
for platform in cl.get_platforms(): |
180 |
for device in platform.get_devices(): |
181 |
deviceType=cl.device_type.to_string(device.type) |
182 |
print "Device #%i from %s of type %s : %s" % (Id,platform.vendor.lstrip(),deviceType,device.name.lstrip()) |
183 |
Id=Id+1
|
184 |
sys.exit() |
185 |
except ImportError: |
186 |
print "Your platform does not seem to support OpenCL" |
187 |
sys.exit() |
188 |
|
189 |
elif opt in ("-d", "--device"): |
190 |
Device=int(arg)
|
191 |
elif opt in ("-n", "--number"): |
192 |
Number=int(arg)
|
193 |
elif opt in ("-s", "--size"): |
194 |
SizeOfBox=np.float32(arg) |
195 |
elif opt in ("-l", "--length"): |
196 |
LengthOfSegment=np.float32(arg) |
197 |
elif opt in ("-r", "--redo"): |
198 |
Redo=int(arg)
|
199 |
|
200 |
print "Device choosed : %s" % Device |
201 |
print "Number of segments : %s" % Number |
202 |
print "Size of Box : %s" % SizeOfBox |
203 |
print "Length of Segment % s" % LengthOfSegment |
204 |
print "Redo the last process % s" % Redo |
205 |
|
206 |
MyData = np.zeros(Number, dtype=cl_array.vec.float8) |
207 |
|
208 |
Id=1
|
209 |
HasXPU=False
|
210 |
for platform in cl.get_platforms(): |
211 |
for device in platform.get_devices(): |
212 |
if Id==Device:
|
213 |
PlatForm=platform |
214 |
XPU=device |
215 |
print "CPU/GPU selected: ",device.name.lstrip() |
216 |
HasXPU=True
|
217 |
Id+=1
|
218 |
|
219 |
if HasXPU==False: |
220 |
print "No XPU #%i found in all of %i devices, sorry..." % (Device,Id-1) |
221 |
sys.exit() |
222 |
|
223 |
# Je cree le contexte et la queue pour son execution
|
224 |
try:
|
225 |
ctx = cl.Context([XPU]) |
226 |
queue = cl.CommandQueue(ctx,properties=cl.command_queue_properties.PROFILING_ENABLE) |
227 |
except:
|
228 |
print "Crash during context creation" |
229 |
|
230 |
|
231 |
MyRoutines = cl.Program(ctx, BlobOpenCL).build() |
232 |
|
233 |
mf = cl.mem_flags |
234 |
clData = cl.Buffer(ctx, mf.READ_WRITE, MyData.nbytes) |
235 |
|
236 |
print 'Tous au meme endroit',MyData |
237 |
|
238 |
MyRoutines.SplutterPoints(queue,(Number,1),None,clData,np.float32(SizeOfBox-LengthOfSegment),np.uint32(nprnd(2**32)),np.uint32(nprnd(2**32))) |
239 |
|
240 |
cl.enqueue_copy(queue, MyData, clData) |
241 |
|
242 |
print 'Tous distribues',MyData |
243 |
|
244 |
MyRoutines.ExtendSegment(queue,(Number,1),None,clData,np.float32(LengthOfSegment),np.uint32(nprnd(2**32)),np.uint32(nprnd(2**32))) |
245 |
|
246 |
cl.enqueue_copy(queue, MyData, clData) |
247 |
|
248 |
print 'Tous avec leur extremite',MyData |
249 |
|
250 |
MySize = np.zeros(len(MyData), dtype=np.float32)
|
251 |
clSize = cl.Buffer(ctx, mf.READ_WRITE, MySize.nbytes) |
252 |
|
253 |
MyRoutines.EstimateLength(queue, (Number,1), None, clData, clSize) |
254 |
cl.enqueue_copy(queue, MySize, clSize) |
255 |
|
256 |
print 'La distance de chacun avec son extremite',MySize |
257 |
|
258 |
MyDistance = np.zeros(len(MyData)*len(MyData), dtype=np.float32) |
259 |
clDistance = cl.Buffer(ctx, mf.READ_WRITE, MyDistance.nbytes) |
260 |
|
261 |
time_start=time.time() |
262 |
for i in xrange(Redo): |
263 |
CLLaunch=MyRoutines.ShortestDistance(queue, (Number,Number), None, clData, clDistance)
|
264 |
sys.stdout.write('.')
|
265 |
CLLaunch.wait() |
266 |
print "\nDuration on %s for each %s" % (Device,(time.time()-time_start)/Redo) |
267 |
cl.enqueue_copy(queue, MyDistance, clDistance) |
268 |
|
269 |
MyDistance=np.reshape(MyDistance,(Number,Number)) |
270 |
clDistance.release() |
271 |
|
272 |
print 'La distance de chacun',MyDistance |
273 |
|
274 |
clData.release() |