Example #1
0
int main(int argc, char* argv[]) {
   int n, i;

   n = Get_n(argc, argv);

   printf("The primes < %d are\n", n);
   printf("2\n");
   for (i = 3; i <= n; i += 2)
      if (Is_prime(i))
         printf("%d\n", i);

   return 0;
}  /* main */
/*---------------------------------------------------------------------------*/
int main(int argc, char* argv[]) {

    int my_rank, m, n, p, max_iter, suppress, i, total_iter, iter;
    double tolerance, residual, elapsed;
    double* temp_A;
    double* local_A;
    double* b;
    double* temp_b;
    double* x;
    double* xx;
    double* xxx;
    double* y;
    double* yy;
    double* yyy;
    double* residuals;
    MPI_Comm comm;
    MPI_Init(&argc, &argv);
    comm = MPI_COMM_WORLD;
    MPI_Comm_size(comm, &p);
    MPI_Comm_rank(comm, &my_rank);
    
    if (my_rank == 0) {
        n = Get_n(argc, argv);
        tolerance = Get_tolerance(argc, argv);
        max_iter = Get_max_iter(argc, argv);
    }
    MPI_Bcast(&n, 1, MPI_INT, 0, comm);
    MPI_Bcast(&tolerance, 1, MPI_DOUBLE, 0, comm);
    MPI_Bcast(&max_iter, 1, MPI_INT, 0, comm);
    MPI_Bcast(&suppress, 1, MPI_INT, 0, comm);
    if (n < 2 || tolerance == -1.0 || max_iter == -1 || suppress == -1) {
        if (my_rank == 0)
            Usage(argv[0]);
        MPI_Finalize();
        exit(0);  
    }   
    
    b = malloc(n*sizeof(double));
    temp_b = malloc(n*sizeof(double));
    x = malloc(n*sizeof(double));
    xx = malloc(n*sizeof(double));
    xxx = malloc(n*sizeof(double));
    y = malloc(n*sizeof(double));
    yy = malloc(n*sizeof(double));
    yyy = malloc(n*sizeof(double));
    local_A = malloc((n*n/p)*sizeof(double));
    
    if (my_rank == 0) {
        residuals = malloc(n*sizeof(double));
        temp_A = malloc((n*n)*sizeof(double));
        Read_matrix_vector(temp_A, b, n, p);
        printf("\n");
    }
     
    MPI_Scatter(temp_A, n*n/p, MPI_DOUBLE, local_A, n*n/p, MPI_DOUBLE, 0, comm);
    MPI_Bcast(b, n, MPI_DOUBLE, 0, comm);
    MPI_Bcast(x, n, MPI_DOUBLE, 0, comm);
    MPI_Bcast(xx, n, MPI_DOUBLE, 0, comm);
    MPI_Bcast(y, n, MPI_DOUBLE, 0, comm);
    MPI_Bcast(yy, n, MPI_DOUBLE, 0, comm);
    for (i = 0; i < n; i++)
        temp_b[i] = b[i];


    Parallel_conjugate_gradient(local_A, x, n, p, tolerance, max_iter,
                                my_rank, comm, &residual, &elapsed,
                                &total_iter);
    printf("Parallel Conjugate Gradient Method was %d.\n", total_iter);
/*
    Parallel_cg_error(local_A, y, c, n, p, tolerance, max_iter,
                                my_rank, comm, &residual, &elapsed,
                                &total_iter);     
*/ 
    iter = 50;
    while ( iter <= 1000)
	{
	   Parallel_conjugate_gradient(local_A, xx, n, p, tolerance, iter,
                                my_rank, comm, &residual, &elapsed,
                                &total_iter);

           Parallel_cg_error(local_A, yy, n, p, tolerance, iter,
                                my_rank, comm);
 
    for (i = 0; i < n; i++)
    {
	xxx[i] = x[i] - xx[i];
	yyy[i] = x[i] -yy[i];
    }
    Matrix_vector(temp_A, xxx, xx, n); 
    Matrix_vector(temp_A, yyy, yy, n);
    double a1 = sqrt(Dot_product(xxx, xx, n));
    double a2 = sqrt(Dot_product(yyy, yy, n));
    double a3 = (a2 -a1) / a1;
    //printf( "At iteration %d, diff is %lf\n", iter, a1 );
    //printf( "At iteration %d, diff is %lf\n", iter, a2 );
    printf( "%lf\n", a3 );
    //printf("The a norm error definition is %lf.\n", a3);
    iter+=50;

	}
    
      	


    if (my_rank == 0) {
        printf("The total number of iterations for the ");
        printf("Parallel Conjugate Gradient Method was %d.\n", total_iter);
        printf("The Parallel Conjugate Gradient Method completed in");
        printf(" %.14e seconds.\n", elapsed);
        if (!suppress) {
           // printf("The solution to the linear system is:\n");
           // Print_vector(x, n);    
        }    
        printf("The norm of the residual calculated by the ");
        printf("Conjugate Gradient Method was %lf.\n", residual);
        Matrix_vector(temp_A, x, residuals, n);
        for (i = 0; i < n; i++) 
            residuals[i] = b[i] - residuals[i];
        printf("The norm of the residual calculated directly by the ");
        printf("definition of residual is %lf.\n", sqrt(Norm(residuals, n)));
           
    }

    free(b);
    free(x);
    free(local_A);
    if (my_rank == 0) {
        free(residuals);
        free(temp_A);
    }
    MPI_Finalize();
    return 0;
}  /* main */