MPI 矩阵向量乘法返回有时正确有时奇怪的值

2024-04-18

我有以下代码:

    //Start MPI...
MPI_Init(&argc, &argv);

int size = atoi(argv[1]);
int delta = 10;
int rnk;
int p;
int root = 0;

MPI_Status mystatus;
MPI_Comm_rank(MPI_COMM_WORLD, &rnk);
MPI_Comm_size(MPI_COMM_WORLD, &p);

//Checking compatibility of size and number of processors
assert(size % p == 0);

//Initialize vector...
double *vector = NULL;
vector = malloc(size*sizeof(double));
double *matrix = NULL;

//Rank 0 -----------------------------------
if (rnk == 0) {

    //Initialize vector...
    srand(1);
    for (int i = 0; i < size; i++) {
        vector[i] = rand() % delta + 1;
    }
    printf("Initial vector:");
    print_vector(vector, size);

    //Initialize matrix...
    matrix = malloc(size*size*sizeof(double));
    srand(2);
    for (int i = 0; i < (size*size); i++) {
        matrix[i] = rand() % delta + 1;
    }

    //Print matrix...
    printf("Initial matrix:");
    print_flat_matrix(matrix, size);

}

//Calculating chunk_size...
int chunk_size = size/p;

//Initialize submatrix..
double *submatrix = malloc(size*chunk_size*sizeof(double));

//Initialize result vector...
double *result = malloc(chunk_size*sizeof(double));

//Broadcasting vector...
MPI_Bcast(vector, size, MPI_DOUBLE, root, MPI_COMM_WORLD);
MPI_Barrier(MPI_COMM_WORLD);

//Scattering matrix...
MPI_Scatter(matrix, (size*chunk_size), MPI_DOUBLE, submatrix, (size*chunk_size), MPI_DOUBLE, root, MPI_COMM_WORLD);
MPI_Barrier(MPI_COMM_WORLD);

printf("I am rank %d and first element of my vector is: %f and of my matrix1: %f/matrix2: %f/matrix3: %f/matrix4: %f\n", rnk, vector[0], submatrix[0], submatrix[1], submatrix[2], submatrix[3]);

//Calculating...
for (int i = 0; i < chunk_size; i++) {
    for (int j = 0; j < size; j++) {
        result[i] += (submatrix[(i*size)+j] * vector[j]);
        printf("Rank %d; current result: %f, ", rnk, result[i]);
    }
    printf("\n");
    printf("Rank %d; result: %f...\n", rnk, result[i]);
}

printf("Rank: %d; first result: %f\n", rnk, result[0]);


double *final_result = NULL;
//Rank 0 -----------------------------------
if (rnk == 0) {
    final_result = malloc(size*sizeof(double));
}

//Gather...
MPI_Gather(result, chunk_size, MPI_DOUBLE, final_result, chunk_size, MPI_DOUBLE, root, MPI_COMM_WORLD);


//Rank 0 -----------------------------------
if (rnk == 0) {
    printf("Final result:\n");
    print_vector(final_result, size);

    free(matrix);
    free(final_result);
}

free(submatrix);
free(result);
free(vector);

MPI_Finalize();

当我运行该程序时,它运行完成而没有错误,但我最后打印的值并不总是正确的。有时我收到具有正确输出的向量,有时它部分正确,有时完全错误。错误的值要么是错误的 2 的值,要么是一些非常长的无用数字序列(在我看来,必须存在错误的内存访问,但我找不到任何东西,而且很奇怪,因为有时作品)。

我也总是选择我的大小,以便它适合 mpi 创建的进程数量。 mpi 在我的机器上创建了 4 个进程(经过测试和检查的值),因此为了测试我的算法,我始终选择 4 作为大小值。对于更大的尺寸也会出现同样的问题。

期待您的帮助和意见,提前感谢!

PS:我是C组


你熟悉valgrind吗?它会立即将您的注意力吸引到有问题的线路上。

您的问题似乎是这一行:

result[i] += (submatrix[(i*size)+j] * vector[j]);

result[] 最初是什么?它被从堆里拉了出来。有时,如果你幸运的话,它会为零。不要指望 C 能带来好运。

初始化数组的方法有很多种。以下是一些方法,按最有可能优化的顺序列出:

使用 calloc 分配 result[]:

double *result = calloc(chunk_size , sizeof(double));

或者,使用 memset 初始化数组:

double *result = malloc(chunk_size *sizeof(double));
memset(result, 0, chunk_size *sizeof(double));

或者,可以循环数组

for (i=0; i < chunk_size; i++)
    result[i] = 0.0
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

MPI 矩阵向量乘法返回有时正确有时奇怪的值 的相关文章

随机推荐