diff options
| author | Ralf Luther <luther.ralf@gmail.com> | 2019-03-27 20:23:17 +0000 |
|---|---|---|
| committer | Gerrit Code Review <gerrit2@aicp-server-3> | 2019-03-27 20:23:17 +0000 |
| commit | 1ce3a9d272e564b22a1333a1e36a3d3ab7cfab01 (patch) | |
| tree | 391382eadd4fec5bb480f2e8934fa352770221d1 /clang-r353983/share/opt-viewer/optpmap.py | |
| parent | d1d48b140bafaa8a50107292f5fce95562575765 (diff) | |
| parent | 4f56932d3416ac03f646bc1a611b3135fec2fe08 (diff) | |
Merge "Update prebuilt Clang to r353983." into p9.0HEADp9.0-backupp9.0
Diffstat (limited to 'clang-r353983/share/opt-viewer/optpmap.py')
| -rwxr-xr-x | clang-r353983/share/opt-viewer/optpmap.py | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/clang-r353983/share/opt-viewer/optpmap.py b/clang-r353983/share/opt-viewer/optpmap.py new file mode 100755 index 00000000..8124c8c9 --- /dev/null +++ b/clang-r353983/share/opt-viewer/optpmap.py @@ -0,0 +1,56 @@ +import sys +import multiprocessing + + +_current = None +_total = None + + +def _init(current, total): + global _current + global _total + _current = current + _total = total + + +def _wrapped_func(func_and_args): + func, argument, should_print_progress, filter_ = func_and_args + + if should_print_progress: + with _current.get_lock(): + _current.value += 1 + sys.stdout.write('\r\t{} of {}'.format(_current.value, _total.value)) + sys.stdout.flush() + + return func(argument, filter_) + + +def pmap(func, iterable, processes, should_print_progress, filter_=None, *args, **kwargs): + """ + A parallel map function that reports on its progress. + + Applies `func` to every item of `iterable` and return a list of the + results. If `processes` is greater than one, a process pool is used to run + the functions in parallel. `should_print_progress` is a boolean value that + indicates whether a string 'N of M' should be printed to indicate how many + of the functions have finished being run. + """ + global _current + global _total + _current = multiprocessing.Value('i', 0) + _total = multiprocessing.Value('i', len(iterable)) + + func_and_args = [(func, arg, should_print_progress, filter_) for arg in iterable] + if processes == 1: + result = list(map(_wrapped_func, func_and_args, *args, **kwargs)) + else: + pool = multiprocessing.Pool(initializer=_init, + initargs=(_current, _total,), + processes=processes) + result = pool.map(_wrapped_func, func_and_args, *args, **kwargs) + pool.close() + pool.join() + + if should_print_progress: + sys.stdout.write('\r') + return result |
