solver_ani.cpp 10.4 KB
Newer Older
Kirill Terekhov's avatar
Kirill Terekhov committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
#include "solver_ani.h"

#if defined(USE_SOLVER_ANI)

#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <math.h>


int ilu2 = 1;
double set_epsilon = 1e-5;

/* BiCGStab solver structure */
typedef struct 
{
    int n, nz, ierr, ipjLU, ipiw, ipBCG;
    double RESID;
    int ITER, INFO, NUNIT;
    int *iW;
    double *rW;
    int *ia, *ja;
    double *A;
    double eps;
} bcg;

typedef struct
{
	int n;
	int * ia;
	int * ja;
	double * A;
} matrix;

typedef struct
{
	int n;
	double * v;
} vector;

/* Initialize bcg solver */
int initbcg(bcg *s, matrix *A, double eps);
/* Reinitialize solver preconditioner with new matrix A */
int renewbcg(bcg *s, double *A);
/* Solve linear system */
int solvebcg(bcg *s, vector *b, vector *x);
/* Free memory used by solver */
void freebcg(bcg *s);



#if defined (__APPLE__) || defined(MAXOSX) || defined(__linux__)
#define ILUOO	iluoo_
#define DDOT	ddot_
#define ILU0	ilu0_
#define PREVEC0	prevec0_
#define PREVEC2	prevec2_
#define MATVEC	matvec_
#define SLPBCGS	slpbcgs_
#endif
#if defined(_WIN32) || defined(_WIN64) || defined(WIN32) || defined(WIN64)
#define ILUOO	ILUOO
#define DDOT	DDOT
#define ILU0	ILU0
#define PREVEC0	PREVEC0
#define PREVEC2	PREVEC2
#define MATVEC	MATVEC
#define SLPBCGS	SLPBCGS
#endif


extern "C"
{
//	 from blas 
	double DDOT(int *n, double *dx, int *incx, double *dy, int *incy);
//	 from aniILU 
    void ILU0(int *n, double *a, int *ja, int *ia, double *alu, int *jlu, int *ju, int *iw, int *ierr);
	void ILUOO(int *n, int *ia, int *ja, double *a, double *tau1, double *tau2, int *verb, 
		    double *rW, int *iW, int *MaxWr, int *MaxWi, double *partlur, double *partlurout, 
		    int *UsedWr, int *UsedWi, int *ierr);
	void PREVEC0(int *iprevec, int *ichange, double *x, double *y, int *iW, double *rW);
	void PREVEC2(int *iprevec, int *ichange, double *x, double *y, int *iW, double *rW);
	void MATVEC(int *imatvec, double *alpha, double *x, double *beta, double *y, int *ia, int *ja, double *a);
	void SLPBCGS(void (*prevec)(int *, int *, double *, double *, int *, double *), int *iprevec, int *iW, double *rW,
				  void (*matvec)(int *, double *, double *, double *, double *, int *, int *, double *), 
				  int *imatvec, int *ia, int *ja, double *a,
				  double *work, int *mw, int *nw,
				  int *n, double *rhs, double *sol,
				  int *iter, double *resid,
				  int *info, int *nuint);
}

/* for internal use */
static double residual_fortran_indices(matrix *A, vector *x, vector *b) 
{
    double d = 0.0, c;
    int i, j;
    for (i=0; i<A->n; i++, d+=c*c)  
		for (c=b->v[i], j=A->ia[i]-1; j<A->ia[i+1]-1; j++)  
			c -= A->A[j] * x->v[A->ja[j]-1];
    return sqrt(d);
}


/* Initialize solver with new matrix A */
static int newmatrixbcg(bcg *s, matrix *A, bool same_precond) 
{
	if( s->n != A->n && same_precond ) throw INMOST::CannotReusePreconditionerOfDifferentSize;
    s->n  = A->n;
    s->nz = A->ia[A->n] - A->ia[0];
    s->ia = A->ia,  s->ja = A->ja;
    if( !same_precond )
    {
		if (!ilu2) 
		{
			s->rW = (double*) realloc(s->rW, sizeof(double)*2*(s->nz + s->n*8    ));
			memset(s->rW,0,sizeof(double)*2*(s->nz + s->n*8    ));
			s->iW = (int*)    realloc(s->iW, sizeof(int)   *2*(s->nz + s->n*2 + 1));
			memset(s->iW,0,sizeof(int)   *2*(s->nz + s->n*2 + 1));
			s->ipBCG = s->nz;
		}
		else 
		{
			s->rW = (double*) realloc(s->rW, sizeof(double)*2*(s->nz*20)); // ilu2
			memset(s->rW,0,sizeof(double)*2*(s->nz*20));
			s->iW = (int*)    realloc(s->iW, sizeof(int)   *2*(s->nz*25)); // ilu2
			memset(s->iW,0,sizeof(int)   *2*(s->nz*25));
		}
		s->ipjLU = s->n + 1;
		s->ipiw  = s->ipjLU + s->nz;
		return renewbcg(s, A->A);
	}
	else return 0;
}

