3
$\begingroup$

Recently, when using Mathematica to process some triangle meshes, I noticed an unusual slowdown when processing a large mesh.

(* import the vertex coordinates and triangle-element connectivity *)
v = Partition[BinaryReadList["/path/to/vertices.bin", "Real32"], 3];
e = Partition[BinaryReadList["/path/to/triangles.bin", "UnsignedInteger64"], 3] + 1;

(* generate random numbers for testing *)
n = 20000000;
v = Partition[RandomReal[1, 3 n], 3];
e = Partition[RandomInteger[n - 1, 3 n], 3] + 1;

(* compute the centroids of the first 250 triangles *)
centroids = Table[Mean[v[[e[[i]]]]], {i, 1, 250}];

(* other other steps in the analysis *)
...

For some reason, that centroid calculation seemed to take a really long time. If I measure with AbsoluteTiming[] I see

Table[Mean[v[[e[[i]]]]], {i, 1, 250}]; // AbsoluteTiming
35.5656

Curiously, if I only want to compute the first 249 centroids, the calculation finishes quickly

Table[Mean[v[[e[[i]]]]], {i, 1, 249}]; // AbsoluteTiming
0.001143

So, it must be the case that element 250 is unusual in some way, causing the slowdown. How long does it take to calculate that centroid in isolation?

Mean[v[[e[[250]]]]] // AbsoluteTiming
0.000039

Hmm, no, that finishes instantly as well. What could be causing this 30,000x slowdown?


For reference, the sizes of the relevant mesh data are given below:

Dimensions[v]
{210848942, 3}
--------
Dimensions[e]
{117676276, 3}

Times were measured on an Ubuntu 22.04 machine running Mathematica 13.3.0. I have also just updated to Mathematica 14.0.0, and the problem still persists.

$\endgroup$
14
  • 1
    $\begingroup$ It is hard to tell what is going on without the data. $\endgroup$ Commented Jan 11 at 22:03
  • 2
    $\begingroup$ With Table you can never know, because it may run some jit compilation at high trip count. The critical number of elements for switching that behavior is 250. (At least I think I saw this number pop up somewhere.) But this is a task that is quicker done without Table by just Total[Partition[(1/3 v)[[Flatten[e]]], 3], {2}]. $\endgroup$ Commented Jan 11 at 22:15
  • 1
    $\begingroup$ That said, 30 seconds is way above what anybody would expect from jit compilation. $\endgroup$ Commented Jan 11 at 22:54
  • 4
    $\begingroup$ You might want to send full code to replicate this to Tech Support. $\endgroup$ Commented Jan 12 at 0:49
  • 3
    $\begingroup$ Does this answer your question? Why is mathematica so slow when tables reach a certain length @VaclavKotesovec, out of the three, you just missed the critical one, namely "TableCompileLength" -> Infinity, which indeed helps! :) $\endgroup$
    – Domen
    Commented Jan 12 at 12:38

0

Browse other questions tagged or ask your own question.