class SynapseEnvironment: def __init__(self, noise=0.0): def beta(maximum, rate=1.0): return betav(maximum, noise=noise, rate=rate) self.beta = beta self.prev_concentrations = [] self.next_concentrations = [] def initialize(self): # Create thread safe arrays. self.prev_concentrations = Array('d', self.prev_concentrations, lock=False) self.next_concentrations = Array('d', self.next_concentrations, lock=False) self.dirty = Value('b', True, lock=False) def register(self, baseline_concentration): pool_id = len(self.prev_concentrations) self.prev_concentrations.append(baseline_concentration) self.next_concentrations.append(baseline_concentration) return pool_id def get_concentration(self, pool_id): try: self.dirty except: self.initialize() return self.prev_concentrations[pool_id] def set_concentration(self, pool_id, new_concentration): try: self.dirty.value = True except: self.initialize() self.next_concentrations[pool_id] = new_concentration def add_concentration(self, pool_id, molecules): try: self.dirty.value = True except: self.initialize() self.next_concentrations[pool_id] += molecules def remove_concentration(self, pool_id, molecules): try: self.dirty.value = True except: self.initialize() self.next_concentrations[pool_id] -= molecules self.next_concentrations[pool_id] = \ max(0.0, self.next_concentrations[pool_id]) def step(self): """ Cycles the environment. Returns whether the environment is stable (not dirty, no changes) """ try: self.dirty except: self.initialize() if self.dirty.value: self.dirty.value = False for i in xrange(len(self.prev_concentrations)): self.prev_concentrations[i]=self.next_concentrations[i] return False else: return True
class NeuronEnvironment: def __init__(self, noise=0.0): def beta(maximum, rate=1.0): return betav(maximum, noise=noise, rate=rate) self.beta = beta self.prev_voltages = [] self.next_voltages = [] self.dirty = Value('b', True, lock=False) def initialize(self): # Create thread safe arrays. self.prev_voltages = Array('d', self.prev_voltages, lock=False) self.next_voltages = Array('d', self.next_voltages, lock=False) def register(self, baseline_voltage=0.0): neuron_id = len(self.prev_voltages) self.prev_voltages.append(baseline_voltage) self.next_voltages.append(baseline_voltage) return neuron_id def get_voltage(self, neuron_id): return self.prev_voltages[neuron_id] def set_voltage(self, neuron_id, new_voltage): self.dirty.value = True self.next_voltages[neuron_id] = new_voltage def adjust_voltage(self, neuron_id, delta): self.dirty.value = True self.next_voltages[neuron_id] += delta def step(self): """ Cycles the environment. Returns whether the environment is stable (not dirty, no changes) """ if self.dirty.value: self.dirty.value = False for i in xrange(len(self.prev_voltages)): self.prev_voltages[i]=self.next_voltages[i] return False else: return True
class Environment: def __init__(self, noise=0.0): def beta(maximum, rate=1.0): return betav(maximum, noise=noise, rate=rate) self.beta = beta self.prev_values = [] self.next_values = [] self.dirty = Value('b', True, lock=False) self.records = dict() self.spikes = dict() def initialize(self): # Create thread safe arrays. self.prev_values = Array('d', self.prev_values, lock=False) self.next_values = Array('d', self.next_values, lock=False) for key in self.records: self.records[key] = manager.list() for key in self.spikes: self.spikes[key] = manager.list() def register(self, initial=0.0, record=False, spiking=False): env_id = len(self.prev_values) self.prev_values.append(initial) self.next_values.append(initial) if record: if spiking: self.spikes[env_id] = True else: self.records[env_id] = True return env_id def get(self, env_id): return self.prev_values[env_id] def set(self, env_id, new_voltage): self.dirty.value = True self.next_values[env_id] = new_voltage def adjust(self, env_id, delta): self.dirty.value = True self.next_values[env_id] += delta def step(self): """ Cycles the environment. Returns whether the environment is stable (not dirty, no changes) """ # Record any env_ids that have been set to record. for env_id in self.records: self.records[env_id].append(self.prev_values[env_id]) for env_id in self.spikes: self.spikes[env_id].append(1 if self.prev_values[env_id] >= 30.0 else 0) if self.dirty.value: self.dirty.value = False for i in xrange(len(self.prev_values)): self.prev_values[i]=self.next_values[i] return False else: return True
import time image = Array('i', range(0)) image = [] def fun(pixels, i): print("Task {} started".format(i)) for _ in range(100000000): pass print("Task {} finised".format(i)) pixels[i] = 1 for j in range(3): pixels = Array('i', range(8)) t1 = time.time() ts = [] for i in range(8): t = Process(target=fun, args=(pixels, i)) t.start() ts.append(t) for t in ts: t.join() print("j : {}", format(j)) image.append(pixels[:]) print("Time Spend: {:.2f} s".format(time.time() - t1)) print(pixels[:])
mylist = [i for i in range(100)] start_time = time.time() # The task is offloaded and distributed among the cores and processes automatically by Pool object # So we don't need to worry about creating processes explicitly p = Pool() # The contents of mylist and definition of square will be distributed among the cores result = p.map(square, mylist) # print(result) print(time.time() - start_time) start_time = time.time() result = [] for l in mylist: result.append(square(l)) # print(f'Worder process id for {l} : {os.getpid()}') # print(result) print(time.time() - start_time) print('**********') print() nums = [10, 100, 1000, 10000, 100000, 1000000, 10000000] result_single = [] result_parallel = [] for num in nums: print('num', num) mylist = [i for i in range(num)]