nl_context.c 9.99 KB
Newer Older
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
/*
 *  Copyright (c) 2004-2010, Bruno Levy
 *  All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  * Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright notice,
 *  this list of conditions and the following disclaimer in the documentation
 *  and/or other materials provided with the distribution.
 *  * Neither the name of the ALICE Project-Team nor the names of its
 *  contributors may be used to endorse or promote products derived from this
 *  software without specific prior written permission.
 * 
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 *  POSSIBILITY OF SUCH DAMAGE.
 *
 *  If you modify this software, you should include a notice giving the
 *  name of the person performing the modification, the date of modification,
 *  and the reason for such modification.
 *
 *  Contact: Bruno Levy
 *
 *     levy@loria.fr
 *
 *     ALICE Project
 *     LORIA, INRIA Lorraine, 
 *     Campus Scientifique, BP 239
 *     54506 VANDOEUVRE LES NANCY CEDEX 
 *     FRANCE
 *
 */

#include <NL/nl_context.h>
#include <NL/nl_iterative_solvers.h>
#include <NL/nl_direct_solvers.h>
#include <NL/nl_preconditioners.h>
#include <NL/nl_superlu.h>
#include <NL/nl_cholmod.h>
#include <NL/nl_cnc_gpu_cuda.h>

NLContextStruct* nlCurrentContext = NULL ;

void nlMatrixVectorProd_default(NLdouble* x, NLdouble* y) {
    nlSparseMatrixMult(&(nlCurrentContext->M), x, y) ;
}

NLContext nlNewContext() {
	NLContextStruct* result       = NL_NEW(NLContextStruct) ;
	result->state                 = NL_STATE_INITIAL ;
	result->solver                = NL_BICGSTAB ;
	result->max_iterations        = 100 ;
	result->threshold             = 1e-6 ;
	result->omega                 = 1.5 ;
	result->row_scaling           = 1.0 ;
	result->right_hand_side       = 0.0 ;
	result->inner_iterations      = 5 ;
69 70 71 72 73 74 75 76
	result->normalize_rows        = NL_FALSE ;
	result->alloc_M               = NL_FALSE ;
	result->alloc_af              = NL_FALSE ;
	result->alloc_al              = NL_FALSE ;
	result->alloc_xl              = NL_FALSE ;
	result->alloc_variable        = NL_FALSE ;
	result->alloc_x               = NL_FALSE ;
	result->alloc_b               = NL_FALSE ;
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
	result->matrix_already_set    = NL_FALSE ;
	result->matrix_vector_prod    = nlMatrixVectorProd_default ;
	result->solver_func           = nlDefaultSolver ;
	result->factorize_func        = nlDefaultFactorize ;
	result->clear_factor_func     = nlDefaultClearFactor ;
	result->direct_solver_context = NULL ;
    nlMakeCurrent(result) ;
    return result ;
}

void nlDeleteContext(NLContext context_in) {
	NLContextStruct* context = (NLContextStruct*)(context_in) ;

    if(context->alloc_M) {
        nlSparseMatrixDestroy(&context->M) ;
    }
    if(context->alloc_af) {
        nlRowColumnDestroy(&context->af) ;
    }
    if(context->alloc_al) {
        nlRowColumnDestroy(&context->al) ;
    }
    if(context->alloc_xl) {
        nlRowColumnDestroy(&context->xl) ;
    }
    if(context->alloc_variable) {
        NL_DELETE_ARRAY(context->variable) ;
    }
    if(context->alloc_x) {
        NL_DELETE_ARRAY(context->x) ;
    }
    if(context->alloc_b) {
        NL_DELETE_ARRAY(context->b) ;
    }
111 112 113 114
	if( context->solver == NL_CHOLMOD_EXT || // or any other direct solver
		context->solver == NL_SUPERLU_EXT ||
		context->solver == NL_PERM_SUPERLU_EXT ||
		context->solver == NL_SYMMETRIC_SUPERLU_EXT) {
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
		context->clear_factor_func() ;
	}

	if(nlCurrentContext == context) {
		nlCurrentContext = NULL ;
	}

#ifdef NL_PARANOID
    NL_CLEAR(NLContextStruct, context) ;
#endif
    NL_DELETE(context) ;
}

