Exemplo n.º 1
0
def from_outputs(outputs):
    extracted_outputs = []
    for output in outputs:
        output = c_uint64(output).value
        extracted_outputs.append(output >> 32)
        extracted_outputs.append(output & ((1 << 32) - 1))

    # Ugly hack, the upper numbers can be off by 1 due to overflow
    state = java.next_bits.predict_state(extracted_outputs)
    for i in range(1, int(len(extracted_outputs)/2)+1):
        if state:
            break
        for inds in combinations(range(0, len(extracted_outputs), 2), i):
            test_outputs = extracted_outputs[:]
            for index in inds:
                test_outputs[index] += 1
            state = java.next_bits.predict_state(test_outputs)
            if state:
                break

    if state is None:
        raise RuntimeError("Unable to recover internal state. (Not enough values?)")

    gen = java.next_bits.generate_values(state, 32)
    while True:
        num = c_int64(next(gen) << 32).value
        num += c_int32(next(gen)).value
        yield c_int64(num).value
Exemplo n.º 2
0
def init_dm_libnao(dm):
  from pyscf.nao.m_libnao import libnao
  from ctypes import POINTER, c_double, c_int64, byref
  
  d = np.require(dm, dtype=c_double, requirements='C')

  libnao.init_dm_libnao.argtypes = (POINTER(c_double), 
    POINTER(c_int64), # nreim 
    POINTER(c_int64), # norbs
    POINTER(c_int64), # nspin
    POINTER(c_int64), # nkpoints
    POINTER(c_int64)) # alloc_stat
  
  alloc_stat = c_int64(-999)

  libnao.init_dm_libnao(d.ctypes.data_as(POINTER(c_double)),
    c_int64(d.shape[-1]), 
    c_int64(d.shape[-2]),
    c_int64(d.shape[-4]),
    c_int64(d.shape[-5]),
    byref(alloc_stat))

  if alloc_stat.value!=0 : 
    raise RuntimeError('could not allocate?') 
    return None

  return dm
Exemplo n.º 3
0
def _split_md5(md5digest):
    hi = lo = 0
    for i in range(0, 8):
        lo = lo | (ord(md5digest[i]) << (i * 8))
    for i in range(8,16):
        hi = hi | (ord(md5digest[i]) << ((i - 8) * 8))
    return ctypes.c_int64(lo).value, ctypes.c_int64(hi).value  # signed int!
Exemplo n.º 4
0
def disk_usage(path):
    if platform.system() == 'Windows':
        freeuser = ctypes.c_int64()
        total = ctypes.c_int64()
        free = ctypes.c_int64()
        ctypes.windll.kernel32.GetDiskFreeSpaceExW(ctypes.c_wchar_p(path), ctypes.byref(freeuser), ctypes.byref(total), ctypes.byref(free))
        used = (total.value - free.value) / (1024*1024*1024)
        total = total.value / (1024*1024*1024)
        free = free.value / (1024*1024*1024)

        return {
            'total': "%.2f" % total,
            'used': "%.2f" % used,
            'free': "%.2f" % free,
            'percentage_used': int((float(used)/float(total))*100),
        }

    else:
        st = os.statvfs(path)

        free = float(st.f_bavail * st.f_frsize) / 1073741824
        total = float(st.f_blocks * st.f_frsize) / 1073741824
        used = float((st.f_blocks - st.f_bfree) * st.f_frsize) / 1073741824

        return {
            'total': "%.2f" % total,
            'used': "%.2f" % used,
            'free': "%.2f" % free,
            'percentage_used': int(used/total * 100),
        }
Exemplo n.º 5
0
def fs_size(device):
    '''Returns a tuple of the total size of the filesystem
       and the free space on it.'''
    # FIXME evand 2009-06-05: Do we want the free bytes available to the user,
    # or the total free bytes?  Right now we're using the latter.

    if sys.platform == 'win32':
        # Taken from Wubi.
        import ctypes
        freeuser = ctypes.c_int64()
        total = ctypes.c_int64()
        free = ctypes.c_int64()
        try:
            ctypes.windll.kernel32.GetDiskFreeSpaceExW(
                    unicode(device),
                    ctypes.byref(freeuser),
                    ctypes.byref(total),
                    ctypes.byref(free))
        except:
            return (0, 0)
        return (total.value, free.value)
    else:
        try:
            stat = os.statvfs(device)
        except:
            return (0, 0)
        free = stat.f_bsize * stat.f_bavail # Include reserved blocks.
        total = stat.f_bsize * stat.f_blocks
        return (total, free)
def _disk_info():
    drive = unicode(os.getenv("SystemDrive"))
    freeuser = ctypes.c_int64()
    total = ctypes.c_int64()
    free = ctypes.c_int64()
    ctypes.windll.kernel32.GetDiskFreeSpaceExW(drive, ctypes.byref(freeuser), ctypes.byref(total), ctypes.byref(free))
    return freeuser.value 
Exemplo n.º 7
0
Arquivo: mf.py Projeto: chrinide/pyscf
  def init_libnao(self, wfsx=None):
    """ Initialization of data on libnao site """
    from pyscf.nao.m_libnao import libnao
    from pyscf.nao.m_sv_chain_data import sv_chain_data
    from ctypes import POINTER, c_double, c_int64, c_int32, byref

    if wfsx is None:
        data = sv_chain_data(self)
        # (nkpoints, nspin, norbs, norbs, nreim)
        #print(' data ', sum(data))
        size_x = np.array([1, self.nspin, self.norbs, self.norbs, 1], dtype=np.int32)
        libnao.init_sv_libnao_orbs.argtypes = (POINTER(c_double), POINTER(c_int64), POINTER(c_int32))
        libnao.init_sv_libnao_orbs(data.ctypes.data_as(POINTER(c_double)), c_int64(len(data)), size_x.ctypes.data_as(POINTER(c_int32)))
        self.init_sv_libnao = True
    else:
        size_x = np.zeros(len(self.wfsx.x.shape), dtype=np.int32)
        for i, sh in enumerate(self.wfsx.x.shape): size_x[i] = sh

        data = sv_chain_data(self)
        libnao.init_sv_libnao_orbs.argtypes = (POINTER(c_double), POINTER(c_int64), POINTER(c_int32))
        libnao.init_sv_libnao_orbs(data.ctypes.data_as(POINTER(c_double)), c_int64(len(data)), size_x.ctypes.data_as(POINTER(c_int32)))
        self.init_sv_libnao = True

    libnao.init_aos_libnao.argtypes = (POINTER(c_int64), POINTER(c_int64))
    info = c_int64(-999)
    libnao.init_aos_libnao(c_int64(self.norbs), byref(info))
    if info.value!=0: raise RuntimeError("info!=0")
    return self
