Hello,
When users write math code, they expect [2, 3, 4] that the code
like
------
import mir.ndslice; //[1]
...
foreach (i; 0..m)
{
foreach (j; 0..n)
{
// use matrix1[i, j], matrix2[i, j], matrix3[i, j]
}
}
------
will be vectorized like in Fortran and other math languages.
There are different kinds of engineers and good math engineer may
not have engineering education. They may be economists,
physicist, geologist and etc. The may not be programmers or they
may not be D programmers. They do not want to learn special loop
free programming idioms. They just want their code be fast with
ordinary loops.
In D the example above can not be vectorised.
The reason is that `matrixX[i, j]` is opIndex call, opIndex is a
function. It can be inlined. But optimizers can not split its
body and move half of opIndex computations out of the inner loop,
which it required for vectorization.
Optimizers should do
------------
foreach (i; 0..m)
{
auto __tempV1 = matrix1[i];
auto __tempV2 = matrix2[i];
auto __tempV3 = matrix3[i];
foreach (j; 0..n)
{
// use __tempV1[j], __tempV2[j], __tempV3[j]
}
}
------------
As was said optimizsers can not split opIndex body because it is
function (inlined or not inlined does not matter).
Walter, Andrei, and D community please help to make D simple for
math world!
I do not know what language changes we should add. I only know
how it should look for compiler:
------
import mir.ndslice;
...
foreach (i; 0..m)
{
foreach (j; 0..n)
{
// matrixX[i, j] should be transformed to
// matrix.ptr[matrix.stride * i + j]
// it is simplified version, ndslice has more complex API
}
}
------
Looks like Rust macro system can do something similar.
What can I do to make it happen?
Ilya
[1] https://github.com/libmir/mir-algorithm
[2]
https://gist.github.com/dextorious/d987865a7da147645ae34cc17a87729d
[3]
https://gist.github.com/dextorious/9a65a20e353542d6fb3a8d45c515bc18
[4] https://gitter.im/libmir/public