void nlMakeCurrent(NLContext context) {
    nlCurrentContext = (NLContextStruct*)(context) ;
}

NLContext nlGetCurrent() {
    return nlCurrentContext ;
}

NLint nlGetCurrentState() {
	return nlCurrentContext->state ;
}

/************************************************************************/
/* Finite state automaton   */

void nlCheckState(NLenum state) {
    nl_assert(nlCurrentContext->state == state) ;
}

void nlTransition(NLenum from_state, NLenum to_state) {
    nlCheckState(from_state) ;
    nlCurrentContext->state = to_state ;
}

/************************************************************************/
/* Default solver */

static void nlSetupPreconditioner() {
    switch(nlCurrentContext->preconditioner) {
    case NL_PRECOND_NONE:
        nlCurrentContext->precond_vector_prod = NULL ;
        break ;
    case NL_PRECOND_JACOBI:
        nlCurrentContext->precond_vector_prod = nlPreconditioner_Jacobi ;
        break ;
    case NL_PRECOND_SSOR:
        nlCurrentContext->precond_vector_prod = nlPreconditioner_SSOR ;
        break ;
    default:
        nl_assert_not_reached ;
        break ;
    }
    /* Check compatibility between solver and preconditioner */
    if(
        nlCurrentContext->solver == NL_BICGSTAB && 
        nlCurrentContext->preconditioner == NL_PRECOND_SSOR
    ) {
        nlWarning(
            "nlSolve", 
            "cannot use SSOR preconditioner with non-symmetric matrix, switching to Jacobi"
        ) ;
        nlCurrentContext->preconditioner = NL_PRECOND_JACOBI ;        
        nlCurrentContext->precond_vector_prod = nlPreconditioner_Jacobi ;
    }
    if(
        nlCurrentContext->solver == NL_GMRES && 
        nlCurrentContext->preconditioner != NL_PRECOND_NONE
    ) {
        nlWarning("nlSolve", "Preconditioner not implemented yet for GMRES") ;
        nlCurrentContext->preconditioner = NL_PRECOND_NONE ;        
        nlCurrentContext->precond_vector_prod = NULL ;
    }
    if(
        nlCurrentContext->solver == NL_SUPERLU_EXT && 
        nlCurrentContext->preconditioner != NL_PRECOND_NONE
    ) {
        nlWarning("nlSolve", "Preconditioner not implemented yet for SUPERLU") ;
        nlCurrentContext->preconditioner = NL_PRECOND_NONE ;        
        nlCurrentContext->precond_vector_prod = NULL ;
    }
    if(
        nlCurrentContext->solver == NL_PERM_SUPERLU_EXT && 
        nlCurrentContext->preconditioner != NL_PRECOND_NONE
    ) {
        nlWarning("nlSolve", "Preconditioner not implemented yet for PERMSUPERLU") ;
        nlCurrentContext->preconditioner = NL_PRECOND_NONE ;        
        nlCurrentContext->precond_vector_prod = NULL ;
    }
    if(
        nlCurrentContext->solver == NL_SYMMETRIC_SUPERLU_EXT && 
        nlCurrentContext->preconditioner != NL_PRECOND_NONE
    ) {
        nlWarning("nlSolve", "Preconditioner not implemented yet for PERMSUPERLU") ;
        nlCurrentContext->preconditioner = NL_PRECOND_NONE ;        
        nlCurrentContext->precond_vector_prod = NULL ;
    }
}