Exemplo n.º 8
0
def get_hard_drive_free_space(drive):
    user_space = ctypes.c_int64()
    total = ctypes.c_int64()
    free = ctypes.c_int64()
    ctypes.windll.kernel32.GetDiskFreeSpaceExW(drive,
                                               ctypes.byref(user_space),
                                               ctypes.byref(total),
                                               ctypes.byref(free))
    return user_space.value
Exemplo n.º 9
0
    def fadviseSeqNoCache(self, fileD):
        """Advise the kernel that we are only going to access file-descriptor
        fileD once, sequentially."""

        POSIX_FADV_SEQUENTIAL = 2
        POSIX_FADV_DONTNEED = 4
        offset = ctypes.c_int64(0)
        length = ctypes.c_int64(0)
        clib.posix_fadvise(fileD, offset, length, POSIX_FADV_SEQUENTIAL)
        clib.posix_fadvise(fileD, offset, length, POSIX_FADV_DONTNEED)
Exemplo n.º 10
0
def get_drive_space(drive_path):
    #Windows only
    freeuser = ctypes.c_int64()
    total = ctypes.c_int64()
    free = ctypes.c_int64()
    ctypes.windll.kernel32.GetDiskFreeSpaceExW(
            unicode(drive_path),
            ctypes.byref(freeuser),
            ctypes.byref(total),
            ctypes.byref(free))
    return total.value
Exemplo n.º 11
0
    def _range_query_int(self) -> Tuple[int, int]:
        range_min = c_int64()
        range_max = c_int64()
        error = vimba_c.vmb_feature_int_range_query(self._handle,
                                                    self._name,
                                                    byref(range_min),
                                                    byref(range_max))
        if error:
            raise VimbaException(error)

        return int(range_min.value), int(range_max.value)
Exemplo n.º 12
0
 def get_space(self):
     drive_path = self.path
     freeuser = ctypes.c_int64()
     total = ctypes.c_int64()
     free = ctypes.c_int64()
     ctypes.windll.kernel32.GetDiskFreeSpaceExW(
             unicode(drive_path),
             ctypes.byref(freeuser),
             ctypes.byref(total),
             ctypes.byref(free))
     return total.value, freeuser.value
Exemplo n.º 13
0
def InitializeWinNowFunction(plat):
  """Sets a monotonic clock for windows platforms.

    Args:
      plat: Platform that is being run on.
  """
  global _CLOCK  # pylint: disable=global-statement
  global _NOW_FUNCTION  # pylint: disable=global-statement

  if IsQPCUsable():
    _CLOCK = _WIN_HIRES
    qpc_return = ctypes.c_int64()
    qpc_frequency = ctypes.c_int64()
    ctypes.windll.Kernel32.QueryPerformanceFrequency(
        ctypes.byref(qpc_frequency))
    qpc_frequency = float(qpc_frequency.value)
    qpc = ctypes.windll.Kernel32.QueryPerformanceCounter

    def WinNowFunctionImpl():
      qpc(ctypes.byref(qpc_return))
      return qpc_return.value / qpc_frequency

  else:
    _CLOCK = _WIN_LORES
    kernel32 = (ctypes.cdll.kernel32
                if plat.startswith(_PLATFORMS['cygwin'])
                else ctypes.windll.kernel32)
    get_tick_count_64 = getattr(kernel32, 'GetTickCount64', None)

    # Windows Vista or newer
    if get_tick_count_64:
      get_tick_count_64.restype = ctypes.c_ulonglong

      def WinNowFunctionImpl():
        return get_tick_count_64() / 1000.0

    else:  # Pre Vista.
      get_tick_count = kernel32.GetTickCount
      get_tick_count.restype = ctypes.c_uint32
      get_tick_count_lock = threading.Lock()

      def WinNowFunctionImpl():
        global GET_TICK_COUNT_LAST_NOW  # pylint: disable=global-statement
        global GET_TICK_COUNT_WRAPAROUNDS  # pylint: disable=global-statement
        with get_tick_count_lock:
          current_sample = get_tick_count()
          if current_sample < GET_TICK_COUNT_LAST_NOW:
            GET_TICK_COUNT_WRAPAROUNDS += 1
          GET_TICK_COUNT_LAST_NOW = current_sample
          final_ms = GET_TICK_COUNT_WRAPAROUNDS << 32
          final_ms += GET_TICK_COUNT_LAST_NOW
          return final_ms / 1000.0

  _NOW_FUNCTION = WinNowFunctionImpl
Exemplo n.º 14
0
def siesta_wfsx_book_read_py(fname, nreim):
  """ Creates buffer for integer data from .WFSX files """
  name = create_string_buffer(fname.encode())
  bufsize = c_int64(-999)
  ios = c_int64(22)
  libnao.siesta_wfsx_book_size(name, c_int64(nreim), bufsize, ios)
  if ios.value!=0 : return None
  idat = empty(bufsize.value, dtype=np.int64)
  libnao.siesta_wfsx_book_read(name, c_int64(nreim), idat.ctypes.data_as(POINTER(c_int64)), ios)
  if ios.value!=0 : return None
  return idat
