很难去平行 [英] Hard time trying to go Parallel

查看:67
本文介绍了很难去平行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我为我的英语道歉

我正在尝试优化/加速我编写的某些代码
用kinect提供的深度框信息制作纹理

我一直在研究System.Threading和其他内容,但无法并行进行任何帮助,将不胜感激

这是我要加速的代码:

first of all I apologize for my English

i am trying to optimize/speed up some code i made
to make a texture with the information of the depth frame provided by a kinect

i have been looking at System.Threading and other stuff but haven''t been able to make this parallel any help would be much appreciated

here is the code i want to speed up:

protected void CopyDepthToTexture(short[] data, TextureBuffer texture)
        {
            CustomPixels.ARGB[,] depthBuffer = new CustomPixels.ARGB[texture.Height, texture.Width];


            float lessSignificant = 0;
            float middleSignificant = 0;
            float moreSignificant = 0;
            int d;

            Action<int, int> indexBuffer;


            for (int y = 0; y < depthBuffer.GetLength(0); y++)
                for (int x = 0; x < depthBuffer.GetLength(1); x++)
                {
                    d = data[y * depthBuffer.GetLength(1) + x] >> DepthImageFrame.PlayerIndexBitmaskWidth;

                    lessSignificant = 0;
                    middleSignificant = 0;
                    moreSignificant = 0;

                    if (d != unknownDepth && d != tooFarDepth && d != tooNearDepth)
                    {
                        lessSignificant = (d % 64) / 64f;
                        middleSignificant = ((d / 64) % 64) / 64f;
                        moreSignificant = ((d / (64 * 64)) % 64) / 64f;
                    }

                    depthBuffer[y, x] = new CustomPixels.ARGB(moreSignificant, middleSignificant, lessSignificant, 1);

                }

            depthTexture.SetData(GraphicResourceUpdateMode.Match, depthBuffer, null);
        }

推荐答案

如果我正确理解了您的问题,则希望并行执行计算线程以使外观运行得更快.这是正确的吗?

您需要将其分解为简单的步骤,然后可以将其变成多线程进程.

1)将循环中的计算分解为一个方法.
If I understand your question correctly -- you want to have computation threads performing in parallel to make this look run faster. Is this correct?

You need to break this down into simple steps then you can turn it into a multi-threaded process.

1) break out your computation in your loop into a single method.
depthBuffer [y, x] = Computation(...)



如果该方法的工作原理与本循环中的工作原理完全相同,那么您将被考虑去考虑多线程.

接下来要做的就是让自己接触多线程.在单独的线程上运行此计算(Google在单独的线程上运行方法-不要问我怎么做)一旦运行成功,便可以尝试将工作分解为并行进程.

您必须弄清楚以下内容:
您的方法必须告知计算已完成(事件过程?)
您的主要过程必须有一种方法来获取答案并将其应用于正确的缓冲区.
您必须阅读MSDN,以确保您的过程是完全线程安全的.因此,请阅读您正在使用的每个主要命令,并遵循必须做的所有事情以确保线程安全.
最后,您必须弄清楚有多少线程是有效的,如何进行计算,然后将循环转换为将数据传递给x线程并处理返回给您的x事件.

祝你好运.只需一个小时的时间就可以解决问题.



When that works EXACTLY the same way as it did in this loop, then you are read to think about multithreading.

Next thing to do is expose yourself to multi-threading. Run this computation on a separate thread (Google running a method on a separate thread -- don''t ask me how to do it) Once this is working then you are ready to try breaking your work out into parallel processes.

YOU have to figure out the following:
Your method must communicate that the computation is complete (event process?)
Your main process must have a way of taking the answer and applying it to the correct buffer.
You must read MSDN to make sure that your process is TOTALLY thread safe. So read each main command you are using and follow whatever you must do to be thread safe.
Finally you must figure out how many threads are efficient, how to break out the computations, and turn your loop into passing data into x threads with handling of x events returned to you.

Good luck. This will not be something you can just crank out in one hours time.


要将解决方案转换为并行解决方案,您还需要检查输出和输入的依存关系.例如,不可能并行运行斐波那契数,因为值X取决于先前的值x-1和x-2.

因此,请注意,并不是所有的事情都能更快地完成,就像Amdahl所说的那样:无论分配多少妇女,生育一个孩子都要花9个月的时间."

但是,您的代码看起来像可以在y变量的外部使用Parallel.For(任务并行库).您也可以在内环上执行此操作,但是根据您的参数,然后可以创建瓶颈.首先尝试并确认解决方案是稳定的.另外,请确保您实际上获得了更好的性能,因为对数组等的访问可能会由于缓存无效等原因而导致瓶颈.

并行编程虽然是一个主题,但却是一个相当不错的主题.
祝您好运.
In order to convert a solution to parallel you need to check also for output and input dependencies. for example it is not possible to run Fibonacci numbers in parallel, because a value X, depends on previous values x-1 and x-2.

So that being said, be aware that not everything can be performed faster, like Amdahl said "The bearing of a child takes nine months, no matter how many women are assigned".

However, your code looks like you can use a Parallel.For (Task Parallel Library) on the outer over the y variable. You could possible do it on the inner loop too, however depending on your parameters, you might then create a bottle neck. Try it first and confirm the solution is stable. Also be sure you are actually getting better performance, since access to an array etc, can cause bottle necks due to cache invalidation etc.

Parallel programming is a though subject, but a pretty one.
Good luck on this.


http://tipsandtricks.runicsoft.com开始/CSharp/ParallelClass.html [ ^ ]
我认为并行无法满足我的需求

但我认为与此有关,但我不确定
http://msdn.microsoft.com/en-us/magazine/cc163340.aspx [ ^ ]
as of this http://tipsandtricks.runicsoft.com/CSharp/ParallelClass.html[^]
i think parallel for wont do what i need

but i think im on to something with this but im not sure yet
http://msdn.microsoft.com/en-us/magazine/cc163340.aspx[^]


这篇关于很难去平行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