Click here to Skip to main content
15,881,413 members
Please Sign up or sign in to vote.
4.67/5 (3 votes)
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:

C#
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);
        }
Posted
Updated 24-Apr-12 7:53am
v2
Comments
Sergey Alexandrovich Kryukov 24-Apr-12 14:34pm    
I cannot see any sign of parallel solution. Could you explain it?
--SA
wizardzz 24-Apr-12 14:51pm    
He doesn't know how to, or if he can, I think.

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.
C#
depthBuffer [y, x] = Computation(...)


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.
 
Share this answer
 
Comments
Armando Martínez González 24-Apr-12 23:15pm    
what i had in mind was to substitute the double for for a parallel for and call the following method instead i wasn't able to find out how to call a method inside a parallel for other than those who are void and accept a single int parameter as i said in the other reply to Leonardo Paneque i am almost certain i am missing something but i cat find what it is and also it si possible you just told me what to do but again my english is not that good

<pre lang="c#"> void CopyDepthToTexture(int x, ref int d, short[] data, CustomPixels.ARGB[,] depthBuffer, ref float lessSignificant, ref float middleSignificant, ref float moreSignificant)
{
//d = data[y * depthBuffer.GetLength(1) + x] >> DepthImageFrame.PlayerIndexBitmaskWidth;

d = data[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[x % depthBuffer.GetLength(0), x / depthBuffer.GetLength(0)] = new CustomPixels.ARGB(moreSignificant, middleSignificant, lessSignificant, 1);
}
}</pre>
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.
 
Share this answer
 
Comments
Armando Martínez González 24-Apr-12 22:58pm    
I tried to use parallel for but it can be done as far as i know only with a task<int> and i use alot of variables to do the code inside the for and task are static so no fields either i dont know how to get the variables i need in static method with just an int as a parameter i am almost certain i am missing something but i cat find what it is
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[^]
 
Share this answer
 
Comments
[no name] 25-Apr-12 21:47pm    
is what I said, you need to use Parallel.For :) btw, Matcom, UH? same as me. :)
just avoid shared/static variables as much as you can, in a few words, you need to re-design the algorithm for parallel if is not obvious.
Armando Martínez González 26-Apr-12 2:14am    
si matcom (Katrib Somoza Iskander) de que año tu eres?
at last i got a hold of something usefull
here maybe this will help another noob like me

C#
void Calling_Method_In_Separate_Thread()
{
    ParameterizedThreadStart parameterizedThreadStart = Method;
    var thread = new Thread(parameterizedThreadStart);
    // a class that wraps everything you need
    var parameter = new RequiredParametersWrapper(all,that,you,need)
    thread.Start(parameter);
}
void Method(Object preciousInformation)
{
    var myParameters = (RequiredParametersWrapper) preciousInformation;
    // necessary code
}


why did nobody gave me an example like this?
i'm sure ill have to work out some other issues but it feels like the right path!!!

here is more info:
http://stackoverflow.com/questions/5432904/how-to-run-method-in-separate-thread[^]
 
Share this answer
 
v2
Comments
[no name] 10-May-12 15:12pm    
that's not parallel, that just in a different thread.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900