hwloc 0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,395 @@
1
+ module Hwloc
2
+
3
+ def self.const_missing( sym )
4
+ value = enum_value( sym )
5
+
6
+ return super unless value
7
+
8
+ value
9
+ end
10
+
11
+ class TopologyDiscoverySupport < BoolStruct
12
+ layout :pu, :uchar
13
+ end
14
+
15
+ class TopologyCpubindSupport < BoolStruct
16
+ layout :set_thisproc_cpubind, :uchar,
17
+ :get_thisproc_cpubind, :uchar,
18
+ :set_proc_cpubind, :uchar,
19
+ :get_proc_cpubind, :uchar,
20
+ :set_thisthread_cpubind, :uchar,
21
+ :get_thisthread_cpubind, :uchar,
22
+ :set_thread_cpubind, :uchar,
23
+ :get_thread_cpubind, :uchar,
24
+ :get_thisproc_last_cpu_location, :uchar,
25
+ :get_proc_last_cpu_location, :uchar,
26
+ :get_thisthread_last_cpu_location, :uchar
27
+ end
28
+
29
+ class TopologyMemSupport < BoolStruct
30
+ layout :set_thisproc_membind, :uchar,
31
+ :get_thisproc_membind, :uchar,
32
+ :set_proc_membind, :uchar,
33
+ :get_proc_membind, :uchar,
34
+ :set_thisthread_membind, :uchar,
35
+ :get_thisthread_membind, :uchar,
36
+ :set_area_membind, :uchar,
37
+ :get_area_membind, :uchar,
38
+ :alloc_membind, :uchar,
39
+ :firsttouch_membind, :uchar,
40
+ :bind_membind, :uchar,
41
+ :interleave_membind, :uchar,
42
+ :replicate_membind, :uchar,
43
+ :nexttouch_membind, :uchar,
44
+ :migrate_membind, :uchar,
45
+ :get_area_memlocation, :uchar
46
+ end
47
+
48
+ class TopologySupport < Struct
49
+ layout :discovery, TopologyDiscoverySupport.ptr,
50
+ :cpubind, TopologyCpubindSupport.ptr,
51
+ :membind, TopologyMemSupport.ptr
52
+ end
53
+
54
+ typedef :pointer, :topology
55
+ typedef :pthread_t, :hwloc_thread_t
56
+ typedef :pid_t, :hwloc_pid_t
57
+
58
+ attach_function :hwloc_topology_init, [:pointer], :int
59
+ attach_function :hwloc_topology_load, [:topology], :int
60
+ attach_function :hwloc_topology_destroy, [:topology], :void
61
+ attach_function :hwloc_topology_dup, [:pointer, :topology], :int
62
+ attach_function :hwloc_topology_check, [:topology], :void
63
+
64
+ attach_function :hwloc_topology_ignore_type, [:topology, :obj_type], :int
65
+ attach_function :hwloc_topology_ignore_type_keep_structure, [:topology, :obj_type], :int
66
+ attach_function :hwloc_topology_ignore_all_keep_structure, [:topology], :int
67
+
68
+ TopologyFlags = enum( FFI::find_type(:ulong), :topology_flags, [
69
+ :TOPOLOGY_FLAG_WHOLE_SYSTEM, 1<<0,
70
+ :TOPOLOGY_FLAG_IS_THISSYSTEM, 1<<1,
71
+ :TOPOLOGY_FLAG_IO_DEVICES, 1<<2,
72
+ :TOPOLOGY_FLAG_IO_BRIDGES, 1<<3,
73
+ :TOPOLOGY_FLAG_WHOLE_IO, 1<<4,
74
+ :TOPOLOGY_FLAG_ICACHES, 1<<5
75
+ ] )
76
+
77
+ attach_function :hwloc_topology_set_flags, [:topology, :ulong], :int
78
+ attach_function :hwloc_topology_get_flags, [:topology], :ulong
79
+
80
+ attach_function :hwloc_topology_set_pid, [:topology, :hwloc_pid_t], :int
81
+ attach_function :hwloc_topology_set_fsroot, [:topology, :string], :int
82
+ attach_function :hwloc_topology_set_synthetic, [:topology, :string], :int
83
+ attach_function :hwloc_topology_set_xml, [:topology, :string], :int
84
+ attach_function :hwloc_topology_set_xmlbuffer, [:topology, :pointer, :size_t], :int
85
+ attach_function :hwloc_topology_set_custom, [:topology], :int
86
+ attach_function :hwloc_topology_set_distance_matrix, [:topology, :obj_type, :uint, :pointer, :pointer], :int
87
+ attach_function :hwloc_topology_is_thissystem, [:topology], :int
88
+
89
+ attach_function :hwloc_topology_get_support, [:topology], TopologySupport.ptr
90
+
91
+ attach_function :hwloc_topology_get_depth, [:topology], :uint
92
+
93
+ GetTypeDepth = enum(:get_type_depth, [
94
+ :TYPE_DEPTH_UNKNOWN, -1,
95
+ :TYPE_DEPTH_MULTIPLE, -2,
96
+ :TYPE_DEPTH_BRIDGE, -3,
97
+ :TYPE_DEPTH_PCI_DEVICE, -4,
98
+ :TYPE_DEPTH_OS_DEVICE, -5
99
+ ] )
100
+
101
+ attach_function :hwloc_get_type_depth, [:topology, :obj_type], :int
102
+ attach_function :hwloc_get_depth_type, [:topology, :uint], :obj_type
103
+ attach_function :hwloc_get_nbobjs_by_depth, [:topology, :uint], :uint
104
+
105
+ attach_function :hwloc_get_obj_by_depth, [:topology, :uint, :uint], Obj.ptr
106
+
107
+ end
108
+
109
+ module Hwloc
110
+
111
+ class TopologyError < Error
112
+ end
113
+
114
+ class Topology
115
+ include Enumerable
116
+
117
+ def self.const_missing( sym )
118
+ begin
119
+ value = Hwloc.const_get( "TOPOLOGY_#{sym}".to_sym )
120
+ return value
121
+ rescue
122
+ end
123
+ super
124
+ end
125
+
126
+ Flags = TopologyFlags
127
+
128
+ attr_reader :ptr
129
+
130
+ def inspect
131
+ return "#<#{self.class}:#{"0x00%x" % (object_id << 1)}>"
132
+ end
133
+
134
+ def initialize( *args )
135
+ if args.length == 0 then
136
+ ptr = FFI::MemoryPointer::new( :pointer )
137
+ err = Hwloc.hwloc_topology_init(ptr)
138
+ raise TopologyError if err == -1
139
+ @ptr = FFI::AutoPointer::new( ptr.read_pointer, Hwloc.method(:hwloc_topology_destroy) )
140
+ elsif args.length == 1 then
141
+ arg = args[0]
142
+ if arg.kind_of?( Topology ) then
143
+ ptr = FFI::MemoryPointer::new( :pointer )
144
+ err = Hwloc.hwloc_topology_dup(ptr, arg.ptr)
145
+ raise TopologyError if err == -1
146
+ @ptr = FFI::AutoPointer::new( ptr.read_pointer, Hwloc.method(:hwloc_topology_destroy) )
147
+ else
148
+ raise TopologyError, "Invalid argument"
149
+ end
150
+ else
151
+ raise TopologyError, "Invalid argument"
152
+ end
153
+ end
154
+
155
+ def load
156
+ err = Hwloc.hwloc_topology_load(@ptr)
157
+ raise TopologyError if err == -1
158
+ return self
159
+ end
160
+
161
+ def dup
162
+ return Topology::new( self )
163
+ end
164
+
165
+ def check
166
+ Hwloc.hwloc_topology_check(@ptr)
167
+ return self
168
+ end
169
+
170
+ def ignore_type(type)
171
+ err = Hwloc.hwloc_topology_ignore_type(@ptr, type)
172
+ raise TopologyError if err == -1
173
+ return self
174
+ end
175
+
176
+ def ignore_type_keep_structure(type)
177
+ if type == :all then
178
+ err = Hwloc.hwloc_topology_ignore_all_keep_structure(@ptr)
179
+ raise TopologyError if err == -1
180
+ else
181
+ err = Hwloc.hwloc_topology_ignore_type_keep_structure(@ptr, type)
182
+ raise TopologyError if err == -1
183
+ end
184
+ return self
185
+ end
186
+
187
+ def set_flags(flags)
188
+ err = Hwloc.hwloc_topology_set_flags(@ptr, flags)
189
+ raise TopologyError if err == -1
190
+ return self
191
+ end
192
+
193
+ alias flags= set_flags
194
+
195
+ def get_flags
196
+ Hwloc.hwloc_topology_get_flags(@ptr)
197
+ end
198
+
199
+ alias flags get_flags
200
+
201
+ def set_pid(pid)
202
+ err = Hwloc.hwloc_topology_set_pid(@ptr, pid)
203
+ raise TopologyError if err == -1
204
+ return self
205
+ end
206
+
207
+ def set_fsroot(fsroot_path)
208
+ err = Hwloc.hwloc_topology_set_fsroot(@ptr, fsroot_path)
209
+ raise TopologyError if err == -1
210
+ return self
211
+ end
212
+
213
+ def set_synthetic(description)
214
+ err = Hwloc.hwloc_topology_set_synthetic(@ptr, description)
215
+ raise TopologyError if err == -1
216
+ return self
217
+ end
218
+
219
+ def set_xml(xmlpath)
220
+ err = Hwloc.hwloc_topology_set_xml(@ptr, xmlpath)
221
+ raise TopologyError if err == -1
222
+ return self
223
+ end
224
+
225
+ def set_xmlbuffer(pointer)
226
+ err = Hwloc.hwloc_topology_set_xmlbuffer(@ptr, pointer, pointer.size)
227
+ raise TopologyError if err == -1
228
+ return self
229
+ end
230
+
231
+ def set_custom
232
+ err = Hwloc.hwloc_topology_set_custom(@ptr)
233
+ raise TopologyError if err == -1
234
+ return self
235
+ end
236
+
237
+ ## Will need some work to define properly...
238
+ # def set_distance_matrix(type, nbobjs, os_index, distances)
239
+ # err = Hwloc.hwloc_topology_set_distance_matrix(@ptr, type, nbobjs, os_index, distances)
240
+ # raise TopologyError if err == -1
241
+ # return self
242
+ # end
243
+
244
+ def is_thissystem
245
+ return Hwloc.hwloc_topology_is_thissystem(@ptr) == 1
246
+ end
247
+
248
+ alias thissystem? is_thissystem
249
+
250
+ def get_support
251
+ p = Hwloc.hwloc_topology_get_support(@ptr)
252
+ p.instance_variable_set(:@topology, self)
253
+ return p
254
+ end
255
+
256
+ alias support get_support
257
+
258
+ def get_depth
259
+ Hwloc.hwloc_topology_get_depth(@ptr)
260
+ end
261
+
262
+ alias depth get_depth
263
+
264
+ def get_type_depth(type)
265
+ Hwloc.hwloc_get_type_depth(@ptr, type)
266
+ end
267
+
268
+ def get_depth_type(depth)
269
+ type = Hwloc.hwloc_get_depth_type(@ptr, depth)
270
+ raise TopologyError, "Invalid argument" if type == -1
271
+ return type
272
+ end
273
+
274
+ def get_type_or_below_depth(type)
275
+ depth = get_type_depth(type)
276
+ return depth if depth != Hwloc::TYPE_DEPTH_UNKNOWN
277
+ depth = get_type_depth(Hwloc::OBJ_PU)
278
+ while depth >= 0 do
279
+ return depth + 1 if Hwloc.compare_types(get_depth_type(depth), type) < 0
280
+ depth -= 1
281
+ end
282
+ raise TopologyError
283
+ end
284
+
285
+ def get_type_or_above_depth(type)
286
+ depth = get_type_depth(type)
287
+ return depth if depth != Hwloc::TYPE_DEPTH_UNKNOWN
288
+ depth = 0
289
+ while depth <= get_type_depth(Hwloc::OBJ_PU) do
290
+ return depth - 1 if Hwloc.compare_types(get_depth_type(depth), type) > 0
291
+ depth += 1
292
+ end
293
+ raise TopologyError
294
+ end
295
+
296
+ def get_nbobjs_by_depth(depth)
297
+ return Hwloc.hwloc_get_nbobjs_by_depth(@ptr, depth)
298
+ end
299
+
300
+ def get_nbobjs_by_type(type)
301
+ depth = get_type_depth(type)
302
+ return 0 if depth == Hwloc::TYPE_DEPTH_UNKNOWN
303
+ return each_obj.select{ |e| e.type == type }.count if depth == Hwloc::TYPE_DEPTH_MULTIPLE
304
+ return Hwloc.hwloc_get_nbobjs_by_depth(@ptr, depth)
305
+ end
306
+
307
+ def get_obj_by_depth(depth, idx)
308
+ p = Hwloc.hwloc_get_obj_by_depth(@ptr, depth, idx)
309
+ return nil if p.to_ptr.null?
310
+ p.instance_variable_set(:@topology, self)
311
+ return p
312
+ end
313
+
314
+ def get_root_obj
315
+ return get_obj_by_depth(0, 0)
316
+ end
317
+
318
+ alias root_obj get_root_obj
319
+
320
+ def get_obj_by_type(type, idx)
321
+ depth = get_type_depth(type)
322
+ return nil if depth == Hwloc::TYPE_DEPTH_UNKNOWN
323
+ return each_obj.select{ |e| e.type == type }[idx] if depth == Hwloc::TYPE_DEPTH_MULTIPLE
324
+ return get_obj_by_depth(depth, idx)
325
+ end
326
+
327
+ def get_next_obj_by_depth(depth, prev)
328
+ return get_obj_by_depth(depth, 0) if prev.nil?
329
+ return nil if prev.depth != depth
330
+ return prev.next_cousin
331
+ end
332
+
333
+ def get_next_obj_by_type(type, prev)
334
+ depth = get_type_depth(type)
335
+ return nil if depth == Hwloc::TYPE_DEPTH_UNKNOWN
336
+ if depth == Hwloc::TYPE_DEPTH_MULTIPLE then
337
+ list = each_obj.select{ |e| e.type == type }
338
+ return list[list.find_index { |e| e.to_ptr == e.to_ptr } + 1]
339
+ end
340
+ return get_next_obj_by_depth(depth, prev)
341
+ end
342
+
343
+ def each_by_depth(depth)
344
+ if block_given? then
345
+ idx = 0
346
+ while o = get_obj_by_depth(depth, idx) do
347
+ yield o
348
+ idx += 1
349
+ end
350
+ return self
351
+ else
352
+ return Enumerator::new do |yielder|
353
+ idx = 0
354
+ while o = get_obj_by_depth(depth, idx) do
355
+ yielder << o
356
+ idx += 1
357
+ end
358
+ end
359
+ end
360
+ end
361
+
362
+ def each_by_type(type, &block)
363
+ depth = get_type_depth(type)
364
+ return each_obj.select{ |e| e.type == type }.each(&block) if depth == Hwloc::TYPE_DEPTH_MULTIPLE
365
+ return each_by_depth(depth, &block)
366
+ end
367
+
368
+ ObjType.symbols[0..-1].each { |sym|
369
+ methname = "each_"
370
+ suffix = sym.to_s[4..-1].downcase
371
+ methname += suffix
372
+ define_method(methname) { |&block|
373
+ each_by_type(sym, &block)
374
+ }
375
+ define_method(suffix+"s") {
376
+ send(methname).to_a
377
+ }
378
+ }
379
+
380
+ def each_obj(&block)
381
+ if block then
382
+ obj = get_root_obj
383
+ obj.each_obj(&block)
384
+ return self
385
+ else
386
+ to_enum(:each_obj)
387
+ end
388
+ end
389
+
390
+ alias traverse each_obj
391
+ alias each each_obj
392
+
393
+ end
394
+
395
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hwloc
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ platform: ruby
6
+ authors:
7
+ - Brice Videau
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-04-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ffi
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.9'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 1.9.3
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '1.9'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 1.9.3
33
+ description: hwloc ruby bindings for versions 1.10 onward
34
+ email: brice.videau@imag.fr
35
+ executables: []
36
+ extensions: []
37
+ extra_rdoc_files: []
38
+ files:
39
+ - LICENSE
40
+ - README.md
41
+ - hwloc.gemspec
42
+ - lib/hwloc.rb
43
+ - lib/hwloc/Bind.rb
44
+ - lib/hwloc/Bitmap.rb
45
+ - lib/hwloc/Edition.rb
46
+ - lib/hwloc/Export.rb
47
+ - lib/hwloc/Hwloc.rb
48
+ - lib/hwloc/Obj.rb
49
+ - lib/hwloc/Topology.rb
50
+ homepage: https://github.com/Nanosim-LIG/hwloc-ruby
51
+ licenses:
52
+ - BSD-2-Clause
53
+ metadata: {}
54
+ post_install_message:
55
+ rdoc_options: []
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: 1.9.3
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ requirements: []
69
+ rubyforge_project:
70
+ rubygems_version: 2.5.2
71
+ signing_key:
72
+ specification_version: 4
73
+ summary: hwloc ruby bindings
74
+ test_files: []