Click here to Skip to main content
13,293,950 members (59,030 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I have build an application in c# with .NET 4.0 to compare two images using parallel library. I have a PC with two cores, but when i run the app in one core i runs faster than when i run it in two.

this is the method that compares images.

private unsafe Bitmap krahas(Bitmap nje, Bitmap dy)
            Bitmap a = new Bitmap(nje);
            Bitmap b = new Bitmap(dy);
            int gjersia = Math.Min(a.Width, b.Width);
            int lartesia = Math.Min(a.Height, b.Height);
            Bitmap rez = new Bitmap(gjersia, lartesia);
            int numri_i_procesorve = Numri_i_berthamave.Value;
            //Bëjmë lock bitat e fotove
            Rectangle per_a = new Rectangle(0, 0, a.Width, a.Height);
            Rectangle per_b = new Rectangle(0, 0, b.Width, b.Height);
            Rectangle per_rez = new Rectangle(0, 0, gjersia, lartesia);
            BitmapData t_per_a = a.LockBits(per_a, ImageLockMode.ReadWrite, a.PixelFormat);
            BitmapData t_per_b = b.LockBits(per_b, ImageLockMode.ReadWrite, b.PixelFormat);
            BitmapData t_per_rez = rez.LockBits(per_rez, ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
            byte* adresa_per_a = (byte*)t_per_a.Scan0;
            byte* adresa_per_b = (byte*)t_per_b.Scan0;
            byte* rezult = (byte*)t_per_rez.Scan0;
            Color cl = Color.Red;
            byte[] ngj = new byte[4];
            ngj[3] = cl.A;
            ngj[2] = cl.R;
            ngj[1] = cl.G;
            ngj[0] = cl.B;
            int rowpadding = t_per_a.Stride - (a.Width * 4);
            ParallelOptions po = new ParallelOptions();
            po.MaxDegreeOfParallelism = numri_i_procesorve;
            object lok = new object();
            Parallel.For(0, lartesia, po, i =>
            lock (lok)
                for (int j = 0; j < gjersia; j++)
                    int same = 0;
                    byte[] tmp = new byte[4];
                    for (int x = 0; x < 4; x++)
                        tmp[x] = adresa_per_b[0];
                        if (adresa_per_a[0] == adresa_per_b[0])
                    for (int x = 0; x < 4; x++)
                        rezult[0] = (same != 4) ? ngj[x] : tmp[x];
                if (rowpadding > 0)
                    adresa_per_a += rowpadding;
                    adresa_per_b += rowpadding;
                    rezult += rowpadding;
            return rez;

I don't now where is the problem. Can anyone help me! Please.
Posted 1-Dec-12 3:30am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Well, I'm not an expert on parallel coding but I have experienced the same 'problem' in my own code (both using parallel coding facilities and raw threads). I suppose that in your case (like in mine) the operations inside the loop are so simple that parallel execution overhead dominates the execution time.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

I'd guess that the problem is the lock(lok).
In a single core there will be no contention for the lock.
With multiple cores, there can be, which will force context switching, etc.
I think you should think about how to partition your algorithm such that you don't need locking.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.171207.1 | Last Updated 3 Dec 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100