def _contract4pdm(kern, eri, civec, norb, nelec, link_index=None): if isinstance(nelec, (int, numpy.integer)): neleca = nelecb = nelec//2 else: neleca, nelecb = nelec if link_index is None: link_indexa = fci.cistring.gen_linkstr_index(range(norb), neleca) link_indexb = fci.cistring.gen_linkstr_index(range(norb), nelecb) else: link_indexa, link_indexb = link_index na,nlinka = link_indexa.shape[:2] nb,nlinkb = link_indexb.shape[:2] fdm2 = numpy.empty((norb,norb,norb,norb)) fdm3 = numpy.empty((norb,norb,norb,norb,norb,norb)) eri = numpy.ascontiguousarray(eri) libmc.NEVPTcontract(getattr(libmc, kern), fdm2.ctypes.data_as(ctypes.c_void_p), fdm3.ctypes.data_as(ctypes.c_void_p), eri.ctypes.data_as(ctypes.c_void_p), civec.ctypes.data_as(ctypes.c_void_p), ctypes.c_int(norb), ctypes.c_int(na), ctypes.c_int(nb), ctypes.c_int(nlinka), ctypes.c_int(nlinkb), link_indexa.ctypes.data_as(ctypes.c_void_p), link_indexb.ctypes.data_as(ctypes.c_void_p)) for i in range(norb): for j in range(i): fdm3[j,:,i] = fdm3[i,:,j].transpose(1,0,2,3) fdm3[j,i,i,:] += fdm2[j,:] fdm3[j,:,i,j] -= fdm2[i,:] return fdm3
def hashCode(str): res = c_int(0) if not isinstance(str, bytes): str = str.encode() for i in bytearray(str): res = c_int(c_int(res.value * 0x1f).value + i) return res.value
def load_texture(renderer, name, colorkey=None): fullname = os.path.join('data', name) fullname = byteify(fullname, 'utf-8') surface = sdl2.SDL_LoadBMP(fullname) """ i'm not sure how to extract the pixel format, so instead: - force pixel format to a known format without - set the colorkey - """ #fmt = sdl2.SDL_PixelFormat(sdl2.SDL_PIXELFORMAT_RGBA8888) #surface = sdl2.SDL_ConvertSurface(_surface, fmt, 0) #sdl2.SDL_FreeSurface(_surface) #colorkey = sdl2.SDL_MapRGB(fmt, 255, 0, 0) #sdl2.SDL_SetColorKey(surface, 1, colorkey) texture = sdl2.SDL_CreateTextureFromSurface(renderer, surface) sdl2.SDL_FreeSurface(surface) sdl2.SDL_SetTextureBlendMode(texture, sdl2.SDL_BLENDMODE_BLEND) flags = c_uint32() access = c_int() w = c_int() h = c_int() sdl2.SDL_QueryTexture(texture, byref(flags), byref(access), byref(w), byref(h)) rect = sdl2.SDL_Rect(0, 0, w, h) return texture, rect
def inet_ntop(address_family, packed_ip, encoding="UTF-8"): addr = sockaddr() addr.sa_family = address_family addr_size = c_int(sizeof(addr)) ip_string = create_string_buffer(128) ip_string_size = c_int(sizeof(addr)) if address_family == socket.AF_INET: if len(packed_ip) != sizeof(addr.ipv4_addr): raise socket.error('packed IP wrong length for inet_ntop') memmove(addr.ipv4_addr, packed_ip, 4) elif address_family == socket.AF_INET6: if len(packed_ip) != sizeof(addr.ipv6_addr): raise socket.error('packed IP wrong length for inet_ntop') memmove(addr.ipv6_addr, packed_ip, 16) else: raise socket.error('unknown address family') if WSAAddressToStringA(byref(addr), addr_size, None, ip_string, byref(ip_string_size)) != 0: raise socket.error(FormatError()) return (ip_string[:ip_string_size.value - 1]).decode(encoding)
def open(self, path, flags, mode=0): self.require_state("mounted") if not isinstance(path, basestring): raise TypeError('path must be a string') if not isinstance(flags, basestring): raise TypeError('flags must be a string') if not isinstance(mode, int): raise TypeError('mode must be an int') cephfs_flags = 0 if flags == '': cephfs_flags = os.O_RDONLY else: for c in flags: if c == 'r': cephfs_flags |= os.O_RDONLY elif c == 'w': cephfs_flags |= os.O_WRONLY | os.O_TRUNC | os.O_CREAT elif c == '+': cephfs_flags |= os.O_RDWR else: raise OperationNotSupported( "open flags doesn't support %s" % c) ret = self.libcephfs.ceph_open(self.cluster, c_char_p(path), c_int(cephfs_flags), c_int(mode)) if ret < 0: raise make_ex(ret, "error in open '%s'" % path) return ret
def find_cell( self , x , y , z , start_ijk = None): """ Lookup cell containg true position (x,y,z). Will locate the cell in the grid which contains the true position (@x,@y,@z), the return value is as a triplet (i,j,k). The underlying C implementation is not veeery efficient, and can potentially take quite long time. If you provide a good intial guess with the parameter @start_ijk (a tuple (i,j,k)) things can speed up quite substantially. If the location (@x,@y,@z) can not be found in the grid, the method will return None. """ if start_ijk: start_index = self.__global_index( ijk = start_ijk ) else: start_index = 0 global_index = cfunc.get_ijk_xyz( self , x , y , z , start_index) if global_index >= 0: i = ctypes.c_int() j = ctypes.c_int() k = ctypes.c_int() cfunc.get_ijk1( self , global_index , ctypes.byref(i) , ctypes.byref(j) , ctypes.byref(k)) return (i.value , j.value , k.value) else: return None
def mlem_emission(data, theta, center, num_grid, iters, init_matrix): """ Applies Maximum-Likelihood Expectation-Maximization (MLEM) method to obtain reconstructions. Parameters ---------- data : ndarray, float32 3-D tomographic data with dimensions: [projections, slices, pixels] theta : ndarray, float32 Projection angles in radians. center : scalar, float32 Pixel index corresponding to the center of rotation axis. num_grid : scalar, int32 Grid size of the econstructed images. iters : scalar int32 Number of mlem iterations. init_matrix : ndarray Initial guess for the reconstruction. Its shape is the same as the reconstructed data. Returns ------- output : ndarray Reconstructed data with dimensions: [slices, num_grid, num_grid] References ---------- - `http://en.wikipedia.org/wiki/Maximum_likelihood \ <http://en.wikipedia.org/wiki/Maximum_likelihood>`_ - `http://en.wikipedia.org/wiki/Expectation%E2%80%93maximization_algorithm \ <http://en.wikipedia.org/wiki/Expectation%E2%80%93maximization_algorithm>`_ - `Dempster, A. P.; Laird, N. M.; Rubin, D. B. Maximum likelihood from incomplete data via the EM algorithm. With discussion. J. Roy. Statist. Soc. Ser. B 39 (1977), no. 1, 1–38 \ <http://www.jstor.org/discover/10.2307/2984875?uid=17682872&uid=3739656&uid=2&uid=3&uid=67&uid=16752384&uid=62&uid=3739256&sid=21103820705023>`_ """ num_projections = np.array(data.shape[0], dtype='int32') num_slices = np.array(data.shape[1], dtype='int32') num_pixels = np.array(data.shape[2], dtype='int32') # Call C function. c_float_p = ctypes.POINTER(ctypes.c_float) librecon.mlem_emission.restype = ctypes.POINTER(ctypes.c_void_p) librecon.mlem_emission(data.ctypes.data_as(c_float_p), theta.ctypes.data_as(c_float_p), ctypes.c_float(center), ctypes.c_int(num_projections), ctypes.c_int(num_slices), ctypes.c_int(num_pixels), ctypes.c_int(num_grid), ctypes.c_int(iters), init_matrix.ctypes.data_as(c_float_p)) return init_matrix
def test_jumps(self): udp = socket(AF_INET, SOCK_DGRAM) udp.sendto(b"a" * 10, ("172.16.1.1", 5000)) udp.close() self.assertGreater(self.stats[c_int(S_IP)].value, 0) self.assertGreater(self.stats[c_int(S_ARP)].value, 0) self.assertGreater(self.stats[c_int(S_EOP)].value, 1)
def pspace(h1e, eri, norb, nelec, hdiag, np=400): if isinstance(nelec, (int, numpy.integer)): neleca = nelec//2 else: neleca, nelecb = nelec assert(neleca == nelecb) h1e = numpy.ascontiguousarray(h1e) eri = pyscf.ao2mo.restore(1, eri, norb) na = cistring.num_strings(norb, neleca) addr = numpy.argsort(hdiag)[:np] # symmetrize addra/addrb addra = addr // na addrb = addr % na stra = numpy.array([cistring.addr2str(norb,neleca,ia) for ia in addra], dtype=numpy.long) strb = numpy.array([cistring.addr2str(norb,neleca,ib) for ib in addrb], dtype=numpy.long) np = len(addr) h0 = numpy.zeros((np,np)) libfci.FCIpspace_h0tril(h0.ctypes.data_as(ctypes.c_void_p), h1e.ctypes.data_as(ctypes.c_void_p), eri.ctypes.data_as(ctypes.c_void_p), stra.ctypes.data_as(ctypes.c_void_p), strb.ctypes.data_as(ctypes.c_void_p), ctypes.c_int(norb), ctypes.c_int(np)) for i in range(np): h0[i,i] = hdiag[addr[i]] h0 = pyscf.lib.hermi_triu_(h0) return addr, h0
def set_scheduling_policy(self, pid, policy, priority): print("[sched_setscheduler] pid={}, priority={} ".format( pid, self._policy_to_string(policy), priority)) param = ctypes.c_int(priority) err = libc.sched_setscheduler(pid, ctypes.c_int(policy), ctypes.byref(param)) return err
def _findContentDescendant(self, obj, position): import ctypes import NVDAHelper import NVDAObjects.IAccessible descendantID=ctypes.c_int() descendantOffset=ctypes.c_int() what = self.FINDCONTENTDESCENDANT_POSITIONS.get(position, position) NVDAHelper.localLib.nvdaInProcUtils_IA2Text_findContentDescendant(obj.appModule.helperLocalBindingHandle,obj.windowHandle,obj.IAccessibleObject.uniqueID,what,ctypes.byref(descendantID),ctypes.byref(descendantOffset)) if descendantID.value == 0: # No descendant. raise LookupError("Object has no text descendants") if position == self.POSITION_SELECTION_END: # As we descend, we need the last offset (not the exclusive end offset), # but we want the exclusive end as the final result. descendantOffset.value += 1 # optimisation: If we already have the target obj, don't make a new instance. for cached in obj, getattr(self.obj, "_lastCaretObj", None): if cached and descendantID.value == cached.IA2UniqueID: obj = cached break else: obj=NVDAObjects.IAccessible.getNVDAObjectFromEvent(obj.windowHandle,winUser.OBJID_CLIENT,descendantID.value) if position == textInfos.POSITION_CARET: # Cache for later use. self.obj._lastCaretObj = obj # optimisation: Passing an Offsets position checks nCharacters, which is an extra call we don't need. ti=self._makeRawTextInfo(obj,textInfos.POSITION_FIRST) ti._startOffset=ti._endOffset=descendantOffset.value return ti,obj
def IsTextConsole(): """Checks if console is test only or GUI. Returns: True if the console is text-only, False if GUI is available """ try: # see TN2083 security_lib = ctypes.cdll.LoadLibrary( '/System/Library/Frameworks/Security.framework/Security') # Security.Framework/Headers/AuthSession.h session = -1 session_id = ctypes.c_int(0) attributes = ctypes.c_int(0) ret = security_lib.SessionGetInfo( session, ctypes.byref(session_id), ctypes.byref(attributes)) if ret != 0: return True return not attributes.value & SESSIONHASGRAPHICACCESS except OSError: return True
def poll(): db = dbal.DBAL() print "getting sensors" protocollength = 20 modellength = 20 valuelength = 20 protocol = create_string_buffer(protocollength) model = create_string_buffer(modellength) idvalue = c_int() dataTypes = c_int() while(lib.tdSensor(protocol, protocollength, model, modellength, byref(idvalue), byref(dataTypes)) == 0): print "Sensor: ", protocol.value, model.value, "id:", idvalue.value value = create_string_buffer(valuelength) timestampvalue = c_int() if((dataTypes.value & TELLSTICK_TEMPERATURE) != 0): success = lib.tdSensorValue(protocol.value, model.value, idvalue.value, TELLSTICK_TEMPERATURE, value, valuelength, byref(timestampvalue)) print "Temperature: ", value.value, "C,", datetime.fromtimestamp(timestampvalue.value) if db.get_device(int(idvalue.value)) is not None: db.insert_sensor_data(int(idvalue.value), value.value) if((dataTypes.value & TELLSTICK_HUMIDITY) != 0): success = lib.tdSensorValue(protocol.value, model.value, idvalue.value, TELLSTICK_HUMIDITY, value, valuelength, byref(timestampvalue)) print "Humidity: ", value.value, "%,", datetime.fromtimestamp(timestampvalue.value) print " "
def visit_InteriorPointsLoop(self, node): dim = len(self.output_grid.shape) self.kernel_target = node.target curr_node = None ret_node = None for d in range(dim): target = SymbolRef(self.gen_fresh_var()) self.var_list.append(target.name) for_loop = For( Assign(SymbolRef(target.name, c_int()), Constant(self.ghost_depth[d])), LtE(target, Constant(self.output_grid.shape[d] - self.ghost_depth[d] - 1)), PostInc(target), [], ) if d == 0: ret_node = for_loop else: curr_node.body = [for_loop] if d == dim - 2: curr_node.body.insert(0, OmpParallelFor()) elif d == dim - 1: curr_node.body.insert(0, OmpIvDep()) curr_node = for_loop self.output_index = self.gen_fresh_var() pt = [SymbolRef(x) for x in self.var_list] macro = self.gen_array_macro(self.output_grid_name, pt) curr_node.body = [Assign(SymbolRef(self.output_index, c_int()), macro)] for elem in map(self.visit, node.body): if type(elem) == list: curr_node.body.extend(elem) else: curr_node.body.append(elem) self.kernel_target = None return ret_node
def _parse_script_result(self, ptr): restype = ctypes.c_int() _call("wdGetScriptResultType", ptr, ctypes.byref(restype)) restype = restype.value # See webdriver.cpp # FIXME: I'm *guessing* 6 is a string, see wdExecuteScript in # webdriver.cpp if restype in (1, 6): # String voidp = ctypes.c_void_p() _call("wdGetStringScriptResult", ptr, ctypes.byref(voidp)) return self._extract_string(voidp) elif restype == 2: # Integer n = ctypes.c_int() _call("wdGetNumberScriptResult", ptr, ctypes.byref(n)) return n.value elif restype == 3: # Boolean n = ctypes.c_int() _call("wdGetBooleanScriptResult", ptr, ctypes.byref(n)) return bool(n) elif restype == 4: # WebElement element = ctypes.c_void_p() _call("wdGetElementScriptResult" ,ptr, ctypes.byref(element)) return WebElement(ptr, self) elif restype == 5: # FIXME: None? return None elif restype == 7: # Double n = ctypes.c_double() _call("wdGetDoubleScriptResult", ptr, ctypes.byref(n)) return n.value else: raise ValueError("Unknown result type - %d" % restype)
def visit_InteriorPointsLoop(self, node): dim = len(self.output_grid.shape) self.kernel_target = node.target body = [] body.append( CppDefine("local_array_macro",["d%d" % i for i in range(dim)], self.gen_local_macro()) ) body.append( CppDefine("global_array_macro", ["d%d" % i for i in range(dim)], self.gen_global_macro()) ) global_idx = SymbolRef('global_index') self.output_index = global_idx body.append(Assign(SymbolRef('global_index', ct.c_int()), self.gen_global_index())) body.extend(self.load_shared_memory_block(SymbolRef('block'), Constant(self.ghost_depth * 2))) body.append(FunctionCall(SymbolRef("barrier"), [SymbolRef("CLK_LOCAL_MEM_FENCE")])) for d in range(0, dim): body.append(Assign(SymbolRef('local_id%d' % d, ct.c_int()), Add(get_local_id(d), Constant(self.ghost_depth)))) self.var_list.append("local_id%d" % d) for child in map(self.visit, node.body): if isinstance(child, list): body.extend(child) else: body.append(child) return body
def visit_FunctionDecl(self, node): super(StencilOmpTransformer, self).visit_FunctionDecl(node) for index, arg in enumerate(self.input_grids + (self.output_grid,)): defname = "_%s_array_macro" % node.params[index].name params = ",".join(["_d" + str(x) for x in range(arg.ndim)]) params = "(%s)" % params calc = "((_d%d)" % (arg.ndim - 1) for x in range(arg.ndim - 1): ndim = str(int(arg.strides[x] / arg.itemsize)) calc += "+((_d%s) * %s)" % (str(x), ndim) calc += ")" params = ["_d" + str(x) for x in range(arg.ndim)] node.defn.insert(0, CppDefine(defname, params, calc)) abs_decl = FunctionDecl(c_int(), SymbolRef("abs"), [SymbolRef("n", c_int())]) macro = CppDefine( "min", [SymbolRef("_a"), SymbolRef("_b")], TernaryOp(Lt(SymbolRef("_a"), SymbolRef("_b")), SymbolRef("_a"), SymbolRef("_b")), ) node.params.append(SymbolRef("duration", POINTER(c_float))) start_time = Assign(SymbolRef("start_time", c_double()), omp_get_wtime()) node.defn.insert(0, start_time) end_time = Assign(Deref(SymbolRef("duration")), Sub(omp_get_wtime(), SymbolRef("start_time"))) node.defn.append(end_time) return [IncludeOmpHeader(), abs_decl, macro, node]
def spawn(self, *args, **kw): """ Spawn a process on designated node with given location and environment variables. @keyword where: which node to run the spawned process. @type where: int @keyword envar: environmental variables. @type envar: dict @return: tid and event. @rtype: tuple """ import os from ctypes import c_void_p, c_int, c_char_p, byref # get where. where = kw.pop('where') # get environment variables. envar = self.paths.copy() envar.pop('LD_LIBRARY_PATH', None) # the var causes segfault. envar['PBS_INSTALL'] = os.environ['PBS_INSTALL'] envar.update(kw.pop('envar', dict())) envp = byref((c_char_p*len(envar))(*[ '%s=%s' % (k, envar[k]) for k in envar])) # spawn. tid = c_int() event = c_int() self._clib_torque.tm_spawn( c_int(len(args)), byref((c_char_p*len(args))(*args)), envp, c_int(where), byref(tid), byref(event), ) return tid.value, event.value
def hybrid_coeff(xc_code, spin=1): x_code = is_hybrid_xc(xc_code) if x_code is not None: libdft.VXChybrid_coeff.restype = ctypes.c_double return libdft.VXChybrid_coeff(ctypes.c_int(x_code), ctypes.c_int(spin)) else: return 0
def contract_2e(eri, fcivec, norb, nelec, link_index=None, orbsym=[]): assert(fcivec.flags.c_contiguous) if not list(orbsym): return direct_spin0.contract_2e(eri, fcivec, norb, nelec, link_index) eri = pyscf.ao2mo.restore(4, eri, norb) if link_index is None: if isinstance(nelec, (int, numpy.integer)): neleca = nelec//2 else: neleca, nelecb = nelec assert(neleca == nelecb) link_index = cistring.gen_linkstr_index_trilidx(range(norb), neleca) na,nlink,_ = link_index.shape ci1 = numpy.empty((na,na)) eri, link_index, dimirrep = \ direct_spin1_symm.reorder4irrep(eri, norb, link_index, orbsym) dimirrep = numpy.array(dimirrep, dtype=numpy.int32) libfci.FCIcontract_2e_spin0_symm(eri.ctypes.data_as(ctypes.c_void_p), fcivec.ctypes.data_as(ctypes.c_void_p), ci1.ctypes.data_as(ctypes.c_void_p), ctypes.c_int(norb), ctypes.c_int(na), ctypes.c_int(nlink), link_index.ctypes.data_as(ctypes.c_void_p), dimirrep.ctypes.data_as(ctypes.c_void_p), ctypes.c_int(len(dimirrep))) return pyscf.lib.transpose_sum(ci1, inplace=True)
def run(): ipdb.routes.add({"dst": "224.0.0.0/4", "oif": ifc.index}).commit() with ipdb.create(ifname="vxlan0", kind="vxlan", vxlan_id=0, vxlan_link=ifc, vxlan_port=htons(4789), vxlan_group=str(mcast), vxlan_flowbased=True, vxlan_collect_metadata=True, vxlan_learning=False) as vx: vx.up() ifc_gc.append(vx.ifname) conf[c_int(1)] = c_int(vx.index) ipr.tc("add", "ingress", vx.index, "ffff:") ipr.tc("add-filter", "bpf", vx.index, ":1", fd=ingress_fn.fd, name=ingress_fn.name, parent="ffff:", action="drop", classid=1) for i in range(0, 2): vni = 10000 + i with ipdb.create(ifname="br%d" % i, kind="bridge") as br: v = ipdb.create(ifname="dummy%d" % i, kind="dummy").up().commit() mcast_key = mac2host.Key(0xFFFFFFFFFFFF, v.index, 0) mcast_leaf = mac2host.Leaf(vni, mcast.value, 0, 0) mac2host[mcast_key] = mcast_leaf ipr.tc("add", "sfq", v.index, "1:") ipr.tc("add-filter", "bpf", v.index, ":1", fd=egress_fn.fd, name=egress_fn.name, parent="1:", action="drop", classid=1) br.add_port(v) br.up() ifc_gc.append(v.ifname) ifc_gc.append(br.ifname) vni2if[c_uint(vni)] = c_int(v.index) ipaddr = "99.1.%d.%d/24" % (i, host_id + 1) br.add_ip(ipaddr)
def _sort_t2_vooo(mycc, orbsym, t1, t2, eris): ovoo = numpy.asarray(eris.ovoo) nocc, nvir = t1.shape if mycc.mol.symmetry: orbsym = numpy.asarray(orbsym, dtype=numpy.int32) o_sorted = _irrep_argsort(orbsym[:nocc]) v_sorted = _irrep_argsort(orbsym[nocc:]) mo_energy = eris.fock.diagonal() mo_energy = numpy.hstack((mo_energy[:nocc][o_sorted], mo_energy[nocc:][v_sorted])) t1T = numpy.asarray(t1.T[v_sorted][:,o_sorted], order='C') t2T = lib.transpose(t2.reshape(nocc**2,-1)) _ccsd.libcc.CCsd_t_sort_t2(t2.ctypes.data_as(ctypes.c_void_p), t2T.ctypes.data_as(ctypes.c_void_p), orbsym.ctypes.data_as(ctypes.c_void_p), ctypes.c_int(nocc), ctypes.c_int(nvir)) t2T = t2.reshape(nvir,nvir,nocc,nocc) vooo = numpy.empty((nvir,nocc,nocc,nocc)) _ccsd.libcc.CCsd_t_sort_vooo(vooo.ctypes.data_as(ctypes.c_void_p), ovoo.ctypes.data_as(ctypes.c_void_p), orbsym.ctypes.data_as(ctypes.c_void_p), ctypes.c_int(nocc), ctypes.c_int(nvir)) ovoo = None else: t1T = t1.T.copy() t2T = lib.transpose(t2.reshape(nocc**2,-1)) t2T = lib.transpose(t2T.reshape(-1,nocc,nocc), axes=(0,2,1), out=t2) t2T = t2T.reshape(nvir,nvir,nocc,nocc) vooo = ovoo.transpose(1,0,2,3).copy() mo_energy = numpy.asarray(eris.fock.diagonal(), order='C') return mo_energy, t1T, t2T, vooo
def __init__(self, g_pool,atb_pos=(0,0)): Plugin.__init__(self) self.active = False self.detected = False self.g_pool = g_pool self.pos = None self.smooth_pos = 0.,0. self.smooth_vel = 0. self.sample_site = (-2,-2) self.counter = 0 self.counter_max = 30 self.candidate_ellipses = [] self.show_edges = c_bool(0) self.aperture = 7 self.dist_threshold = c_int(10) self.area_threshold = c_int(30) self.world_size = None self.stop_marker_found = False self.auto_stop = 0 self.auto_stop_max = 30 atb_label = "calibrate using handheld marker" # Creating an ATB Bar is required. Show at least some info about the Ref_Detector self._bar = atb.Bar(name = self.__class__.__name__, label=atb_label, help="ref detection parameters", color=(50, 50, 50), alpha=100, text='light', position=atb_pos,refresh=.3, size=(300, 100)) self._bar.add_button("start/stop", self.start_stop, key='c') self._bar.add_var("show edges",self.show_edges, group="Advanced")
def takebak_2d(out, a, idx, idy): '''Reverse operation of take_2d. Equivalent to out[idx[:,None],idy] += a for a 2D array. Examples: >>> out = numpy.zeros((3,3)) >>> takebak_2d(out, numpy.ones((2,2)), [0,2], [0,2]) [[ 1. 0. 1.] [ 0. 0. 0.] [ 1. 0. 1.]] ''' assert(out.flags.c_contiguous) a = numpy.asarray(a, order='C') if a.dtype == numpy.double: fn = _np_helper.NPdtakebak_2d else: fn = _np_helper.NPztakebak_2d idx = numpy.asarray(idx, dtype=numpy.int32) idy = numpy.asarray(idy, dtype=numpy.int32) fn(out.ctypes.data_as(ctypes.c_void_p), a.ctypes.data_as(ctypes.c_void_p), idx.ctypes.data_as(ctypes.c_void_p), idy.ctypes.data_as(ctypes.c_void_p), ctypes.c_int(out.shape[1]), ctypes.c_int(a.shape[1]), ctypes.c_int(idx.size), ctypes.c_int(idy.size)) return out
def createTopAndFrontMaps(raw, num, top_flip, front_flip, top_paras, front_paras, c_lib_path): [TOP_X_MIN, TOP_X_MAX, TOP_Y_MIN, TOP_Y_MAX, TOP_Z_MIN, TOP_Z_MAX, TOP_X_DIVISION, TOP_Y_DIVISION, TOP_Z_DIVISION, Xn, Yn, Zn] = top_paras [FRONT_PHI_MIN, FRONT_PHI_MAX, FRONT_THETA_MIN, FRONT_THETA_MAX, FRONT_PHI_DIVISION, FRONT_THETA_DIVISION, Rn, Cn, Fn] = front_paras # create a handle to LidarPreprocess.c SharedLib = ctypes.cdll.LoadLibrary(c_lib_path) # call the C function to create top view maps # The np array indata will be edited by createTopViewMaps to populate it with the 8 top view maps SharedLib.createTopAndFrontMaps(ctypes.c_void_p(raw.ctypes.data), ctypes.c_int(num), ctypes.c_void_p(top_flip.ctypes.data), ctypes.c_void_p(front_flip.ctypes.data), ctypes.c_float(TOP_X_MIN), ctypes.c_float(TOP_X_MAX), ctypes.c_float(TOP_Y_MIN), ctypes.c_float(TOP_Y_MAX), ctypes.c_float(TOP_Z_MIN), ctypes.c_float(TOP_Z_MAX), ctypes.c_float(TOP_X_DIVISION), ctypes.c_float(TOP_Y_DIVISION), ctypes.c_float(TOP_Z_DIVISION), ctypes.c_int(Xn), ctypes.c_int(Yn), ctypes.c_int(Zn), ctypes.c_float(FRONT_PHI_MIN), ctypes.c_float(FRONT_PHI_MAX), ctypes.c_float(FRONT_THETA_MIN), ctypes.c_float(FRONT_THETA_MAX), ctypes.c_float(FRONT_PHI_DIVISION), ctypes.c_float(FRONT_THETA_DIVISION), ctypes.c_float(Cn), ctypes.c_float(Rn), ctypes.c_float(Fn) )
def pack_tril(mat, axis=-1, out=None): '''flatten the lower triangular part of a matrix. Given mat, it returns mat[...,numpy.tril_indices(mat.shape[0])] Examples: >>> pack_tril(numpy.arange(9).reshape(3,3)) [0 3 4 6 7 8] ''' if mat.size == 0: return numpy.zeros(mat.shape+(0,), dtype=mat.dtype) if mat.ndim == 2: count, nd = 1, mat.shape[0] shape = nd*(nd+1)//2 else: count, nd = mat.shape[:2] shape = (count, nd*(nd+1)//2) if mat.ndim == 2 or axis == -1: mat = numpy.asarray(mat, order='C') out = numpy.ndarray(shape, mat.dtype, buffer=out) if mat.dtype == numpy.double: fn = _np_helper.NPdpack_tril_2d else: fn = _np_helper.NPzpack_tril_2d fn(ctypes.c_int(count), ctypes.c_int(nd), out.ctypes.data_as(ctypes.c_void_p), mat.ctypes.data_as(ctypes.c_void_p)) return out else: # pack the leading two dimension assert(axis == 0) out = mat[numpy.tril_indices(nd)] return out
def take_2d(a, idx, idy, out=None): '''Equivalent to a[idx[:,None],idy] for a 2D array. Examples: >>> out = numpy.arange(9.).reshape(3,3) >>> take_2d(a, [0,2], [0,2]) [[ 0. 2.] [ 6. 8.]] ''' a = numpy.asarray(a, order='C') out = numpy.ndarray((len(idx),len(idy)), dtype=a.dtype, buffer=out) if a.dtype == numpy.double: fn = _np_helper.NPdtake_2d else: fn = _np_helper.NPztake_2d idx = numpy.asarray(idx, dtype=numpy.int32) idy = numpy.asarray(idy, dtype=numpy.int32) fn(out.ctypes.data_as(ctypes.c_void_p), a.ctypes.data_as(ctypes.c_void_p), idx.ctypes.data_as(ctypes.c_void_p), idy.ctypes.data_as(ctypes.c_void_p), ctypes.c_int(out.shape[1]), ctypes.c_int(a.shape[1]), ctypes.c_int(idx.size), ctypes.c_int(idy.size)) return out
def load(cls, filename): """ @brief Loads a png from a file as a 16-bit heightmap. @param filename Name of target .png image @returns A 16-bit, 1-channel image. """ # Get various png parameters so that we can allocate the # correct amount of storage space for the new image dx, dy, dz = ctypes.c_float(), ctypes.c_float(), ctypes.c_float() ni, nj = ctypes.c_int(), ctypes.c_int() libfab.load_png_stats(filename, ni, nj, dx, dy, dz) # Create a python image data structure img = cls(ni.value, nj.value, channels=1, depth=16) # Add bounds to the image if math.isnan(dx.value): print 'Assuming 72 dpi for x resolution.' img.xmin, img.xmax = 0, 72*img.width/25.4 else: img.xmin, img.xmax = 0, dx.value if math.isnan(dy.value): print 'Assuming 72 dpi for y resolution.' img.ymin, img.ymax = 0, 72*img.height/25.4 else: img.ymin, img.ymax = 0, dy.value if not math.isnan(dz.value): img.zmin, img.zmax = 0, dz.value # Load the image data from the file libfab.load_png(filename, img.pixels) img.filename = filename return img
def Xbeam_Asbly_Frigid(XBINPUT, NumNodes_tot, XBELEM, XBNODE, \ PosIni, PsiIni, PosDefor, PsiDefor, NumDof, \ frf, FrigidFull_foll, frd, FrigidFull_dead, Cao): """@brief Python wrapper for f_xbeam_asbly_frigid. @details Numpy arrays are mutable so the changes made here are reflected in the data of the calling script after execution.""" f_xbeam_asbly_frigid( \ ct.byref(ct.c_int(XBINPUT.NumElems)),\ ct.byref(NumNodes_tot),\ XBELEM.NumNodes.ctypes.data_as(ct.POINTER(ct.c_int)),\ XBELEM.MemNo.ctypes.data_as(ct.POINTER(ct.c_int)),\ XBELEM.Conn.ctypes.data_as(ct.POINTER(ct.c_int)),\ XBELEM.Master.ctypes.data_as(ct.POINTER(ct.c_int)),\ XBELEM.Length.ctypes.data_as(ct.POINTER(ct.c_double)),\ XBELEM.PreCurv.ctypes.data_as(ct.POINTER(ct.c_double)),\ XBELEM.Psi.ctypes.data_as(ct.POINTER(ct.c_double)),\ XBELEM.Vector.ctypes.data_as(ct.POINTER(ct.c_double)),\ XBNODE.Master.ctypes.data_as(ct.POINTER(ct.c_int)),\ XBNODE.Vdof.ctypes.data_as(ct.POINTER(ct.c_int)),\ XBNODE.Fdof.ctypes.data_as(ct.POINTER(ct.c_int)),\ PosIni.ctypes.data_as(ct.POINTER(ct.c_double)),\ PsiIni.ctypes.data_as(ct.POINTER(ct.c_double)),\ PosDefor.ctypes.data_as(ct.POINTER(ct.c_double)),\ PsiDefor.ctypes.data_as(ct.POINTER(ct.c_double)),\ ct.byref(ct.c_int(Settings.DimMat)),\ ct.byref(NumDof),\ ct.byref(frf), \ FrigidFull_foll.ctypes.data_as(ct.POINTER(ct.c_double)),\ ct.byref(frd),\ FrigidFull_dead.ctypes.data_as(ct.POINTER(ct.c_double)),\ Cao.ctypes.data_as(ct.POINTER(ct.c_double)) )
def make_rdm1_spin1(fname, cibra, ciket, norb, nelec, link_index=None): assert(cibra is not None and ciket is not None) cibra = numpy.asarray(cibra, order='C') ciket = numpy.asarray(ciket, order='C') if link_index is None: neleca, nelecb = _unpack_nelec(nelec) link_indexa = link_indexb = cistring.gen_linkstr_index(range(norb), neleca) if neleca != nelecb: link_indexb = cistring.gen_linkstr_index(range(norb), nelecb) else: link_indexa, link_indexb = link_index na,nlinka = link_indexa.shape[:2] nb,nlinkb = link_indexb.shape[:2] assert(cibra.size == na*nb) assert(ciket.size == na*nb) rdm1 = numpy.empty((norb,norb)) fn = getattr(librdm, fname) fn(rdm1.ctypes.data_as(ctypes.c_void_p), cibra.ctypes.data_as(ctypes.c_void_p), ciket.ctypes.data_as(ctypes.c_void_p), ctypes.c_int(norb), ctypes.c_int(na), ctypes.c_int(nb), ctypes.c_int(nlinka), ctypes.c_int(nlinkb), link_indexa.ctypes.data_as(ctypes.c_void_p), link_indexb.ctypes.data_as(ctypes.c_void_p)) return rdm1.T
def ServerGetVARs(self, Valores, Offsets, Quantidade, Tamanho, Handle): try: self.smlib.ServerGetVARs.argtypes = (ctypes.POINTER(ctypes.c_float), ctypes.POINTER(ctypes.c_float), ctypes.POINTER(ctypes.c_int), ctypes.c_int, ctypes.c_void_p) return self.smlib.ServerGetVARs(Valores, Offsets, Quantidade, ctypes.c_int(Tamanho), ctypes.c_void_p(Handle)) except Exception as e: logger.debug('ServerGetVARs error: '+ str(e))
def SendInput(*inputs): nInputs = len(inputs) LPINPUT = INPUT * nInputs pInputs = LPINPUT(*inputs) cbSize = ctypes.c_int(ctypes.sizeof(INPUT)) return ctypes.windll.user32.SendInput(nInputs, pInputs, cbSize)
import ctypes import ctypes.wintypes as wintypes LPOFNHOOKPROC = ctypes.c_voidp # TODO LPCTSTR = LPTSTR = ctypes.c_wchar_p # Avoid any blurry display awareness = ctypes.c_int() errorCode = ctypes.windll.shcore.GetProcessDpiAwareness( 0, ctypes.addressof(awareness)) # Set DPI Awareness (Windows 10 and 8) errorCode = ctypes.windll.shcore.SetProcessDpiAwareness(2) # Set DPI Awareness (Windows 7 and Vista) success = ctypes.windll.user32.SetProcessDPIAware() class OPENFILENAME(ctypes.Structure): _fields_ = [("lStructSize", wintypes.DWORD), ("hwndOwner", wintypes.HWND), ("hInstance", wintypes.HINSTANCE), ("lpstrFilter", LPCTSTR), ("lpstrCustomFilter", LPTSTR), ("nMaxCustFilter", wintypes.DWORD), ("nFilterIndex", wintypes.DWORD), ("lpstrFile", LPTSTR), ("nMaxFile", wintypes.DWORD), ("lpstrFileTitle", LPTSTR), ("nMaxFileTitle", wintypes.DWORD), ("lpstrInitialDir", LPCTSTR), ("lpstrTitle", LPCTSTR), ("flags", wintypes.DWORD), ("nFileOffset", wintypes.WORD), ("nFileExtension", wintypes.WORD), ("lpstrDefExt", LPCTSTR), ("lCustData", wintypes.LPARAM), ("lpfnHook", LPOFNHOOKPROC), ("lpTemplateName", LPCTSTR), ("pvReserved", wintypes.LPVOID), ("dwReserved", wintypes.DWORD), ("flagsEx", wintypes.DWORD)]
def get_integrity_level(): """Returns the integrity level of the current process as a string. TODO(maruel): It'd be nice to make it work on cygwin. The problem is that ctypes.windll is unaccessible and it is not known to the author how to use stdcall convention through ctypes.cdll. """ if get_os_version_number() == u'5.1': # Integrity level is Vista+. return None mapping = { 0x0000: u'untrusted', 0x1000: u'low', 0x2000: u'medium', 0x2100: u'medium high', 0x3000: u'high', 0x4000: u'system', 0x5000: u'protected process', } # This was specifically written this way to work on cygwin except for the # windll part. If someone can come up with a way to do stdcall on cygwin, that # would be appreciated. BOOL = ctypes.c_long DWORD = ctypes.c_ulong HANDLE = ctypes.c_void_p class SID_AND_ATTRIBUTES(ctypes.Structure): _fields_ = [ ('Sid', ctypes.c_void_p), ('Attributes', DWORD), ] class TOKEN_MANDATORY_LABEL(ctypes.Structure): _fields_ = [ ('Label', SID_AND_ATTRIBUTES), ] TOKEN_READ = DWORD(0x20008) # Use the same casing as in the C declaration: # https://msdn.microsoft.com/library/windows/desktop/aa379626.aspx TokenIntegrityLevel = ctypes.c_int(25) ERROR_INSUFFICIENT_BUFFER = 122 # All the functions used locally. First open the process' token, then query # the SID to know its integrity level. ctypes.windll.kernel32.GetLastError.argtypes = () ctypes.windll.kernel32.GetLastError.restype = DWORD ctypes.windll.kernel32.GetCurrentProcess.argtypes = () ctypes.windll.kernel32.GetCurrentProcess.restype = ctypes.c_void_p ctypes.windll.advapi32.OpenProcessToken.argtypes = ( HANDLE, DWORD, ctypes.POINTER(HANDLE)) ctypes.windll.advapi32.OpenProcessToken.restype = BOOL ctypes.windll.advapi32.GetTokenInformation.argtypes = ( HANDLE, ctypes.c_long, ctypes.c_void_p, DWORD, ctypes.POINTER(DWORD)) ctypes.windll.advapi32.GetTokenInformation.restype = BOOL ctypes.windll.advapi32.GetSidSubAuthorityCount.argtypes = [ctypes.c_void_p] ctypes.windll.advapi32.GetSidSubAuthorityCount.restype = ctypes.POINTER( ctypes.c_ubyte) ctypes.windll.advapi32.GetSidSubAuthority.argtypes = (ctypes.c_void_p, DWORD) ctypes.windll.advapi32.GetSidSubAuthority.restype = ctypes.POINTER(DWORD) # First open the current process token, query it, then close everything. token = ctypes.c_void_p() proc_handle = ctypes.windll.kernel32.GetCurrentProcess() if not ctypes.windll.advapi32.OpenProcessToken( proc_handle, TOKEN_READ, ctypes.byref(token)): logging.error('Failed to get process\' token') return None if token.value == 0: logging.error('Got a NULL token') return None try: # The size of the structure is dynamic because the TOKEN_MANDATORY_LABEL # used will have the SID appened right after the TOKEN_MANDATORY_LABEL in # the heap allocated memory block, with .Label.Sid pointing to it. info_size = DWORD() if ctypes.windll.advapi32.GetTokenInformation( token, TokenIntegrityLevel, ctypes.c_void_p(), info_size, ctypes.byref(info_size)): logging.error('GetTokenInformation() failed expectation') return None if info_size.value == 0: logging.error('GetTokenInformation() returned size 0') return None if ctypes.windll.kernel32.GetLastError() != ERROR_INSUFFICIENT_BUFFER: logging.error( 'GetTokenInformation(): Unknown error: %d', ctypes.windll.kernel32.GetLastError()) return None token_info = TOKEN_MANDATORY_LABEL() ctypes.resize(token_info, info_size.value) if not ctypes.windll.advapi32.GetTokenInformation( token, TokenIntegrityLevel, ctypes.byref(token_info), info_size, ctypes.byref(info_size)): logging.error( 'GetTokenInformation(): Unknown error with buffer size %d: %d', info_size.value, ctypes.windll.kernel32.GetLastError()) return None p_sid_size = ctypes.windll.advapi32.GetSidSubAuthorityCount( token_info.Label.Sid) res = ctypes.windll.advapi32.GetSidSubAuthority( token_info.Label.Sid, p_sid_size.contents.value - 1) value = res.contents.value return mapping.get(value) or u'0x%04x' % value finally: ctypes.windll.kernel32.CloseHandle(token)
def jtag_get_reg(self, reg, width, value): out_value = ctypes.c_int() if not self.module.cable_jtag_get_reg(reg, width, ctypes.byref(out_value), value): return None return out_value.value
def locals_to_fast(frame): # type: (FrameType) -> None ctypes.pythonapi.PyFrame_LocalsToFast(ctypes.py_object(frame), ctypes.c_int(0))
def chew_func(data, arg): ''' Callback for chew. ''' print '+--> In chew: cpu :', c_int(data.contents.dtpda_cpu).value return 0
def ClientGetVARs(self, Valores, Tamanho, Handle): try: self.smlib.ClientGetVARs.argtypes = (ctypes.POINTER(ctypes.c_float), ctypes.c_int, ctypes.c_void_p) return self.smlib.ClientGetVARs(Valores, ctypes.c_int(Tamanho), ctypes.c_void_p(Handle)) except Exception as e: logger.debug('ClientGetVARs error: '+ str(e))
def check_router_settings(config): os_type = get_os_type() if os_type == OS_LINUX: if open('/proc/sys/net/ipv4/ip_forward', 'r').read()[0:1] == "0": internal_print("The IP forwarding is not set.", -1) internal_print( "Please use the following two commands to set it properly (root needed):\n#\tsysctl -w net.ipv4.ip_forward=1\n#\tiptables -t nat -A POSTROUTING -s {0}/{1} -o [YOUR_INTERFACE/e.g./eth0] -j MASQUERADE\n" .format(config.get("Global", "serverip"), config.get("Global", "servernetmask"))) return False if os_type == OS_MACOSX: import ctypes import ctypes.util # load libc libc_name = ctypes.util.find_library('c') libc = ctypes.CDLL(libc_name, use_errno=True) # get value of forwarding with sysctl fw_value = ctypes.c_int(-1) err = libc.sysctlbyname("net.inet.ip.forwarding", ctypes.byref(fw_value), ctypes.byref(ctypes.c_uint(4)), None, 0) if err < 0: err = ctypes.get_errno() internal_print( "sysctl failed: {0} : {1}".format(err, os.strerror(err)), -1) return False if fw_value.value != 1: internal_print("The IP forwarding is not set.", -1) internal_print( "Please use the following commands to set it properly (root needed):\n#\tsysctl -w net.inet.ip.forwarding=1\nPut the following line into the /etc/pf.conf after the \'nat-anchor \"com.apple/*\"' line:\n#\tnat on en0 from {0}/{1} to any -> (en0)\nThen load the config file with pfctl:\n#\tpfctl -f /etc/pf.conf -e -v" .format(config.get("Global", "serverip"), config.get("Global", "servernetmask"))) return False if os_type == OS_WINDOWS: import _winreg as registry #HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\IPEnableRouter - DWORD 1 - enable #"Routing and Remote Access" service - Enable, start PATH = "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\" try: regkey = registry.OpenKey(registry.HKEY_LOCAL_MACHINE, PATH) router_value = registry.QueryValueEx(regkey, "IPEnableRouter")[0] except WindowsError as e: internal_print( "Cannot get IPEnableRouter value. Registry key cannot be found: {0}" .format(e), -1) return False if router_value != 1: internal_print("The IP forwarding is not set.", -1) internal_print( "Please set the IPEnableRouter value to 1 with the following command:\n\treg add HKLM\\SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters /t REG_DWORD /v IPEnableRouter /d 1 /f\n\tMoreover you need to enable and start the \"Routing and Remote Access\" service.\n" ) return False if os_type == OS_FREEBSD: try: f = open('/etc/rc.conf', 'r') content = f.read() f.close() found = False for line in content.split("\n"): if line.find("gateway_enable") != -1: if line.lower().find("yes") != -1: found = True break if not found: internal_print("The IP forwarding is not set.", -1) internal_print( "Please set the gateway_enable directive to yes in rc.conf.", -1) return False except IOError: internal_print("Could not open rc.conf", -1) return False return True
def SMCreateVARFile(self, Tamanho, Ficheiro): try: self.smlib.SMCreateVARFile.argtypes = (ctypes.c_int, ctypes.POINTER(ctypes.c_char)) return self.smlib.SMCreateVARFile(ctypes.c_int(Tamanho), Ficheiro) except Exception as e: logger.debug('SMCreateVARFile error: '+ str(e))
import ctypes import os if "CUDA_VISIBLE_DEVICES" in os.environ: del os.environ["CUDA_VISIBLE_DEVICES"] if os.name == 'nt': cuda_driver = ctypes.CDLL("nvcuda") else: cuda_driver = ctypes.CDLL("libcuda.so") driver_version = ctypes.c_int() r = cuda_driver.cuDriverGetVersion(ctypes.byref(driver_version)) assert r == 0 v = driver_version.value dcount = ctypes.c_int() cuda_driver.cuInit(0) r = cuda_driver.cuDeviceGetCount(ctypes.byref(dcount)) for i in range(dcount.value): dev = ctypes.c_void_p() major = ctypes.c_int() minor = ctypes.c_int() assert 0 == cuda_driver.cuDeviceGet(ctypes.byref(dev), i) assert 0 == cuda_driver.cuDeviceGetAttribute(ctypes.byref(major), 75, dev) assert 0 == cuda_driver.cuDeviceGetAttribute(ctypes.byref(minor), 76, dev) print(major.value * 10 + minor.value)
def ClientSetVAR(self, Valor, Offset, Handle): try: self.smlib.ClientSetVAR.argtypes = (ctypes.c_float, ctypes.c_int, ctypes.c_void_p) return self.smlib.ClientSetVAR(ctypes.c_float(Valor), ctypes.c_int(Offset), ctypes.c_void_p(Handle)) except Exception as e: logger.debug('ClientSetVAR error: '+ str(e))
def pushpull(self, key, value, out=None, priority=0): """ Performs push and pull a single value or a sequence of values from the store. This function is coalesced form of push and pull operations. This function returns immediately after adding an operator to the engine. Subsequent attempts to read from the `out` variable will be blocked until the pull operation completes. `value` is pushed to the kvstore server for the specified keys and the updated values are pulled from the server to `out`. If `out` is not specified the pulled values are written to `value`. The returned values are guaranteed to be the latest values in the store. pushpull with `RowSparseNDArray` is not supported for dist kvstore. Parameters ---------- key : str, int, or sequence of str or int Keys. value : NDArray, list of NDArray, or list of list of NDArray Values corresponding to the keys. out: NDArray or list of NDArray or list of list of NDArray Values corresponding to the keys. priority : int, optional The priority of the operation. Higher priority operations are likely to be executed before other actions. Examples -------- >>> # pushpull a single key-value pair >>> kv.pushpull('3', mx.nd.ones(shape)*8, out=a) >>> print a.asnumpy() [[ 8. 8. 8.] [ 8. 8. 8.]] >>> # aggregate the value and then pushpull >>> gpus = [mx.gpu(i) for i in range(4)] >>> b = [mx.nd.ones(shape, gpu) for gpu in gpus] >>> kv.pushpull('3', b, out=a) >>> print a.asnumpy() [[ 4. 4. 4.] [ 4. 4. 4.]] >>> # pushpull a list of keys. >>> # single device >>> keys = ['4', '5', '6'] >>> b = [mx.nd.zeros(shape)]*len(keys) >>> kv.pushpull(keys, [mx.nd.ones(shape)]*len(keys), out=b) >>> print b[1].asnumpy() [[ 1. 1. 1.] [ 1. 1. 1.]] >>> # multiple devices: >>> keys = ['7', '8', '9'] >>> b = [[mx.nd.ones(shape, gpu) for gpu in gpus]] * len(keys) >>> kv.pushpull(keys, b) >>> print b[1][1].asnumpy() [[ 4. 4. 4.] [ 4. 4. 4.]] """ cvkeys, cvals, use_str_keys = _ctype_key_value(key, value) if out is not None: cokeys, couts, _ = _ctype_key_value(key, out) else: cokeys = cvkeys couts = cvals if use_str_keys: check_call( _LIB.MXKVStorePushPullEx(self.handle, mx_uint(len(cvkeys)), cvkeys, mx_uint(len(cokeys)), cokeys, cvals, couts, ctypes.c_int(priority))) else: check_call( _LIB.MXKVStorePushPull(self.handle, mx_uint(len(cvkeys)), cvkeys, mx_uint(len(cokeys)), cokeys, cvals, couts, ctypes.c_int(priority)))
def ints(facs, c_shls_slice): nimgs, nkpts = facs.shape drv(fintor, fill, outs, c_xyz, c_ptr_coords, c_nxyz, c_Ls, ctypes.c_int(nimgs), facs.ctypes.data_as(ctypes.c_void_p), ctypes.c_int(nkpts), c_comp, c_shls_slice, c_ao_loc, cintopt, c_atm, c_natm, c_bas, c_nbas, c_env)
def push(self, key, value, priority=0): """ Pushes a single or a sequence of key-value pairs into the store. This function returns immediately after adding an operator to the engine. The actual operation is executed asynchronously. If there are consecutive pushes to the same key, there is no guarantee on the serialization of pushes. The execution of a push does not guarantee that all previous pushes are finished. There is no synchronization between workers. One can use ``_barrier()`` to sync all workers. Parameters ---------- key : str, int, or sequence of str or int Keys. value : NDArray, RowSparseNDArray, list of NDArray or RowSparseNDArray, or list of list of NDArray or RowSparseNDArray Values corresponding to the keys. priority : int, optional The priority of the push operation. Higher priority push operations are likely to be executed before other push actions. Examples -------- >>> # push a single key-value pair >>> kv.push('3', mx.nd.ones(shape)*8) >>> kv.pull('3', out=a) # pull out the value >>> print a.asnumpy() [[ 8. 8. 8.] [ 8. 8. 8.]] >>> # aggregate the value and the push >>> gpus = [mx.gpu(i) for i in range(4)] >>> b = [mx.nd.ones(shape, gpu) for gpu in gpus] >>> kv.push('3', b) >>> kv.pull('3', out=a) >>> print a.asnumpy() [[ 4. 4. 4.] [ 4. 4. 4.]] >>> # push a list of keys. >>> # single device >>> keys = ['4', '5', '6'] >>> kv.push(keys, [mx.nd.ones(shape)]*len(keys)) >>> b = [mx.nd.zeros(shape)]*len(keys) >>> kv.pull(keys, out=b) >>> print b[1].asnumpy() [[ 1. 1. 1.] [ 1. 1. 1.]] >>> # multiple devices: >>> keys = ['7', '8', '9'] >>> b = [[mx.nd.ones(shape, gpu) for gpu in gpus]] * len(keys) >>> kv.push(keys, b) >>> kv.pull(keys, out=b) >>> print b[1][1].asnumpy() [[ 4. 4. 4.] [ 4. 4. 4.]] >>> # push a row_sparse value >>> b = mx.nd.sparse.zeros('row_sparse', shape) >>> kv.init('10', mx.nd.sparse.zeros('row_sparse', shape)) >>> kv.push('10', mx.nd.ones(shape).tostype('row_sparse')) >>> # pull out the value >>> kv.row_sparse_pull('10', row_ids=mx.nd.array([0, 1]), out=b) >>> print b <RowSparseNDArray 2x3 @cpu(0)> """ ckeys, cvals, use_str_keys = _ctype_key_value(key, value) if use_str_keys: check_call( _LIB.MXKVStorePushEx(self.handle, mx_uint(len(ckeys)), ckeys, cvals, ctypes.c_int(priority))) else: check_call( _LIB.MXKVStorePush(self.handle, mx_uint(len(ckeys)), ckeys, cvals, ctypes.c_int(priority)))
def row_sparse_pull(self, key, out=None, priority=0, row_ids=None): """ Pulls a single RowSparseNDArray value or a sequence of RowSparseNDArray values \ from the store with specified row_ids. When there is only one row_id, KVStoreRowSparsePull \ is invoked just once and the result is broadcast to all the rest of outputs. `row_sparse_pull` is executed asynchronously after all previous `pull`/`row_sparse_pull` calls and the last `push` call for the same input key(s) are finished. The returned values are guaranteed to be the latest values in the store. Parameters ---------- key : str, int, or sequence of str or int Keys. out: RowSparseNDArray or list of RowSparseNDArray or list of list of RowSparseNDArray Values corresponding to the keys. The stype is expected to be row_sparse priority : int, optional The priority of the pull operation. Higher priority pull operations are likely to be executed before other pull actions. row_ids : NDArray or list of NDArray The row_ids for which to pull for each value. Each row_id is an 1-D NDArray \ whose values don't have to be unique nor sorted. Examples -------- >>> shape = (3, 3) >>> kv.init('3', mx.nd.ones(shape).tostype('row_sparse')) >>> a = mx.nd.sparse.zeros('row_sparse', shape) >>> row_ids = mx.nd.array([0, 2], dtype='int64') >>> kv.row_sparse_pull('3', out=a, row_ids=row_ids) >>> print a.asnumpy() [[ 1. 1. 1.] [ 0. 0. 0.] [ 1. 1. 1.]] >>> duplicate_row_ids = mx.nd.array([2, 2], dtype='int64') >>> kv.row_sparse_pull('3', out=a, row_ids=duplicate_row_ids) >>> print a.asnumpy() [[ 0. 0. 0.] [ 0. 0. 0.] [ 1. 1. 1.]] >>> unsorted_row_ids = mx.nd.array([1, 0], dtype='int64') >>> kv.row_sparse_pull('3', out=a, row_ids=unsorted_row_ids) >>> print a.asnumpy() [[ 1. 1. 1.] [ 1. 1. 1.] [ 0. 0. 0.]] """ assert (out is not None) assert (row_ids is not None) if isinstance(row_ids, NDArray): row_ids = [row_ids] assert(isinstance(row_ids, list)), \ "row_ids should be NDArray or list of NDArray" first_out = out # whether row_ids are the same single_rowid = False if len(row_ids) == 1 and isinstance(out, list): single_rowid = True first_out = [out[0]] ckeys, cvals, use_str_keys = _ctype_key_value(key, first_out) _, crow_ids, _ = _ctype_key_value(key, row_ids) assert(len(crow_ids) == len(cvals)), \ "the number of row_ids doesn't match the number of values" if use_str_keys: check_call( _LIB.MXKVStorePullRowSparseEx(self.handle, mx_uint(len(ckeys)), ckeys, cvals, crow_ids, ctypes.c_int(priority))) else: check_call( _LIB.MXKVStorePullRowSparse(self.handle, mx_uint(len(ckeys)), ckeys, cvals, crow_ids, ctypes.c_int(priority))) # the result can be copied to other devices without invoking row_sparse_pull # if the indices are the same if single_rowid: for out_i in out[1:]: out[0].copyto(out_i)
env[off + 0] = 2 * numpy.sqrt(numpy.pi) nfitid1 = nbas * 2 + 1 off += n bas[nfitid1, ATOM_OF] = 0 bas[nfitid1, ANG_OF] = 0 bas[nfitid1, KAPPA_OF] = 0 bas[nfitid1, NPRIM_OF] = 1 bas[nfitid1, NCTR_OF] = 1 bas[nfitid1, PTR_EXP] = off env[off + 0] = 0 off += 1 bas[nfitid1, PTR_COEFF] = off env[off + 0] = 2 * numpy.sqrt(numpy.pi) natm = ctypes.c_int(natm) nbas = ctypes.c_int(nbas) c_atm = atm.ctypes.data_as(ctypes.c_void_p) c_bas = bas.ctypes.data_as(ctypes.c_void_p) c_env = env.ctypes.data_as(ctypes.c_void_p) opt = ctypes.POINTER(ctypes.c_void_p)() _cint.CINTlen_spinor.restype = ctypes.c_int def close(v1, vref, count, place): return round(abs(v1 - vref) / count, place) == 0 def test_int3c2e_sph(name, fnref, vref, dim, place): intor = getattr(_cint, name)
def pull(self, key, out=None, priority=0, ignore_sparse=True): """ Pulls a single value or a sequence of values from the store. This function returns immediately after adding an operator to the engine. Subsequent attempts to read from the `out` variable will be blocked until the pull operation completes. `pull` is executed asynchronously after all previous `pull` calls and only the last `push` call for the same input key(s) are finished. The returned values are guaranteed to be the latest values in the store. pull with `RowSparseNDArray` is not supported for dist kvstore. Please use ``row_sparse_pull`` instead. Parameters ---------- key : str, int, or sequence of str or int Keys. out: NDArray or list of NDArray or list of list of NDArray Values corresponding to the keys. priority : int, optional The priority of the pull operation. Higher priority pull operations are likely to be executed before other pull actions. ignore_sparse: bool, optional, default True Whether to ignore sparse arrays in the request. Examples -------- >>> # pull a single key-value pair >>> a = mx.nd.zeros(shape) >>> kv.pull('3', out=a) >>> print a.asnumpy() [[ 2. 2. 2.] [ 2. 2. 2.]] >>> # pull into multiple devices >>> b = [mx.nd.ones(shape, gpu) for gpu in gpus] >>> kv.pull('3', out=b) >>> print b[1].asnumpy() [[ 2. 2. 2.] [ 2. 2. 2.]] >>> # pull a list of key-value pairs. >>> # On single device >>> keys = ['5', '7', '9'] >>> b = [mx.nd.zeros(shape)]*len(keys) >>> kv.pull(keys, out=b) >>> print b[1].asnumpy() [[ 2. 2. 2.] [ 2. 2. 2.]] >>> # On multiple devices >>> keys = ['6', '8', '10'] >>> b = [[mx.nd.ones(shape, gpu) for gpu in gpus]] * len(keys) >>> kv.pull(keys, out=b) >>> print b[1][1].asnumpy() [[ 2. 2. 2.] [ 2. 2. 2.]] """ assert (out is not None) ckeys, cvals, use_str_keys = _ctype_key_value(key, out) if use_str_keys: check_call( _LIB.MXKVStorePullWithSparseEx(self.handle, mx_uint(len(ckeys)), ckeys, cvals, ctypes.c_int(priority), ctypes.c_bool(ignore_sparse))) else: check_call( _LIB.MXKVStorePullWithSparse(self.handle, mx_uint(len(ckeys)), ckeys, cvals, ctypes.c_int(priority), ctypes.c_bool(ignore_sparse)))
def uc_version(): major = ctypes.c_int() minor = ctypes.c_int() combined = _uc.uc_version(ctypes.byref(major), ctypes.byref(minor)) return (major.value, minor.value, combined)
'TM': 4, 'DB': 5, }) wordlen_to_ctypes = ADict({ S7WLBit: ctypes.c_int16, S7WLByte: ctypes.c_int8, S7WLWord: ctypes.c_int16, S7WLDWord: ctypes.c_int32, S7WLReal: ctypes.c_int32, S7WLCounter: ctypes.c_int16, S7WLTimer: ctypes.c_int16, }) block_types = ADict({ 'OB': ctypes.c_int(0x38), 'DB': ctypes.c_int(0x41), 'SDB': ctypes.c_int(0x42), 'FC': ctypes.c_int(0x43), 'SFC': ctypes.c_int(0x44), 'FB': ctypes.c_int(0x45), 'SFB': ctypes.c_int(0x46), }) server_statuses = { 0: 'SrvStopped', 1: 'SrvRunning', 2: 'SrvError', } cpu_statuses = {
c_uint, c_uint, keyboard_state_type, LPWSTR, c_int, c_uint ] ToUnicode.restype = c_int SendInput = user32.SendInput SendInput.argtypes = [c_uint, POINTER(INPUT), c_int] SendInput.restype = c_uint MAPVK_VK_TO_VSC = 0 MAPVK_VSC_TO_VK = 1 VkKeyScan = user32.VkKeyScanW VkKeyScan.argtypes = [WCHAR] VkKeyScan.restype = c_short NULL = c_int(0) WM_KEYDOWN = 0x0100 WM_KEYUP = 0x0101 WM_SYSKEYDOWN = 0x104 # Used for ALT key WM_SYSKEYUP = 0x105 keyboard_event_types = { WM_KEYDOWN: KEY_DOWN, WM_KEYUP: KEY_UP, WM_SYSKEYDOWN: KEY_DOWN, WM_SYSKEYUP: KEY_UP, } from_virtual_key = { 0x03: ('control-break processing', False),
def hook_add(self, htype, callback, user_data=None, begin=1, end=0, arg1=0): _h2 = uc_hook_h() # save callback & user_data self._callback_count += 1 self._callbacks[self._callback_count] = (callback, user_data) cb = None if htype == uc.UC_HOOK_INSN: insn = ctypes.c_int(arg1) if arg1 == x86_const.UC_X86_INS_IN: # IN instruction cb = ctypes.cast(UC_HOOK_INSN_IN_CB(self._hook_insn_in_cb), UC_HOOK_INSN_IN_CB) if arg1 == x86_const.UC_X86_INS_OUT: # OUT instruction cb = ctypes.cast(UC_HOOK_INSN_OUT_CB(self._hook_insn_out_cb), UC_HOOK_INSN_OUT_CB) if arg1 in (x86_const.UC_X86_INS_SYSCALL, x86_const.UC_X86_INS_SYSENTER ): # SYSCALL/SYSENTER instruction cb = ctypes.cast( UC_HOOK_INSN_SYSCALL_CB(self._hook_insn_syscall_cb), UC_HOOK_INSN_SYSCALL_CB) status = _uc.uc_hook_add( self._uch, ctypes.byref(_h2), htype, cb, ctypes.cast(self._callback_count, ctypes.c_void_p), ctypes.c_uint64(begin), ctypes.c_uint64(end), insn) elif htype == uc.UC_HOOK_INTR: cb = ctypes.cast(UC_HOOK_INTR_CB(self._hook_intr_cb), UC_HOOK_INTR_CB) status = _uc.uc_hook_add( self._uch, ctypes.byref(_h2), htype, cb, ctypes.cast(self._callback_count, ctypes.c_void_p), ctypes.c_uint64(begin), ctypes.c_uint64(end)) elif htype == uc.UC_HOOK_INSN_INVALID: cb = ctypes.cast( UC_HOOK_INSN_INVALID_CB(self._hook_insn_invalid_cb), UC_HOOK_INSN_INVALID_CB) status = _uc.uc_hook_add( self._uch, ctypes.byref(_h2), htype, cb, ctypes.cast(self._callback_count, ctypes.c_void_p), ctypes.c_uint64(begin), ctypes.c_uint64(end)) else: if htype in (uc.UC_HOOK_BLOCK, uc.UC_HOOK_CODE): # set callback with wrapper, so it can be called # with this object as param cb = ctypes.cast(UC_HOOK_CODE_CB(self._hookcode_cb), UC_HOOK_CODE_CB) status = _uc.uc_hook_add( self._uch, ctypes.byref(_h2), htype, cb, ctypes.cast(self._callback_count, ctypes.c_void_p), ctypes.c_uint64(begin), ctypes.c_uint64(end)) elif htype & (uc.UC_HOOK_MEM_READ_UNMAPPED | uc.UC_HOOK_MEM_WRITE_UNMAPPED | uc.UC_HOOK_MEM_FETCH_UNMAPPED | uc.UC_HOOK_MEM_READ_PROT | uc.UC_HOOK_MEM_WRITE_PROT | uc.UC_HOOK_MEM_FETCH_PROT): cb = ctypes.cast( UC_HOOK_MEM_INVALID_CB(self._hook_mem_invalid_cb), UC_HOOK_MEM_INVALID_CB) status = _uc.uc_hook_add( self._uch, ctypes.byref(_h2), htype, cb, ctypes.cast(self._callback_count, ctypes.c_void_p), ctypes.c_uint64(begin), ctypes.c_uint64(end)) else: cb = ctypes.cast( UC_HOOK_MEM_ACCESS_CB(self._hook_mem_access_cb), UC_HOOK_MEM_ACCESS_CB) status = _uc.uc_hook_add( self._uch, ctypes.byref(_h2), htype, cb, ctypes.cast(self._callback_count, ctypes.c_void_p), ctypes.c_uint64(begin), ctypes.c_uint64(end)) # save the ctype function so gc will leave it alone. self._ctype_cbs[self._callback_count] = cb if status != uc.UC_ERR_OK: raise UcError(status) return _h2.value
def semi_minor(self): "Returns the Semi Minor Axis for this Spatial Reference." return capi.semi_minor(self.ptr, byref(c_int()))
def listen(queue, is_allowed=lambda *args: True): setup_tables() def process_key(event_type, vk, scan_code, is_extended): global alt_gr_is_pressed global shift_is_pressed name = 'unknown' is_keypad = False if scan_code == alt_gr_scan_code: alt_gr_is_pressed = event_type == KEY_DOWN name = 'alt gr' else: if vk in from_virtual_key: # Pressing AltGr also triggers "right menu" quickly after. We # try to filter out this event. The `alt_gr_is_pressed` flag # is to avoid messing with keyboards that don't even have an # alt gr key. if vk == 165: return True name, is_keypad = from_virtual_key[vk] if vk in possible_extended_keys and not is_extended: is_keypad = True # What the hell Windows? if vk in reversed_extended_keys and is_extended: is_keypad = True elif scan_code in from_scan_code: name = from_scan_code[scan_code][shift_is_pressed] if event_type == KEY_DOWN and name == 'shift': shift_is_pressed = True elif event_type == KEY_UP and name == 'shift': shift_is_pressed = False # Not sure how long this takes, but may need to move it? queue.put( KeyboardEvent(event_type=event_type, scan_code=scan_code, name=name, is_keypad=is_keypad)) return is_allowed(name, event_type == KEY_UP) def low_level_keyboard_handler(nCode, wParam, lParam): try: vk = lParam.contents.vk_code # Ignore events generated by SendInput with Unicode. if vk != VK_PACKET: event_type = keyboard_event_types[wParam] is_extended = lParam.contents.flags & 1 scan_code = lParam.contents.scan_code should_continue = process_key(event_type, vk, scan_code, is_extended) if not should_continue: return -1 except Exception as e: print('Error in keyboard hook: ', e) return CallNextHookEx(NULL, nCode, wParam, lParam) WH_KEYBOARD_LL = c_int(13) keyboard_callback = LowLevelKeyboardProc(low_level_keyboard_handler) keyboard_hook = SetWindowsHookEx(WH_KEYBOARD_LL, keyboard_callback, NULL, NULL) # Register to remove the hook when the interpreter exits. Unfortunately a # try/finally block doesn't seem to work here. atexit.register(UnhookWindowsHookEx, keyboard_callback) msg = LPMSG() while not GetMessage(msg, NULL, NULL, NULL): TranslateMessage(msg) DispatchMessage(msg)
class BigInt(Structure): """ Multiple precision integer used by RELIC. """ BN_SIZE = 34 POSITIVE_FLAG = c_int(0) NEGATIVE_FLAG = c_int(1) # This maps to the type bn_st in relic_bn.h _fields_ = [ ("alloc", c_int), ("used", c_int), ("sign", c_int), # NOTE: We've promoted the type to c_ulonglong and halfed the # size of BN_SIZE compared to the representation in # relic_bn.h because BN_MAGNI = DOUBLE, meaning each "digit" # is a double-word. ("digits", c_ulonglong * int(BN_SIZE/2)), ] def __init__(self, x=None): """ Initialize an empty BigInt or create one from a Python long value @x """ if x is None: return if not isinstance(x, (int, int)): raise TypeError("BigInt can only be initialized from a Python long or int value.") # The number of bits in each "digit" bits = sizeof(c_ulonglong)*8 # Set the bookkeeping fields self.alloc = 0 self.used = 0 self.sign = BigInt.POSITIVE_FLAG if x >= 0 else BigInt.NEGATIVE_FLAG # Grab and set the "digits" from the value x. Digits are arranged # little-endian. x = abs(x) while x > 0: self.digits[self.used] = x % (2**bits) self.used += 1 x >>= bits def __add__(self, other): """ Add @other integer type to this BigInt. """ other = coerceBigInt(other) if not other: return NotImplemented result = BigInt() librelic.bn_add(byref(result), byref(self), byref(other)) return result def __eq__(self, other): """ Compares this BigInt against an integer type. """ return compare(self, other) == EQUAL def __le__(self, other): """ Compares this BigInt against an integer type. """ return compare(self, other) in [LESS_THAN, EQUAL] def __lt__(self, other): """ Compares this BigInt against an integer type. """ return compare(self, other) == LESS_THAN def __ge__(self, other): """ Compares this BigInt against an integer type. """ return compare(self, other) in [GREATER_THAN, EQUAL] def __gt__(self, other): """ Compares this BigInt against an integer type. """ return compare(self, other) == GREATER_THAN def __ne__(self, other): """ Compares this BigInt against an @other BigInt or long value. """ return not self.__eq__(other) def __str__(self): """ Retrieves a hexadecimal representation of this BigInt. """ return "BigInt<{}>".format(hexString(self.digits[:self.used])) def __mod__(self, other): """ Computes self modulo other where other is either a python integer type of a BigInt. """ other = coerceBigInt(other) if not other: return NotImplemented result = BigInt() librelic.bn_mod_abi(byref(result), byref(self), byref(other)) return result def __mul__(self, other): """ Computes self*other assuming other is also a BigInt. """ other = coerceBigInt(other) if not other: return NotImplemented result = BigInt() librelic.bn_mul_basic(byref(result), byref(self), byref(other)) return result def __sub__(self, other): """ Subtract @other BigInt from this BigInt. """ other = coerceBigInt(other) if not other: return NotImplemented result = BigInt() librelic.bn_sub(byref(result), byref(self), byref(other)) return result def __int__(self): """ Convert this BigInt to a Python long value. """ # Add up the digits from smallest index to largest as specified in # relic_bn.h # r:result, d:digit r = int(0) for i,d in enumerate(self.digits): r += d << (sizeof(c_ulonglong)*8*i) return r
def inverse_flattening(self): "Returns the Inverse Flattening for this Spatial Reference." return capi.invflattening(self.ptr, byref(c_int()))
def set_ipmi_maintenance_mode(self, mode): fcntl.ioctl(self.ipmidev, self.IPMICTL_SET_MAINTENANCE_MODE_CMD, c_int(mode))
def lznt1(self): # // lznt1 uses an interesting method to delimit compressed data # // 0xBXXX, with XXX being 0 to F (3 bytes) followed by zero in the next byte. # // bitmasks are used to determin data lenth, compression bits etc... pattern = re.compile(b"[\x01-\xFF][\xB0-\xBF]") pid = str(self.proc.UniqueProcessId) for page, size in self.pages: pid = str(self.proc.UniqueProcessId) fh = tempfile.TemporaryFile() fh.write(self.procspace.zread(page, size)) ofp = fh.tell() fh.seek(0, os.SEEK_END) fhsize = fh.tell() fh.seek(0) if fhsize: pagebuff = fh.read() fh.close() scale = 16 num_of_bits = 16 lastoffset = -1 offsetlist = [] nsize = 0 iscompressed = 0 lzntindex = [m.start() for m in re.finditer(pattern, pagebuff)] headerlist = [] for m in re.finditer(pattern, pagebuff): lzntheader = m.start() if lzntheader + 2 <= len(pagebuff): cheader = "0X%04X" % struct.unpack( "<H", pagebuff[lzntheader:lzntheader + 2]) headerint = int(cheader, 16) csize = (int(cheader, 16) + 3) & 0x0FFF if (headerint >= 0xB001 and headerint <= 0xBFFF) or ( headerint >= 0x3000 and headerint <= 0x3FFF): CompressedBuffer = create_string_buffer( pagebuff[lzntheader:lzntheader + csize]) CompressedBufferSize = ctypes.c_int( len(CompressedBuffer.raw)) UncompressedBuffer = create_string_buffer(0x1000) UnCompressedBufferSize = ctypes.c_int(0x1000) ucdatalen = self.nt.lznt1_uncompressed_size( CompressedBuffer, CompressedBufferSize) lznt1chunkrtn = self.nt.lznt1_decompress_chunk( byref(CompressedBuffer, 2), byref(CompressedBuffer, csize), UncompressedBuffer, byref(UncompressedBuffer, UnCompressedBufferSize.value)) if lznt1chunkrtn >= 0 and ucdatalen >= 0: tcompressedbuff = pagebuff[ lzntheader:lzntheader + csize] tdecompressedbuff = UncompressedBuffer.raw[: csize] minsize = self.config.SET_MINSIZE if tcompressedbuff not in tdecompressedbuff: if len(CompressedBuffer.raw) > minsize: checkheader = headerint & ucdatalen checkheader += 1 if lznt1chunkrtn / 4096 == 1: if len(headerlist) == 0: headerlist.append(lzntheader) headerlist.append(lzntheader + csize) UncompressedFile = pid + "_" + self.proc.ImageFileName + "_" + hex( page ) + "_" + hex( lzntheader ) + "_lznt1__UncompressedBuffer.bin" #outfd.write("") f = open( os.path.join( self.config.DUMP_DIR, UncompressedFile), 'wb') f.write(UncompressedBuffer.raw) #self.outfd.write("Decompressing data at offset: {0} of page {1} for Pid {3} \n".format(hex(lzntheader), hex(page), pid)) self.outfd.write( "Decompresing data for PID: {0}, Process: {1}, Page: {2}, Offset: {3}\n" .format( pid, self. proc.ImageFileName, hex(page), hex(lzntheader))) #outfd.write(self.pktstring) f.close() if self.config.DUMP_COMPRESSED: CompressedFile = pid + "_" + self.proc.ImageFileName + "_" + hex( page ) + "_" + hex( lzntheader ) + "_lznt1__CompressedBuffer.bin" f = open( os.path.join( self.config. DUMP_DIR, CompressedFile), 'wb') f.write( CompressedBuffer.raw) f.close() else: if lzntheader in headerlist: headerlist.append( lzntheader + csize) headerlist.append( lzntheader + csize) headerlist.sort() first_lzntheader = headerlist[ 0] UncompressedFile = pid + "_" + self.proc.ImageFileName + "_" + hex( page ) + "_" + hex( first_lzntheader ) + "_lznt1__UncompressedBuffer.bin" f = open( os.path.join( self.config. DUMP_DIR, UncompressedFile), 'ab') f.write( UncompressedBuffer.raw) f.close() if self.config.DUMP_COMPRESSED: CompressedFile = pid + "_" + self.proc.ImageFileName + "_" + hex( page ) + "_" + hex( first_lzntheader ) + "_lznt1__CompressedBuffer.bin" f = open( os.path.join( self.config. DUMP_DIR, CompressedFile ), 'ab') f.write( CompressedBuffer. raw) f.close() else: if lzntheader not in headerlist: headerlist = [] #f = open(pid + "_" +hex(page)+"_"+str(lzntheader)+"_lznt1__UncompressedBuffer.bin",'wb')\ UncompressedFile = pid + "_" + self.proc.ImageFileName + "_" + hex( page ) + "_" + hex( lzntheader ) + "_lznt1__UncompressedBuffer.bin" f = open( os.path.join( self.config. DUMP_DIR, UncompressedFile ), 'wb') f.write( UncompressedBuffer. raw) f.close() if self.config.DUMP_COMPRESSED: CompressedFile = pid + "_" + self.proc.ImageFileName + "_" + hex( page ) + "_" + hex( lzntheader ) + "_lznt1__CompressedBuffer.bin" f = open( os.path.join( self.config .DUMP_DIR, CompressedFile ), 'wb') f.write( CompressedBuffer .raw) f.close() else: if lzntheader in headerlist: headerlist.append(lzntheader + csize) headerlist.sort() first_lzntheader = headerlist[ 0] UncompressedFile = pid + "_" + self.proc.ImageFileName + "_" + hex( page ) + "_" + hex( first_lzntheader ) + "_lznt1__UncompressedBuffer.bin" f = open( os.path.join( self.config.DUMP_DIR, UncompressedFile), 'ab') f.write(UncompressedBuffer.raw) f.close() if self.config.DUMP_COMPRESSED: CompressedFile = pid + "_" + self.proc.ImageFileName + "_" + hex( page ) + "_" + hex( first_lzntheader ) + "_lznt1__CompressedBuffer.bin" f = open( os.path.join( self.config. DUMP_DIR, CompressedFile), 'ab') f.write( CompressedBuffer.raw) f.close()
def wrap(coordinates, bonds, box, centersel=None): """Wrap the coordinates back into the unit cell. Molecules will remain continuous, so may escape the bounds of the prinary unit cell. Parameters ---------- coordinates : bonds : box : Return ------ coordinates """ import platform libdir = home(libDir=True) if coordinates.dtype != np.float32: raise ValueError("Coordinates is not float32") if coordinates.ndim == 2: c = coordinates.shape coordinates = coordinates.reshape((c[0], c[1], 1)) if coordinates.shape[1] != 3: # print(coordinates.shape) raise NameError("Coordinates needs to be natoms x 3 x nframes") z = coordinates.shape[2] if coordinates.strides[0] != 12 * z or coordinates.strides[1] != 4 * z: # It's a view -- need to make a copy to ensure contiguity of memory coordinates = numpy.array(coordinates, dtype=numpy.float32) if coordinates.strides[0] != 12 * z or coordinates.strides[1] != 4 * z: raise ValueError("Coordinates is a view with unsupported strides") natoms = coordinates.shape[0] nframes = coordinates.shape[2] # print(box.shape) if numpy.size(bonds, 1) != 2: raise NameError("'bonds' not nbonds x 2 in length") if numpy.size(box, 0) != 3: raise NameError("'box' not nframes x 3 in length") if numpy.size(box, 1) != nframes: raise NameError("'box' not nframes x 3 in length") if platform.system() == "Windows": ct.cdll.LoadLibrary(os.path.join(libdir, "libgcc_s_seh-1.dll")) if os.path.exists(os.path.join(libdir, "psprolib.dll")): ct.cdll.LoadLibrary(os.path.join(libdir, "psprolib.dll")) lib = ct.cdll.LoadLibrary(os.path.join(libdir, "libvmdparser.so")) nbonds = bonds.shape[0] ll = 3 * nbonds c_bonds = (ct.c_int * ll)() for z in range(0, nbonds): for y in [0, 1]: c_bonds[z * 2 + y] = bonds[z, y] ll = 3 * nframes c_box = (ct.c_double * ll)() z = 0 for i in range(0, nframes): for j in range(0, 3): c_box[z] = box[j][i] z = z + 1 c_nbonds = ct.c_int(nbonds) c_natoms = ct.c_int(natoms) c_nframes = ct.c_int(nframes) lenv = natoms * 3 * nframes if centersel is None: centersel = numpy.array([-1], dtype=numpy.int32) centersel = numpy.append(centersel, numpy.array([-1], dtype=numpy.int32)) c_centersel = centersel.ctypes.data_as(ct.POINTER(ct.c_int)) c_coords = coordinates.ctypes.data_as(ct.POINTER(ct.c_float)) lib.wrap(c_bonds, c_coords, c_box, c_nbonds, c_natoms, c_nframes, c_centersel) return coordinates
def _solver_fgmres(self, rhs, atol, maxiter=0, restart=150, precon=None, ztol=1e-12, preconargs={}, **args): rci = c_int(0) n = c_int(len(rhs)) b = numpy.array(rhs, dtype=numpy.float64) x = numpy.zeros_like(b) ipar = numpy.zeros(128, dtype=numpy.int32) ipar[0] = len(rhs) # problem size ipar[1] = 6 # output on screen ipar[ 2] = 1 # current stage of the RCI FGMRES computations; the initial value is 1 ipar[3] = 0 # current iteration number; the initial value is 0 ipar[4] = 0 # maximum number of iterations ipar[ 5] = 1 # output error messages in accordance with the parameter ipar[1] ipar[ 6] = 1 # output warning messages in accordance with the parameter ipar[1] ipar[ 7] = 0 # do not perform the stopping test for the maximum number of iterations: ipar[3] <= ipar[4] ipar[ 8] = 0 # do not perform the residual stopping test: dpar[4] <= dpar[3] ipar[ 9] = 1 # perform the user-defined stopping test by setting RCI_request=2 if precon is None: ipar[ 10] = 0 # run the non-preconditioned version of the FGMRES method else: ipar[10] = 1 # run the preconditioned version of the FGMRES method precon = self.getprecon(precon, **args, **preconargs) ipar[ 11] = 0 # do not perform the automatic test for zero norm of the currently generated vector: dpar[6] <= dpar[7] ipar[ 12] = 1 # update the solution to the vector b according to the computations done by the dfgmres routine ipar[ 13] = 0 # internal iteration counter that counts the number of iterations before the restart takes place; the initial value is 0 ipar[14] = min( restart, len(rhs)) # the number of non-restarted FGMRES iterations dpar = numpy.zeros(128, dtype=numpy.float64) tmp = numpy.zeros((2 * ipar[14] + 1) * ipar[0] + (ipar[14] * (ipar[14] + 9)) // 2 + 1, dtype=numpy.float64) libmkl.dfgmres_check(byref(n), x.ctypes, b.ctypes, byref(rci), ipar.ctypes, dpar.ctypes, tmp.ctypes) if rci.value != 0: raise MatrixError('dgmres check failed with error code {}'.format( rci.value)) with log.context('fgmres {:.0f}%', 0, 0) as format: while True: libmkl.dfgmres(byref(n), x.ctypes, b.ctypes, byref(rci), ipar.ctypes, dpar.ctypes, tmp.ctypes) if rci.value == 1: # multiply the matrix tmp[ipar[22] - 1:ipar[22] + n.value - 1] = self @ tmp[ipar[21] - 1:ipar[21] + n.value - 1] elif rci.value == 2: # perform the stopping test if dpar[4] < atol: libmkl.dfgmres_get(byref(n), x.ctypes, b.ctypes, byref(rci), ipar.ctypes, dpar.ctypes, tmp.ctypes, byref(c_int(0))) if numpy.linalg.norm(self @ b - rhs) < atol: break b[:] = rhs # reset rhs vector for restart format(100 * numpy.log(dpar[2] / dpar[4]) / numpy.log(dpar[2] / atol)) if ipar[3] > maxiter > 0: break elif rci.value == 3: # apply the preconditioner tmp[ipar[22] - 1:ipar[22] + n.value - 1] = precon( tmp[ipar[21] - 1:ipar[21] + n.value - 1]) elif rci.value == 4: # check if the norm of the current orthogonal vector is zero if dpar[6] < ztol: libmkl.dfgmres_get(byref(n), x.ctypes, b.ctypes, byref(rci), ipar.ctypes, dpar.ctypes, tmp.ctypes, byref(c_int(0))) if numpy.linalg.norm(self @ b - rhs) < atol: break raise MatrixError('singular matrix') else: raise MatrixError( 'this should not have occurred: rci={}'.format( rci.value)) log.debug('performed {} fgmres iterations, {} restarts'.format( ipar[3], ipar[3] // ipar[14])) return b