Commit af87fed7 by Francois Gygi

removed USE_MPI macros


git-svn-id: http://qboxcode.org/svn/qb/trunk@1808 cba15fb0-1239-40c8-b417-11db7ca47a34
parent 48feca8c
......@@ -15,100 +15,95 @@
// testAndersonMixer.C
//
////////////////////////////////////////////////////////////////////////////////
// $Id: testAndersonMixer.C,v 1.7 2009-03-08 01:16:33 fgygi Exp $
#include <iostream>
#include <vector>
using namespace std;
#include "Context.h"
#include "AndersonMixer.h"
// use: testAndersonMixer ndim nmax niter
int main(int argc, char** argv)
{
#if USE_MPI
int mype;
MPI_Comm comm = MPI_COMM_WORLD;
MPI_Init(&argc,&argv);
#endif
MPI_Comm_rank(MPI_COMM_WORLD,&mype);
if ( argc != 4 )
{
if ( argc != 4 )
{
cout << " use: testAndersonMixer ndim nmax niter" << endl;
return 1;
}
const int ndim = atoi(argv[1]);
const int nmax = atoi(argv[2]);
const int niter = atoi(argv[3]);
Context ctxt;
cout << " use: testAndersonMixer ndim nmax niter" << endl;
return 1;
}
// ndim: dimension of vector x
const int ndim = atoi(argv[1]);
// nmax: maximum dimension of subspace used for acceleration
const int nmax = atoi(argv[2]);
// niter: number of iterations
const int niter = atoi(argv[3]);
char processor_name[MPI_MAX_PROCESSOR_NAME];
int namelen;
PMPI_Get_processor_name(processor_name,&namelen);
// cout << " Process " << ctxt.mype() << " on " << processor_name << endl;
char processor_name[MPI_MAX_PROCESSOR_NAME];
int namelen;
PMPI_Get_processor_name(processor_name,&namelen);
// cout << " Process " << ctxt.mype() << " on " << processor_name << endl;
const double alpha = 0.1;
vector<double> x,f,xbar,fbar;
x.resize(ndim);
f.resize(ndim);
xbar.resize(ndim);
fbar.resize(ndim);
const double alpha = 0.1;
vector<double> x,f,xbar,fbar;
x.resize(ndim);
f.resize(ndim);
xbar.resize(ndim);
fbar.resize(ndim);
AndersonMixer mixer(ndim,nmax,&ctxt);
AndersonMixer mixer(ndim,nmax,&comm);
for ( int i = 0; i < ndim; i++ )
x[i] = (i+5);
for ( int i = 0; i < ndim; i++ )
x[i] = (i+5);
for ( int iter = 0; iter < niter; iter++ )
{
// compute gradient
for ( int iter = 0; iter < niter; iter++ )
{
// compute gradient
#if 1
// quadratic form
for ( int i = 0; i < ndim; i++ )
f[i] = -(i+1) * (x[i]-i);
//f[i] = -(0.1*i+1)*(x[i]-i);
// quadratic form
for ( int i = 0; i < ndim; i++ )
f[i] = -(i+1) * (x[i]-i);
//f[i] = -(0.1*i+1)*(x[i]-i);
#if 1
// precondition f
for ( int i = 0; i < ndim; i++ )
f[i] *= 1.0/(i+3);
// precondition f
for ( int i = 0; i < ndim; i++ )
f[i] *= 1.0/(i+3);
#endif
#else
// Rosenbrock function
assert(ndim%2==0);
// f = sum_i^(n-1) (1-x_i)^2 + 100*(x_i+1 - x_i^2)^2
f[0] = 0.0;
for ( int i = 0; i < ndim-1; i++ )
{
f[i+1] = 200 * ( x[i+1] - x[i]*x[i] );
f[i] += -2.0 * ( 1.0 - x[i] ) + 200 * x[i] * ( x[i+1] - x[i]*x[i] );
}
#endif
double resnorm = 0.0;
for ( int i = 0; i < ndim; i++ )
resnorm += f[i]*f[i];
#if USE_MPI
ctxt.dsum(1,1,&resnorm,1);
// Rosenbrock function
assert(ndim%2==0);
// f = sum_i^(n-1) (1-x_i)^2 + 100*(x_i+1 - x_i^2)^2
f[0] = 0.0;
for ( int i = 0; i < ndim-1; i++ )
{
f[i+1] = 200 * ( x[i+1] - x[i]*x[i] );
f[i] += -2.0 * ( 1.0 - x[i] ) + 200 * x[i] * ( x[i+1] - x[i]*x[i] );
}
#endif
double resnorm = 0.0;
for ( int i = 0; i < ndim; i++ )
resnorm += f[i]*f[i];
double rbuf;
MPI_Allreduce(&resnorm,&rbuf,1,MPI_DOUBLE,MPI_SUM,MPI_COMM_WORLD);
resnorm = rbuf;
if ( mype == 0 )
cout << " resnorm: " << sqrt(resnorm) << endl;
mixer.update(&x[0],&f[0],&xbar[0],&fbar[0]);
mixer.update(&x[0],&f[0],&xbar[0],&fbar[0]);
#if 0
// precondition fbar
for ( int i = 0; i < ndim; i++ )
fbar[i] /= (i+3);
//fbar[i] *= 1.0/(i+2);
// precondition fbar
for ( int i = 0; i < ndim; i++ )
fbar[i] /= (i+3);
//fbar[i] *= 1.0/(i+2);
#endif
for ( int i = 0; i < ndim; i++ )
x[i] = xbar[i] + alpha * fbar[i];
}
for ( int i = 0; i < ndim; i++ )
x[i] = xbar[i] + alpha * fbar[i];
}
#if USE_MPI
MPI_Finalize();
#endif
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment