def test_map_dict_uniform(mylib): from seamless.highlevel import Context, Cell ctx = Context() ctx.include(mylib.map_dict) ctx.b = 1000 ctx.add = Context() ctx.add.inp = Cell("mixed") ctx.add.uniform = Cell("mixed") def add(a, b): print("ADD", a, b) return a + b ctx.add.tf = add ctx.add.tf.debug = True ctx.add.tf.a = ctx.add.inp ctx.add.tf.b = ctx.add.uniform ctx.add.result = ctx.add.tf ctx.add.result.celltype = "int" ctx.compute() ctx.inp = {"key1": 10, "key2": 220, "key3": 30, "key4": 40} ctx.inp.hash_pattern = {"*": "#"} ctx.result = Cell() ctx.keyorder = Cell("plain") ctx.mapping = ctx.lib.map_dict(context_graph=ctx.add, inp=ctx.inp, uniform=ctx.b, keyorder0=[], keyorder=ctx.keyorder, result=ctx.result, elision=True, elision_chunksize=2) ctx.compute() print(ctx.mapping.ctx.status) print(ctx.mapping.ctx.m.ctx.top.exception) print(ctx.result.value) ctx.b = 6000 ctx.compute() print(ctx.result.value)
def test_map_list_uniform(mylib): from seamless.highlevel import Context, Cell ctx = Context() ctx.include(mylib.map_list) ctx.b = -1000 ctx.uniform = Cell() ctx.uniform.b = ctx.b ctx.add = Context() ctx.add.inp = Cell("mixed") ctx.add.uniform = Cell("mixed") ctx.add.uniform2 = ctx.add.uniform def add(a, b): print("ADD", a, b) return a + b ctx.add.tf = add ctx.add.tf.debug = True ctx.add.tf.a = ctx.add.inp ctx.add.tf.b = ctx.add.uniform2.b ctx.add.result = ctx.add.tf ctx.add.result.celltype = "int" ctx.compute() ctx.inp = [210, 220, 230, 240] ctx.inp.hash_pattern = {"!": "#"} ctx.result = Cell() ctx.mapping = ctx.lib.map_list(context_graph=ctx.add, inp=ctx.inp, uniform=ctx.uniform, result=ctx.result, elision=True, elision_chunksize=2) ctx.compute() print(ctx.mapping.ctx.m.exception) print(ctx.result.value) ctx.b = 1000 ctx.compute() print(ctx.result.value)
def test_map_list_N_uniform(mylib): from seamless.highlevel import Context, Cell ctx = Context() ctx.include(mylib.map_list_N) ctx.add = Context() ctx.add.uniform = Cell("mixed") ctx.add.inp = Context() ctx.add.inp.a = Cell("mixed") ctx.add.inp.b = Cell("mixed") def add(a, b, c): print("ADD", a, b, c) return a + b + c ctx.add.tf = add ctx.add.tf.debug = True ctx.add.tf.a = ctx.add.inp.a ctx.add.tf.b = ctx.add.inp.b ctx.add.tf.c = ctx.add.uniform ctx.add.result = ctx.add.tf ctx.add.result.celltype = "int" ctx.compute() ctx.a = [110, 120, 130, 140] ctx.a.hash_pattern = {"!": "#"} ctx.b = [2, 4, 8, 12] ctx.b.hash_pattern = {"!": "#"} ctx.c = 7000 ctx.result = Cell() ctx.mapping = ctx.lib.map_list_N(context_graph=ctx.add, inp={ "a": ctx.a, "b": ctx.b, }, uniform=ctx.c, result=ctx.result, elision=True, elision_chunksize=2) ctx.compute() print(ctx.result.value) ctx.c = 8000 ctx.compute() print(ctx.result.value)
import os, tempfile from seamless.highlevel import Context, Cell ctx = Context() ctx.a = 10 ctx.a.celltype = "plain" ctx.b = 30 ctx.b.celltype = "plain" def build_transformer(): del ctx.transform ctx.transform = lambda a, b: a + b ctx.translate() ctx.transform.example.a = 0 ctx.transform.example.b = 0 ctx.result = ctx.transform ctx.result.celltype = "plain" ctx.transform.a = ctx.a ctx.transform.b = ctx.b ctx.transform.language = "cpp" ctx.transform.main_module.compiler_verbose = False ctx.code = ctx.transform.code.pull() ctx.code = """ extern "C" double add(int a, int b); extern "C" int transform(int a, int b, double *result) { *result = add(a,b);
from seamless.highlevel import Context ctx = Context() ctx.a = {"x": 123} z = ctx.get_zip() print("ZIP size:", len(z)) ctx.b = ctx.a.x ctx.compute() print(ctx.b.value, ctx.b.checksum) z = ctx.get_zip() print("ZIP size:", len(z)) ctx.b.scratch = True ctx.compute() print(ctx.b.value, ctx.b.checksum) z = ctx.get_zip() print("ZIP size (should be 448):", len(z))
print("") ctx.s = "NOT TO BE PRINTED 2" ctx.s.a = ctx.a ctx.s.c = ctx.c ctx.compute() print(ctx.s.value) print(ctx.ss.value) print(ctx.s.exception) print("") ctx.s.set({}) ctx.compute() print(ctx.s.value) print(ctx.ss.value) print(ctx.s.exception) print("") ctx.b = Cell("int").set(999) ctx.s.b = ctx.b ctx.compute() print(ctx.s.value) print(ctx.ss.value) print(ctx.s.exception) print("") ctx.b = None ctx.compute() print(ctx.s.value) print(ctx.ss.value) print(ctx.s.exception) print("") ctx.d = 123 #ctx.d.celltype = "int" ### ctx.s.d = ctx.d
ctx.schema = Cell("plain") ctx.link(ctx.a.schema, ctx.schema) ctx.a.example.first = 0 ctx.a.example.step = 0 ctx.a.example.length = 0 ctx.a.add_validator(validate, "validate") ctx.b_first = Cell("int").set(8) ctx.b_first.share(readonly=False) ctx.b_step = Cell("int").set(1) ctx.b_step.share(readonly=False) ctx.b_length = Cell("int").set(4) ctx.b_length.share(readonly=False) ctx.b = Cell() ctx.translate() ctx.link(ctx.b.schema, ctx.schema) ctx.b.first = ctx.b_first ctx.b.step = ctx.b_step ctx.b.length = ctx.b_length def calc_bits(a, b): import numpy as np result = np.zeros((a.length*b.length, 3),int) count = 0 aa = a.first.unsilk for apos in range(a.length): bb = b.first.unsilk for bpos in range(b.length): mul = aa * bb
checksum = 'a768afb52fb0be2c8bf1657ea5c892df910a2a70bac7310cd8595e0f62b89fbf' # 136 from seamless.highlevel import Context ctx = Context() ctx.a = 10 ctx.compute() ctx.b = 136 ctx.compute() ctx.a.set_checksum(checksum) ctx.compute() print(ctx.a.value) print(ctx.a.status) print(ctx.a.exception)
mylib.instantiate.constructor = ctx.constructor_code.value mylib.instantiate.params = ctx.constructor_params.value # 4: Run test example ctx = Context() ctx.include(mylib.instantiate) ctx.a = Cell().set({ "instance1": 3, "instance2": 5, "instance3": 7, "instance5": 9, }) ctx.b = Cell().set({ "instance1": 8, "instance2": 6, "instance3": 4, "instance5": 2, }) ctx.result = Cell() ctx.result2 = Cell() def mul(fa, fb): return fa * fb ctx.subcontext = Context() sctx = ctx.subcontext sctx.mul = mul sctx.fa = Cell("int") sctx.fb = Cell("int")
seamless.set_ncores(2) seamless.set_parallel_evaluations(5) """ import logging logging.basicConfig() logging.getLogger("seamless").setLevel(logging.DEBUG) """ from seamless.highlevel import Context, Cell, Macro sctx = Context() sctx.inp = Cell("mixed") sctx.inp2 = Cell() sctx.inp2 = sctx.inp sctx.a = Cell("str") sctx.b = Cell("str") sctx.a = sctx.inp2.a sctx.b = sctx.inp2.b def add(a, b): print("ADD", a[:10], b[:10]) return a + b sctx.add = add sctx.add.a = sctx.a sctx.add.b = sctx.b sctx.result = sctx.add sctx.result.celltype = "str" sctx.compute()
from seamless.highlevel import Context, Cell, Transformer ctx = Context() ctx.a = Cell("int").set(10) ctx.a.share(readonly=False) ctx.b = Cell("int").set(20) ctx.b.share(readonly=False) ctx.c = Cell("int").set(30).share() ctx.tf = lambda a,b: a+b ctx.tf.a = ctx.a ctx.tf.b = ctx.b ctx.c = ctx.tf ctx.translate() ctx.save_graph("initial-graph.seamless") ctx.save_zip("initial-graph.zip")
# 4: Run test example ctx = Context() ctx.include(mylib.scatter_transformer) def add(a, b): return a + b ctx.tf = ctx.lib.scatter_transformer() ctx.tf.code = add ctx.tf.scatter = "a" ctx.tf.a = [10, 20, 30] ctx.b = 1000 ctx.tf.b = ctx.b ctx.result = ctx.tf.result ctx.compute() print(ctx.tf.status) if ctx.tf.status != "Status: OK": print(ctx.tf.exception) print(ctx.result.value) ctx.tf.a = {"x": 100.1, "y": 200.1, "z": 300.1} ctx.tf.celltypes = {"a": "int"} ctx.b = -1000 ctx.compute() print(ctx.tf.status) if ctx.tf.status != "Status: OK": print(ctx.tf.exception)
from seamless.highlevel import Context ctx = Context() def add(a, b): return a + b ctx.a = 10 ctx.b = 20 ctx.add = add ctx.add.a = ctx.a ctx.add.b = ctx.b ctx.c = ctx.add ctx.compute() print(ctx.c.value) ctx.a.celltype = "plain" ctx.a.mount("/tmp/a") ctx.b.celltype = "plain" ctx.b.mount("/tmp/b") ctx.c.celltype = "plain" ctx.c.mount("/tmp/c", mode="w") ctx.add.code.mount("/tmp/code.py") ctx.compute()
from seamless.highlevel import Context, Cell, Macro sctx = Context() sctx.inp = Cell("mixed") sctx.inp2 = Cell() sctx.inp2 = sctx.inp sctx.a = Cell("int") sctx.b = Cell("int") sctx.a = sctx.inp2.a sctx.b = sctx.inp2.b def add(a,b): return a+b sctx.add = add sctx.add.a = sctx.a sctx.add.b = sctx.b sctx.result = sctx.add sctx.result.celltype = "int" sctx.compute() data = [ { "a": 5, "b": 6, }, { "a": -2, "b": 8, }, { "a": 3, "b": 14,
from seamless.highlevel import Context, Cell, Macro ctx = Context() ctx.a = Cell("int") ctx.b = Cell("int") def add(a,b): return a+b ctx.add = add ctx.add.a = ctx.a ctx.add.b = ctx.b ctx.result = ctx.add ctx.result.celltype = "int" ctx.compute() graph = ctx.get_graph(runtime=True) ctx = Context() ctx.graph = Cell("plain").set(graph) m = ctx.m = Macro() ctx.par_static = 100 ctx.par_dynamic = 20 m.par_static = ctx.par_static m.graph = ctx.graph m.pins.par_dynamic = {"io": "input", "celltype": "int"} m.pins.graph_result = {"io": "output", "celltype": "int"} def run_macro(ctx, par_static, graph): print("RUN MACRO", par_static) ctx.subctx = HighLevelContext(graph) ctx.subctx.a.set(par_static) ctx.par_dynamic = cell("int") ctx.par_dynamic.connect(ctx.subctx.b)
import os, tempfile from seamless.highlevel import Context, Cell ctx = Context() ctx.a = 10 ctx.a.celltype = "json" ctx.b = 30 ctx.b.celltype = "json" def build_transformer(): ctx.transform = lambda a,b: a + b ctx.transform.example.a = 0 ctx.transform.example.b = 0 ctx.result = ctx.transform ctx.result.celltype = "json" ctx.transform.a = ctx.a ctx.transform.b = ctx.b ctx.transform.language = "cpp" ctx.transform.main_module.compiler_verbose = False ctx.code >> ctx.transform.code ctx.code = """ extern "C" double add(int a, int b); extern "C" double transform(int a, int b) { return add(a,b); }""" ctx.transform.result.example = 0.0 #example, just to fill the schema
from seamless.highlevel import Context, Cell import traceback ctx = Context() ctx.a = Cell("int").set(20) ctx.b = ctx.a ctx.b.celltype = "int" ctx.compute() print(ctx.b.value) try: ctx.b.mount("/tmp/x") except Exception: traceback.print_exc() try: ctx.b.share(readonly=False) except Exception: traceback.print_exc() ctx.compute()
from seamless.highlevel import Context ctx = Context() def func(a, b, c, x): return a + b + 10 * c - x ctx.tf = func ctx.a = 10 ctx.b = 20 ctx.tf.a = ctx.a ctx.tf.b = ctx.b ctx.tf.c = 80 ctx.tf.x = 123 ctx.compute() def func2(b, c, d): return b + 10 * c - d ctx.tf = func2 ctx.tf.d = 5 ctx.compute() print(ctx.tf.status) print(ctx.tf.exception) print(ctx.tf.inp.value) print(ctx.tf.result.value)