class FutharkContext: def __init__(self): self.FutEnv = Futhark(_main) self.tables = {} def create_table(self, table_name, table): """ Stores a table if the table """ table = Table(table_name, table) # possibly need to do an exception check here or line above self.tables[table_name] = table def drop_table(self, table_name): del self.tables[table_name] def sql(self, sql_statement): """ Should call something here that takes two arguements sql_parse(tables, sql_statement) """ val_dic = sql_parse(self.tables, sql_statement) t1 = val_dic["table"] sel_cols = val_dic["select"] if "groupbys" not in val_dic: res = self.FutEnv.query_sel(t1, np.array(sel_cols)) return self.FutEnv.from_futhark(res) else: t_cols = val_dic["groupbys"] g_col = val_dic["g_col"] res = self.FutEnv.query_groupby(t1, g_col, np.array(sel_cols), np.array(t_cols)) return self.FutEnv.from_futhark(res)
def make_wrapper(self, ff): wrapper = Futhark.make_wrapper(self,ff) @wraps(wrapper) def subwrapper(*args): uwargs = [] for arg in args: if hasattr(arg, 'futdata'): uwargs.append(arg.futdata) else: uwargs.append(arg) res = wrapper(*uwargs) wrres = [] try: for r in res: if isinstance(r, self.ffi.CData): wrres.append(Gettable(self, r)) else: wrres.append(r) return tuple(wrres) except TypeError: if isinstance(res, self.ffi.CData): return Gettable(self, res) else: return res return subwrapper
def setUp(self): self.fut = Futhark(_test)
class TestFFI(unittest.TestCase): def setUp(self): self.fut = Futhark(_test) def test_int(self): self.assertEqual(self.fut.test1(5), 6) def test_list_input(self): data = np.arange(10) self.assertEqual(self.fut.test2(data), np.sum(data)) def test_list_output(self): data = np.arange(10) res = self.fut.test3(data) pyres = self.fut.from_futhark(res) assert_array_equal(pyres, np.cumsum(data)) def test_multi_output(self): self.assertEqual(self.fut.test4(4, 5), (4 + 5, 4 - 5)) def test_2d(self): data = np.arange(9).reshape(3, 3) res = self.fut.test5(data) pyres = self.fut.from_futhark(res) assert_array_equal(pyres, data * 2) res = self.fut.test5(data.T) pyres = self.fut.from_futhark(res) assert_array_equal(pyres, (data * 2).T) def test_opaque(self): res = self.fut.test6(10) (pos, neg) = self.fut.test7(res) (pos, neg) = self.fut.from_futhark(pos, neg) assert_array_equal(pos, np.arange(10)) assert_array_equal(neg, -np.arange(10)) def test_bool(self): res = self.fut.test8(True) self.assertEqual(res, False) def test_error(self): with self.assertRaises(ValueError): self.fut.test9(np.arange(4))
# Test for FFI. By pepijndevos import numpy as np import pyopencl import _heat from futhark_ffi import Futhark data = np.zeros([3,3]) data[1,1] = 1 test = Futhark(_heat) res = test.main(1, 0.5, data) print(test.from_futhark(res))
from futhark_data import dump from futhark_ffi import Futhark import numpy as np import _fsvm fsvm = Futhark(_fsvm) kernels = { 'linear': 0, 'sigmoid': 1, 'polynomial': 2, 'rbf': 3 } class SVC(): # Negative values doesn't work for primitives w/ futhark_ffi # it seems, so max_t_out is set to a high value instead of -1. def __init__(self, kernel='rbf', C=10., n_ws=1024, max_t=100000000, max_t_in=102400, max_t_out=100000000, eps=0.001, gamma='auto', coef0=0., degree=3., verbose=0.): if kernel not in kernels: raise Exception('Unknown kernel') self.kernel = kernel self.__fit = getattr(fsvm, f'svc_{kernel}_fit') self.__predict = getattr(fsvm, f'svc_{kernel}_predict') self.C = C self.n_ws = n_ws self.max_t = max_t
def __init__(self): self.FutEnv = Futhark(_main) self.tables = {}
container_out.mux(packet) decode_queue = queue.Queue() encode_queue = queue.Queue() decode_thread = threading.Thread(target=decode, daemon=True, name="decode") decode_thread.start() # Dump input video info into globals for the encode thread and other consumers frames, width, height, codec_name, framerate = decode_queue.get() encode_thread = threading.Thread(target=encode, daemon=True, name="encode") encode_thread.start() carve = Futhark(_carve, profiling=args.profile) # We want to keep `frame` on the GPU for the duration of its carving. This # means we need to use to_futhark(). But this requires us to know the fut_type, # which isn't directly exposed. So, we have to search through carve.types. for ftype in carve.types.values(): if ftype.itemtype.cname == "uint8_t *" and ftype.rank == 2: u8_2d = ftype seams = np.empty((args.pixels, height), np.int16) def main(): hist_dist = 0 prev_hist = None # The maximum distance between two histograms is 2 * width * height (e.g.