abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
pyabc_split Namespace Reference

Data Structures

class  _splitter
 
class  abc_state
 

Functions

def _retry_select
 
def make_splitter
 
def split_all_full
 
def defer
 
def split_all
 
def temp_file_names
 
def abc_split_all
 
def f_1
 
def f_2
 
def f_3
 
def abc_f
 

Variables

tuple t_1 = (f_1, [1])
 
tuple t_2 = (f_2, [1,2])
 
tuple t_3 = (f_3, [1,2,3])
 
list funcs = [t_1, t_2, t_3]
 
 best = None
 

Detailed Description

module pyabc_split

Executes python functions and their arguements as separate processes and returns their return values through pickling. This modules offers a single function:

Function: split_all(funcs)

The function returns a generator objects that allowes iteration over the results,

Arguments:

funcs: a list of tuples (f, args) where f is a python function and args is a collection of arguments for f.

Caveats:

1. Global variables in the parent process are not affected by the child processes.
2. The functions can only return simple types, see the pickle module for details

Usage:

Assume you would like to run the function f_1(1), f_2(1,2), f_3(1,2,3) in different processes. 

def f_1(i):
return i+1

def f_2(i,j):
return i*10+j+1

def f_3(i,j,k):
return i*100+j*10+k+1

Construct a tuple of the function and arguments for each function

t_1 = (f_1, [1])
t_2 = (f_2, [1,2])
t_3 = (f_3, [1,2,3])

Create a list containing these tuples:

funcs = [t_1, t_2, t_3]

Use the function split_all() to run these functions in separate processes:

for res in split_all(funcs):
print res

The output will be:

2
13
124

(The order may be different, except that in this case the processes are so fast that they terminate before the next one is created)

Alternatively, you may quite in the middle, say after the first process returns:

for res in split_all(funcs):
print res
break

This will kill all processes not yet finished.

To run ABC operations, that required saving the child process state and restoring it at the parent, use abc_split_all().

import pyabc

def abc_f(truth):
    import os
    print "pid=%d, abc_f(%s)"%(os.getpid(), truth)
    pyabc.run_command('read_truth %s'%truth)
    pyabc.run_command('strash')
    
funcs = [
    defer(abc_f)("1000"),
    defer(abc_f)("0001")
]

for _ in abc_split_all(funcs):
    pyabc.run_command('write_verilog /dev/stdout')

Author: Baruch Sterin <sterin@berkeley.edu>

Function Documentation

def pyabc_split._retry_select (   rlist)
private

Definition at line 99 of file pyabc_split.py.

99 
100 def _retry_select(rlist):
101  while True:
102  try:
103  rrdy,_,_ = select.select(rlist,[],[])
104  if rrdy:
105  return rrdy
106  except select.error as e:
107  if e[0] == errno.EINTR:
108  continue
109  raise
def _retry_select
Definition: pyabc_split.py:99
def pyabc_split.abc_f (   truth)

Definition at line 388 of file pyabc_split.py.

389  def abc_f(truth):
390  import os
391  print "pid=%d, abc_f(%s)"%(os.getpid(), truth)
392  pyabc.run_command('read_truth %s'%truth)
393  pyabc.run_command('strash')
394  return 100
def pyabc_split.abc_split_all (   funcs)

Definition at line 329 of file pyabc_split.py.

330 def abc_split_all(funcs):
331  import pyabc
332 
333  def child(f, aig, log):
334  res = f()
335  pyabc.run_command(r'write_status %s'%log)
336  pyabc.run_command(r'write_aiger %s'%aig)
337  return res
338 
339  def parent(res, aig, log):
340  pyabc.run_command(r'read_aiger %s'%aig)
341  pyabc.run_command(r'read_status %s'%log)
342  return res
343 
344  with temp_file_names( ['.aig','.log']*len(funcs) ) as tmp:
345 
346  funcs = [ defer(child)(f, tmp[2*i],tmp[2*i+1]) for i,f in enumerate(funcs) ]
347 
348  for i, res in split_all_full(funcs):
349  yield i, parent(res, tmp[2*i],tmp[2*i+1])
def abc_split_all
Definition: pyabc_split.py:329
def split_all_full
Definition: pyabc_split.py:282
def temp_file_names
Definition: pyabc_split.py:301
def pyabc_split.defer (   f)

Definition at line 291 of file pyabc_split.py.

292 def defer(f):
293  return lambda *args, **kwargs: lambda : f(*args,**kwargs)
def pyabc_split.f_1 (   i)

Definition at line 354 of file pyabc_split.py.

355  def f_1(i):
356  return i+1
def pyabc_split.f_2 (   i,
  j 
)

Definition at line 357 of file pyabc_split.py.

358  def f_2(i,j):
359  return i*10+j+1
def pyabc_split.f_3 (   i,
  j,
  k 
)

Definition at line 360 of file pyabc_split.py.

361  def f_3(i,j,k):
362  return i*100+j*10+k+1
def pyabc_split.make_splitter ( )

Definition at line 274 of file pyabc_split.py.

275 def make_splitter():
276  # ensure cleanup of child processes
277  s = _splitter()
278  try:
279  yield s
280  finally:
281  s.cleanup()
def make_splitter
Definition: pyabc_split.py:274
def pyabc_split.split_all (   funcs)

Definition at line 294 of file pyabc_split.py.

295 def split_all(funcs):
296  for _, res in split_all_full( ( defer(f)(*args) for f,args in funcs ) ):
297  yield res
def split_all_full
Definition: pyabc_split.py:282
def pyabc_split.split_all_full (   funcs)

Definition at line 282 of file pyabc_split.py.

283 def split_all_full(funcs):
284  # provide an iterator for child process result
285  with make_splitter() as s:
286 
287  s.fork_all(funcs)
288 
289  for res in s:
290  yield res
def make_splitter
Definition: pyabc_split.py:274
def split_all_full
Definition: pyabc_split.py:282
def pyabc_split.temp_file_names (   suffixes)

Definition at line 301 of file pyabc_split.py.

302 def temp_file_names(suffixes):
303  names = []
304  try:
305  for suffix in suffixes:
306  with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as file:
307  names.append( file.name )
308  yield names
309  finally:
310  for name in names:
311  os.unlink(name)
def temp_file_names
Definition: pyabc_split.py:301

Variable Documentation

tuple pyabc_split.best = None

Definition at line 400 of file pyabc_split.py.

list pyabc_split.funcs = [t_1, t_2, t_3]

Definition at line 371 of file pyabc_split.py.

tuple pyabc_split.t_1 = (f_1, [1])

Definition at line 365 of file pyabc_split.py.

tuple pyabc_split.t_2 = (f_2, [1,2])

Definition at line 366 of file pyabc_split.py.

tuple pyabc_split.t_3 = (f_3, [1,2,3])

Definition at line 367 of file pyabc_split.py.