3

I'm doing a digital filter and using a vector and SIMD instructions to make it faster, but during debugging I noticed that when a new vector was created it only initialized half of the items in the vector, for example when creating a vector with length of 8, only the first 4 items of the vector would have value, the rest would be 0, even with the array that is used to create the vector having 31 items, all different than 0. This is causing the filter use only half of the coefficients and half the data.

enter image description here

The relevant code is below.

var simdLength = Vector<float>.Count;
var leftOver = m_filterSize % simdLength;
    for (int i = 0; i < m_filterSize - leftOver; i += simdLength)
    {
        var filterVector = new Vector<float>(m_filter, i);
        var dataVector = new Vector<float>(data, i);
        filteredValueVector += filterVector * dataVector;
    }

There is some code after to treat the left over but it is not a vector and works fine.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • 1
    It's only debuger view ... your data is still there – Selvin Apr 12 '19 at 13:43
  • No, I just did a test doing the same math not vectorized just after the code on the question with the same values and the answers are different, so it seems that the vectors are really initialized incorrectly – Rafael Fortes Apr 12 '19 at 18:29
  • I've hit this issue as well. I'm running the latest .NET Core 3.1 on 64 bit windows. I'm using `Vector.One`, and the Vector returned contains 8 entries, but only the first 4 elements are set to 1. The rest are set to 0. – Ryan Feb 26 '20 at 04:16
  • 2
    Never mind, it was indeed just the debugger: https://github.com/dotnet/runtime/issues/9688 Calculation difference was due to an error elsewhere in the vectorized code. – Ryan Feb 26 '20 at 07:37
  • Also affects me using .NET 5.0 in VS 2019 – Elaskanator Dec 17 '21 at 06:55
  • Does this answer your question? [Visual Studio's 'watch' incorrectly shows zero for half of the numbers in a Vector](https://stackoverflow.com/questions/51570775/visual-studios-watch-incorrectly-shows-zero-for-half-of-the-numbers-in-a-vect) – Elaskanator Dec 20 '21 at 00:04

1 Answers1

4

This is a debugger bug:

int capacity = Vector<float>.Count;
float[] testVals = Enumerable.Range(0, capacity).Select(i => (float)i).ToArray();
Vector<float> testV = new(testVals);
float[] returnedVals = Enumerable.Range(0, capacity).Select(i => testV[i]).ToArray();

enter image description here

Interestingly, string.Join always formats your vector to its own choosing:

Vector<int> powersOfTwo = new Vector<int>(Enumerable.Range(0, Vector<int>.Count).Select(i => 1 << i).ToArray());
string powersOfTwoString = string.Join("abc", powersOfTwo);

enter image description here

Elaskanator
  • 1,135
  • 10
  • 28