Why is Vector <T> .Count static?

I am trying to use System.Numerics.Vector<T> ( documentation ).

I wrote a simple unit test:

 var v = new System.Numerics.Vector<double>(new double[] { 12, 13, 14 }); Assert.AreEqual(3, v.Count); 

But this gave me a build error:

User 'Vector.Count' cannot be accessed with reference to an instance; qualify it instead of type name

To my surprise, Vector<T>.Count is static.

So I tried:

 var v = new System.Numerics.Vector<double>(new double[] { 12, 13, 14 }); Assert.AreEqual(3, Vector<double>.Count); 

Now the code builds, but the unit test fails:

Error Assert.AreEqual. Expected: <3>. Actual :. & L; 2>

What's happening?


The research I discovered:

 Assert.AreEqual(2, Vector<double>.Count); Assert.AreEqual(4, Vector<float>.Count); Assert.AreEqual(4, Vector<int>.Count); Assert.AreEqual(2, Vector<long>.Count); 
+5
source share
2 answers

The documentation suggests that this is by design:

The instance instance counter is fixed, but its upper limit is processor dependent.

Its purpose is to allow vectorization operations using hardware capabilities, and therefore its capacity is tied to your processor architecture.

+4
source

A vector can be a somewhat confusing type. This is a fixed assembly with a predefined length. It is fixed because its length is always == Vector<T>.Count . So if you do this:

 var v = new Vector<double>(new double[] { 12, 13, 14 }); Console.WriteLine(v); 

result:...:

 <12, 13> 

It simply lowers all values ​​compared to Vector<double>.Count , which is equal to 2. The trick is that Vector<T>.Count can vary depending on the processor architecture.

This is actually a rather primitive low level, as the description says:

It is a single vector of the specified numerical type, which is suitable for low-level optimization of parallel algorithms.

+2
source

Source: https://habr.com/ru/post/1242791/


All Articles