On 24.05.20 18:34, data pulverizer wrote:
I'm getting the error:
```
Error: variable i cannot be read at compile time
Error: template instance
script.runKernelBenchmarks!(Tuple!(DotProduct!float, Gaussian!float,
Polynomial!float, Exponential!float, Log!float, Cauchy!float,
Power!float, Wave!float, Sigmoid!float)) error instantiating
```
When I run ...
```
...
auto runKernelBenchmarks(KS)(KS kernels, long[] n, bool verbose = true)
{
auto tmp = bench(kernels[0], n, verbose);
alias R = typeof(tmp);
R[] results = new R[kernels.length];
results[0] = tmp;
for(size_t i = 1; i < results.length; ++i)
{
results[i] = bench(kernels[i], n, verbose);
}
return results;
}
void main()
{
alias T = float;
auto kernels = tuple(DotProduct!(T)(), Gaussian!(T)(1),
Polynomial!(T)(2.5f, 1),
Exponential!(T)(1), Log!(T)(3), Cauchy!(T)(1),
Power!(T)(2.5f), Wave!(T)(1), Sigmoid!(T)(1,
1));
string[] kernelNames = ["DotProduct", "Gaussian", "Polynomial",
"Exponential", "Log", "Cauchy",
"Power", "Wave", "Sigmoid"];
long[] n = [100L, 500L, 1000L];
auto results = runKernelBenchmarks(kernels, n);
writeln("Results: ", results);
}
```
Since `kernel` is a `Tuple`, you can only access it with compile-time
constant indices.
But your loop variable `i` is not a compile-time constant, it's being
calculated at run time. What's more, it depends on `results.length`
which is also not a compile-time constant. But `results.length` is the
same as `kernels.length`. And being the length of a `Tuple`, that one is
a compile-time constant.
So you can rewrite your loop as a `static foreach` (which is evaluated
during compile-time) using `kernels.length` instead of `results.length`:
static foreach (i; 1 .. kernels.length)
{
results[i] = bench(kernels[i], n, verbose);
}