Click here to Skip to main content
11,639,305 members (67,825 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
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;
            koha_e_kaluar.Reset();
            koha_e_kaluar.Start();
            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])
                        {
                            same++;
                        }
                        adresa_per_a++;
                        adresa_per_b++;
                    }
                    for (int x = 0; x < 4; x++)
                    {
                        rezult[0] = (same != 4) ? ngj[x] : tmp[x];
                        rezult++;
                    }
                }
                if (rowpadding > 0)
                {
                    adresa_per_a += rowpadding;
                    adresa_per_b += rowpadding;
                    rezult += rowpadding;
                }
            }
        });
            koha_e_kaluar.Stop();
            a.UnlockBits(t_per_a);
            b.UnlockBits(t_per_b);
            rez.UnlockBits(t_per_rez);
            return rez;
        }


I don't now where is the problem. Can anyone help me! Please.
Posted 1-Dec-12 2:30am
Rate this: bad
good
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.
  Permalink  
Rate this: bad
good
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.
  Permalink  

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

  Print Answers RSS
0 Suvendu Shekhar Giri 320
1 Sergey Alexandrovich Kryukov 283
2 Richard Deeming 200
3 CPallini 187
4 PIEBALDconsult 175
0 OriginalGriff 9,081
1 Sergey Alexandrovich Kryukov 8,812
2 Mika Wendelius 7,027
3 Suvendu Shekhar Giri 2,600
4 F-ES Sitecore 2,548


Advertise | Privacy | Mobile
Web03 | 2.8.150728.1 | Last Updated 3 Dec 2012
Copyright © CodeProject, 1999-2015
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