pyopencl 2024.2.2__cp311-cp311-macosx_11_0_arm64.whl → 2024.2.4__cp311-cp311-macosx_11_0_arm64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of pyopencl might be problematic. Click here for more details.

Files changed (102) hide show
  1. pyopencl/__init__.py +16 -4
  2. pyopencl/_cl.cpython-311-darwin.so +0 -0
  3. pyopencl/algorithm.py +3 -1
  4. pyopencl/bitonic_sort.py +2 -0
  5. pyopencl/characterize/__init__.py +23 -0
  6. pyopencl/compyte/.git +1 -0
  7. pyopencl/compyte/.github/workflows/autopush.yml +21 -0
  8. pyopencl/compyte/.github/workflows/ci.yml +30 -0
  9. pyopencl/compyte/.gitignore +21 -0
  10. pyopencl/compyte/ndarray/Makefile +31 -0
  11. pyopencl/compyte/ndarray/gpu_ndarray.h +35 -0
  12. pyopencl/compyte/ndarray/pygpu_language.h +207 -0
  13. pyopencl/compyte/ndarray/pygpu_language_cuda.cu +622 -0
  14. pyopencl/compyte/ndarray/pygpu_language_opencl.cpp +317 -0
  15. pyopencl/compyte/ndarray/pygpu_ndarray.cpp +1546 -0
  16. pyopencl/compyte/ndarray/pygpu_ndarray.h +71 -0
  17. pyopencl/compyte/ndarray/pygpu_ndarray_object.h +232 -0
  18. pyopencl/compyte/setup.cfg +9 -0
  19. pyopencl/tools.py +60 -56
  20. pyopencl/version.py +7 -3
  21. {pyopencl-2024.2.2.dist-info → pyopencl-2024.2.4.dist-info}/METADATA +14 -14
  22. pyopencl-2024.2.4.dist-info/RECORD +59 -0
  23. {pyopencl-2024.2.2.dist-info → pyopencl-2024.2.4.dist-info}/WHEEL +1 -1
  24. pyopencl-2024.2.2.data/data/CITATION.cff +0 -74
  25. pyopencl-2024.2.2.data/data/CMakeLists.txt +0 -83
  26. pyopencl-2024.2.2.data/data/Makefile.in +0 -21
  27. pyopencl-2024.2.2.data/data/README.rst +0 -70
  28. pyopencl-2024.2.2.data/data/README_SETUP.txt +0 -34
  29. pyopencl-2024.2.2.data/data/aksetup_helper.py +0 -1013
  30. pyopencl-2024.2.2.data/data/configure.py +0 -6
  31. pyopencl-2024.2.2.data/data/contrib/cldis.py +0 -91
  32. pyopencl-2024.2.2.data/data/contrib/fortran-to-opencl/README +0 -29
  33. pyopencl-2024.2.2.data/data/contrib/fortran-to-opencl/translate.py +0 -1441
  34. pyopencl-2024.2.2.data/data/contrib/pyopencl.vim +0 -84
  35. pyopencl-2024.2.2.data/data/doc/Makefile +0 -23
  36. pyopencl-2024.2.2.data/data/doc/algorithm.rst +0 -214
  37. pyopencl-2024.2.2.data/data/doc/array.rst +0 -305
  38. pyopencl-2024.2.2.data/data/doc/conf.py +0 -26
  39. pyopencl-2024.2.2.data/data/doc/howto.rst +0 -105
  40. pyopencl-2024.2.2.data/data/doc/index.rst +0 -137
  41. pyopencl-2024.2.2.data/data/doc/make_constants.py +0 -561
  42. pyopencl-2024.2.2.data/data/doc/misc.rst +0 -885
  43. pyopencl-2024.2.2.data/data/doc/runtime.rst +0 -51
  44. pyopencl-2024.2.2.data/data/doc/runtime_const.rst +0 -30
  45. pyopencl-2024.2.2.data/data/doc/runtime_gl.rst +0 -78
  46. pyopencl-2024.2.2.data/data/doc/runtime_memory.rst +0 -527
  47. pyopencl-2024.2.2.data/data/doc/runtime_platform.rst +0 -184
  48. pyopencl-2024.2.2.data/data/doc/runtime_program.rst +0 -364
  49. pyopencl-2024.2.2.data/data/doc/runtime_queue.rst +0 -182
  50. pyopencl-2024.2.2.data/data/doc/subst.rst +0 -36
  51. pyopencl-2024.2.2.data/data/doc/tools.rst +0 -4
  52. pyopencl-2024.2.2.data/data/doc/types.rst +0 -42
  53. pyopencl-2024.2.2.data/data/examples/black-hole-accretion.py +0 -2227
  54. pyopencl-2024.2.2.data/data/examples/demo-struct-reduce.py +0 -75
  55. pyopencl-2024.2.2.data/data/examples/demo.py +0 -39
  56. pyopencl-2024.2.2.data/data/examples/demo_array.py +0 -32
  57. pyopencl-2024.2.2.data/data/examples/demo_array_svm.py +0 -37
  58. pyopencl-2024.2.2.data/data/examples/demo_elementwise.py +0 -34
  59. pyopencl-2024.2.2.data/data/examples/demo_elementwise_complex.py +0 -53
  60. pyopencl-2024.2.2.data/data/examples/demo_mandelbrot.py +0 -183
  61. pyopencl-2024.2.2.data/data/examples/demo_meta_codepy.py +0 -56
  62. pyopencl-2024.2.2.data/data/examples/demo_meta_template.py +0 -55
  63. pyopencl-2024.2.2.data/data/examples/dump-performance.py +0 -38
  64. pyopencl-2024.2.2.data/data/examples/dump-properties.py +0 -86
  65. pyopencl-2024.2.2.data/data/examples/gl_interop_demo.py +0 -84
  66. pyopencl-2024.2.2.data/data/examples/gl_particle_animation.py +0 -218
  67. pyopencl-2024.2.2.data/data/examples/ipython-demo.ipynb +0 -203
  68. pyopencl-2024.2.2.data/data/examples/median-filter.py +0 -99
  69. pyopencl-2024.2.2.data/data/examples/n-body.py +0 -1070
  70. pyopencl-2024.2.2.data/data/examples/narray.py +0 -37
  71. pyopencl-2024.2.2.data/data/examples/noisyImage.jpg +0 -0
  72. pyopencl-2024.2.2.data/data/examples/pi-monte-carlo.py +0 -1166
  73. pyopencl-2024.2.2.data/data/examples/svm.py +0 -82
  74. pyopencl-2024.2.2.data/data/examples/transpose.py +0 -229
  75. pyopencl-2024.2.2.data/data/pytest.ini +0 -3
  76. pyopencl-2024.2.2.data/data/src/bitlog.cpp +0 -51
  77. pyopencl-2024.2.2.data/data/src/bitlog.hpp +0 -83
  78. pyopencl-2024.2.2.data/data/src/clinfo_ext.h +0 -134
  79. pyopencl-2024.2.2.data/data/src/mempool.hpp +0 -444
  80. pyopencl-2024.2.2.data/data/src/pyopencl_ext.h +0 -77
  81. pyopencl-2024.2.2.data/data/src/tools.hpp +0 -90
  82. pyopencl-2024.2.2.data/data/src/wrap_cl.cpp +0 -61
  83. pyopencl-2024.2.2.data/data/src/wrap_cl.hpp +0 -5853
  84. pyopencl-2024.2.2.data/data/src/wrap_cl_part_1.cpp +0 -369
  85. pyopencl-2024.2.2.data/data/src/wrap_cl_part_2.cpp +0 -702
  86. pyopencl-2024.2.2.data/data/src/wrap_constants.cpp +0 -1274
  87. pyopencl-2024.2.2.data/data/src/wrap_helpers.hpp +0 -213
  88. pyopencl-2024.2.2.data/data/src/wrap_mempool.cpp +0 -738
  89. pyopencl-2024.2.2.data/data/test/add-vectors-32.spv +0 -0
  90. pyopencl-2024.2.2.data/data/test/add-vectors-64.spv +0 -0
  91. pyopencl-2024.2.2.data/data/test/empty-header.h +0 -1
  92. pyopencl-2024.2.2.data/data/test/test_algorithm.py +0 -1180
  93. pyopencl-2024.2.2.data/data/test/test_array.py +0 -2392
  94. pyopencl-2024.2.2.data/data/test/test_arrays_in_structs.py +0 -100
  95. pyopencl-2024.2.2.data/data/test/test_clmath.py +0 -529
  96. pyopencl-2024.2.2.data/data/test/test_clrandom.py +0 -75
  97. pyopencl-2024.2.2.data/data/test/test_enqueue_copy.py +0 -271
  98. pyopencl-2024.2.2.data/data/test/test_wrapper.py +0 -1565
  99. pyopencl-2024.2.2.dist-info/LICENSE +0 -282
  100. pyopencl-2024.2.2.dist-info/RECORD +0 -123
  101. pyopencl-2024.2.2.dist-info/top_level.txt +0 -1
  102. {pyopencl-2024.2.2.data/data → pyopencl-2024.2.4.dist-info/licenses}/LICENSE +0 -0