Exemplo n.º 15
0
    def millis():
        "return a timestamp in milliseconds (ms)"
        tics = ctypes.c_int64() #use *signed* 64-bit variables; see the "QuadPart" variable here: https://msdn.microsoft.com/en-us/library/windows/desktop/aa383713(v=vs.85).aspx 
        freq = ctypes.c_int64()

        #get ticks on the internal ~2MHz QPC clock
        ctypes.windll.Kernel32.QueryPerformanceCounter(ctypes.byref(tics)) 
        #get the actual freq. of the internal ~2MHz QPC clock 
        ctypes.windll.Kernel32.QueryPerformanceFrequency(ctypes.byref(freq)) 
        
        t_ms = tics.value*1e3/freq.value
        return t_ms
Exemplo n.º 16
0
 def disk_usage(self):
     """
     Getting information about every device in self.disk_dict.
     :return: None
     """
     freeuser = ctypes.c_int64()
     total = ctypes.c_int64()
     free = ctypes.c_int64()
     for drive in self.disk_dict:
         GetDiskFreeSpaceExW(unicode(drive), ctypes.byref(freeuser), ctypes.byref(total), ctypes.byref(free))
         self.disk_dict[drive] = {'total': total.value,
                                  'used': (total.value - free.value)}
Exemplo n.º 17
0
def test_line_search(nu, nu_insert, number_of_lines, expected_params):
    nu = (c_double * number_of_lines)(*nu)
    nu_insert = c_double(nu_insert)
    number_of_lines = c_int64(number_of_lines)
    obtained_result = c_int64(0)

    cmontecarlo_methods.line_search.restype = c_uint
    obtained_tardis_error = cmontecarlo_methods.line_search(
                        byref(nu), nu_insert, number_of_lines, byref(obtained_result))

    assert obtained_result.value == expected_params['result']
    assert obtained_tardis_error == expected_params['ret_val']
Exemplo n.º 18
0
	def values(self):
		try:
			drive = unicode("C:")
			freeuser = ctypes.c_int64()
			total = ctypes.c_int64()
			free = ctypes.c_int64()
			ctypes.windll.kernel32.GetDiskFreeSpaceExW(drive, ctypes.byref(freeuser), ctypes.byref(total), ctypes.byref(free))
			return "drive_free_c.value " + str(100.0 * free.value / total.value) + """
.
"""
		except Error, e:
			return e, """
Exemplo n.º 19
0
    def read(self, fd, offset, l):
        self.require_state("mounted")
        if not isinstance(offset, int):
            raise TypeError("path must be an int")
        if not isinstance(l, int):
            raise TypeError("path must be an int")

        buf = create_string_buffer(l)
        ret = self.libcephfs.ceph_read(self.cluster, c_int(fd), buf, c_int64(l), c_int64(offset))
        if ret < 0:
            raise make_ex(ret, "error in close")
        return buf.value
Exemplo n.º 20
0
    def write(self, fd, buf, offset):
        self.require_state("mounted")
        if not isinstance(buf, basestring):
            raise TypeError('buf must be a string')
        if not isinstance(offset, int):
            raise TypeError('offset must be an int')

        ret = self.libcephfs.ceph_write(self.cluster, c_int(fd),
                                        c_char_p(buf), c_int64(len(buf)),
                                        c_int64(offset))
        if ret < 0:
            raise make_ex(ret, "error in close")
        return ret
Exemplo n.º 21
0
def init():
    global _InitTime
    global _TimeStart
    global _Frequency
    _InitTime = time.time()
    # time.clock()
    from ctypes import byref, c_int64, windll
    time_start = c_int64()
    freq = c_int64()
    windll.Kernel32.QueryPerformanceCounter(byref(time_start))
    windll.Kernel32.QueryPerformanceFrequency(byref(freq))
    _TimeStart = float(time_start.value)
    _Frequency = float(freq.value)
Exemplo n.º 22
0
def test_reverse_binary_search(x, x_insert, imin, imax, expected_params):
    x = (c_double * (imax - imin + 1))(*x)
    x_insert = c_double(x_insert)
    imin = c_int64(imin)
    imax = c_int64(imax)
    obtained_result = c_int64(0)

    cmontecarlo_methods.reverse_binary_search.restype = c_uint
    obtained_tardis_error = cmontecarlo_methods.reverse_binary_search(
                        byref(x), x_insert, imin, imax, byref(obtained_result))

    assert obtained_result.value == expected_params['result']
    assert obtained_tardis_error == expected_params['ret_val']
def disk_c():
    import os
    import ctypes
    
    drive = unicode(os.getenv("SystemDrive"))
    freeuser = ctypes.c_int64()
    total = ctypes.c_int64()
    free = ctypes.c_int64()
    ctypes.windll.kernel32.GetDiskFreeSpaceExW(drive, 
                                    ctypes.byref(freeuser), 
                                    ctypes.byref(total), 
                                    ctypes.byref(free))
    
    return {'freeuser': freeuser, 'total': total, 'free': free}
Exemplo n.º 24
0
 def _disk_c(self):
     drive = unicode(os.getenv("SystemDrive"))
     freeuser = ctypes.c_int64()
     total = ctypes.c_int64()
     free = ctypes.c_int64()
     ctypes.windll.kernel32.GetDiskFreeSpaceExW(drive,
                                     ctypes.byref(freeuser),
                                     ctypes.byref(total),
                                     ctypes.byref(free))
     d = dict(drive=drive,
              freeuser = freeuser.value,
              total = total.value,
              free  = free.value)
     return d
Exemplo n.º 25
0
def calculate_free_space(mount_point):
    """
    Calculates the amount of free space on the given mount_point
    """
    if os.name == 'posix':
        v = os.statvfs(mount_point)
        free_space = v.f_frsize * v.f_bfree
        return free_space
    else:
        import ctypes
        from ctypes import windll
        free_bytes = ctypes.c_int64()
        total_bytes = ctypes.c_int64()
        windll.kernel32.GetDiskFreeSpaceExW(ctypes.c_wchar_p(mount_point), ctypes.byref(free_bytes), ctypes.byref(total_bytes), None)
        return free_bytes.value
Exemplo n.º 26
0
 def GetDriveSpace(self, path):
     """ Returns the amount of free space, in gigabytes, on the drive containing the provided path. """
     if sys.platform[:5] == "linux":
         st = os.statvfs(path)
         return '%.1f' % ((st.f_bavail * st.f_frsize) / 1024/1024/1024) + " GB"
     elif sys.platform[:5] == "win32":
         drive = os.getenv(path)
         freeuser = ctypes.c_int64()
         total = ctypes.c_int64()
         free = ctypes.c_int64()
         ctypes.windll.kernel32.GetDiskFreeSpaceExW(drive, 
                                         ctypes.byref(freeuser), 
                                         ctypes.byref(total), 
                                         ctypes.byref(free))
         return str('%.1f' % (free.value/1024/1024/1024)) + " GB"
     return 0
Exemplo n.º 27
0
 def comp_apair_pp_libint(self, a1,a2):
   """ Get's the vertex coefficient and conversion coefficients for a pair of atoms given by their atom indices """
   from operator import mul
   from pyscf.nao.m_prod_biloc import prod_biloc_c
   if not hasattr(self, 'sv_pbloc_data') : raise RuntimeError('.sv_pbloc_data is absent')
   assert a1>=0
   assert a2>=0
   
   t1 = timer()
   sv = self.sv
   aos = self.sv.ao_log
   sp12 = np.require( np.array([sv.atom2sp[a] for a in (a1,a2)], dtype=c_int64), requirements='C')
   rc12 = np.require( np.array([sv.atom2coord[a,:] for a in (a1,a2)]), requirements='C')
   icc2a = np.require( np.array(self.ls_contributing(a1,a2), dtype=c_int64), requirements='C')
   npmx = aos.sp2norbs[sv.atom2sp[a1]]*aos.sp2norbs[sv.atom2sp[a2]]
   npac = sum([self.prod_log.sp2norbs[sv.atom2sp[ia]] for ia in icc2a ])
   nout = c_int64(npmx**2+npmx*npac+10)
   dout = np.require( zeros(nout.value), requirements='CW')
   
   libnao.vrtx_cc_apair( sp12.ctypes.data_as(POINTER(c_int64)), rc12.ctypes.data_as(POINTER(c_double)), icc2a.ctypes.data_as(POINTER(c_int64)), c_int64(len(icc2a)), dout.ctypes.data_as(POINTER(c_double)), nout )    
   if dout[0]<1: return None
   
   nnn = np.array(dout[0:3], dtype=int)
   nnc = np.array([dout[8],dout[7]], dtype=int)
   ncc = int(dout[9])
   if ncc!=len(icc2a): raise RuntimeError('ncc!=len(icc2a)')
   s = 10; f=s+np.prod(nnn); vrtx  = dout[s:f].reshape(nnn)
   s = f;  f=s+np.prod(nnc); ccoe  = dout[s:f].reshape(nnc)
   icc2s = np.zeros(len(icc2a)+1, dtype=np.int64)
   for icc,a in enumerate(icc2a): icc2s[icc+1] = icc2s[icc] + self.prod_log.sp2norbs[sv.atom2sp[a]]
   pbiloc = prod_biloc_c(atoms=array([a2,a1]),vrtx=vrtx,cc2a=icc2a,cc2s=icc2s,cc=ccoe)
   
   return pbiloc
