Esempio n. 1
0
        public static void MatrixTimesSource(ReadOnlySpan <float> matrix, ReadOnlySpan <int> rgposSrc, ReadOnlySpan <float> sourceValues,
                                             int posMin, int iposMin, int iposLimit, Span <float> destination, int stride)
        {
            Contracts.Assert(iposMin >= 0);
            Contracts.Assert(iposMin <= iposLimit);
            Contracts.Assert(iposLimit <= rgposSrc.Length);
            Contracts.AssertNonEmpty(matrix);
            Contracts.AssertNonEmpty(sourceValues);
            Contracts.AssertNonEmpty(destination);
            Contracts.AssertNonEmpty(rgposSrc);
            Contracts.Assert(stride > 0);
            Contracts.Assert(matrix.Length == destination.Length * sourceValues.Length);

            if (iposMin >= iposLimit)
            {
                destination.Clear();
                return;
            }

            Contracts.AssertNonEmpty(rgposSrc);
            Contracts.Assert(stride >= 0);

            if (Avx.IsSupported)
            {
                Contracts.Assert(stride <= destination.Length);
                AvxIntrinsics.MatMulP(matrix, rgposSrc, sourceValues, posMin, iposMin, iposLimit, destination, stride, sourceValues.Length);
            }
            else if (Sse.IsSupported)
            {
                Contracts.Assert(stride <= destination.Length);
                SseIntrinsics.MatMulP(matrix, rgposSrc, sourceValues, posMin, iposMin, iposLimit, destination, stride, sourceValues.Length);
            }
            else
            {
                Contracts.Assert(stride <= destination.Length);
                for (int i = 0; i < stride; i++)
                {
                    float dotProduct = 0;
                    for (int j = iposMin; j < iposLimit; j++)
                    {
                        int col = rgposSrc[j] - posMin;
                        dotProduct += matrix[i * sourceValues.Length + col] * sourceValues[col];
                    }
                    destination[i] = dotProduct;
                }
            }
        }
Esempio n. 2
0
        public static void MatrixTimesSource(AlignedArray matrix, ReadOnlySpan <int> rgposSrc, AlignedArray sourceValues,
                                             int posMin, int iposMin, int iposLimit, AlignedArray destination, int stride)
        {
            Contracts.AssertValue(rgposSrc);
            Contracts.Assert(iposMin >= 0);
            Contracts.Assert(iposMin <= iposLimit);
            Contracts.Assert(iposLimit <= rgposSrc.Length);
            Contracts.Assert(matrix.Size == destination.Size * sourceValues.Size);

            if (iposMin >= iposLimit)
            {
                destination.ZeroItems();
                return;
            }

            Contracts.AssertNonEmpty(rgposSrc);
            Contracts.Assert(stride >= 0);

            if (Avx.IsSupported)
            {
                Contracts.Assert(stride <= destination.Size);
                AvxIntrinsics.MatMulP(matrix, rgposSrc, sourceValues, posMin, iposMin, iposLimit, destination, stride, sourceValues.Size);
            }
            else if (Sse.IsSupported)
            {
                Contracts.Assert(stride <= destination.Size);
                SseIntrinsics.MatMulP(matrix, rgposSrc, sourceValues, posMin, iposMin, iposLimit, destination, stride, sourceValues.Size);
            }
            else
            {
                Contracts.Assert(stride <= destination.Size);
                for (int i = 0; i < stride; i++)
                {
                    float dotProduct = 0;
                    for (int j = iposMin; j < iposLimit; j++)
                    {
                        int col = rgposSrc[j] - posMin;
                        dotProduct += matrix[i * sourceValues.Size + col] * sourceValues[col];
                    }
                    destination[i] = dotProduct;
                }
            }
        }
        public static void MatTimesSrc(AlignedArray mat, int[] rgposSrc, AlignedArray srcValues,
                                       int posMin, int iposMin, int iposLim, AlignedArray dst, int crun)
        {
            Contracts.AssertValue(rgposSrc);
            Contracts.Assert(iposMin >= 0);
            Contracts.Assert(iposMin <= iposLim);
            Contracts.Assert(iposLim <= rgposSrc.Length);
            Contracts.Assert(mat.Size == dst.Size * srcValues.Size);

            if (iposMin >= iposLim)
            {
                dst.ZeroItems();
                return;
            }

            Contracts.AssertNonEmpty(rgposSrc);
            Contracts.Assert(crun >= 0);

            if (Avx.IsSupported)
            {
                Contracts.Assert(crun <= dst.Size);
                AvxIntrinsics.MatMulP(mat, rgposSrc, srcValues, posMin, iposMin, iposLim, dst, crun, srcValues.Size);
            }
            else if (Sse.IsSupported)
            {
                Contracts.Assert(crun <= dst.Size);
                SseIntrinsics.MatMulP(mat, rgposSrc, srcValues, posMin, iposMin, iposLim, dst, crun, srcValues.Size);
            }
            else
            {
                Contracts.Assert(crun <= dst.Size);
                for (int i = 0; i < crun; i++)
                {
                    float dotProduct = 0;
                    for (int j = iposMin; j < iposLim; j++)
                    {
                        int col = rgposSrc[j] - posMin;
                        dotProduct += mat[i * srcValues.Size + col] * srcValues[col];
                    }
                    dst[i] = dotProduct;
                }
            }
        }