Ejemplo n.º 1
0
int main(int argc, char *argv[])
{
  int rank, size;
  double start_time,end_time;
  MPI_Init(&argc,&argv);

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

  MPI_Barrier(MPI_COMM_WORLD);
  int retval = generateMatrix(size, rank);    
  if (retval != 0) {
    MPI_Finalize();
    return retval;  
  }
  MPI_Barrier(MPI_COMM_WORLD);
  // start timing
  if (MASTER(rank)) {
    start_time = MPI_Wtime();
  }
  double spectralRadius = powerMethod(rank);
  if (MASTER(rank)) {
    end_time = MPI_Wtime();
  }
  // end timing

  if (MASTER(rank)) {
    printf("The spectral radius is %f\n", spectralRadius);
    printf("It took %f seconds\n", end_time-start_time);

    /*
    int index = 0;
    for (index = 0; index < n; index++) {
      printf("%f ", x[index]);
    }
    printf("\nsize of n is %d\n", n);
    */
    // checking
    if(cs240_verify(x,n,end_time-start_time)>0){
        printf("yay, we win!\n");
    }else{
        printf("Boo, we lose... again\n");
    }
  }


  //printf("calling MPI_Finalize()\n");
  MPI_Finalize();
  return(0);
}
Ejemplo n.º 2
0
int main( int argc, char* argv[] ) {
	int writeOutX = 0;
	int n, k;
	int maxiterations = 1000;
	int niters=0;
 	double norm;
	double* b;
	double* x;
	double time;
	double t1, t2;
	
	MPI_Init( &argc, &argv );
	MPI_Comm_rank(MPI_COMM_WORLD, &rank);
	MPI_Comm_size(MPI_COMM_WORLD, &size);
	
	// Read command line args.
	// 1st case runs model problem, 2nd Case allows you to specify your own b vector
	if ( argc == 3 ) {
		k = atoi( argv[1] );
		n = k*k;
		// each processor calls cs240_getB to build its own part of the b vector!
	} else if  ( !strcmp( argv[1], "-i" ) && argc == 4 ) {
		b = load_vec( argv[2], &k );
	} else {
		printf( "\nCGSOLVE Usage: \n\t"
			"Model Problem:\tmpirun -np [number_procs] cgsolve [k] [output_1=y_0=n]\n\t"
			"Custom Input:\tmpirun -np [number_procs] cgsolve -i [input_filename] [output_1=y_0=n]\n\n");
		exit(0);
	}
	writeOutX = atoi( argv[argc-1] ); // Write X to file if true, do not write if unspecified.

	
	// Start Timer
	t1 = MPI_Wtime();
	
	// CG Solve here!
	x = cgsolve(k);
 	// End Timer
	t2 = MPI_Wtime();
	
	printf("TEST: %s\n", cs240_verify(x, k, 0.0) ? "PASSED" : "FAILED");

	if ( writeOutX ) {
		save_vec( k, x );
	}
		
	// Output
	printf( "Problem size (k): %d\n",k);
	if(niters>0){
          printf( "Norm of the residual after %d iterations: %lf\n",niters,norm);
        }
	printf( "Elapsed time during CGSOLVE: %lf\n", t2-t1);
	
        // Deallocate 
        if(niters > 0){
	  free(b);
	}
        if(niters > 0){
          free(x);
	}
	
	MPI_Finalize();
	
	return 0;
}