Note that there are some explanatory texts on larger screens.

plurals
  1. POParallel.for causes different results
    primarykey
    data
    text
    <p>I am currently trying to improve a C# project I am working on. Specifically, my goal is to parallelize some operations to reduce processing time. I am starting with small snippets just to get the hang of it. The following code (not parallel) works correctly (as expected)</p> <pre><code>for (int i = 0; i &lt; M; i++) { double d; try { d = Double.Parse(lData[i]); } catch (Exception) { throw new Exception("Wrong formatting on data number " + (i + 1) + " on line " + (lCount + 1)); } sg[lCount % N][i] = d; } </code></pre> <p>By using the following (parallel) code I would expect to obtain the exact same results, but that is not the case.</p> <pre><code>Parallel.For(0, M, i =&gt; { double d; try { d = Double.Parse(lData[i]); } catch (Exception) { throw new Exception("Wrong formatting on data number " + (i + 1) + " on line " + (lCount + 1)); } sg[lCount % N][i] = d; }); </code></pre> <p>The part of the program these snippets are from reads data from a file, one line at a time. Each line is a sequence of comma-separated double precision numbers, that I put in the vector lData[] using String.Split(). Every M lines, the data sequence starts over with a new data frame (hence the <code>% M</code> in the element index when i assign the values).</p> <p>It is my understanding (clearly wrong) that by putting the code from the (serial) for-loop in the third parameter of <code>Parallel.For</code> I parallelize its execution. This shouldn't change the results. Is the problem in the fact that the threads are all accessing to lCount and M? Should I make thread-local copies?</p> <p>Thanks.</p> <p>(since I'm new I am not allowed to create the <code>Parallel.For</code> tag)</p> <p>EDIT: I ran some more tests. Basically I looked at an output earlier in the code than what I did before. It would appear that the parallel version of my code does not fill the <code>sg[][]</code> array entirely. Rather, some values are left to their defaults (0, in my case).</p> <p>EDIT 2 (to answer some of the comments): <code>lData[]</code> is a <code>string[]</code> obtained by using <code>string.Split()</code>. The original string I am splitting is read from my data files. I wrote the code that generates them, so they are generally well-formatted (I still used the <code>try-catch</code> construct out of habit). Just before the for-loop (wither parallel or serial) I check to verify that <code>lData[]</code> has the correct number of values (M). If it doesn't, I throw an exception that prevents the program from reaching the for-loop in question. <code>sg[][]</code> is a N by M array of type <code>double</code> (there was a typo in the snippets, now corrected; In my original code this error was not present). After I read N lines from the file the array <code>sg[][]</code> contains a whole data set. After the for-loop (either parallel or serial) there is a portion of come that looks like this: lCount++; //counting the lines I have already read if((lCount % N) == 0) { //do things with sg[][] //reset sg[][] } So, I am on purpose overwriting all lines of <code>sg[][]</code>. The for-loop's whole purpose is to update the values in <code>sg[][]</code>.</p>
    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.
 

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