def start(self, port): self = State(port) # bind socket try: ip = self.ip s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind((ip, port)) s.listen() self.sock = s print('Listening on {}:{}'.format(ip, port)) except Exception as e: print(e) # print the usage of this system print('-----------------------') print("usage : \n ping: ping the current node \n create_ring: create a new ring \n join <ip> <port>: join an existing ring \n exit: exit the system ") print('-----------------------') # keep the state of this node up-to-date. Func([None,None], self, True).start() #keep accepting new command while True: peer = s.accept() print ("after accept") Func(peer, self, False).start() print ("after func") s.close()
def p_functionblock(p): """ functionblock : functionblockname LFBRACK functionlines returnstmtforfunc SEMICOLON RFBRACK | functionblockname1 LFBRACK functionlines RETURN SEMICOLON RFBRACK | functionblockname1 LFBRACK functionlines RFBRACK """ # type, name, pointerdepth, paramList global FunctionNodes if p[4] == '}': temp = Func(p[1][1],p[1][0],p[1][2],p[1][3],p[3],[]) elif p[4] == 'return': temp = Func(p[1][1],p[1][0],p[1][2],p[1][3],p[3],[]) else: temp = Func(p[1][1],p[1][0],p[1][2],p[1][3],p[3],[p[4]]) FunctionNodes.append(temp)
def p_mainblock(p): """ mainblock : VOID mainfunctionname LFBRACK functionlines RFBRACK """ global FunctionNodes temp = Func('main','void',0,[],p[4],None) FunctionNodes.append(temp) p[0] = temp
def parse_script(self, file_object): func_counter = -1 line_counter = -1 func = Func('') for line in file_object.readlines(): line_counter += 1 splitted = line.split(' ') if splitted[0] == 'func': func_name = self.get_func_name(splitted[1]) func = Func(func_name) func.add_content(line) self.functions.append(func) func_counter = line_counter elif re.match(r'\s', splitted[0]) and func_counter != -1: func.add_content(line) else: self.general_content.append(line) func_counter = -1
def get_function_class(clause, nlp, run_selenium): """ Read clause and return the Function class with assign the function type. Input: clause(str): one clause for function nlp(StanfordCoreNLP parser) Output: func: 'func' class with assignd function type """ assert isinstance(clause, str) if args.use_corenlp: pos_tag_clause = nlp.pos_tag(clause) else: pos_tag_clause = nlp.pos_tag(nlp.word_tokenize(clause)) func = Func(clause, pos_tag_clause, run_selenium) func.assign_func_name() return func
def add_func( self, cpp_name, returns=None, params=[], opt_params=[], kl_name=None, promotion_prolog=None, dfg_preset_omit=False, ): cpp_local_name = cpp_name try: cpp_global_name = "::".join(self.nested_cpp_names + [cpp_local_name]) kl_local_name = kl_name if not kl_local_name: kl_local_name = cpp_local_name kl_global_name = "_".join(self.nested_kl_names + [kl_local_name]) returns = massage_returns(returns) params = massage_params(params) opt_params = massage_params(opt_params) result = None for i in range(0, len(opt_params)+1): func = Func( self, cpp_global_name, kl_global_name, returns, params + opt_params[0:i], promotion_prolog=promotion_prolog, dfg_preset_omit=dfg_preset_omit, ) self.ext.add_decl(func) promotion_sig, promotion_cost = func.get_promotion_data() if not promotion_sig in self.ext.func_promotions \ or self.ext.func_promotions[promotion_sig][1] > promotion_cost: self.ext.func_promotions[promotion_sig] = (func, promotion_cost) if not result: result = func return result except Exception as e: self.warning("Ignoring func %s: %s" % (cpp_local_name, e)) return EmptyCommentContainer()
def inject(self, **kwargs): internal = kwargs.get('internal', False) is_asm = kwargs.get('is_asm', False) mark_func = kwargs.get('mark_func', False) return_size = kwargs.get('size', False) target = kwargs.get('target', 'patch') desc = kwargs.get('desc', '') if desc: desc = ' | "%s"' % desc addr = self.binary.next_alloc(target) c = kwargs.get('c') if c: asm = compiler.compile(c, self.binary.linker) raw = self.asm(asm, addr=addr, att_syntax=True) typ = 'c' is_asm = True else: raw, typ = self._compile(addr, **kwargs) self._lint(addr, raw, typ, is_asm=kwargs.get('is_asm')) if typ == 'asm': ret = self.asm(self.arch.ret()) if raw[-len(ret):] != ret and not internal: self.warn('Injected asm does not return!') self.info( pfcol('INJECT') + '@0x%x-0x%x%s' % (addr, addr + len(raw), desc)) if not kwargs.get('silent'): if typ == 'asm' or is_asm: self.debug(dis=self.arch.dis(raw, addr=addr)) else: self.debug(binascii.hexlify(raw)) addr = self.binary.alloc(len(raw), target=target) if mark_func: self.marked_funcs.append(Func(self, addr, len(raw))) self.elf.write(addr, raw) if return_size: return addr, len(raw) else: return addr
def funcs(self, marked=False): addrs = [] self.func_printed = None try: funcs = self.relopen('funcs') except IOError: return for line in funcs: s, e = line.strip().split(' ') start, end = int(s, 16), int(e, 16) func = Func(self, start, end - start) self.current_func = func yield func self.current_func = None if marked: tmp = self.marked_funcs[:] for func in tmp: yield func
def add_func( self, cpp_name, returns=None, params=[], opt_params=[], kl_name=None, ): cpp_local_name = cpp_name try: cpp_global_name = "::".join(self.nested_cpp_names + [cpp_local_name]) kl_local_name = kl_name if not kl_local_name: kl_local_name = cpp_local_name kl_global_name = "_".join(self.nested_kl_names + [kl_local_name]) returns = massage_returns(returns) params = massage_params(params) opt_params = massage_params(opt_params) result = None for i in range(0, len(opt_params) + 1): func = Func( self, cpp_global_name, kl_global_name, returns, params + opt_params[0:i], ) self.ext.add_decl(func) if not result: result = func return result except Exception as e: self.warning("Ignoring func %s: %s" % (cpp_local_name, e)) return EmptyCommentContainer()
def gaInfo(self): try: function = Func(self.f_entry.get()) optimizer = OptimizerGA(function) optimizer.startGA( chromosomes_number=int(self.chromosomes_number_entry.get()), generations_number=int(self.generations_number_entry.get()), mutation=self.mutation.get(), mutation_range=self.mutation_range.get(), optimizer=self.optimizer_entry.get(), statistics=self.statistics.get(), save=self.save.get(), plot=self.plot.get()) if self.statistics.get(): with open('results/GA-statistics.txt', 'r') as f: mytext = f.read() root = Tk() root.title("STATISTICS") root.geometry('700x500+100+100') frame = tk.Frame(master=root, bg='grey') frame.pack(fill='both', expand='yes') text = tkst.ScrolledText(master=frame, wrap=tk.WORD, width=700, height=500) text.pack() text.insert(1.0, mytext) root.mainloop() except Exception as err: print('Ошибка!\n', type(err)) print(err) self.errorGUI()
def gt(num): def inner(args): return len(args) > num inner.__doc__ = "Number of arguments must be greater than %s" % num return inner def between(lower, upper): """Lower and upper are inclusive""" def inner(args): return lower <= len(args) <= upper inner.__doc__ = "Number of arguments must be " \ "between (inclusive) %s and %s" % (lower, upper) return inner IF = Func('IF', args_cmp_func=eq(3), doc="IF function. Param 1: Conditional. " "Param 2: True Part. Param 3: False Part.") IFS = Func('IFS', args_cmp_func=between(2, 127 * 2)) IFERROR = Func('IFERROR', args_cmp_func=eq(2)) SUMIFS = Func('SUMIFS', args_cmp_func=between(2, 127 * 2)) SUM = Func('SUM', args_cmp_func=between(1, 255)) DATE = Func('DATE', args_cmp_func=eq(3)) YEAR = Func('YEAR', args_cmp_func=eq(1)) MONTH = Func('MONTH', args_cmp_func=eq(1)) DAY = Func('DAY', args_cmp_func=eq(1))
include_list = ["mli_krn_avepool_chw.h"] define_list = [] # commandline arguments can be used to generate a specific output 'fx16' | 'fx8' | 'header'] # if no arguments are given, all files are generated. no_args = len(sys.argv) == 1 #------------------------------------------------------------ # Create a list of specialization functions #------------------------------------------------------------ #construct the different specializtions for stride 1 and kernel 2x2 corefunc = "avepool_chw_nopad_k2x2" stride = 1 k = 2 ch = 0 f_list.extend([Func(fbase, k, k, ch, stride, stride, corefunc, "nopad")]) corefunc = "avepool_chw_k4x4_str1_nopad" stride = 1 k = 4 ch = 0 f_list.extend([Func(fbase, k, k, ch, stride, stride, corefunc, "nopad")]) corefunc = "avepool_chw_krnpad" stride = 0 kernel_range = range(3, 11, 2) ch = 0 f_list.extend([Func(fbase, k, k, ch, stride, stride, corefunc, "krnpad") for k in kernel_range]) corefunc = "avepool_chw_krnpad_k4_Nx2_N_even" stride = 0
def test_twosum(self): fc = Func() self.assertEqual(fc.two_sum(2, 3), 5) self.assertEqual(fc.two_sum(2, 4), 6) self.assertEqual(fc.two_sum(2, 5), 7) self.assertEqual(fc.two_sum(2, 6), 8)
#!/usr/bin/env python """ To run these tests, execute "python path.py". Example use: define a few nodes, here four nodes in 2D: >>> path = ((-100., -100.), (0., -50.), (0., 50.), (100., 100.)) and construct a path connecting them by default with 4-equidistant nodes >>> p = Path(path) This builds linear, quadratic or spline representation of the path, depending on the number of nodes. Now you can evaluate the path function at any point (between 0 and 1), the equidistant values 0, 1/3, 2/3 and 1 correspond to the four nodes we provided: >>> p(0) array([-100., -100.]) >>> p(1) array([ 100., 100.]) Rounding is only for tests to succeed despite the finite precision: >>> from numpy import round >>> round(p(1./3.), 10)
def inject(self, **kwargs): """injects user-defind code Args: raw (str): the data to inject when injecting raw bytes asm (str): the assembly to inject when injecting assembly c (str): the C code to inject when injecting C code internal (bool): ? TODO is_asm (bool): is the content assembling lang mark_func (bool): ? TODO size (int): how many size to inject target (str): ? TODO desc (str): descritpion of the data to inject silent (bool): silent mode Returns: int: injected address """ internal = kwargs.get('internal', False) is_asm = kwargs.get('is_asm', False) mark_func = kwargs.get('mark_func', False) return_size = kwargs.get('size', False) target = kwargs.get('target', 'patch') desc = kwargs.get('desc', '') if desc: desc = ' | "%s"' % desc # injected address is the next allocation position addr = self.binary.next_alloc(target) c = kwargs.get('c') if c: asm = compiler.compile(c, self.binary.linker) raw = self.asm(asm, addr=addr, att_syntax=True) typ = 'c' is_asm = True else: raw, typ = self._compile(addr, **kwargs) self._lint(addr, raw, typ, is_asm=kwargs.get('is_asm')) if typ == 'asm': ret = self.asm(self.arch.ret()) if raw[-len(ret):] != ret and not internal: self.warn('Injected asm does not return!') self.info( pfcol('INJECT') + '@0x%x-0x%x%s' % (addr, addr + len(raw), desc)) if not kwargs.get('silent'): if typ == 'asm' or is_asm: self.debug(dis=self.arch.dis(raw, addr=addr)) else: self.debug(binascii.hexlify(raw)) # then we allocate addr = self.binary.alloc(len(raw), target=target) if mark_func: self.marked_funcs.append(Func(self, addr, len(raw))) self.elf.write(addr, raw) if return_size: return addr, len(raw) else: return addr
#------------------------------------------------------------ # Create a list of specialization functions for fx16 #------------------------------------------------------------ #conv2d_chw_str1 can only be used with stride==1 and it is using vmac #conv2d_chw can be used for any stride and is using dmac #conv2d_chw_nopad_k1x1_str1 is optimized for 1x1 kernelsize and stride==1 #construct the different specializtions for stride 1 and kernel 1x1 corefunc = "conv2d_chw_nopad_k1x1_str1" stride = 1 k = 1 channel_range = [0, 1, 3, 4] f_list.extend([ Func(fbase, k, k, ch, stride, stride, corefunc, "nopad") for ch in channel_range ]) #stride = 1, any kernel size, any channel size corefunc = "conv2d_chw_str1" stride = 1 kernel_range = [ 2, 3, 4, 5, 6, 7 ] #above 8x8 there is less than 20% benefit of using specialized version channel_range = [ 0, 1 ] #for larger number of channels the generic channel case has similar performance. maybe 3 channels is still useful f_list.extend([ Func(fbase, k, k, ch, stride, stride, corefunc, "krnpad") for k in kernel_range for ch in channel_range
include_list = ["mli_krn_maxpool_chw.h"] define_list = [] # commandline arguments can be used to generate a specific output 'fx16' | 'fx8' | 'header'] # if no arguments are given, all files are generated. no_args = len(sys.argv) == 1 #------------------------------------------------------------ # Create a list of specialization functions for fx16 #------------------------------------------------------------ corefunc = "maxpool_chw_nopad" stride = 1 kernel_range = [2] channel_range = [0] f_list.extend([ Func(fbase, k, k, ch, stride, stride, corefunc, "nopad") for k in kernel_range for ch in channel_range ]) corefunc = "maxpool_chw_nopad" stride = 0 kernel_range = range(2, 11) channel_range = [0] f_list.extend([ Func(fbase, k, k, ch, stride, stride, corefunc, "nopad") for k in kernel_range for ch in channel_range ]) #stride = 0, 1xk and kx1 versions corefunc = "maxpool_chw_nopad" stride = 0
def test_twomul(self): fc = Func() self.assertEqual(fc.two_mul(2, 3), 6)