@@ -1,84 +0,0 @@
1
- from OpenGL.GL import *
2
- from OpenGL.GLUT import *
3
- from OpenGL.raw.GL.VERSION.GL_1_5 import glBufferData as rawGlBufferData
4
-
5
- import pyopencl as cl
6
-
7
-
8
- n_vertices = 10000
9
-
10
- src = """
11
-
12
- __kernel void generate_sin(__global float2* a)
13
- {
14
- int id = get_global_id(0);
15
- int n = get_global_size(0);
16
- float r = (float)id / (float)n;
17
- float x = r * 16.0f * 3.1415f;
18
- a[id].x = r * 2.0f - 1.0f;
19
- a[id].y = native_sin(x);
20
- }
21
-
22
- """
23
-
24
- def initialize():
25
- platform = cl.get_platforms()[0]
26
-
27
- import sys
28
-
29
- from pyopencl.tools import get_gl_sharing_context_properties
30
- if sys.platform == "darwin":
31
- ctx = cl.Context(properties=get_gl_sharing_context_properties(),
32
- devices=[])
33
- else:
34
- # Some OSs prefer clCreateContextFromType, some prefer
35
- # clCreateContext. Try both.
36
- try:
37
- ctx = cl.Context(properties=[
38
- (cl.context_properties.PLATFORM, platform)]
39
- + get_gl_sharing_context_properties())
40
- except:
41
- ctx = cl.Context(properties=[
42
- (cl.context_properties.PLATFORM, platform)]
43
- + get_gl_sharing_context_properties(),
44
- devices = [platform.get_devices()[0]])
45
-
46
- glClearColor(1, 1, 1, 1)
47
- glColor(0, 0, 1)
48
- vbo = glGenBuffers(1)
49
- glBindBuffer(GL_ARRAY_BUFFER, vbo)
50
- rawGlBufferData(GL_ARRAY_BUFFER, n_vertices * 2 * 4, None, GL_STATIC_DRAW)
51
- glEnableClientState(GL_VERTEX_ARRAY)
52
- glVertexPointer(2, GL_FLOAT, 0, None)
53
- coords_dev = cl.GLBuffer(ctx, cl.mem_flags.READ_WRITE, int(vbo))
54
- prog = cl.Program(ctx, src).build()
55
- queue = cl.CommandQueue(ctx)
56
- cl.enqueue_acquire_gl_objects(queue, [coords_dev])
57
- prog.generate_sin(queue, (n_vertices,), None, coords_dev)
58
- cl.enqueue_release_gl_objects(queue, [coords_dev])
59
- queue.finish()
60
- glFlush()
61
-
62
- def display():
63
- glClear(GL_COLOR_BUFFER_BIT)
64
- glDrawArrays(GL_LINE_STRIP, 0, n_vertices)
65
- glFlush()
66
-
67
- def reshape(w, h):
68
- glViewport(0, 0, w, h)
69
- glMatrixMode(GL_PROJECTION)
70
- glLoadIdentity()
71
- glMatrixMode(GL_MODELVIEW)
72
-
73
- if __name__ == '__main__':
74
- import sys
75
- glutInit(sys.argv)
76
- if len(sys.argv) > 1:
77
- n_vertices = int(sys.argv[1])
78
- glutInitWindowSize(800, 160)
79
- glutInitWindowPosition(0, 0)
80
- glutCreateWindow('OpenCL/OpenGL Interop Tutorial: Sin Generator')
81
- glutDisplayFunc(display)
82
- glutReshapeFunc(reshape)
83
- initialize()
84
- glutMainLoop()
@@ -1,218 +0,0 @@
1
- # Visualization of particles with gravity
2
- # Source: http://enja.org/2010/08/27/adventures-in-opencl-part-2-particles-with-opengl/
3
-
4
- import sys
5
-
6
- import numpy as np
7
- from OpenGL import GL, GLU, GLUT
8
- from OpenGL.arrays import vbo
9
- from OpenGL.GL import (
10
- GL_ARRAY_BUFFER, GL_BLEND, GL_COLOR_ARRAY, GL_COLOR_BUFFER_BIT,
11
- GL_DEPTH_BUFFER_BIT, GL_DYNAMIC_DRAW, GL_FLOAT, GL_MODELVIEW,
12
- GL_ONE_MINUS_SRC_ALPHA, GL_POINT_SMOOTH, GL_POINTS, GL_PROJECTION, GL_SRC_ALPHA,
13
- GL_VERTEX_ARRAY)
14
- from OpenGL.GLUT import GLUT_DEPTH, GLUT_DOUBLE, GLUT_RGBA
15
-
16
- import pyopencl as cl
17
- from pyopencl.tools import get_gl_sharing_context_properties
18
-
19
-
20
- mf = cl.mem_flags
21
-
22
- width = 800
23
- height = 600
24
- num_particles = 100000
25
- time_step = 0.005
26
- mouse_down = False
27
- mouse_old = {"x": 0.0, "y": 0.0}
28
- rotate = {"x": 0.0, "y": 0.0, "z": 0.0}
29
- translate = {"x": 0.0, "y": 0.0, "z": 0.0}
30
- initial_translate = {"x": 0.0, "y": 0.0, "z": -2.5}
31
-
32
-
33
- def glut_window():
34
- GLUT.glutInit(sys.argv)
35
- GLUT.glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH)
36
- GLUT.glutInitWindowSize(width, height)
37
- GLUT.glutInitWindowPosition(0, 0)
38
- window = GLUT.glutCreateWindow("Particle Simulation")
39
-
40
- GLUT.glutDisplayFunc(on_display) # Called by GLUT every frame
41
- GLUT.glutKeyboardFunc(on_key)
42
- GLUT.glutMouseFunc(on_click)
43
- GLUT.glutMotionFunc(on_mouse_move)
44
- GLUT.glutTimerFunc(10, on_timer, 10) # Call draw every 30 ms
45
-
46
- GL.glViewport(0, 0, width, height)
47
- GL.glMatrixMode(GL_PROJECTION)
48
- GL.glLoadIdentity()
49
- GLU.gluPerspective(60.0, width / float(height), 0.1, 1000.0)
50
-
51
- return window
52
-
53
-
54
- def initial_buffers(num_particles):
55
- rng = np.random.default_rng()
56
-
57
- np_position = np.empty((num_particles, 4), dtype=np.float32)
58
- np_color = np.empty((num_particles, 4), dtype=np.float32)
59
- np_velocity = np.empty((num_particles, 4), dtype=np.float32)
60
-
61
- np_position[:, 0] = np.sin(
62
- np.arange(0.0, num_particles) * 2.001 * np.pi / num_particles
63
- )
64
- np_position[:, 0] *= rng.integers(num_particles) / 3.0 + 0.2
65
- np_position[:, 1] = np.cos(
66
- np.arange(0.0, num_particles) * 2.001 * np.pi / num_particles
67
- )
68
- np_position[:, 1] *= rng.integers(num_particles) / 3.0 + 0.2
69
- np_position[:, 2] = 0.0
70
- np_position[:, 3] = 1.0
71
-
72
- np_color[:, :] = [1.0, 1.0, 1.0, 1.0] # White particles
73
-
74
- np_velocity[:, 0] = np_position[:, 0] * 2.0
75
- np_velocity[:, 1] = np_position[:, 1] * 2.0
76
- np_velocity[:, 2] = 3.0
77
- np_velocity[:, 3] = rng.integers(num_particles)
78
-
79
- gl_position = vbo.VBO(
80
- data=np_position, usage=GL_DYNAMIC_DRAW, target=GL_ARRAY_BUFFER
81
- )
82
- gl_position.bind()
83
- gl_color = vbo.VBO(data=np_color, usage=GL_DYNAMIC_DRAW, target=GL_ARRAY_BUFFER)
84
- gl_color.bind()
85
-
86
- return (np_position, np_velocity, gl_position, gl_color)
87
-
88
-
89
- def on_timer(t):
90
- GLUT.glutTimerFunc(t, on_timer, t)
91
- GLUT.glutPostRedisplay()
92
-
93
-
94
- def on_key(*args):
95
- if args[0] == "\033" or args[0] == "q":
96
- sys.exit()
97
-
98
-
99
- def on_click(button, state, x, y):
100
- mouse_old["x"] = x
101
- mouse_old["y"] = y
102
-
103
-
104
- def on_mouse_move(x, y):
105
- rotate["x"] += (y - mouse_old["y"]) * 0.2
106
- rotate["y"] += (x - mouse_old["x"]) * 0.2
107
-
108
- mouse_old["x"] = x
109
- mouse_old["y"] = y
110
-
111
-
112
- def on_display():
113
- """Render the particles"""
114
- # Update or particle positions by calling the OpenCL kernel
115
- cl.enqueue_acquire_gl_objects(queue, [cl_gl_position, cl_gl_color])
116
- kernelargs = (
117
- cl_gl_position,
118
- cl_gl_color,
119
- cl_velocity,
120
- cl_start_position,
121
- cl_start_velocity,
122
- np.float32(time_step),
123
- )
124
- program.particle_fountain(queue, (num_particles,), None, *(kernelargs))
125
- cl.enqueue_release_gl_objects(queue, [cl_gl_position, cl_gl_color])
126
- queue.finish()
127
- GL.glFlush()
128
-
129
- GL.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
130
- GL.glMatrixMode(GL_MODELVIEW)
131
- GL.glLoadIdentity()
132
-
133
- # Handle mouse transformations
134
- GL.glTranslatef(initial_translate["x"], initial_translate["y"], initial_translate["z"])
135
- GL.glRotatef(rotate["x"], 1, 0, 0)
136
- GL.glRotatef(rotate["y"], 0, 1, 0) # we switched around the axis so make this rotate_z
137
- GL.glTranslatef(translate["x"], translate["y"], translate["z"])
138
-
139
- # Render the particles
140
- GL.glEnable(GL_POINT_SMOOTH)
141
- GL.glPointSize(2)
142
- GL.glEnable(GL_BLEND)
143
- GL.glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
144
-
145
- # Set up the VBOs
146
- gl_color.bind()
147
- GL.glColorPointer(4, GL_FLOAT, 0, gl_color)
148
- gl_position.bind()
149
- GL.glVertexPointer(4, GL_FLOAT, 0, gl_position)
150
- GL.glEnableClientState(GL_VERTEX_ARRAY)
151
- GL.glEnableClientState(GL_COLOR_ARRAY)
152
-
153
- # Draw the VBOs
154
- GL.glDrawArrays(GL_POINTS, 0, num_particles)
155
-
156
- GL.glDisableClientState(GL_COLOR_ARRAY)
157
- GL.glDisableClientState(GL_VERTEX_ARRAY)
158
-
159
- GL.glDisable(GL_BLEND)
160
-
161
- GLUT.glutSwapBuffers()
162
-
163
-
164
- window = glut_window()
165
-
166
- (np_position, np_velocity, gl_position, gl_color) = initial_buffers(num_particles)
167
-
168
- platform = cl.get_platforms()[0]
169
- context = cl.Context(
170
- properties=[(cl.context_properties.PLATFORM, platform)]
171
- + get_gl_sharing_context_properties()
172
- )
173
- queue = cl.CommandQueue(context)
174
-
175
- cl_velocity = cl.Buffer(context, mf.COPY_HOST_PTR, hostbuf=np_velocity)
176
- cl_start_position = cl.Buffer(
177
- context, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=np_position
178
- )
179
- cl_start_velocity = cl.Buffer(
180
- context, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=np_velocity
181
- )
182
-
183
- cl_gl_position = cl.GLBuffer(context, mf.READ_WRITE, int(gl_position))
184
- cl_gl_color = cl.GLBuffer(context, mf.READ_WRITE, int(gl_color))
185
-
186
- kernel = """__kernel void particle_fountain(__global float4* position,
187
- __global float4* color,
188
- __global float4* velocity,
189
- __global float4* start_position,
190
- __global float4* start_velocity,
191
- float time_step)
192
- {
193
- unsigned int i = get_global_id(0);
194
- float4 p = position[i];
195
- float4 v = velocity[i];
196
- float life = velocity[i].w;
197
- life -= time_step;
198
- if (life <= 0.f)
199
- {
200
- p = start_position[i];
201
- v = start_velocity[i];
202
- life = 1.0f;
203
- }
204
-
205
- v.z -= 9.8f*time_step;
206
- p.x += v.x*time_step;
207
- p.y += v.y*time_step;
208
- p.z += v.z*time_step;
209
- v.w = life;
210
-
211
- position[i] = p;
212
- velocity[i] = v;
213
-
214
- color[i].w = life; /* Fade points as life decreases */
215
- }"""
216
- program = cl.Program(context, kernel).build()
217
-
218
- GLUT.glutMainLoop()
@@ -1,203 +0,0 @@
1
- {
2
- "cells": [
3
- {
4
- "cell_type": "code",
5
- "execution_count": null,
6
- "id": "cc7d0709",
7
- "metadata": {
8
- "collapsed": false,
9
- "jupyter": {
10
- "outputs_hidden": false
11
- }
12
- },
13
- "outputs": [],
14
- "source": [
15
- "from __future__ import division\n",
16
- "import numpy as np\n",
17
- "import pyopencl as cl\n",
18
- "import pyopencl.array"
19
- ]
20
- },
21
- {
22
- "cell_type": "markdown",
23
- "id": "8ac8d7bb",
24
- "metadata": {},
25
- "source": [
26
- "Load the PyOpenCL IPython extension:"
27
- ]
28
- },
29
- {
30
- "cell_type": "code",
31
- "execution_count": null,
32
- "id": "7023ca2f",
33
- "metadata": {
34
- "collapsed": false,
35
- "jupyter": {
36
- "outputs_hidden": false
37
- }
38
- },
39
- "outputs": [],
40
- "source": [
41
- "%load_ext pyopencl.ipython_ext"
42
- ]
43
- },
44
- {
45
- "cell_type": "markdown",
46
- "id": "9544b53c",
47
- "metadata": {},
48
- "source": [
49
- "Create an OpenCL context and a command queue:"
50
- ]
51
- },
52
- {
53
- "cell_type": "code",
54
- "execution_count": null,
55
- "id": "fac17999",
56
- "metadata": {
57
- "collapsed": false,
58
- "jupyter": {
59
- "outputs_hidden": false
60
- }
61
- },
62
- "outputs": [],
63
- "source": [
64
- "ctx = cl.create_some_context(interactive=True)\n",
65
- "queue = cl.CommandQueue(ctx)"
66
- ]
67
- },
68
- {
69
- "cell_type": "markdown",
70
- "id": "a29daf04",
71
- "metadata": {},
72
- "source": [
73
- "-----\n",
74
- "\n",
75
- "Define an OpenCL kernel using the `%%cl_kernel` magic:"
76
- ]
77
- },
78
- {
79
- "cell_type": "code",
80
- "execution_count": null,
81
- "id": "65c7e6c9",
82
- "metadata": {
83
- "collapsed": false,
84
- "jupyter": {
85
- "outputs_hidden": false
86
- }
87
- },
88
- "outputs": [],
89
- "source": [
90
- "%%cl_kernel -o \"-cl-fast-relaxed-math\"\n",
91
- "\n",
92
- "__kernel void sum_vector(__global const float *a,\n",
93
- "__global const float *b, __global float *c)\n",
94
- "{\n",
95
- " int gid = get_global_id(0);\n",
96
- " c[gid] = a[gid] + b[gid];\n",
97
- "}"
98
- ]
99
- },
100
- {
101
- "cell_type": "markdown",
102
- "id": "cfb57357",
103
- "metadata": {},
104
- "source": [
105
- "This looks for `cl_ctx` or `ctx` in the user namespace to find a PyOpenCL context.\n",
106
- "\n",
107
- "Kernel names are automatically injected into the user namespace, so we can just use `sum_vector` from Python below.\n",
108
- "\n",
109
- "Now create some data to work on:"
110
- ]
111
- },
112
- {
113
- "cell_type": "code",
114
- "execution_count": null,
115
- "id": "1d80ff38",
116
- "metadata": {
117
- "collapsed": false,
118
- "jupyter": {
119
- "outputs_hidden": false
120
- }
121
- },
122
- "outputs": [],
123
- "source": [
124
- "n = 10000\n",
125
- "\n",
126
- "a = cl.array.empty(queue, n, dtype=np.float32)\n",
127
- "a.fill(15)\n",
128
- "\n",
129
- "b_host = np.random.randn(n).astype(np.float32)\n",
130
- "b = cl.array.to_device(queue, b_host)\n",
131
- "\n",
132
- "c = cl.array.empty_like(a)"
133
- ]
134
- },
135
- {
136
- "cell_type": "markdown",
137
- "id": "61fccb61",
138
- "metadata": {},
139
- "source": [
140
- "Run the kernel:"
141
- ]
142
- },
143
- {
144
- "cell_type": "code",
145
- "execution_count": null,
146
- "id": "2ba991b3",
147
- "metadata": {
148
- "collapsed": false,
149
- "jupyter": {
150
- "outputs_hidden": false
151
- }
152
- },
153
- "outputs": [],
154
- "source": [
155
- "sum_vector(queue, (n,), None, a.data, b.data, c.data)"
156
- ]
157
- },
158
- {
159
- "cell_type": "markdown",
160
- "id": "11a55b38",
161
- "metadata": {},
162
- "source": [
163
- "Check the result using `numpy` operations:"
164
- ]
165
- },
166
- {
167
- "cell_type": "code",
168
- "execution_count": null,
169
- "id": "ee3560c1",
170
- "metadata": {
171
- "collapsed": false,
172
- "jupyter": {
173
- "outputs_hidden": false
174
- }
175
- },
176
- "outputs": [],
177
- "source": [
178
- "assert (c.get() == b_host + 15).all()"
179
- ]
180
- }
181
- ],
182
- "metadata": {
183
- "kernelspec": {
184
- "display_name": "Python 3 (ipykernel)",
185
- "language": "python",
186
- "name": "python3"
187
- },
188
- "language_info": {
189
- "codemirror_mode": {
190
- "name": "ipython",
191
- "version": 3
192
- },
193
- "file_extension": ".py",
194
- "mimetype": "text/x-python",
195
- "name": "python",
196
- "nbconvert_exporter": "python",
197
- "pygments_lexer": "ipython3",
198
- "version": "3.10.5"
199
- }
200
- },
201
- "nbformat": 4,
202
- "nbformat_minor": 5
203
- }
@@ -1,99 +0,0 @@
1
- import numpy as np
2
- from imageio import imread, imsave
3
-
4
- import pyopencl as cl
5
-
6
-
7
- # Read in image
8
- img = imread("noisyImage.jpg").astype(np.float32)
9
- print(img.shape)
10
-
11
- img = np.mean(img, axis=2)
12
- print(img.shape)
13
-
14
- ctx = cl.create_some_context()
15
- queue = cl.CommandQueue(ctx)
16
-
17
- mf = cl.mem_flags
18
-
19
- # Kernel function
20
- src = """
21
- void sort(int *a, int *b, int *c) {
22
- int swap;
23
- if(*a > *b) {
24
- swap = *a;
25
- *a = *b;
26
- *b = swap;
27
- }
28
- if(*a > *c) {
29
- swap = *a;
30
- *a = *c;
31
- *c = swap;
32
- }
33
- if(*b > *c) {
34
- swap = *b;
35
- *b = *c;
36
- *c = swap;
37
- }
38
- }
39
- __kernel void medianFilter(
40
- __global float *img, __global float *result, __global int *width, __global
41
- int *height)
42
- {
43
- int w = *width;
44
- int h = *height;
45
- int posx = get_global_id(1);
46
- int posy = get_global_id(0);
47
- int i = w*posy + posx;
48
- // Keeping the edge pixels the same
49
- if( posx == 0 || posy == 0 || posx == w-1 || posy == h-1 )
50
- {
51
- result[i] = img[i];
52
- }
53
- else
54
- {
55
- int pixel00, pixel01, pixel02, pixel10, pixel11, pixel12, pixel20,
56
- pixel21, pixel22;
57
- pixel00 = img[i - 1 - w];
58
- pixel01 = img[i- w];
59
- pixel02 = img[i + 1 - w];
60
- pixel10 = img[i - 1];
61
- pixel11 = img[i];
62
- pixel12 = img[i + 1];
63
- pixel20 = img[i - 1 + w];
64
- pixel21 = img[i + w];
65
- pixel22 = img[i + 1 + w];
66
- //sort the rows
67
- sort( &(pixel00), &(pixel01), &(pixel02) );
68
- sort( &(pixel10), &(pixel11), &(pixel12) );
69
- sort( &(pixel20), &(pixel21), &(pixel22) );
70
- //sort the columns
71
- sort( &(pixel00), &(pixel10), &(pixel20) );
72
- sort( &(pixel01), &(pixel11), &(pixel21) );
73
- sort( &(pixel02), &(pixel12), &(pixel22) );
74
- //sort the diagonal
75
- sort( &(pixel00), &(pixel11), &(pixel22) );
76
- // median is the the middle value of the diagonal
77
- result[i] = pixel11;
78
- }
79
- }
80
- """
81
-
82
- # Kernel function instantiation
83
- prg = cl.Program(ctx, src).build()
84
- # Allocate memory for variables on the device
85
- img_g = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=img)
86
- result_g = cl.Buffer(ctx, mf.WRITE_ONLY, img.nbytes)
87
- width_g = cl.Buffer(
88
- ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=np.int32(img.shape[1])
89
- )
90
- height_g = cl.Buffer(
91
- ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=np.int32(img.shape[0])
92
- )
93
- # Call Kernel. Automatically takes care of block/grid distribution
94
- prg.medianFilter(queue, img.shape, None, img_g, result_g, width_g, height_g)
95
- result = np.empty_like(img)
96
- cl.enqueue_copy(queue, result, result_g)
97
-
98
- # Show the blurred image
99
- imsave("medianFilter-OpenCL.jpg", result, mode="RGB")