Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your second code isn't correct. I think the correct code is like this:</p> <pre><code>var accumulatedDelta= new Vector2[MAX_PARTICLES]; for(int i = 0; i &lt; _numActiveParticles; ++i) { accumulatedDelta = calculateForce(_activeParticles[i], accumulatedDelta); } for (int i = _numActiveParticles - 1; i &gt;= 0; i--) { int index = _activeParticles[i]; _delta[index] += accumulatedDelta[index] / MULTIPLIER; } </code></pre> <p>I don't know what <code>.net2</code> has and what doesn't. But you can simulate <code>Parallel.For</code> yourself.</p> <p>explanation of this overload of <code>Parallel.For</code> is this:</p> <p>First parameter: start index of loop</p> <p>Second parameter: end index of loop</p> <p>Third parameter: a delegate that will create task local data. for each every thread that <code>Parallel.For</code> use (a task) this delegate will be called and return <code>localInit</code> data.</p> <p>Fourth parameter: a delegate that act as body of <code>for</code>. In first execution of body delegate, this delegate will retrieve data that is created by previuse delegate (<code>localInit</code>). in every subsequent loop, body delegate can change <code>localInit</code> and then return that to next body execution. In last execution of body delegate, <code>localInit</code> data will be passed to last delegate.</p> <p>Last parameter: another delegate that will be called per each task, when task has finished it's work. <code>localInit</code> will be passed to this delegate. Because this delegate can be called concurrency by multiple tasks, you must protect your shared data.</p> <p><strong>Edit:</strong></p> <p>A version of <code>ParallelFor</code> can be like this:</p> <pre><code>public static void ParallerFor&lt;TLocal&gt;(int startIndex, int endIndex, Func&lt;TLocal&gt; initData, Func&lt;int, TLocal, TLocal&gt; body, Action&lt;TLocal&gt; finalizer) { int numThreads = Environment.ProcessorCount; int chunkOffset = ((endIndex - startIndex) / numThreads) + 1; Task[] tasks = new Task[numThreads]; Enumerable.Range(0, numThreads).ToList().ForEach(x =&gt; { int start = x * chunkOffset; int end = ((x + 1) * chunkOffset); end = end &gt; endIndex ? endIndex : end; tasks[x] = Task.Factory.StartNew(() =&gt; { TLocal init = initData(); for(int i = start; i &lt; end; ++i) { init = body(i, init); } finalizer(init); }); }); Task.WhenAll(tasks).Wait(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload