Esempio n. 1
0
    def __getitem__(self, key):
        pos, tidx, shape = gpt.map_key(self, key)
        val = cgpt.lattice_export(self.v_obj, pos, tidx, shape)

        # if only a single element is returned and we have the full shape,
        # wrap in a tensor
        if len(val) == 1 and shape == self.otype.shape:
            return gpt.util.value_to_tensor(val[0], self.otype)

        return val
Esempio n. 2
0
    def __setitem__(self, key, value):
        # unpack cache
        cache, key = unpack_cache_key(key)
        cache_key = None if cache is None else "set"

        # short code path to zero lattice
        if type(key) == slice and key == slice(None, None, None):

            if gpt.util.is_num(value):
                for o in self.v_obj:
                    cgpt.lattice_set_to_number(o, value)
                return

            cache_key = (
                f"{self.otype.__name__}_{self.checkerboard().__name__}_{self.grid.describe()}"
            )
            cache = lattice.cache

        # general code path, map key
        pos, tidx, shape = gpt.map_key(self, key)
        n_pos = len(pos)

        # convert input to proper numpy array
        value = gpt.util.tensor_to_value(
            value, dtype=self.grid.precision.complex_dtype)
        if value is None:
            value = memoryview(bytearray())

        # needed bytes and optional cyclic upscaling
        nbytes_needed = n_pos * numpy.prod(
            shape) * self.grid.precision.nbytes * 2
        value = cgpt.copy_cyclic_upscale(value, nbytes_needed)

        # create plan
        if cache_key is None or cache_key not in cache:
            plan = gpt.copy_plan(self, value)
            plan.destination += gpt.lattice_view(self, pos, tidx)
            plan.source += gpt.global_memory_view(
                self.grid,
                [[self.grid.processor, value, 0, value.nbytes]]
                if value.nbytes > 0 else None,
            )

            # skip optimization if we only use it once
            xp = plan(
                local_only=isinstance(pos, gpt.core.local_coordinates),
                skip_optimize=cache_key is None,
            )
            if cache_key is not None:
                cache[cache_key] = xp
        else:
            xp = cache[cache_key]

        xp(self, value)
Esempio n. 3
0
    def __getitem__(self, key):

        # unpack cache
        cache, key = unpack_cache_key(key)
        cache_key = None if cache is None else "get"

        # general code path, map key
        pos, tidx, shape = gpt.map_key(self, key)
        n_pos = len(pos)

        # create target
        value = cgpt.ndarray((n_pos, *shape),
                             self.grid.precision.complex_dtype)

        # create plan
        if cache_key is None or cache_key not in cache:
            plan = gpt.copy_plan(value, self)
            plan.destination += gpt.global_memory_view(
                self.grid,
                [[self.grid.processor, value, 0, value.nbytes]]
                if value.nbytes > 0 else None,
            )
            plan.source += gpt.lattice_view(self, pos, tidx)
            xp = plan()

            if cache_key is not None:
                cache[cache_key] = xp
        else:
            xp = cache[cache_key]

        xp(value, self)

        # if only a single element is returned and we have the full shape,
        # wrap in a tensor
        if len(value) == 1 and shape == self.otype.shape:
            return gpt.util.value_to_tensor(value[0], self.otype)

        return value
Esempio n. 4
0
    def __setitem__(self, key, value):
        # short code path to zero lattice
        if (type(key) == slice and key == slice(None, None, None)
                and type(value) == int and value == 0):
            for o in self.v_obj:
                cgpt.lattice_set_to_zero(o)
            return

        # general code path, map key
        pos, tidx, shape = gpt.map_key(self, key)

        # copy from view or array
        if type(value) == tuple:
            # direct copy from view
            cgpt.lattice_import_view(self.v_obj, pos, tidx, value[3], value[0],
                                     value[1])
        else:
            # convert input to proper numpy array
            value = gpt.util.tensor_to_value(
                value, dtype=self.grid.precision.complex_dtype)

            # and import
            cgpt.lattice_import(self.v_obj, pos, tidx, value)
Esempio n. 5
0
 def __getitem__(self, key):
     pos, tidx, shape = gpt.map_key(self.parent, key)
     return gpt.lattice_view(self.parent, pos, tidx)
Esempio n. 6
0
 def __getitem__(self, key):
     return gpt.map_key(self.parent, key) + (self.parent.v_obj, )