Exemplo n.º 28
0
 def test_assignment_ckernel(self):
     ckd = _lowlevel.make_ckernel_deferred_from_assignment(
                 ndt.float32, ndt.int64,
                 "unary", "none")
     self.assertEqual(nd.as_py(ckd.types), [ndt.float32, ndt.int64])
     # Instantiate as a single kernel
     with _lowlevel.ckernel.CKernelBuilder() as ckb:
         meta = (ctypes.c_void_p * 2)()
         _lowlevel.ckernel_deferred_instantiate(ckd, ckb, 0, meta, "single")
         ck = ckb.ckernel(_lowlevel.UnarySingleOperation)
         # Do an assignment using ctypes
         i64 = ctypes.c_int64(1234)
         f32 = ctypes.c_float(1)
         ck(ctypes.addressof(f32), ctypes.addressof(i64))
         self.assertEqual(f32.value, 1234.0)
     # Instantiate as a strided kernel
     with _lowlevel.ckernel.CKernelBuilder() as ckb:
         meta = (ctypes.c_void_p * 2)()
         _lowlevel.ckernel_deferred_instantiate(ckd, ckb, 0, meta, "strided")
         ck = ckb.ckernel(_lowlevel.UnaryStridedOperation)
         # Do an assignment using ctypes
         i64 = (ctypes.c_int64 * 3)()
         for i, v in enumerate([3,7,21]):
             i64[i] = v
         f32 = (ctypes.c_float * 3)()
         ck(ctypes.addressof(f32), 4,
                     ctypes.addressof(i64), 8,
                     3)
         self.assertEqual([f32[i] for i in range(3)], [3,7,21])
Exemplo n.º 29
0
 def reg_read(self, reg_id):
     # read to 64bit number to be safe
     reg = ctypes.c_int64(0)
     status = _uc.uc_reg_read(self._uch, reg_id, ctypes.byref(reg))
     if status != UC_ERR_OK:
         raise UcError(status)
     return reg.value
Exemplo n.º 30
0
 def change_auid(self, auid):
     self.require_ioctx_open()
     ret = self.librados.rados_ioctx_pool_set_auid(self.io,\
             ctypes.c_int64(auid))
     if ret < 0:
         raise make_ex(ret, "error changing auid of '%s' to %lld" %\
             (self.name, auid))
Exemplo n.º 31
0
 def _set_handle(self):
     out = _lib.hdfsOpenFile(self._fs, ensure_byte(self.path),
                             mode_numbers[self.mode], self.buff,
                             ctypes.c_short(self.repl),
                             ctypes.c_int64(self.block_size))
     if not out:
         raise IOError("Could not open file: %s, mode: %s" %
                       (self.path, self.mode))
     self._handle = out