/* solver */
/* Initialize bcg solver */
int initbcg(bcg *s, matrix *A, double eps) 
{
    s->eps = eps;
    s->rW = NULL;
    s->iW = NULL;
    return newmatrixbcg(s, A, false);
}

/* Reinitialize solver preconditioner with new matrix A */
int renewbcg(bcg *s, double *A) 
{
    int ierr=0;
    int v, k;
    
    /* Oh, we need to check the indexing again */
    v = 1 - s->ia[0];
    if (v)  for (k=0; k<=s->n; k++)  s->ia[k] += v;
    if (v)  for (k=0; k<s->nz; k++)  s->ja[k] += v;
    /* Reinitialize ilu0 */
    s->A = A;
    
    if (!ilu2)
		ILU0(&s->n, s->A, s->ja, s->ia, s->rW, s->iW + s->ipjLU, s->iW, s->iW + s->ipiw, &ierr);
    else 
    {
		int verb=0, MaxWr=2*s->nz*20, MaxWi=2*s->nz*25, UsedWr, UsedWi;
		double tau1=1e-3, tau2=1e-6, partlur=0.3, partlurout;
		ILUOO(&s->n, s->ia, s->ja, s->A, &tau1, &tau2, &verb, s->rW, s->iW,
			   &MaxWr, &MaxWi, &partlur, &partlurout, &UsedWr, &UsedWi, &ierr);
		s->ipBCG = UsedWr;
    }
    if (ierr)  printf("initialization of ilu failed, zero pivot=%d\n", ierr);
    return ierr;
}

/* Solve linear system */
int solvebcg(bcg *s, vector *b, vector *x) 
{
    int NUNIT = 6, INFO = 0, eight = 8;
	
    s->ITER = 1000;
    matrix temp;
    temp.n = s->n;
    temp.ia = s->ia;
    temp.ja = s->ja;
    temp.A = s->A;
    s->RESID = s->eps * residual_fortran_indices(&temp, x, b);
    if (!ilu2)
	SLPBCGS(PREVEC0, &s->n, s->iW, s->rW,
		MATVEC, &s->n, s->ia, s->ja, s->A,
		s->rW + s->ipBCG, &s->n, &eight,
		&s->n, b->v, x->v,
		&s->ITER, &s->RESID,
		&INFO, &NUNIT);
    else
	SLPBCGS(PREVEC2, &s->n, s->iW, s->rW,
		MATVEC, &s->n, s->ia, s->ja, s->A,
		s->rW + s->ipBCG, &s->n, &eight,
		&s->n, b->v, x->v,
		&s->ITER, &s->RESID,
		&INFO, &NUNIT);
    return INFO;
}

/* Free memory used by solver */
void freebcg(bcg *s) 
{
    free(s->rW),  free(s->iW);
}




void MatrixCopyDataAni(void ** ppA, void * pB)
{
	matrix * B = (matrix *)pB;
	if( ppA == NULL || pB == NULL ) throw INMOST::DataCorruptedInSolver;
	*ppA = malloc(sizeof(matrix));
	matrix * A = (matrix *)ppA;
	A->n = B->n;
	if( B->n != 0 )
	{
		int nnz = B->ia[B->n] - B->ia[0];
		A->ia = (int *) malloc(sizeof(int)*(A->n+1));
		memcpy(A->ia,B->ia,sizeof(int)*(A->n+1));
		A->ja = (int *) malloc(sizeof(int)*nnz);
		memcpy(A->ja,B->ja,sizeof(int)*nnz);
		A->A = (double *) malloc(sizeof(double)*nnz);
		memcpy(A->A,B->A,sizeof(double)*nnz);
	}
}

void MatrixAssignDataAni(void * pA, void * pB)
{
	matrix * A = (matrix *)pA;
	matrix * B = (matrix *)pB;
	if( A == NULL || B == NULL ) throw INMOST::DataCorruptedInSolver;
	if( A != B )
	{
		if( A->n != 0 )
		{
			free(A->ia);
			free(A->ja);
			free(A->A);
		}
		if( B->n != 0 )
		{
			int nnz = B->ia[B->n] - B->ia[0];
			A->n = B->n;
			A->ia = (int *) malloc(sizeof(int)*(A->n+1));
			memcpy(A->ia,B->ia,sizeof(int)*(A->n+1));
			A->ja = (int *) malloc(sizeof(int)*nnz);
			memcpy(A->ja,B->ja,sizeof(int)*nnz);
			A->A = (double *) malloc(sizeof(double)*nnz);
			memcpy(A->A,B->A,sizeof(double)*nnz);	
		}
	}
}

void MatrixInitDataAni(void ** ppA, INMOST_MPI_Comm comm, const char * name)
{
	if( ppA == NULL ) throw INMOST::DataCorruptedInSolver;
	if( *ppA == NULL )
	{
		*ppA = malloc(sizeof(matrix));
		matrix * A = (matrix *)*ppA;
		A->n = 0;
	}
}

