我的问题是,当我进行乘法运算时,它仅将矩阵的第一行与向量的第一个元素相乘,而下一个元素使它们为零,因此结果向量给出了错误的结果。
using namespace std;
#define N 100
#define F 3
#define X 7
__global__ void matvec(int *MAT, int *VEC, int *SOL) {
int bx = blockIdx.x;
int tx = threadIdx.x;
int i = 32*bx+tx;
for (int j = 0; j < X; j++) {
SOL[i] = ((MAT[i * X + j] * VEC[j]) +SOL[i]) % 2;
}
}
int main () {
int i, j;
int MAT[N][N], VEC[N], SOL[N];
int *MAT_dev, *VEC_dev, *SOL_dev;
size_t nBytes = X * X * sizeof(int);
cout << "\t- - - - - MATRIX - - - - -\n\n";
for (i = 0; i < X; i++) {
for (j = 0; j < X; j++) {
cout << "Element [" << i << "][" << j << "]: ";
cin >> MAT[i][j];
}
}
cout << endl << endl;
for (i = 0; i < X; i++) {
for (j = 0; j < X; j++) {
cout << MAT[i][j] << " ";
if (j == (X - 1))
cout << endl;
}
}
cout << endl << endl;
cout << "\t- - - - - VECTOR - - - - -\n\n";
for (i = 0; i < X; i++) {
cout << "Element [" << i << "]: ";
cin >> VEC[i];
}
cout << endl << endl;
for (i = 0; i < X; i++) {
cout << VEC[i] << " ";
}
cout << endl << endl;
cudaMalloc((void**)&MAT_dev, nBytes);
cudaMalloc((void**)&VEC_dev, nBytes);
cudaMalloc((void**)&SOL_dev, nBytes);
cudaMemcpy(MAT_dev, MAT, nBytes, cudaMemcpyHostToDevice);
cudaMemcpy(VEC_dev, VEC, nBytes, cudaMemcpyHostToDevice);
dim3 dimBlock(X,X);
dim3 dimGrid(1,1);
matvec<<<dimGrid,dimBlock>>>(MAT_dev, VEC_dev, SOL_dev);
cudaMemcpy(SOL, SOL_dev, nBytes, cudaMemcpyDeviceToHost);
cout << "\t- - - - - RESULT - - - - -\n\n";
for (i = 0; i < X; i++)
{
cout << SOL[i] << " ";
}
cout << endl << endl;
cudaFree(MAT_dev);
cudaFree(VEC_dev);
cudaFree(SOL_dev);
system("PAUSE");
return 0;
}
谢谢您的帮助