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选项。