首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >OpenMPI矩阵乘法

OpenMPI矩阵乘法
EN

Stack Overflow用户
提问于 2012-09-22 14:22:05
回答 1查看 3.5K关注 0票数 3

我正在用C语言学习OpenMPI,我在用这个程序做矩阵乘法时遇到了一点麻烦,结果是错误的。程序可以编译,但我觉得我的矩阵乘法算法在某个地方出错了。

我解决这个问题的方法是使用MPI_Scatter对矩阵A进行散布,然后转置矩阵B,然后对MPI_Scatter矩阵B进行散布,一旦它们被散布,我就进行矩阵乘法的计算,并将结果收集回根过程。我不确定我是否遗漏了什么,但我还没有完全理解Scatter和Gather。我知道使用Send可以从不同的进程发送到单独的进程和Recv,但是如何使用Scatter和Gather来工作呢?如果我在这段代码中的某个地方犯了错误,请告诉我。谢谢。

我的源码:

代码语言:javascript
复制
#define N 512
#include <stdio.h>
#include <math.h>
#include <mpi.h>
#include <sys/time.h>
print_results(char *prompt, float a[N][N]);
 int main(int argc, char *argv[]) {
  int size, rank, blksz, i, j, k;
  float a[N][N], b[N][N], c[N][N];
  char *usage = "Usage: %s file\n";
  float row[N][N], col[N][N];
  FILE *fd;
  int portion, lowerbound, upperbound;
  double elapsed_time, start_time, end_time;
  struct timeval tv1, tv2;

  MPI_Init(&argc, &argv);
  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  MPI_Comm_size(MPI_COMM_WORLD, &size);
  blksz = (int) ceil((double) N / size);
  /*
  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;
 }
 */

//Read input from file for matrices a and b.
//The I/O is not timed because this I/O needs
//to be done regardless of whether this program
//is run sequentially on one processor or in
//parallel on many processors. Therefore, it is
//irrelevant when considering speedup.
if (rank == 0) {
    for (i = 0; i < N; i++)
        for (j = 0; j < N; j++)
            a[i][j] = i + j;
    for (i = 0; i < N; i++)
        for (j = 0; j < N; j++)
            b[i][j] = i + j;
    /*
    for (i = 0; i < N; i++) {
        for (j = i + 1; j < N; j++) {
            int temp = b[i][j];
            b[i][j] = b[j][i];
            b[j][i] = temp;
        }
    }
    */
}

//TODO: Add a barrier prior to the time stamp.
MPI_Barrier(MPI_COMM_WORLD);
// Take a time stamp
gettimeofday(&tv1, NULL);
//TODO: Scatter the input matrices a and b.
    MPI_Scatter(a, blksz * N, MPI_FLOAT, row, blksz * N, MPI_FLOAT, 0,
        MPI_COMM_WORLD);
    MPI_Scatter(b, blksz * N, MPI_FLOAT, col, blksz * N, MPI_FLOAT, 0,
        MPI_COMM_WORLD);
//TODO: Add code to implement matrix multiplication (C=AxB) in parallel.
for (i = 0; i < blksz && rank * blksz + i < N; i++) {
    for (j = 0; j < N; j++) {
        c[i][j] = 0.0;
        for (k = 0; k < N; k++) {
            c[i][j] += row[i][j] * col[j][k];
        }
    }
}
//TODO: Gather partial result back to the master process.
MPI_Gather(c, blksz * N, MPI_FLOAT, c, blksz * N, MPI_FLOAT, 0,
        MPI_COMM_WORLD);
// Take a time stamp. This won't happen until after the master
// process has gathered all the input from the other processes.
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
MPI_Barrier(MPI_COMM_WORLD);
print_results("C = ", c);
MPI_Finalize();

}

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");
}
EN

回答 1

Stack Overflow用户

发布于 2013-01-23 09:09:31

希望你正在尝试做一个矩阵乘法。不需要转置矩阵。

你不能分散矩阵b。因为对于矩阵a中的每一行,您都需要整个b矩阵。广播B矩阵将是正确的。

代码语言:javascript
复制
MPI_Scatter(a, blksz * N, MPI_FLOAT, row, blksz * N, MPI_FLOAT, 0,MPI_COMM_WORLD);
MPI_Bcast(b, N * N, MPI_FLOAT, 0,MPI_COMM_WORLD);

同样,正如@Hristo lliev所提到的,您的乘法代码需要更改。

代码语言:javascript
复制
for (i = 0; i < blksz && rank * blksz + i < N; i++) {
    for (j = 0; j < N; j++) {
        product[i][j] = 0.0;
        for (k = 0; k < N; k++) {
            product[i][j] = product[i][j]+ row[i][k] * b[k][j];
        }
    }
}

此实现的正确数组声明为

代码语言:javascript
复制
float row[blksz][N] , product[blksz][N]

使用gather从根节点的所有节点合并product数组。

代码语言:javascript
复制
MPI_Gather(product, blksz * N, MPI_FLOAT, c, blksz * N, MPI_FLOAT, 0,MPI_COMM_WORLD);

并且您需要使用MPI_ScattervMPI_Gatherv

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12541312

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档