array expressions #144

wants to merge 56 commits into


None yet

2 participants


Element-wise array expressions for Cython

markflorisson added some commits May 21, 2012
@markflorisson markflorisson Split buffer and memoryview operations from IndexNode fbae57b
@markflorisson markflorisson Split analyse_types of IndexNode some more 26d7c28
@markflorisson markflorisson Fix copy/copy_fortran return type & tests 1df5077
@markflorisson markflorisson Refactor memoryview dtype validation 98d0d60
@markflorisson markflorisson Fix transpose memoryview type 36f037b
@markflorisson markflorisson Refactor slice assignment and copying 922af21
@markflorisson markflorisson Fix python 3 import 9536b62
@markflorisson markflorisson Start on elementwise expressions 6244394
@markflorisson markflorisson Allow strided memoryviews to be copied to contiguous memoryviews 148c290
@markflorisson markflorisson Specialize for contiguous operands 9b0fc9e
@markflorisson markflorisson Add minivect as submodule 1217a97
@markflorisson markflorisson Choose typedefed type in widest_numeric_type & support tyepdef Cython…
… types in vector expressions
@markflorisson markflorisson Allow arbitrary Cython types compatible with C expressions as vector …
…expression dtypes
@markflorisson markflorisson Support complex numbers in vector expressions 1e53ae2
@markflorisson markflorisson Support objects in vector expressions 451c802
@markflorisson markflorisson Use array funcarg method f4496bc
@markflorisson markflorisson Select specialization at runtime & support broadcasting b8ab0f3
@markflorisson markflorisson Support overlapping memory and broadcasting assignment
    (Avoid recomputation along broadcasting axes)
    (Need some tests)
@markflorisson markflorisson DECREF lhs in slice assignment w/ dtype object c4e72cf
@markflorisson markflorisson Don't choose contig specialization for broadcasting operations known …
…at compile time
@markflorisson markflorisson Remove broadcasting leading dimensions from RHS 48672b0
@markflorisson markflorisson Split vector expression tests & add runtime broadcasting test bc22f9f
@markflorisson markflorisson Select specialization at runtime 32e6671
@markflorisson markflorisson Support tiling specializations aae2d35
@markflorisson markflorisson Let the LHS participate in determining array layout order b13c9e2
@markflorisson markflorisson Implement a code cache 46fe298
@markflorisson markflorisson Support scalar arguments a02e733
@markflorisson markflorisson Support unary operations d640297
@markflorisson markflorisson Support non-slice array expression assignment 62f8783
@markflorisson markflorisson Support non-assignment array expressions ce10532
@markflorisson markflorisson Fix broadcasting & add omitted tests 0653f04
@markflorisson markflorisson Support restrict and const qualifiers for array expression arguments 6a6e9a7
@markflorisson markflorisson Support inner-contiguous specialization selection 94ddebd
@markflorisson markflorisson Map more types to minivect, change qualify to use immutable types e19b8a7
@markflorisson markflorisson Support OpenMP in array expressions d22b3c4
@markflorisson markflorisson Filter duplicate expression arguments ef46dfd
@markflorisson markflorisson Add auto-tuner for square tiling blocksize (+caching) 2182b34
@markflorisson markflorisson Name mangle all variable and function names in the tiling utility code 842952f
@markflorisson markflorisson Make tiling blocksize type signed (avoid compiler warnings about sign…
…ed/unsigned comparison)
@markflorisson markflorisson Divide tiling blocksize by itemsize 5c8409c
@markflorisson markflorisson Auto-tune OpenMP size e413b45
@markflorisson markflorisson Test various binary operators dd455de
@markflorisson markflorisson Support (partial) elementwise function calls b42b157
@markflorisson markflorisson Avoid tiled specializations if all operands are C or F contig d41d090
@markflorisson markflorisson Support vectorized specializations ebc8850
@markflorisson markflorisson Add support for graphviz AST visualisation c596986
@markflorisson markflorisson Use more sensible output filenames for graphviz .dot files 7c5f2f2
@markflorisson markflorisson Only print debug code for expression 1ef2ebd
@markflorisson markflorisson Add variable resolving mixin class adbb911
@markflorisson markflorisson Fix code caching for array expressions 53b77f7
@markflorisson markflorisson Omit type information for graphviz 1719bdb
@markflorisson markflorisson Resolve non-external typedefs for array expressions d146ac7
@markflorisson markflorisson Add some more documentation 4ae867f
@markflorisson markflorisson Create minivect function type for function calls bdaeea5
@scoder scoder referenced this pull request Oct 12, 2012