Exemplo n.º 32
0
def getFreeDiskSpace(dirname):
    if hasattr(os, 'statvfs'):
        statvfs = os.statvfs(os.getcwd())
        return statvfs.f_bavail * statvfs.f_frsize
    elif os.name == 'nt':
        import ctypes
        drive = unicode(dirname)
        freeuser = ctypes.c_int64()
        total = ctypes.c_int64()
        free = ctypes.c_int64()
        ctypes.windll.kernel32.GetDiskFreeSpaceExW(drive,
                                                   ctypes.byref(freeuser),
                                                   ctypes.byref(total),
                                                   ctypes.byref(free))
        return freeuser.value
    else:
        message("Could not detect free disk space", EXCEPTION)
        return -1
Exemplo n.º 33
0
 def add_int(self, number: int) -> "Builder":
     if number == 0:
         self.program.append(OP_0)
         return self
     if 1 <= number <= 16:
         self.program.append(OP_1 + number - 1)
         return self
     # return
     return self.add_bytes(bytes(c_int64(number)).rstrip(b'\x00'))
Exemplo n.º 34
0
 def encode(self, obj):
     self._check(obj)
     elem_type = dtype_to_weld_type(obj.dtype)
     c_class = WeldVec(elem_type).ctype_class
     elem_class = elem_type.ctype_class
     ptr = obj.ctypes.data_as(POINTER(elem_class))
     # obj.size gives the correct value for multi-dimensional arrays.
     size = ctypes.c_int64(obj.size)
     return c_class(ptr=ptr, size=size)
Exemplo n.º 35
0
    def display_code_at(self, libc_start_main_offset, lim=100):
        inss = self.read_max_memory(libc_start_main_offset, lim=lim)

        disas = binary.X86Machine(x64=False)
        calls = []
        for i in disas.get_ins(inss, libc_start_main_offset):

            print(ctypes.c_int64(i.address), i.mnemonic, i.op_str)
            x = disas.get_call(i)
Exemplo n.º 36
0
 def release_quiz(self, i_quiz: int, throw: bool = True) -> PqaError:
     c_err = ctypes.c_void_p()
     c_err.value = pqa_core.PqaEngine_ReleaseQuiz(self.c_engine,
                                                  ctypes.c_int64(i_quiz))
     err = PqaError.factor(c_err)
     if err:
         if throw:
             raise PqaException('Failed to release_quiz(): ' + str(err))
     return err
Exemplo n.º 37
0
 def clear_old_quizzes(self, max_count: int, max_age_sec: float, throw: bool = True) -> PqaError:
     c_err = ctypes.c_void_p()
     c_err.value = pqa_core.PqaEngine_ClearOldQuizzes(self.c_engine, ctypes.c_int64(max_count),
                                                      ctypes.c_double(max_age_sec))
     err = PqaError.factor(c_err)
     if err:
         if throw:
             raise PqaException('Failed to clear_old_quizzes(): ' + str(err))
     return err
Exemplo n.º 38
0
def read_int64(hProcess, address):
    ReadBuffer = ctypes.c_int64()
    lpBuffer = ctypes.byref(ReadBuffer)
    nSize = ctypes.sizeof(ReadBuffer)
    bytesRead = ctypes.c_ulong(0)

    ctypes.windll.kernel32.ReadProcessMemory(hProcess, address, lpBuffer,
                                             nSize, bytesRead)
    return ReadBuffer.value
Exemplo n.º 39
0
    def setPosition(self, Position):
        _Position = ctypes.c_int64(Position)

        __func = PhidgetSupport.getDll().PhidgetEncoder_setPosition
        __func.restype = ctypes.c_int32
        result = __func(self.handle, _Position)

        if result > 0:
            raise PhidgetException(result)
Exemplo n.º 40
0
    def drawFunc(self):
        # return self.drawFunc1()
        # limit FPS = 60
        # if self.last_tick is None:
        # 	self.last_tick = time.time()
        # else:
        # 	curr_tick = time.time()
        # 	delta_time = 1/60. - (curr_tick-self.last_tick)
        # 	if delta_time > 0:
        # 		time.sleep(delta_time)
        # 		self.last_tick = time.time()
        # 	else:
        # 		self.last_tick = curr_tick

        length = self.FFT_size * 2 + 1
        wav, tms = self.stream_reader.get(
            length=length), self.stream_reader.wav_time
        if tms is None or wav.shape[1] < length: return

        # Pre-emphasis
        wav = wav[:, :-1] - wav[:, 1:] * 0

        # Compute FFT
        fft = np.abs(np.fft.rfft(wav)[:, :self.FFT_size], dtype=wav.dtype)
        # fft = np.log1p(fft)
        stereo = wav.shape[0] > 1
        tml = TimedLevel(
            (ctypes.c_void_p * 2)(fft[0, :].ctypes.data,
                                  fft[1, :].ctypes.data if stereo else 0),
            (ctypes.c_void_p * 2)(wav[0, :].ctypes.data,
                                  wav[1, :].ctypes.data if stereo else 0),
            ctypes.c_int64(int(tms * 1e7)), ctypes.c_int32(2))
        self.tempoVis.DrawFrame(ctypes.pointer(tml))
        glutSwapBuffers()

        # wav0 = self.stream_reader.get()
        # Compute tempo at regular intervals
        if self.last_tempo_calc_time is None:
            self.last_tempo_calc_time = tms
        elif tms - self.last_tempo_calc_time >= self.tempo_calc_interval:
            self.last_tempo_calc_time = tms
            wav = self.stream_reader.get()
            if False:  # DEBUG: listen to what has been recorded
                import pyaudio
                p = pyaudio.PyAudio()
                stream = p.open(format=pyaudio.paFloat32,
                                channels=2,
                                rate=self.rate,
                                output=True)
                stream.write(wav.T.tobytes())
            if wav.shape[0] > 1: wav = wav.mean(axis=0, keepdims=True)
            print('compute tempo: length=%s sec' %
                  (wav.shape[1] / self.stream_reader.rate))
            self.tempoVis.CreateTempoThread(
                ctypes.c_void_p(wav[0, :].ctypes.data),
                ctypes.c_int32(wav.shape[1]),
                ctypes.c_int32(int(self.stream_reader.rate)))
