# Contents of /trunk/paso/src/SparseMatrix.c

Revision 1388 - (show annotations)
Fri Jan 11 07:45:58 2008 UTC (11 years, 8 months ago) by trankine
File MIME type: text/plain
File size: 4683 byte(s)
```And get the *(&(*&(* name right
```
 1 2 /* \$Id: SparseMatrix.c 1306 2007-09-18 05:51:09Z ksteube \$ */ 3 4 /******************************************************* 5 * 6 * Copyright 2003-2007 by ACceSS MNRF 7 * Copyright 2007 by University of Queensland 8 * 9 * http://esscc.uq.edu.au 10 * Primary Business: Queensland, Australia 11 * Licensed under the Open Software License version 3.0 12 * http://www.opensource.org/licenses/osl-3.0.php 13 * 14 *******************************************************/ 15 16 /**************************************************************/ 17 18 /* Paso: SparseMatrix */ 19 20 /**************************************************************/ 21 22 /* Author: gross@access.edu.au */ 23 24 /**************************************************************/ 25 26 #include "Paso.h" 27 #include "SparseMatrix.h" 28 #include "TRILINOS.h" 29 30 /**************************************************************/ 31 32 /* allocates a SparseMatrix of type type using the given matrix pattern 33 if type is UNKOWN CSR is used. 34 if CSC or CSC_BLK1 is used pattern has to give the CSC pattern. 35 if CSR or CSR_BLK1 is used pattern has to give the CSR pattern. 36 Values are initialized by zero. */ 37 38 Paso_SparseMatrix* Paso_SparseMatrix_alloc(Paso_SparseMatrixType type,Paso_Pattern *pattern, int row_block_size, int col_block_size) { 39 40 double time0; 41 Paso_SparseMatrix*out=NULL; 42 dim_t n_norm,i; 43 Paso_SparseMatrixType pattern_format_out; 44 45 Paso_resetError(); 46 time0=Paso_timer(); 47 out=MEMALLOC(1,Paso_SparseMatrix); 48 if (! Paso_checkPtr(out)) { 49 out->pattern=NULL; 50 out->val=NULL; 51 out->reference_counter=1; 52 out->type=type; 53 54 pattern_format_out= (type & MATRIX_FORMAT_OFFSET1)? PATTERN_FORMAT_OFFSET1: PATTERN_FORMAT_DEFAULT; 55 /* ====== compressed sparse columns === */ 56 if (type & MATRIX_FORMAT_CSC) { 57 if (type & MATRIX_FORMAT_SYM) { 58 Paso_setError(TYPE_ERROR,"Generation of matrix pattern for symmetric CSC is not implemented yet."); 59 return NULL; 60 } else { 61 if ((type & MATRIX_FORMAT_BLK1) || row_block_size!=col_block_size || col_block_size>3) { 62 out->pattern=Paso_Pattern_unrollBlocks(pattern,pattern_format_out,col_block_size,row_block_size); 63 out->row_block_size=1; 64 out->col_block_size=1; 65 } else { 66 out->pattern=Paso_Pattern_unrollBlocks(pattern,pattern_format_out,1,1); 67 out->row_block_size=row_block_size; 68 out->col_block_size=col_block_size; 69 } 70 out->numRows = out->pattern->numInput; 71 out->numCols = out->pattern->numOutput; 72 } 73 } else { 74 /* ====== compressed sparse row === */ 75 if (type & MATRIX_FORMAT_SYM) { 76 Paso_setError(TYPE_ERROR,"Generation of matrix pattern for symmetric CSR is not implemented yet."); 77 return NULL; 78 } else { 79 if ((type & MATRIX_FORMAT_BLK1) || row_block_size!=col_block_size || col_block_size>3) { 80 out->pattern=Paso_Pattern_unrollBlocks(pattern,pattern_format_out,row_block_size,col_block_size); 81 out->row_block_size=1; 82 out->col_block_size=1; 83 } else { 84 out->pattern=Paso_Pattern_unrollBlocks(pattern,pattern_format_out,1,1); 85 out->row_block_size=row_block_size; 86 out->col_block_size=col_block_size; 87 } 88 out->numRows = out->pattern->numOutput; 89 out->numCols = out->pattern->numInput; 90 } 91 } 92 out->block_size=out->row_block_size*out->col_block_size; 93 out->len=(size_t)(out->pattern->len)*(size_t)(out->block_size); 94 95 out->val=MEMALLOC(out->len,double); 96 if (! Paso_checkPtr(out->val)) Paso_SparseMatrix_setValues(out,DBLE(0)); 97 } 98 /* all done: */ 99 if (! Paso_noError()) { 100 Paso_SparseMatrix_free(out); 101 return NULL; 102 } else { 103 #ifdef Paso_TRACE 104 printf("timing: system matrix %.4e sec\n",Paso_timer()-time0); 105 printf("Paso_SparseMatrix_alloc: %ld x %ld system matrix has been allocated.\n",(long)out->numRows,(long)out->numCols); 106 #endif 107 return out; 108 } 109 } 110 111 /* returns a reference to Paso_SparseMatrix in */ 112 113 Paso_SparseMatrix* Paso_SparseMatrix_getReference(Paso_SparseMatrix* in) { 114 if (in!=NULL) ++(in->reference_counter); 115 return in; 116 } 117 118 /* deallocates a SparseMatrix: */ 119 120 void Paso_SparseMatrix_free(Paso_SparseMatrix* in) { 121 if (in!=NULL) { 122 in->reference_counter--; 123 if (in->reference_counter<=0) { 124 Paso_Pattern_free(in->pattern); 125 MEMFREE(in->val); 126 MEMFREE(in); 127 #ifdef Paso_TRACE 128 printf("Paso_SparseMatrix_free: system matrix as been deallocated.\n"); 129 #endif 130 } 131 } 132 }