/[escript]/trunk/paso/src/MKL.c
ViewVC logotype

Contents of /trunk/paso/src/MKL.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3259 - (show annotations)
Mon Oct 11 01:48:14 2010 UTC (8 years, 11 months ago) by jfenwick
File MIME type: text/plain
File size: 11857 byte(s)
Merging dudley and scons updates from branches

1
2 /*******************************************************
3 *
4 * Copyright (c) 2003-2010 by University of Queensland
5 * Earth Systems Science Computational Center (ESSCC)
6 * http://www.uq.edu.au/esscc
7 *
8 * Primary Business: Queensland, Australia
9 * Licensed under the Open Software License version 3.0
10 * http://www.opensource.org/licenses/osl-3.0.php
11 *
12 *******************************************************/
13
14
15 /**************************************************************/
16
17 /* Paso: interface to the Intel MKL library */
18
19 /**************************************************************/
20
21 /* Copyrights by ACcESS Australia 2006 */
22 /* Author: l.gross@uq.edu.au */
23
24 /**************************************************************/
25
26 #include "Paso.h"
27 #include "MKL.h"
28 #ifdef _OPENMP
29 #include <omp.h>
30 #endif
31
32
33 /**************************************************************/
34
35 /* free any extra stuff possibly used by the MKL library */
36
37 void Paso_MKL_free(Paso_SystemMatrix* A) {
38 #ifdef MKL
39 index_t i;
40 if (A->solver!=NULL) {
41 _INTEGER_t mtype = MKL_MTYPE_UNSYM;
42 if (A->type & MATRIX_FORMAT_SYM) mtype=MKL_MTYPE_SYM;
43 _INTEGER_t n = A->mainBlock->numRows;
44 _INTEGER_t maxfct=1; /* number of factorizations on the same pattern */
45 _INTEGER_t mnum =1; /* factoriztion to be handeled in this call */
46 _INTEGER_t msglvl=0; /* message level */
47 _INTEGER_t nrhs=1; /* number of right hand sides */
48 _INTEGER_t idum; /* dummy integer */
49 _DOUBLE_PRECISION_t ddum; /* dummy float */
50 _INTEGER_t error=MKL_ERROR_NO; /* error code */
51 _INTEGER_t iparm[64]; /* parameters */
52 for (i=0;i<64;++i) iparm[i]=0;
53
54 _INTEGER_t phase = MKL_PHASE_RELEASE_MEMORY;
55 PARDISO ((_MKL_DSS_HANDLE_t *)(A->solver), &maxfct, &mnum, &mtype, &phase,
56 &n, A->mainBlock->val, A->mainBlock->pattern->ptr, A->mainBlock->pattern->index, &idum, &nrhs,
57 iparm, &msglvl,&ddum, &ddum, &error);
58 MEMFREE(A->solver);
59 if (error != MKL_ERROR_NO) Esys_setError(TYPE_ERROR,"memory release in paradiso library failed.");
60 }
61 #endif
62 }
63 /* call the solver: */
64
65 void Paso_MKL(Paso_SystemMatrix* A,
66 double* out,
67 double* in,
68 Paso_Options* options,
69 Paso_Performance* pp) {
70 #ifdef MKL
71 double time0, time1;
72 index_t i;
73
74 if (! (A->type & (MATRIX_FORMAT_OFFSET1 + MATRIX_FORMAT_BLK1)) ) {
75 Esys_setError(TYPE_ERROR,"Paso_MKL: MKL requires CSR format with index offset 1 and block size 1.");
76 return;
77 }
78 options->converged=FALSE;
79 Performance_startMonitor(pp,PERFORMANCE_ALL);
80 _INTEGER_t mtype = MKL_MTYPE_UNSYM;
81 if (A->type & MATRIX_FORMAT_SYM) mtype=MKL_MTYPE_SYM;
82 _INTEGER_t n = A->mainBlock->numRows;
83 _INTEGER_t maxfct=1; /* number of factorizations on the same pattern */
84 _INTEGER_t mnum =1; /* factoriztion to be handeled in this call */
85 _INTEGER_t msglvl=0; /* message level */
86 _INTEGER_t nrhs=1; /* number of right hand sides */
87 _INTEGER_t idum; /* dummy integer */
88 _DOUBLE_PRECISION_t ddum; /* dummy float */
89 _INTEGER_t phase = MKL_PHASE_SYMBOLIC_FACTORIZATION;
90
91 _INTEGER_t error=MKL_ERROR_NO; /* error code */
92 _INTEGER_t iparm[64]; /* parameters */
93 _MKL_DSS_HANDLE_t* pt = (_MKL_DSS_HANDLE_t *)(A->solver);
94 /* set iparm */
95 for (i=0;i<64;++i) iparm[i]=0;
96 iparm[0] = 1; /* no default settings*/
97 switch (options->reordering) {
98 case PASO_MINIMUM_FILL_IN:
99 iparm[1]=MKL_REORDERING_MINIMUM_DEGREE;
100 break;
101 default:
102 iparm[1]=MKL_REORDERING_NESTED_DISSECTION;
103 break;
104 }
105 #ifdef _OPENMP
106 iparm[2] =omp_get_max_threads();
107 #else
108 iparm[2] = 1;
109 #endif
110 iparm[5] = 0; /* store solution into output array */
111 iparm[7] = 2; /* maximum number of refinements */
112 iparm[9] = 13; /* 10**(-iparm[9]) preturbation of pivot elements */
113 iparm[10] = 1; /* rescaling the matrix before factorization started */
114 iparm[17] =0; /* =-1 report number of non-zeroes */
115 iparm[18] =0; /* =-1 report flops */
116
117 if (pt==NULL) {
118 /* allocate address pointer */
119 pt=MEMALLOC(64,_MKL_DSS_HANDLE_t);
120 if (Esys_checkPtr(pt)) return;
121 A->solver=(void*) pt;
122 for (i=0;i<64;++i) pt[i]=NULL;
123 time0=Esys_timer();
124 /* symbolic factorization */
125 phase = MKL_PHASE_SYMBOLIC_FACTORIZATION;
126 PARDISO (pt, &maxfct, &mnum, &mtype, &phase,
127 &n, A->mainBlock->val, A->mainBlock->pattern->ptr, A->mainBlock->pattern->index, &idum, &nrhs,
128 iparm, &msglvl, in, out, &error);
129 if (error != MKL_ERROR_NO) {
130 if (options->verbose) printf("MKL: symbolic factorization factorization failed.\n");
131 Esys_setError(VALUE_ERROR,"symbolic factorization in paradiso library failed.");
132 Paso_MKL_free(A);
133 } else {
134 /* LDU factorization */
135 phase = MKL_PHASE_FACTORIZATION;
136 PARDISO(pt, &maxfct, &mnum, &mtype, &phase,
137 &n, A->mainBlock->val, A->mainBlock->pattern->ptr, A->mainBlock->pattern->index, &idum, &nrhs,
138 iparm, &msglvl, in, out, &error);
139 if (error != MKL_ERROR_NO) {
140 if (options->verbose) printf("MKL: LDU factorization failed.\n");
141 Esys_setError(ZERO_DIVISION_ERROR,"factorization in paradiso library failed. Most likely the matrix is singular.");
142 Paso_MKL_free(A);
143 }
144 if (options->verbose) printf("MKL: LDU factorization completed.\n");
145 }
146 options->set_up_time=Esys_timer()-time0;
147 } else {
148 options->set_up_time=0;
149 }
150 /* forward backward substitution\ */
151 if (Esys_noError()) {
152 time0=Esys_timer();
153 phase = MKL_PHASE_SOLVE;
154 PARDISO (pt, &maxfct, &mnum, &mtype, &phase,
155 &n, A->mainBlock->val, A->mainBlock->pattern->ptr, A->mainBlock->pattern->index, &idum, &nrhs,
156 iparm, &msglvl, in, out, &error);
157 if (options->verbose) printf("MKL: solve completed.\n");
158 if (error != MKL_ERROR_NO) {
159 if (options->verbose) printf("MKL: forward/backward substitution failed.\n");
160 Esys_setError(VALUE_ERROR,"forward/backward substitution in paradiso library failed. Most likely the matrix is singular.");
161 } else {
162 if (options->verbose) printf("MKL: forward/backward substitution completed.\n");
163 options->residual_norm=0.;
164 options->num_iter=0;
165 options->num_level=0;
166 options->num_inner_iter=0;
167 options->converged=TRUE;
168 }
169 options->time=Esys_timer()-time0 + options->set_up_time;
170 }
171 Performance_stopMonitor(pp,PERFORMANCE_ALL);
172 #else
173 Esys_setError(SYSTEM_ERROR,"Paso_MKL:MKL is not avialble.");
174 #endif
175 }
176
177 void Paso_MKL_free1(Paso_SparseMatrix* A) {
178 #ifdef MKL
179 index_t i;
180 if (A->solver!=NULL) {
181 _INTEGER_t mtype = MKL_MTYPE_UNSYM;
182 if (A->type & MATRIX_FORMAT_SYM) mtype=MKL_MTYPE_SYM;
183 _INTEGER_t n = A->numRows;
184 _INTEGER_t maxfct=1; /* number of factorizations on the same pattern */
185 _INTEGER_t mnum =1; /* factoriztion to be handeled in this call */
186 _INTEGER_t msglvl=0; /* message level */
187 _INTEGER_t nrhs=1; /* number of right hand sides */
188 _INTEGER_t idum; /* dummy integer */
189 _DOUBLE_PRECISION_t ddum; /* dummy float */
190 _INTEGER_t error=MKL_ERROR_NO; /* error code */
191 _INTEGER_t iparm[64]; /* parameters */
192 for (i=0;i<64;++i) iparm[i]=0;
193
194 _INTEGER_t phase = MKL_PHASE_RELEASE_MEMORY;
195 PARDISO ((_MKL_DSS_HANDLE_t *)(A->solver), &maxfct, &mnum, &mtype, &phase,
196 &n, A->val, A->pattern->ptr, A->pattern->index, &idum, &nrhs,
197 iparm, &msglvl,&ddum, &ddum, &error);
198 MEMFREE(A->solver);
199 if (error != MKL_ERROR_NO) Esys_setError(TYPE_ERROR,"memory release in paradiso library failed.");
200 }
201 #endif
202 }
203 /* call the solver: */
204
205 void Paso_MKL1(Paso_SparseMatrix* A,
206 double* out,
207 double* in,
208 bool_t verbose) {
209 #ifdef MKL
210 index_t i;
211
212 if (! (A->type & (MATRIX_FORMAT_OFFSET1 + MATRIX_FORMAT_BLK1)) ) {
213 Esys_setError(TYPE_ERROR,"Paso_MKL: MKL requires CSR format with index offset 1 and block size 1.");
214 return;
215 }
216 _INTEGER_t mtype = MKL_MTYPE_UNSYM;
217 if (A->type & MATRIX_FORMAT_SYM) mtype=MKL_MTYPE_SYM;
218 _INTEGER_t n = A->numRows;
219 _INTEGER_t maxfct=1; /* number of factorizations on the same pattern */
220 _INTEGER_t mnum =1; /* factoriztion to be handeled in this call */
221 _INTEGER_t msglvl=0; /* message level */
222 _INTEGER_t nrhs=1; /* number of right hand sides */
223 _INTEGER_t idum; /* dummy integer */
224 _DOUBLE_PRECISION_t ddum; /* dummy float */
225 _INTEGER_t phase = MKL_PHASE_SYMBOLIC_FACTORIZATION;
226
227 _INTEGER_t error=MKL_ERROR_NO; /* error code */
228 _INTEGER_t iparm[64]; /* parameters */
229 _MKL_DSS_HANDLE_t* pt = (_MKL_DSS_HANDLE_t *)(A->solver);
230 /* set iparm */
231 for (i=0;i<64;++i) iparm[i]=0;
232 iparm[0] = 1; /* no default settings*/
233 iparm[1]=MKL_REORDERING_MINIMUM_DEGREE;
234 #ifdef _OPENMP
235 iparm[2] = omp_get_max_threads();
236 #else
237 iparm[2] = 1;
238 #endif
239
240 iparm[5] = 0; /* store solution into output array */
241 iparm[7] = 2; /* maximum number of refinements */
242 iparm[9] = 13; /* 10**(-iparm[9]) preturbation of pivot elements */
243 iparm[10] = 1; /* rescaling the matrix before factorization started */
244 iparm[17] =0; /* =-1 report number of non-zeroes */
245 iparm[18] =0; /* =-1 report flops */
246
247
248 if (pt==NULL) {
249 /* allocate address pointer */
250 pt=MEMALLOC(64,_MKL_DSS_HANDLE_t);
251 if (Esys_checkPtr(pt)) return;
252 A->solver=(void*) pt;
253 for (i=0;i<64;++i) pt[i]=NULL;
254 /* symbolic factorization */
255 phase = MKL_PHASE_SYMBOLIC_FACTORIZATION;
256 PARDISO (pt, &maxfct, &mnum, &mtype, &phase,
257 &n, A->val, A->pattern->ptr, A->pattern->index, &idum, &nrhs,
258 iparm, &msglvl, in, out, &error);
259 if (error != MKL_ERROR_NO) {
260 Esys_setError(VALUE_ERROR,"symbolic factorization in paradiso library failed.");
261 Paso_MKL_free1(A);
262 } else {
263 /* LDU factorization */
264 phase = MKL_PHASE_FACTORIZATION;
265 PARDISO(pt, &maxfct, &mnum, &mtype, &phase,
266 &n, A->val, A->pattern->ptr, A->pattern->index, &idum, &nrhs,
267 iparm, &msglvl, in, out, &error);
268 if (error != MKL_ERROR_NO) {
269 Esys_setError(ZERO_DIVISION_ERROR,"factorization in paradiso library failed. Most likely the matrix is singular.");
270 Paso_MKL_free1(A);
271 }
272 if (verbose) printf("MKL: LDU factorization completed.\n");
273 }
274 }
275 /* forward backward substitution\ */
276 if (Esys_noError()) {
277 phase = MKL_PHASE_SOLVE;
278 PARDISO (pt, &maxfct, &mnum, &mtype, &phase,
279 &n, A->val, A->pattern->ptr, A->pattern->index, &idum, &nrhs,
280 iparm, &msglvl, in, out, &error);
281 if (verbose) printf("MKL: solve completed.\n");
282 if (error != MKL_ERROR_NO) {
283 Esys_setError(VALUE_ERROR,"forward/backward substition in paradiso library failed. Most likely the matrix is singular.");
284 }
285 }
286 #else
287 Esys_setError(SYSTEM_ERROR,"Paso_MKL:MKL is not avialble.");
288 #endif
289 }
290 /*
291 * $Log$
292 *
293 */

  ViewVC Help
Powered by ViewVC 1.1.26