assertOpenCL  September 19, 2018
opencl_infos.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- coding: latin-1 -*-
3 """
4 Print information on all OpenCL platforms available and all its devices.
5 
6 :license: GPLv3 --- Copyright (C) 2018 Olivier Pirson
7 :author: Olivier Pirson --- http://www.opimedia.be/
8 :version: September 10, 2018
9 """
10 
11 from __future__ import division
12 from __future__ import print_function
13 
14 import sys
15 
16 import pyopencl as cl
17 
18 if sys.version_info[0] >= 3:
19  long = int
20 
21 
22 def help_quit(error):
23  """
24  Print the help message on stderr
25  and exit with the error code.
26 
27  :param error: 0 <= int < 256
28  """
29  assert isinstance(error, int), type(error)
30  assert 0 <= error < 256, error
31 
32  print("""Usage: opencl_infos.py [--complete]
33 Print information on all OpenCL platforms available and all its devices.
34 
35 Option:
36  --complete all information about device, else only main information""",
37  file=sys.stderr)
38  exit(error)
39 
40 
41 def print_device(device, complete):
42  """
43  Print information about the device.
44 
45  :param device: pyopencl.Device
46  :param complete: bool
47  """
48  assert isinstance(device, cl.Device), type(device)
49  assert isinstance(complete, bool), type(complete)
50 
51  print(' Device name: "{}"'.format(device.name))
52 
53  attributes = (tuple(sorted(dir(device))) if complete
54  else ('global_mem_cache_size', 'global_mem_size',
55  'local_mem_size',
56  'max_clock_frequency',
57  'max_compute_units',
58  'max_constant_buffer_size',
59  'max_mem_alloc_size',
60  'max_work_group_size', 'max_work_item_sizes',
61  'opencl_c_version'))
62  for attribute in attributes:
63  if (attribute[:1] != '_') and (attribute != 'name'):
64  try:
65  value = getattr(device, attribute)
66  except cl.LogicError:
67  continue
68 
69  attribute = attribute.replace('_', ' ')
70  if isinstance(value, int) or isinstance(value, long):
71  print(' {}: {}'
72  .format(attribute,
73  (size_units(value) if attribute[-4:] == 'size'
74  else value)))
75  elif isinstance(value, list) or isinstance(value, tuple):
76  print(' {}: {}'
77  .format(attribute, ' '.join(str(v) for v in value)))
78  elif isinstance(value, str):
79  print(' {}: "{}"'.format(attribute, value))
80 
81 
82 def print_platform(platform, platform_id, complete):
83  """
84  Print information about the platform and its devices.
85 
86  :param platform: pyopencl.Platform
87  :param platform_id: int >= 0
88  :param complete: bool
89  """
90  assert isinstance(platform, cl.Platform), type(platform)
91 
92  assert isinstance(platform_id, int), type(platform_id)
93  assert platform_id >= 0, platform_id
94 
95  assert isinstance(complete, bool), type(complete)
96 
97  print('Platform name: "{}"'.format(platform.name))
98  print(' vendor: "{}"'.format(platform.vendor))
99  print(' version: "{}"'.format(platform.version))
100  print(' profile: "{}"'.format(platform.profile))
101  print(' extensions: "{}"'.format(platform.extensions))
102 
103  devices = platform.get_devices()
104  nb_device = len(devices)
105  print(' {} device{}'.format(nb_device, s(nb_device)))
106 
107  for device_id, device in enumerate(devices):
108  print(' ', '{}:{}'.format(platform_id, device_id), '-' * 30)
109  print_device(device, complete)
110 
111 
112 def s(x):
113  """
114  If x >= 2 then return 's',
115  else return ''.
116 
117  :param x: int or long or float
118 
119  :return: 's' or ''
120  """
121  assert isinstance(x, int) or isinstance(x, long) or isinstance(x, float), type(x)
122 
123  return ('s' if x >= 2
124  else '')
125 
126 
127 def size_units(n):
128  """
129  Return a string with the size n expressed in several units.
130 
131  :param: (int or long) >= 0
132 
133  :return: str
134  """
135  assert isinstance(n, int) or isinstance(n, long), type(n)
136  assert n >= 0, n
137 
138  seq = ['{} o'.format(n)]
139  units = 'kMGT'
140 
141  while True:
142  n //= 1024
143  unit, units = units[0], units[1:]
144  if n <= 0:
145  break
146 
147  seq.append('{} {}io'.format(n, unit))
148 
149  return ' = '.join(seq)
150 
151 
152 ########
153 # Main #
154 ########
155 def main():
156  """
157  Print information on all OpenCL platforms
158  and its devices.
159  """
160  complete = False
161  for arg in sys.argv[1:]:
162  if arg == '--complete':
163  complete = True
164  elif arg == '--help':
165  help_quit(0)
166  else:
167  help_quit(1)
168 
169  print('Python version:', sys.version.replace('\n', ' '))
170  print('PyOpenCL version:', cl.VERSION_TEXT)
171 
172  platforms = cl.get_platforms()
173 
174  nb_platform = len(platforms)
175  print('{} OpenCL platform{}'.format(nb_platform, s(nb_platform)))
176 
177  for platform_id, platform in enumerate(platforms):
178  print('=' * 40)
179  print_platform(platform, platform_id, complete)
180 
181 if __name__ == '__main__':
182  main()
def print_platform(platform, platform_id, complete)
Definition: opencl_infos.py:82
def help_quit(error)
Definition: opencl_infos.py:22
def print_device(device, complete)
Definition: opencl_infos.py:41
def size_units(n)
def main()
Main #.