我正在尝试在 C 中使用 MPI 进行矩阵乘法,我们必须做一个顺序版本和一个并行版本。我的并行版本没有给出正确的答案,我不知道为什么。我认为我没有向流程发送正确的通信,但我不能确定。教授只是查看了不同的发送/接收/收集等消息,但并没有真正深入细节......我看过很多不同的例子,但没有一个完整的,也没有使用分散/收集的。如果有人可以查看我的代码并告诉我是否有任何内容出现在他们身上,我将不胜感激。我很确定我的问题出在分散/收集消息或 c 矩阵的实际计算中。
#define N 512
#include <stdio.h>
#include <math.h>
#include <sys/time.h>
#include <stdlib.h>
#include <stddef.h>
#include "mpi.h"
print_results(char *prompt, float a[N][N]);
int main(int argc, char *argv[])
{
int i, j, k, rank, size, tag = 99, blksz, sum = 0;
float a[N][N], b[N][N], c[N][N];
char *usage = "Usage: %s file\n";
FILE *fd;
double elapsed_time, start_time, end_time;
struct timeval tv1, tv2;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if (argc < 2) {
fprintf (stderr, usage, argv[0]);
return -1;
}
if ((fd = fopen (argv[1], "r")) == NULL) {
fprintf (stderr, "%s: Cannot open file %s for reading.\n",
argv[0], argv[1]);
fprintf (stderr, usage, argv[0]);
return -1;
}
for (i = 0; i < N; i++)
for (j = 0; j < N; j++)
fscanf (fd, "%f", &a[i][j]);
for (i = 0; i < N; i++)
for (j = 0; j < N; j++)
fscanf (fd, "%f", &b[i][j]);
MPI_Barrier(MPI_COMM_WORLD);
gettimeofday(&tv1, NULL);
MPI_Scatter(a, N*N/size, MPI_INT, a, N*N/size, MPI_INT, 0,
MPI_COMM_WORLD);
MPI_Bcast(b, N*N, MPI_INT, 0, MPI_COMM_WORLD);
if (rank != 0) {
for (i = 0; i < N; i++)
{
for (j = 0; j < N; j++)
{
for (k = 0; k < N; k++)
{
sum = sum + a[i][k] * b[k][j];
}
c[i][j] = sum;
sum = 0;
}
}
}
MPI_Gather(c, N*N/size, MPI_INT, c, N*N/size, MPI_INT, 0,
MPI_COMM_WORLD);
MPI_Finalize();
gettimeofday(&tv2, NULL);
elapsed_time = (tv2.tv_sec - tv1.tv_sec) + ((tv2.tv_usec - tv1.tv_usec)/1000000.0);
printf ("elapsed_time=\t%lf (seconds)\n", elapsed_time);
print_results("C = ", c);
}
print_results(char *prompt, float a[N][N])
{
int i, j;
printf ("\n\n%s\n", prompt);
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
printf(" %.2f", a[i][j]);
}
printf ("\n");
}
printf ("\n\n");
}
更新部分代码:
for (i=0;i<size; i++)
{
if (rank == i)
{
for (i = rank*(N/size); i < (rank*(N/size)+(N/size)); i++)
{
for (j = rank*(N/size); j < (rank*(N/size)+(N/size)); j++)
{
for (k = rank*N; k < rank*N+N; k++)
{
sum = sum + a[i][k] * b[k][j];
}
c[i][j] = sum;
sum = 0;
}
}
}
}