def test_variable_name_contains_forbidden(self) -> None: with self.assertRaises(pt.ValidateError): pt.Symbol(pt.Int32_t, "a*") with self.assertRaises(pt.ValidateError): pt.Symbol(pt.Int32_t, "a-a") with self.assertRaises(pt.ValidateError): pt.Symbol(pt.Int32_t, "a+a") with self.assertRaises(pt.ValidateError): pt.Symbol(pt.Int32_t, "a🤔") with self.assertRaises(pt.ValidateError): pt.Symbol(pt.Int32_t, "a猫") with self.assertRaises(pt.ValidateError): pt.Symbol(pt.Int32_t, "a ") pt.Symbol(pt.Int32_t, "aBCD") pt.Symbol(pt.Int32_t, "a_B_02") pt.Symbol(pt.Int32_t, "a554")
def test_variable_name_starts_not_lowercase(self) -> None: with self.assertRaises(pt.ValidateError): pt.Symbol(pt.Int32_t, "_") with self.assertRaises(pt.ValidateError): pt.Symbol(pt.Int32_t, "_a") with self.assertRaises(pt.ValidateError): pt.Symbol(pt.Int32_t, "A") with self.assertRaises(pt.ValidateError): pt.Symbol(pt.Int32_t, "Aa") with self.assertRaises(pt.ValidateError): pt.Symbol(pt.Int32_t, "0") with self.assertRaises(pt.ValidateError): pt.Symbol(pt.Int32_t, "0a") pt.Symbol(pt.Int32_t, "a") pt.Symbol(pt.Int32_t, "aa")
from typing import cast, Callable import petra as pt import unittest from ctypes import CFUNCTYPE, c_int8, c_int16, c_int32, c_int64 program = pt.Program("module") # Int8_t functions. # TODO(adbenson): refactor this to take in arguments for better testing a = pt.Symbol(pt.Int8_t, "a") b = pt.Symbol(pt.Int8_t, "b") c = pt.Symbol(pt.Int8_t, "c") d = pt.Symbol(pt.Int8_t, "d") e = pt.Symbol(pt.Int8_t, "e") f = pt.Symbol(pt.Int8_t, "f") g = pt.Symbol(pt.Int8_t, "g") h = pt.Symbol(pt.Int8_t, "h") i = pt.Symbol(pt.Int8_t, "i") program.add_func( "add_i8", (), pt.Int8_t, pt.Block([ pt.DefineVar(a, pt.Add(pt.Int8(-11), pt.Int8(-4))), pt.DefineVar(b, pt.Add(pt.Int8(-11), pt.Int8(0))), pt.DefineVar(c, pt.Add(pt.Int8(-11), pt.Int8(7))),
import petra as pt 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")
from typing import cast, Callable import subprocess import petra as pt import unittest from ctypes import CFUNCTYPE, c_int32 program = pt.Program("module") My_Array = pt.ArrayType(pt.Int32_t, 3) array_var = pt.Symbol(My_Array, "array_var") program.add_func( "array_set_get_values", (), pt.Int32_t, pt.Block( [ pt.DefineVar(array_var), pt.Assign( pt.Var(array_var), pt.SetElement(pt.Var(array_var), pt.Int32(1), 0) ), pt.Assign( pt.Var(array_var), pt.SetElement(pt.Var(array_var), pt.Int32(2), 1) ), pt.Assign( pt.Var(array_var), pt.SetElement(pt.Var(array_var), pt.Int32(3), 2) ), pt.Return( pt.Add(
from typing import cast, Callable import petra as pt import unittest from ctypes import CFUNCTYPE, c_bool, c_int8 program = pt.Program("module") x, y = pt.Symbol(pt.Int8_t, "x"), pt.Symbol(pt.Int8_t, "y") z, w = pt.Symbol(pt.Bool_t, "z"), pt.Symbol(pt.Bool_t, "w") program.add_func( "lt", (x, y), pt.Bool_t, pt.Block([pt.Return(pt.Lt(pt.Var(x), pt.Var(y)))]), ) program.add_func( "lte", (x, y), pt.Bool_t, pt.Block([pt.Return(pt.Lte(pt.Var(x), pt.Var(y)))]), ) program.add_func( "gt", (x, y), pt.Bool_t, pt.Block([pt.Return(pt.Gt(pt.Var(x), pt.Var(y)))]),
from typing import cast, Callable import petra as pt import unittest from ctypes import CFUNCTYPE, c_int32 program = pt.Program("module") x = pt.Symbol(pt.Int32_t, "x") y = pt.Symbol(pt.Int32_t, "y") z = pt.Symbol(pt.Int8_t, "z") program.add_func( "return_temp", (), pt.Int32_t, pt.Block([pt.DefineVar(x, pt.Int32(2)), pt.Return(pt.Var(x))]), ) program.add_func( "return_shuffle_temp", (), pt.Int32_t, pt.Block([ pt.DefineVar(x, pt.Int32(2)), pt.DefineVar(y, pt.Var(x)), pt.Return(pt.Var(y)), ]), )
(legion_point_1d_t, ), legion_domain_point_t, attributes=("byval", )) program.add_func_decl( "legion_logical_partition_get_logical_subregion_by_color_domain_point", ( legion_runtime_t, legion_logical_partition_t, legion_domain_point_t, ), legion_logical_region_t, attributes=("byval", "byval", "byval"), ) # Define variables: runtime = pt.Symbol(legion_runtime_t, "runtime_ptr") parent_ptr = pt.Symbol(pt.PointerType(legion_logical_partition_t), "parent_ptr") point_ptr = pt.Symbol(pt.PointerType(legion_domain_point_t), "point_ptr") domain_ptr = pt.Symbol(pt.PointerType(legion_domain_t), "domain_ptr") point1d = pt.Symbol(legion_point_1d_t, "point1d") point1d_x = pt.Symbol(legion_point_1d_array, "point1d_x") x = pt.Symbol(coord_t, "x") x_plus_1 = pt.Symbol(coord_t, "x_plus_1") point1d_x_plus_1 = pt.Symbol(legion_point_1d_t, "point1d_x_plus_1") domain_point_x_plus_1 = pt.Symbol(legion_domain_point_t, "domain_point_x_plus_1") result = pt.Symbol(legion_logical_region_t, "result") point1d_x_plus_1_x = pt.Symbol(pt.ArrayType(coord_t, DIM), "point1d_x_plus_1_x")
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))]), )
from ctypes import CFUNCTYPE, c_int32, c_int64 program = pt.Program("module") Pointer_Int64_t = pt.PointerType(pt.Int64_t) # void* malloc( size_t size ); program.add_func_decl("malloc", (pt.Int64_t, ), Pointer_Int64_t) # 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(
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")
"if_else_false", (), pt.Int32_t, pt.Block([ pt.If( pt.Lt(pt.Int32(1), pt.Int32(0)), pt.Block([ pt.Return(pt.Int32(1)), ]), pt.Block([pt.Return(pt.Int32(2))]), ), pt.Return(pt.Int32(0)), ]), ) x = pt.Symbol(pt.Int32_t, "x") program.add_func( "while_then", (), pt.Int32_t, pt.Block([ pt.DefineVar(x, pt.Int32(0)), pt.While( pt.Lt(pt.Var(x), pt.Int32(10)), pt.Block([ pt.Assign( pt.Var(x), pt.Add(pt.Var(x), pt.Int32(1)), ), ]), ),