I just write the following code:
--------------------------------
int main() {
        int i = 0;
        int s = 0;

#pragma omp parallel for
        for(i = 0; i < 10001; i++) {
                /* uncommenting the follow line, it works */
                printf("thread %d : %d\n", omp_get_thread_num(), s = s + i);

                /* uncommenting the follow line, it gets wrong */
                /* s += i; */

                /* but when uncommenting the follow line, the above line works
*/
                /* printf("%d\n", s); */

        }
        printf("%d\n", s);

}
------------------------------------------------
The above runs just as I described in the comment.
when I said "it gets worng", I mean that, when I run it many time, it returns
different result, but every one is wrong.
such as:
----------------------------------------
 $ ./another
31653559
lfs_625:wizard | Sat 12 Apr 2008 11:46:05 AM GMT | ~/programe/c/openMP
 $ ./another
50005000
lfs_625:wizard | Sat 12 Apr 2008 11:46:06 AM GMT | ~/programe/c/openMP
 $ ./another
24946634
lfs_625:wizard | Sat 12 Apr 2008 11:46:07 AM GMT | ~/programe/c/openMP
 $ ./another
19633725
lfs_625:wizard | Sat 12 Apr 2008 11:46:07 AM GMT | ~/programe/c/openMP
 $ ./another
49257506
lfs_625:wizard | Sat 12 Apr 2008 11:46:08 AM GMT | ~/programe/c/openMP
----------------------------------------


-- 
           Summary: "omp parallel for"  wrong result
           Product: gcc
           Version: 4.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libgomp
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: whitelilis at gmail dot com
  GCC host triplet: "omp parallel for" gets the wrong result


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=35912

Reply via email to