1

Suppose, I have a nonlinear map, like this:

m1 = Compile[{{n, _Real}, {k2, _Real}, {k3, _Real}, {x, _Real, 1}},
{x[[1]] Cos[2. \[Pi] (0.663 + n)] + 10. x[[2]] Sin[2. \[Pi] (0.663 + n)], 
x[[2]] Cos[2. \[Pi] (0.663 + n)] - 0.1 x[[1]] Sin[2. \[Pi] (0.663 + n)] - 
k2 (x[[1]] Cos[2. \[Pi] (0.663 + n)] + 10. x[[2]] Sin[2. \[Pi] (0.663 + n)])^2 - 
k3 (x[[1]] Cos[2. \[Pi] (0.663 + n)] + 10. x[[2]] Sin[2. \[Pi] (0.663 + n)])^3}, 
CompilationTarget -> "C", RuntimeAttributes -> {Listable}, Parallelization -> True]

I turned On["Packing"].

Just after evaluation I see warnings from MMA about "Unpackung array with dimensions {21}", then other "dimensions", then "Unpacking array with dimensions {1} in call to Flatten", etc. What does this mean?

I use this m1 inside FoldList. The problem is that this FoldList function has great memory consumption. So I decided it might be related to using unpacked arrays and used On["Packing"] to check.

The main function is:

data = RandomVariate[BinormalDistribution[{Sqrt[10^-5 10.], Sqrt[10^-5/10.]}, 0], 100000];

With[{n = 0.004, k2 = 0.25, k3 = -1., t = 10000, nt = 1000}, Block[{mo = Transpose[{PadLeft[Range[0., n, n/t], t + nt + 1, 0.], PadRight[Range[0., k2, k2/nt], t + nt + 1, k2], PadRight[Range[0., k3, k3/nt], t + nt + 1, k3]}], rp}, rp = Reap[ FoldList[(Sow[Count[UnitStep[(#[[;; , 1]] - 0.05)], 1]]; m1[#2[[1]], #2[[2]], #2[[3]], Pick[#, UnitStep[(#[[;; , 1]] - 0.05) (#[[;; , 1]] + 0.05)], 0]]) &, data, mo]][[2, 1]]]]

Running this function also gives warnings about unpacking. I'm not sure unpacking affects memory consumption, but my main goal is to optimize this function in terms of time and memory. Now what I see in Windows task manager is ~10GB memory of MMA when this function finished.

ADD

OK, more simple question. Why this does not unpack:

Compile[{{x,_Real}},{{Cos@x, Sin@x},{-x^2,x}}]

and this one displays warning about unpacking:

Compile[{{x,_Real}},{{Cos@x, Sin@x},{-Sin@x,Cos@x}}].

I do not understand, for me these two Compile are the same, what is the difference?

InputForm for the first example gives clean code, but for the second one it contains

Function[{x}, Block[{Compile`$10}, Compile`$10 = Cos[x]; {{Compile`$10, x}, 
{-Sin[x], Compile`$10}}]], Evaluate

Why Cos[x] is inside Block and Function, why not Sin[x] as well?

macros
  • 99
  • 4
  • Related: https://mathematica.stackexchange.com/q/3496, https://mathematica.stackexchange.com/q/5258 – Michael E2 May 03 '21 at 13:48
  • @MichaelE2, OK, and...? What to do in my specific case? – macros May 03 '21 at 14:47
  • Analyze your code? :) Seriously, "Related: link" comments might, or not, help solve the problem. While they might help the asker, especially if they didn't know the related knowledge, another function they serve to help those with a similar problem to track down relevant ideas through the "Linked Question" list on the right. In your case, they seem to answer the points you raised, "What does this mean?" and "I'm not sure unpacking affects memory consumption." Given that and that I don't have time to analyze your code myself at the moment (sorry about that), I hoped they would help you. – Michael E2 May 03 '21 at 15:10
  • There are many site users who probably can answer your main question, and with luck, someone will. I was just trying to add the little bit I could, since I knew of these Q&A. – Michael E2 May 03 '21 at 15:12
  • The unpacking messages generated during the compilation of m1 are not so relevant. They occur during some processing for optimizing expressions, generating and compiling C code etc. What happens when running the already compiled function is more important. – ilian May 04 '21 at 04:33
  • @ilian, thanks for comment. What about my main part of the question, do you have ideas/advices? – macros May 04 '21 at 05:11
  • Count definitely unpacks, perhaps some improvement can be obtained by replacing Count[UnitStep[...], 1] with Total[UnitStep[...]]] – ilian May 04 '21 at 20:33
  • @ilian, thanks, I will try. – macros May 05 '21 at 00:57

0 Answers0