def test_set_by_inds_scalar(self): # Nonempty index array: pm = GPUPortMapper('/foo[0:3]', np.zeros(3, np.double)) pm.set_by_inds(np.array([0, 1]), 1.0) assert_array_equal(np.ones(2, np.double), pm.data.get()[0:2]) # Empty index array: pm = GPUPortMapper('/foo[0:3]', np.zeros(3, np.double)) pm.set_by_inds(np.array([]), 1.0) assert_array_equal(np.zeros(2, np.double), pm.data.get()[0:2])
def test_set_array(self): # Valid empty: pm = GPUPortMapper('/foo[0:3]') new_data = np.arange(2).astype(np.double) pm['/foo[0:2]'] = new_data assert_array_equal(new_data, pm.data.get()[0:2]) # Valid nonempty: data = np.random.rand(3) pm = GPUPortMapper('/foo[0:3]', data) new_data = np.arange(2).astype(np.double) pm['/foo[0:2]'] = new_data assert_array_equal(new_data, pm.data.get()[0:2])
def test_from_pm_gpu(self): # Empty: pm0 = GPUPortMapper('/foo[0:3]') pm1 = GPUPortMapper.from_pm(pm0) assert_series_equal(pm0.portmap, pm1.portmap) # Nonempty: data = np.random.rand(3) pm0 = GPUPortMapper('/foo[0:3]', data) pm1 = GPUPortMapper.from_pm(pm0) assert_array_equal(pm0.data.get(), pm1.data.get()) assert_series_equal(pm0.portmap, pm1.portmap) assert pm0.data.ptr != pm1.data.ptr
def test_set_by_inds_array(self): # Nonempty index array: data = np.random.rand(3) pm = GPUPortMapper('/foo[0:3]', data) new_data = np.arange(2).astype(np.double) pm.set_by_inds(np.array([0, 1]), new_data) assert_array_equal(new_data, pm.data.get()[0:2]) # Empty index array: data = np.random.rand(3) pm = GPUPortMapper('/foo[0:3]', data) new_data = np.arange(2).astype(np.double) pm.set_by_inds(np.array([], np.int_), new_data) assert_array_equal(data[0:2], pm.data.get()[0:2])
def test_copy(self): # Empty: pm0 = GPUPortMapper('/foo[0:5]') pm1 = pm0.copy() assert_series_equal(pm0.portmap, pm1.portmap) assert pm0.data is None and pm1.data is None # Nonempty: data = np.random.rand(5) pm0 = GPUPortMapper('/foo[0:5]', data) pm1 = pm0.copy() assert_array_equal(pm0.data.get(), pm1.data.get()) assert_series_equal(pm0.portmap, pm1.portmap) assert pm0.data.ptr != pm1.data.ptr
def test_set_scalar(self): # Nonempty index array: data = np.random.rand(3) pm = GPUPortMapper('/foo[0:3]', data) new_data = 1.0 ind = np.array([0, 1]) pm.set_by_inds(ind, new_data) assert_array_equal(np.full(ind.shape, new_data, type(new_data)), pm.data.get()[0:2]) # Empty index array: data = np.random.rand(3) pm = GPUPortMapper('/foo[0:3]', data) new_data = 1.0 pm.set_by_inds(np.array([], np.int_), new_data) assert_array_equal(data[0:2], pm.data.get()[0:2])
def test_get_by_inds(self): # Nonempty index array: data = np.random.rand(3) pm = GPUPortMapper('/foo[0:3]', data) res = pm.get_by_inds(np.array([0, 1])) assert_array_equal(data[0:2], res) # Empty index array: res = pm.get_by_inds(np.array([], np.int_)) assert len(res) == 0
def test_get(self): data = np.random.rand(3) pm = GPUPortMapper('/foo[0:3]', data) res = pm['/foo[0:2]'] assert_array_equal(data[0:2], res)
def test_set_by_inds(self): data = np.random.rand(3) pm = GPUPortMapper('/foo[0:3]', data) new_data = np.arange(2).astype(np.double) pm.set_by_inds(np.array([0, 1]), new_data) assert_array_equal(new_data, pm.data.get()[0:2])
def test_get_by_inds(self): data = np.random.rand(3) pm = GPUPortMapper('/foo[0:3]', data) res = pm.get_by_inds(np.array([0, 1])) assert_array_equal(data[0:2], res)
def __init__(self, sel, sel_in, sel_out, sel_gpot, sel_spike, data_gpot, data_spike, columns=['interface', 'io', 'type'], ctrl_tag=CTRL_TAG, gpot_tag=GPOT_TAG, spike_tag=SPIKE_TAG, id=None, device=None, routing_table=None, rank_to_id=None, debug=False, time_sync=False, print_timing=False): super(Module, self).__init__(ctrl_tag) self.debug = debug self.time_sync = time_sync self.device = device self.print_timing = print_timing self._gpot_tag = gpot_tag self._spike_tag = spike_tag # Require several necessary attribute columns: if 'interface' not in columns: raise ValueError('interface column required') if 'io' not in columns: raise ValueError('io column required') if 'type' not in columns: raise ValueError('type column required') # Initialize GPU here so as to be able to initialize a port mapper # containing GPU memory: self._init_gpu() # This is needed to ensure that MPI_Finalize is called before PyCUDA # attempts to clean up; see # https://groups.google.com/forum/#!topic/mpi4py/by0Rd5q0Ayw atexit.register(MPI.Finalize) # Manually register the file close method associated with MPIOutput # so that it is called by atexit before MPI.Finalize() (if the file is # closed after MPI.Finalize() is called, an error will occur): for k, v in iteritems(twiggy.emitters): if isinstance(v._output, MPIOutput): atexit.register(v._output.close) # Ensure that the input and output port selectors respectively # select mutually exclusive subsets of the set of all ports exposed by # the module: if not SelectorMethods.is_in(sel_in, sel): raise ValueError('input port selector not in selector of all ports') if not SelectorMethods.is_in(sel_out, sel): raise ValueError('output port selector not in selector of all ports') if not SelectorMethods.are_disjoint(sel_in, sel_out): raise ValueError('input and output port selectors not disjoint') #assert(SelectorMethods.is_in(sel_in, sel)) #assert(SelectorMethods.is_in(sel_out, sel)) #assert(SelectorMethods.are_disjoint(sel_in, sel_out)) # Ensure that the graded potential and spiking port selectors # respectively select mutually exclusive subsets of the set of all ports # exposed by the module: #assert(SelectorMethods.is_in(sel_gpot, sel)) #assert(SelectorMethods.is_in(sel_spike, sel)) #assert(SelectorMethods.are_disjoint(sel_gpot, sel_spike)) # Save routing table and mapping between MPI ranks and module IDs: self.routing_table = routing_table self.rank_to_id = rank_to_id # Generate a unique ID if none is specified: if id is None: self.id = uid() else: # If a unique ID was specified and the routing table is not empty # (i.e., there are connections between multiple modules), the id # must be a node in the routing table: if routing_table is not None and len(routing_table.ids) and \ not routing_table.has_node(id): raise ValueError('routing table must contain specified ' 'module ID: {}'.format(id)) self.id = id # Reformat logger name: LoggerMixin.__init__(self, 'mod %s' % self.id) if self.print_timing: start = time.time() # Create module interface given the specified ports: self.interface = Interface(sel, columns) # Set the interface ID to 0; we assume that a module only has one interface: self.interface[sel, 'interface'] = 0 # Set the port attributes: if len(sel_in): self.interface[sel_in, 'io'] = 'in' if len(sel_out): self.interface[sel_out, 'io'] = 'out' if len(sel_gpot): self.interface[sel_gpot, 'type'] = 'gpot' if len(sel_spike): self.interface[sel_spike, 'type'] = 'spike' if self.print_timing: self.log_info('Elapsed time for setting up interface: {:.3f} seconds'.format(time.time()-start)) start = time.time() # Find the graded potential and spiking ports: self.gpot_ports = self.interface.gpot_ports() self.spike_ports = self.interface.spike_ports() if len(self.gpot_ports): self.in_gpot_ports = self.gpot_ports.in_ports(tuples=True) self.out_gpot_ports = self.gpot_ports.out_ports(tuples=True) else: self.in_gpot_ports = [] self.out_gpot_ports = [] if len(self.spike_ports): self.in_spike_ports = self.spike_ports.in_ports(tuples=True) self.out_spike_ports = self.spike_ports.out_ports(tuples=True) else: self.in_spike_ports = [] self.out_spike_ports = [] if self.print_timing: self.log_info('Elapsed time for extracting ports: {:.3f} seconds'.format(time.time()-start)) start = time.time() # Set up mapper between port identifiers and their associated data: if len(data_gpot) != len(self.gpot_ports): raise ValueError('incompatible gpot port data array length') if len(data_spike) != len(self.spike_ports): raise ValueError('incompatible spike port data array length') self.data = {} self.data['gpot'] = gpuarray.to_gpu(data_gpot) self.data['spike'] = gpuarray.to_gpu(data_spike) self.pm = {} self.pm['gpot'] = GPUPortMapper(sel_gpot, self.data['gpot'], make_copy=False) self.pm['spike'] = GPUPortMapper(sel_spike, self.data['spike'], make_copy=False) if self.print_timing: cuda.Context.synchronize() self.log_info('Elapsed time for creating array and PortMapper {} seconds'.format(time.time()-start))