NLboolean nlDefaultSolver() {
    NLboolean result = NL_TRUE ;
    nlSetupPreconditioner() ;
    switch(nlCurrentContext->solver) {
    case NL_CG: {
        if(nlCurrentContext->preconditioner == NL_PRECOND_NONE) {
            nlCurrentContext->used_iterations = nlSolve_CG() ;
        } else {
            nlCurrentContext->used_iterations = nlSolve_CG_precond() ;
        }
    } break ;
    case NL_BICGSTAB: {
        if(nlCurrentContext->preconditioner == NL_PRECOND_NONE) {
            nlCurrentContext->used_iterations = nlSolve_BICGSTAB() ;
        } else {
            nlCurrentContext->used_iterations = nlSolve_BICGSTAB_precond() ;
        }
    } break ;
    case NL_GMRES: {
        nlCurrentContext->used_iterations = nlSolve_GMRES() ;
    } break ;
    case NL_CNC_FLOAT_CRS:
    case NL_CNC_DOUBLE_CRS:
    case NL_CNC_FLOAT_BCRS2:
    case NL_CNC_DOUBLE_BCRS2:
    case NL_CNC_FLOAT_ELL:
    case NL_CNC_DOUBLE_ELL:
    case NL_CNC_FLOAT_HYB:
    case NL_CNC_DOUBLE_HYB: {
        nlCurrentContext->used_iterations = nlSolve_CNC() ;
    } break ;
    case NL_SUPERLU_EXT: 
    case NL_PERM_SUPERLU_EXT: 
    case NL_SYMMETRIC_SUPERLU_EXT: {
        result = nlSolve_SUPERLU() ;
    } break ;
	case NL_CHOLMOD_EXT: {
		result = nlSolve_CHOLMOD() ;
	} break ;
    default:
        nl_assert_not_reached ;
    }
    return result ;
}

NLboolean nlDefaultFactorize() {
	NLboolean result = NL_TRUE ;
	switch(nlCurrentContext->solver) {
	case NL_CG:
	case NL_BICGSTAB:
	case NL_GMRES:
	case NL_CNC_FLOAT_CRS:
	case NL_CNC_DOUBLE_CRS:
	case NL_CNC_FLOAT_BCRS2:
	case NL_CNC_DOUBLE_BCRS2:
	case NL_CNC_FLOAT_ELL:
	case NL_CNC_DOUBLE_ELL:
	case NL_CNC_FLOAT_HYB:
274
	case NL_CNC_DOUBLE_HYB: break ;
275 276
	case NL_SUPERLU_EXT:
	case NL_PERM_SUPERLU_EXT:
277 278 279
	case NL_SYMMETRIC_SUPERLU_EXT: {
			result = nlFactorize_SUPERLU() ;
	} break ;
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
	case NL_CHOLMOD_EXT: {
		result = nlFactorize_CHOLMOD() ;
	} break ;
	default:
		nl_assert_not_reached ;
	}
	return result ;
}

void nlDefaultClearFactor() {
	switch(nlCurrentContext->solver) {
	case NL_CG:
	case NL_BICGSTAB:
	case NL_GMRES:
	case NL_CNC_FLOAT_CRS:
	case NL_CNC_DOUBLE_CRS:
	case NL_CNC_FLOAT_BCRS2:
	case NL_CNC_DOUBLE_BCRS2:
	case NL_CNC_FLOAT_ELL:
	case NL_CNC_DOUBLE_ELL:
	case NL_CNC_FLOAT_HYB:
301
	case NL_CNC_DOUBLE_HYB: break ;
302 303
	case NL_SUPERLU_EXT:
	case NL_PERM_SUPERLU_EXT:
304 305 306
	case NL_SYMMETRIC_SUPERLU_EXT: {
		nlClear_SUPERLU() ;
	} break ;
307 308 309 310 311 312 313 314 315 316 317 318 319
	case NL_CHOLMOD_EXT: {
		nlClear_CHOLMOD() ;
	} break ;
	default:
		nl_assert_not_reached ;
	}

	/* free direct solver context */
	if(nlCurrentContext->direct_solver_context != NULL) {
		free(nlCurrentContext->direct_solver_context) ;
		nlCurrentContext->direct_solver_context = NULL ;
	}
}