Esempio n. 1
0
 private static float DotProductSparse(Span <float> a, Span <float> b, Span <int> indices)
 {
     if (Avx.IsSupported)
     {
         return(AvxIntrinsics.DotSU(a, b, indices));
     }
     else if (Sse.IsSupported)
     {
         return(SseIntrinsics.DotSU(a, b, indices));
     }
     else
     {
         float result = 0;
         for (int i = 0; i < indices.Length; i++)
         {
             int index = indices[i];
             result += a[index] * b[i];
         }
         return(result);
     }
 }
Esempio n. 2
0
 private static void SdcaL1UpdateSparse(float primalUpdate, Span <float> src, Span <int> indices, float threshold, Span <float> v, Span <float> w)
 {
     if (Avx.IsSupported)
     {
         AvxIntrinsics.SdcaL1UpdateSU(primalUpdate, src, indices, threshold, v, w);
     }
     else if (Sse.IsSupported)
     {
         SseIntrinsics.SdcaL1UpdateSU(primalUpdate, src, indices, threshold, v, w);
     }
     else
     {
         for (int i = 0; i < indices.Length; i++)
         {
             int index = indices[i];
             v[index] += src[i] * primalUpdate;
             float value = v[index];
             w[index] = Math.Abs(value) > threshold ? (value > 0 ? value - threshold : value + threshold) : 0;
         }
     }
 }
Esempio n. 3
0
        public static float SumAbs(float mean, ReadOnlySpan <float> source)
        {
            Contracts.AssertNonEmpty(source);

            if (Avx.IsSupported)
            {
                return((mean == 0) ? AvxIntrinsics.SumAbsU(source) : AvxIntrinsics.SumAbsDiffU(mean, source));
            }
            else if (Sse.IsSupported)
            {
                return((mean == 0) ? SseIntrinsics.SumAbsU(source) : SseIntrinsics.SumAbsDiffU(mean, source));
            }
            else
            {
                float sum = 0;
                for (int i = 0; i < source.Length; i++)
                {
                    sum += Math.Abs(source[i] - mean);
                }
                return(sum);
            }
        }
Esempio n. 4
0
        public static float SumSq(float mean, ReadOnlySpan <float> source)
        {
            Contracts.AssertNonEmpty(source);

            if (Avx.IsSupported)
            {
                return((mean == 0) ? AvxIntrinsics.SumSqU(source) : AvxIntrinsics.SumSqDiffU(mean, source));
            }
            else if (Sse.IsSupported)
            {
                return((mean == 0) ? SseIntrinsics.SumSqU(source) : SseIntrinsics.SumSqDiffU(mean, source));
            }
            else
            {
                float result = 0;
                for (int i = 0; i < source.Length; i++)
                {
                    result += (source[i] - mean) * (source[i] - mean);
                }
                return(result);
            }
        }
Esempio n. 5
0
        public static float SumSq(ReadOnlySpan <float> source)
        {
            Contracts.AssertNonEmpty(source);

            if (Avx.IsSupported)
            {
                return(AvxIntrinsics.SumSqU(source));
            }
            else if (Sse.IsSupported)
            {
                return(SseIntrinsics.SumSqU(source));
            }
            else
            {
                float result = 0;
                for (int i = 0; i < source.Length; i++)
                {
                    result += source[i] * source[i];
                }
                return(result);
            }
        }
        public static float SumAbs(ReadOnlySpan <float> src)
        {
            Contracts.AssertNonEmpty(src);

            if (Avx.IsSupported)
            {
                return(AvxIntrinsics.SumAbsU(src));
            }
            else if (Sse.IsSupported)
            {
                return(SseIntrinsics.SumAbsU(src));
            }
            else
            {
                float sum = 0;
                for (int i = 0; i < src.Length; i++)
                {
                    sum += Math.Abs(src[i]);
                }
                return(sum);
            }
        }
Esempio n. 7
0
 private static float MaxAbsDiff(float mean, Span <float> src)
 {
     if (Avx.IsSupported)
     {
         return(AvxIntrinsics.MaxAbsDiffU(mean, src));
     }
     else if (Sse.IsSupported)
     {
         return(SseIntrinsics.MaxAbsDiffU(mean, src));
     }
     else
     {
         float max = 0;
         for (int i = 0; i < src.Length; i++)
         {
             float abs = Math.Abs(src[i] - mean);
             if (abs > max)
             {
                 max = abs;
             }
         }
         return(max);
     }
 }
