Esempio n. 1
0
 def test_return_call_nothing_for_int32_t(self) -> None:
     with self.assertRaises(pt.TypeCheckError):
         pt.Program("module").add_func(
             "nothing", (), (), pt.Block([pt.Return(())])).add_func(
                 "call_nothing",
                 (),
                 pt.Int32_t,
                 pt.Block([pt.Return(pt.Call("nothing", []))]),
             )
Esempio n. 2
0
program = pt.Program("module")

n = pt.Symbol(pt.Int32_t, "n")

program.add_func(
    "collatz",
    (n, ),
    pt.Int32_t,
    pt.Block([
        pt.If(
            pt.Eq(pt.Var(n), pt.Int32(1)),
            pt.Block([pt.Return(pt.Int32(0))]),
            pt.Block([]),
        ),
        pt.If(
            pt.Eq(pt.Mod(pt.Var(n), pt.Int32(2)), pt.Int32(0)),
            pt.Block([pt.Assign(pt.Var(n), pt.Div(pt.Var(n), pt.Int32(2)))]),
            pt.Block([
                pt.Assign(
                    pt.Var(n),
                    pt.Add(pt.Mul(pt.Int32(3), pt.Var(n)), pt.Int32(1)),
                ),
            ]),
        ),
        pt.Return(pt.Add(pt.Int32(1), pt.Call("collatz", [pt.Var(n)]))),
    ]),
)

program.save_object("collatz.py.o")
Esempio n. 3
0
point1d_x_plus_1_x = pt.Symbol(pt.ArrayType(coord_t, DIM),
                               "point1d_x_plus_1_x")

program.add_func(
    "proj_functor",
    (
        runtime_ptr,
        parent_ptr,
        point_ptr,
        domain_ptr,
    ),
    legion_logical_region_t,
    pt.Block([
        pt.DefineVar(
            point1d,
            pt.Call("legion_domain_point_get_point_1d",
                    [pt.Deref(pt.Var(point_ptr))])),
        pt.DefineVar(
            point1d_x,
            pt.GetElement(pt.Var(point1d), name="x"),
        ),
        pt.DefineVar(x, pt.GetElement(pt.Var(point1d_x), idx=0)),
        pt.DefineVar(x_plus_1, pt.Add(pt.Var(x), pt.Int64(1))),
        pt.DefineVar(point1d_x_plus_1_x),
        pt.Assign(
            pt.Var(point1d_x_plus_1_x),
            pt.SetElement(pt.Var(point1d_x_plus_1_x), pt.Var(x_plus_1), 0),
        ),
        pt.DefineVar(point1d_x_plus_1),
        pt.Assign(
            pt.Var(point1d_x_plus_1),
            pt.SetElement(pt.Var(point1d_x_plus_1),
Esempio n. 4
0
from typing import cast, Callable

import petra as pt
import unittest

from ctypes import CFUNCTYPE, c_int32

program = pt.Program("module")

x, y = pt.Symbol(pt.Int32_t, "x"), pt.Symbol(pt.Int32_t, "y")

program.add_func("return_2", (), pt.Int32_t,
                 pt.Block([pt.Return(pt.Int32(2))]))

program.add_func("call_return_2", (), pt.Int32_t,
                 pt.Block([pt.Return(pt.Call("return_2", []))]))

program.add_func(
    "call_return_2_discard",
    (),
    pt.Int32_t,
    pt.Block([pt.Call("return_2", []),
              pt.Return(pt.Int32(3))]),
)

program.add_func(
    "iden",
    (x, ),
    pt.Int32_t,
    pt.Block([pt.Return(pt.Var(x))]),
)
Esempio n. 5
0
# void free( void* ptr);
program.add_func_decl("free", (Pointer_Int64_t, ), ())
# void* memset ( void* ptr, int value, size_t num );
program.add_func_decl("memset", (Pointer_Int64_t, pt.Int32_t, pt.Int64_t),
                      Pointer_Int64_t)

ptr = pt.Symbol(Pointer_Int64_t, "ptr")
val = pt.Symbol(pt.Int64_t, "val")
v32 = pt.Symbol(pt.Int32_t, "val")

program.add_func(
    "malloc_free",
    (),
    pt.Int32_t,
    pt.Block([
        pt.DefineVar(ptr, pt.Call("malloc", [pt.Int64(8)])),
        pt.Call("free", [pt.Var(ptr)]),
        pt.Return(pt.Int32(1)),
    ]),
)

program.add_func(
    "malloc_memset",
    (),
    pt.Int64_t,
    pt.Block([
        pt.DefineVar(ptr, pt.Call("malloc", [pt.Int64(8)])),
        pt.Assign(
            pt.Var(ptr),
            pt.Call("memset",
                    [pt.Var(ptr), pt.Int32(0x3A),
Esempio n. 6
0
import petra as pt

program = pt.Program("module")

program.add_func_decl("sqrtf", (pt.Float32_t, ), pt.Float32_t)

x = pt.Symbol(pt.Float32_t, "x")

program.add_func(
    "call_sqrtf",
    (x, ),
    pt.Float32_t,
    pt.Block([pt.Return(pt.Call("sqrtf", [pt.Var(x)]))]),
)

program.save_object("sqrt.py.o")