void MatrixDestroyDataAni(void ** pA)
{
	matrix * A = (matrix *)(*pA);
	if( A != NULL )
	{
		if( A->n != 0 )
		{
			free(A->ia);
			free(A->ja);
			free(A->A);
		}
		free(*pA);
		*pA = NULL;
	}
}



void MatrixFillAni(void * pA, int size, int * ia, int * ja, double * values)
{
	if( pA == NULL ) throw INMOST::DataCorruptedInSolver;
	matrix * A = (matrix *) pA;
	A->n = size;
	A->ia = ia;
	A->ja = ja;
	A->A = values;
}

void MatrixFillValuesAni(void * pA, double * values)
{
	if( pA == NULL ) throw INMOST::DataCorruptedInSolver;
	matrix * A = (matrix *) pA;
	free(A->A);
	A->A = values;
}

void MatrixFinalizeAni(void * data)
{
	//don't need to do anything
}

void VectorInitDataAni(void ** ppA, INMOST_MPI_Comm comm, const char * name)
{
	if( ppA == NULL ) throw INMOST::DataCorruptedInSolver;
	*ppA = malloc(sizeof(vector));
	vector * A = (vector *)*ppA;
	A->n = 0;
}

void VectorCopyDataAni(void ** ppA, void * pB)
{
	if( ppA == NULL || pB == NULL ) throw INMOST::DataCorruptedInSolver;
	*ppA = malloc(sizeof(vector));
	vector * A = (vector *)*ppA;
	vector * B = (vector *)pB;
	A->n = B->n;
	if( B->n != 0 )
	{
		A->v = (double *)malloc(sizeof(double)*A->n);
		memcpy(A->v,B->v,sizeof(double)*A->n);
	}
}

void VectorAssignDataAni(void * pA, void * pB)
{
	vector * A = (vector *)pA;
	vector * B = (vector *)pB;
	if( A == NULL || B == NULL ) throw INMOST::DataCorruptedInSolver;
	if( A != B )
	{
		if( A->n != 0 ) free(A->v);
		A->n = B->n;
		if( B->n != 0 )
		{
			A->v = (double *) malloc(sizeof(double)*A->n);
			memcpy(A->v,B->v,sizeof(double)*A->n);
		}
	}
}

void VectorPreallocateAni(void * pA, int size)
{
	vector * A = (vector *)pA;
	if( A == NULL ) throw INMOST::DataCorruptedInSolver;
	A->n = size;
	A->v = (double *)malloc(sizeof(double)*size);
}

void VectorFillAni(void * pA, double * values)
{
	vector * A = (vector *)pA;
	if( A == NULL ) throw INMOST::DataCorruptedInSolver;
	memcpy(A->v,values,sizeof(double)*A->n);
}
void VectorLoadAni(void * pA, double * values)
{
	vector * A = (vector *)pA;
	if( A == NULL ) throw INMOST::DataCorruptedInSolver;
	memcpy(values,A->v,sizeof(double)*A->n);
}

void VectorFinalizeAni(void * data)
{
}

void VectorDestroyDataAni(void ** ppA)
{
	if( ppA == NULL) throw INMOST::DataCorruptedInSolver;
	if( *ppA != NULL )
	{
		vector * A = (vector *)*ppA;
		free(A->v);
		free(*ppA);
		*ppA = NULL;
	}
}

void SolverInitializeAni(int * argc,char *** argv, const char * file_options)
{
}

bool SolverIsFinalizedAni()
{
	return true; //no need to finalize
}

void SolverFinalizeAni()
{
}


void SolverDestroyDataAni(void ** data)
{
	if( data != NULL )
	{
		if( *data != NULL )
		{
			bcg * m = (bcg *)*data;
			freebcg(m);
			free(m);
		}
		*data = NULL;
	}
}


void SolverInitDataAni(void ** data, INMOST_MPI_Comm comm, const char * name)
{
	*data = malloc(sizeof(bcg));
	((bcg *)*data)->n = 0;
}

void SolverCopyDataAni(void ** data, void * other_data, INMOST_MPI_Comm comm)
{
	throw INMOST::NotImplemented; //later
}
void SolverAssignDataAni(void * data, void * other_data)
{
	throw INMOST::NotImplemented; //later
}


void SolverSetMatrixAni(void * data, void * matrix_data, bool same_pattern, bool reuse_preconditioner)
{
	bcg * m = (bcg *)data;
	matrix * A = (matrix *)matrix_data;
	if( A == NULL || m == NULL ) throw INMOST::DataCorruptedInSolver;
	if( m->n == 0 )
		initbcg(m,A,set_epsilon);
	else
		newmatrixbcg(m,A,reuse_preconditioner);
}

bool SolverSolveAni(void * data, void * rhs_data, void * sol_data)
{
	bcg * m = (bcg *)data;
	vector * rhs = (vector*)rhs_data, * sol = (vector *)sol_data;
	return solvebcg(m,rhs,sol) == 0;
}
int SolverIterationNumberAni(void * data)
{
	return ((bcg *)data)->ITER;
}
double SolverResidualNormAni(void * data)
{
	return ((bcg *)data)->RESID;
}


#endif //USE_SOLVER_ANI