mpi中的二维矩阵乘法 [英] Two dimensional matrix multiplication in mpi

查看:213
本文介绍了mpi中的二维矩阵乘法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是一维矩阵乘法的代码





  #include   <   stdio.h  >  
#include < stdlib.h >
#include < mpi.h >
#define SIZE 1024
#define FROM MASTER 1
#define FROM WORKER 2
#define DEBUG 0 / * 1 = de bug on,0 = debug off * /
MPI状态;
静态 double a [SIZE] [SIZE];
静态 double b [SIZE] [SIZE];
静态 double c [SIZE] [SIZE];
static void
init matrix( void
{
int i,j;
for (i = 0 ; i< SIZE; i ++)
< span class =code-keyword> for (j = 0 ; j< SIZE; j ++){
a [i] [j ] = 1 0 ;
b [i] [j] = 1 0 ;
}
}
静态 void
打印矩阵( void
{
int i,j;
for (i = 0 ; i< SIZE; i ++){
for (j = 0 ; j< SIZE; j ++)
printf( %f,c [i] [j]);
printf( \ n);
}
}
int
main( int argc, char ** argv)
{
int myrank,nproc;
int 行;
int mtype;
int dest,src,offset;
double 开始时间,结束时间;
int i,j,k;
MPI Init(& argc,& argv);
MPI通信大小(MPI COMM WORLD,& nproc);
MPI Comm排名(MPI COMM WORLD,& myrank);
if (myrank == 0 ){
printf( SIZE =%d,节点数=%d \ n,SIZE,nproc);
init matrix();
开始时间= MPI Wtime();
rows = SIZE / nproc;
mtype = FROM MASTER;
offset = rows;
for (dest = 1 ; dest< nproc; dest ++){
if (DEBUG)
printf( 发送%d行到任务%d \ n,rows,dest);
MPI发送(&偏移, 1 ,MPI INT,dest,mtype,MPI_COMM_WORLD);
MPI发送(&行, 1 ,MPI INT,dest,mtype,MPI_COMM_WORLD);
MPI发送(& a [offset] [ 0 ],行* SIZE,MPI DOUBLE,dest,mtype,
MPI_COMM_WORLD);
MPI发送(& b,SIZE * SIZE,MPI DOUBLE,dest,mtype,MPI_COMM_WORLD);
offset + = rows;
}
for (i = 0 ; i< rows; i ++) {
for (j = 0 ; j< SIZE; j ++){
c [i] [j] = 0 0 ;
for (k = 0 ; k< SIZE; k ++)
c [ i] [j] = c [i] [j] + a [i] [k] * b [k] [j];
}
}
mtype = FROM WORKER;
for (src = 1 ; src< nproc; src ++){
MPI Recv(& offset, 1 ,MPI INT,src,mtype,MPI_COMM_WORLD,& status);
MPI Recv(& rows, 1 ,MPI INT,src,mtype,MPI_COMM_WORLD,& status);
MPI Recv(& c [offset] [ 0 ],行* SIZE,MPI_DOUBLE,src,mtype,
MPI_COMM_WORLD,& status );
if (DEBUG)
printf( recvd%d来自任务%d,offset =%d \ n
rows,src,offset);
}
结束时间= MPI_Wtime();
if (DEBUG)
print matrix();
printf( %2d节点上的执行时间:%f \ n,nproc ,结束时间 - 开始时间);
} 其他 {
mtype = FROM MASTER;
MPI Recv(&偏移量, 1 ,MPI INT, 0 ,mtype,MPI COMM世界,地位);
MPI Recv(& rows, 1 ,MPI INT, 0 ,mtype,MPI COMM世界,地位);
MPI Recv(& a [offset] [ 0 ],行* SIZE,MPI DOUBLE, 0 ,mtype,
MPI COMM WORLD,& status);
MPI Recv(& b,SIZE * SIZE,MPI DOUBLE, 0 ,mtype,MPI COMM WORLD,
& status);
if (DEBUG)
printf( Rank =%d,offset =%d,row =%d,a [offset] [0] =%e,b [0] [0] =%e \ n
myrank,offset,rows,a [offset] [ 0 ],b [ 0 ] [< span class =code-digit> 0 ]);
for (i = offset; i< offset + rows; i ++)
for (j = 0 ; j< SIZE; j ++){
c [i] [j] = 0 0 ;
for (k = 0 ; k< SIZE; k ++)
c [ i] [j] = c [i] [j] + a [i] [k] * b [k] [j];
}
if (DEBUG)
printf( Rank =%d,offset =%d,row =%d,c [offset] [0] =%e \ n
myrank,offset,行,[偏移] [ 0 ]);
mtype = FROM WORKER;
MPI发送(&偏移量, 1 ,MPI INT, 0 ,mtype,MPI COMM世界);
MPI发送(&行, 1 ,MPI INT, 0 ,mtype,MPI COMM世界);
MPI发送(& c [offset] [ 0 ],行* SIZE,MPI DOUBLE, 0 ,mtype,
MPI COMM WORLD);
}
MPI_Finalize();
return 0 ;
}







我尝试了什么:



我想要在mpi中写两维矩阵乘法的技巧

解决方案

请参阅我对该问题的评论。您不需要太多的编程技巧,以及您可以在许多地方找到的数学定义。 Matrix产品是一种非常简单的操作(与分割或反转,复杂和时间相比)。例如,参见:

Matrix(数学) - 维基百科,免费的百科全书 [ ^ ],

矩阵乘法 - 维基百科,免费的百科全书 [ ^ ]。



-SA

Here is the code for one dimentional matrix multiplication


#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
#define SIZE 1024
#define FROM MASTER 1 
#define FROM WORKER 2
#define DEBUG 0 /∗ 1 = debug on, 0 = debug off ∗/
MPI Status status;
static double a[SIZE][SIZE];
static double b[SIZE][SIZE];
static double c[SIZE][SIZE];
static void
init matrix(void)
{
    int i, j;
    for (i = 0; i < SIZE; i++)
        for (j = 0; j < SIZE; j++) {
            a[i][j] = 1.0;
            b[i][j] = 1.0;
        }
}
static void
print matrix(void)
{
    int i, j;
    for (i = 0; i < SIZE; i++) {
        for (j = 0; j < SIZE; j++)
            printf(" %f", c[i][j]);
        printf("\n");
    }
}
int
main(int argc, char ∗∗argv)
{
    int myrank, nproc;
    int rows;
    int mtype;
    int dest, src, offset;
    double start time, end time;
    int i, j, k;
    MPI Init(&argc, &argv);
    MPI Comm size(MPI COMM WORLD, &nproc);
    MPI Comm rank(MPI COMM WORLD, &myrank);
    if (myrank == 0) {
        printf("SIZE = %d, number of nodes = %d\n", SIZE, nproc);
        init matrix();
        start time = MPI Wtime();
        rows = SIZE / nproc;
        mtype = FROM MASTER;
        offset = rows;
        for (dest = 1; dest < nproc; dest++) {
            if (DEBUG)
                printf(" sending %d rows to task %d\n", rows, dest);
            MPI Send(&offset, 1, MPI INT, dest, mtype, MPI_COMM_WORLD);
            MPI Send(&rows, 1, MPI INT, dest, mtype, MPI_COMM_WORLD);
            MPI Send(&a[offset][0], rows∗SIZE, MPI DOUBLE, dest, mtype,
                MPI_COMM_WORLD);
            MPI Send(&b, SIZE∗SIZE, MPI DOUBLE, dest, mtype, MPI_COMM_WORLD);
            offset += rows;
        }
        for (i = 0; i < rows; i++) {
            for (j = 0; j < SIZE; j++) {
                c[i][j] = 0.0;
                for (k = 0; k < SIZE; k++)
                    c[i][j] = c[i][j] + a[i][k] ∗ b[k][j];
            }
        }
        mtype = FROM WORKER;
        for (src = 1; src < nproc; src++) {
            MPI Recv(&offset, 1, MPI INT, src, mtype, MPI_COMM_WORLD, &status);
            MPI Recv(&rows, 1, MPI INT, src, mtype, MPI_COMM_WORLD, &status);
            MPI Recv(&c[offset][0], rows∗SIZE, MPI_DOUBLE, src, mtype,
                MPI_COMM_WORLD, &status);
            if (DEBUG)
                printf(" recvd %d rows from task %d, offset = %d\n",
                rows, src, offset);
        }
        end time = MPI_Wtime();
        if (DEBUG)
            print matrix();
        printf("Execution time on % 2d nodes: %f\n", nproc, end time−start time);
    } else {
        mtype = FROM MASTER;
        MPI Recv(&offset, 1, MPI INT, 0, mtype, MPI COMM WORLD, &status);
        MPI Recv(&rows, 1, MPI INT, 0, mtype, MPI COMM WORLD, &status);
        MPI Recv(&a[offset][0], rows∗SIZE, MPI DOUBLE, 0, mtype,
            MPI COMM WORLD, &status);
        MPI Recv(&b, SIZE∗SIZE, MPI DOUBLE, 0, mtype, MPI COMM WORLD,
            &status);
        if (DEBUG)
            printf("Rank = %d, offset = %d, row = %d, a[offset][0] = %e, b[0][0] = %e\n",
            myrank, offset, rows, a[offset][0], b[0][0]);
        for (i = offset; i < offset + rows; i++)
            for (j = 0; j < SIZE; j++) {
            c[i][j] = 0.0;
            for (k = 0; k < SIZE; k++)
                c[i][j] = c[i][j] + a[i][k] ∗ b[k][j];
            }
        if (DEBUG)
            printf("Rank = %d, offset = %d, row = %d, c[offset][0] = %e\n",
            myrank, offset, rows, a[offset][0]);
        mtype = FROM WORKER;
        MPI Send(&offset, 1, MPI INT, 0, mtype, MPI COMM WORLD);
        MPI Send(&rows, 1, MPI INT, 0, mtype, MPI COMM WORLD);
        MPI Send(&c[offset][0], rows∗SIZE, MPI DOUBLE, 0, mtype,
            MPI COMM WORLD);
    }
    MPI_Finalize();
    return 0;
}




What I have tried:

I want tips writing two dimentional matrix multiplication in mpi

解决方案

Please see my comment to the question. You don't need programming tips much, and the mathematical definitions you can find in many places. Matrix product is a very simple operation (in contrast to division, or inversion, complicated and time-taking). See, for example:
Matrix (mathematics) — Wikipedia, the free encyclopedia[^],
Matrix multiplication — Wikipedia, the free encyclopedia[^].

—SA


这篇关于mpi中的二维矩阵乘法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