1

我坚持项目的第二阶段:将一个字节 [] 拆分为 4 个切片(以加载 QuadCore I5 CPU),然后在每个切片上,在每个内核上启动一个线程(比较任务)。

原因是试图加速两个相同大小的字节数组之间的比较我如何处理这个?

    [DllImport("msvcrt.dll", CallingConvention = CallingConvention.Cdecl)]
    static extern int memcmp(byte[] b1, byte[] b2, long count);




class ArrayView<T> : IEnumerable<T> 
    {
        private readonly T[] array;
        private readonly int offset, count;
        public ArrayView(T[] array, int offset, int count)
        {
            this.array = array; this.offset = offset; this.count = count;
        }
        public int Length { get { return count; } }
        public T this[int index] {
            get { if (index < 0 || index >= this.count)
                throw new IndexOutOfRangeException();
            else
                return this.array[offset + index]; }
            set { if (index < 0 || index >= this.count)
                throw new IndexOutOfRangeException();
            else
                this.array[offset + index] = value; }
        }
        public IEnumerator<T> GetEnumerator()
        {
            for (int i = offset; i < offset + count; i++)
                yield return array[i];
        } 
        IEnumerator IEnumerable.GetEnumerator()
        {
            IEnumerator<T> enumerator = this.GetEnumerator();
            while (enumerator.MoveNext())
            {
                yield return enumerator.Current;
            }
        }
    } 




    public void CopmarArrSlice()
    {

    byte[] LoadedArr = File.ReadAllBytes("testFileCompare2Scr.bmp");

    int LoddArLn = OrgArr.Length;
    int range =  (LoddArLn / 4) - LoddAremainder;
    int divisionremain = LoddArLn - (range * 4);

    ArrayView<byte> LddP1 = new ArrayView<byte>(OrgArr, 0, range);
    ArrayView<byte> LddP2 = new ArrayView<byte>(OrgArr, p1.Length, range);
    ArrayView<byte> LddP3 = new ArrayView<byte>(OrgArr, (p1.Length + p2.Length), range);
    ArrayView<byte> LddP4 = new ArrayView<byte>(OrgArr, (p1.Length + p2.Length + p3.Length), range + divisionremain);


        if (AreEqual(LddP1, CapturedP1)) ....Do Somthing

    }


    public bool AreEqual(byte[] a, byte[] b)
    {
        if (a == b)
           return true;
        if (a == null || b == null)
            return false;
        if (a.Length != b.Length)
            return false;
        return memcmp(a, b, a.Length) == 0;
    }


    CopmarArrSlice();

在这种情况下,如何使用 AreEqual(using memcmp) 将其与使用 4 个线程/Parallelism 进行比较,以在每个 CpuCore 上进行计算

4

2 回答 2

3

我编写了一个函数,尽可能利用多个内核,但它似乎受到 p/invoke 调用的严重性能影响。我认为这个版本只有在测试非常大的数组时才有意义。

static unsafe class NativeParallel
{
    [DllImport("msvcrt.dll", CallingConvention = CallingConvention.Cdecl)]
    static extern int memcmp(byte* b1, byte* b2, int count);

    public static bool AreEqual(byte[] a, byte[] b)
    {
        // The obvious optimizations
        if (a == b)
            return true;
        if (a == null || b == null)
            return false;
        if (a.Length != b.Length)
            return false;

        int quarter = a.Length / 4;
        int r0 = 0, r1 = 0, r2 = 0, r3 = 0;
        Parallel.Invoke(
            () =>  {
                fixed (byte* ap = &a[0])
                fixed (byte* bp = &b[0])
                    r0 = memcmp(ap, bp, quarter);                        
            },
            () => {
                fixed (byte* ap = &a[quarter])
                fixed (byte* bp = &b[quarter])
                    r1 = memcmp(ap, bp, quarter);
            },
            () => {
                fixed (byte* ap = &a[quarter * 2])
                fixed (byte* bp = &b[quarter * 2])
                    r2 = memcmp(ap, bp, quarter);
            },
            () => {
                fixed (byte* ap = &a[quarter * 3])
                fixed (byte* bp = &b[quarter * 3])
                    r3 = memcmp(ap, bp, a.Length - (quarter * 3));
            }
        );
        return r0 + r1 + r2 + r3 == 0;
    }
}

在大多数情况下,它实际上比优化的安全版本慢。

static class SafeParallel
{
    public static bool AreEqual(byte[] a, byte[] b)
    {
        if (a == b)
            return true;
        if (a == null || b == null)
            return false;
        if (a.Length != b.Length)
            return false;

        bool b1 = false;
        bool b2 = false;
        bool b3 = false;
        bool b4 = false;
        int quarter = a.Length / 4;
        Parallel.Invoke(
            () => b1 = AreEqual(a, b, 0, quarter),
            () => b2 = AreEqual(a, b, quarter, quarter),
            () => b3 = AreEqual(a, b, quarter * 2, quarter),
            () => b4 = AreEqual(a, b, quarter * 3, a.Length)
        );
        return b1 && b2 && b3 && b4;
    }

    static bool AreEqual(byte[] a, byte[] b, int start, int length)
    {
        var len = length / 8;
        if (len > 0)
        {
            for (int i = start; i < len; i += 8)
            {
                if (BitConverter.ToInt64(a, i) != BitConverter.ToInt64(b, i))
                    return false;
            }
        }
        var remainder = length % 8;
        if (remainder > 0)
        {
            for (int i = length - remainder; i < length; i++)
            {
                if (a[i] != b[i])
                    return false;
            }
        }
        return true;
    }
}
于 2012-09-03T20:11:26.253 回答
1

我认为您不必以单线程和经典 c# 方式拆分字节,您将使用

foreach(byte currentArr in LoadedArr)
{
    if (AreEqyal(currentArr, CapturedP1))
           ....Do Somthing
}

但是要通过将工作负载分配给多个线程来处理每个字节,您必须使用以下语法;

// max your threads count in my case 16,
int[] sums = new int[16];// optional, just to know the workload
public void ProcessMyByte(byte current)
{
    if (AreEqyal(current, CapturedP1))
           ....Do Somthing

       // optional just to know what thread is in
        sums[Thread.CurrentThread.ManagedThreadId]++;// increment the number of iterations done by the thread who did this elementary process
}

.... Main()...
{
....

byte[] LoadedArr = File.ReadAllBytes("testFileCompare2Scr.bmp");
Parallel.ForEach(LoadedArr, ProcessMyByte);

...
}

所以并行性将代表您进行管理,甚至更好,因为当一个线程空闲时,它会获得下一个任务,而不是好像您将它分成 4 个,每个线程都必须处理 Length/4。

于 2012-09-03T20:07:30.457 回答