Parallel.for Loop - result varies for each click .. why does this happen?

Sherm

New member
Joined
Aug 15, 2015
Messages
2
Programming Experience
Beginner
Following parallel.for loop uses data of a bitarray which is 300000 bits in length. and the data is fixed not to change. So the produced results "Count_of_Found_Pattern1" must be same no matter how many times I execute the function "check"
But, the issue is the values of "Count_of_Found_Pattern1" & "Count_of_Found_Pattern2" produce different values every time I execute the function "check" .. what have I done wrong?
when I check it using small amount of bits (about 16 bits instead of 300000) it produces good results. But, when the bitarray length is lengthier, it produces a total mess.
For Example:
1st execution --> Count_of_Found_Pattern1 = 150526 , Count_of_Found_Pattern2 = 97855
2nd execution --> Count_of_Found_Pattern1 = 45855 , Count_of_Found_Pattern2 = 187562
Regards!


 Private Function check()

        Dim Count_of_Found_Pattern1 As Int64 = 0
        Dim Count_of_Found_Pattern2 As Int64 = 0
        Dim Current_Position1, Current_Position2 As Int64

        Parallel.For(0, lastbitarrayover2, Sub(countbits, loopstate)

                                               If BitArray(Current_Position1) = False And BitArray(Current_Position2) = True Then
                                                   Count_of_Found_Pattern1 = Count_of_Found_Pattern1 + 1
                                               End If

                                               If BitArray(Current_Position1) = True And BitArray(Current_Position2) = False Then
                                                   Count_of_Found_Pattern2 = Count_of_Found_Pattern2 + 1
                                               End If

                                               Current_Position1 = Current_Position1 + 2
                                               Current_Position2 = Current_Position2 + 2
                                               Number_of_Completed_Iterations = Number_of_Completed_Iterations + 1

                                           End Sub)

    End Function
 
Last edited:
The whole point of a parallel For loop is to execute multiple iterations in parallel. How, then, can you possibly use a single variable for the index of the array element you want to examine? The method that gets execute for each index receives the index, so you're supposed to use that inside the method. In your case that's `countbits`, but you're not using that anywhere.
 
Back
Top