/* ************************************************************************ */ int main (int argc, char** argv) { struct options options; struct calculation_arguments arguments; struct calculation_results results; /* get parameters */ AskParams(&options, argc, argv); /* ************************* */ initVariables(&arguments, &results, &options); /* ******************************************* */ allocateMatrices(&arguments); /* get and initialize variables and matrices */ initMatrices(&arguments, &options); /* ******************************************* */ pthread_barrier_init(&loop_barrier, NULL, options.number+1); /* init der Loop-Barriere */ gettimeofday(&start_time, NULL); /* start timer */ calculate(&arguments, &results, &options); /* solve the equation */ gettimeofday(&comp_time, NULL); /* stop timer */ displayStatistics(&arguments, &results, &options); DisplayMatrix(&arguments, &results, &options); freeMatrices(&arguments); /* free memory */ return 0; }
/* ************************************************************************ */ int main (int argc, char** argv) { struct options options; struct calculation_arguments arguments; struct calculation_results results; /* get parameters */ AskParams(&options, argc, argv); /* ************************* */ initVariables(&arguments, &results, &options); /* ******************************************* */ allocateMatrices(&arguments); /* get and initialize variables and matrices */ initMatrices(&arguments, &options); /* ******************************************* */ gettimeofday(&start_time, NULL); /* start timer */ calculate(&arguments, &results, &options); /* solve the equation */ gettimeofday(&comp_time, NULL); /* stop timer */ displayStatistics(&arguments, &results, &options); /* **************** */ DisplayMatrix("Matrix:", /* display some */ arguments.Matrix[results.m][0], options.interlines); /* statistics and */ freeMatrices(&arguments); /* free memory */ return 0; }
/* ************************************************************************ */ int main (int argc, char** argv) { struct options options; struct mpi_options mpi_options; struct calculation_arguments arguments; struct calculation_results results; initMpi(&mpi_options, &argc, &argv); /* get parameters */ AskParams(&options, argc, argv, mpi_options.mpi_rank == 0); /* ************************* */ initVariables(&arguments, &results, &options, &mpi_options); /* ******************************************* */ allocateMatrices(&arguments); /* get and initialize variables and matrices */ initMatrices(&arguments, &options); /* ******************************************* */ gettimeofday(&start_time, NULL); /* start timer */ calculate(&arguments, &results, &options, &mpi_options); /* solve the equation */ gettimeofday(&comp_time, NULL); /* stop timer */ if(mpi_options.mpi_rank == 0) displayStatistics(&arguments, &results, &options); DisplayMatrix(&arguments, &results, &options, mpi_options.mpi_rank, mpi_options.mpi_size, arguments.row_start, arguments.row_end); freeMatrices(&arguments); /* free memory */ MPI_Finalize(); return 0; }
/* ************************************************************************ */ int main (int argc, char** argv) { struct options options; struct calculation_arguments arguments; struct calculation_results results; struct comm_options comm; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &comm.rank); MPI_Comm_size(MPI_COMM_WORLD, &comm.num_procs); /* get parameters */ AskParams(&options, argc, argv, comm.rank); /* ************************* */ initVariables(&arguments, &results, &options); /* ******************************************* */ if (options.method == METH_JACOBI && comm.num_procs > 1) { allocateMatrices_mpi(&arguments, &comm); initMatrices_mpi(&arguments, &options, &comm); gettimeofday(&start_time, NULL); /* start timer */ calculate_mpi(&arguments, &results, &options, &comm); /* solve the equation */ gettimeofday(&comp_time, NULL); /* stop timer */ if (comm.rank == ROOT) { displayStatistics(&arguments, &results, &options); } DisplayMatrix_mpi(&arguments, &results, &options, comm.rank, comm.num_procs, comm.absoluteStartRow, comm.absoluteStartRow + comm.matrixRows -3); freeMatrices(&arguments); /* free memory */ } else { allocateMatrices(&arguments); /* get and initialize variables and matrices */ initMatrices(&arguments, &options); /* ******************************************* */ gettimeofday(&start_time, NULL); /* start timer */ calculate(&arguments, &results, &options); /* solve the equation */ gettimeofday(&comp_time, NULL); /* stop timer */ displayStatistics(&arguments, &results, &options); DisplayMatrix(&arguments, &results, &options); freeMatrices(&arguments); /* free memory */ } MPI_Finalize(); return 0; }
/* ************************************************************************ */ int main (int argc, char** argv) { MPI_Init(&argc, &argv); struct options options; struct calculation_arguments arguments; struct calculation_results results; /* get parameters */ AskParams(&options, argc, argv); initVariables(&arguments, &results, &options); allocateMatrices(&arguments); /* get and initialize variables and matrices */ initMatrices(&arguments, &options); gettimeofday(&start_time, NULL); /* start timer */ if (options.method == METH_JACOBI) calculate_jacobi(&arguments, &results, &options); /* solve the equation using Jaocbi */ else calculate_gaussseidel(&arguments, &results, &options); /* solve the equation using Gauss-Seidel */ MPI_Barrier(MPI_COMM_WORLD); gettimeofday(&comp_time, NULL); /* stop timer */ // only attempt communication if we have more than 1 procss if(arguments.nproc > 1) { // communicate final maxresiduum from last rank to rank 0 if(arguments.rank == 0) MPI_Recv(&results.stat_precision, 1, MPI_DOUBLE, arguments.nproc - 1, 1, MPI_COMM_WORLD, NULL); if(arguments.rank == arguments.nproc - 1) MPI_Send(&results.stat_precision, 1, MPI_DOUBLE, 0, 1, MPI_COMM_WORLD); } //printDebug(&arguments, &results); // pretty-print matrix if we are debugging if(arguments.rank == 0) displayStatistics(&arguments, &results, &options); DisplayMatrix("Matrix:", arguments.Matrix[results.m][0], options.interlines, arguments.rank, arguments.nproc, arguments.offset + ((arguments.rank > 0) ? 1 : 0), (arguments.offset + arguments.N - ((arguments.rank != arguments.nproc - 1) ? 1 : 0))); freeMatrices(&arguments); /* free memory */ MPI_Finalize(); return 0; }
/* ************************************************************************ */ int main (int argc, char** argv) { struct options options; struct calculation_arguments arguments; struct calculation_results results; // mpi setup --------------------------------------------------------------- MPI_Init(NULL, NULL); int rank, nprocs; MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_size(MPI_COMM_WORLD, &nprocs); /* get parameters */ AskParams(&options, argc, argv, rank); initVariables(&arguments, &results, &options, rank, nprocs); // printf("[%d] %" PRIu64 " %" PRIu64 "\n", rank, arguments.offset_start, arguments.offset_end); // get and initialize variables and matrices allocateMatrices(&arguments); initMatrices(&arguments, &options); gettimeofday(&start_time, NULL); /* start timer */ // solve the equation if (options.method == METH_JACOBI) { calculate_mpi(&arguments, &results, &options); } else { calculate(&arguments, &results, &options); } // MPI_Barrier(MPI_COMM_WORLD); // gettimeofday(&comp_time, NULL); /* stop timer */ DisplayMatrix(&arguments, &results, &options); freeMatrices(&arguments); MPI_Finalize(); return 0; }
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) { struct options options; struct calculation_arguments arguments; struct calculation_results results; // MPI Init MPI_Init(&argc, &argv); MPI_Comm_size(MPI_COMM_WORLD, &size); MPI_Comm_rank(MPI_COMM_WORLD, &rank); /* get parameters */ AskParams(&options, argc, argv); /* ************************* */ initVariables(&arguments, &results, &options); /* ******************************************* */ allocateMatrices(&arguments); /* get and initialize variables and matrices */ initMatrices(&arguments, &options); /* ******************************************* */ gettimeofday(&start_time, NULL); /* start timer */ calculate(&arguments, &results, &options); /* solve the equation */ //wait MPI_Barrier(MPI_COMM_WORLD); gettimeofday(&comp_time, NULL); /* stop timer */ if (rank == root){ displayStatistics(&arguments, &results, &options); } DisplayMatrix(&arguments, &results, &options); freeMatrices(&arguments); /* free memory */ //MPI End MPI_Finalize(); return 0; }
/* ************************************************************************ */ int main (int argc, char** argv) { struct options options; struct calculation_arguments arguments; struct calculation_results results; /* get parameters */ AskParams(&options, argc, argv); /* ************************* */ initVariables(&arguments, &results, &options); /* ******************************************* */ allocateMatrices(&arguments, &options); /* get and initialize variables and matrices */ initMatrices(&arguments, &options); /* ******************************************* */ if (options.inf_func == FUNC_FPISIN) /* Init Cache für Sinus berechnung */ initMysin(&arguments); gettimeofday(&start_time, NULL); /* start timer */ calculate(&arguments, &results, &options); /* solve the equation */ gettimeofday(&comp_time, NULL); /* stop timer */ // int i, j; // for(i = 0; i <=arguments.N ; i++) // for(j = 0; j <=arguments.N; j++) // arguments.Matrix[results.m][i][j] = Matrix_getValue(arguments.Mat[results.m], i, j); displayStatistics(&arguments, &results, &options); /* **************** */ DisplayMatrix("Matrix:", /* display some */ arguments.Mat[results.m], options.interlines); /* statistics and */ freeMysin(&arguments); /* Cache für Sinus freigeben */ freeMatrices(&arguments); /* free memory */ return 0; }
/* ************************************************************************ */ int main (int argc, char** argv) { struct options options; struct calculation_arguments arguments; struct calculation_results results; int rc; rc = MPI_Init(&argc,&argv); if (rc != MPI_SUCCESS) { printf("Error initializing MPI. Terminating.\n"); MPI_Abort(MPI_COMM_WORLD, rc); } AskParams(&options, argc, argv); /* get parameters */ initVariables(&arguments, &results, &options); /* ******************************************* */ initMPI(&mpis, &arguments); /* initalize MPI */ allocateMatrices(&arguments); /* get and initialize variables and matrices */ initMatrices(&arguments, &options); /* ******************************************* */ gettimeofday(&start_time, NULL); /* start timer */ calculate(&arguments, &results, &options); /* solve the equation */ gettimeofday(&comp_time, NULL); /* stop timer */ if (0 == mpis.rank) { displayStatistics(&arguments, &results, &options); /* **************** */ DisplayMatrix("Matrix:", /* display some */ arguments.Matrix[results.m][0], options.interlines); /* statistics and */ } freeMatrices(&arguments); freeMPI(&mpis); /* free memory */ /* **************** */ //MPI_Barrier(MPI_COMM_WORLD); MPI_Finalize(); return 0; }
/* ************************************************************************ */ static void calculate (struct calculation_arguments const* arguments, struct calculation_results *results, struct options const* options) { int i, j; /* local variables for loops */ int m1, m2; /* used as indices for old and new matrices */ double star; /* four times center value minus 4 neigh.b values */ double residuum; /* residuum of current iteration */ double maxresiduum; /* maximum residuum value of a slave in iteration */ int const N = arguments->N; double const h = arguments->h; double pih = 0.0; double fpisin = 0.0; int term_iteration = options->term_iteration; /* initialize m1 and m2 depending on algorithm */ if (options->method == METH_JACOBI) { m1 = 0; m2 = 1; } else { m1 = 0; m2 = 0; } if (options->inf_func == FUNC_FPISIN) { pih = PI * h; fpisin = 0.25 * TWO_PI_SQUARE * h * h; } #if defined(ROWS) || defined(COLS) || defined(ELEMENTS) omp_set_num_threads(options->number); #endif while (term_iteration > 0) { double** Matrix_Out = arguments->Matrix[m1]; double** Matrix_In = arguments->Matrix[m2]; maxresiduum = 0; #if !defined(ROWS) && !defined(COLS) && !defined(ELEMENTS) for (i = 1; i < N; i++) { double fpisin_i = 0.0; if (options->inf_func == FUNC_FPISIN) { fpisin_i = fpisin * sin(pih * (double)i); } /* over all columns */ for (j = 1; j < N; j++) { #endif #ifdef ROWS #pragma omp parallel for private(j, star, residuum) reduction(max : maxresiduum) /* over all rows */ for (i = 1; i < N; i++) { double fpisin_i = 0.0; if (options->inf_func == FUNC_FPISIN) { fpisin_i = fpisin * sin(pih * (double)i); } /* over all columns */ for (j = 1; j < N; j++) { #endif #ifdef COLS #pragma omp parallel for private(i, star, residuum) reduction(max : maxresiduum) /* over all colums */ for (j = 1; j < N; j++) { /* over all rows */ for (i = 1; i < N; i++) { double fpisin_i = 0.0; if (options->inf_func == FUNC_FPISIN) { fpisin_i = fpisin * sin(pih * (double)i); } #endif #ifdef ELEMENTS int k; #pragma omp parallel for private(i, j, star, residuum) reduction(max : maxresiduum) for (k = 0; k < ((N-1) * (N-1) - 1); k++) { j = k % (N - 1) + 1; // +1 weil k bei 0 anfängt i = k / N +1; // i ist int, die Nachkommastellen fallen weg; +1 weil k bei 0 anfängt double fpisin_i = 0.0; if (options->inf_func == FUNC_FPISIN) { fpisin_i = fpisin * sin(pih * (double)i); } #endif star = 0.25 * (Matrix_In[i-1][j] + Matrix_In[i][j-1] + Matrix_In[i][j+1] + Matrix_In[i+1][j]); if (options->inf_func == FUNC_FPISIN) { star += fpisin_i * sin(pih * (double)j); } if (options->termination == TERM_PREC || term_iteration == 1) { residuum = Matrix_In[i][j] - star; residuum = (residuum < 0) ? -residuum : residuum; maxresiduum = (residuum < maxresiduum) ? maxresiduum : residuum; } Matrix_Out[i][j] = star; } #ifndef ELEMENTS } #endif results->stat_iteration++; results->stat_precision = maxresiduum; /* exchange m1 and m2 */ i = m1; m1 = m2; m2 = i; /* check for stopping calculation, depending on termination method */ if (options->termination == TERM_PREC) { if (maxresiduum < options->term_precision) { term_iteration = 0; } } else if (options->termination == TERM_ITER) { term_iteration--; } } results->m = m2; } /* ************************************************************************ */ /* displayStatistics: displays some statistics about the calculation */ /* ************************************************************************ */ static void displayStatistics (struct calculation_arguments const* arguments, struct calculation_results const* results, struct options const* options) { int N = arguments->N; 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("Speicherbedarf: %f MiB\n", (N + 1) * (N + 1) * sizeof(double) * arguments->num_matrices / 1024.0 / 1024.0); printf("Berechnungsmethode: "); if (options->method == METH_GAUSS_SEIDEL) { printf("Gauss-Seidel"); } else if (options->method == METH_JACOBI) { printf("Jacobi"); } printf("\n"); printf("Interlines: %" PRIu64 "\n",options->interlines); printf("Stoerfunktion: "); if (options->inf_func == FUNC_F0) { printf("f(x,y) = 0"); } else if (options->inf_func == FUNC_FPISIN) { printf("f(x,y) = 2pi^2*sin(pi*x)sin(pi*y)"); } printf("\n"); printf("Terminierung: "); if (options->termination == TERM_PREC) { printf("Hinreichende Genaugkeit"); } else if (options->termination == TERM_ITER) { printf("Anzahl der Iterationen"); } printf("\n"); printf("Anzahl Iterationen: %" PRIu64 "\n", results->stat_iteration); printf("Norm des Fehlers: %e\n", results->stat_precision); printf("\n"); } /****************************************************************************/ /** Beschreibung der Funktion DisplayMatrix: **/ /** **/ /** Die Funktion DisplayMatrix gibt eine Matrix **/ /** in einer "ubersichtlichen Art und Weise auf die Standardausgabe aus. **/ /** **/ /** Die "Ubersichtlichkeit wird erreicht, indem nur ein Teil der Matrix **/ /** ausgegeben wird. Aus der Matrix werden die Randzeilen/-spalten sowie **/ /** sieben Zwischenzeilen ausgegeben. **/ /****************************************************************************/ static void DisplayMatrix (struct calculation_arguments* arguments, struct calculation_results* results, struct options* options) { int x, y; double** Matrix = arguments->Matrix[results->m]; int const interlines = options->interlines; printf("Matrix:\n"); for (y = 0; y < 9; y++) { for (x = 0; x < 9; x++) { printf ("%7.4f", Matrix[y * (interlines + 1)][x * (interlines + 1)]); } printf ("\n"); } fflush (stdout); } /* ************************************************************************ */ /* main */ /* ************************************************************************ */ int main (int argc, char** argv) { struct options options; struct calculation_arguments arguments; struct calculation_results results; /* get parameters */ AskParams(&options, argc, argv); /* ************************* */ initVariables(&arguments, &results, &options); /* ******************************************* */ allocateMatrices(&arguments); /* get and initialize variables and matrices */ initMatrices(&arguments, &options); /* ******************************************* */ gettimeofday(&start_time, NULL); /* start timer */ calculate(&arguments, &results, &options); /* solve the equation */ gettimeofday(&comp_time, NULL); /* stop timer */ displayStatistics(&arguments, &results, &options); DisplayMatrix(&arguments, &results, &options); freeMatrices(&arguments); /* free memory */ return 0; }
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); calculate(); 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(); //testPrint(); /*MPI_Barrier(MPI_COMM_WORLD); if (rank == 0) { for (int i = 0; i < actuallines; i++) { for (int j = 0; j <= N; j++) { printf("%7.4f ", newmatrix[i][j]); } printf("\n"); } printf("\n"); } MPI_Barrier(MPI_COMM_WORLD); if (rank == 1) { for (int i = 0; i < actuallines; i++) { for (int j = 0; j <= N; j++) { printf("%7.4f ", newmatrix[i][j]); } printf("\n"); } printf("\n"); } MPI_Barrier(MPI_COMM_WORLD); if (rank == 2) { for (int i = 0; i < actuallines; i++) { for (int j = 0; j <= N; j++) { printf("%7.4f ", newmatrix[i][j]); } printf("\n"); } printf("\n"); } MPI_Barrier(MPI_COMM_WORLD); if (rank == 3) { for (int i = 0; i < actuallines; i++) { for (int j = 0; j <= N; j++) { printf("%7.4f ", newmatrix[i][j]); } printf("\n"); } printf("\n"); }*/ 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; }