int main(int argc,char **argv) { PetscErrorCode ierr; /* used to check for functions returning nonzeros */ Vec x, f; /* solution, function */ Mat J; /* Jacobian matrix */ Tao tao; /* Tao solver context */ PetscInt i; /* iteration information */ PetscReal hist[100],resid[100]; PetscInt lits[100]; AppCtx user; /* user-defined work context */ PetscInitialize(&argc,&argv,(char *)0,help); /* Allocate vectors */ ierr = VecCreateSeq(MPI_COMM_SELF,NPARAMETERS,&x);CHKERRQ(ierr); ierr = VecCreateSeq(MPI_COMM_SELF,NOBSERVATIONS,&f);CHKERRQ(ierr); /* Create the Jacobian matrix. */ ierr = MatCreateSeqDense(MPI_COMM_SELF,NOBSERVATIONS,NPARAMETERS,NULL,&J);CHKERRQ(ierr); for (i=0;i<NOBSERVATIONS;i++) user.idm[i] = i; for (i=0;i<NPARAMETERS;i++) user.idn[i] = i; /* Create TAO solver and set desired solution method */ ierr = TaoCreate(PETSC_COMM_SELF,&tao);CHKERRQ(ierr); ierr = TaoSetType(tao,TAOPOUNDERS);CHKERRQ(ierr); /* Set the function and Jacobian routines. */ ierr = InitializeData(&user);CHKERRQ(ierr); ierr = FormStartingPoint(x);CHKERRQ(ierr); ierr = TaoSetInitialVector(tao,x);CHKERRQ(ierr); ierr = TaoSetSeparableObjectiveRoutine(tao,f,EvaluateFunction,(void*)&user);CHKERRQ(ierr); ierr = TaoSetJacobianRoutine(tao, J, J, EvaluateJacobian, (void*)&user);CHKERRQ(ierr); /* Check for any TAO command line arguments */ ierr = TaoSetFromOptions(tao);CHKERRQ(ierr); ierr = TaoSetConvergenceHistory(tao,hist,resid,0,lits,100,PETSC_TRUE);CHKERRQ(ierr); /* Perform the Solve */ ierr = TaoSolve(tao);CHKERRQ(ierr); ierr = TaoView(tao,PETSC_VIEWER_STDOUT_SELF);CHKERRQ(ierr); /* Free TAO data structures */ ierr = TaoDestroy(&tao);CHKERRQ(ierr); /* Free PETSc data structures */ ierr = VecDestroy(&x);CHKERRQ(ierr); ierr = VecDestroy(&f);CHKERRQ(ierr); ierr = MatDestroy(&J);CHKERRQ(ierr); PetscFinalize(); return 0; }
/*@ TaoSolve - Solves an unconstrained minimization problem. Call TaoSolve() after calling TaoCreate() and optional routines of the form TaoSetXXX(). Collective on TAO_SOLVER Input Parameters: . tao - the TAO_SOLVER solver context Notes: By default the TAO solvers use an initial starting guess of zero. To provide an alternative initial guess, the user must call TaoAppSetInitialSolutionVec() before calling TaoSolve(). Level: advanced .keywords: Solve .seealso: TaoCreate(), TaoDestroy() @*/ int TaoSolve(TAO_SOLVER tao) { int info; TaoVec *xx; TaoFunctionBegin; TaoValidHeaderSpecific(tao,TAO_COOKIE,1); info = TaoGetSolution(tao,&xx);CHKERRQ(info); info = tao->taoappl->InitializeVariables(xx);CHKERRQ(info); info = TaoSetUp(tao);CHKERRQ(info); info = TaoSetDefaultStatistics(tao); CHKERRQ(info); if (tao->solve){ info = (*(tao)->solve)(tao,tao->data);CHKERRQ(info); } if (tao->viewtao) { info = TaoView(tao);CHKERRQ(info); } if (tao->viewksptao) { info = TaoViewLinearSolver(tao);CHKERRQ(info); } TaoFunctionReturn(0); }
int main (int argc, char **argv) { double startTime; int info; /* used to check for functions returning nonzeros */ GAVec ga_x; /* solution vector */ TAO_SOLVER tao; /* TAO_SOLVER solver context */ TAO_GA_APPLICATION taoapp; /* TAO application context */ TaoTerminateReason reason; AppCtx user; /* user-defined application context */ /*initialize GA and MPI */ int heap = 400000, stack = 400000; MPI_Init (&argc, &argv); /* initialize MPI */ GA_Initialize (); /* initialize GA */ user.me = GA_Nodeid (); user.nproc = GA_Nnodes (); startTime = MPI_Wtime(); if (user.me == 0) { if (GA_Uses_fapi ()) GA_Error ("Program runs with C array API only", 0); printf ("Using %ld processes\n", (long) user.nproc); fflush (stdout); } heap /= user.nproc; stack /= user.nproc; if (!MA_init (MT_F_DBL, stack, heap)) GA_Error ("MA_init failed", stack + heap); /* initialize memory allocator */ /* Initialize TAO */ TaoInitialize (&argc, &argv, (char *) 0, help); /* Initialize problem parameters */ user.ndim = NDIM; user.natoms = NATOMS; user.BlockSize = BLOCKSIZE; /* Allocate vectors for the solution and gradient */ int dims[2]; dims[0] = user.ndim*user.natoms; ga_x = NGA_Create (C_DBL, 1, dims, "GA_X", NULL); if (!ga_x) GA_Error ("lennard-jones.main::NGA_Create ga_x", ga_x); /* Set up structures for data distribution */ info = SetupBlocks(&user); CHKERRQ(info); /* The TAO code begins here */ /* Create TAO solver with desired solution method */ info = TaoCreate (MPI_COMM_WORLD, "tao_lmvm", &tao); CHKERRQ(info); info = TaoGAApplicationCreate (MPI_COMM_WORLD, &taoapp); CHKERRQ(info); /* Set the initial solution */ info = InitializeVariables(ga_x, &user); CHKERRQ(info); info = TaoGAAppSetInitialSolutionVec(taoapp, ga_x); CHKERRQ(info); /* Set routines for function, gradient */ info = TaoGAAppSetObjectiveAndGradientRoutine (taoapp, FormFunctionGradient, (void *) &user); CHKERRQ(info); /* Check for TAO command line options */ info = TaoSetFromOptions (tao); CHKERRQ(info); /* SOLVE THE APPLICATION */ info = TaoSolveGAApplication (taoapp, tao); CHKERRQ(info); /* To View TAO solver information use */ info = TaoView(tao); CHKERRQ(info); /* Get termination information */ info = TaoGetTerminationReason (tao, &reason); if(info) GA_Error("lennard-jones.main.TaoGetTerminationReason",info); if (user.me == 0) { if (reason <= 0) printf("Try a different TAO method, adjust some parameters, or check the function evaluation routines\n"); printf("WALL TIME TAKEN = %lf\n", MPI_Wtime()-startTime); /*output the solutions */ printf ("The solution is :\n"); } GA_Print (ga_x); /* Free TAO data structures */ info = TaoDestroy (tao); CHKERRQ(info); info = TaoGAAppDestroy (taoapp); CHKERRQ(info); /* Free GA data structures */ GA_Destroy (ga_x); if (!MA_pop_stack(user.memHandle)) ga_error("Main::MA_pop_stack for memHandle failed",0); /* Finalize TAO, GA, and MPI */ TaoFinalize (); GA_Terminate (); MPI_Finalize (); return 0; }
int main (int argc, char **argv) { double startTime; int info; /* used to check for functions returning nonzeros */ GAVec ga_x; /* solution vector */ TAO_SOLVER tao; /* TAO_SOLVER solver context */ TAO_GA_APPLICATION taoapp; /* TAO application context */ TaoTerminateReason reason; AppCtx user; /* user-defined application context */ /*initialize GA and MPI */ int heap = 4000, stack = 4000; MPI_Init (&argc, &argv); /* initialize MPI */ GA_Initialize (); /* initialize GA */ if (!MA_init(MT_F_DBL, stack, heap)) GA_Error((char*)"MA_init failed", stack+heap); /* Initialize TAO */ TaoInitialize (&argc, &argv, (char *) 0, help); startTime = MPI_Wtime(); /* Initialize problem parameters */ user.natoms = NATOMS; user.ndim = NDIM; user.n = user.natoms*user.ndim; /* Create working space */ if (MA_push_stack(C_DBL, 2*user.n, "Vector buffers", &user.memHandle) == MA_FALSE) GA_Error((char*)"MAIN::ma_alloc_get failed",2*user.n); /* Allocate Global Array vector for the solution */ int dims[2]; dims[0] = user.n; ga_x = NGA_Create (C_DBL, 1, dims, (char*)"GA_X", NULL); if (!ga_x) GA_Error ((char*)"lennard-jones.main::NGA_Create ga_x", ga_x); /* The TAO code begins here */ /* Create TAO solver with desired solution method */ info = TaoCreate (MPI_COMM_WORLD, "tao_cg", &tao); CHKERRQ(info); info = TaoGAApplicationCreate (MPI_COMM_WORLD, &taoapp); CHKERRQ(info); /* Set initial vector */ info = InitializeVariables(ga_x, &user); CHKERRQ(info); info = TaoGAAppSetInitialSolutionVec(taoapp, ga_x); CHKERRQ(info); /* Set routines for function, gradient */ info = TaoGAAppSetObjectiveAndGradientRoutine (taoapp, FormFunctionGradient, (void *) &user); CHKERRQ(info); /* Check for TAO command line options */ info = TaoSetFromOptions (tao); CHKERRQ(info); /* SOLVE THE APPLICATION */ info = TaoSolveGAApplication (taoapp, tao); CHKERRQ(info); /* To View TAO solver information use */ info = TaoView(tao); CHKERRQ(info); /* Get termination information */ info = TaoGetTerminationReason (tao, &reason); CHKERRQ(info); if (reason <= 0) printf("Try a different TAO method, adjust some parameters, or check the function evaluation routines\n"); printf("TIME TAKEN = %lf\n", MPI_Wtime()-startTime); /*output the solutions */ printf ("The solution is :\n"); GA_Print (ga_x); /* Free TAO data structures */ info = TaoDestroy (tao); CHKERRQ(info); info = TaoGAAppDestroy (taoapp); CHKERRQ(info); /* Free GA data structures */ GA_Destroy (ga_x); if (!MA_pop_stack(user.memHandle)) GA_Error((char*)"Main::MA_pop_stack failed",0); /* Finalize TAO, GA, and MPI */ TaoFinalize (); GA_Terminate (); MPI_Finalize (); return 0; }