资源简介
由用户指定矩阵维数,程序会随机生成相应维数的矩阵,调用MPI中的相关函数,模拟并行算法计算得出矩阵乘的结果
代码片段和文件信息
#include
#include
#include
#include “mpi.h“
#define M_OK 1;
#define MAX_MATRIX_SIZE 200;
#define M_ERR 0;
/*Show the usage*/
void usage(char * program)
{
printf(“Usage %s a b c\n“ program);
printf(“The size of the first matrix is a rows b columns\n“);
printf(“The size of the second matrix is b rows c columns\n“);
}
/*Create a matrix which has $row rows $col columns and fill it*/
double ** create_matrix(int row int col char flag)
{
int ij;
double ** new_matrix = (double **)malloc(row * sizeof(double*));
srand((unsigned int)time(NULL));
for(i = 0; i < row; i ++)
{
new_matrix[i] = (double * )malloc(col * sizeof(double));
if(flag != ‘n‘)
{
for(j = 0; j < col; j ++)
new_matrix[i][j] = (double)(rand() % 10);
}
else
{
for(j = 0; j < col; j ++)
new_matrix[i][j] = 0.00;
}
}
return new_matrix;
}
/*Free the matirx*/
int free_matrix(double ** matrix int row int col)
{
int i;
for(i = 0; i < row; i ++)
free(matrix[i]);
free(matrix);
return M_OK;
}
/*Print a matrix with $row rows $col columns*/
void show_matrix(double ** matrix int row int col)
{
int ij;
for(i = 0; i < col; i ++)
printf(“=======“);
printf(“\n“);
for(i = 0; i < row; i ++)
{
for(j = 0; j < col; j ++)
{
printf(“%2.2f “ matrix[i][j]);
}
printf(“\n“);
}
for(i = 0; i < col; i ++)
printf(“=======“);
printf(“\n“);
}
double ** matrix_multi(double ** first_m double ** second_m int a int b int c)
{
int i j k;
double res_tmp;
double ** matrix_res_part = create_matrix(a c ‘n‘);
for(i = 0; i < a; i++)
{
for(j = 0; j < c; j++)
{
res_tmp = 0.00;
for(k = 0; k < b; k++)
res_tmp += first_m[i][k] * second_m[k][j];
matrix_res_part[i][j] = res_tmp;
}
}
return matrix_res_part;
}
int main(int argc char * argv[])
{
/*[axb]*[bxc]=[axc] matrix_size[0] = a matrix_size[1] = b matrix_size[2] = c*/
long matrix_size[3];
double ** matrix_1st = NULL;
double ** matrix_2nd = NULL;
double ** matrix_res = NULL;
int i j;
/*MPICH related*/
int myid rank numprocs;
char processor_name[MPI_MAX_PROCESSOR_NAME];
double * tmp_buf_dis = NULL;
double * tmp
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 489984 2009-12-25 13:41 matrix_plus\Debug\matrix_plus.exe
文件 1100344 2009-12-25 13:41 matrix_plus\Debug\matrix_plus.ilk
文件 2214912 2009-12-25 13:41 matrix_plus\Debug\matrix_plus.pdb
文件 4868 2009-12-25 13:41 matrix_plus\matrix_plus\Debug\BuildLog.htm
文件 406 2009-12-25 13:14 matrix_plus\matrix_plus\Debug\matrix_plus.exe.em
文件 472 2009-12-25 13:14 matrix_plus\matrix_plus\Debug\matrix_plus.exe.em
文件 381 2009-12-25 13:41 matrix_plus\matrix_plus\Debug\matrix_plus.exe.intermediate.manifest
文件 18935 2009-12-25 13:41 matrix_plus\matrix_plus\Debug\matrix_plus.obj
文件 69 2009-12-25 13:41 matrix_plus\matrix_plus\Debug\mt.dep
文件 60416 2009-12-25 13:41 matrix_plus\matrix_plus\Debug\vc90.idb
文件 69632 2009-12-25 13:41 matrix_plus\matrix_plus\Debug\vc90.pdb
文件 9925 2009-12-25 13:42 matrix_plus\matrix_plus\matrix_plus.cpp
文件 4376 2009-12-25 13:18 matrix_plus\matrix_plus\matrix_plus.vcproj
文件 1421 2009-12-27 13:56 matrix_plus\matrix_plus\matrix_plus.vcproj.WENSHASHA.shasha.user
文件 756736 2009-12-27 13:56 matrix_plus\matrix_plus.ncb
文件 899 2009-12-24 15:17 matrix_plus\matrix_plus.sln
..A..H. 11264 2009-12-27 13:56 matrix_plus\matrix_plus.suo
目录 0 2009-12-27 13:57 matrix_plus\matrix_plus\Debug
目录 0 2009-12-27 13:57 matrix_plus\Debug
目录 0 2009-12-27 13:57 matrix_plus\matrix_plus
目录 0 2009-12-27 13:57 matrix_plus
----------- --------- ---------- ----- ----
4745040 21
评论
共有 条评论