int main(int argc, char* argv[]) { /* Set parameters */ int N = atoi(argv[1]); int nIterations = atoi(argv[2]); /* Start parallel calculations */ int rank, p; MPI_Init(&argc, & argv); MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_size(MPI_COMM_WORLD, &p); /* p has to be a divisor of N !! */ if(rank==0) assert(!(N%p)); /* Test the used algorithms */ //matrix_testAll(); /* Generate the matrix */ double matrix[N*N/p]; generateMatrix(matrix,N); /* Run the powerMethod algorithm */ double start = MPI_Wtime(); double lambda = powerMethod(matrix,nIterations,N); double stop = MPI_Wtime(); /* Calculating times */ double timediff = stop - start; double times[p]; double average = 0; MPI_Gather(&timediff,1,MPI_DOUBLE,times,1,MPI_DOUBLE,0,MPI_COMM_WORLD); if(rank==0){ for(int i=0; i<p; i++) average += times[i]; average /= p; } /* Print the results */ if(rank==0) printf("dimension\t%d\tlambda\t%f\ttime\t%f\n",N,lambda,average); /* End MPI */ MPI_Finalize(); return 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); }
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(); }