WVM hangs or crashes on empty array when compiled Listable

10

2

Bug introduced in 12.0 and persisting through 12.0 or later


When I run the following code, the kernel hangs indefinitely:

$Version
(*  "12.0.0 for Mac OS X x86 (64-bit) (April 7, 2019)"  *)

cf = Compile[{{x, _Integer, 1}},
   Length@x,
   RuntimeAttributes -> {Listable}
   ];

cf[{}]   (* Works fine *)
(*  0  *)

cf[{{1}, {2, 3, 4}}]  (* Listability seems ok *)
(*  {1, 3}  *)

cf[{{}}] (* WARNING: Causes hang/crash *)

With Parallelization -> True, the kernel crashes after a couple of seconds. Without Parallelization, it hangs until I interrupt the kernel, which causes a crash.

Update: The following runs without crashing and produces the right result. I'm not sure how to tell if the program ran the uncompiled or compiled code. (I suppose it ran the compiled code, but I don't know how to verify that.)

cf[{{2, 3}, {}}]

CompiledFunction::cfta: Argument {{2,3},{}} at position 1 should be a rank 1 tensor of machine-size integers.

(*  {2, 0}  *)

I'm not very familiar with the compiler, so I'm wondering if there's some issue I'm ignorant of, such as some kind of type mismatch. Otherwise, I suppose it's a bug. It's rather inconvenient, as I was working with programmatically produced arrays that are sometimes empty.

If my program is correct and it is a bug, does anyone have a workaround (other than manually mapping/threading the compiled function over the list of arrays)?

Michael E2

Posted 2019-07-12T17:49:15.883

Reputation: 190 928

I also stumbled upon this issue several times now. Usually, I try to circumvent it by assurring that the lists supplied to CompiledFunction are never empty, e.g., by appending dummy 0-entries... Sometimes, this come at almost no additiona price. The newest example is here.

– Henrik Schumacher – 2019-07-12T18:12:17.473

1@HenrikSchumacher Thanks. Your comment led me to this: I never realized before that an empty array {} cannot be packed, not even with Most@Developer`ToPackedArray[{0}, Integer] (like Most[{0}] inside Compile[]). I'll see what WRI has to say this coming week, assuming I get an enlightening response. – Michael E2 – 2019-07-13T16:05:28.910

Answers

2

This was passed on to developers at WRI as issue, [CASE:4280135]. No workaround was suggested.

Michael E2

Posted 2019-07-12T17:49:15.883

Reputation: 190 928