Esempio n. 8
0
        public static void Add(ReadOnlySpan <float> source, Span <float> destination, int count)
        {
            Contracts.AssertNonEmpty(source);
            Contracts.AssertNonEmpty(destination);
            Contracts.Assert(count > 0);
            Contracts.Assert(count <= source.Length);
            Contracts.Assert(count <= destination.Length);

            if (Avx.IsSupported)
            {
                AvxIntrinsics.AddU(source, destination, count);
            }
            else if (Sse.IsSupported)
            {
                SseIntrinsics.AddU(source, destination, count);
            }
            else
            {
                for (int i = 0; i < count; i++)
                {
                    destination[i] += source[i];
                }
            }
        }
        public static void Add(ReadOnlySpan <float> src, Span <float> dst, int count)
        {
            Contracts.AssertNonEmpty(src);
            Contracts.AssertNonEmpty(dst);
            Contracts.Assert(count > 0);
            Contracts.Assert(count <= src.Length);
            Contracts.Assert(count <= dst.Length);

            if (Avx.IsSupported)
            {
                AvxIntrinsics.AddU(src, dst, count);
            }
            else if (Sse.IsSupported)
            {
                SseIntrinsics.AddU(src, dst, count);
            }
            else
            {
                for (int i = 0; i < count; i++)
                {
                    dst[i] += src[i];
                }
            }
        }
Esempio n. 10
0
        public static void MatTimesSrc(bool tran, bool add, AlignedArray mat, AlignedArray src, AlignedArray dst, int crun)
        {
            Contracts.Assert(mat.Size == dst.Size * src.Size);
            Contracts.Assert(crun >= 0);

            if (Avx.IsSupported)
            {
                if (!tran)
                {
                    Contracts.Assert(crun <= dst.Size);
                    AvxIntrinsics.MatMulX(add, mat, src, dst, crun, src.Size);
                }
                else
                {
                    Contracts.Assert(crun <= src.Size);
                    AvxIntrinsics.MatMulTranX(add, mat, src, dst, dst.Size, crun);
                }
            }
            else if (Sse.IsSupported)
            {
                if (!tran)
                {
                    Contracts.Assert(crun <= dst.Size);
                    SseIntrinsics.MatMulA(add, mat, src, dst, crun, src.Size);
                }
                else
                {
                    Contracts.Assert(crun <= src.Size);
                    SseIntrinsics.MatMulTranA(add, mat, src, dst, dst.Size, crun);
                }
            }
            else
            {
                if (!tran)
                {
                    Contracts.Assert(crun <= dst.Size);
                    for (int i = 0; i < crun; i++)
                    {
                        float dotProduct = 0;
                        for (int j = 0; j < src.Size; j++)
                        {
                            dotProduct += mat[i * src.Size + j] * src[j];
                        }

                        if (add)
                        {
                            dst[i] += dotProduct;
                        }
                        else
                        {
                            dst[i] = dotProduct;
                        }
                    }
                }
                else
                {
                    Contracts.Assert(crun <= src.Size);
                    for (int i = 0; i < dst.Size; i++)
                    {
                        float dotProduct = 0;
                        for (int j = 0; j < crun; j++)
                        {
                            dotProduct += mat[j * src.Size + i] * src[j];
                        }

                        if (add)
                        {
                            dst[i] += dotProduct;
                        }
                        else
                        {
                            dst[i] = dotProduct;
                        }
                    }
                }
            }
        }
Esempio n. 11
0
        public static void MatTimesSrc(bool tran, bool add, 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)
            {
                if (!add)
                {
                    dst.ZeroItems();
                }
                return;
            }

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

            if (Avx.IsSupported)
            {
                if (!tran)
                {
                    Contracts.Assert(crun <= dst.Size);
                    AvxIntrinsics.MatMulPX(add, mat, rgposSrc, srcValues, posMin, iposMin, iposLim, dst, crun, srcValues.Size);
                }
                else
                {
                    Contracts.Assert(crun <= srcValues.Size);
                    AvxIntrinsics.MatMulTranPX(add, mat, rgposSrc, srcValues, posMin, iposMin, iposLim, dst, dst.Size);
                }
            }
            else if (Sse.IsSupported)
            {
                if (!tran)
                {
                    Contracts.Assert(crun <= dst.Size);
                    SseIntrinsics.MatMulPA(add, mat, rgposSrc, srcValues, posMin, iposMin, iposLim, dst, crun, srcValues.Size);
                }
                else
                {
                    Contracts.Assert(crun <= srcValues.Size);
                    SseIntrinsics.MatMulTranPA(add, mat, rgposSrc, srcValues, posMin, iposMin, iposLim, dst, dst.Size);
                }
            }
            else
            {
                if (!tran)
                {
                    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];
                        }

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

                        if (add)
                        {
                            dst[i] += dotProduct;
                        }
                        else
                        {
                            dst[i] = dotProduct;
                        }
                    }
                }
            }
        }
