Click here to Skip to main content
15,881,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to do a small program that calculates eigen value and eigen vector in parallel processors using MPI.

I have written a code that works correctly but its for uni processor.
Please help me with some code for parallel processor.

I want to compare the processing time for different number of processors with different size of matrix.

Please help me.

What I have tried:

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<mpi.h>
#include<time.h>

int main(int argc, char* argv[])
{
    float a[1000][1000],x[1000],c[1000],d=0,temp;
    int n,i,j;
    double startTime;
    int rank, size;

    MPI_Init(&argc, &argv);

    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &size);

    if(rank==0)
    {
	    printf("Enter the order of matrix ? ");
	    scanf("%d",&n);

	    for(i=0;i<n;i++)
	    {
		for(j=0;j<n;j++)
		   a[i][j] = rand()%9;
	    }

	    for(i=0;i<n;i++)
	    x[i] = rand()%5;
    }

    do
    {
        for(i=0;i<n;i++)
        {
            c[i]=0;
            for(j=0;j<n;j++)
                c[i]+=a[i][j]*x[j];
        }
        for(i=0;i<n;i++)
            x[i]=c[i];
            
        temp=d;
        d=0;
        
        for(i=0;i<n;i++)
        {
            if(fabs(x[i])>fabs(d))
                d=x[i];
        }
        for(i=0;i<n;i++)
            x[i]/=d;
            
    }while(fabs(d-temp)>0.00001);




    if(rank==0)
    {
	    printf("\nThe required eigen value is %f",d);
	    printf("\nThe required eigen vector is :\n");
	    for(i=0; i<n; i++)
	    {
		printf("%f\n",x[i]);
	    }

	   printf("Time taken %f sec\n",(clock()-startTime)/CLOCKS_PER_SEC);
    }

   MPI_Finalize();
}
Posted
Updated 18-Apr-17 15:56pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900