并行比较图像的问题 [英] problem with comparing images in parallel
本文介绍了并行比较图像的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用.NET 4.0在c#中构建应用程序,以使用并行库比较两个图像。我有一台带有两个核心的PC,但是当我在一个核心中运行应用程序时,我的运行速度比我在两个核心运行时快。
这是比较图像的方法。
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.
推荐答案
嗯,我不是并行编码的专家,但我在自己的代码中遇到了同样的问题(两者都使用了并行编码)编码设施和原始线程)。我想在你的情况下(就像在我的情况下)循环中的操作非常简单,以至于并行执行开销占据执行时间。
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.
我猜这个问题是lock(lok)
。
在一个核心中,没有争用锁定。
有多个核心,有可能会强制进行上下文切换等。
我认为你应该考虑如何对你的算法进行分区,这样你就不需要锁定了。
I''d guess that the problem is thelock(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.
这篇关于并行比较图像的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文