Exemplo n.º 41
0
 def i64(r: list):
     if utils.is_all_real(r[0], r[1], r[2], r[3], r[4], r[5], r[6], r[7]):
         return ctypes.c_int64(r[0] + (r[1] << 8) + (r[2] << 16) +
                               (r[3] << 24) + (r[4] << 32) + (r[5] << 40) +
                               (r[6] << 48) + (r[7] << 56)).value
     else:
         for i in range(len(r)):
             r[i] = utils.to_symbolic(r[i], 8)
         return z3.Concat(r[7], r[6], r[5], r[4], r[3], r[2], r[1], r[0])
Exemplo n.º 42
0
    def _get_int(self) -> int:
        value = c_int64()
        error = vimba_c.vmb_feature_int_get(self._handle,
                                            self._name,
                                            byref(value))
        if error:
            raise VimbaException(error)

        return value.value
def c_image_open_region(image_id, level, region):
    extent = region.get_extent()
    num_channels = c_image_get_num_channels(image_id)
    c_region = convert_py_to_c_box(region)
    buf = np.ndarray((num_channels, extent.z, extent.y, extent.x))
    check(_HISTOKAT_LIB.image_open_region(
        image_id, ctypes.c_int64(level), ctypes.byref(c_region), buf.ctypes))
    # move c-dimension to end
    return np.rollaxis(buf, 0, 4)
Exemplo n.º 44
0
 def remove_targets(self, target_ids: List[int], throw: bool = True):
     c_ids, n_ids = PqaEngine.to_c_ids(target_ids)
     c_err = ctypes.c_void_p()
     c_err.value = pqa_core.PqaEngine_RemoveTargets(self.c_engine, ctypes.c_int64(n_ids), c_ids)
     err = PqaError.factor(c_err)
     if err:
         if throw:
             raise PqaException('Failed to remove_targets(): ' + str(err))
     return err
Exemplo n.º 45
0
 def _disk_c(self):
     drive = os.getenv("SystemDrive")
     freeuser = ctypes.c_int64()
     total = ctypes.c_int64()
     free = ctypes.c_int64()
     try:
         ctypes.windll.kernel32.GetDiskFreeSpaceExW(unicode(drive),
                                                    ctypes.byref(freeuser),
                                                    ctypes.byref(total),
                                                    ctypes.byref(free))
     except:
         try:
             ctypes.windll.kernel32.GetDiskFreeSpaceExW(
                 drive, ctypes.byref(freeuser), ctypes.byref(total),
                 ctypes.byref(free))
         except:
             raise ctypes.WinError()
     return freeuser.value
Exemplo n.º 46
0
 def test_parse(self, field: 'IntType', expected):
     assert field.parse(expected['unset_field']) == None
     for data, expected in [
         (c_int64(1), 1),
         ('1', 1),
         (b'1', 1),
         (1, 1),
     ]:
         assert field.parse(data).value == expected
def c_analysis_get_minor_modes(session_id, major_mode):
    string_list_ptr = ctypes.POINTER(c_string_list)()
    check(_HISTOKAT_LIB.analysis_get_minor_modes(
        session_id, ctypes.c_int64(major_mode), ctypes.byref(string_list_ptr)))
    modes = []
    for i in range(string_list_ptr.contents.num_strings):
        modes.append(string_list_ptr.contents.string_list[i].decode('utf-8'))
    check(_HISTOKAT_LIB.destroy_string_list(ctypes.byref(string_list_ptr)))
    return modes
Exemplo n.º 48
0
def ao_log_hartree_lap_libnao(ao):
  """
    Computes radial parts of Hartree potentials generated by the radial orbitals using Laplace transform (r_>, r_<)
    Uses a call to Fortran version of the calculation.
    Args: 
      self: class instance of ao_log_c
    Result:
      ao_pot with respective radial parts
  """
  from ctypes import POINTER, c_double, c_int64
  from pyscf.nao.m_libnao import libnao

  libnao.ao_hartree_lap.argtypes = (
    POINTER(c_double), # rr(nr)
    POINTER(c_int64),  # nmult
    POINTER(c_double), # mu2ff(nr,nmult)
    POINTER(c_int64),  # nr
    POINTER(c_int64),  # mu2j
    POINTER(c_double)) # mu2vh(nr,nmult)

  ao_pot = copy.deepcopy(ao)
  
  rr = np.require(ao.rr, dtype=c_double, requirements='C')  
  for sp in range(ao.nspecies):
    mu2j = np.require(ao.sp_mu2j[sp], dtype=c_int64, requirements='C')
    ff_rad = np.require(ao.psi_log[sp], dtype=c_double, requirements='C')
    ff_pot = np.require(ao_pot.psi_log[sp], dtype=c_double, requirements='CW')
    
    libnao.ao_hartree_lap(
      rr.ctypes.data_as(POINTER(c_double)), 
      c_int64(ao.sp2nmult[sp]),
      ff_rad.ctypes.data_as(POINTER(c_double)), 
      c_int64(ao.nr),
      mu2j.ctypes.data_as(POINTER(c_int64)),
      ff_pot.ctypes.data_as(POINTER(c_double)))
      
    ao_pot.psi_log[sp] = ff_pot  
    for mu,am in enumerate(ao.sp_mu2j[sp]):
        ao_pot.psi_log_rl[sp][mu,:] = ao_pot.psi_log[sp][mu,:]/(ao.rr**am)

  for sp in range(ao.nspecies): ao_pot.sp_mu2rcut[sp].fill(ao.rr[-1])
  ao_pot.sp2rcut.fill(ao.rr[-1])

  return ao_pot
