logo

MPI Program for Vector Matrix Multiplication

   

Added on  2022-12-29

8 Pages1472 Words64 Views
Introduction
stdio .h It is header file, for writing the program, we are trying to import
some predefined library function in order to enhance our program. This
will make bulk of c standard library header.
Stdlib stands for standardlibraray,
Stdlib.h for general purpose as a standard library of C or C++
Programming it will indicates header which has all functions of memory
allocation, processs controls and conversion.
MPI full form is Message Passing Library based on the standards in the
Programs.
The MPI forum consists of vendors, researchers, developers and users
The MPI program include MPI header file.
#include<mpi.h>
Every MPI program has the following code
#include “mpi.h”
MPI_Init(&arc, &arv);
.....
.............
MPI_Finalize();
MPI_Init, will determine MPI’s international ,variable detected
internally square measure created
MPI_Common_Size is used to identify the size of the Communication Time
MPI_Common_World is did by MPI environment
MPI_Comm_Rank is used to identify sending messages and receiving
messages. Rank will always starts from zero. These ranks will be always
used to followup sending and receiving messages
MPI_Finalize is used to clean MPI things. Further after cleaning up will not
find any MPI here.
Next thing will go to vector
Vector matrix multiplication indicates, row by column vector, when
performing this operation number of rows should be having that many
columns to perform multiplication matrix .
Idea of a matrix and we have row and column with the variable names is
using MPI with vector matrix multiplication. If not mpi.h in your system
MPI Program for Vector Matrix Multiplication_1
add mpi.h in the INCLUDE folder. Also this is the clear understanding
program which is practically done
Below program was done in C Compiler
For your information there is not much difference between c and c++
Number of Ranks vs Time:
#include<stdio.h>
#include<stdlib.h>
#include<mpi.h>
void multi(int Count,float *Sum,float Vec[],float Data[],int Column)
{
int i=0,j=0,k=0;
while(i<Count)
{
Sum[i] = 0;
for(j=0;j<Column;j++)
{
Sum[i] = Sum[i] + Data[k] * Vec[j];
k++;
}
i++;
}
}
int main(int arc,char *arv[]){
int rank,size, *sendcount,*displace,*reccount;
MPI_INIT(&arc,&arv);
MPI_Comm_size(MPI_COMM_WORLD,&size);
MPI_Status status;
MPI Program for Vector Matrix Multiplication_2
FILE *fp;
char c;
int i,j,k=0,count=0,row=0,column=0;
float n=0,*sum,*rec_data,*data,*vec;
sendcount = (int*)calloc(sizeof(int),size);
reccount = (int*)calloc(sizeof(int),size);
displace = (int*)calloc(sizeof(int),size);
if(rank==0)
{
fp=fopen("matrix.txt","r");
while(fscanf(fp,"%f",&n)!=-1)
{
c=fgetc(fp);
if(c=='\n'){ row=row+1; }
count++;
}
column=count/row;
printf("Row=%d column=%d proc=%d\n",row,column,size);
float mat[row][column];
fseek( fp, 0, SEEK_SET );
data = (float*)calloc(sizeof(float),row*column);
vec = (float*)calloc(sizeof(float),column);
for(i=0;i<row;i++)
{
for(j=0;j<column;j++)
{
fscanf(fp,"%f",&mat[i][j]);
data[k] = mat[i][j];
k++;
MPI Program for Vector Matrix Multiplication_3

End of preview

Want to access all the pages? Upload your documents or become a member.