Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagesmc
Path: blob/master/src/sage/parallel/reference.py
8814 views
1
"""
2
Reference Parallel Primitives
3
4
These are reference implementations of basic parallel
5
primitives. These are not actually parallel, but work the same way.
6
They are good for testing.
7
"""
8
9
from sage.misc.prandom import shuffle
10
11
def parallel_iter(f, inputs):
12
"""
13
Reference parallel iterator implementation.
14
15
INPUT:
16
17
- ``f`` -- a Python function that can be pickled using
18
the pickle_function command.
19
20
- ``inputs`` -- a list of pickleable pairs (args, kwds), where
21
args is a tuple and kwds is a dictionary.
22
23
OUTPUT:
24
25
- iterator over 2-tuples ``(inputs[i], f(inputs[i]))``, where the
26
order may be completely random
27
28
EXAMPLES::
29
30
sage: def f(N,M=10): return N*M
31
sage: inputs = [((2,3),{}), (tuple([]), {'N':3,'M':5}), ((2,),{})]
32
sage: set_random_seed(0)
33
sage: for a, val in sage.parallel.reference.parallel_iter(f, inputs):
34
... print a, val
35
((2,), {}) 20
36
((), {'M': 5, 'N': 3}) 15
37
((2, 3), {}) 6
38
sage: for a, val in sage.parallel.reference.parallel_iter(f, inputs):
39
... print a, val
40
((), {'M': 5, 'N': 3}) 15
41
((2,), {}) 20
42
((2, 3), {}) 6
43
"""
44
v = list(inputs)
45
shuffle(v)
46
for args, kwds in v:
47
yield ((args, kwds), f(*args, **kwds))
48
49