refactor indexnode2 #137

@robertwb robertwb commented on the diff Oct 16, 2012
+# Print generated minivect code
+_debug = False
+# Generate debug calls from specialized functions
+_context_debug = False
+### Graphviz related things. .dot files are only written when write_graphviz
+### is true.
+graphviz_out_filename_unspecialized = os.path.expanduser("~/")
+graphviz_out_filename = os.path.expanduser("~/")
+write_graphviz = False
+# Macro that should be defined to enable explicit vectorization
+cython_vector_size = "CYTHON_VECTOR_SIZE"
robertwb Oct 16, 2012 Contributor

Meaning of this value?

markflorisson Oct 16, 2012 Contributor

Compiling with -DCYTHON_VECTOR_SIZE=X enables vectorization, with 4 meaning SSE2 (4 floats), and 8 meaning AVX (8 floats).

The new branch is called _array_expressions_rebased, but the py3k build segfaults with a refcount error. I still need to investigate that.

@robertwb robertwb commented on the diff Oct 16, 2012
+ Map Cython types to minitypes.
+ """
+ def map_type(self, type, wrap=False):
+ if type.is_typedef:
+ if type.typedef_is_external:
+ return minitypes.TypeWrapper(type, self.context)
+ else:
+ type = type.resolve()
+ if type.is_memoryviewslice:
+ dtype = self.map_type(type.dtype, wrap=wrap)
+ return minitypes.ArrayType(dtype, len(type.axes),
+ is_c_contig=type.is_c_contig,
+ is_f_contig=type.is_f_contig)
+ elif type.is_float:
robertwb Oct 16, 2012 Contributor

Would these be better expressed as a dictionary?

markflorisson Oct 16, 2012 Contributor

Yes I think that would be nicer.

markflorisson Oct 28, 2012 Contributor

Oh I remember why I didn't do that now. It's because you can create new types, and for instance c_sint_type and c_int_type are the same, but don't compare equal (I'm not sure why?).

robertwb Oct 30, 2012 Contributor

Like typedefs I guess. Does this then assume you know the exact size at compile time? Hmm...

markflorisson Oct 30, 2012 Contributor

No, it needs that only for the LLVM backend (which isn't used by Cython). It uses the external typedef name if the typedef is external, otherwise it uses the actual types to allow code reuse of expressions of equivalent types. I got everything working, except for one pyximport test, but I think that might be my test setup. I'll try a new PR next weekend.

@robertwb robertwb commented on the diff Oct 16, 2012
+ code.putln("else {")
+ code.putln("/* Strided specializations */")
+ self.put_ordered_specializations(code, specializers.StridedSpecializer,
+ specializers.StridedFortranSpecializer)
+ if if_clause != "if":
+ code.putln("}")
+ def generate_result_code(self, code):
+ "Generate a branch and call to each specialization"
+ contig, mixed_contig, c_contig, f_contig = all_c_or_f_contig(self.operands)
+ self.context.original_cython_code = code
+ if_clause = "if"
robertwb Oct 16, 2012 Contributor

unused code


In my (albeit somewhat cursory) perusal of the code, I think it looks good. Is there any way to test that your code is being used (i.e. unnecessary intermediates are not being created) as well as getting the correct output? (Perhaps an object type that prints out arithmetic operations could verify the expected sequence of operations, though this might be too much subject to change). Also, it'd be good to have a link somewhere to your thesis or other high-level overview.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment