Unexpected Results while saving results to an Array using Parallel.ForThis is a modified version of some code I posted in another thread for CPU utilization.  I discovered while I was debugging that problem that I had a more fundamental problem of my loop does not appear to save the correct data in the correct place in the Array for either calc_results or calc_geom.   An array of [][][][] was suggested in the other thread instead of [,,,] but when I try to initialize it I get an error.  I am not familiar with the [][][][] arrays and really don't know the differance between the two arrays.  Thanks for your help. <div><br/></div> <div> <div> Results[, , ,] calc_results = new Results[heave_size, pitch_size, roll_size, steer_size];</div> <div>            Start_Geom[,,,] calc_geom = new Start_Geom[heave_size, pitch_size, roll_size, steer_size];</div> <div>            //Results[][][][] test_results = new Results[heave_size] [pitch_size] [roll_size] [steer_size];</div> <div><br/></div> <div>           dnAnalytics.LinearAlgebra.Matrix x;</div> <div><br/></div> <div>            double[] heaves = new double[heave_size];</div> <div>            double[] pitches = new double[pitch_size];</div> <div>            double[] rolls = new double[roll_size];</div> <div>            double[] steering = new double[steer_size];</div> <div>            </div> <div>            Parallel.For(0, heave_size, delegate(int h)</div> <div>            //for (int h = 0; h &lt; heave_size; h++)</div> <div>            {</div> <div>                for (int p = 0; p &lt; pitch_size; p++)</div> <div>                {</div> <div>                    for (int r = 0; r &lt; roll_size; r++)</div> <div>                    {</div> <div>                        for (int s = 0; s &lt; steer_size; s++)</div> <div>                        {</div> <div>                            double heave_val = heave_min + h * heave_inc;</div> <div>                            double pitch_val = pitch_min + p * pitch_inc;</div> <div>                            double roll_val = roll_min + r * roll_inc;</div> <div>                            double steer_val = steer_min + s * steer_inc;</div> <div><br/></div> <div>                            x = new DenseMatrix(new double[,] { { heave_val }, { roll_val }, { pitch_val }, { steer_val }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 } });</div> <div><br/></div> <div>                            Start_Geom calc_geom_temp = new Start_Geom();</div> <div>                            Results calc_results_temp = new Results();</div> <div><br/></div> <div>                            kinematics.locate_vehicle(x, last_geom, start_geom,</div> <div>                                vehicle, test_track, component_params, static_results[i],</div> <div>                                results_cur, results_last, out calc_geom_temp, out calc_results_temp);</div> <div><br/></div> <div>                            </div> <div>                            calc_geom[h, p, r, s] = new Start_Geom(calc_geom_temp);</div> <div>                            calc_results[h, p, r, s] = new Results(calc_results_temp);</div> <div>                            //test_results[h][p][r][s] = new Results(calc_results_temp);</div> <div><br/></div> <div>                            steering[s] = steer_val;</div> <div>                            rolls[r] = roll_val;</div> <div>                            pitches[p] = pitch_val;</div> <div>                            heaves[h] = heave_val; </div> <div>                        }</div> <div>                        progressBar.Dispatcher.Invoke(</div> <div>                                System.Windows.Threading.DispatcherPriority.Normal,</div> <div>                          new System.Action(</div> <div>                            delegate()</div> <div>                            {</div> <div>                                progressBar.Value = progressBar.Value+steer_size;</div> <div>                            }</div> <div>                            ));</div> <div><br/></div> <div><br/></div> <div>                    }</div> <div>                }</div> <div>            }</div> <div>            );</div> </div>© 2009 Microsoft Corporation. All rights reserved.Tue, 30 Jun 2009 22:42:41 Z1161a248-e5bd-444e-9458-36433d4ab566http://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/1161a248-e5bd-444e-9458-36433d4ab566#1161a248-e5bd-444e-9458-36433d4ab566http://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/1161a248-e5bd-444e-9458-36433d4ab566#1161a248-e5bd-444e-9458-36433d4ab566pltaylorhttp://social.msdn.microsoft.com/Profile/en-US/?user=pltaylorUnexpected Results while saving results to an Array using Parallel.ForThis is a modified version of some code I posted in another thread for CPU utilization.  I discovered while I was debugging that problem that I had a more fundamental problem of my loop does not appear to save the correct data in the correct place in the Array for either calc_results or calc_geom.   An array of [][][][] was suggested in the other thread instead of [,,,] but when I try to initialize it I get an error.  I am not familiar with the [][][][] arrays and really don't know the differance between the two arrays.  Thanks for your help. <div><br/></div> <div> <div> Results[, , ,] calc_results = new Results[heave_size, pitch_size, roll_size, steer_size];</div> <div>            Start_Geom[,,,] calc_geom = new Start_Geom[heave_size, pitch_size, roll_size, steer_size];</div> <div>            //Results[][][][] test_results = new Results[heave_size] [pitch_size] [roll_size] [steer_size];</div> <div><br/></div> <div>           dnAnalytics.LinearAlgebra.Matrix x;</div> <div><br/></div> <div>            double[] heaves = new double[heave_size];</div> <div>            double[] pitches = new double[pitch_size];</div> <div>            double[] rolls = new double[roll_size];</div> <div>            double[] steering = new double[steer_size];</div> <div>            </div> <div>            Parallel.For(0, heave_size, delegate(int h)</div> <div>            //for (int h = 0; h &lt; heave_size; h++)</div> <div>            {</div> <div>                for (int p = 0; p &lt; pitch_size; p++)</div> <div>                {</div> <div>                    for (int r = 0; r &lt; roll_size; r++)</div> <div>                    {</div> <div>                        for (int s = 0; s &lt; steer_size; s++)</div> <div>                        {</div> <div>                            double heave_val = heave_min + h * heave_inc;</div> <div>                            double pitch_val = pitch_min + p * pitch_inc;</div> <div>                            double roll_val = roll_min + r * roll_inc;</div> <div>                            double steer_val = steer_min + s * steer_inc;</div> <div><br/></div> <div>                            x = new DenseMatrix(new double[,] { { heave_val }, { roll_val }, { pitch_val }, { steer_val }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 } });</div> <div><br/></div> <div>                            Start_Geom calc_geom_temp = new Start_Geom();</div> <div>                            Results calc_results_temp = new Results();</div> <div><br/></div> <div>                            kinematics.locate_vehicle(x, last_geom, start_geom,</div> <div>                                vehicle, test_track, component_params, static_results[i],</div> <div>                                results_cur, results_last, out calc_geom_temp, out calc_results_temp);</div> <div><br/></div> <div>                            </div> <div>                            calc_geom[h, p, r, s] = new Start_Geom(calc_geom_temp);</div> <div>                            calc_results[h, p, r, s] = new Results(calc_results_temp);</div> <div>                            //test_results[h][p][r][s] = new Results(calc_results_temp);</div> <div><br/></div> <div>                            steering[s] = steer_val;</div> <div>                            rolls[r] = roll_val;</div> <div>                            pitches[p] = pitch_val;</div> <div>                            heaves[h] = heave_val; </div> <div>                        }</div> <div>                        progressBar.Dispatcher.Invoke(</div> <div>                                System.Windows.Threading.DispatcherPriority.Normal,</div> <div>                          new System.Action(</div> <div>                            delegate()</div> <div>                            {</div> <div>                                progressBar.Value = progressBar.Value+steer_size;</div> <div>                            }</div> <div>                            ));</div> <div><br/></div> <div><br/></div> <div>                    }</div> <div>                }</div> <div>            }</div> <div>            );</div> </div>Mon, 08 Jun 2009 12:54:07 Z2009-06-08T12:54:07Zhttp://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/1161a248-e5bd-444e-9458-36433d4ab566#f3ddc690-636c-4822-9a4c-8f0cb0cd85a4http://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/1161a248-e5bd-444e-9458-36433d4ab566#f3ddc690-636c-4822-9a4c-8f0cb0cd85a4pltaylorhttp://social.msdn.microsoft.com/Profile/en-US/?user=pltaylorUnexpected Results while saving results to an Array using Parallel.ForJust to add a little more detail if I uncomment &quot;for (int h = 0; h &lt; heave_size; h++)&quot; and comment &quot;Parallel.For(0, heave_size, delegate(int h)&quot; the procedure works exactly as expected.Mon, 08 Jun 2009 16:46:12 Z2009-06-08T16:46:12Zhttp://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/1161a248-e5bd-444e-9458-36433d4ab566#0f234c13-76f8-43c3-804a-5fcba2628e66http://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/1161a248-e5bd-444e-9458-36433d4ab566#0f234c13-76f8-43c3-804a-5fcba2628e66Tibor19http://social.msdn.microsoft.com/Profile/en-US/?user=Tibor19Unexpected Results while saving results to an Array using Parallel.ForHi,<br/><br/>Your probable cause of your problem is  progressBar.Dispatcher.Invoke( <div>                                System.Windows.Threading.DispatcherPriority.Normal,</div> <div>                          new System.Action(</div> <div>                            delegate()</div> <div>                            {</div> <div>                                progressBar.Value = progressBar.Value+steer_size;</div> <div>                            }</div> <div>                            ));<br/><br/>What happends if you comment that out? Does it works ok? I know you won't get you progress bar to update the way you want, but it helps isolate the problem.<br/><br/>The second cause might be locality. When you create such a big array, then you will get a contigous area of memory for your array, which will be locked on every use by the processor using it at that time so you won't get any speed up in the end.<br/><br/>Best regards,<br/>Tibi </div><hr class="sig">MCT, MCDBA, MCSD.NET, MCPD 2.0(*.*), MCPD 3.5(*.*)Mon, 08 Jun 2009 16:59:27 Z2009-06-08T16:59:27Zhttp://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/1161a248-e5bd-444e-9458-36433d4ab566#18d60fe0-8110-41d4-b9bd-4316c1840cdfhttp://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/1161a248-e5bd-444e-9458-36433d4ab566#18d60fe0-8110-41d4-b9bd-4316c1840cdfpltaylorhttp://social.msdn.microsoft.com/Profile/en-US/?user=pltaylorUnexpected Results while saving results to an Array using Parallel.ForCommenting out the progress bar has no effect, other than the obvious one of the progress bar not functioning.  I have even attempted to change the calc_results[h, p, r, s] = new Results(calc_results_temp) to lock(calc_results[h, p, r, s] = new Results(calc_results_temp)); and that doens't seem to have any effect.  The results in calc_results and calc_geom are still random.Mon, 08 Jun 2009 17:04:21 Z2009-06-08T17:04:21Zhttp://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/1161a248-e5bd-444e-9458-36433d4ab566#d0dc9914-7bca-478c-bc80-06a82772023fhttp://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/1161a248-e5bd-444e-9458-36433d4ab566#d0dc9914-7bca-478c-bc80-06a82772023fTibor19http://social.msdn.microsoft.com/Profile/en-US/?user=Tibor19Unexpected Results while saving results to an Array using Parallel.ForHi Again,<br/><br/>If you want to use a different kind of array, here is a suggestion on how you can modify your code to avoid cache contention.<br/><br/> <pre lang="x-c#">Results[][,,] calc_results = new Results[heave_size][, , ]; Start_Geom[][,,] calc_geom = new Start_Geom[heave_size][, , ]; // Results[][][][] test_results = new Results[heave_size] [pitch_size] [roll_size] [steer_size]; dnAnalytics.LinearAlgebra.Matrix x; double[] heaves = new double[heave_size]; double[] pitches = new double[pitch_size]; double[] rolls = new double[roll_size]; double[] steering = new double[steer_size]; //for (int h = 0; h &lt; heave_size; h++) Parallel.For(0, heave_size, (h) =&gt; { calc_results[h] = new Results[pitch_size, roll_size, steer_size]; calc_geom[h] = new Start_Geom[pitch_size, roll_size, steer_size]; for (int p = 0; p &lt; pitch_size; p++) { for (int r = 0; r &lt; roll_size; r++) { for (int s = 0; s &lt; steer_size; s++) { double heave_val = heave_min + h * heave_inc; double pitch_val = pitch_min + p * pitch_inc; double roll_val = roll_min + r * roll_inc; double steer_val = steer_min + s * steer_inc; x = new DenseMatrix(new double[,] { { heave_val }, { roll_val }, { pitch_val }, { steer_val }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 } }); Start_Geom calc_geom_temp = new Start_Geom(); Results calc_results_temp = new Results(); kinematics.locate_vehicle(x, last_geom, start_geom, vehicle, test_track, component_params, static_results[i], results_cur, results_last, out calc_geom_temp, out calc_results_temp); calc_geom[h][p, r, s] = new Start_Geom(calc_geom_temp); calc_results[h][p, r, s] = new Results(calc_results_temp); //test_results[h][p,r,s] = new Results(calc_results_temp); steering[s] = steer_val; rolls[r] = roll_val; pitches[p] = pitch_val; heaves[h] = heave_val; } progressBar.Dispatcher.Invoke( System.Windows.Threading.DispatcherPriority.Normal, new System.Action( delegate() { progressBar.Value = progressBar.Value + steer_size; } )); } } } ); Best regards,<br/>Tibi<br/></pre><hr class="sig">MCT, MCDBA, MCSD.NET, MCPD 2.0(*.*), MCPD 3.5(*.*)Mon, 08 Jun 2009 17:16:55 Z2009-06-08T17:16:55Zhttp://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/1161a248-e5bd-444e-9458-36433d4ab566#df5e1cc7-4f8c-4ad2-9f74-a476f7ab6655http://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/1161a248-e5bd-444e-9458-36433d4ab566#df5e1cc7-4f8c-4ad2-9f74-a476f7ab6655Tibor19http://social.msdn.microsoft.com/Profile/en-US/?user=Tibor19Unexpected Results while saving results to an Array using Parallel.ForBTW,<br/><br/>I suppose that <br/><span style="color:blue">new</span> Start_Geom(calc_geom_temp); and <span style="color:blue">new</span> Results(calc_results_temp);<br/>are copy constuctors, which you don't need in C#. <br/><br/>Instead you can just write calc_geom[h][p, r, s] = calc_geom_temp; calc_results[h][p, r, s] = calc_results_temp;<br/>and remove the copy constructors!<br/><br/>Best regards,<br/>Tibi<br/><hr class="sig">MCT, MCDBA, MCSD.NET, MCPD 2.0(*.*), MCPD 3.5(*.*)Mon, 08 Jun 2009 17:29:41 Z2009-06-08T17:29:41Zhttp://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/1161a248-e5bd-444e-9458-36433d4ab566#d24df6ed-5cd0-4307-9826-8c00067cfb6fhttp://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/1161a248-e5bd-444e-9458-36433d4ab566#d24df6ed-5cd0-4307-9826-8c00067cfb6fpltaylorhttp://social.msdn.microsoft.com/Profile/en-US/?user=pltaylorUnexpected Results while saving results to an Array using Parallel.ForIf I remove the copy constructors it makes the entire array equal to the last thing I copied into the array. <div><br/></div> <div>I tried the previous suggestion and though it did speed it up a tiny bit it is still giving me random values for the calc_results and calc_geom.</div>Mon, 08 Jun 2009 17:34:20 Z2009-06-08T17:34:20Zhttp://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/1161a248-e5bd-444e-9458-36433d4ab566#36872e54-56df-41ca-8c4c-d3fa7b70c13bhttp://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/1161a248-e5bd-444e-9458-36433d4ab566#36872e54-56df-41ca-8c4c-d3fa7b70c13bTibor19http://social.msdn.microsoft.com/Profile/en-US/?user=Tibor19Unexpected Results while saving results to an Array using Parallel.ForI think that you have some interdependencies between results. You would have to ananlyze kinematics.locate_vehicle(x, last_geom, start_geom,<br/>                    vehicle, test_track, component_params, static_results[i],<br/>                    results_cur, results_last, <span style="color:blue">out</span> calc_geom_temp, <span style="color:blue">out</span> calc_results_temp);<br/><br/>Best regards,<br/>Tibi<br/><hr class="sig">MCT, MCDBA, MCSD.NET, MCPD 2.0(*.*), MCPD 3.5(*.*)Mon, 08 Jun 2009 18:09:13 Z2009-06-08T18:09:13Zhttp://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/1161a248-e5bd-444e-9458-36433d4ab566#19c59ea7-8a20-4ea4-89b1-6130ff63df30http://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/1161a248-e5bd-444e-9458-36433d4ab566#19c59ea7-8a20-4ea4-89b1-6130ff63df30pltaylorhttp://social.msdn.microsoft.com/Profile/en-US/?user=pltaylorUnexpected Results while saving results to an Array using Parallel.ForDo you have any suggestions on what I should look for?Mon, 08 Jun 2009 18:10:35 Z2009-06-08T18:10:35Zhttp://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/1161a248-e5bd-444e-9458-36433d4ab566#ad1d2346-ff3c-425d-a9c5-b1b8852b379dhttp://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/1161a248-e5bd-444e-9458-36433d4ab566#ad1d2346-ff3c-425d-a9c5-b1b8852b379dpltaylorhttp://social.msdn.microsoft.com/Profile/en-US/?user=pltaylorUnexpected Results while saving results to an Array using Parallel.ForCould static methods be the culprit?Mon, 08 Jun 2009 18:31:00 Z2009-06-08T18:31:00Zhttp://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/1161a248-e5bd-444e-9458-36433d4ab566#ea9ed3c0-9c1d-485b-855d-fac2a1b7ba1bhttp://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/1161a248-e5bd-444e-9458-36433d4ab566#ea9ed3c0-9c1d-485b-855d-fac2a1b7ba1bTibor19http://social.msdn.microsoft.com/Profile/en-US/?user=Tibor19Unexpected Results while saving results to an Array using Parallel.ForIf they don't share data no!<br/><br/>/Tibi<hr class="sig">MCT, MCDBA, MCSD.NET, MCPD 2.0(*.*), MCPD 3.5(*.*)Mon, 08 Jun 2009 18:40:50 Z2009-06-08T18:40:50Zhttp://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/1161a248-e5bd-444e-9458-36433d4ab566#553db8ea-ca82-4e27-ab84-c3bb2465c1c9http://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/1161a248-e5bd-444e-9458-36433d4ab566#553db8ea-ca82-4e27-ab84-c3bb2465c1c9pltaylorhttp://social.msdn.microsoft.com/Profile/en-US/?user=pltaylorUnexpected Results while saving results to an Array using Parallel.ForDoes anyone have any suggestions on how I can track this error down?  I.e. what tools I can use to figure out what is happening here?  I have narrowed it down to something wrong in the parallel loop cause when I run it w/ only one outer loop everything calculates fine.Tue, 09 Jun 2009 14:19:48 Z2009-06-09T14:19:48Zhttp://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/1161a248-e5bd-444e-9458-36433d4ab566#219e4c11-779a-4882-b6a2-37582a7b43f5http://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/1161a248-e5bd-444e-9458-36433d4ab566#219e4c11-779a-4882-b6a2-37582a7b43f5Josh Phillipshttp://social.msdn.microsoft.com/Profile/en-US/?user=Josh%20%20PhillipsUnexpected Results while saving results to an Array using Parallel.ForHi pltaylor, <div><br/></div> <div>My guess is that the problem lies here:</div> <div><br/></div> <div> <div> <pre style="list-style-type:none;text-decoration:none;font-size:1.25em;padding:0px;margin:0px;border:initial none initial">steering[s] = steer_val; rolls[r] = roll_val; pitches[p] = pitch_val; heaves[h] = heave_val;</pre> <pre style="list-style-type:none;text-decoration:none;font-size:1.25em;padding:0px;margin:0px;border:initial none initial"><br/></pre> <pre style="list-style-type:none;text-decoration:none;font-size:1.25em;padding:0px;margin:0px;border:initial none initial">You've parallelized the outer-most for loop with the index 'h'. Multiple iterations each with their own given index 'h' may be </pre> <pre style="list-style-type:none;text-decoration:none;font-size:1.25em;padding:0px;margin:0px;border:initial none initial">executing in an overlapped manner and because you've defined these four arrays outside of the loop, the same state is shared between</pre> <pre style="list-style-type:none;text-decoration:none;font-size:1.25em;padding:0px;margin:0px;border:initial none initial">threads. With just the right (wrong?) overlapping, two threads could be processing the same index 'r' and race to set roll[r], for example. </pre> <pre style="list-style-type:none;text-decoration:none;font-size:1.25em;padding:0px;margin:0px;border:initial none initial"><br/></pre> <pre style="list-style-type:none;text-decoration:none;font-size:1.25em;padding:0px;margin:0px;border:initial none initial">I can't see in your code, however, if you're actually using the values stored in these arrays, or just storing them. What do the constructors </pre> <pre style="list-style-type:none;text-decoration:none;font-size:1.25em;padding:0px;margin:0px;border:initial none initial">for Start_Geom and Results do?</pre> <pre style="list-style-type:none;text-decoration:none;font-size:1.25em;padding:0px;margin:0px;border:initial none initial"><br/></pre> <pre style="list-style-type:none;text-decoration:none;font-size:1.25em;padding:0px;margin:0px;border:initial none initial">Josh</pre> </div> </div>Tue, 09 Jun 2009 15:23:04 Z2009-06-09T15:23:36Zhttp://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/1161a248-e5bd-444e-9458-36433d4ab566#0b888791-feeb-4087-bfa1-bbae02802e28http://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/1161a248-e5bd-444e-9458-36433d4ab566#0b888791-feeb-4087-bfa1-bbae02802e28pltaylorhttp://social.msdn.microsoft.com/Profile/en-US/?user=pltaylorUnexpected Results while saving results to an Array using Parallel.ForThe  <div><br/></div> <div> <pre style="list-style-type:none;text-decoration:none;font-size:1.25em;padding:0px;margin:0px;border:initial none initial">steering[s] = steer_val; rolls[r] = roll_val; pitches[p] = pitch_val; heaves[h] = heave_val;</pre> <pre style="list-style-type:none;text-decoration:none;font-size:1.25em;padding:0px;margin:0px;border:initial none initial"><br/></pre> <pre style="list-style-type:none;text-decoration:none;font-size:1.25em;padding:0px;margin:0px;border:initial none initial">are in the inner most part of the loop and are actually filling correctly. (They fill multiple times </pre> <pre style="list-style-type:none;text-decoration:none;font-size:1.25em;padding:0px;margin:0px;border:initial none initial">during the looping but they have so far always ended up correct at the end. It is the </pre> <pre style="list-style-type:none;text-decoration:none;font-size:1.25em;padding:0px;margin:0px;border:initial none initial"><span style="font-family:Verdana;white-space:normal"><pre style="list-style-type:none;text-decoration:none;font-size:1.25em;padding:0px;margin:0px;border:initial none initial">x = <span style="color:blue">new</span> DenseMatrix(<span style="color:blue">new</span> <span style="color:blue">double</span>[,] { { heave_val }, { roll_val }, { pitch_val }, { steer_val }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 } });</pre> <pre style="list-style-type:none;text-decoration:none;font-size:1.25em;padding:0px;margin:0px;border:initial none initial"><br/></pre> <pre style="list-style-type:none;text-decoration:none;font-size:1.25em;padding:0px;margin:0px;border:initial none initial">...</pre> </span></pre> <pre style="list-style-type:none;text-decoration:none;font-size:1.25em;padding:0px;margin:0px;border:initial none initial"><span style="font-family:Verdana;white-space:normal"><pre style="list-style-type:none;text-decoration:none;font-size:1.25em;padding:0px;margin:0px;border:initial none initial">kinematics.locate_vehicle(x, last_geom, start_geom, vehicle, test_track, component_params, static_results[i], results_cur, results_last, <span style="color:blue">out</span> calc_geom_temp, <span style="color:blue">out</span> calc_results_temp); calc_geom[h][p, r, s] = <span style="color:blue">new</span> Start_Geom(calc_geom_temp); calc_results[h][p, r, s] = <span style="color:blue">new</span> Results(calc_results_temp);</pre> <pre style="list-style-type:none;text-decoration:none;font-size:1.25em;padding:0px;margin:0px;border:initial none initial">that is not executing as expected. It appeared as though my x was going sideways w/ multiple x's </pre> <pre style="list-style-type:none;text-decoration:none;font-size:1.25em;padding:0px;margin:0px;border:initial none initial">existing at the same time. So I tried </pre> <pre style="list-style-type:none;text-decoration:none;font-size:1.25em;padding:0px;margin:0px;border:initial none initial"><br/></pre> <pre style="list-style-type:none;padding:0px;margin:0px;border:initial none initial"><span style="font-size:medium"><span style="font-size:14px">dnAnalytics.LinearAlgebra.Matrix x = new DenseMatrix(new double[,] { { heave_val }, { roll_val }, { pitch_val }, { steer_val }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 } });</span></span></pre> <pre style="list-style-type:none;padding:0px;margin:0px;border:initial none initial"><span style="font-size:medium"><span style="font-size:14px"><br/></span></span></pre> <pre style="list-style-type:none;padding:0px;margin:0px;border:initial none initial"><span style="font-size:medium"><span style="font-size:14px">to localize it to the thread. This gives me different and still not valid results. The only thing</span></span></pre> <pre style="list-style-type:none;padding:0px;margin:0px;border:initial none initial"><span style="font-size:medium"><span style="font-size:14px">I have been able to do to give valid results is limit the outermost loop to a single iteration which </span></span></pre> <pre style="list-style-type:none;padding:0px;margin:0px;border:initial none initial"><span style="font-size:medium"><span style="font-size:14px">just makes it all happen in one thread.</span></span></pre> <pre style="list-style-type:none;padding:0px;margin:0px;border:initial none initial"><span style="font-size:medium"><span style="font-size:14px"><br/></span></span></pre> <pre style="list-style-type:none;padding:0px;margin:0px;border:initial none initial"><span style="font-size:medium"><span style="font-size:14px">The constructors are just copy constructors.</span></span></pre> </span></pre> </div>Tue, 09 Jun 2009 15:47:37 Z2009-06-09T15:48:17Zhttp://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/1161a248-e5bd-444e-9458-36433d4ab566#8b4e5683-267f-41ef-a578-6d373345a283http://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/1161a248-e5bd-444e-9458-36433d4ab566#8b4e5683-267f-41ef-a578-6d373345a283Josh Phillipshttp://social.msdn.microsoft.com/Profile/en-US/?user=Josh%20%20PhillipsUnexpected Results while saving results to an Array using Parallel.For<p>The fact that those four arrays are filling correctly is probably just luck :)  The code is non-deterministic, and if you're actually consuming those values later, you're bound to get just the right interleaving at some point in time that may cause unexpected behavior.  <br/><br/>From what you've described I'm willing to bet the farm that you have some state that is shared -- now it's just a matter of finding said state and handling it well.  <br/><br/>For example, in...<br/><br/>kinematics.locate_vehicle(x, last_geom, start_geom,<br/>                    vehicle, test_track, component_params, static_results[i],<br/>                    results_cur, results_last, <span style="color:blue">out</span> calc_geom_temp, <span style="color:blue">out</span> calc_results_temp);<br/><br/>... some of these parameters must be defined outside the loop as I don't see their definitions inside the loop.  What is last_geom, start_geom?  What is static_results and is it possible that the same value for 'i' can be given to multiple threads?<br/><br/>Are there any static variables referenced in locate_vehicle?<br/><br/>You'll need to track down what state these loop bodies share.  Also, what happens when you change your Parallel.For loop line to the following?<br/><br/>var parOpts = new ParallelOptions();<br/>parOpts.MaxDegreeOfParallelism = 1;<br/>Parallel.For(0, heave_size, parOpts, delegate(int h)<br/><br/>Is the behavior correct?<br/><br/>Josh</p>Tue, 09 Jun 2009 17:00:15 Z2009-06-09T17:00:15Zhttp://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/1161a248-e5bd-444e-9458-36433d4ab566#f443a936-65d5-4a7d-ab0a-544955f14deahttp://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/1161a248-e5bd-444e-9458-36433d4ab566#f443a936-65d5-4a7d-ab0a-544955f14deapltaylorhttp://social.msdn.microsoft.com/Profile/en-US/?user=pltaylorUnexpected Results while saving results to an Array using Parallel.ForIn reverse order,  <div><br/></div> <div>var parOpts = new ParallelOptions();<br/>parOpts.MaxDegreeOfParallelism = 1;<br/>Parallel.For(0, heave_size, parOpts, delegate(int h)</div> <div><br/></div> <div>behaves correctly, albeit sloooooowwly.</div> <div><br/></div> <div>locate vehicle is as follows.</div> <div><br/></div> <div> <div>public static void locate_vehicle(Matrix x, Start_Geom last_geom, Start_Geom start_geom,</div> <div>            Veh_Def veh_def, Track_Def track_def, Component_Params component_params,Results static_results, </div> <div>            Results results_cur, Results results_last, out Start_Geom new_geom, out Results results)</div> <div>        {</div> <div>            new_geom = new Start_Geom();</div> <div>            results = new Results();</div> <div>            int m = x.Columns;</div> <div>            </div> <div>            for (int i = 0; i &lt; m; i++)</div> <div>            {</div> <div>                double front_twist, rear_twist;</div> <div>                //results.index = i;</div> <div>                </div> <div>                new_geom = locate_chassis(x.GetColumn(i), start_geom);</div> <div>                new_geom = locate_steering_system(x.GetColumn(i), new_geom, start_geom, veh_def);</div> <div>                new_geom = locate_front_suspension(x.GetColumn(i), new_geom, start_geom, start_geom, component_params);</div> <div>                locate_front_arb(new_geom, veh_def, component_params, out new_geom, out front_twist);</div> <div>                new_geom = locate_rear_suspension(x.GetColumn(i), new_geom, start_geom, veh_def, component_params);</div> <div>                locate_rear_arb(new_geom, veh_def, component_params, out new_geom, out rear_twist);</div> <div>                calc_results(x.GetColumn(i), new_geom, start_geom, results_cur, results_last, static_results, veh_def, track_def, </div> <div>                    component_params, &quot;setup&quot;, out new_geom, out results, out component_params);</div> <div>            }</div> <div><br/></div> <div><br/></div> <div>        }</div> <div>All the methods this calls are public static....May not be the cleanest way, but it is how I have done it for now.</div> <div><br/></div> <div>All of the variables in locate_vehicle stay constant for the entire loop except for x.  In this particular loop a lot of the variables are actually the same thing just named differently as this is a special case of a much more generic method.</div> <div><br/></div> <div>static_results[i] is a List&lt;Results&gt; static_results = new List&lt;Results&gt;(); defined in my Main Class so it carries over to all methods.</div> </div>Tue, 09 Jun 2009 17:21:06 Z2009-06-09T17:21:06Zhttp://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/1161a248-e5bd-444e-9458-36433d4ab566#a63d7710-3e92-4cf9-ad14-f624314781d7http://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/1161a248-e5bd-444e-9458-36433d4ab566#a63d7710-3e92-4cf9-ad14-f624314781d7Josh Phillipshttp://social.msdn.microsoft.com/Profile/en-US/?user=Josh%20%20PhillipsUnexpected Results while saving results to an Array using Parallel.ForIf P.For with the MaxDOP set to 1 works, it's not a bug with TPL, it must be some shared state somewhere.  The slow performance is expected, though I'd like to understand just how slow sloooooooowly is.  :) <div><br/></div> <div>To be honest, your code has a lot of layers and depth that makes this hard to debug here. Again, I encourage you to follow all your method calls and the parameters you pass in to determine where multiple threads might be touching the same variable(s).</div> <div><br/></div> <div>Hope that helps,</div> <div>Josh</div>Tue, 09 Jun 2009 19:17:45 Z2009-06-09T19:17:45Zhttp://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/1161a248-e5bd-444e-9458-36433d4ab566#e019ada6-7def-4aac-9542-f2b9c0868589http://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/1161a248-e5bd-444e-9458-36433d4ab566#e019ada6-7def-4aac-9542-f2b9c0868589pltaylorhttp://social.msdn.microsoft.com/Profile/en-US/?user=pltaylorUnexpected Results while saving results to an Array using Parallel.ForI guess my only other question then is do you have any suggestions for trying to find those varaibles that are being shared other than looking through it? <div><br/></div> <div>Slow is going from approx 20 secs with a reg for loop to approx 29 secs w/ single threaded p.for()</div>Tue, 09 Jun 2009 19:34:23 Z2009-06-09T19:34:23Zhttp://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/1161a248-e5bd-444e-9458-36433d4ab566#d8ebc302-0ce6-4f9e-93b2-cfd964cab2b2http://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/1161a248-e5bd-444e-9458-36433d4ab566#d8ebc302-0ce6-4f9e-93b2-cfd964cab2b2Josh Phillipshttp://social.msdn.microsoft.com/Profile/en-US/?user=Josh%20%20PhillipsUnexpected Results while saving results to an Array using Parallel.ForHmm.  A 45% increase seems a little much but not unfathomable.  Is this on the Visual 2010 Beta or the old CTP?  <br/><br/>As for suggestions...<br/><br/>There are certainly tools that exist to detect data-races (doing a quick bing-search should give you some ideas) and this is an extremely hot area of research but, unfortunately, there is no definitive tool I can recommend at this point in time.  <br/><br/>Some general suggestions:<br/><br/>1.  Watch out for closures.  What variables are being capture in your anonymous methods or lambdas?  <br/>2.  List the variables that are defined outside of your parallel for loop (or threads) and track how they are used in the loop iterations.  Is there a chance that any of them are touched concurrently?  <br/>3.  Look out for things like loops nested inside of a parallel for loop.  Is there any chance that the nested loops on any two threads might be processing the same index at any given point in time?  <br/><br/>Tue, 09 Jun 2009 23:03:20 Z2009-06-09T23:03:20Zhttp://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/1161a248-e5bd-444e-9458-36433d4ab566#b17ac981-6733-4c1c-8607-4e06a9e8bebbhttp://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/1161a248-e5bd-444e-9458-36433d4ab566#b17ac981-6733-4c1c-8607-4e06a9e8bebbpltaylorhttp://social.msdn.microsoft.com/Profile/en-US/?user=pltaylorUnexpected Results while saving results to an Array using Parallel.ForI'll run it a couple more times tomorrow to double check the performance hit.  It is VS 2010 Team edition. <div><br/></div> <div>I assume that since I define int p, int r, and int s that they are thread safe and should be fine?</div> <div><br/></div> <div>Something tells me I'll just have to take some serious time eliminating possibilities one at a time until all is good.</div>Tue, 09 Jun 2009 23:10:46 Z2009-06-09T23:10:46Zhttp://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/1161a248-e5bd-444e-9458-36433d4ab566#833688ce-54fd-4870-9859-24358d3865f7http://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/1161a248-e5bd-444e-9458-36433d4ab566#833688ce-54fd-4870-9859-24358d3865f7Josh Phillipshttp://social.msdn.microsoft.com/Profile/en-US/?user=Josh%20%20PhillipsUnexpected Results while saving results to an Array using Parallel.ForYep. p, r, and s, should be fine, except in the case I described before where the same index may be processed concurrently with another thread, i.e. Thread A is processing h = 10, p = 0, r = 0, s = 1 and Thread B is processing h = 11, p = 1, r = 3, s = 1.  In this case A and B will be racing to set steering[1].  <br/><br/>Unfortunately, the story around data races is sad.  That's definitely good feedback for us to have though.<br/><br/>Thanks!<br/><br/>JoshTue, 09 Jun 2009 23:46:44 Z2009-06-09T23:46:44Zhttp://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/1161a248-e5bd-444e-9458-36433d4ab566#eee2d52a-b8f3-40cd-9abc-64b2190af214http://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/1161a248-e5bd-444e-9458-36433d4ab566#eee2d52a-b8f3-40cd-9abc-64b2190af214pltaylorhttp://social.msdn.microsoft.com/Profile/en-US/?user=pltaylorUnexpected Results while saving results to an Array using Parallel.ForThat would actually explain some of the behavior I was witnessing this afternoon.  How can I set up these loops so that it doesn't race?  Or is multiple nested for loops pretty much a no-no. <div>This may seem a little goofey but could I put multiple p.for loops to delegate the variables individually?  Would that prevent the race?  I may be going down the wrong path.</div>Tue, 09 Jun 2009 23:53:22 Z2009-06-10T00:10:24Zhttp://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/1161a248-e5bd-444e-9458-36433d4ab566#9f600797-a87d-45c9-8429-b9ec0dfaf57dhttp://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/1161a248-e5bd-444e-9458-36433d4ab566#9f600797-a87d-45c9-8429-b9ec0dfaf57dJosh Phillipshttp://social.msdn.microsoft.com/Profile/en-US/?user=Josh%20%20PhillipsUnexpected Results while saving results to an Array using Parallel.ForUnfortunately, nesting p.for loops won't help here because those nested loops can still process the same index simultaneously.   <div><br/></div> <div>Why is it that you store these values and overwrite them multiple times?  Do you use them anywhere?  Are you just storing the last calculated value?  Where are the XX_min and XX_inc variables define?  Are they invariants or do they actually change?  It looks to me that unless those are static variables and updated inside one of the methods you call in your loop, they are invariants.  If they are in fact invariants then the XX_val variables only need to be calculated in 4 non-nested loops rather than a nested loop.  That would reduce a lot of unnecessary computation from your nested for loop.  Essentially, you'd reduce the complexity of filling the steering, rolls, pitches, and heaves arrays from O(n^4) to O(n) -- a big savings.  </div> <div><br/></div> <div>That said, if those values really are invariants than the data-race there is benign (except for performance) as each cell is just being updated with the same value over and over.  </div> <div><br/></div> <div>If they are not invariants and they truly do change, then you must just be overwriting each cell with the most recent value calculated.  If those values were floats, the races would still be benign as you'd have avoided <a href="http://www.bluebytesoftware.com/blog/2006/02/08/ThreadsafetyTornReadsAndTheLike.aspx">torn reads</a>.  Because they are doubles, if they are not invariants, you will need to create an object to lock on for each cell. :(</div> <div><br/></div> <div>I don't see those arrays actually be read from in your loop so my guess is that they can be calculated outside of your loops and therefore, present no concurrency issue.</div> <div><br/></div> <div>Josh</div>Wed, 10 Jun 2009 04:56:32 Z2009-06-10T04:56:32Zhttp://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/1161a248-e5bd-444e-9458-36433d4ab566#b3c8461b-0089-40d2-a869-1d78518554achttp://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/1161a248-e5bd-444e-9458-36433d4ab566#b3c8461b-0089-40d2-a869-1d78518554acJosh Phillipshttp://social.msdn.microsoft.com/Profile/en-US/?user=Josh%20%20PhillipsUnexpected Results while saving results to an Array using Parallel.For<blockquote>In reverse order,  <div><br/></div> <div>var parOpts = new ParallelOptions();<br/>parOpts.MaxDegreeOfParallelism = 1;<br/>Parallel.For(0, heave_size, parOpts, delegate(int h)</div> <div><br/></div> <div>behaves correctly, albeit sloooooowwly.</div> <div><br/></div> <div>locate vehicle is as follows.</div> <div><br/></div> <div> <div>public static void locate_vehicle(Matrix x, Start_Geom last_geom, Start_Geom start_geom,</div> <div>            Veh_Def veh_def, Track_Def track_def, Component_Params component_params,Results static_results, </div> <div>            Results results_cur, Results results_last, out Start_Geom new_geom, out Results results)</div> <div>        {</div> <div>            new_geom = new Start_Geom();</div> <div>            results = new Results();</div> <div>            int m = x.Columns;</div> <div>            </div> <div>            for (int i = 0; i &lt; m; i++)</div> <div>            {</div> <div>                double front_twist, rear_twist;</div> <div>                //results.index = i;</div> <div>                </div> <div>                new_geom = locate_chassis(x.GetColumn(i), start_geom);</div> <div>                new_geom = locate_steering_system(x.GetColumn(i), new_geom, start_geom, veh_def);</div> <div>                new_geom = locate_front_suspension(x.GetColumn(i), new_geom, start_geom, start_geom, component_params);</div> <div>                locate_front_arb(new_geom, veh_def, component_params, out new_geom, out front_twist);</div> <div>                new_geom = locate_rear_suspension(x.GetColumn(i), new_geom, start_geom, veh_def, component_params);</div> <div>                locate_rear_arb(new_geom, veh_def, component_params, out new_geom, out rear_twist);</div> <div>                calc_results(x.GetColumn(i), new_geom, start_geom, results_cur, results_last, static_results, veh_def, track_def, </div> <div>                    component_params, &quot;setup&quot;, out new_geom, out results, out component_params);</div> <div>            }</div> <div><br/></div> <div><br/></div> <div>        }</div> <div>All the methods this calls are public static....May not be the cleanest way, but it is how I have done it for now.</div> <div><br/></div> <div>All of the variables in locate_vehicle stay constant for the entire loop except for x.  In this particular loop a lot of the variables are actually the same thing just named differently as this is a special case of a much more generic method.</div> <div><br/></div> <div>static_results[i] is a List&lt;Results&gt; static_results = new List&lt;Results&gt;(); defined in my Main Class so it carries over to all methods.</div> </div> </blockquote> BTW, I just re-read this.  I don't know where 'i' is defined but I would look closely at static_results.  Since it's a static variable shared by all threads, it worries me.  Also, if all the methods in here are static or take in out params (and it seems they do both) that's bothersome.  Wed, 10 Jun 2009 05:06:46 Z2009-06-10T05:06:46Zhttp://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/1161a248-e5bd-444e-9458-36433d4ab566#500292be-d3a9-4f5b-99f5-5d47aa631a44http://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/1161a248-e5bd-444e-9458-36433d4ab566#500292be-d3a9-4f5b-99f5-5d47aa631a44pltaylorhttp://social.msdn.microsoft.com/Profile/en-US/?user=pltaylorUnexpected Results while saving results to an Array using Parallel.ForCan you elaborate on  <div>&quot;Also, if all the methods in here are static or take in out params (and it seems they do both) that's bothersome. &quot;</div> <div>What about static methods or out params are worrisome?  It would take some interesting workarounds not to use the out params, but the static methods would be an easy change, that I have experimented unsuccssefully with but not going through and getting rid of all the staitc methods.  In my latest round of debugging I think I have narrowed it down to the </div> <div> <div>calc_results(x.GetColumn(i), new_geom, start_geom, results_cur, results_last, static_results, veh_def, track_def, </div> <div>                    component_params, &quot;setup&quot;, out new_geom, out results, out component_params);</div> <div>method.  Is there anything particularly troublesome as to how this function is called?  The actual method is a lot of code that contains more methods itself, so I'm not going to bother posting it.</div> </div>Tue, 16 Jun 2009 20:05:47 Z2009-06-16T20:05:47Zhttp://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/1161a248-e5bd-444e-9458-36433d4ab566#7ae6d408-d751-488e-aa2a-a57939f16749http://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/1161a248-e5bd-444e-9458-36433d4ab566#7ae6d408-d751-488e-aa2a-a57939f16749Josh Phillipshttp://social.msdn.microsoft.com/Profile/en-US/?user=Josh%20%20PhillipsUnexpected Results while saving results to an Array using Parallel.For<p>Sorry for not being clear.  I meant methods that mutate static variables.</p> <p>For example,</p> <p>class MyClass<br/>{<br/>    private static A m_state;</p> <p>    public void Foo( out B myParam )<br/>    {<br/>         ...<br/>         m_state = ...;<br/>         ...<br/>         myParam = ...;<br/>    }<br/>}</p> <p>void Main()<br/>{<br/>    ...<br/>    B data = ...;<br/>    ...<br/>    ParallelFor( 0, N, (i) =&gt;<br/>    {<br/>         ...<br/>         var x = new MyClass();<br/>         ...<br/>         x.Foo( out data );<br/>    });<br/>}</p> <p>is bad because it's modifying shared state through both a static variable and a variable that is defined outside of the P.For.</p> <p>So, looking at calc_results, are there any static variables being written to inside?  Are new_geom, results, or component_params defined outside of the P.For?</p> <p>Josh</p>Thu, 18 Jun 2009 17:16:24 Z2009-06-18T17:16:24Zhttp://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/1161a248-e5bd-444e-9458-36433d4ab566#1e190e54-353f-4113-a4b9-fbc5ac71cc34http://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/1161a248-e5bd-444e-9458-36433d4ab566#1e190e54-353f-4113-a4b9-fbc5ac71cc34pltaylorhttp://social.msdn.microsoft.com/Profile/en-US/?user=pltaylorUnexpected Results while saving results to an Array using Parallel.ForI have been able to cut down on alot of the racing but it appears that I am still getting some about 50% of the time.  It definetly appears that the out variables are my biggest problems and playing with them has moved me forward.  For instance changing  <div> <div>public static void  calc_results(dnAnalytics.LinearAlgebra.Vector x, Start_Geom new_geom_in ,Start_Geom start_geom,</div> <div>           </div> <div> Results results , Results results_last, Results static_results, Veh_Def veh_def, Track_Def track_def, Component_Params_in component_params,</div> <div>                    string mode, out Start_Geom new_geom, out Results results, out Component_Params component_params)</div> <div>        {</div> <div><br/></div> <div>            <span style="white-space:pre"> </span>new_geom = new Start_Geom(new_geom_in);</div> <div><span style="white-space:pre"> </span>results=new Results(results_cur);</div> <div>            <span style="white-space:pre"> </span>component_params=new Component_Params(component_params_in);</div> <div>...... lots of calculations here</div> <div><br/></div> <div><span style="white-space:pre"> </span>}<br/></div> <div><br/></div> <div>to</div> <div><br/></div> <div> <div>public static void  calc_results(dnAnalytics.LinearAlgebra.Vector x, Start_Geom new_geom ,Start_Geom start_geom,</div> <div>            Results results , Results results_last, Results static_results, Veh_Def veh_def, Track_Def track_def, Component_Params component_params,</div> <div>                    string mode, out Start_Geom new_geom_out, out Results results_out, out Component_Params component_params_out)</div> <div>        {</div> <div><br/></div> <div>           ....lots of calculations.....</div> <div><br/></div> <div> <div>            new_geom_out = new Start_Geom(new_geom);</div> <div>            results_out = new Results(results);</div> <div>            component_params_out = new Component_Params(component_params);</div> <div>}</div> <div>helped significantly.  Obviously I haven't actually solved the probelm, just lessoned the time that there can be a race.  How can I eliminate this race?  Is there a different way to create methods that have multiple variables coming out of the left side that I am just not familiar with?</div> <div>Once again I appreciate the help</div> <div>-perry</div> <div><br/></div> </div> </div> </div>Fri, 19 Jun 2009 17:44:34 Z2009-06-19T17:44:34Zhttp://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/1161a248-e5bd-444e-9458-36433d4ab566#dc4b5d93-af1c-41ea-a88e-e1075cce265ehttp://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/1161a248-e5bd-444e-9458-36433d4ab566#dc4b5d93-af1c-41ea-a88e-e1075cce265eJosh Phillipshttp://social.msdn.microsoft.com/Profile/en-US/?user=Josh%20%20PhillipsUnexpected Results while saving results to an Array using Parallel.For<p>Hi Perry,<br/><br/>I'm glad you're making progress!  It's perfectly acceptable to use out params, you just have to be really careful with how you use them.  You really don't want to pass in paramaters that are defined outside of the the thread boundary (in this case outside of the P.For) unless you're 100% positive that those variables are only being read from and never being written to concurrently.  In fact, make use of immutable types as much as possible -- there's lots o' good reading here: <a href="http://weblogs.asp.net/bleroy/archive/2008/01/16/immutability-in-c.aspx">http://weblogs.asp.net/bleroy/archive/2008/01/16/immutability-in-c.aspx</a>.  <br/><br/>Josh</p>Mon, 22 Jun 2009 16:29:06 Z2009-06-22T16:29:06Zhttp://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/1161a248-e5bd-444e-9458-36433d4ab566#86db9213-62f2-4305-aac0-4f4509bf7486http://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/1161a248-e5bd-444e-9458-36433d4ab566#86db9213-62f2-4305-aac0-4f4509bf7486pltaylorhttp://social.msdn.microsoft.com/Profile/en-US/?user=pltaylorUnexpected Results while saving results to an Array using Parallel.ForJosh- <div><span style="white-space:pre"> </span>I've continued to make progress, but I am confused as to my latest progress.  What is the difference between these two pieces of code inside a p.for loop?<br/></div> <div><br/></div> <div> <div>                            ...</div> <div><span style="white-space:pre"> </span>    Results calc_results_temp = new Results();</div> <div><br/></div> <div>                            calc_results_temp = kinematics.locate_vehicle(x, start_geom, vehicle, component_params);</div> <div><br/></div> <div>                            calc_results[h, p, r, s] = new Results(calc_results_temp);</div> <div><span style="white-space:pre"> </span>            ....</div> <div><br/></div> <div>and</div> <div><span style="white-space:pre"> </span>    ...<span style="white-space:pre"> </span><br/></div> <div>                            calc_results[h, p, r, s]= kinematics.locate_vehicle(x, start_geom, vehicle, component_params);</div> <div><span style="white-space:pre"> </span>  <span style="white-space:pre"> </span>    ...<br/></div> <div><br/></div> <div>obviously the second is a lot better from a performance, cleanliness, and overal coding reason but I would have thought the result would have beeen identical.  It turns out that if I run the first bit of code I get some random results.  If I use the second bit of code all is good and I get predidictable results.</div> </div>Mon, 22 Jun 2009 16:33:49 Z2009-06-22T16:33:49Zhttp://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/1161a248-e5bd-444e-9458-36433d4ab566#e774fac1-2842-446d-a9b8-6b77ff608005http://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/1161a248-e5bd-444e-9458-36433d4ab566#e774fac1-2842-446d-a9b8-6b77ff608005Josh Phillipshttp://social.msdn.microsoft.com/Profile/en-US/?user=Josh%20%20PhillipsUnexpected Results while saving results to an Array using Parallel.ForHey Perry,<br/><br/>Is there any way to send me a compilable repro?  That would help a lot.<br/><br/>Thanks,<br/>JoshMon, 22 Jun 2009 20:24:41 Z2009-06-22T20:24:41Z