Exemplo n.º 49
0
 def test_mpz_models(self):
     i1 = define_const('i1', self.int_t)
     i2 = define_const('i2', self.int_t)
     assert_formula('(> i1 987654321987654321987654321)', self.ctx)
     assert_formula('(< i2 i1)', self.ctx)
     self.assertEqual(yapi.yices_check_context(self.ctx, self.param),
                      yapi.STATUS_SAT)
     mdl = yapi.yices_get_model(self.ctx, 1)
     mdlstr = yapi.yices_model_to_string(mdl, 80, 100, 0)
     self.assertEqual(
         mdlstr,
         '(= i1 987654321987654321987654322)\n(= i2 987654321987654321987654321)'
     )
     i32v1 = c_int32()
     #iam: 9/19/2018 with self.assertRaisesRegexp(YicesAPIException,
     #iam: 9/19/2018                              'eval error: the term value does not fit the expected type'):
     #iam: 9/19/2018     yapi.yices_get_int32_value(mdl, i1, i32v1)
     errcode = yapi.yices_get_int32_value(mdl, i1, i32v1)
     error_string = yapi.yices_error_string()
     self.assertEqual(errcode, -1)
     self.assertEqual(
         error_string,
         'eval error: the term value does not fit the expected type')
     i64v1 = c_int64()
     #iam: 9/19/2018 with self.assertRaisesRegexp(YicesAPIException,
     #iam: 9/19/2018                              'eval error: the term value does not fit the expected type'):
     #iam: 9/19/2018     yapi.yices_get_int64_value(mdl, i1, i64v1)
     errcode = yapi.yices_get_int64_value(mdl, i1, i64v1)
     error_string = yapi.yices_error_string()
     self.assertEqual(errcode, -1)
     self.assertEqual(
         error_string,
         'eval error: the term value does not fit the expected type')
     gmpz1 = yapi.yices_new_mpz()
     gmpz2 = yapi.yices_new_mpz()
     yapi.yices_get_mpz_value(mdl, i1, gmpz1)
     yapi.yices_get_mpz_value(mdl, i2, gmpz2)
     mpz1 = yapi.yices_mpz(gmpz1)
     mpz2 = yapi.yices_mpz(gmpz2)
     self.assertEqual(yapi.yices_term_to_string(mpz1, 200, 10, 0),
                      '987654321987654321987654322')
     self.assertEqual(yapi.yices_term_to_string(mpz2, 200, 10, 0),
                      '987654321987654321987654321')
     if not yapi.yices_has_mcsat():
         return
     yapi.yices_pp_term_fd(1, mpz1, 100, 10, 0)
     alg1 = yapi.lp_algebraic_number_t()
     #yapi.yices_get_algebraic_number_value(mdl, i1, alg1)
     #iam: 9/19/2018 with self.assertRaisesRegexp(YicesAPIException,
     #iam: 9/19/2018                              'could not convert value \(in model\) to a term'):
     #iam: 9/19/2018     yapi.yices_get_algebraic_number_value(mdl, i1, alg1)
     errcode = yapi.yices_get_algebraic_number_value(mdl, i1, alg1)
     error_string = yapi.yices_error_string()
     self.assertEqual(errcode, -1)
     self.assertEqual(error_string,
                      'could not convert value (in model) to a term')
Exemplo n.º 50
0
def mul(a, b, result=None):
    if(type(a) == np.ndarray and type(b) != np.ndarray):
        if result is None:
            result = np.empty_like(a, order='C')

        if (a.dtype == 'int32'):
            __lib.scalar_mul_int32(__getPointer(a), ct.c_int32(np.int32(b)),
                                   __getLen(a), __getPointer(result))
        elif (a.dtype == 'int64'):
            __lib.scalar_mul_int64(__getPointer(a), ct.c_int64(np.int64(b)),
                                   __getLen(a), __getPointer(result))
        elif (a.dtype == 'float32'):
            __lib.scalar_mul_float64(__getPointer(a), ct.c_float(np.float32(b)),
                                     __getLen(a), __getPointer(result))
        elif (a.dtype == 'float64'):
            __lib.scalar_mul_float64(__getPointer(a), ct.c_double(np.float64(b)),
                                     __getLen(a), __getPointer(result))
        elif (a.dtype == 'complex64'):
            __lib.scalar_mul_compex64(__getPointer(a), c_complex64(np.complex64(b)),
                                      __getLen(a), __getPointer(result))
        elif (a.dtype == 'complex128'):
            __lib.scalar_mul_complex128(__getPointer(a), c_complex128(np.complex128(b)),
                                        __getLen(a), __getPointer(result))
        else:
            raise TypeError('type ', a.dtype, ' is not supported')

    elif(type(b) == np.ndarray and type(a) != np.ndarray):
        return mul(b, a, result)
    elif(type(a) == np.ndarray and type(b) == np.ndarray):
        if result is None:
            result = np.empty_like(a, order='C')

        if (a.dtype == 'int32'):
            __lib.vector_mul_int32(__getPointer(a), __getPointer(b),
                                   __getLen(a), __getPointer(result))
        elif (a.dtype == 'int64'):
            __lib.vector_mul_int64(__getPointer(a), __getPointer(b),
                                   __getLen(a), __getPointer(result))
        elif (a.dtype == 'float32'):
            __lib.vector_mul_float64(__getPointer(a), __getPointer(b),
                                     __getLen(a), __getPointer(result))
        elif (a.dtype == 'float64'):
            __lib.vector_mul_float64(__getPointer(a), __getPointer(b),
                                     __getLen(a), __getPointer(result))
        elif (a.dtype == 'complex64'):
            __lib.vector_mul_compex64(__getPointer(a), __getPointer(b),
                                      __getLen(a), __getPointer(result))
        elif (a.dtype == 'complex128'):
            __lib.vector_mul_complex128(__getPointer(a), __getPointer(b),
                                        __getLen(a), __getPointer(result))
        else:
            raise TypeError('type ', a.dtype, ' is not supported')
    else:
        raise TypeError(
            'types {} and {} are not supported'.format(type(a), type(b)))
    return result
