void mexFunction(int nlhs, mxArray **plhs, int nrhs, const mxArray **prhs ) { #define f_IN prhs[0] #define f_OUT plhs[0] if(nrhs<1 || nrhs>1){ mexErrMsgTxt("Wrong number of input arguments"); return; } double *Fr,*Fi; size_t K = mxGetNumberOfDimensions(f_IN); const mwSize *N = mxGetDimensions(f_IN); Fr = (double*) calloc(N[0],sizeof(Fr)); Fi = (double*) calloc(N[1],sizeof(Fi)); callFFTN(mxGetPr(f_IN),K,N,&Fr,&Fi); DisplayMatrix("SOL",Fr,Fi,N[0],N[1]); fftshiftC(Fr,Fi,N[1]); DisplayMatrix("SOL",Fr,Fi,N[0],N[1]); return; }
void iAuction::MainAlgo(){ InitAlgo(); lu_seq.clear(); size_sum=0; while(allocated_cols.size()<row_size){ int next = -1; for(uint j=0; j<col_size; j++) if(record_allocations[j]>1){ next = j; break; } assert(next!=-1); // _cout("Selected column: "<<next<<endl); //Preprocess(); MainStage(next); }//while //DoubleCheck(); #if 0 cout<<"Final solution:"<<endl; DisplayMatrix(orig_matrix, assignment, true); cout<<"Assigned row-col pairs: "<<endl; DisplayAssignment(); cout<<"Maximization result: "<<ComputeCostSum(orig_matrix, assignment)<<endl; #endif }
void callFFTN(double *data, size_t K, const mwSize *N, double **sol_r, double **sol_i ) { mxArray *F,*f; mxArray *LHS[1]; int N1 = N[0]; int N2 = N[1]; f = mxCreateNumericArray(K,N,mxDOUBLE_CLASS,mxCOMPLEX); memcpy(mxGetPr(f),data,sizeof(double)*N1*N2); mexCallMATLABWithTrap(1,LHS,1,&f,"fftn"); F = LHS[0]; *sol_r = (double *)mxGetData(F); *sol_i = (double *)mxGetImagData(F); DisplayMatrix("F",mxGetPr(F),mxGetPi(F),N1,N2); mxDestroyArray(f); // mxDestroyArray(F); }
/* ************************************************************************ */ 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 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; /* 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; }
bool LibTest::TestMatrix() { mat::Matrix4 testMatrix = { { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0 } }; std::cout << "testMatrix: " << std::endl; std::cout << "Size: " << sizeof( mat::Matrix4 ) << " Size of double x16: " << sizeof( double ) * 16 << std::endl; DisplayMatrix( testMatrix ); memset( &testMatrix, 0, sizeof( mat::Matrix4 ) ); DisplayMatrix( testMatrix ); TestMatrix4Identity(); TestMatrix4Operators(); TestMatrix4Mult(); return true; }
bool LibTest::TestMatrix4Mult() const { mat::Matrix4 matA ( mat::k_identity ), matB = { { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0 } }; std::cout << "TestMatrix4Mult()\nmatA x matB:\n"; DisplayMatrix( mat::Multiply( matA, matB ) ); std::cout << "matB x matA:\n"; DisplayMatrix( mat::Multiply( matB, matA ) ); std::cout << "identity x 2\n"; mat::Matrix4 test = { { 2, 0, 0, 0, 0, 2, 0, 0, 0, 0, 2, 0, 0, 0, 0, 2 } }; matA.Multiply( test ); DisplayMatrix( matA ); std::cout << "matA x matB:\n"; DisplayMatrix( mat::Multiply( matA, matB ) ); std::cout << "matB x matA:\n"; DisplayMatrix( mat::Multiply( matB, matA ) ); return true; }
/* ************************************************************************ */ 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; }
void CallEig(double *Data, int M, int N) { /* Perform QR factorization by calling the MATLAB function */ mxArray *Var, *Vec, *A; mxArray *ppLhs[2]; DisplayMatrix("Input", Data, M, N); A = mxCreateDoubleMatrix(M, N, mxREAL); /* Put input in an mxArray */ memcpy(mxGetPr(A), Data, sizeof(double)*M*N); mexCallMATLAB(2, ppLhs, 1, &A, "eig"); /* Call MATLAB's qr function */ Var = ppLhs[0]; Vec = ppLhs[1]; DisplayMatrix("Var", mxGetPr(Var), M, N); DisplayMatrix("Vec", mxGetPr(Vec), M, N); mxDestroyArray(Var); /* No longer need these */ mxDestroyArray(Vec); mxDestroyArray(A); }
bool LibTest::TestMatrix4Identity() const { mat::Matrix4 const identityMatrixCopy ( mat::k_identity ); std::cout << "identityMatrixCopy:\n"; DisplayMatrix( identityMatrixCopy ); mat::Matrix4 const identityMatrixAssignment = identityMatrixCopy; std::cout << "identityMatrixAssignment:\n"; DisplayMatrix( identityMatrixAssignment ); mat::Matrix4 uninitializedMatrix; std::cout << "uninitializedMatrix:\n"; DisplayMatrix( uninitializedMatrix ); uninitializedMatrix.SetIdentity(); std::cout << "uninitializedMatrix.SetIdentity()\n"; DisplayMatrix( uninitializedMatrix ); return true; }
/* ************************************************************************ */ 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; }
void CallQR(double *Data, int M, int N) { /* Perform QR factorization by calling the MATLAB function */ mxArray *S, *V, *U , *A; mxArray *ppLhs[3]; DisplayMatrix("Input", Data, M, N); A = mxCreateDoubleMatrix(M, N, mxREAL); /* Put input in an mxArray */ memcpy(mxGetPr(A), Data, sizeof(double)*M*N); mexCallMATLAB(2, ppLhs, 1, &A, "svd"); /* Call MATLAB's qr function */ U = ppLhs[0]; S = ppLhs[1]; V = ppLhs[2]; DisplayMatrix("U", mxGetPr(U), M, N); DisplayMatrix("S", mxGetPr(S), M, N); DisplayMatrix("V", mxGetPr(V), M, N); mxDestroyArray(U); /* No longer need these */ mxDestroyArray(S); mxDestroyArray(V); mxDestroyArray(A); }
/* ************************************************************************ */ 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; }
void CPFAOptimize::Example() { CMainFrame* pMainFrame = NULL; pMainFrame = (CMainFrame*)AfxGetApp()->m_pMainWnd; CChildFrame* pChildFrame = NULL; pChildFrame = (CChildFrame*)pMainFrame->GetActiveFrame(); CAutoPFAView* pAutoView = NULL; pAutoView = (CAutoPFAView*)pChildFrame->GetActiveView(); ComponentManager* pComManager = pAutoView->GetDocument()->m_scenarioManager.GetCurScenario()->GetCompManager(); JunSystemTransformCircleSystem( *pComManager, OptData ); DisplayMatrix( "F://CirPipe.txt", 100, OptData ); InitializePipeCircle( OptData ); DisplayMatrix( "F://Pipe.txt", 102, OptData ); // 从pComManager中初始化OptData的 初始实际流量,管道长度,节点流量,配水源,控制点, // 先手动设置配水源和控制点 int iTemp = 1; OptData.SetProperty( 0, 200, iTemp ); // 节点1为配水源 iTemp = 6; OptData.SetProperty( 0, 201, iTemp ); // 节点10为控制点 CMinimumSquareSum MinSquSum; //MinSquSum.InitializeFactFlux( *pComManager, OptData ); // 初始实际流量 /////////////////////////////////////// int i = 0; int iKeyOfPipe = 0; double Temp = 0.0; Temp = 0.03; OptData.SetProperty( 1, 1, Temp ); OptData.SetProperty( 3, 1, Temp ); OptData.SetProperty( 6, 1, Temp ); OptData.SetProperty( 7, 1, Temp ); Temp = 0.02; OptData.SetProperty( 2, 1, Temp ); OptData.SetProperty( 4, 1, Temp ); OptData.SetProperty( 5, 1, Temp ); //////////////////////////////////////// double dTemp = 0.0; Pipe* pPipe = NULL; Component* pComponent = NULL; map< int, double > BeforeDiameterMap; // 存储了上一次计算的直径 int iIteratorTime = 0; // 迭代次数 IteratorPtr<Component> PipeItPtr( pComManager->CreatPipeIterator() ); for( PipeItPtr->Fist(); !PipeItPtr->IsDone(); PipeItPtr->Next() ) { pComponent = &PipeItPtr->CurrentItem(); pPipe = dynamic_cast<Pipe*>( pComponent ); dTemp = pPipe->Len(); OptData.SetProperty( pPipe->GetKey(), 2, dTemp ); dTemp = pPipe->InDia(); OptData.SetProperty( pPipe->GetKey(), 4, dTemp ); } // 节点流量 QuantityManager& qm = QuantityManager::Instance(); CString strUnit = ""; CString strValue = ""; Jun* pJun = NULL; IteratorPtr<Component> JunItPtr( pComManager->CreatJunIterator() ); for( JunItPtr->Fist(); !JunItPtr->IsDone(); JunItPtr->Next() ) { pComponent = &JunItPtr->CurrentItem(); pJun = dynamic_cast<Jun*>( pComponent ); strUnit = pJun->ms_InitGuess.GetValue( 2 ); // 获得单位 strValue = pJun->ms_InitGuess.GetValue( 1 ); TCHAR* pChar = NULL; qm.TransformToStd( dTemp, strUnit.GetBuffer( 32 ), _tcstod( strValue.GetBuffer( 32 ), &pChar ) ); // 转化成标准单位 if( IDS_STRVOLUMEFLOW != _ttoi( pJun->ms_InitGuess.GetValue( 0 ).GetBuffer( 32 ) ) ) // 质量流量,要转化为体积流量 { double dTempDensity = 0.0; NumFlyWeight *pDensity = &( ( pComManager->SysProperty().GetFuild() )->ms_Density ); dTemp *= pDensity->GetNum(); // GetNum返回标准单位数 } OptData.SetProperty( pJun->GetKey(), 3, dTemp ); } //////////// dTemp = 0.06; OptData.SetProperty( 1, 3, dTemp ); dTemp = -0.06; OptData.SetProperty( 6, 3, dTemp ); //////////// // 初始化经济参数 OptData.SetAModulus( 8.4 ); OptData.SetBModulus( 107 ); OptData.SetAlfaModulus( 1.6 ); OptData.SetDisinvestmentTime( 1.0e3 ); OptData.SetRepairModulus( 3.3 ); OptData.SetEletricityPrice( 0.5 ); OptData.SetAsymmetryModulus( 1.0 ); OptData.SetPumpEfficiency( 0.7 ); OptData.SetKModulus( 1.34e-10 ); OptData.SetMModulus( 5.3 ); OptData.SetNModulus( 2 ); OptData.Initialization(); BeforeDiameterMap.clear(); iIteratorTime = 0; do { // 初始化虚流量 MinSquSum.InitializeDummyFlux( *pComManager, OptData ); // 虚流量平差 CalEconomyDiameterAndWaterHarmer( *pComManager, 0.0001, 5000, OptData ); // 通过经济管径计算标准管径 FindStandardDiameter( OptData ); // 实际流量平差 CalFactFlux( *pComManager, 0.0001, 5000, OptData ); // 迭代次数加一 iIteratorTime++; } while( !DiameterIsEqual( OptData, BeforeDiameterMap ) && ( 5000 > iIteratorTime ) ); // 比较前后管径,不相等则跳到 初始化虚流量 // 计算其他要素 CalOthersVariable( *pComManager, OptData ); // 输出结果 }
/* ************************************************************************ */ 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; }
uint iAuction::MainStage(uint s){ InitStage(); int sink = -1; //here sink is a column int i_bar= -1; int j_bar= -1; SV.insert(s); UpdateLabeledRows(s); while(sink == -1){ double delta = std::numeric_limits<double>::infinity(); for(set<uint>::iterator itr=LU.begin(); itr!=LU.end(); itr++){ uint cur_j = heaps[*itr].top().first; while(SV.find(cur_j)!=SV.end()){ heaps[*itr].pop(); cur_j = heaps[*itr].top().first; } if(SV.find(cur_j) == SV.end()){ if((orig_matrix[*itr][GetAssignedCol(*itr)]-Deltas[GetAssignedCol(*itr)]) - (orig_matrix[*itr][cur_j] - Deltas[cur_j]) < delta){ delta = (orig_matrix[*itr][GetAssignedCol(*itr)]-Deltas[GetAssignedCol(*itr)]) - (orig_matrix[*itr][cur_j] - Deltas[cur_j]); j_bar = cur_j; i_bar = *itr; } }//if SV }//for assert(j_bar != -1 && i_bar != -1); //pred[i_bar] = j_bar; pred[i_bar].push_back(j_bar); assert(pred_j[j_bar] == -1); pred_j[j_bar] = i_bar; for(set<uint>::iterator itr=SV.begin(); itr!=SV.end(); itr++) Deltas[*itr] += delta; price_cnt ++; if(!GetNumAsgnRows(j_bar)){ sink = j_bar; } else{ //update LU SV SV.insert(j_bar); UpdateLabeledRows(j_bar); } #if 0 //display data _cout("\tset SV: "); DisplaySet(SV); _cout("\tset LU: "); DisplaySet(LU); //_cout("\tpred: "); //DisplayVec<int>(pred); _cout("\tdalta="<<delta<<"; j*="<<j_bar<<endl); _cout("\tDeltas: "); DisplayVec<double>(Deltas); _cout(endl); #endif }//while sink assert(sink!=-1); //update the assignment and allocation allocated_cols.insert(sink); int j_asgn=-1; uint cnt = 0; uint true_pred = sink; while(j_asgn!=(int)s){ if(cnt++ > col_size) throw EXCEPTION_BROKEN; j_asgn=assignment[i_bar]; assignment[i_bar] = true_pred; #if 0 _cout("\ti_bar="<<i_bar<<" j_asgn="<<j_asgn<<" pred="<<true_pred<<endl); #endif if(pred_j[j_asgn] != -1){ i_bar=pred_j[j_asgn]; for(uint j=0; j<pred[i_bar].size(); j++) if((int)pred[i_bar][j] == j_asgn){ true_pred = pred[i_bar][j]; break; } } //assert(i<row_size || j_asgn==(int)s); }//while //update num_allocation, by re-scanning num of assigned rows in each column UpdateRecordAllocations(); #if 0 _cout("Stats of this stage:"<<endl); _cout("\tstart col = "<<s<<"; sink col = "<<sink<<endl); _cout("\tassignment: "); DisplayVec<uint>(assignment); _cout("\tallocation: "); DisplayVec<uint>(allocation); _cout("\trecord_allocations: "); DisplayVec<uint>(record_allocations); _cout("transformed matrix with current assignment and Deltas: "<<endl); DisplayMatrix(orig_matrix, assignment, Deltas); _cout("-----------------------------------------------------"<<endl); #endif return (uint)sink; }
int main(int argc, char* argv[]) { cl_int cl_error; /** Init **/ unsigned int matrix_size = MATRIX_SIZE; unsigned int matrix_total_size = matrix_size*matrix_size; size_t cl_buff_size = matrix_total_size * sizeof(MATRIX_TYPE); printf("Matrix size : %d (%d length)\t |\t sous bloc de %d\n",matrix_size,matrix_total_size,LOCAL_DIM_KERNEL); MATRIX_TYPE * matA = malloc(matrix_total_size * sizeof(*matA)); MATRIX_TYPE * matB = malloc(matrix_total_size * sizeof(*matB)); MATRIX_TYPE * matC = malloc(matrix_total_size * sizeof(*matC)); MATRIX_TYPE * matD = malloc(matrix_total_size * sizeof(*matD)); // Init matA InitMatrix2(matA, matrix_size); // InitMatrix(matB, matrix_size); printf("Matrix A:\n"); DisplayMatrix(matA,matrix_size); // Init GPU cl_uint nb_platf; clGetPlatformIDs(0, NULL, &nb_platf); printf("Nombre de plateformes: %d\t", nb_platf); cl_platform_id platfs[nb_platf]; clGetPlatformIDs(nb_platf, platfs, NULL); size_t plat_name_size; clGetPlatformInfo(platfs[0], CL_PLATFORM_NAME, 0, NULL, &plat_name_size); char plat_name[plat_name_size]; clGetPlatformInfo(platfs[0], CL_PLATFORM_NAME, plat_name_size, &plat_name, NULL); printf("( %s ", plat_name); size_t plat_vendor_size; clGetPlatformInfo(platfs[0], CL_PLATFORM_VENDOR, 0, NULL, &plat_vendor_size); char plat_vendor[plat_vendor_size]; clGetPlatformInfo(platfs[0], CL_PLATFORM_VENDOR, plat_vendor_size, &plat_vendor, NULL); printf("| %s)\n", plat_vendor); cl_uint nb_devs; clGetDeviceIDs(platfs[0], CL_DEVICE_TYPE_ALL, 0, NULL, &nb_devs); cl_device_id devs[nb_devs]; clGetDeviceIDs(platfs[0], CL_DEVICE_TYPE_ALL, nb_devs, devs, NULL); cl_context ctx = clCreateContext(NULL, nb_devs, devs, NULL, NULL, NULL); cl_command_queue command_queue = clCreateCommandQueue(ctx, devs[0], CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE | CL_QUEUE_PROFILING_ENABLE, NULL); /** Chargement des kernels **/ cl_kernel ker1, ker2, ker3; bool verbose = false; CreateKernel(&ker1, ctx, devs, nb_devs, "./cholesky_kernel1.cl", "cholesky_diag", verbose); CreateKernel(&ker2, ctx, devs, nb_devs, "./cholesky_kernel2.cl", "cholesky_inf", verbose); CreateKernel(&ker3, ctx, devs, nb_devs, "./cholesky_kernel3.cl", "cholesky_subdiag", verbose); int nombre_de_kernel = 3; cl_event ev_ker[nombre_de_kernel], ev_readA; // Création/Préparation du buffer GPU cl_mem bufA = clCreateBuffer(ctx, CL_MEM_READ_WRITE, cl_buff_size, NULL, &cl_error); CHECK_ERROR(cl_error,"clCreateBuffer"); clEnqueueWriteBuffer(command_queue, bufA, CL_TRUE, 0, cl_buff_size, matA, 0, NULL, &ev_ker[nombre_de_kernel-1]); // ev_ker[n-1] pour préparer la boucle. size_t globalDim[] = {matrix_size, matrix_size}; size_t localDim[] = {LOCAL_DIM_KERNEL, LOCAL_DIM_KERNEL}; // GPU Calculs int i; for (i=0 ; i<matrix_size/LOCAL_DIM_KERNEL/**/ ; i++) { // Kernel 1 : Bloc diagonal clSetKernelArg(ker1, 0, sizeof(bufA), &bufA); clSetKernelArg(ker1, 1, sizeof(i), &i); clEnqueueNDRangeKernel(command_queue, ker1, 2, NULL, globalDim, localDim, 1, &ev_ker[nombre_de_kernel-1], &ev_ker[0]); // Kernel 2 : Blocs sous-diagonaux inferieur clSetKernelArg(ker2, 0, sizeof(bufA), &bufA); clSetKernelArg(ker2, 1, sizeof(i), &i); clEnqueueNDRangeKernel(command_queue, ker2, 2, NULL, globalDim, localDim, 1, &ev_ker[0], &ev_ker[1]); // Kernel 3 : Blocs sous-diagonaux clSetKernelArg(ker3, 0, sizeof(bufA), &bufA); clSetKernelArg(ker3, 1, sizeof(i), &i); clEnqueueNDRangeKernel(command_queue, ker3, 2, NULL, globalDim, localDim, 1, &ev_ker[1], &ev_ker[2]); } clEnqueueReadBuffer(command_queue, bufA, CL_TRUE, 0, cl_buff_size, matB, 1, &ev_ker[nombre_de_kernel-1], &ev_readA); clFinish(command_queue); // clGetEvenProfilingInfo clReleaseMemObject(bufA); /********************/ /*** CHECK RESULT ***/ /********************/ // ClearUpMatrix(matB,matrix_size); printf("\nMatrix B:\n"); DisplayMatrix(matB,matrix_size); /**/ ClearUpMatrix(matB,matrix_size); TransposeMatrix(matB,matC,matrix_size); MullMatrix(matB,matC,matD,matrix_size); MinusMatrix(matD,matA,matrix_size); printf("\nMatrix D - A:\n"); DisplayMatrix(matD,matrix_size); //*/ // Free free(matA); free(matB); free(matC); free(matD); 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; }