Parallel Programming-Quiz1
#define SIZE 100
float waveData[SIZE];
void sequential() {
for(int i = 0; i < SIZE; i++) {
// Average the current, left, and right values into the current cell
// i - 1 + SIZE % SIZE ensures that the returned index is always positive
waveData[i] = (waveData[i - 1 + SIZE % SIZE] + waveData[i] + waveData[i + 1 % SIZE]) / 3;
}
}
void parallel(int myIndex, int worldSize) {
int blockSize = SIZE / worldSize;
for (int i = myIndex * blockSize; i < (myIndex + 1) * blockSize; i++) {
waveData[i] = (waveData[i - 1 + SIZE % SIZE] + waveData[i] + waveData[i+1 % SIZE]) / 3;
}
}
The parallel implementation is called on two different threads with the parameters parallel(0, 2) and parallel(1,2). Assume that they are sharing memory in the waveData array.
两次的参数分别是(0,2), (1,2)
Q1:Will running the sequential program twice produce the same output?
Q2:Will running the parallel program twice produce the same output?
Q3:Will the sequential program output the same information as the parallel program?
Q1:Yes
Q2:Maybe
Q3:Maybe
Q1不用多说
Q2因为function的计算会涉及到 “交叉区域”,在该区域会因不同的thread执行的先后顺序差异产生不同的结果。
Q3因为有可能para就是很完美的先算完第一部分,再算第二部分,那么结果就会一样。