"use strict" var uniq = require("uniq") // This function generates very simple loops analogous to how you typically traverse arrays (the outermost loop corresponds to the slowest changing index, the innermost loop to the fastest changing index) // TODO: If two arrays have the same strides (and offsets) there is potential for decreasing the number of "pointers" and related variables. The drawback is that the type signature would become more specific and that there would thus be less potential for caching, but it might still be worth it, especially when dealing with large numbers of arguments. function innerFill(order, proc, body) { var dimension = order.length , nargs = proc.arrayArgs.length , has_index = proc.indexArgs.length>0 , code = [] , vars = [] , idx=0, pidx=0, i, j for(i=0; i 0) { code.push("var " + vars.join(",")) } //Scan loop for(i=dimension-1; i>=0; --i) { // Start at largest stride and work your way inwards idx = order[i] code.push(["for(i",i,"=0;i",i," 0) { code.push(["index[",pidx,"]-=s",pidx].join("")) } code.push(["++index[",idx,"]"].join("")) } code.push("}") } return code.join("\n") } // Generate "outer" loops that loop over blocks of data, applying "inner" loops to the blocks by manipulating the local variables in such a way that the inner loop only "sees" the current block. // TODO: If this is used, then the previous declaration (done by generateCwiseOp) of s* is essentially unnecessary. // I believe the s* are not used elsewhere (in particular, I don't think they're used in the pre/post parts and "shape" is defined independently), so it would be possible to make defining the s* dependent on what loop method is being used. function outerFill(matched, order, proc, body) { var dimension = order.length , nargs = proc.arrayArgs.length , blockSize = proc.blockSize , has_index = proc.indexArgs.length > 0 , code = [] for(var i=0; i0;){"].join("")) // Iterate back to front code.push(["if(j",i,"<",blockSize,"){"].join("")) // Either decrease j by blockSize (s = blockSize), or set it to zero (after setting s = j). code.push(["s",order[i],"=j",i].join("")) code.push(["j",i,"=0"].join("")) code.push(["}else{s",order[i],"=",blockSize].join("")) code.push(["j",i,"-=",blockSize,"}"].join("")) if(has_index) { code.push(["index[",order[i],"]=j",i].join("")) } } for(var i=0; i 0) { allEqual = allEqual && summary[i] === summary[i-1] } } if(allEqual) { return summary[0] } return summary.join("") } //Generates a cwise operator function generateCWiseOp(proc, typesig) { //Compute dimension // Arrays get put first in typesig, and there are two entries per array (dtype and order), so this gets the number of dimensions in the first array arg. var dimension = (typesig[1].length - Math.abs(proc.arrayBlockIndices[0]))|0 var orders = new Array(proc.arrayArgs.length) var dtypes = new Array(proc.arrayArgs.length) for(var i=0; i 0) { vars.push("shape=SS.slice(0)") // Makes the shape over which we iterate available to the user defined functions (so you can use width/height for example) } if(proc.indexArgs.length > 0) { // Prepare an array to keep track of the (logical) indices, initialized to dimension zeroes. var zeros = new Array(dimension) for(var i=0; i 0) { code.push("var " + vars.join(",")) } for(var i=0; i 3) { code.push(processBlock(proc.pre, proc, dtypes)) } //Process body var body = processBlock(proc.body, proc, dtypes) var matched = countMatches(loopOrders) if(matched < dimension) { code.push(outerFill(matched, loopOrders[0], proc, body)) // TODO: Rather than passing loopOrders[0], it might be interesting to look at passing an order that represents the majority of the arguments for example. } else { code.push(innerFill(loopOrders[0], proc, body)) } //Inline epilog if(proc.post.body.length > 3) { code.push(processBlock(proc.post, proc, dtypes)) } if(proc.debug) { console.log("-----Generated cwise routine for ", typesig, ":\n" + code.join("\n") + "\n----------") } var loopName = [(proc.funcName||"unnamed"), "_cwise_loop_", orders[0].join("s"),"m",matched,typeSummary(dtypes)].join("") var f = new Function(["function ",loopName,"(", arglist.join(","),"){", code.join("\n"),"} return ", loopName].join("")) return f() } module.exports = generateCWiseOp