
from __future__ import division

import sys

from util import timer

from distarray.dist import Context, Distribution, hypot
from distarray.dist.random import Random


context = Context()
random = Random(context)


def local_sum(mask):
    return mask.ndarray.sum()


@timer
def calc_pi(n):
    """Estimate pi using distributed NumPy arrays."""
    distribution = Distribution.from_shape(context=context, shape=(n,))
    x = random.rand(distribution)
    y = random.rand(distribution)
    r = hypot(x, y)
    mask = (r < 1)
    lsum = context.apply(local_sum, (mask.key,))
    return 4 * sum(lsum) / n


if __name__ == '__main__':
    N = int(sys.argv[1])
    result, time = calc_pi(N)
    print('time  : %3.4g\nresult: %.7f' % (time, result))


from __future__ import division, print_function

import sys

from util import timer

from distarray.dist import Context, Distribution, hypot
from distarray.dist.random import Random


context = Context()
random = Random(context)


@timer
def calc_pi(n):
    """Estimate pi using distributed NumPy arrays."""
    distribution = Distribution.from_shape(context=context, shape=(n,))
    x = random.rand(distribution)
    y = random.rand(distribution)
    r = hypot(x, y)
    mask = (r < 1)
    return 4 * mask.sum().toarray() / n


if __name__ == '__main__':
    N = int(sys.argv[1])
    result, time = calc_pi(N)
    print('time  : %3.4g\nresult: %.7f' % (time, result))

Update pi-montecarlo example to use `sum` again.
Update pi-montecarlo example to use `sum` again.

  
- from __future__ import division
+ from __future__ import division, print_function
  
  import sys
  
  from util import timer
  
  from distarray.dist import Context, Distribution, hypot
  from distarray.dist.random import Random
  
  
  context = Context()
  random = Random(context)
  
  
- def local_sum(mask):
-     return mask.ndarray.sum()
- 
- 
  @timer
  def calc_pi(n):
      """Estimate pi using distributed NumPy arrays."""
      distribution = Distribution.from_shape(context=context, shape=(n,))
      x = random.rand(distribution)
      y = random.rand(distribution)
      r = hypot(x, y)
      mask = (r < 1)
+     return 4 * mask.sum().toarray() / n
-     lsum = context.apply(local_sum, (mask.key,))
-     return 4 * sum(lsum) / n
  
  
  if __name__ == '__main__':
      N = int(sys.argv[1])
      result, time = calc_pi(N)
      print('time  : %3.4g\nresult: %.7f' % (time, result))
  
Update pi-montecarlo example to use `sum` again.
## Code Before:

from __future__ import division

import sys

from util import timer

from distarray.dist import Context, Distribution, hypot
from distarray.dist.random import Random


context = Context()
random = Random(context)


def local_sum(mask):
    return mask.ndarray.sum()


@timer
def calc_pi(n):
    """Estimate pi using distributed NumPy arrays."""
    distribution = Distribution.from_shape(context=context, shape=(n,))
    x = random.rand(distribution)
    y = random.rand(distribution)
    r = hypot(x, y)
    mask = (r < 1)
    lsum = context.apply(local_sum, (mask.key,))
    return 4 * sum(lsum) / n


if __name__ == '__main__':
    N = int(sys.argv[1])
    result, time = calc_pi(N)
    print('time  : %3.4g\nresult: %.7f' % (time, result))

## Instruction:
Update pi-montecarlo example to use `sum` again.
## Code After:

from __future__ import division, print_function

import sys

from util import timer

from distarray.dist import Context, Distribution, hypot
from distarray.dist.random import Random


context = Context()
random = Random(context)


@timer
def calc_pi(n):
    """Estimate pi using distributed NumPy arrays."""
    distribution = Distribution.from_shape(context=context, shape=(n,))
    x = random.rand(distribution)
    y = random.rand(distribution)
    r = hypot(x, y)
    mask = (r < 1)
    return 4 * mask.sum().toarray() / n


if __name__ == '__main__':
    N = int(sys.argv[1])
    result, time = calc_pi(N)
    print('time  : %3.4g\nresult: %.7f' % (time, result))
