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?

Thanks

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

@dask.delayed
def fa(k):
  return 1/Decimal(16)**k

@dask.delayed
def fb(k):
  return Decimal(4)/(8*k+1)

@dask.delayed
def fc(k):
  return Decimal(2)/(8*k+4)

@dask.delayed
def fd(k):
  return Decimal(1)/(8*k+5)

@dask.delayed
def fe(k):
  return Decimal(1)/(8*k+6)

@dask.delayed
def fsub(b, c, d, e):
  return b - c - d - e

@dask.delayed
def fmul(a, bcde):
  return a * bcde

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

precision = 100
getcontext().prec=precision
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)
  output.append(mul)

total = dask.delayed(sum)(output)

print(total.compute())

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

client.run(set_context, 2)

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