⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 matrmult.c

📁 在MPI上实现的矩阵相乘并行计算的源程序。
💻 C
字号:
#include "stdio.h"#include "stdlib.h"#define INTSIZE sizeof(int)#define CHARSIZE sizeof(char)#define A(x,y) A[x*K+y]#define B(x,y) B[x*N+y]#define C(x,y) C[x*N+y]int M,N,K;int *A,*B,*C;FILE *fdA,*fdB,*fdC;void fatal(char *message)/*print error message and exit*/{printf("Error:%s\n",message);exit(0);}void open_file_A_B_C(){char *fileA,*fileB,*fileC;fileA=(char *)malloc(CHARSIZE*40);if (fileA==NULL)  fatal("Fail to danamicly allocate fileA's space!");printf("File A name:");/*scanf("%s",fileA);*/fileA="liuA";fileB=(char *)malloc(CHARSIZE*40);if (fileB==NULL) { free(fileA); fatal("Fail to danamicly allocate fileB's space!");}printf("File B name:");/*scanf("%s",fileB);*/fileB="liuB";fileC=(char *)malloc(CHARSIZE*40);if (fileC==NULL) { free(fileA);free(fileB); fatal("Fail to danamicly allocate fileC's space!");}printf("File C name:");/*scanf("%s",fileC);*/fileC="liuC0";fdA=fopen(fileA,"r");if (fdA==NULL) {    free(fileA);free(fileB); }fdB=fopen(fileB,"r");if(fdB==NULL)  {free(fileA);free(fileB);fclose(fdA);}fdC=fopen(fileC,"w");/* Truncate  file  to  zero length or create text file  for writing.   The  stream  is  positioned  at  the  beginning of the file.*/if(fdC==NULL)  {free(fileA);free(fileB);free(fileC);fclose(fdA);fclose(fdB);}free(fileA);free(fileB);free(fileC);}/*open_file_A_and_B*/void get_M_N_K(){int RowB;fread(&M,INTSIZE,1,fdA);fread(&K,INTSIZE,1,fdA);fread(&RowB,INTSIZE,1,fdB);if (RowB!=K) fatal("Column-number of matrix A should equal to Row-number of matrix B,otherwise A cannot mutilple with B");fread(&N,INTSIZE,1,fdB);printf("M=%d,K=%d,N=%d\n",M,K,N);}/*get_M_N_K*/void dynamic_allocation( ){A=(int *)malloc(INTSIZE*M*K);if (A==NULL)fatal("Fail to allocate space for A");B=(int *)malloc(INTSIZE*K*N);if (B==NULL){free(A);fatal("Fail to allocate space for B");}C=(int *)malloc(INTSIZE*M*N);if (C==NULL){free(A);free(B);fatal("Fail to allocate space for C");}}/*dynamic_allocation*/void get_matrix_A(){ fread(A,INTSIZE,M*K,fdA);} /*get_matrix_A*/void get_matrix_B(){fread(B,INTSIZE,K*N,fdB);} /*get_matrix_B*/void  print_matrix_A(){ /*int i,j;     printf("Matrix A:\n");  for (i=0;i<M;i++)    {for (j=0;j<K;j++)       printf("%d ",A(i,j));    printf("\n");   }printf("\n");*/printf("Already get A\n");} /*print_matrix_A*/void  print_matrix_B(){ /*int i,j;    printf("Matrix B:\n");  for (i=0;i<K;i++)    {for (j=0;j<N;j++)       printf("%d ", B(i,j));    printf("\n");   }printf("\n");*/printf("Already get B\n");} /*print_matrix_B*/void multiple(){int i,j,s;for (i=0;i<M;i++){   for (j=0;j<N;j++)     for (C(i,j)=0,s=0;s<K;s++)         C(i,j)+=A(i,s)*B(s,j);  printf("Complete Rate:%6.4f%%\n",100.0*i/M);}fwrite(&M,INTSIZE,1,fdC);fwrite(&N,INTSIZE,1,fdC);fwrite(C,INTSIZE,M*N,fdC);}/*multiple*/void  print_matrix_C(){ /*int i,j;  printf("Matrix C:\n");  for (i=0;i<M;i++)    {for (j=0;j<N;j++)       printf("%d ", C(i,j));        printf("\n");   }printf("\n\n");*/printf("Compute Complete!\n");} /*print_matrix_C*/int main( ){open_file_A_B_C();get_M_N_K();dynamic_allocation( );get_matrix_A( );get_matrix_B();print_matrix_A();print_matrix_B();multiple();fclose(fdA);fclose(fdB);fclose(fdC);print_matrix_C( );return (0);} /*main */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -