4片bytearray使用多线程进行比较

时间:2012-09-03 19:12:35

标签: c# multithreading arraylist parallel-processing bytearray

我被困在项目的第二阶段: 将一个byte []拆分为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(使用memcmp)将其与使用4个线程/ Parallelism进行比较,以便在每个CpuCore上进行计算

2 个答案:

答案 0 :(得分: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;
    }
}

答案 1 :(得分: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。