Exemplo n.º 51
0
 def _set_handle(self):
     out = _lib.hdfsOpenFile(self._fs, ensure_bytes(self.path),
                             mode_numbers[self.mode], self.buff,
                             ctypes.c_short(self.replication),
                             ctypes.c_int64(self.block_size))
     if not out:
         msg = ensure_string(_lib.hdfsGetLastError())
         raise IOError("Could not open file: %s, mode: %s %s" %
                       (self.path, self.mode, msg))
     self._handle = out
    def value(self) -> any:
        if self._attr & _VarAttr.READ == 0:
            raise SdkError(f"variable ({self.name}) not readable")
        if not self.valid:
            raise SdkError(
                f"variable ({self.name}) does not hold a valid value")

        if self._type == _VarType.BOOL:
            v = ctypes.c_int()
            if _intf.gsGetVariableValueAsInt(self._handle, ctypes.byref(v)):
                return v.value != 0
        # int
        if self._type == _VarType.INT:
            v = ctypes.c_int()
            if _intf.gsGetVariableValueAsInt(self._handle, ctypes.byref(v)):
                return v.value

        if self._type == _VarType.INT64 or self._type == _VarType.UINT:
            v = ctypes.c_int64()
            if _intf.gsGetVariableValueAsInt64(self._handle, ctypes.byref(v)):
                return v.value

        if self._type == _VarType.FLOAT:
            v = ctypes.c_float()
            if _intf.gsGetVariableValueAsFloat(self._handle, ctypes.byref(v)):
                return v.value

        if self._type == _VarType.DOUBLE:
            v = ctypes.c_double()
            if _intf.gsGetVariableValueAsDouble(self._handle, ctypes.byref(v)):
                return v.value

        # string
        if self._type == _VarType.STRING:
            return pchar2str(_intf.gsGetVariableValueAsString(self._handle))

        # time
        if self._type == _VarType.TIME:
            v = ctypes.c_int64()
            if _intf.gsGetVariableValueAsInt64(self._handle, ctypes.byref(v)):
                return datetime.utcfromtimestamp(v.value)

        raise SdkError(f"Unsupported variable type, name ({self.name})")
Exemplo n.º 53
0
    def num_data(self):
        """Get the number of rows in the Dataset.

        Returns
        -------
        number of rows : int
        """
        ret = ctypes.c_int64()
        _safe_call(_LIB.LGBM_DatasetGetNumData(self.handle, ctypes.byref(ret)))
        return ret.value
Exemplo n.º 54
0
    def get_metadata(self, object_id):
        """Create a buffer from the PlasmaStore based on object ID.

    If the object has not been sealed yet, this call will block until the object
    has been sealed. The retrieved buffer is immutable.

    Args:
      object_id (str): A string used to identify an object.
    """
        size = ctypes.c_int64()
        data = ctypes.c_void_p()
        metadata_size = ctypes.c_int64()
        metadata = ctypes.c_void_p()
        buf = self.client.plasma_get(self.store_conn,
                                     make_plasma_id(object_id),
                                     ctypes.byref(size), ctypes.byref(data),
                                     ctypes.byref(metadata_size),
                                     ctypes.byref(metadata))
        return self.buffer_from_memory(metadata, metadata_size)
Exemplo n.º 55
0
 def __init__(self, x, y, z, vals):
     self.n = len(x)
     self.tria_list = np.empty(6 * self.n, dtype='int64')
     self.tria_lptr = np.empty(6 * self.n, dtype='int64')
     self.tria_lend = np.empty(self.n, dtype='int64')
     self.tria_lnew = ctypes.c_int64()
     self.x = x
     self.y = y
     self.z = z
     self.vals = vals
Exemplo n.º 56
0
    def int64(self, n: int):
        self._type = TIBRVMSG_I64
        self._size = 8
        self._data = 0

        try:
            n = _ctypes.c_int64(n)
            self._data = n.value
        except:
            pass
Exemplo n.º 57
0
def mklGetCpuClocks():
    '''The mkl_get_cpu_clocks function returns the elapsed CPU clocks.
    This may be useful when timing short intervals with high resolution.
    The mkl_get_cpu_clocks function is also applied in pairs like second/dsecnd.
    Note that out-of-order code execution on IA-32 or Intel® 64 architecture
    processors may disturb the exact elapsed CPU clocks value a little bit,
    which may be important while measuring extremely short time intervals. '''
    clocks = ctypes.c_int64(0)
    _mklGetCpuClocks(ctypes.byref(clocks))
    return clocks.value
Exemplo n.º 58
0
    def execute(self, frame: Frame):
        stack = frame.operand_stack
        val = stack.pop_numeric()
        index = stack.pop_numeric()
        arr_ref = stack.pop_ref()

        check_not_none(arr_ref)
        long_array = arr_ref.longs()
        check_index(len(long_array), index)
        long_array[index] = ctypes.c_int64(val).value
Exemplo n.º 59
0
 def get_block_locations(self, path, start=0, length=0):
     """ Fetch physical locations of blocks """
     if not self._handle:
         raise IOError("Filesystem not connected")
     start = int(start) or 0
     length = int(length) or self.info(path)['size']
     nblocks = ctypes.c_int(0)
     out = _lib.hdfsGetFileBlockLocations(self._handle, ensure_bytes(path),
                             ctypes.c_int64(start), ctypes.c_int64(length),
                             ctypes.byref(nblocks))
     locs = []
     for i in range(nblocks.value):
         block = out[i]
         hosts = [block.hosts[i] for i in
                  range(block.numOfNodes)]
         locs.append({'hosts': hosts, 'length': block.length,
                      'offset': block.offset})
     _lib.hdfsFreeFileBlockLocations(out, nblocks)
     return locs
Exemplo n.º 60
0
 def get_value_from_rational_yval(self, yval):
     if yapi.yices_val_is_int64(self.model, yval):
         val = ctypes.c_int64()
         errcode = yapi.yices_val_get_int64(self.model,  yval, val)
         if errcode == -1:
             raise YicesException('yices_val_get_int64')
         return val.value
     if yapi.yices_val_is_rational64(self.model, yval):
         ytnum = ctypes.c_int64()
         ytden = ctypes.c_uint64()
         errcode = yapi.yices_val_get_rational64(self.model,  yval, ytnum, ytden)
         if errcode == -1:
             raise YicesException('yices_val_get_rational64')
         return Fraction(ytnum.value, ytden.value)
     val = ctypes.c_double()
     errcode = yapi.yices_val_get_double(self.model,  yval, val)
     if errcode == -1:
         raise YicesException('yices_val_get_double')
     return val.value