Guest User

Untitled

a guest
Mar 9th, 2016
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.84 KB | None | 0 0
  1. import numpy
  2. import shutils
  3. import time
  4.  
  5. def fast():
  6.     CHANNELS = 16
  7.     SAMPLE_LENGTH = CHANNELS // 8
  8.     BUFFER = 256
  9.     PROCNUM = 4
  10.    
  11.     vals = []
  12.     for i in range(2**CHANNELS):
  13.         vals.append(tuple(b'1' if e == '1' else b'0' for e in list("{:016b}".format(i))))
  14.  
  15.     data = np.fromfile('largefile.dat', dtype=np.uint16)
  16.     size = len(data)
  17.     print("Data size: %d" % size)
  18.  
  19.     def work(data, vals, proc):
  20.         size = len(data)
  21.    
  22.         print("Process %d data size: %d" % (proc, size))
  23.    
  24.         out_list = []
  25.         for i in range(CHANNELS):
  26.             out_file = open('dir/largefile_dump_%d_%d.dat' % (i, proc), 'wb')
  27.             out_list.append(out_file)
  28.        
  29.         samples = []
  30.         j = 0
  31.        
  32.         for sample in data:
  33.             samples.append(vals[sample])
  34.             if j >= BUFFER:
  35.                 samples = zip(*samples)
  36.                 for channel, bits in enumerate(samples):
  37.                     s = b''.join(bits)
  38.                     out_list[channel].write(s)
  39.                 samples = []
  40.                 j = 0
  41.             j += 1
  42.        
  43.         if samples:
  44.             samples = zip(*samples)
  45.             for channel, bits in enumerate(samples):
  46.                 s = b''.join(bits)
  47.                 out_list[channel].write(s)
  48.  
  49.         for i in range(CHANNELS):
  50.             out_list[i].close()
  51.    
  52.         print("Process %d is complete" % proc)
  53.    
  54.     samples_per_process = (size // SAMPLE_LENGTH) // PROCNUM
  55.     procs = []
  56.    
  57.     for i in range(PROCNUM):
  58.         d_size = SAMPLE_LENGTH*samples_per_process
  59.         if i != (PROCNUM - 1):
  60.             d = data[i*d_size:(i+1)*d_size]
  61.         else:
  62.             d = data[i*d_size:]
  63.         p = Process(target=work, args=(d, vals, i))
  64.         procs.append(p)
  65.         p.start()
  66.        
  67.     while True:
  68.         if not any(Process.is_alive(proc) for proc in procs):
  69.             break
  70.         time.sleep(0.5)
  71.        
  72.     print("STARTING TO MERGE FILES!")
  73.    
  74.     for i in range(CHANNELS):
  75.         files = ['dir/%s' % f for f in os.listdir('dir') if '_%d_' % i in f]
  76.         with open("dir/channel_%d.dat" % i, 'wb') as w:
  77.             for f in files:
  78.                 with open(f, 'rb') as r:
  79.                     shutil.copyfileobj(r, w, 10*1024*1024)
  80.  
  81. fast()
Advertisement
Add Comment
Please, Sign In to add comment