29 #ifndef BASKER_DEF_HPP
30 #define BASKER_DEF_HPP
32 #include "basker_decl.hpp"
33 #include "basker_scalartraits.hpp"
45 namespace BaskerClassicNS{
47 template <
class Int,
class Entry>
48 BaskerClassic<Int, Entry>::BaskerClassic()
52 A =
new basker_matrix<Int,Entry>;
55 L =
new basker_matrix<Int, Entry>;
59 U =
new basker_matrix<Int,Entry>;
70 template <
class Int,
class Entry>
71 BaskerClassic<Int, Entry>::BaskerClassic(Int nnzL, Int nnzU)
75 A =
new basker_matrix<Int, Entry>;
77 L =
new basker_matrix<Int, Entry>;
80 U =
new basker_matrix<Int, Entry>;
91 template <
class Int,
class Entry>
92 BaskerClassic<Int, Entry>::~BaskerClassic()
114 template <
class Int,
class Entry>
115 int BaskerClassic<Int,Entry>:: basker_dfs
131 Int start, end, done, *store ;
136 bool has_elements =
true;
153 BASKERASSERT (color[j] == 1) ;
161 for ( i1 = start ; i1 < end ; i1++ )
175 pattern[--*top] = j ;
179 has_elements =
false;
188 std::cout <<
"Out of DFS: " << j << std::endl;
193 template <
class Int,
class Entry>
194 int BaskerClassic<Int,Entry>::factor(Int nrow, Int ncol , Int nnz, Int *col_ptr, Int *row_idx, Entry *val)
200 BASKERASSERT(nrow > 0);
201 BASKERASSERT(ncol > 0);
202 BASKERASSERT(nnz > 0);
209 A->col_ptr = col_ptr;
210 A->row_idx = row_idx;
222 L->col_ptr =
new Int[ncol+1]();
224 L->row_idx =
new Int[L->nnz]();
226 L->val =
new Entry[L->nnz]();
235 U->col_ptr =
new Int[ncol+1]();
237 U->row_idx =
new Int[U->nnz]();
239 U->val =
new Entry[U->nnz]();
241 if((L->col_ptr ==
nullptr) || (L->row_idx ==
nullptr) || (L->val ==
nullptr) ||
242 (U->col_ptr ==
nullptr) || (U->row_idx ==
nullptr) || (U->val ==
nullptr))
253 tptr =
new Int[(ncol)+(4*nrow)]();
255 X =
new Entry[2*nrow]();
257 pinv =
new Int[ncol+1]();
260 if( (tptr ==
nullptr) || (X ==
nullptr) || (pinv ==
nullptr) )
270 Int *color, *pattern, *stack;
271 Int top, top1, maxindex, t;
272 Int lnnz, unnz, xnnz, lcnt, ucnt;
273 Int cu_ltop, cu_utop;
276 Entry pivot, value, xj;
295 for(k = 0 ; k < ncol; k++)
301 for (k = 0; k < ncol; k++)
305 cout <<
"k = " << k << endl;
317 BASKERASSERT (top == ncol);
319 for(i = 0; i < nrow; i++)
321 BASKERASSERT(X[i] == (Entry)0);
323 for(i = 0; i < ncol; i++)
325 BASKERASSERT(color[i] == 0);
329 for( i = col_ptr[k]; i < col_ptr[k+1]; i++)
337 basker_dfs(nrow, j, L->row_idx, L->col_ptr, color, pattern, &top, pinv, stack);
346 cout << ncol << endl;
347 cout << xnnz << endl;
352 for(pp = 0; pp < xnnz; pp++)
361 p2 = L->col_ptr[t+1];
362 for(p = L->col_ptr[t]+1; p < p2; p++)
364 X[L->row_idx[p]] -= L->val[p] * xj;
372 for(i = top; i < nrow; i++)
379 absv = BASKER_ScalarTraits<Entry>::approxABS(value);
384 if( BASKER_ScalarTraits<Entry>::gt(absv , maxv))
393 ucnt = nrow - top - lcnt + 1;
395 if(maxindex == ncol || pivot == ((Entry)0))
397 cout <<
"Matrix is singular at index: " << maxindex <<
" pivot: " << pivot << endl;
406 cout <<
"Permuting pivot: " << k <<
" for row: " << maxindex << endl;
410 if(lnnz + lcnt >= L->nnz)
413 newsize = L->nnz * 1.1 + 2*nrow + 1;
415 cout <<
"Out of memory -- Reallocating. Old Size: " << L->nnz <<
" New Size: " << newsize << endl;
418 L->row_idx = int_realloc(L->row_idx , L->nnz, newsize);
421 cout <<
"WARNING: Cannot Realloc Memory" << endl;
426 L->val = entry_realloc(L->val, L->nnz, newsize);
429 cout <<
"WARNING: Cannot Realloc Memory" << endl;
437 if(unnz + ucnt >= U->nnz)
439 newsize = U->nnz*1.1 + 2*nrow + 1;
441 cout <<
"Out of memory -- Reallocating. Old Size: " << U->nnz <<
" New Size: " << newsize << endl;
444 U->row_idx = int_realloc(U->row_idx, U->nnz, newsize);
447 cout <<
"WARNING: Cannot Realloc Memory" << endl;
453 U->val = entry_realloc(U->val, U->nnz, newsize);
456 cout <<
"WARNING: Cannot Realloc Memory" << endl;
464 L->row_idx[lnnz] = maxindex;
468 Entry last_v_temp = 0;
470 for(i = top; i < nrow; i++)
478 if(X[j] != ((Entry)0))
485 cout <<
"BASKER: Insufficent memory for U" << endl;
492 U->row_idx[unnz] = pinv[j];
508 cout <<
"BASKER: Insufficent memory for L" << endl;
513 L->row_idx[lnnz] = j;
515 L->val[lnnz] = BASKER_ScalarTraits<Entry>::divide(X[j],pivot);
527 U->row_idx[unnz] = k;
528 U->val[unnz] = last_v_temp;
534 L->col_ptr[k] = cu_ltop;
535 L->col_ptr[k+1] = lnnz;
538 U->col_ptr[k] = cu_utop;
539 U->col_ptr[k+1] = unnz;
546 for(k = 0; k < lnnz; k++)
548 printf(
"L[%d]=%g" , k , L->val[k]);
551 for(k = 0; k < lnnz; k++)
553 printf(
"Li[%d]=%d", k, L->row_idx[k]);
556 for(k = 0; k < nrow; k++)
558 printf(
"p[%d]=%d", k, pinv[k]);
563 for(k = 0; k < ncol; k++)
565 printf(
"Up[%d]=%d", k, U->col_ptr[k]);
569 for(k = 0; k < unnz; k++)
571 printf(
"U[%d]=%g" , k , U->val[k]);
574 for(k = 0; k < unnz; k++)
576 printf(
"Ui[%d]=%d", k, U->row_idx[k]);
583 for( i = 0; i < ncol; i++)
585 for(k = L->col_ptr[i]; k < L->col_ptr[i+1]; k++)
595 cout <<
"After Permuting" << endl;
596 for(k = 0; k < lnnz; k++)
598 printf(
"Li[%d]=%d", k, L->row_idx[k]);
615 template <
class Int,
class Entry>
616 Int BaskerClassic<Int, Entry>::get_NnzL()
621 template <
class Int,
class Entry>
622 Int BaskerClassic<Int, Entry>::get_NnzU()
627 template <
class Int,
class Entry>
628 Int BaskerClassic<Int, Entry>::get_NnzLU()
630 return (actual_lnnz + actual_unnz);
633 template <
class Int,
class Entry>
634 int BaskerClassic<Int, Entry>::returnL(Int *dim, Int *nnz, Int **col_ptr, Int **row_idx, Entry **val)
643 *col_ptr =
new Int[L->nrow+1];
645 *row_idx =
new Int[L->nnz];
647 *val =
new Entry[L->nnz];
649 if( (*col_ptr ==
nullptr) || (*row_idx ==
nullptr) || (*val ==
nullptr) )
654 for(i = 0; i < L->nrow+1; i++)
656 (*col_ptr)[i] = L->col_ptr[i];
659 for(i = 0; i < actual_lnnz; i++)
661 (*row_idx)[i] = pinv[L->row_idx[i]];
662 (*val)[i] = L->val[i];
668 template <
class Int,
class Entry>
669 int BaskerClassic<Int, Entry>::returnU(Int *dim, Int *nnz, Int **col_ptr, Int **row_idx, Entry **val)
676 *col_ptr =
new Int[U->nrow+1];
678 *row_idx =
new Int[U->nnz];
680 *val =
new Entry[U->nnz];
682 if( (*col_ptr ==
nullptr) || (*row_idx ==
nullptr) || (*val ==
nullptr) )
687 for(i = 0; i < U->nrow+1; i++)
689 (*col_ptr)[i] = U->col_ptr[i];
691 for(i = 0; i < actual_unnz; i++)
693 (*row_idx)[i] = U->row_idx[i];
694 (*val)[i] = U->val[i];
699 template <
class Int,
class Entry>
700 int BaskerClassic<Int, Entry>::returnP(Int** p)
704 *p =
new Int[A->nrow];
706 if( (*p ==
nullptr ) )
711 for(i = 0; i < A->nrow; i++)
718 template <
class Int,
class Entry>
719 void BaskerClassic<Int, Entry>::free_factor()
738 template <
class Int,
class Entry>
739 void BaskerClassic<Int, Entry>::free_perm_matrix()
746 template <
class Int,
class Entry>
747 int BaskerClassic<Int, Entry>::solveMultiple(Int nrhs, Entry *b, Entry *x)
750 for(i = 0; i < nrhs; i++)
753 int result = solve(&(b[k]), &(x[k]));
756 cout <<
"Error in Solving \n";
764 template <
class Int,
class Entry>
765 int BaskerClassic<Int, Entry>::solve(Entry* b, Entry* x)
773 Entry* temp =
new Entry[A->nrow]();
776 for(i = 0 ; i < A->ncol; i++)
782 result = low_tri_solve_csc(L->nrow, L->col_ptr, L->row_idx, L->val, temp, x);
785 result = up_tri_solve_csc(U->nrow, U->col_ptr, U->row_idx, U->val, x, temp);
794 template <
class Int,
class Entry>
795 int BaskerClassic<Int, Entry>::low_tri_solve_csc( Int n, Int *col_ptr, Int *row_idx, Entry* val, Entry *x, Entry *b)
799 for(i = 0; i < n ; i++)
802 BASKERASSERT(val[col_ptr[i]] != (Entry)0);
804 if(val[col_ptr[i]] == (Entry) 0)
809 x[i] = BASKER_ScalarTraits<Entry>::divide(b[i], val[col_ptr[i]]);
811 for(j = col_ptr[i]+1; j < (col_ptr[i+1]); j++)
813 b[pinv[row_idx[j]]] = b[pinv[row_idx[j]]] - (val[j]*x[i]);
819 template <
class Int,
class Entry>
820 int BaskerClassic<Int, Entry>::up_tri_solve_csc( Int n, Int *col_ptr, Int *row_idx, Entry *val, Entry *x, Entry *b)
824 for(i = n; i > 1 ; i--)
828 BASKERASSERT(val[col_ptr[i]-1] != (Entry)0);
830 if(val[col_ptr[i]-1] == (Entry) 0)
832 cout <<
"Dig(" << i <<
") = " << val[col_ptr[i]-1] << endl;
837 x[ii] = BASKER_ScalarTraits<Entry>::divide(b[ii],val[col_ptr[i]-1]);
839 for(j = (col_ptr[i]-2); j >= (col_ptr[ii]); j--)
841 b[row_idx[j]] = b[row_idx[j]] - (val[j]*x[ii]);
845 x[0] = BASKER_ScalarTraits<Entry>::divide(b[0],val[col_ptr[1]-1]);
849 template <
class Int,
class Entry>
850 int BaskerClassic<Int, Entry>::preorder(Int *row_perm, Int *col_perm)
853 basker_matrix <Int, Entry> *B;
854 B =
new basker_matrix<Int, Entry>;
858 B->col_ptr = (Int *) BASKERCALLOC(A->ncol + 1,
sizeof(Int));
859 B->row_idx = (Int *) BASKERCALLOC(A->nnz,
sizeof(Int));
860 B->val = (Entry *) BASKERCALLOC(A->val,
sizeof(Int));
862 if( (B->col_ptr ==
nullptr) || (B->row_idx ==
nullptr) || (B->val ==
nullptr) )
868 (void) permute_column(col_perm, B);
869 (void) permute_row(row_perm, B);
873 A->col_ptr = B->col_ptr;
874 A->row_idx = B->row_idx;
882 template <
class Int,
class Entry>
883 int BaskerClassic <Int, Entry>::permute_column(Int *p, basker_matrix<Int,Entry> *B)
889 for(j=0; j < B->ncol; j++)
892 B->col_ptr[i+1] = A->col_ptr[j+1] - A->col_ptr[j];
896 for(j=0; j < B->ncol; j++)
898 B->col_ptr[j+1] = B->col_ptr[j+1] + B->col_ptr[j];
903 for(ii = 0 ; ii < B->ncol; ii++)
905 ko = B->col_ptr(p[ii]);
906 for(k = A->col_ptr[ii]; k < A->col_ptr[ii+1]; k++)
908 B->row_index[ko] = A->row_index[k];
909 B->val[ko] = A->val[ko];
916 template <
class Int,
class Entry>
917 int BaskerClassic <Int, Entry>::permute_row(Int *p, basker_matrix<Int,Entry> *B)
920 for(k=0; k < A->nnz; k++)
922 B->row_idx[k] = p[A->row_idx[k]];
927 template <
class Int,
class Entry>
928 int BaskerClassic <Int, Entry>::sort_factors()
935 for(i = 0 ; i < L->ncol; i++)
940 for(j = L->col_ptr[i]+1; j < (L->col_ptr[i+1]); j++)
942 if(L->row_idx[j] < val)
948 Int temp_index = L->row_idx[L->col_ptr[i]];
949 Entry temp_entry = L->val[L->col_ptr[i]];
950 L->row_idx[L->col_ptr[i]] = val;
951 L->val[L->col_ptr[i]] = L->val[p];
952 L->row_idx[p] = temp_index;
953 L->val[p] = temp_entry;
958 for(i = 0 ; i < U->ncol; i++)
960 p = U->col_ptr[i+1]-1;
963 for(j = U->col_ptr[i]; j < (U->col_ptr[i+1]-1); j++)
965 if(U->row_idx[j] > val)
971 Int temp_index = U->row_idx[U->col_ptr[i+1]-1];
972 Entry temp_entry = U->val[U->col_ptr[i+1]-1];
973 U->row_idx[U->col_ptr[i+1]-1] = val;
974 U->val[U->col_ptr[i+1]-1] = U->val[p];
975 U->row_idx[p] = temp_index;
976 U->val[p] = temp_entry;
982 template <
class Int,
class Entry>
983 Entry* BaskerClassic <Int, Entry>::entry_realloc(Entry *old , Int old_size, Int new_size)
985 Entry *new_entry =
new Entry[new_size];
986 for(Int i = 0; i < old_size; i++)
989 new_entry[i] = old[i];
996 template <
class Int,
class Entry>
997 Int* BaskerClassic <Int, Entry>::int_realloc(Int *old, Int old_size, Int new_size)
999 Int *new_int =
new Int[new_size];
1000 for(Int i =0; i < old_size; i++)
1003 new_int[i] = old[i];