I'm looking for tips on quickly applying the following update to $B$ pairs of $(w_i,x_i)$
$$\text{step}(w_i,x_i)=w_i-a x_i \langle w_i, x_i \rangle$$
Below is a readable but very slow version batchStep which uses MapThread which seems unsupported by FunctionCompile.
What's a good pattern for compiling a function like this, For loops writing into pre-allocated array?
d = 2;
h = ConstantArray[1., d];
B = 200;
a = 1.;
step[w_, x_] = w - a x x . w;
dist = MultinormalDistribution[DiagonalMatrix[h]];
batchStep[W_] :=
With[{X = RandomVariate[dist, B]}, MapThread[step, {W, X}]];
W0 = RandomVariate[NormalDistribution[], {B, d}];
Nest[batchStep, W0, 100]; // Timing
Here's an attempt with FunctionCompile which fails with Cannot find a definition for the function Closure CreateRaw that \ takes an argument with the type PackedArray[Real64, 1:Integer64]
- Is
Forloop that writes into pre-allocated array a correct replacement forMapThreadin this setting? - Is there a way to specify all intermediate arguments to be
Real32(if it matters for performance)
d = 2;
h = ConstantArray[1., d];
B = 200;
a = 1.;
fun = Function[{Typed[W, "Real32"]},
Module[{data, out, i, X, diagSqrt},
diagSqrt = Sqrt[h];
X = diagSqrt*# & /@ RandomVariate[NormalDistribution[], {B, d}];
out = ConstantArray[0., {B, d}];
For[i = 1, i <= B, i += 1,
out[[i]] = step[W[[i]], X[[i]]]];
out
]
];
dec = FunctionDeclaration[fun, Typed[{"Real32"} -> "Real32"]@fun];
cf = FunctionCompile[dec, fun]
MapThread, butRandomVariate, see: https://mathematica.stackexchange.com/q/1124/1871 – xzczd Mar 29 '23 at 03:05MapThreadis only defined for"DenseArray". Check for detail in system files here:SystemFiles\Components\Compile\TypeSystem\Declarations\RectangularArray\DenseArray\Functional\MapThread.m. – Silvia Aug 27 '23 at 06:14