5 Simple Python (2 and 3) example 6 to show how run a kernel with assert*() and PRINT*() macros 11 :license: GPLv3 --- Copyright (C) 2018 Olivier Pirson 12 :author: Olivier Pirson --- http://www.opimedia.be/ 13 :version: September 19, 2018 16 from __future__
import division
17 from __future__
import print_function
28 Return the given device of the given platform OpenCL. 30 :param platform_i: int >= 0 31 :param device_i: int >= 0 33 :return: None or OpenCL device 35 assert isinstance(platform_i, int), type(platform_i)
36 assert platform_i >= 0, platform_i
38 assert isinstance(device_i, int), type(device_i)
39 assert device_i >= 0, device_i
41 platforms = cl.get_platforms()
42 if platform_i >= len(platforms):
45 devices = platforms[platform_i].get_devices()
46 if device_i >= len(devices):
49 return devices[device_i]
52 def run_example(nb_work_group, nb_work_items_by_work_group,
53 device=None, debug=__debug__):
55 Run the kernel ../kernel/example.cl. 58 then use the default device, 62 then run the kernel in debug mode, 63 else run the kernel with the macro NDEBUG defined. 65 :param nb_work_group: int > 0 66 :param nb_work_items_by_work_group: int > 0 67 :param device: None or OpenCL device 70 assert isinstance(nb_work_group, int), type(nb_work_group)
71 assert nb_work_group > 0, nb_work_group
73 assert isinstance(nb_work_items_by_work_group, int), \
74 type(nb_work_items_by_work_group)
75 assert nb_work_items_by_work_group > 0, nb_work_items_by_work_group
77 assert isinstance(debug, bool), type(debug)
80 h_outs = np.empty(2).astype(np.uint32)
83 context = (cl.create_some_context()
if device
is None 84 else cl.Context((device, )))
87 path = os.path.dirname(__file__)
88 options = [
'-I', os.path.join(path,
'../../OpenCL/')]
90 print(
'OpenCL in DEBUG mode!', file=sys.stderr)
93 options.extend((
'-D',
'NDEBUG'))
95 kernel_filename = os.path.join(path,
'../kernel/example.cl')
96 kernel_src = (
'#line 1 "{}"\n{}' 97 .format(kernel_filename,
98 open(os.path.join(path,
'../kernel/example.cl'))
100 program = cl.Program(context, kernel_src).build(options=options)
103 queue = cl.CommandQueue(context,
104 properties=cl.command_queue_properties
109 d_outs = cl.Buffer(context, mf.WRITE_ONLY | mf.COPY_HOST_PTR,
113 type_params = [np.uint32,
None]
114 params = [666, d_outs]
116 h_asserts = np.zeros(2).astype(np.uint64)
117 h_assert_float = np.zeros(1).astype(np.float32)
119 d_asserts = cl.Buffer(context, mf.READ_WRITE | mf.COPY_HOST_PTR,
121 d_assert_float = cl.Buffer(context, mf.READ_WRITE | mf.COPY_HOST_PTR,
122 hostbuf=h_assert_float)
124 type_params.extend((
None,
None))
125 params.extend((d_asserts, d_assert_float))
128 global_size = nb_work_items_by_work_group * nb_work_group
129 kernel_f = program.example
130 kernel_f.set_scalar_arg_dtypes(type_params)
131 print(
'===== run kernel =====')
134 kernel_f(queue, (global_size, ), (nb_work_items_by_work_group, ), *params)
140 print(
'===== end kernel =====')
144 cl.enqueue_copy(queue, h_outs, d_outs)
147 cl.enqueue_copy(queue, h_asserts, d_asserts)
148 cl.enqueue_copy(queue, h_assert_float, d_assert_float)
150 line = int(h_asserts[0])
152 uint64_value = int(h_asserts[1])
153 sint64_value = int(h_asserts[1].astype(np.int64))
154 float_value = float(h_assert_float[0])
155 print(
'{}:{}\tAssertion failed | Maybe\t{}\t{} | Maybe\t{}' 156 .format(kernel_filename, line,
157 uint64_value, sint64_value,
166 print(
'Results:', tuple(h_outs))
174 Get the optional parameter --device platform:device 175 and run the kernel ../kernel/example.cl 182 while i < len(sys.argv):
184 if (arg ==
'--debug')
or (arg ==
'--ndebug'):
185 debug = (arg ==
'--debug')
186 elif arg ==
'--device':
189 both_i = [arg
for arg
in sys.argv[i].split(
':')]
190 platform_i = int(both_i[0])
191 device_i = (int(both_i[1])
if len(both_i) >= 2
193 if ((platform_i >= 0)
and (device_i >= 0)
and 198 except (IndexError, ValueError):
205 print(
'Device default')
207 print(
'Device {}:{} {}'.format(platform_i, device_i, device.name))
213 if __name__ ==
'__main__':
def run_example(nb_work_group, nb_work_items_by_work_group, device=None, debug=__debug__)
def get_device(platform_i, device_i)