矩阵乘法C语言算法及MPI实例

Catalog 目录

  • 1  -  1.1 矩阵乘法C算法实例

  •        1.2 矩阵乘法MPI算法实例

  • 2  -  2.1 编译环境需求

  • 3  -  3.1 结果现象

  • 4  -  4.1 mpiexec命令的介绍

1.1 矩阵乘法C算法实例

#include <stdio.h>
#define NRA 4     /* number of rows in matrix A */
#define NCA 4     /* number of columns in matrix A */
#define NCB 4     /* number of columns in matrix B */
double a[NRA][NCA]; /* matrix A to be multiplied */
double b[NCA][NCB]; /* matrix B to be multiplied */
double c[NRA][NCB]; /* result matrix C */
int main()
{
    int i, j, k; /* misc */
    //init matrix a
    for(i=0; i<NRA; i++)
        for (j=0; j<NCA; j++)
            a[i][j]=i+j;
    //init matrix b
    for (j=0; j<NCA; j++)
        for (k=0; k<NCB; k++)
            b[j][k]=j*k;
    //computed matrix c
    for (k=0; k<NCB; k++)
        for(i=0; i<NRA; i++)
        {
            c[i][k] = 0.0;
            for (j=0; j<NCA; j++)
                c[i][k] = c[i][k] + a[i][j] * b[j][k];
        }
}

1.2 矩阵乘法MPI算法实例

#pragma warning(disable : 4996)
#include <mpi.h>
#include <stdio.h>
#define NRA 4      /* number of rows in matrix A */
#define NCA NRA    /* number of columns in matrix A */
#define NCB NRA    /* number of columns in matrix B */
#define MASTER 0   /* taskid of first task */
float a[NRA][NCA]; /* matrix A to be multiplied */
float b[NCA][NCB]; /* matrix B to be multiplied */
float c[NRA][NCB]; /* result matrix C */
int main(int argv, char* argc[])
{
    int size,    /* number of tasks in partition */
    taskid,  /* a task identifier */
    num_row, /*num_row of each task;*/
    i, j, k; /* misc */
    MPI_Status status;
    double startwtime, endwtime;
    MPI_Init(&argv, &argc);
    MPI_Comm_rank(MPI_COMM_WORLD, &taskid);
    MPI_Comm_size(MPI_COMM_WORLD, &size);
    num_row = NRA / size;
    //init matrix a
    for (i = taskid * num_row; i < (taskid + 1) * num_row; i++)
       for (j = 0; j < NCA; j++)
           a[i][j] = i + j;
    //init matrix b
    for (i = 0; i < NCA; i++)
       for (j = 0; j < NCB; j++)
           b[i][j] = i * j;
    if (taskid == MASTER)
       startwtime = MPI_Wtime(); //begain compute time
    //computed matrix c
    for (k = 0; k < NCB; k++)
       for (i = taskid * num_row; i < (taskid + 1) * num_row; i++)
       {
           c[i][k] = 0.0;
           for (j = 0; j < NCA; j++)
                c[i][k] = c[i][k] + a[i][j] * b[j][k];
        }
    if (taskid > MASTER)
    {
        int row = taskid * num_row;
        MPI_Send(&c[row][0], num_row * NCB, MPI_FLOAT, MASTER, 99,
                 MPI_COMM_WORLD);
    }
    if (taskid == MASTER)
    {
        for (i = size - 1; i >= 1; i--)
        {
            int row = i * num_row;
            MPI_Recv(&c[row][0], num_row * NCB, MPI_FLOAT, i, 99,
                      MPI_COMM_WORLD, &status);
        }
        endwtime = MPI_Wtime(); //end compute time
        printf("wall clock time = %lf\n", endwtime - startwtime);
        for (i = 0; i < NCA; i++)
        {
             for (k = 0; k < NCB; k++)
                printf("c[%d][%d] = %.4f \t", i, k, c[i][k]);
             printf("\n");
        }
    }
    MPI_Finalize();
    return 0;
}

2.1 编译环境需求

1、visual studio 2019
2、安装Microsoft MPI库
3、powershell

3.1 结果现象

使用命令: mpiexec.exe -n 进程数 可执行文件

4.1 mpiexec命令的介绍

Q1:mpiexec是什么用途的命令?
A1:mpiexec是运行MPI并行程序的指令,基本格式为
   mpiexec -n N ./xxxxxx
   其中N是并行进程数,xxxxxx是并行程序名称。
Q2:如果并行程序不在mpiexec所在目录下应该怎么用?
A2:添加mpiexec.exe的路径到环境变量到系统路径中,位置“我的电脑”属性的高级设置中有PATH选项。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

没有对象的野指针°

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值