forked from m0mchil/poclbm
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDeviceFinder.py
140 lines (112 loc) · 3.41 KB
/
DeviceFinder.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/python
import sys
import logging
import re
if sys.version_info < (2, 7):
print 'Sorry: you must use Python 2.7'
sys.exit(1)
try:
import pyopencl as cl
except ImportError:
cl = None
class Device(object):
TYPE_UNKNOWN = 'Unknown'
TYPE_CPU_C = 'CPU (c)'
TYPE_CPU_SSE2 = 'CPU (SSE2)'
TYPE_OPENCL = 'OpenCL'
def __init__(self, name, number, type_=TYPE_UNKNOWN):
self._name = name
self._number = number
self._type = type_
def getName(self):
return self._name
def getNumber(self):
return self._number
def getType(self):
return self._type
def __str__(self):
return 'Device "({numnber} {name})" of type "{type_}"'.format(
name=self.name,
number=self.number,
type_=self._type
)
class CpuDevice(Device):
def __init__(self, name, number, cpuNumber, sse=False, sse2=False):
if sse2:
Device.__init__(self, name, number, Device.TYPE_CPU_SSE2)
else:
Device.__init__(self, name, number, Device.TYPE_CPU_C)
self._cpuNumber = cpuNumber
self.sse = sse
self.sse2 = sse2
class OpenCLDevice(Device):
def __init__(self, name, number, openCl):
Device.__init__(self, name, number, Device.TYPE_OPENCL)
self._openCl = openCl
def getOpenCL(self):
return self._openCl
class DeviceList(object):
def __init__(self):
self.logger = logging.getLogger("DeviceList")
self._devices = []
if cl:
self._populateOpenCl()
else:
self.logger.warn('No OpenCL libary')
self._populateCpus()
def __getitem__(self, key):
if type(key) != type(int()):
raise TypeError("Indexed by int")
if key < 0 or key >= len(self._devices):
raise IndexError("Index out of range")
return self._devices[key]
def __len__(self):
return len(self._devices)
def showAllDevices(self):
print '# | Type | Name'
print '---+------------+--' + '-'*30
for device in self._devices:
print '{num:<2} | {type_:<10} | {name}'.format(
num=device.getNumber(),
type_=device.getType(),
name=device.getName()
)
print '---+------------+--' + '-'*30
def _populateOpenCl(self):
for platform in cl.get_platforms():
for device in platform.get_devices():
self._devices.append( OpenCLDevice(device.name, len(self._devices), device) )
def _populateCpus(self):
try:
proc = open("/proc/cpuinfo", "r")
except IOError as e:
self._cpus.append( Cpu("Unknown (Only assumed)", 0) )
return
sse = False
sse2 = False
for line in proc:
if line == "\n":
cpu = CpuDevice(name, len(self._devices), number, sse=sse, sse2=sse2)
self._devices.append(cpu)
sse = False
sse2 = False
continue
match = re.match('processor\t*: (?P<number>[0-9]+)', line)
if match != None:
number = match.group("number")
continue
match = re.match('model name\t*: (?P<name>.*)$', line)
if match != None:
name = match.group("name")
continue
match = re.match('flags\t*: (?P<flags>.*)$', line)
if match != None:
flags = match.groups("flags")[0].split(" ")
for flag in flags:
if flag == "sse":
sse = True
elif flag == "sse2":
sse2 = True
continue
if __name__ == '__main__':
print "This is only a module and can not be run as a program"