Création d'un compte pour un collaborateur extérieur au laboratoire depuis l'intranet ICube : https://intranet.icube.unistra.fr/fr/labs/member/profile

nl_superlu.c 6.89 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
/*
 *  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_superlu.h>
#include <NL/nl_context.h>

/************************************************************************/
/* SuperLU wrapper */

#ifdef NL_USE_SUPERLU

/* SuperLU includes */
#include <slu_cdefs.h>
#include <supermatrix.h>

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

typedef struct {
	superlu_options_t options ;
	SuperLUStat_t stat ;
	SuperMatrix L ;
	SuperMatrix U ;
	NLint* perm_c ;
	NLint* perm_r;
} superlu_context ;



NLboolean nlFactorize_SUPERLU() {

	/* OpenNL Context */
	NLSparseMatrix* M   = &(nlCurrentContext->M) ;
	NLuint          n   = nlCurrentContext->n ;
	NLuint          nnz = nlSparseMatrixNNZ(M) ; /* Number of Non-Zero coeffs */

	superlu_context* context = (superlu_context*)(nlCurrentContext->direct_solver_context) ;
	if(context == NULL) {
		nlCurrentContext->direct_solver_context = malloc(sizeof(superlu_context)) ;
		context = (superlu_context*)(nlCurrentContext->direct_solver_context) ;
	}

	/* SUPERLU variables */
	NLint info ;
	SuperMatrix A, AC ;

	/* Temporary variables */
	NLRowColumn* Ci = NULL ;
	NLuint       i,j,count ;

	/* Sanity checks */
	nl_assert(!(M->storage & NL_MATRIX_STORE_SYMMETRIC)) ;
	nl_assert(M->storage & NL_MATRIX_STORE_ROWS) ;
	nl_assert(M->m == M->n) ;

	set_default_options(&(context->options)) ;

	switch(nlCurrentContext->solver) {
	case NL_SUPERLU_EXT: {
		context->options.ColPerm = NATURAL ;
	} break ;
	case NL_PERM_SUPERLU_EXT: {
		context->options.ColPerm = COLAMD ;
	} break ;
	case NL_SYMMETRIC_SUPERLU_EXT: {
		context->options.ColPerm = MMD_AT_PLUS_A ;
		context->options.SymmetricMode = YES ;
	} break ;
	default: {
		nl_assert_not_reached ;
	} break ;
	}

	StatInit(&(context->stat)) ;

	/*
	 * Step 1: convert matrix M into SUPERLU compressed column representation
	 * ----------------------------------------------------------------------
	 */

	NLint*    xa   = NL_NEW_ARRAY(NLint, n+1) ;
	NLdouble* a    = NL_NEW_ARRAY(NLdouble, nnz) ;
	NLint*    asub = NL_NEW_ARRAY(NLint, nnz) ;

	count = 0 ;
	for(i = 0; i < n; i++) {
		Ci = &(M->row[i]) ;
		xa[i] = count ;
		for(j = 0; j < Ci->size; j++) {
			a[count]    = Ci->coeff[j].value ;
			asub[count] = Ci->coeff[j].index ;
			count++ ;
		}
	}
	xa[n] = nnz ;

	dCreate_CompCol_Matrix(
		&A, n, n, nnz, a, asub, xa,
		SLU_NR, /* Row wise     */
		SLU_D,  /* doubles         */
		SLU_GE  /* general storage */
	);

	/*
	 * Step 2: factorize matrix
	 * ------------------------
	 */

	context->perm_c = NL_NEW_ARRAY(NLint, n) ;
	context->perm_r = NL_NEW_ARRAY(NLint, n) ;
	NLint* etree    = NL_NEW_ARRAY(NLint, n) ;

	get_perm_c(context->options.ColPerm, &A, context->perm_c) ;
	sp_preorder(&(context->options), &A, context->perm_c, etree, &AC) ;

	int panel_size = sp_ienv(1) ;
	int relax = sp_ienv(2) ;

	dgstrf(&(context->options),
		   &AC,
		   relax,
		   panel_size,
		   etree,
		   NULL,
		   0,
		   context->perm_c,
		   context->perm_r,
		   &(context->L),
		   &(context->U),
		   &(context->stat),
		   &info) ;

	/*
	 * Step 3: cleanup
	 * ---------------
	 */

	NL_DELETE_ARRAY(xa) ;
	NL_DELETE_ARRAY(a) ;
	NL_DELETE_ARRAY(asub) ;
	NL_DELETE_ARRAY(etree) ;
	Destroy_SuperMatrix_Store(&A);
	Destroy_CompCol_Permuted(&AC);
	StatFree(&(context->stat));

	return NL_TRUE ;
}

188
189
NLboolean nlSolve_SUPERLU() {

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
	/* OpenNL Context */
	NLdouble* b = nlCurrentContext->b ;
	NLdouble* x = nlCurrentContext->x ;
	NLuint    n = nlCurrentContext->n ;

	superlu_context* context = (superlu_context*)(nlCurrentContext->direct_solver_context) ;
	nl_assert(context != NULL) ;

	/* SUPERLU variables */
	SuperMatrix B ;
	DNformat *vals = NULL ; /* access to result */
	double *rvals  = NULL ; /* access to result */

	/* Temporary variables */
	NLuint i ;
	NLint info ;

	StatInit(&(context->stat)) ;

	/*
	 * Step 1: convert right-hand side into SUPERLU representation
	 * -----------------------------------------------------------
	 */

	dCreate_Dense_Matrix(
		&B, n, 1, b, n,
		SLU_DN, /* Fortran-type column-wise storage */
		SLU_D,  /* doubles                          */
		SLU_GE  /* general storage                  */
	);

	/*
	 * Step 2: solve
	 * -------------
	 */

	dgstrs(NOTRANS,
		   &(context->L),
		   &(context->U),
		   context->perm_c,
		   context->perm_r,
		   &B,
		   &(context->stat),
		   &info) ;

	/*
	 * Step 3: get the solution
	 * ------------------------
	 */

	vals = (DNformat*)B.Store;
	rvals = (double*)(vals->nzval);
	for(i = 0; i <  n; i++)
		x[i] = rvals[i];

	/*
	 * Step 4: cleanup
	 * ---------------
	 */

	Destroy_SuperMatrix_Store(&B);
	StatFree(&(context->stat));

	return NL_TRUE ;
}

void nlClear_SUPERLU() {

	superlu_context* context = (superlu_context*)(nlCurrentContext->direct_solver_context) ;
	if(context != NULL) {
		Destroy_SuperNode_Matrix(&(context->L)) ;
		Destroy_CompCol_Matrix(&(context->U)) ;
		NL_DELETE_ARRAY(context->perm_c) ;
		NL_DELETE_ARRAY(context->perm_r) ;
	}
265
266
267
268
}

#else

269
270
NLboolean nlFactorize_SUPERLU() {
	nl_assert_not_reached ;
Pierre Kraemer's avatar
Pierre Kraemer committed
271
	return NL_FALSE;
272
273
}

274
275
NLboolean nlSolve_SUPERLU() {
    nl_assert_not_reached ;
Pierre Kraemer's avatar
Pierre Kraemer committed
276
    return NL_FALSE;
277
278
}

Sylvain Thery's avatar
Sylvain Thery committed
279
void nlClear_SUPERLU() {
280
281
282
	nl_assert_not_reached ;
}

283
284
#endif