Debian Codegraph @

3rdparty/dissection/src/C-test/MM-Dissection.cpp

raw
1 /*! \file MM-DissectionSolver.cpp
2 \brief test rouinte of dissection solver reading Matrix Market format
3 \author Atsushi Suzuki, Laboratoire Jacques-Louis Lions
4 \date Jun. 20th 2014
5 \date Jul. 12th 2015
6 \date Nov. 30th 2016
7 \dahe Apr. 24th 2018
8 */
9
10 // This file is part of Dissection
11 //
12 // Dissection is free software: you can redistribute it and/or modify
13 // it under the terms of the GNU General Public License as published by
14 // the Free Software Foundation, either version 3 of the License, or
15 // (at your option) any later version.
16 //
17 // Linking Dissection statically or dynamically with other modules is making
18 // a combined work based on Disssection. Thus, the terms and conditions of
19 // the GNU General Public License cover the whole combination.
20 //
21 // As a special exception, the copyright holders of Dissection give you
22 // permission to combine Dissection program with free software programs or
23 // libraries that are released under the GNU LGPL and with independent modules
24 // that communicate with Dissection solely through the Dissection-fortran
25 // interface. You may copy and distribute such a system following the terms of
26 // the GNU GPL for Dissection and the licenses of the other code concerned,
27 // provided that you include the source code of that other code when and as
28 // the GNU GPL requires distribution of source code and provided that you do
29 // not modify the Dissection-fortran interface.
30 //
31 // Note that people who make modified versions of Dissection are not obligated
32 // to grant this special exception for their modified versions; it is their
33 // choice whether to do so. The GNU General Public License gives permission to
34 // release a modified version without this exception; this exception also makes
35 // it possible to release a modified version which carries forward this
36 // exception. If you modify the Dissection-fortran interface, this exception
37 // does not apply to your modified version of Dissection, and you must remove
38 // this exception when you distribute your modified version.
39 //
40 // This exception is an additional permission under section 7 of the GNU
41 // General Public License, version 3 ("GPLv3")
42 //
43 // Dissection is distributed in the hope that it will be useful,
44 // but WITHOUT ANY WARRANTY; without even the implied warranty of
45 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
46 // GNU General Public License for more details.
47 //
48 // You should have received a copy of the GNU General Public License
49 // along with Dissection. If not, see <http://www.gnu.org/licenses/>.
50 //
51
52 #include "Compiler/OptionCompiler.hpp"
53 #include "Compiler/OptionLibrary.hpp"
54 #include "Driver/DissectionSolver.hpp"
55 #include <cstdio>
56 #include <cstdlib>
57 #include <cstring>
58 #include <fcntl.h>
59 #include <math.h>
60
61 #ifdef BLAS_MKL
62 #include <mkl_service.h>
63 #endif
64 #ifdef POSIX_THREADS
65 #include <pthread.h>
66 #endif
67 #include <iostream>
68 #include <sstream>
69 #include <fstream>
70 #include <complex>
71 using namespace std;
72
73 #ifdef POSIX_THREADS
74 static int _stat = (-1);
75
76 void *thread_child(void *arg)
77 {
78 char buf[256];
79 int *pid = (int *)arg;
80 unsigned int mem_tmp, mem_min, mem_max;
81 double avg_mem;
82 avg_mem = 0.0;
83 mem_min = (1U << 31) - 1;
84 mem_max = 0U;
85 int stat0, stat1;
86 stat0 = _stat;
87 unsigned int count = 0U;
88 // fprintf(stderr, "thread_child forked\n");
89 while(_stat != 0) {
90 stat1 = _stat;
91 if (stat1 == 1) {
92 sprintf(buf, "/proc/%d/statm", *pid);
93 ifstream fin(buf);
94 fin >> mem_tmp;
95 fin.close();
96 if (mem_tmp > mem_max) {
97 mem_max = mem_tmp;
98 }
99 if (mem_tmp < mem_min) {
100 mem_min = mem_tmp;
101 }
102 avg_mem += (double)mem_tmp;
103 count++;
104 }
105 if ((stat1 == (-1)) && (stat0 == 1)) {
106 fprintf(stderr,
107 "used memory :min: %14.8e max: %14.8e avg: %14.8e count: %d\n",
108 (double)mem_min * 4.0 / (1024.0 * 1024.0),
109 (double)mem_max * 4.0 / (1024.0 * 1024.0),
110 (avg_mem / (double)count) * 4.0 / (1024.0 * 1024.0),
111 count);
112 count = 0U;
113 avg_mem = 0.0;
114 mem_min = (1U << 31) - 1;
115 mem_max = 0U;
116 }
117 stat0 = stat1;
118 #ifdef POSIX_THREADS
119 usleep(1000);
120 #endif
121 }
122 // fprintf(stderr, "thread_child join\n count = %ld\n", count);
123 pthread_exit(arg);
124
125 return (void *)NULL;
126 }
127 #endif
128 template<typename T>
129 bool generate_CSR(std::list<int>* ind_cols_tmp, std::list<T>* val_tmp,
130 int nrow, int *nnz, int *mask, int *old2new,
131 int *irow, int *jcol, T* val, bool symmetrize)
132 {
133 bool flag_modified = false;
134 const T zero(0.0);
135 // ind_cols_tmp = new std::list<int>[nrow];
136 // val_tmp = new std::list<T>[nrow];
137 int nnz1 = *nnz;
138 for (int i = 0; i < *nnz; i++) {
139 const int i0 = irow[i];
140 const int j0 = jcol[i];
141 const int ii = old2new[i0];
142 const int jj = old2new[j0];
143 if ((mask[i0] != 1) || (mask[j0] != 1)) {
144 // fprintf(stderr, "%d %d\n", i0, j0);
145 nnz1--;
146 continue;
147 }
148 // fprintf(stderr, "%d %d -> %d %d \n", i0, j0, ii, jj);
149 if (ind_cols_tmp[ii].empty()) {
150 ind_cols_tmp[ii].push_back(jj);
151 val_tmp[ii].push_back(val[i]);
152 }
153 else {
154 if (ind_cols_tmp[ii].back() < jj) {
155 ind_cols_tmp[ii].push_back(jj);
156 val_tmp[ii].push_back(val[i]);
157 }
158 else {
159 typename std::list<T>::iterator iv = val_tmp[ii].begin();
160 std::list<int>::iterator it = ind_cols_tmp[ii].begin();
161 for ( ; it != ind_cols_tmp[ii].end(); ++it, ++iv) {
162 if (*it == jj) {
163 fprintf(stderr, "already exits? (%d %d)\n", ii, jj);
164 break;
165 }
166 if (*it > jj) {
167 ind_cols_tmp[ii].insert(it, jj);
168 val_tmp[ii].insert(iv, val[i]);
169 break;
170 }
171 }
172 }
173 }
174 }
175 // symmetrize
176 if (symmetrize) {
177 for (int i = 0; i < nrow; i++) {
178 for (std::list<int>::iterator jt = ind_cols_tmp[i].begin();
179 jt != ind_cols_tmp[i].end(); ++jt) {
180 const int jj = (*jt);
181 bool flag = false;
182 for (std::list<int>::iterator it = ind_cols_tmp[jj].begin();
183 it != ind_cols_tmp[jj].end(); ++it) {
184 if ((*it) == i) {
185 flag = true;
186 // fprintf(stderr, "%d %d symmetric position found\n", i, jj);
187 break;
188 }
189 }
190 if (!flag) {
191 flag_modified = true;
192 // fprintf(stderr, "%d %d need to be added\n", i, jj);
193 if (ind_cols_tmp[jj].back() < i) {
194 ind_cols_tmp[jj].push_back(i);
195 val_tmp[jj].push_back(zero);
196 // fprintf(stderr, "%d %d append\n", i, jj);
197 nnz1++;
198 }
199 else {
200 typename std::list<T>::iterator iv = val_tmp[jj].begin();
201 std::list<int>::iterator it = ind_cols_tmp[jj].begin();
202 for (; it != ind_cols_tmp[jj].end(); ++it, ++iv) {
203 if ((*it) > i) {
204 ind_cols_tmp[jj].insert(it, i);
205 val_tmp[jj].insert(iv, zero);
206 nnz1++;
207 // fprintf(stderr, "%d %d inserted\n", i, jj);
208 break;
209 }
210 }
211 }
212 } // if (!flag);
213 }
214 }
215 }
216 if (symmetrize) {
217 for (int i = 0; i < nrow; i++) {
218 std::list<int>::iterator jt = ind_cols_tmp[i].begin();
219 for ( ; jt != ind_cols_tmp[i].end(); ++jt) {
220 const int jj = (*jt);
221 bool flag = false;
222 for (std::list<int>::iterator it = ind_cols_tmp[jj].begin();
223 it != ind_cols_tmp[jj].end(); ++it) {
224 if ((*it) == i) {
225 flag = true;
226 break;
227 }
228 }
229 if (!flag) {
230 fprintf(stderr, "%d %d position is not symmetric\n", i, jj);
231 }
232 }
233 }
234 }
235 *nnz = nnz1;
236 return flag_modified;
237 }
238
239 template
240 bool generate_CSR<double>(std::list<int>* ind_cols_tmp, std::list<double>* val_tmp,
241 int nrow, int *nnz, int *mask, int *old2new,
242 int *irow, int *jcol, double* val, bool symmetrize);
243 template
244 bool generate_CSR<complex<double> >(std::list<int>* ind_cols_tmp,
245 std::list<complex<double> >* val_tmp,
246 int nrow, int *nnz, int *mask, int *old2new,
247 int *irow, int *jcol, complex<double>* val,
248 bool symmetrize);
249
250 template<typename T>
251 void copy_CSR(int *indcols, int *ptrows, T* coefs, int nrow,
252 bool upper_flag, bool isSym,
253 std::list<int>* ind_cols_tmp, std::list<T>* val_tmp)
254 {
255 const T zero(0.0);
256 ptrows[0] = 0;
257 for (int i = 0; i < nrow; i++) {
258 int k;
259 int itmp = ind_cols_tmp[i].size();
260 if (upper_flag) {
261 if (ind_cols_tmp[i].front() == i) {
262 ptrows[i + 1] = ptrows[i] + itmp;
263 k = ptrows[i];
264 }
265 else {
266 fprintf(stderr, "zero is added to diagonal : %d\n", i);
267 ptrows[i + 1] = ptrows[i] + itmp + 1;
268 indcols[ptrows[i]] = i;
269 coefs[ptrows[i]] = zero;
270 k = ptrows[i] + 1;
271 }
272 }
273 else {
274 k = ptrows[i];
275 if (ind_cols_tmp[i].back() == i || (!isSym)) {
276 ptrows[i + 1] = ptrows[i] + itmp;
277 }
278 else {
279 fprintf(stderr, "zero is added to diagonal : %d\n", i);
280 ptrows[i + 1] = ptrows[i] + itmp + 1;
281 indcols[ptrows[i + 1] - 1] = i;
282 coefs[ptrows[i + 1] - 1] = zero;
283 }
284 }
285 std::list<int>::iterator it = ind_cols_tmp[i].begin();
286 typename std::list<T>::iterator iv = val_tmp[i].begin();
287 for ( ; it != ind_cols_tmp[i].end(); ++it, ++iv, k++) {
288 indcols[k] = *it;
289 coefs[k] = *iv;
290 }
291 } // loop : i
292 }
293
294 template
295 void copy_CSR<double>(int *indcols, int *ptrows, double* coefs, int nrow,
296 bool upper_flag, bool isSym,
297 std::list<int>* ind_cols_tmp, std::list<double>* val_tmp);
298
299 template
300 void copy_CSR<complex<double> >(int *indcols, int *ptrows, complex<double>* coefs, int nrow,
301 bool upper_flag, bool isSym,
302 std::list<int>* ind_cols_tmp, std::list<complex<double> >* val_tmp);
303
304 #if 0
305 inline
306 double SpMAX(double a, double b)
307 {
308 return (a > b ? a : b);
309 }
310
311 template<typename T>
312 void normalize(const int type, const int n,
313 const int *ptRows, const int *indCols,
314 const T* coefs0, T* coefs, T* u)
315 {
316 const T zero(0.0);
317 const T one(1.0);
318 const T Wzero(0.0);
319 T *v, *d;
320 v = new T[n];
321 d = new T[n];
322
323 T alower, aupper, adiag;
324
325 for (int i = 0; i < n; i++) {
326 d[i] = zero;
327 u[i] = zero;
328 v[i] = zero;
329 }
330
331 for (int i = 0; i < n; i++) {
332 for (int k = ptRows[i]; k < ptRows[i + 1]; k++) {
333 int j = indCols[k];
334 if (j == i) {
335 u[i] = coefs0[k] < zero ? -coefs0[k] : coefs0[k];
336 d[i] = u[i];
337 v[i] = SpMAX(v[i],u[i]);
338 }
339 else {
340 T acoef = coefs0[k] < zero ? -coefs0[k] : coefs0[k];
341 v[i] = SpMAX(v[i], acoef);
342 v[j] = SpMAX(v[j], acoef);
343 }
344 }
345 }
346
347 switch(type) {
348 case DIAGONAL_SCALING:
349 for (int i = 0; i < n; i++) {
350 u[i] = ((u[i] != zero) ? sqrt(one / u[i]) :
351 ((v[i] != zero) ? sqrt(one / v[i]) : one));
352 }
353 break;
354 case KKT_SCALING:
355 for (int i = 0 ; i < n; i++) {
356 if (d[i] != zero) {
357 u[i] = sqrt(one / u[i]);
358 }
359 else {
360 T xtmp = Wzero;
361 for (int m = ptRows[i]; m < ptRows[i + 1]; m++) {
362 const int j = indCols[m];
363 alower = coefs0[m];
364 int flag = 0;
365 for (int n = ptRows[j]; n < ptRows[j + 1]; n++) {
366 const int k = indCols[n];
367 if (k == j) {
368 adiag = coefs0[n];
369 if (adiag != Wzero) {
370 flag++;
371 }
372 else {
373 continue;
374 }
375 }
376 if (k == i) {
377 aupper = coefs0[n];
378 flag++;
379 }
380 if (flag == 2) {
381 break;
382 }
383 } // loop : n
384 if (flag == 2) {
385 xtmp += alower * aupper / adiag;
386 }
387 } // loop : m
388 if (xtmp == zero) {
389 u[i] = one;
390 }
391 else {
392 u[i] = one / sqrt(xtmp < zero ? -xtmp : xtmp);
393 }
394 } // if (d[i] != zero)
395 } // loop : i
396 break;
397 default:
398 for (int i = 0; i < n; i++) {
399 u[i] = one;
400 }
401 break;
402 } // witch (type)
403 // Scaling
404 if (type == NO_SCALING) {
405 for (int i = 0; i < ptRows[n]; i++) {
406 coefs[i] = coefs0[i];
407 }
408 // blas_copy<T>(_ptRows[n], coefs0, 1, &_coefs[0], 1);
409 // const W* coefs0 to T* _coefs is not supported becuase of type conversion
410 }
411 else {
412 for ( int i = 0; i < n; i++) {
413 for (int k = ptRows[i]; k < ptRows[i+1]; k++) {
414 int j = indCols[k];
415 // _coefs[k] = tohigher((1), coefs0[k] * W(u[i]) * W(u[j]));
416 coefs[k] = coefs0[k] * u[i] * u[j];
417 }
418 }
419 }
420 delete [] d;
421 delete [] v;
422 }
423
424 template void
425 normalize<double>(const int type, const int n,
426 const int *ptRows, const int *indCols,
427 const double* coefs0, double* coefs, double* u);
428 #endif
429 int main(int argc, char **argv)
430 {
431 int n, itmp, jtmp;
432 char fname[256], fname1[256];
433 char buf[1024];
434 int nrow, nnz, flag, nnz1;
435 int *ptrows, *indcols;
436 int *irow, *jcol;
437 double *val, *coefs;
438 complex<double> *valc, *ccoefs;
439 int decomposer;
440 int num_threads;
441 int scaling = 1;
442 double eps_pivot;
443 int numlevels = -1;
444 int minNodes = 128;
445 std::list<int>* ind_cols_tmp;
446 std::list<double>* val_tmp;
447 std::list<complex<double> >* val_tmpc;
448 FILE *fp;
449 bool isSym, isComplex;
450 bool upper_flag = true;
451 bool isWhole = false;
452 bool kernel_detection_all = false;
453 int *indx_excl;
454 int nexcl = 0;
455 bool excl_flag = false;
456 bool flag_modified = false;
457 bool assume_invertible = false;
458 if (argc < 6) {
459 fprintf(stderr, "MM-dissection [data file] [decomposer] [num_threads] [eps_pivot] [num_levels] [scaling] [kerner_detection_all] [upper_flag] [minNodes]\n");
460 exit(-1);
461 }
462 strcpy(fname, argv[1]);
463 decomposer = atoi(argv[2]);
464 num_threads = atoi(argv[3]);
465 eps_pivot = atof(argv[4]);
466 numlevels = atof(argv[5]);
467 if (argc >= 7) {
468 scaling = atoi(argv[6]);
469 }
470 if (argc >= 8) {
471 assume_invertible = atoi(argv[7]) > 0 ? true : false;
472 }
473 if (argc >= 9) {
474 upper_flag = (atoi(argv[8]) == 1);
475 isWhole = (atoi(argv[8]) == (-1));
476 }
477 if (argc >= 10) {
478 strcpy(fname1, argv[9]);
479 excl_flag = true;
480 }
481 if (argc >= 11) {
482 minNodes = atoi(argv[10]);
483 }
484
485 // read from the file
486 if ((fp = fopen(fname, "r")) == NULL) {
487 fprintf(stderr, "fail to open %s\n", fname);
488 }
489 fgets(buf, 256, fp);
490 //
491 if (strstr(buf, "symmetric") != NULL) {
492 isSym = true;
493 }
494 else {
495 isSym = false;
496 upper_flag = false;
497 }
498
499 if (strstr(buf, "complex") != NULL) {
500 isComplex = true;
501 }
502 else {
503 isComplex = false;
504 }
505
506 fprintf(stderr, "symmetric = %s\n", isSym ? "true " : "false");
507 fprintf(stderr, "scaling = %d\n", scaling);
508 fprintf(stderr, "upper = %s\n", upper_flag ? "true" : "false");
509 if (kernel_detection_all) {
510 fprintf(stderr, "kernel detection is activated for all submatrices\n");
511 }
512 if (excl_flag) {
513 fprintf(stderr, "list of singular nodes %s\n", fname1);
514 }
515 while (1) {
516 fgets(buf, 256, fp);
517 if (buf[0] != '%') {
518 sscanf(buf, "%d %d %d", &nrow, &itmp, &nnz);
519 break;
520 }
521 }
522 irow = new int[nnz];
523 jcol = new int[nnz];
524 nnz1 = 0;
525 if (isComplex) {
526 double xreal, ximag;
527 valc = new complex<double>[nnz];
528 if (upper_flag) {
529 for (int i = 0; i < nnz; i++) {
530 fscanf(fp, "%d %d %lf %lf", &jcol[i], &irow[i], &xreal, &ximag);
531 valc[i] = complex<double>(xreal, ximag);
532 irow[i]--;
533 jcol[i]--;
534 if (isSym && irow[i] > jcol[i]) {
535 fprintf(stderr, "exchanged : %d > %d\n", irow[i], jcol[i]);
536 itmp = irow[i];
537 irow[i] = jcol[i];
538 jcol[i] = itmp;
539 }
540 }
541 }
542 else {
543 for (int i = 0; i < nnz; i++) {
544 fscanf(fp, "%d %d %lf %lf", &irow[i], &jcol[i], &xreal, &ximag);
545 valc[i] = complex<double>(xreal, ximag);
546 irow[i]--;
547 jcol[i]--;
548 if (isSym && irow[i] < jcol[i]) {
549 fprintf(stderr, "exchanged : %d > %d\n", irow[i], jcol[i]);
550 itmp = irow[i];
551 irow[i] = jcol[i];
552 jcol[i] = itmp;
553 }
554 }
555 }
556 }
557 else { // if (isComplex)
558 val = new double[nnz];
559 if (upper_flag) {
560 for (int i = 0; i < nnz; i++) {
561 fscanf(fp, "%d %d %lf", &jcol[i], &irow[i], &val[i]); // read lower
562 irow[i]--;
563 jcol[i]--;
564 if (isSym && irow[i] > jcol[i]) {
565 fprintf(stderr, "exchanged : %d > %d\n", irow[i], jcol[i]);
566 itmp = irow[i];
567 irow[i] = jcol[i];
568 jcol[i] = itmp;
569 }
570 }
571 }
572 else {
573 int ii = 0;
574 int itmp, jtmp;
575 double vtmp;
576 for (int i = 0; i < nnz; i++) {
577 fscanf(fp, "%d %d %lf", &itmp, &jtmp, &vtmp);
578 if (vtmp != 0.0 || (itmp == jtmp)) {
579 // if (true) { // 04 Apr.2018
580 irow[ii] = itmp - 1;
581 jcol[ii] = jtmp - 1;
582 val[ii] = vtmp;
583 ii++;
584 }
585 else {
586 nnz1++;
587 }
588 }
589 } // else
590 } // if (isComplex)
591 fclose (fp);
592 if (nnz1 > 0) {
593 fprintf(stderr, "%s %d : %d zero entries excluded %d -> %d\n",
594 __FILE__, __LINE__, nnz1, nnz, (nnz - nnz1));
595 nnz = nnz - nnz1;
596 }
597 if (excl_flag) {
598 if ((fp = fopen(fname1, "r")) == NULL) {
599 fprintf(stderr, "fail to open %s\n", fname1);
600 }
601 fgets(buf, 256, fp);
602 sscanf(buf, "# %d", &nexcl);
603 indx_excl = new int[nexcl];
604 for (int i = 0; i < nexcl; i++) {
605 fgets(buf, 256, fp);
606 sscanf(buf, "%d", &itmp);
607 indx_excl[i] = itmp;
608 }
609 fclose(fp);
610 }
611
612 int *mask = new int[nrow];
613 int *old2new = new int[nrow];
614 for (int i = 0; i < nrow; i++) {
615 mask[i] = 1;
616 }
617 for (int i = 0; i < nexcl; i++) {
618 mask[indx_excl[i]] = 0;
619 }
620 itmp = 0;
621 jtmp = nrow - nexcl;
622 for (int i = 0; i < nrow; i++) {
623 if (mask[i] == 1) {
624 old2new[i] = itmp++;
625 }
626 else {
627 old2new[i] = jtmp++;
628 }
629 }
630 nrow -= nexcl;
631
632 ind_cols_tmp = new std::list<int>[nrow];
633 fprintf(stderr, "%s %d : getnerate_CSR\n", __FILE__, __LINE__);
634 if (isComplex) {
635 val_tmpc = new std::list<complex<double> >[nrow];
636 nnz1 = nnz;
637 flag_modified = generate_CSR<complex<double> >(ind_cols_tmp, val_tmpc,
638 nrow, &nnz1, mask, old2new,
639 irow, jcol, valc, (!isSym));
640 }
641 else {
642 val_tmp = new std::list<double>[nrow];
643 nnz1 = nnz;
644 flag_modified = generate_CSR<double>(ind_cols_tmp, val_tmp,
645 nrow, &nnz1, mask, old2new,
646 irow, jcol, val, (!isSym));
647 }
648 if (flag_modified) {
649 fprintf(stderr, "%s %d : matrix is not structual symmetric %d ->%d\n",
650 __FILE__, __LINE__, nnz, nnz1);
651 }
652 nnz = nnz1;
653 delete [] irow;
654 delete [] jcol;
655 delete [] mask;
656 delete [] old2new;
657 if (isComplex) {
658 delete [] valc;
659 }
660 else {
661 delete [] val;
662 }
663
664 if (upper_flag) {
665 for (int i = 0; i < nrow; i++) {
666 if (ind_cols_tmp[i].front() != i) {
667 nnz++;
668 }
669 }
670 }
671 else {
672 for (int i = 0; i < nrow; i++) {
673 if (ind_cols_tmp[i].back() != i) {
674 nnz++;
675 }
676 }
677 }
678 fprintf(stderr, "%s %d : copy_CSR\n", __FILE__, __LINE__);
679 ptrows = new int[nrow + 1];
680 indcols = new int[nnz];
681 if (isComplex) {
682 ccoefs = new complex<double>[nnz];
683 copy_CSR<complex<double> >(indcols, ptrows, ccoefs,
684 nrow, upper_flag, isSym,
685 ind_cols_tmp, val_tmpc);
686 }
687 else {
688 coefs = new double[nnz];
689 copy_CSR<double>(indcols, ptrows, coefs,
690 nrow, upper_flag, isSym,
691 ind_cols_tmp, val_tmp);
692 }
693 delete [] ind_cols_tmp;
694
695 if (isComplex) {
696 delete [] val_tmpc;
697 }
698 else {
699 delete [] val_tmp;
700 }
701 int pid = get_process_id();
702 #if 1
703 fprintf(stderr, "pid = %d\n", pid);
704 sprintf(fname, "dissection.%04d.log", pid);
705 fp = fopen(fname, "a");
706 #else
707 fp = stderr;
708 #endif
709 for (int i = 0; i < argc; i++) {
710 fprintf(fp, "%s ", argv[i]);
711 }
712 fprintf(fp, "\n");
713 fprintf(stderr, "%s %d : before pthread_create\n", __FILE__, __LINE__);
714 void* results;
715 #ifdef POSIX_THREADS
716 pthread_attr_t th_attr;
717 pthread_t thread;
718 pthread_attr_init(&th_attr);
719 pthread_attr_setdetachstate(&th_attr, PTHREAD_CREATE_JOINABLE);
720 int pthid = pthread_create(&thread, &th_attr,
721 &thread_child,
722 (void *)&pid);
723 if (pthid != 0) {
724 cout << "bad thread creation ? " << pid << endl;
725 exit(0);
726 }
727 fprintf(stderr, "%s %d : after pthread_create\n", __FILE__, __LINE__);
728 #endif
729 if (isWhole) {
730 isSym = true;
731 upper_flag = false;
732 }
733
734 if (isComplex) {
735 DissectionSolver<complex<double>, double>*dslv =
736 new DissectionSolver<complex<double>, double>(num_threads, true, 0, fp);
737
738 int called = 0;
739 clock_t t0_cpu, t1_cpu, t2_cpu, t3_cpu, t4_cpu, t5_cpu;
740 elapsed_t t0_elapsed, t1_elapsed, t2_elapsed, t3_elapsed,
741 t4_elapsed, t5_elapsed;
742 #ifdef BLAS_MKL
743 mkl_set_num_threads(1);
744 #endif
745 #ifdef VECLIB
746 setenv("VECLIB_MAXIMUM_THREADS", "1", true);
747 #endif
748 t0_cpu = clock();
749 get_realtime(&t0_elapsed);
750
751 dslv->SymbolicFact(nrow, (int *)ptrows, (int *)indcols,
752 isSym,
753 upper_flag,
754 isWhole,
755 decomposer, numlevels, minNodes);
756
757 t1_cpu = clock();
758 get_realtime(&t1_elapsed);
759
760 t2_cpu = clock();
761 get_realtime(&t2_elapsed);
762 #ifdef POSIX_THREADS
763 _stat = 1;
764 usleep(5000);
765 #endif
766 dslv->NumericFact(0, (complex<double> *)ccoefs, scaling,
767 eps_pivot, kernel_detection_all, 4, -1.0,
768 assume_invertible);
769 #ifdef POSIX_THREADS
770 _stat = (-1);
771 #endif
772 t3_cpu = clock();
773 get_realtime(&t3_elapsed);
774 #ifdef POSIX_THREADS
775 usleep(5000);
776 #endif
777 fprintf(stderr, "%s %d : NumericFact() done\n", __FILE__, __LINE__);
778
779 int n0;
780 n0 = dslv->kern_dimension();
781 fprintf(fp, "%s %d : ## kernel dimension = %d\n", __FILE__, __LINE__, n0);
782
783 complex<double> *x = new complex<double>[nrow];
784 complex<double> *y = new complex<double>[nrow];
785 complex<double> *z = new complex<double>[nrow];
786 for (int i = 0; i < nrow; i++) {
787 y[i] = complex<double>((double)(i % 11), 0.0);
788 }
789 dslv->SpMV(y, x);
790 dslv->SpMV(x, y);
791 for (int i = 0; i < nrow; i++) {
792 z[i] = y[i];
793 }
794
795 t4_cpu = clock();
796 get_realtime(&t4_elapsed);
797 #ifdef POSIX_THREADS
798 _stat = 1;
799 usleep(5000);
800 #endif
801 dslv->SolveSingle(y, true, false, true); // with projection + scaling
802 #ifdef POSIX_THREADS
803 _stat = (-1);
804 usleep(5000);
805 #endif
806 fprintf(stderr, "%s %d : SolveSingle() done\n", __FILE__, __LINE__);
807 t5_cpu = clock();
808 get_realtime(&t5_elapsed);
809 double norm0, norm1;
810 norm0 = 0.0;
811 norm1 = 0.0;
812 for (int i = 0; i < nrow; i++) {
813 norm0 += x[i].real() * x[i].real() + x[i].imag() * x[i].imag();
814 complex<double> ztmp = y[i] - x[i];
815 norm1 += ztmp.real() * ztmp.real() + ztmp.imag() * ztmp.imag();
816 }
817 fprintf(fp, "%s %d : ## error = %18.7e\n",
818 __FILE__, __LINE__,
819 sqrt(norm1 / norm0));
820
821 dslv->SpMV(y, x);
822
823 norm0 = 0.0;
824 norm1 = 0.0;
825 for (int i = 0; i < nrow; i++) {
826 norm0 += z[i].real() * z[i].real() + z[i].imag() * z[i].imag();
827 complex<double> ztmp = z[i] - x[i];
828 norm1 += ztmp.real() * ztmp.real() + ztmp.imag() * ztmp.imag();
829 }
830 fprintf(fp, "%s %d : ## residual = %18.7e\n",
831 __FILE__, __LINE__,
832 sqrt(norm1 / norm0));
833 #ifdef POSIX_THREADS
834 _stat = 0;
835 pthread_attr_destroy(&th_attr);
836 pthid = pthread_join(thread, &results);
837 if (pthid != 0) {
838 cout << "bad thread join ? " << pthid << endl;
839 exit(0);
840 }
841 #endif
842 fprintf(fp, "%s %d : ## symbolic fact : cpu time = %.4e elapsed time = %.4e\n",
843 __FILE__, __LINE__,
844 (double)(t1_cpu - t0_cpu) / (double)CLOCKS_PER_SEC,
845 convert_time(t1_elapsed, t0_elapsed));
846
847 fprintf(fp, "%s %d : ## numeric fact : cpu time = %.4e elapsed time = %.4e\n",
848 __FILE__, __LINE__,
849 (double)(t3_cpu - t2_cpu) / (double)CLOCKS_PER_SEC,
850 convert_time(t3_elapsed, t2_elapsed));
851
852 fprintf(fp, "%s %d : ## solve single RHS : cpu time = %.4e elapsed time = %.4e\n",
853 __FILE__, __LINE__,
854 (double)(t5_cpu - t4_cpu) / (double)CLOCKS_PER_SEC,
855 convert_time(t5_elapsed, t4_elapsed));
856
857 delete dslv;
858 delete [] ptrows;
859 delete [] indcols;
860 delete [] ccoefs;
861 delete [] x;
862 delete [] y;
863 delete [] z;
864 } // if (isComplex)
865 else {
866 DissectionSolver<double> *dslv =
867 new DissectionSolver<double>(num_threads, true, 0, fp);
868
869 int called = 0;
870 clock_t t0_cpu, t1_cpu, t2_cpu, t3_cpu, t4_cpu, t5_cpu;
871 elapsed_t t0_elapsed, t1_elapsed, t2_elapsed, t3_elapsed,
872 t4_elapsed, t5_elapsed;
873 #ifdef BLAS_MKL
874 mkl_set_num_threads(1);
875 #endif
876 #ifdef VECLIB
877 setenv("VECLIB_MAXIMUM_THREADS", "1", true);
878 #endif
879 t0_cpu = clock();
880 get_realtime(&t0_elapsed);
881
882 dslv->SymbolicFact(nrow, (int *)ptrows, (int *)indcols,
883 isSym,
884 upper_flag,
885 isWhole,
886 decomposer, numlevels, minNodes);
887 // sym, upper
888 // dslv->SaveMMMatrix(0, coefs);
889 // exit(-1);
890 t1_cpu = clock();
891 get_realtime(&t1_elapsed);
892 #ifdef POSIX_THREADS
893 _stat = 1;
894 usleep(5000);
895 #endif
896 t2_cpu = clock();
897 get_realtime(&t2_elapsed);
898 dslv->NumericFact(0, (double *)coefs, scaling,
899 eps_pivot, kernel_detection_all,
900 4, -1.0,
901 assume_invertible);
902 t3_cpu = clock();
903 get_realtime(&t3_elapsed);
904 #ifdef POSIX_THREADS
905 _stat = (-1);
906 usleep(5000);
907 #endif
908 fprintf(stderr, "%s %d : NumericFact() done\n", __FILE__, __LINE__);
909 int n0;
910 n0 = dslv->kern_dimension();
911 fprintf(fp, "%s %d : ## kernel dimension = %d\n", __FILE__, __LINE__, n0);
912
913 double *x = new double[nrow];
914 double *y = new double[nrow];
915 double *z = new double[nrow];
916 for (int i = 0; i < nrow; i++) {
917 y[i] = (double)(i % 11);
918 }
919 dslv->SpMV(y, x);
920 if (n0 > 0) {
921 dslv->ProjectionKernelOrthSingle(y, "given", false);
922 }
923 dslv->SpMV(x, y);
924 for (int i = 0; i < nrow; i++) {
925 z[i] = y[i];
926 }
927 #ifdef POSIX_THREADS
928 _stat = 1;
929 usleep(5000);
930 #endif
931 t4_cpu = clock();
932 get_realtime(&t4_elapsed);
933 dslv->SolveSingle(y, true, false, true); // with projection + scaling
934 #ifdef POSIX_THREADS
935 _stat = (-1);
936 usleep(5000);
937 #endif
938 fprintf(stderr, "%s %d : SolveSingle() done\n", __FILE__, __LINE__);
939 if (n0 > 0) {
940 dslv->ProjectionImageSingle(y);
941 }
942 t5_cpu = clock();
943 get_realtime(&t5_elapsed);
944
945 double norm0, norm1;
946 norm0 = 0.0;
947 norm1 = 0.0;
948 for (int i = 0; i < nrow; i++) {
949 norm0 += x[i] * x[i];
950 norm1 += (y[i] - x[i]) * (y[i] - x[i]);
951 }
952 fprintf(fp, "%s %d : ## error = %18.7e\n",
953 __FILE__, __LINE__, sqrt(norm1 / norm0));
954
955 dslv->SpMV(y, x);
956
957 norm0 = 0.0;
958 norm1 = 0.0;
959 for (int i = 0; i < nrow; i++) {
960 norm0 += z[i] * z[i];
961 norm1 += (z[i] - x[i]) * (z[i] - x[i]);
962 }
963 fprintf(fp, "%s %d : ## residual = %18.7e\n",
964 __FILE__, __LINE__, sqrt(norm1 / norm0));
965 #ifdef POSIX_THREADS
966 _stat = 0;
967 pthread_attr_destroy(&th_attr);
968 pthid = pthread_join(thread, &results);
969 if (pthid != 0) {
970 cout << "bad thread join ? " << pthid << endl;
971 exit(0);
972 }
973 #endif
974 fprintf(fp, "%s %d : ## symbolic fact : cpu time = %.4e elapsed time = %.4e\n",
975 __FILE__, __LINE__,
976 (double)(t1_cpu - t0_cpu) / (double)CLOCKS_PER_SEC,
977 convert_time(t1_elapsed, t0_elapsed));
978
979 fprintf(fp, "%s %d : ## numeric fact : cpu time = %.4e elapsed time = %.4e\n",
980 __FILE__, __LINE__,
981 (double)(t3_cpu - t2_cpu) / (double)CLOCKS_PER_SEC,
982 convert_time(t3_elapsed, t2_elapsed));
983
984 fprintf(fp, "%s %d : ## solve single RHS : cpu time = %.4e elapsed time = %.4e\n",
985 __FILE__, __LINE__,
986 (double)(t5_cpu - t4_cpu) / (double)CLOCKS_PER_SEC,
987 convert_time(t5_elapsed, t4_elapsed));
988
989 delete dslv;
990 delete [] ptrows;
991 delete [] indcols;
992 delete [] coefs;
993 delete [] x;
994 delete [] y;
995 delete [] z;
996 }
997 fclose(fp);
998
999 }