How it works...

In the first lines of the script, we import all the requested modules.

In order to initialize the context, we use the cl.create_some_context() method. This asks the user which context must be used to perform the calculation:

Choose platform:
[0] <pyopencl.Platform 'NVIDIA CUDA' at 0x1c0a25aecf0>
[1] <pyopencl.Platform 'Intel(R) OpenCL' at 0x1c0a2608400>

Then, we need to instantiate the queue that will receive ElementwiseKernel:

queue = cl.CommandQueue(context)

Input and output vectors are instantiated. The input vectors, vector_a and vector_b, are integer vectors of random values obtained using the random.randint NumPy function. These vectors are then copied into the device by using the PyOpenCL statement:

cl.array_to_device(queue,array)

In ElementwiseKernel, an object is created:

elementwiseSum = cl.elementwise.ElementwiseKernel(context,
"int *a, int *b, int *c", "c[i] = a[i] + b[i]", "sum")
Note that all the arguments are in the form of a string formatted as a C argument list (they are all integers).

The operation is a C-like code snippet that carries out the operation, that is, the sum of the input vector elements.
The name of the function with which the kernel will be compiled is sum.

Finally, we call the elementwiseSum function with the arguments defined previously:

elementwiseSum(vector_a, vector_b, result_vector)

The example ends by printing the input vectors and the result obtained. The output looks like this:

(base) C:>python elementwisePyopencl.py

Choose platform:
[0] <pyopencl.Platform 'NVIDIA CUDA' at 0x1c0a25aecf0>
[1] <pyopencl.Platform 'Intel(R) OpenCL' at 0x1c0a2608400>
Choice [0]:1

Choose device(s):
[0] <pyopencl.Device 'Intel(R) HD Graphics 5500' on 'Intel(R) OpenCL' at 0x1c0a1640db0>
[1] <pyopencl.Device 'Intel(R) Core(TM) i7-5500U CPU @ 2.40GHz' on 'Intel(R) OpenCL' at 0x1c0a15e53f0>
Choice, comma-separated [0]:0
PyOpenCL ELEMENTWISE SUM OF TWO VECTORS
VECTOR LENGTH = 100
INPUT VECTOR A
[24 64 73 37 40 4 41 85 19 90 32 51 6 89 98 56 97 53 34 91 82 89 97 2
54 65 90 90 91 75 30 8 62 94 63 69 31 99 8 18 28 7 81 72 14 53 91 80
76 39 8 47 25 45 26 56 23 47 41 18 89 17 82 84 10 75 56 89 71 56 66 61
58 54 27 88 16 20 9 61 68 63 74 84 18 82 67 30 15 25 25 3 93 36 24 27
70 5 78 15]

INPUT VECTOR B
[49 18 69 43 51 72 37 50 79 34 97 49 51 29 89 81 33 7 47 93 70 52 63 90
99 95 58 33 41 70 84 87 20 83 74 43 78 34 94 47 89 4 30 36 34 56 32 31
56 22 50 52 68 98 52 80 14 98 43 60 20 49 15 38 74 89 99 29 96 65 89 41
72 53 89 31 34 64 0 47 87 70 98 86 41 25 34 10 44 36 54 52 54 86 33 38
25 49 75 53]

OUTPUT VECTOR RESULT A + B
[73 82 142 80 91 76 78 135 98 124 129 100 57 118 187 137 130 60 81 184
152 141 160 92 153 160 148 123 132 145 114 95 82 177 137 112
109 133
102 65 117 11 111 108 48 109 123 111 132 61 58 99 93 143
78 136 37 145
84 78 109 66 97 122 84 164 155 118 167 121 155 102
130 107 116 119 50
84 9 108 155 133 172 170 59 107 101 40 59 61
79 55 147 122 57 65
95 54 153 68]
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset