diff options
author | orivej <orivej@yandex-team.ru> | 2022-02-10 16:45:01 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:45:01 +0300 |
commit | 2d37894b1b037cf24231090eda8589bbb44fb6fc (patch) | |
tree | be835aa92c6248212e705f25388ebafcf84bc7a1 /contrib/tools/cython/Cython/Compiler/MemoryView.py | |
parent | 718c552901d703c502ccbefdfc3c9028d608b947 (diff) | |
download | ydb-2d37894b1b037cf24231090eda8589bbb44fb6fc.tar.gz |
Restoring authorship annotation for <orivej@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'contrib/tools/cython/Cython/Compiler/MemoryView.py')
-rw-r--r-- | contrib/tools/cython/Cython/Compiler/MemoryView.py | 200 |
1 files changed, 100 insertions, 100 deletions
diff --git a/contrib/tools/cython/Cython/Compiler/MemoryView.py b/contrib/tools/cython/Cython/Compiler/MemoryView.py index 9974bb4db3..0406d6c716 100644 --- a/contrib/tools/cython/Cython/Compiler/MemoryView.py +++ b/contrib/tools/cython/Cython/Compiler/MemoryView.py @@ -25,7 +25,7 @@ ERR_UNINITIALIZED = ("Cannot check if memoryview %s is initialized without the " def concat_flags(*flags): return "(%s)" % "|".join(flags) - + format_flag = "PyBUF_FORMAT" memview_c_contiguous = "(PyBUF_C_CONTIGUOUS | PyBUF_FORMAT)" @@ -67,7 +67,7 @@ memview_typeptr_cname = '__pyx_memoryview_type' memview_objstruct_cname = '__pyx_memoryview_obj' memviewslice_cname = u'__Pyx_memviewslice' - + def put_init_entry(mv_cname, code): code.putln("%s.data = NULL;" % mv_cname) code.putln("%s.memview = NULL;" % mv_cname) @@ -76,7 +76,7 @@ def put_init_entry(mv_cname, code): #def axes_to_str(axes): # return "".join([access[0].upper()+packing[0] for (access, packing) in axes]) - + def put_acquire_memoryviewslice(lhs_cname, lhs_type, lhs_pos, rhs, code, have_gil=False, first_assignment=True): "We can avoid decreffing the lhs if we know it is the first assignment" @@ -97,7 +97,7 @@ def put_acquire_memoryviewslice(lhs_cname, lhs_type, lhs_pos, rhs, code, if not pretty_rhs: code.funcstate.release_temp(rhstmp) - + def put_assign_to_memviewslice(lhs_cname, rhs, rhs_cname, memviewslicetype, code, have_gil=False, first_assignment=False): if not first_assignment: @@ -108,7 +108,7 @@ def put_assign_to_memviewslice(lhs_cname, rhs, rhs_cname, memviewslicetype, code code.putln("%s = %s;" % (lhs_cname, rhs_cname)) - + def get_buf_flags(specs): is_c_contig, is_f_contig = is_cf_contig(specs) @@ -124,13 +124,13 @@ def get_buf_flags(specs): else: return memview_strided_access - + def insert_newaxes(memoryviewtype, n): axes = [('direct', 'strided')] * n axes.extend(memoryviewtype.axes) return PyrexTypes.MemoryViewSliceType(memoryviewtype.dtype, axes) - + def broadcast_types(src, dst): n = abs(src.ndim - dst.ndim) if src.ndim < dst.ndim: @@ -173,20 +173,20 @@ def valid_memslice_dtype(dtype, i=0): class MemoryViewSliceBufferEntry(Buffer.BufferEntry): - """ - May be used during code generation time to be queried for - shape/strides/suboffsets attributes, or to perform indexing or slicing. - """ + """ + May be used during code generation time to be queried for + shape/strides/suboffsets attributes, or to perform indexing or slicing. + """ def __init__(self, entry): self.entry = entry self.type = entry.type self.cname = entry.cname - + self.buf_ptr = "%s.data" % self.cname dtype = self.entry.type.dtype - self.buf_ptr_type = PyrexTypes.CPtrType(dtype) - self.init_attributes() + self.buf_ptr_type = PyrexTypes.CPtrType(dtype) + self.init_attributes() def get_buf_suboffsetvars(self): return self._for_all_ndim("%s.suboffsets[%d]") @@ -203,12 +203,12 @@ class MemoryViewSliceBufferEntry(Buffer.BufferEntry): return self._generate_buffer_lookup_code(code, axes) def _generate_buffer_lookup_code(self, code, axes, cast_result=True): - """ - Generate a single expression that indexes the memory view slice - in each dimension. - """ + """ + Generate a single expression that indexes the memory view slice + in each dimension. + """ bufp = self.buf_ptr - type_decl = self.type.dtype.empty_declaration_code() + type_decl = self.type.dtype.empty_declaration_code() for dim, index, access, packing in axes: shape = "%s.shape[%d]" % (self.cname, dim) @@ -257,9 +257,9 @@ class MemoryViewSliceBufferEntry(Buffer.BufferEntry): then it must be coercible to Py_ssize_t Simply call __pyx_memoryview_slice_memviewslice with the right - arguments, unless the dimension is omitted or a bare ':', in which - case we copy over the shape/strides/suboffsets attributes directly - for that dimension. + arguments, unless the dimension is omitted or a bare ':', in which + case we copy over the shape/strides/suboffsets attributes directly + for that dimension. """ src = self.cname @@ -267,46 +267,46 @@ class MemoryViewSliceBufferEntry(Buffer.BufferEntry): code.putln("%(dst)s.memview = %(src)s.memview;" % locals()) code.put_incref_memoryviewslice(dst) - all_dimensions_direct = all(access == 'direct' for access, packing in self.type.axes) - suboffset_dim_temp = [] - - def get_suboffset_dim(): - # create global temp variable at request - if not suboffset_dim_temp: - suboffset_dim = code.funcstate.allocate_temp(PyrexTypes.c_int_type, manage_ref=False) - code.putln("%s = -1;" % suboffset_dim) - suboffset_dim_temp.append(suboffset_dim) - return suboffset_dim_temp[0] - + all_dimensions_direct = all(access == 'direct' for access, packing in self.type.axes) + suboffset_dim_temp = [] + + def get_suboffset_dim(): + # create global temp variable at request + if not suboffset_dim_temp: + suboffset_dim = code.funcstate.allocate_temp(PyrexTypes.c_int_type, manage_ref=False) + code.putln("%s = -1;" % suboffset_dim) + suboffset_dim_temp.append(suboffset_dim) + return suboffset_dim_temp[0] + dim = -1 - new_ndim = 0 + new_ndim = 0 for index in indices: - if index.is_none: - # newaxis - for attrib, value in [('shape', 1), ('strides', 0), ('suboffsets', -1)]: - code.putln("%s.%s[%d] = %d;" % (dst, attrib, new_ndim, value)) - - new_ndim += 1 - continue - - dim += 1 - access, packing = self.type.axes[dim] + if index.is_none: + # newaxis + for attrib, value in [('shape', 1), ('strides', 0), ('suboffsets', -1)]: + code.putln("%s.%s[%d] = %d;" % (dst, attrib, new_ndim, value)) + + new_ndim += 1 + continue + + dim += 1 + access, packing = self.type.axes[dim] if isinstance(index, ExprNodes.SliceNode): # slice, unspecified dimension, or part of ellipsis - d = dict(locals()) + d = dict(locals()) for s in "start stop step".split(): idx = getattr(index, s) have_idx = d['have_' + s] = not idx.is_none - d[s] = idx.result() if have_idx else "0" + d[s] = idx.result() if have_idx else "0" - if not (d['have_start'] or d['have_stop'] or d['have_step']): + if not (d['have_start'] or d['have_stop'] or d['have_step']): # full slice (:), simply copy over the extent, stride # and suboffset. Also update suboffset_dim if needed d['access'] = access - util_name = "SimpleSlice" + util_name = "SimpleSlice" else: - util_name = "ToughSlice" + util_name = "ToughSlice" d['error_goto'] = code.error_goto(index.pos) new_ndim += 1 @@ -314,42 +314,42 @@ class MemoryViewSliceBufferEntry(Buffer.BufferEntry): # normal index idx = index.result() - indirect = access != 'direct' - if indirect: - generic = access == 'full' + indirect = access != 'direct' + if indirect: + generic = access == 'full' if new_ndim != 0: return error(index.pos, "All preceding dimensions must be " "indexed and not sliced") - d = dict( - locals(), - wraparound=int(directives['wraparound']), + d = dict( + locals(), + wraparound=int(directives['wraparound']), boundscheck=int(directives['boundscheck']), - ) + ) if d['boundscheck']: d['error_goto'] = code.error_goto(index.pos) - util_name = "SliceIndex" + util_name = "SliceIndex" + + _, impl = TempitaUtilityCode.load_as_string(util_name, "MemoryView_C.c", context=d) + code.put(impl) - _, impl = TempitaUtilityCode.load_as_string(util_name, "MemoryView_C.c", context=d) - code.put(impl) + if suboffset_dim_temp: + code.funcstate.release_temp(suboffset_dim_temp[0]) - if suboffset_dim_temp: - code.funcstate.release_temp(suboffset_dim_temp[0]) - def empty_slice(pos): none = ExprNodes.NoneNode(pos) return ExprNodes.SliceNode(pos, start=none, stop=none, step=none) - -def unellipsify(indices, ndim): + +def unellipsify(indices, ndim): result = [] seen_ellipsis = False have_slices = False - newaxes = [newaxis for newaxis in indices if newaxis.is_none] + newaxes = [newaxis for newaxis in indices if newaxis.is_none] n_indices = len(indices) - len(newaxes) for index in indices: @@ -364,7 +364,7 @@ def unellipsify(indices, ndim): result.extend([full_slice] * nslices) seen_ellipsis = True else: - have_slices = have_slices or index.is_slice or index.is_none + have_slices = have_slices or index.is_slice or index.is_none result.append(index) result_length = len(result) - len(newaxes) @@ -373,9 +373,9 @@ def unellipsify(indices, ndim): nslices = ndim - result_length result.extend([empty_slice(indices[-1].pos)] * nslices) - return have_slices, result, newaxes + return have_slices, result, newaxes + - def get_memoryview_flag(access, packing): if access == 'full' and packing in ('strided', 'follow'): return 'generic' @@ -391,47 +391,47 @@ def get_memoryview_flag(access, packing): assert (access, packing) == ('direct', 'contig'), (access, packing) return 'contiguous' - + def get_is_contig_func_name(contig_type, ndim): assert contig_type in ('C', 'F') return "__pyx_memviewslice_is_contig_%s%d" % (contig_type, ndim) - + def get_is_contig_utility(contig_type, ndim): assert contig_type in ('C', 'F') C = dict(context, ndim=ndim, contig_type=contig_type) utility = load_memview_c_utility("MemviewSliceCheckContig", C, requires=[is_contig_utility]) return utility - -def slice_iter(slice_type, slice_result, ndim, code): - if slice_type.is_c_contig or slice_type.is_f_contig: - return ContigSliceIter(slice_type, slice_result, ndim, code) + +def slice_iter(slice_type, slice_result, ndim, code): + if slice_type.is_c_contig or slice_type.is_f_contig: + return ContigSliceIter(slice_type, slice_result, ndim, code) else: - return StridedSliceIter(slice_type, slice_result, ndim, code) + return StridedSliceIter(slice_type, slice_result, ndim, code) class SliceIter(object): - def __init__(self, slice_type, slice_result, ndim, code): + def __init__(self, slice_type, slice_result, ndim, code): self.slice_type = slice_type - self.slice_result = slice_result + self.slice_result = slice_result self.code = code self.ndim = ndim - + class ContigSliceIter(SliceIter): def start_loops(self): code = self.code code.begin_block() - type_decl = self.slice_type.dtype.empty_declaration_code() + type_decl = self.slice_type.dtype.empty_declaration_code() - total_size = ' * '.join("%s.shape[%d]" % (self.slice_result, i) - for i in range(self.ndim)) + total_size = ' * '.join("%s.shape[%d]" % (self.slice_result, i) + for i in range(self.ndim)) code.putln("Py_ssize_t __pyx_temp_extent = %s;" % total_size) code.putln("Py_ssize_t __pyx_temp_idx;") code.putln("%s *__pyx_temp_pointer = (%s *) %s.data;" % ( - type_decl, type_decl, self.slice_result)) + type_decl, type_decl, self.slice_result)) code.putln("for (__pyx_temp_idx = 0; " "__pyx_temp_idx < __pyx_temp_extent; " "__pyx_temp_idx++) {") @@ -443,20 +443,20 @@ class ContigSliceIter(SliceIter): self.code.putln("}") self.code.end_block() - + class StridedSliceIter(SliceIter): def start_loops(self): code = self.code code.begin_block() for i in range(self.ndim): - t = i, self.slice_result, i + t = i, self.slice_result, i code.putln("Py_ssize_t __pyx_temp_extent_%d = %s.shape[%d];" % t) code.putln("Py_ssize_t __pyx_temp_stride_%d = %s.strides[%d];" % t) code.putln("char *__pyx_temp_pointer_%d;" % i) code.putln("Py_ssize_t __pyx_temp_idx_%d;" % i) - code.putln("__pyx_temp_pointer_0 = %s.data;" % self.slice_result) + code.putln("__pyx_temp_pointer_0 = %s.data;" % self.slice_result) for i in range(self.ndim): if i > 0: @@ -486,23 +486,23 @@ def copy_c_or_fortran_cname(memview): return "__pyx_memoryview_copy_slice_%s_%s" % ( memview.specialization_suffix(), c_or_f) - + def get_copy_new_utility(pos, from_memview, to_memview): - if (from_memview.dtype != to_memview.dtype and - not (from_memview.dtype.is_const and from_memview.dtype.const_base_type == to_memview.dtype)): - error(pos, "dtypes must be the same!") - return + if (from_memview.dtype != to_memview.dtype and + not (from_memview.dtype.is_const and from_memview.dtype.const_base_type == to_memview.dtype)): + error(pos, "dtypes must be the same!") + return if len(from_memview.axes) != len(to_memview.axes): - error(pos, "number of dimensions must be same") - return + error(pos, "number of dimensions must be same") + return if not (to_memview.is_c_contig or to_memview.is_f_contig): - error(pos, "to_memview must be c or f contiguous.") - return + error(pos, "to_memview must be c or f contiguous.") + return for (access, packing) in from_memview.axes: if access != 'direct': - error(pos, "cannot handle 'full' or 'ptr' access at this time.") - return + error(pos, "cannot handle 'full' or 'ptr' access at this time.") + return if to_memview.is_c_contig: mode = 'c' @@ -516,14 +516,14 @@ def get_copy_new_utility(pos, from_memview, to_memview): context=dict( context, mode=mode, - dtype_decl=to_memview.dtype.empty_declaration_code(), + dtype_decl=to_memview.dtype.empty_declaration_code(), contig_flag=contig_flag, ndim=to_memview.ndim, func_cname=copy_c_or_fortran_cname(to_memview), dtype_is_object=int(to_memview.dtype.is_pyobject)), requires=[copy_contents_new_utility]) - + def get_axes_specs(env, axes): ''' get_axes_specs(env, axes) -> list of (access, packing) specs for each axis. @@ -569,7 +569,7 @@ def get_axes_specs(env, axes): if entry.name in view_constant_to_access_packing: axes_specs.append(view_constant_to_access_packing[entry.name]) else: - raise CompileError(axis.step.pos, INVALID_ERR) + raise CompileError(axis.step.pos, INVALID_ERR) else: raise CompileError(axis.step.pos, INVALID_ERR) @@ -704,7 +704,7 @@ def validate_axes_specs(positions, specs, is_c_contig, is_f_contig): if access == 'ptr': last_indirect_dimension = idx - for idx, (pos, (access, packing)) in enumerate(zip(positions, specs)): + for idx, (pos, (access, packing)) in enumerate(zip(positions, specs)): if not (access in access_specs and packing in packing_specs): |