def decode(self, obj, restype): # This stuff is same as grizzly. if restype == WeldInt(): data = cweld.WeldValue(obj).data() result = ctypes.cast(data, ctypes.POINTER(c_int)).contents.value return np.int32(result) elif restype == WeldLong(): data = cweld.WeldValue(obj).data() result = ctypes.cast(data, ctypes.POINTER(c_long)).contents.value return np.int64(result) elif restype == WeldFloat(): data = cweld.WeldValue(obj).data() result = ctypes.cast(data, ctypes.POINTER(c_float)).contents.value return np.float32(result) elif restype == WeldDouble(): data = cweld.WeldValue(obj).data() result = ctypes.cast(data, ctypes.POINTER(c_double)).contents.value return np.float64(result) # is a WeldVec() - depending on the types, need to make minor changes. assert isinstance(restype, WeldVec) obj = obj.contents size = obj.size data = obj.ptr dtype = restype.elemType.cTypeClass if restype == WeldVec(WeldInt()) or restype == WeldVec(WeldFloat()): # these have same sizes. ArrayType = ctypes.c_float*size elif restype == WeldVec(WeldLong()) or restype == WeldVec(WeldDouble()): ArrayType = ctypes.c_double*size array_pointer = ctypes.cast(data, ctypes.POINTER(ArrayType)) result = np.frombuffer(array_pointer.contents, dtype=dtype,count=size) return result
def evaluate(self, restype, verbose=True, decode=True, passes=None): function = self.to_weld_func() # Returns a wrapped ctypes Structure def args_factory(encoded): class Args(ctypes.Structure): _fields_ = [e for e in encoded] return Args # Encode each input argument. This is the positional argument list # which will be wrapped into a Weld struct and passed to the Weld API. names = self.context.keys() names.sort() start = time.time() encoded = [] argtypes = [] for name in names: if name in self.argtypes: argtypes.append(self.argtypes[name].ctype_class) encoded.append(self.context[name]) else: argtypes.append( self.encoder.py_to_weld_type( self.context[name]).ctype_class) encoded.append(self.encoder.encode(self.context[name])) end = time.time() if verbose: print "Python->Weld:", end - start start = time.time() Args = args_factory(zip(names, argtypes)) weld_args = Args() for name, value in zip(names, encoded): setattr(weld_args, name, value) start = time.time() void_ptr = ctypes.cast(ctypes.byref(weld_args), ctypes.c_void_p) arg = cweld.WeldValue(void_ptr) conf = cweld.WeldConf() err = cweld.WeldError() if passes is not None: conf.set("weld.optimization.passes", ",".join(passes)) # conf.set("weld.compile.dumpCode", "true") # conf.set("weld.compile.multithreadSupport", "false") module = cweld.WeldModule(function, conf, err) if err.code() != 0: raise ValueError("Could not compile function {}: {}".format( function, err.message())) end = time.time() if verbose: print "Weld compile time:", end - start start = time.time() conf = cweld.WeldConf() weld_num_threads = os.environ.get("WELD_NUM_THREADS", "1") conf.set("weld.threads", weld_num_threads) mem_limit = "1000000000000" conf.set("weld.memory.limit", mem_limit) err = cweld.WeldError() weld_ret = module.run(conf, arg, err) if err.code() != 0: raise ValueError( ("Error while running function,\n{}\n\n" "Error message: {}").format(function, err.message())) ptrtype = POINTER(restype.ctype_class) data = ctypes.cast(weld_ret.data(), ptrtype) end = time.time() if verbose: print "Weld:", end - start start = time.time() if decode: result = self.decoder.decode(data, restype) else: data = cweld.WeldValue(weld_ret).data() result = ctypes.cast(data, ctypes.POINTER(ctypes.c_int64)).contents.value end = time.time() if verbose: print "Weld->Python:", end - start return result
def evaluate(self, restype, verbose=True, decode=True): function = self.toWeldFunc() # Returns a wrapped ctypes Structure def args_factory(encoded): class Args(ctypes.Structure): _fields_ = [e for e in encoded] return Args # Encode each input argument. This is the positional argument list # which will be wrapped into a Weld struct and passed to the Weld API. names = self.context.keys() names.sort() start = time.time() encoded = [] argtypes = [] for name in names: if name in self.argtypes: argtypes.append(self.argtypes[name].cTypeClass) encoded.append(self.context[name]) else: argtypes.append( self.encoder.pyToWeldType(self.context[name]).cTypeClass) encoded.append(self.encoder.encode(self.context[name])) end = time.time() if verbose: print "Total time encoding:", end - start start = time.time() Args = args_factory(zip(names, argtypes)) weld_args = Args() for name, value in zip(names, encoded): setattr(weld_args, name, value) void_ptr = ctypes.cast(ctypes.byref(weld_args), ctypes.c_void_p) arg = cweld.WeldValue(void_ptr) err = cweld.WeldError() module = cweld.WeldModule(function, cweld.WeldConf(), err) if err.code() != 0: raise ValueError("Could not compile function {}: {}".format( function, err.message())) conf = cweld.WeldConf() conf.set("weld.threads", "1") conf.set("weld.memory.limit", "100000000000") err = cweld.WeldError() weld_ret = module.run(conf, arg, err) if err.code() != 0: raise ValueError( ("Error while running function,\n{}\n\n" "Error message: {}").format(function, err.message())) ptrtype = POINTER(restype.cTypeClass) data = ctypes.cast(weld_ret.data(), ptrtype) end = time.time() if verbose: print "Total time running:", end - start start = time.time() if decode: result = self.decoder.decode(data, restype) else: data = cweld.WeldValue(weld_ret).data() result = ctypes.cast(data, ctypes.POINTER(ctypes.c_int64)).contents.value end = time.time() if verbose: print "Total time decoding:", end - start return result