int main (int argc, char** argv) { AskParams(&options, argc, argv); if (argc > 7) { altermode = argv[7]; if (rank == root) { printf("altermode enabled!\n"); } } MPI_Init(&argc, &argv); MPI_Comm_size(MPI_COMM_WORLD, &size); MPI_Comm_rank(MPI_COMM_WORLD, &rank); last = size-1; printf("process %d starting ...\n", rank); initVariables(); initMatrices(); gettimeofday(&start_time, NULL); if (options.method == METH_JACOBI) { calculateJacobi(); } MPI_Barrier(MPI_COMM_WORLD); gettimeofday(&comp_time, NULL); if (rank == root) { double time = (comp_time.tv_sec - start_time.tv_sec) + (comp_time.tv_usec - start_time.tv_usec) * 1e-6; printf("Berechnungszeit: %f s \n", time); printf("Anzahl Iterationen: %d\n", iteration); } DisplayMatrix(); freeMemory(); MPI_Finalize(); }
/* ************************************************************************ */ int main (int argc, char** argv) { int rank; int size; int rest; int from, to; MPI_Init(&argc, &argv); MPI_Comm_size(MPI_COMM_WORLD, &size); MPI_Comm_rank(MPI_COMM_WORLD, &rank); struct options options; struct calculation_arguments arguments; struct calculation_results results; /* Parameter nur einmal abfragen */ if(rank == 0) { AskParams(&options, argc, argv); } MPI_Bcast(&options, (sizeof(options)), MPI_BYTE, MASTER, MPI_COMM_WORLD); initVariables(&arguments, &results, &options); /* Damit allocation + initialization richtig läuft, wird für GS size = 1 gesetzt */ if(options.method == METH_GAUSS_SEIDEL) { size = 1; } /* Aufteilen bis auf rest */ int N_part = arguments.N; int lines = N_part - 1; rest = lines % size; N_part = (lines - rest) / size; /* globale zeilennummer berechnen, hier wird der rest beachtet */ /* offset ist (rank + 1) für rank < rest, steigt also linear mit steigendem rang */ if(rank < rest) { from = N_part * rank + rank + 1; to = N_part * (rank + 1) + (rank + 1); } /* offset hier ist rest also die der maximale offset von oben */ else { from = N_part * rank + rest + 1; to = N_part * (rank + 1) + rest ; } arguments.to = to; arguments.from = from; /* at least we only need N - 1 processes for calculation */ if((unsigned int)size > (arguments.N -1)) { size = (arguments.N - 1); if(rank == MASTER ) { printf("\nWarning, you are using more processes than rows.\n This can slow down the calculation process! \n\n"); } } //calculate Number of Rows arguments.numberOfRows = ((to - from + 1) > 0 ) ? (to - from + 1) : 0; allocateMatrices(&arguments); initMatrices(&arguments, &options, rank, size); gettimeofday(&start_time, NULL); /* start timer */ if (options.method == METH_JACOBI ) { calculateJacobi(&arguments, &results, &options, rank, size); } else { /* GS berechnet nur MASTER */ if(rank == MASTER) { printf("\nGS wird nur sequentiell berechnet! \n"); calculate(&arguments, &results, &options); } } gettimeofday(&comp_time, NULL); /* stop timer */ /* only once */ if(rank == MASTER) { displayStatistics(&arguments, &results, &options, size); } /* GS macht alte ausgabe */ if((options.method == METH_GAUSS_SEIDEL) && (rank == MASTER)) { DisplayMatrix(&arguments, &results, &options); } else { DisplayMatrixMPI(&arguments, &results, &options, rank, size, from, to); } freeMatrices(&arguments); /* free memory */ MPI_Finalize(); return 0; }