【发布时间】:2017-07-01 08:48:23
【问题描述】:
我正在将两个整数数组相乘以获得大数。我基本上乘以大约 20 位数字,但对于更多数字,它的行为不稳定。我在 CPU 和内核上有一些代码用于测试,内核在每次工作后给我不同的数字。问题可能是障碍或诸如互斥锁之类的东西,但我对此并不感兴趣。我该如何解决这种不稳定?这是代码
kernel void multiply(global int* A,
const int M,
global int* B,
const int N,
global int* C){
const int globalRow = get_global_id(0); // Row ID of C (0..M)
int globalCol,i;
// Compute a single element (loop over K)
for (globalCol=0; globalCol<N; globalCol++) {
int val=A[globalRow]*B[globalCol];
printf("Row is %d , Col is %d \n",globalRow,globalCol);
//C[globalCol + globalRow +1]+=val/10;
C[globalCol + globalRow]+=val%10;
C[globalCol+1+globalRow]+=val/10;
}
int flag=1;
while (flag) {
flag=0;
for (i=M+N-1 ; i>=0 ; i--) {
if (C[i]>9) {
C[i+1]+=C[i]/10;
C[i]=C[i]%10;
flag=1;
}
}
}
}
代码上的标志正在移动。这听起来像是访问元素的问题,但我不知道如何解决。
【问题讨论】: