Hello everyone,
We are trying to implement a function similar to numpy.maximum for our project. It is supposed to take in two arrays and return an array containing the element-wise maxima from the arrays.
Here is a simple function that does the work.

proc np_maximum(A:[?d1], B:[?d2]) {
/*
Array element-wise comparison
Returns an array containing the maximum elements from the 2 arrays
Parameters
----------
A: array_like
Input array-1
B: array_like
Input array-2
*/
if(A.domain != B.domain) {
writeln("The arrays' sizes do not match !!");
exit();
}
var res: [1..A.shape(1), 1..A.shape(2)] real;
forall (i,j) in A.domain do {
res(i,j) = if A(i,j) > B(i,j) then A(i,j) else B(i,j);
}
return res;
}

This is the type of operation that Chapel is pretty good at doing without a special routine, where my first thought surprised me by not working (*), and my second thought was to write:

the A.domain != B.domain check will check that the two arrays have the same indices rather than just the same shape. You may want to compare A.shape and B.shape if you're interested in permitting an array with indices 0..<n to compare to one whose indices were 1..n or 1..2*n by 2.

a lot of your code will permit it to only apply to 2D arrays. You could probably make this a rank-neutral routine fairly easily using changes like:

var res: [A.domain] real; — make res share A's domain whatever rank it is

forall idx in A.domain do ... A(idx)...B(idx) — use a single-variable tuple index idx rather than constraining it to be a 2D tuple

Some advantages of taking your approach (in a rank-neutral manner) over mine is that Chapel has a long-term bug with some zippered forall loops in which, if the sizes don't match, the extra iterations are dropped on the floor: zippered forall loops with size mismatches can silently drop iterations on the floor · Issue #11428 · chapel-lang/chapel · GitHub And even if you get an error due to size mismatch, it'll just halt your program in a vague way, whereas your error message is more specific to this case (you could also throw an error if desired).

A few style notes:

writeln(msg) + exit is essentially equivalent to halt(msg) in Chapel. A conditional + halt is equivalent to assert(test, msg)

the do { in your forall loop is fine, but redundant. When you use { ... }, there's no need for do. Or when your forall loop's body is a single statement, you can simply use do (the reason both can be used together is that { ... } is a single statement even though it contains multiple statements.

-Brad

= My first thought was just to write var Max = max(a, b); but this didn't work as I was expecting; it would for most scalar routines, but something about the overloads we've provided for max() don't allow it to. That seems like something we should fix.

Hello Brad,
Thank you for the reply. It was really helpful and I was also able to make the functions rank-independent. And also thank you for the style notes, I'll keep them in mind.

As a follow-up, we merged my PR early last week, so writing var M = max(A, B); will work in this month's 1.26.0 release. Writing a wrapper routine as you did may still be beneficial in terms of generating better error messages if there are size/shape/rank mismatches or the like, but I'm happy that this pattern which I thought should work will soon (and does on main today).