10 #ifndef BASKER_DEF_HPP
11 #define BASKER_DEF_HPP
13 #include "basker_decl.hpp"
14 #include "basker_scalartraits.hpp"
26 namespace BaskerClassicNS{
28 template <
class Int,
class Entry>
29 BaskerClassic<Int, Entry>::BaskerClassic()
33 A =
new basker_matrix<Int,Entry>;
36 L =
new basker_matrix<Int, Entry>;
40 U =
new basker_matrix<Int,Entry>;
51 template <
class Int,
class Entry>
52 BaskerClassic<Int, Entry>::BaskerClassic(Int nnzL, Int nnzU)
56 A =
new basker_matrix<Int, Entry>;
58 L =
new basker_matrix<Int, Entry>;
61 U =
new basker_matrix<Int, Entry>;
72 template <
class Int,
class Entry>
73 BaskerClassic<Int, Entry>::~BaskerClassic()
95 template <
class Int,
class Entry>
96 int BaskerClassic<Int,Entry>:: basker_dfs
112 Int start, end, done, *store ;
117 bool has_elements =
true;
134 BASKERASSERT (color[j] == 1) ;
142 for ( i1 = start ; i1 < end ; i1++ )
156 pattern[--*top] = j ;
160 has_elements =
false;
169 std::cout <<
"Out of DFS: " << j << std::endl;
174 template <
class Int,
class Entry>
175 int BaskerClassic<Int,Entry>::factor(Int nrow, Int ncol , Int nnz, Int *col_ptr, Int *row_idx, Entry *val)
181 BASKERASSERT(nrow > 0);
182 BASKERASSERT(ncol > 0);
183 BASKERASSERT(nnz > 0);
190 A->col_ptr = col_ptr;
191 A->row_idx = row_idx;
211 L->col_ptr =
new Int[ncol+1]();
213 L->row_idx =
new Int[L->nnz]();
215 L->val =
new Entry[L->nnz]();
224 U->col_ptr =
new Int[ncol+1]();
226 U->row_idx =
new Int[U->nnz]();
228 U->val =
new Entry[U->nnz]();
230 if((L->col_ptr ==
nullptr) || (L->row_idx ==
nullptr) || (L->val ==
nullptr) ||
231 (U->col_ptr ==
nullptr) || (U->row_idx ==
nullptr) || (U->val ==
nullptr))
239 Int *color, *pattern, *stack;
241 color =
new Int[ncol]();
242 pattern =
new Int[nrow]();
243 stack =
new Int[2*nrow]();
245 X =
new Entry[2*nrow]();
247 pinv =
new Int[ncol+1]();
250 if( (color ==
nullptr) || (pattern ==
nullptr) || (stack ==
nullptr) || (X ==
nullptr) || (pinv ==
nullptr) )
260 Int top, top1, maxindex, t;
261 Int lnnz, unnz, xnnz, lcnt, ucnt;
262 Int cu_ltop, cu_utop;
265 Entry pivot, value, xj;
275 for(k = 0 ; k < ncol; k++)
281 for (k = 0; k < ncol; k++)
285 cout <<
"k = " << k << endl;
297 BASKERASSERT (top == ncol);
299 for(i = 0; i < nrow; i++)
301 BASKERASSERT(X[i] == (Entry)0);
303 for(i = 0; i < ncol; i++)
305 BASKERASSERT(color[i] == 0);
309 for( i = col_ptr[k]; i < col_ptr[k+1]; i++)
317 basker_dfs(nrow, j, L->row_idx, L->col_ptr, color, pattern, &top, pinv, stack);
326 cout << ncol << endl;
327 cout << xnnz << endl;
332 for(pp = 0; pp < xnnz; pp++)
341 p2 = L->col_ptr[t+1];
342 for(p = L->col_ptr[t]+1; p < p2; p++)
344 X[L->row_idx[p]] -= L->val[p] * xj;
352 for(i = top; i < nrow; i++)
359 absv = BASKER_ScalarTraits<Entry>::approxABS(value);
364 if( BASKER_ScalarTraits<Entry>::gt(absv , maxv))
373 ucnt = nrow - top - lcnt + 1;
375 if(maxindex == ncol || pivot == ((Entry)0))
377 cout <<
"Matrix is singular at index: " << maxindex <<
" pivot: " << pivot << endl;
386 cout <<
"Permuting pivot: " << k <<
" for row: " << maxindex << endl;
390 if(lnnz + lcnt >= L->nnz)
393 newsize = L->nnz * 1.1 + 2*nrow + 1;
395 cout <<
"Out of memory -- Reallocating. Old Size: " << L->nnz <<
" New Size: " << newsize << endl;
398 L->row_idx = int_realloc(L->row_idx , L->nnz, newsize);
401 cout <<
"WARNING: Cannot Realloc Memory" << endl;
406 L->val = entry_realloc(L->val, L->nnz, newsize);
409 cout <<
"WARNING: Cannot Realloc Memory" << endl;
417 if(unnz + ucnt >= U->nnz)
419 newsize = U->nnz*1.1 + 2*nrow + 1;
421 cout <<
"Out of memory -- Reallocating. Old Size: " << U->nnz <<
" New Size: " << newsize << endl;
424 U->row_idx = int_realloc(U->row_idx, U->nnz, newsize);
427 cout <<
"WARNING: Cannot Realloc Memory" << endl;
433 U->val = entry_realloc(U->val, U->nnz, newsize);
436 cout <<
"WARNING: Cannot Realloc Memory" << endl;
444 L->row_idx[lnnz] = maxindex;
448 Entry last_v_temp = 0;
450 for(i = top; i < nrow; i++)
458 if(X[j] != ((Entry)0))
465 cout <<
"BASKER: Insufficent memory for U" << endl;
472 U->row_idx[unnz] = pinv[j];
488 cout <<
"BASKER: Insufficent memory for L" << endl;
493 L->row_idx[lnnz] = j;
495 L->val[lnnz] = BASKER_ScalarTraits<Entry>::divide(X[j],pivot);
507 U->row_idx[unnz] = k;
508 U->val[unnz] = last_v_temp;
514 L->col_ptr[k] = cu_ltop;
515 L->col_ptr[k+1] = lnnz;
518 U->col_ptr[k] = cu_utop;
519 U->col_ptr[k+1] = unnz;
526 for(k = 0; k < lnnz; k++)
528 printf(
"L[%d]=%g" , k , L->val[k]);
531 for(k = 0; k < lnnz; k++)
533 printf(
"Li[%d]=%d", k, L->row_idx[k]);
536 for(k = 0; k < nrow; k++)
538 printf(
"p[%d]=%d", k, pinv[k]);
543 for(k = 0; k < ncol; k++)
545 printf(
"Up[%d]=%d", k, U->col_ptr[k]);
549 for(k = 0; k < unnz; k++)
551 printf(
"U[%d]=%g" , k , U->val[k]);
554 for(k = 0; k < unnz; k++)
556 printf(
"Ui[%d]=%d", k, U->row_idx[k]);
563 for( i = 0; i < ncol; i++)
565 for(k = L->col_ptr[i]; k < L->col_ptr[i+1]; k++)
575 cout <<
"After Permuting" << endl;
576 for(k = 0; k < lnnz; k++)
578 printf(
"Li[%d]=%d", k, L->row_idx[k]);
597 template <
class Int,
class Entry>
598 Int BaskerClassic<Int, Entry>::get_NnzL()
603 template <
class Int,
class Entry>
604 Int BaskerClassic<Int, Entry>::get_NnzU()
609 template <
class Int,
class Entry>
610 Int BaskerClassic<Int, Entry>::get_NnzLU()
612 return (actual_lnnz + actual_unnz);
615 template <
class Int,
class Entry>
616 int BaskerClassic<Int, Entry>::returnL(Int *dim, Int *nnz, Int **col_ptr, Int **row_idx, Entry **val)
625 *col_ptr =
new Int[L->nrow+1];
627 *row_idx =
new Int[L->nnz];
629 *val =
new Entry[L->nnz];
631 if( (*col_ptr ==
nullptr) || (*row_idx ==
nullptr) || (*val ==
nullptr) )
636 for(i = 0; i < L->nrow+1; i++)
638 (*col_ptr)[i] = L->col_ptr[i];
641 for(i = 0; i < actual_lnnz; i++)
643 (*row_idx)[i] = pinv[L->row_idx[i]];
644 (*val)[i] = L->val[i];
650 template <
class Int,
class Entry>
651 int BaskerClassic<Int, Entry>::returnU(Int *dim, Int *nnz, Int **col_ptr, Int **row_idx, Entry **val)
658 *col_ptr =
new Int[U->nrow+1];
660 *row_idx =
new Int[U->nnz];
662 *val =
new Entry[U->nnz];
664 if( (*col_ptr ==
nullptr) || (*row_idx ==
nullptr) || (*val ==
nullptr) )
669 for(i = 0; i < U->nrow+1; i++)
671 (*col_ptr)[i] = U->col_ptr[i];
673 for(i = 0; i < actual_unnz; i++)
675 (*row_idx)[i] = U->row_idx[i];
676 (*val)[i] = U->val[i];
681 template <
class Int,
class Entry>
682 int BaskerClassic<Int, Entry>::returnP(Int** p)
686 *p =
new Int[A->nrow];
688 if( (*p ==
nullptr ) )
693 for(i = 0; i < A->nrow; i++)
700 template <
class Int,
class Entry>
701 void BaskerClassic<Int, Entry>::free_factor()
721 template <
class Int,
class Entry>
722 void BaskerClassic<Int, Entry>::free_perm_matrix()
729 template <
class Int,
class Entry>
730 int BaskerClassic<Int, Entry>::solveMultiple(Int nrhs, Entry *b, Entry *x)
733 for(i = 0; i < nrhs; i++)
736 int result = solve(&(b[k]), &(x[k]));
739 cout <<
"Error in Solving \n";
747 template <
class Int,
class Entry>
748 int BaskerClassic<Int, Entry>::solve(Entry* b, Entry* x)
756 Entry* temp =
new Entry[A->nrow]();
759 for(i = 0 ; i < A->ncol; i++)
765 result = low_tri_solve_csc(L->nrow, L->col_ptr, L->row_idx, L->val, temp, x);
768 result = up_tri_solve_csc(U->nrow, U->col_ptr, U->row_idx, U->val, x, temp);
777 template <
class Int,
class Entry>
778 int BaskerClassic<Int, Entry>::low_tri_solve_csc( Int n, Int *col_ptr, Int *row_idx, Entry* val, Entry *x, Entry *b)
782 for(i = 0; i < n ; i++)
785 BASKERASSERT(val[col_ptr[i]] != (Entry)0);
787 if(val[col_ptr[i]] == (Entry) 0)
792 x[i] = BASKER_ScalarTraits<Entry>::divide(b[i], val[col_ptr[i]]);
794 for(j = col_ptr[i]+1; j < (col_ptr[i+1]); j++)
796 b[pinv[row_idx[j]]] = b[pinv[row_idx[j]]] - (val[j]*x[i]);
802 template <
class Int,
class Entry>
803 int BaskerClassic<Int, Entry>::up_tri_solve_csc( Int n, Int *col_ptr, Int *row_idx, Entry *val, Entry *x, Entry *b)
807 for(i = n; i > 1 ; i--)
811 BASKERASSERT(val[col_ptr[i]-1] != (Entry)0);
813 if(val[col_ptr[i]-1] == (Entry) 0)
815 cout <<
"Dig(" << i <<
") = " << val[col_ptr[i]-1] << endl;
820 x[ii] = BASKER_ScalarTraits<Entry>::divide(b[ii],val[col_ptr[i]-1]);
822 for(j = (col_ptr[i]-2); j >= (col_ptr[ii]); j--)
824 b[row_idx[j]] = b[row_idx[j]] - (val[j]*x[ii]);
828 x[0] = BASKER_ScalarTraits<Entry>::divide(b[0],val[col_ptr[1]-1]);
832 template <
class Int,
class Entry>
833 int BaskerClassic<Int, Entry>::preorder(Int *row_perm, Int *col_perm)
836 basker_matrix <Int, Entry> *B;
837 B =
new basker_matrix<Int, Entry>;
841 B->col_ptr = (Int *) BASKERCALLOC(A->ncol + 1,
sizeof(Int));
842 B->row_idx = (Int *) BASKERCALLOC(A->nnz,
sizeof(Int));
843 B->val = (Entry *) BASKERCALLOC(A->val,
sizeof(Int));
845 if( (B->col_ptr ==
nullptr) || (B->row_idx ==
nullptr) || (B->val ==
nullptr) )
851 (void) permute_column(col_perm, B);
852 (void) permute_row(row_perm, B);
856 A->col_ptr = B->col_ptr;
857 A->row_idx = B->row_idx;
865 template <
class Int,
class Entry>
866 int BaskerClassic <Int, Entry>::permute_column(Int *p, basker_matrix<Int,Entry> *B)
872 for(j=0; j < B->ncol; j++)
875 B->col_ptr[i+1] = A->col_ptr[j+1] - A->col_ptr[j];
879 for(j=0; j < B->ncol; j++)
881 B->col_ptr[j+1] = B->col_ptr[j+1] + B->col_ptr[j];
886 for(ii = 0 ; ii < B->ncol; ii++)
888 ko = B->col_ptr(p[ii]);
889 for(k = A->col_ptr[ii]; k < A->col_ptr[ii+1]; k++)
891 B->row_index[ko] = A->row_index[k];
892 B->val[ko] = A->val[ko];
899 template <
class Int,
class Entry>
900 int BaskerClassic <Int, Entry>::permute_row(Int *p, basker_matrix<Int,Entry> *B)
903 for(k=0; k < A->nnz; k++)
905 B->row_idx[k] = p[A->row_idx[k]];
910 template <
class Int,
class Entry>
911 int BaskerClassic <Int, Entry>::sort_factors()
918 for(i = 0 ; i < L->ncol; i++)
923 for(j = L->col_ptr[i]+1; j < (L->col_ptr[i+1]); j++)
925 if(L->row_idx[j] < val)
931 Int temp_index = L->row_idx[L->col_ptr[i]];
932 Entry temp_entry = L->val[L->col_ptr[i]];
933 L->row_idx[L->col_ptr[i]] = val;
934 L->val[L->col_ptr[i]] = L->val[p];
935 L->row_idx[p] = temp_index;
936 L->val[p] = temp_entry;
941 for(i = 0 ; i < U->ncol; i++)
943 p = U->col_ptr[i+1]-1;
946 for(j = U->col_ptr[i]; j < (U->col_ptr[i+1]-1); j++)
948 if(U->row_idx[j] > val)
954 Int temp_index = U->row_idx[U->col_ptr[i+1]-1];
955 Entry temp_entry = U->val[U->col_ptr[i+1]-1];
956 U->row_idx[U->col_ptr[i+1]-1] = val;
957 U->val[U->col_ptr[i+1]-1] = U->val[p];
958 U->row_idx[p] = temp_index;
959 U->val[p] = temp_entry;
965 template <
class Int,
class Entry>
966 Entry* BaskerClassic <Int, Entry>::entry_realloc(Entry *old , Int old_size, Int new_size)
968 Entry *new_entry =
new Entry[new_size];
969 for(Int i = 0; i < old_size; i++)
972 new_entry[i] = old[i];
979 template <
class Int,
class Entry>
980 Int* BaskerClassic <Int, Entry>::int_realloc(Int *old, Int old_size, Int new_size)
982 Int *new_int =
new Int[new_size];
983 for(Int i =0; i < old_size; i++)