How do I set Decimal precision on dask distributed?

Hey Guys sorry I’m a bit new to Python as well as dask, but I’d like to get a POC working with the code below.

I think it’s pretty close (feel free to tell me how I can make it better) but I need the result to show the digit defined by the precision variable

Locally, I can just call getcontext().prec=precision but how do I set the precision on the remote workers?


from decimal import Decimal
from decimal import getcontext
import dask
from dask.distributed import Client

def fa(k):
  return 1/Decimal(16)**k

def fb(k):
  return Decimal(4)/(8*k+1)

def fc(k):
  return Decimal(2)/(8*k+4)

def fd(k):
  return Decimal(1)/(8*k+5)

def fe(k):
  return Decimal(1)/(8*k+6)

def fsub(b, c, d, e):
  return b - c - d - e

def fmul(a, bcde):
  return a * bcde

client = Client(address='dask-scheduler.dojo-luke-dask.svc:8786')

precision = 100
inits = range(precision)
output = []
for x in inits:
  a = fa(x)
  b = fb(x)
  c = fc(x)
  d = fd(x)
  e = fe(x)
  bcde = fsub(b, c, d, e)
  mul = fmul(a, bcde)

total = dask.delayed(sum)(output)


Hi @myarbrou-rh,

You need to make Workers aware about the precision you want them to work with.

I give it a quick try, launching:

def set_context(precision):
    getcontext().prec=precision, 2)

before computing any delayed operation works. Maybe you can set the default in other ways with Decimal, I don’t know.