Skip to content

Commit 77fc925

Browse files
committed
Implemented array shape equality test.
1 parent fa3efbc commit 77fc925

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

lib/thunk.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ function createThunk(proc) {
3535
var typesig = []
3636
var string_typesig = []
3737
var proc_args = [["array",proc.arrayArgs[0],".shape.slice(", // Slice shape so that we only retain the shape over which we iterate (which gets passed to the cwise operator as SS).
38-
Math.max(0,proc.arrayBlockIndices[i]),proc.arrayBlockIndices[i]<0?(","+proc.arrayBlockIndices[i]+")"):")"].join("")]
38+
Math.max(0,proc.arrayBlockIndices[0]),proc.arrayBlockIndices[0]<0?(","+proc.arrayBlockIndices[0]+")"):")"].join("")]
39+
var shapeLengthConditions = [], shapeConditions = []
40+
// Process array arguments
3941
for(var i=0; i<proc.arrayArgs.length; ++i) {
4042
var j = proc.arrayArgs[i]
4143
vars.push(["t", j, "=array", j, ".dtype,",
@@ -47,10 +49,21 @@ function createThunk(proc) {
4749
proc_args.push("array" + j + ".data")
4850
proc_args.push("array" + j + ".stride")
4951
proc_args.push("array" + j + ".offset|0")
52+
if (i>0) { // Gather conditions to check for shape equality (ignoring block indices)
53+
shapeLengthConditions.push("array" + proc.arrayArgs[0] + ".shape.length===array" + j + ".shape.length+" + (Math.abs(proc.arrayBlockIndices[0])-Math.abs(proc.arrayBlockIndices[i])))
54+
shapeConditions.push("array" + proc.arrayArgs[0] + ".shape[shapeIndex+" + Math.max(0,proc.arrayBlockIndices[0]) + "]===array" + j + ".shape[shapeIndex+" + Math.max(0,proc.arrayBlockIndices[i]) + "]")
55+
}
5056
}
57+
// Check for shape equality
58+
code.push("if (!(" + shapeLengthConditions.join(" && ") + ")) throw new Error('cwise: Generic arrays not supported in combination with blocks!')")
59+
code.push("for(var shapeIndex=array" + proc.arrayArgs[0] + ".shape.length-" + Math.abs(proc.arrayBlockIndices[0]) + "; shapeIndex-->0;) {")
60+
code.push("if (!(" + shapeConditions.join(" && ") + ")) throw new Error('cwise: Generic arrays not supported in combination with blocks!')")
61+
code.push("}")
62+
// Process scalar arguments
5163
for(var i=0; i<proc.scalarArgs.length; ++i) {
5264
proc_args.push("scalar" + proc.scalarArgs[i])
5365
}
66+
// Check for cached function (and if not present, generate it)
5467
vars.push(["type=[", string_typesig.join(","), "].join()"].join(""))
5568
vars.push("proc=CACHED[type]")
5669
code.push("var " + vars.join(","))

0 commit comments

Comments
 (0)