Post your solutions to the Feb 14 In-Class Exercise to this Thread.
Best, Chris
example latex of matrices
[[a,b],[c,d]]
Matrix, vector, and situation with race condition:
[[2,2],[3,3]][[4],[4]]
When i == 0, if the j=0 strand and j=1 strand both read the value before either writes, there will be a race condition.
Matrix, vector, without race condition:
[[0,0],[0,0]][[0],[0]]
The operations will always have zeroes in the result vector no matter what order the operations occur in.
If we have a matrix [[a,b],[c,d]], if any of the values are different to each other, there may be a race condition.
In the example below, we will face a race condition:
[[1,2],[3,3]] * [1,1]
If we have a matrix [[a,b],[c,d]], a, b, c and d should all be the same value. In this case, there would not be a race condition
(1) has race condition.
a = [[1,3],[4,6]]
x = [[4],[6]]
a * x would have race condition,
case: when 2 strands read the y[i] together and then calc y[i] independently and write to y[i] with code y[i] = y[i] + a[i][j] * x[j] then y[i] would get corrupted due to race condition. e.g. j= 1 and j=2 strands start together
(2) does not have race condition
a = [[0,0],[0,0]]
x = [[0],[0]]
as all values are zeros so product and sum does not matter and race condition wont affect.
(Edited: 2018-02-14)[[1 2 3] [1 2 3] [1 2 3]] x = [1 2 3][[1 0 0][0 2 0][0 0 3]] x = [1 2 3] Since all elements are 0 except for one, the parallel sum would not create a race condition.Suppose we have a matrix like this:
A=[[a, b],[c, d]]
In the first parallel loop, suppose that i=1 comes first. then it is possible that the 2nd loop will attempt to read A[i][j] at same time when j=0 and j=1 before writing to y[i], hence race condition is possible.
There are cases where race conditions are not possible, only if all a[i][j] values are same for all i and j.
(Edited: 2018-02-14)<nowiki> Name: Kunal Deshmukh
Below matrix will result in race condition:</nowiki>
[[1,3],[4,2]],[[2],[9]]<nowiki>
since inner loop is also paralalized, y[i] = y[i] + a[i][j] * x[j] will be calculated by by multiple processes and since it involves updateion in the value of y[i], and processes many not run sequentialy, race condition can occur.
condition to avoid race condition:
Same element in a matrix.
eg. </nowiki>[[1,1],[1,1]] , [[0],[0]]
[[1,3],[3,4]] * [[2],[5]] , this can give race condition, if each thread i reads the value of y[i] at the same time.
The situation where no race condition will occur:
[[0,0],[0,0]] * [[1],[1]]
For,
[[1,2,3],[4,5,6],[7,8,9]][[4],[5],[6]]
the race condition would occur when both j=0 and j=1 occur at the same time.
The fix for this would be to have the vector with all 0s.
(Edited: 2018-02-14)