Parallel Programming---课程练习Q1

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就是很完美的先算完第一部分,再算第二部分,那么结果就会一样。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值