Commit 1929d313581c4ff09b23cdfd22803992378478fd

Authored by Brice COLOMBIER
1 parent b67dffe368
Exists in master

Add memory info to pairwise_operation

Showing 6 changed files with 62 additions and 46 deletions

  1 +import numpy as np
  2 +import argparse
  3 +import os
  4 +
  5 +import matplotlib.pyplot as plt
  6 +
  7 +def realign(traces_file):
  8 + traces = np.load(traces_file)
  9 + averaged_traces = np.mean(traces, axis=0)
  10 + print np.shape(averaged_traces)
  11 + dirname, filename = os.path.split(args.traces_file)
  12 + np.save(os.path.join(dirname, "averaged_"+filename), averaged_traces[None,:])
  13 +
  14 +if __name__ == "__main__":
  15 +
  16 + # Parsing arguments
  17 + parser = argparse.ArgumentParser(description='Preprocess traces')
  18 + parser.add_argument("traces_file", type=str)
  19 + args = parser.parse_args()
  20 + realign(args.traces_file)
check_ram.py View file @ 1929d31
1   -import psutil
2   -
3   -import logging as log
4   -import argparse
5   -
6   -def check_ram(nb_samples, sample_size, verbose=False):
7   -
8   - if verbose:
9   - log.basicConfig(format="%(levelname)s: %(message)s", level=log.DEBUG)
10   - log.info("Verbose output enabled")
11   - else:
12   - log.basicConfig(format="%(levelname)s: %(message)s")
13   - available = psutil.virtual_memory().available
14   - log.info("{0} bits available".format(available))
15   - to_store = nb_samples*sample_size
16   - log.info("{0} bits to store".format(to_store))
17   - if to_store < available:
18   - return True
19   - else:
20   - log.warning("Not enough RAM available to store all the samples")
21   - return False
22   -
23   -if __name__ == "__main__":
24   -
25   - parser = argparse.ArgumentParser(description='Check RAM availability')
26   - parser.add_argument("--nb_samples", type=int)
27   - parser.add_argument("--sample_size", type=int)
28   - parser.add_argument('-v', '--verbose', action='store_true')
29   - args = parser.parse_args()
30   - print check_ram(args.nb_samples, args.sample_size, args.verbose)
npy_to_raw.py View file @ 1929d31
  1 +import numpy as np
  2 +
  3 +import argparse
  4 +import os
  5 +
  6 +def npy_to_raw(traces_name):
  7 + traces = np.load(traces_name)
  8 +
  9 + print traces
  10 +
  11 + dirname, filename = os.path.split(traces_name)
  12 + filename, extension = os.path.splitext(filename)
  13 +
  14 + traces.tofile(os.path.join(dirname, filename)+'.raw',
  15 + sep = "",
  16 + format = "%s")
  17 +
  18 +if __name__ == "__main__":
  19 +
  20 + # Parsing arguments
  21 + parser = argparse.ArgumentParser(description='Preprocess traces')
  22 + parser.add_argument("traces_name", type=str)
  23 + args = parser.parse_args()
  24 +
  25 + npy_to_raw(args.traces_name)
pairwise_operation.py View file @ 1929d31
1 1 # Imports for processing
2 2 import numpy as np
3 3 from skimage.util.shape import view_as_windows
4   -import check_ram
5 4  
6 5 # Imports for parallel processing
7 6 from multiprocessing import Pool, current_process
8 7  
9 8  
... ... @@ -63,16 +62,18 @@
63 62 for current_start_index in xrange(nb_samples - current_distance):
64 63 if first_chunk or (not first_chunk and current_start_index+current_distance>=window_size-1):
65 64 preprocessed_trace_length+=1
  65 + log.info("Original traces occupying {0} Mbytes".format(traces.nbytes/1000000))
  66 + increase = preprocessed_trace_length/np.shape(traces)[1]
  67 + log.info("Preprocessed traces will occupy {0}x more memory".format(increase))
  68 + log.info("Preprocessed traces will occupy {0} Mbytes".format(increase*traces.nbytes/1000000))
  69 + bla
66 70 # Allocate memory for the preprocessed trace
67   - if not check_ram.check_ram(preprocessed_trace_length, 64):
68   - raise MemoryError("The preprocessed trace, consisting in {0} samples of {1} bits, cannot be stored in RAM".format(preprocessed_trace_length, 64))
69   - else:
70   - log.info('This will occupy {0} Mo in RAM'.format(round(preprocessed_trace_length/(1024*1024))))
71 71 preprocessed_trace = np.zeros((preprocessed_trace_length, nb_traces), dtype=dtype)
72 72 current_index = 0
73 73 indexes = np.zeros((preprocessed_trace_length),dtype='i,i')
74 74 # For all possible start indices in the window
75 75 for current_distance in xrange(minimum_distance, window_size):
  76 + print current_distance
76 77 for current_start_index in xrange(nb_samples - current_distance):
77 78 if first_chunk or (not first_chunk and current_start_index+current_distance>=window_size-1):
78 79 value = np.array(operation(traces[:,current_start_index], traces[:,current_start_index+current_distance]), ndmin=2)
... ... @@ -80,7 +81,7 @@
80 81 preprocessed_trace[current_index,:] = np.transpose(value)[:,0]
81 82 indexes[current_index] = (start_index+current_start_index, start_index+current_start_index+current_distance)
82 83 # Increase the running index
83   - if current_index in [16290, 17685, 16304, 16436, 16318, 16450, 16332, 16464]:
  84 + if current_index in []:
84 85 print current_index
85 86 print current_start_index
86 87 print current_distance
... ... @@ -5,14 +5,15 @@
5 5  
6 6 def plot(traces_file, nb_subplots=1):
7 7 traces = np.load(traces_file)
8   - points_of_interest = [(21, 66),
9   - (35, 66),
10   - (49, 66),
11   - (63, 66),
12   - (167, 66),
13   - (181, 66),
14   - (195, 66),
15   - (153, 72)]
  8 + # points_of_interest = [(21, 66),
  9 + # (35, 66),
  10 + # (49, 66),
  11 + # (63, 66),
  12 + # (167, 66),
  13 + # (181, 66),
  14 + # (195, 66),
  15 + # (153, 72)]
  16 + points_of_interest = []
16 17 colors = {66:'#1250D2',
17 18 72:'#C21637'}
18 19 if nb_subplots>1:
... ... @@ -24,7 +24,7 @@
24 24 # Parsing arguments
25 25 parser = argparse.ArgumentParser(description='Preprocess traces')
26 26 parser.add_argument("traces_file", type=str)
27   - parser.add_argument("-r", "--ref_trace_index", type=int)
  27 + parser.add_argument("-r", "--ref_trace_index", type=int, default=0)
28 28 args = parser.parse_args()
29 29 realign(args.traces_file, args.ref_trace_index)