Esempio n. 12
0
        public static void MatrixTimesSource(bool transpose, AlignedArray matrix, AlignedArray source, AlignedArray destination, int stride)
        {
            Contracts.Assert(matrix.Size == destination.Size * source.Size);
            Contracts.Assert(stride >= 0);

            if (Avx.IsSupported)
            {
                if (!transpose)
                {
                    Contracts.Assert(stride <= destination.Size);
                    AvxIntrinsics.MatMul(matrix, source, destination, stride, source.Size);
                }
                else
                {
                    Contracts.Assert(stride <= source.Size);
                    AvxIntrinsics.MatMulTran(matrix, source, destination, destination.Size, stride);
                }
            }
            else if (Sse.IsSupported)
            {
                if (!transpose)
                {
                    Contracts.Assert(stride <= destination.Size);
                    SseIntrinsics.MatMul(matrix, source, destination, stride, source.Size);
                }
                else
                {
                    Contracts.Assert(stride <= source.Size);
                    SseIntrinsics.MatMulTran(matrix, source, destination, destination.Size, stride);
                }
            }
            else
            {
                if (!transpose)
                {
                    Contracts.Assert(stride <= destination.Size);
                    for (int i = 0; i < stride; i++)
                    {
                        float dotProduct = 0;
                        for (int j = 0; j < source.Size; j++)
                        {
                            dotProduct += matrix[i * source.Size + j] * source[j];
                        }

                        destination[i] = dotProduct;
                    }
                }
                else
                {
                    Contracts.Assert(stride <= source.Size);
                    for (int i = 0; i < destination.Size; i++)
                    {
                        float dotProduct = 0;
                        for (int j = 0; j < stride; j++)
                        {
                            dotProduct += matrix[j * source.Size + i] * source[j];
                        }

                        destination[i] = dotProduct;
                    }
                }
            }
        }
Esempio n. 13
0
        public static void MatrixTimesSource(bool transpose, ReadOnlySpan <float> matrix, ReadOnlySpan <float> source, Span <float> destination, int stride)
        {
            Contracts.AssertNonEmpty(matrix);
            Contracts.AssertNonEmpty(source);
            Contracts.AssertNonEmpty(destination);
            Contracts.Assert(matrix.Length == destination.Length * source.Length);
            Contracts.Assert(stride >= 0);

            if (!transpose)
            {
                if (Avx.IsSupported && source.Length >= 8)
                {
                    Contracts.Assert(stride <= destination.Length);
                    AvxIntrinsics.MatMul(matrix, source, destination, stride, source.Length);
                }
                else if (Sse.IsSupported && source.Length >= 4)
                {
                    Contracts.Assert(stride <= destination.Length);
                    SseIntrinsics.MatMul(matrix, source, destination, stride, source.Length);
                }
                else
                {
                    Contracts.Assert(stride <= destination.Length);
                    for (int i = 0; i < stride; i++)
                    {
                        float dotProduct = 0;
                        for (int j = 0; j < source.Length; j++)
                        {
                            dotProduct += matrix[i * source.Length + j] * source[j];
                        }

                        destination[i] = dotProduct;
                    }
                }
            }
            else
            {
                if (Avx.IsSupported && destination.Length >= 8)
                {
                    Contracts.Assert(stride <= source.Length);
                    AvxIntrinsics.MatMulTran(matrix, source, destination, destination.Length, stride);
                }
                else if (Sse.IsSupported && destination.Length >= 4)
                {
                    Contracts.Assert(stride <= source.Length);
                    SseIntrinsics.MatMulTran(matrix, source, destination, destination.Length, stride);
                }
                else
                {
                    Contracts.Assert(stride <= source.Length);
                    for (int i = 0; i < destination.Length; i++)
                    {
                        float dotProduct = 0;
                        for (int j = 0; j < stride; j++)
                        {
                            dotProduct += matrix[j * destination.Length + i] * source[j];
                        }

                        destination[i] = dotProduct;
                    }
                }
            }
        }