www.pudn.com > jordan.rar > jordan.c


#include "stdio.h"
#include "stdlib.h"
#include "mpi.h"
#include "math.h"

/* somethings convinient for programming use */
#define a(x,y) a[x*M+y]
#define b(x) b[x]
#define A(x,y) A[x*M+y]
#define B(x) B[x]
#define floatsize sizeof(float)
#define intsize sizeof(int)

/* M*M matrix */
int M;

/* dimision for per node */
int m;

/* A, B array pointer */
float *A;
float *B;

/* time starting to compute, and other virable for time use */
double starttime;
double time1;
double time2;

/* node rank */
int my_rank;

/* size of nodes */
int p;

int l;
MPI_Status status;

/*
 *Function: fatal()
 *Desc:     output some message for user to know, and exit
 *Param:    message-char pointer to msg going to print on the screen
 */
void
fatal(char *message)
{
    printf("%s\n",message);
    exit(1);
}

/*
 *Function: Environment_Finalize()
 *Desc:     release some pointer before all be finished
 *Param:    a,b,x,f - float pointer
 *
 */
void
Environment_Finalize(float *a,float *b,float *x,float *f)
{
    free(a);
    free(b);
    free(x);
    free(f);
}

/*
 *Function:  main()
 *Desc:      program entry
 *Param:
 */
int
main(int argc, char **argv)
{
    int i,j,t,k,my_rank,group_size;
    int i1,i2;
    int v,w;
    float s;
    float temp;
    int tem;
    float *f;
    float lmax;
    float *a;
    float *b;
    float *x;
    int *shift;
    FILE *fdA,*fdB;

    /* initialize MPI , group_size, my_rank */
    MPI_Init(&argc,&argv);
    MPI_Comm_size(MPI_COMM_WORLD,&group_size);
    MPI_Comm_rank(MPI_COMM_WORLD,&my_rank);
    if(my_rank==0)
        printf("group size: %d\n",group_size);

    /* node number */
    p=group_size;

    /*
     for the node which my_rank==0, set starttime
     and read A,B elements from file"dataIn.txt",
     p.s. M=N-1
    */
    if (my_rank==0)
    {
        starttime=MPI_Wtime();

        fdA=fopen("dataIn.txt","r");

        fscanf(fdA,"%d",&M);

        /* allocate memory for matrix A an B */
        A=(float *)malloc(floatsize*M*M);
        B=(float *)malloc(floatsize*M);

        /* read A&B elements(M*(M+1)) from the file */
        for(i = 0; i < M; i++)
        {
            /* the 0 to M-1 th element of each row is the element of A */
            for(j = 0; j < M; j++)
            {
                fscanf(fdA,"%f", A+i*M+j);
            }
            /* the last one of each row  is the element of B */
            fscanf(fdA,"%f", B+i);
        }
        fclose(fdA);
    }

    /* broadcast the value of M */
    MPI_Bcast(&M,1,MPI_INT,0,MPI_COMM_WORLD);
    /* allocate area for each node */
    m=M/p;
    if (M%p!=0) m++;

    f=(float*)malloc(sizeof(float)*(M+1));
    a=(float*)malloc(sizeof(float)*m*M);
    b=(float*)malloc(sizeof(float)*m);
    x=(float*)malloc(sizeof(float)*M);
    shift=(int*)malloc(sizeof(int)*M);

    /* initialize shift value for each column */
    for(i=0;ilmax)
            {
                lmax=a(i,k);
                l=k;
            }

            /* exchange the with the max */
            if (l!=v)
            {
                for(t=0;t