int main(int argc, char **argv) { //Input matrix and vector declaration double *mat, *vec; double spectral_radius; double start,stop; MPI_Init(&argc,&argv); // Determine the size of the matrix and number of iterations from the // command line arguments. int size = atoi(argv[1]); int iter = atoi(argv[2]); // Determine the rank of the current process, and the total number of processes. int myrank,nprocs; MPI_Comm_rank(MPI_COMM_WORLD, &myrank); MPI_Comm_size(MPI_COMM_WORLD, &nprocs); // Allocate memory for the matrix and the vector of appropriate size here. mat = (double *)malloc(size*size/nprocs*sizeof(double)); vec = (double *)malloc(size*sizeof(double)); // Use process 0 for all the print statements. if( myrank == 0) { printf("\nThis program generates a matrix of dimensions %d x %d and runs the powermethod\non it for %d iterations. If you are using the input matrix from the given example,\nthe result should be 6\n\n",size,size,iter); } // Generate matrix generatematrix(mat,size); //Generate a random vector generatevec(vec,size); // Power Method to generate the spectral radius of the matrix. start = MPI_Wtime(); spectral_radius = powerMethod(mat,vec,size,iter); stop = MPI_Wtime(); // Free all the variables free(mat); free(vec); // Print the radius and execution time using thread 0. // You can change the print format anyway you want. if(myrank == 0) { printf("Spectral radius is : %lf\n",spectral_radius); printf("Time in seconds : %lf\n", stop - start); // myprintouts } MPI_Finalize(); }
bool testinvldlt(bool silent) { bool result; ap::real_2d_array a; ap::real_2d_array a2; ap::real_2d_array a3; ap::integer_1d_array p; int n; int pass; int mtask; int i; int j; int k; int minij; bool upperin; bool cr; double v; double err; bool waserrors; int passcount; int maxn; int htask; double threshold; err = 0; passcount = 10; maxn = 20; threshold = 100000*ap::machineepsilon; waserrors = false; // // Test // for(n = 1; n <= maxn; n++) { a.setbounds(0, n-1, 0, n-1); a2.setbounds(0, n-1, 0, n-1); a3.setbounds(0, n-1, 0, n-1); for(mtask = 2; mtask <= 2; mtask++) { for(htask = 0; htask <= 1; htask++) { for(pass = 1; pass <= passcount; pass++) { upperin = htask==0; // // Prepare task: // * A contains symmetric matrix // * A2, A3 contains its upper (or lower) half // generatematrix(a, n, mtask); for(i = 0; i <= n-1; i++) { for(j = 0; j <= n-1; j++) { a2(i,j) = a(i,j); a3(i,j) = a(i,j); } } for(i = 0; i <= n-1; i++) { for(j = 0; j <= n-1; j++) { if( upperin ) { if( j<i ) { a2(i,j) = 0; a3(i,j) = 0; } } else { if( i<j ) { a2(i,j) = 0; a3(i,j) = 0; } } } } // // Test 1: inv(A2) // smatrixinverse(a2, n, upperin); restorematrix(a2, n, upperin); for(i = 0; i <= n-1; i++) { for(j = 0; j <= n-1; j++) { v = ap::vdotproduct(a.getrow(i, 0, n-1), a2.getcolumn(j, 0, n-1)); if( i==j ) { v = v-1; } err = ap::maxreal(err, fabs(v)); } } // // Test 2: inv(LDLt(A3)) // smatrixldlt(a3, n, upperin, p); smatrixldltinverse(a3, p, n, upperin); restorematrix(a3, n, upperin); for(i = 0; i <= n-1; i++) { for(j = 0; j <= n-1; j++) { v = ap::vdotproduct(a.getrow(i, 0, n-1), a3.getcolumn(j, 0, n-1)); if( i==j ) { v = v-1; } err = ap::maxreal(err, fabs(v)); } } } } } } // // report // waserrors = err>threshold; if( !silent ) { printf("TESTING LDLT INVERSE\n"); printf("ERROR: %5.3le\n", double(err)); if( waserrors ) { printf("TEST FAILED\n"); } else { printf("TEST PASSED\n"); } printf("\n\n"); } result = !waserrors; return result; }