root / ETSN / MySteps_1.py
Historique | Voir | Annoter | Télécharger (2,28 ko)
1 |
#!/usr/bin/env python3
|
---|---|
2 |
|
3 |
import numpy as np |
4 |
import pyopencl as cl |
5 |
|
6 |
# Native Operation under Numpy (for prototyping & tests
|
7 |
def NativeAddition(a_np,b_np): |
8 |
return(a_np+b_np)
|
9 |
|
10 |
# OpenCL complete operation
|
11 |
def OpenCLAddition(a_np,b_np): |
12 |
|
13 |
# Context creation
|
14 |
ctx = cl.create_some_context() |
15 |
# Every process is stored in a queue
|
16 |
queue = cl.CommandQueue(ctx) |
17 |
|
18 |
# Copy from Host to Device using pointers
|
19 |
mf = cl.mem_flags |
20 |
a_g = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=a_np) |
21 |
b_g = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=b_np) |
22 |
|
23 |
# Definition of kernel under OpenCL
|
24 |
prg = cl.Program(ctx, """
|
25 |
__kernel void sum(
|
26 |
__global const float *a_g, __global const float *b_g, __global float *res_g)
|
27 |
{
|
28 |
int gid = get_global_id(0);
|
29 |
res_g[gid] = a_g[gid] + b_g[gid];
|
30 |
}
|
31 |
""").build()
|
32 |
|
33 |
# Memory allocation on Device for result
|
34 |
res_g = cl.Buffer(ctx, mf.WRITE_ONLY, a_np.nbytes) |
35 |
# Synthesis of function "sum" inside Kernel Sources
|
36 |
knl = prg.sum # Use this Kernel object for repeated calls
|
37 |
# Call of kernel previously defined
|
38 |
knl(queue, a_np.shape, None, a_g, b_g, res_g)
|
39 |
|
40 |
# Creation of vector for result with same size as input vectors
|
41 |
res_np = np.empty_like(a_np) |
42 |
# Copy from Device to Host
|
43 |
cl.enqueue_copy(queue, res_np, res_g) |
44 |
|
45 |
return(res_np)
|
46 |
|
47 |
import sys |
48 |
import time |
49 |
|
50 |
if __name__=='__main__': |
51 |
|
52 |
# Size of input vectors definition based on stdin
|
53 |
import sys |
54 |
try:
|
55 |
SIZE=int(sys.argv[1]) |
56 |
print("Size of vectors set to %i" % SIZE)
|
57 |
except:
|
58 |
SIZE=50000
|
59 |
print("Size of vectors set to default size %i" % SIZE)
|
60 |
|
61 |
a_np = np.random.rand(SIZE).astype(np.float32) |
62 |
b_np = np.random.rand(SIZE).astype(np.float32) |
63 |
|
64 |
TimeIn=time.time() |
65 |
res_np=NativeAddition(a_np,b_np) |
66 |
NativeElapsed=time.time()-TimeIn |
67 |
NativeRate=int(SIZE/NativeElapsed)
|
68 |
print("NativeRate: %i" % NativeRate)
|
69 |
|
70 |
TimeIn=time.time() |
71 |
res_cl=OpenCLAddition(a_np,b_np) |
72 |
OpenCLElapsed=time.time()-TimeIn |
73 |
OpenCLRate=int(SIZE/OpenCLElapsed)
|
74 |
print("OpenCLRate: %i" % OpenCLRate)
|
75 |
|
76 |
print("OpenCLvsNative ratio: %f" % (OpenCLRate/NativeRate))
|
77 |
|
78 |
# Check on CPU with Numpy:
|
79 |
print(res_cl - res_np) |
80 |
print(np.linalg.norm(res_cl - res_np)) |
81 |
assert np.allclose(res_np, res_cl)
|