source: CIVL/examples/amg/csr/csr_add.c@ 1aaefd4

main test-branch
Last change on this file since 1aaefd4 was ea777aa, checked in by Alex Wilton <awilton@…>, 3 years ago

Moved examples, include, build_default.properties, common.xml, and README out from dev.civl.com into the root of the repo.

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@5704 fb995dde-84ed-4084-dfe6-e5aef3e2452c

  • Property mode set to 100644
File size: 6.4 KB
Line 
1
2#include <stdlib.h>
3/*
4Required source files:
5f1 : AMG2013/test/steve_test.c
6f2 : AMG2013/seq_mv/seq_mv.h
7f9 : /include/abc/stdlib.h
8f10 : /include/abc/stdio.h
9f11 : /include/abc/civl-stdio.cvh
10f97 : AMG2013/seq_mv/csr_matop.c
11f98 : AMG2013/seq_mv/csr_matrix.c
12f158 : AMG2013/utilities/hypre_error.c
13f159 : AMG2013/utilities/hypre_memory.c
14
15Entities used from f1: AMG2013/test/steve_test.c :
16 main
17
18Entities used from f2: AMG2013/seq_mv/seq_mv.h :
19 hypre_CSRMatrix
20
21Entities used from f9: /include/abc/stdlib.h :
22 size_t
23 free
24 calloc
25
26Entities used from f10: /include/abc/stdio.h :
27 fflush
28 stdout
29 printf
30
31Entities used from f11: /include/abc/civl-stdio.cvh :
32 FILE
33
34Entities used from f97: AMG2013/seq_mv/csr_matop.c :
35 hypre_CSRMatrixAdd
36
37Entities used from f98: AMG2013/seq_mv/csr_matrix.c :
38 hypre_CSRMatrixCreate
39 hypre_CSRMatrixInitialize
40
41Entities used from f158: AMG2013/utilities/hypre_error.c :
42 hypre_error_handler
43 hypre__global_error
44
45Entities used from f159: AMG2013/utilities/hypre_memory.c :
46 hypre_Free
47 hypre_CAlloc
48 hypre_OutOfMemory
49
50 */
51
52// Preliminaries ....
53
54// from hyper_memory.c.
55// Somewhat simplified by sfs to remove call to hyper_error.
56char * hypre_CAlloc( int count, int elt_size ) {
57 char *ptr;
58 int size = count*elt_size;
59
60 if (size > 0) {
61 ptr = calloc(count, elt_size);
62 } else {
63 ptr = NULL;
64 }
65 return ptr;
66}
67
68// from hypre_memory.h:
69#define hypre_CTAlloc(type, count) \
70( (type *)hypre_CAlloc((unsigned int)(count), (unsigned int)sizeof(type)) )
71
72// from hyper_memory.c:
73void
74hypre_Free( char *ptr )
75{
76 if (ptr)
77 {
78#ifdef HYPRE_USE_UMALLOC
79 int threadid = hypre_GetThreadID();
80
81 _ufree_(ptr);
82#else
83 free(ptr);
84#endif
85 }
86}
87
88// from hyper_memory.h:
89#define hypre_TFree(ptr) \
90( hypre_Free((char *)ptr), ptr = NULL )
91
92
93
94// from csr_matrix.h:
95typedef struct {
96 double *data;
97 int *i;
98 int *j;
99 int num_rows;
100 int num_cols;
101 int num_nonzeros;
102
103 /* for compressing rows in matrix multiplication */
104 int *rownnz;
105 int num_rownnz;
106
107 /* Does the CSRMatrix create/destroy `data', `i', `j'? */
108 int owns_data;
109} hypre_CSRMatrix;
110
111/*--------------------------------------------------------------------------
112 * Accessor functions for the CSR Matrix structure
113 *--------------------------------------------------------------------------*/
114
115#define hypre_CSRMatrixData(matrix) ((matrix) -> data)
116#define hypre_CSRMatrixI(matrix) ((matrix) -> i)
117#define hypre_CSRMatrixJ(matrix) ((matrix) -> j)
118#define hypre_CSRMatrixNumRows(matrix) ((matrix) -> num_rows)
119#define hypre_CSRMatrixNumCols(matrix) ((matrix) -> num_cols)
120#define hypre_CSRMatrixNumNonzeros(matrix) ((matrix) -> num_nonzeros)
121#define hypre_CSRMatrixRownnz(matrix) ((matrix) -> rownnz)
122#define hypre_CSRMatrixNumRownnz(matrix) ((matrix) -> num_rownnz)
123#define hypre_CSRMatrixOwnsData(matrix) ((matrix) -> owns_data)
124
125
126// from csr_matop.c:
127
128hypre_CSRMatrix *
129hypre_CSRMatrixCreate( int num_rows,
130 int num_cols,
131 int num_nonzeros )
132{
133 hypre_CSRMatrix *matrix;
134
135 matrix = hypre_CTAlloc(hypre_CSRMatrix, 1);
136
137 hypre_CSRMatrixData(matrix) = NULL;
138 hypre_CSRMatrixI(matrix) = NULL;
139 hypre_CSRMatrixJ(matrix) = NULL;
140 hypre_CSRMatrixRownnz(matrix) = NULL;
141 hypre_CSRMatrixNumRows(matrix) = num_rows;
142 hypre_CSRMatrixNumCols(matrix) = num_cols;
143 hypre_CSRMatrixNumNonzeros(matrix) = num_nonzeros;
144
145 /* set defaults */
146 hypre_CSRMatrixOwnsData(matrix) = 1;
147 hypre_CSRMatrixNumRownnz(matrix) = num_rows;
148
149
150 return matrix;
151}
152
153
154// from csr_matop.c:
155
156int
157hypre_CSRMatrixInitialize( hypre_CSRMatrix *matrix )
158{
159 int num_rows = hypre_CSRMatrixNumRows(matrix);
160 int num_nonzeros = hypre_CSRMatrixNumNonzeros(matrix);
161 int ierr=0;
162
163
164 if ( ! hypre_CSRMatrixData(matrix) && num_nonzeros )
165 hypre_CSRMatrixData(matrix) = hypre_CTAlloc(double, num_nonzeros);
166 if ( ! hypre_CSRMatrixI(matrix) )
167 hypre_CSRMatrixI(matrix) = hypre_CTAlloc(int, num_rows + 1);
168 if ( ! hypre_CSRMatrixJ(matrix) && num_nonzeros )
169 hypre_CSRMatrixJ(matrix) = hypre_CTAlloc(int, num_nonzeros);
170
171 return ierr;
172}
173
174
175// from csr_matop.c:
176
177hypre_CSRMatrix *
178hypre_CSRMatrixAdd( hypre_CSRMatrix *A, hypre_CSRMatrix *B) {
179 double *A_data = hypre_CSRMatrixData(A);
180 int *A_i = hypre_CSRMatrixI(A);
181 int *A_j = hypre_CSRMatrixJ(A);
182 int nrows_A = hypre_CSRMatrixNumRows(A);
183 int ncols_A = hypre_CSRMatrixNumCols(A);
184 double *B_data = hypre_CSRMatrixData(B);
185 int *B_i = hypre_CSRMatrixI(B);
186 int *B_j = hypre_CSRMatrixJ(B);
187 int nrows_B = hypre_CSRMatrixNumRows(B);
188 int ncols_B = hypre_CSRMatrixNumCols(B);
189 hypre_CSRMatrix *C;
190 double *C_data;
191 int *C_i;
192 int *C_j;
193 int ia, ib, ic, jcol, num_nonzeros;
194 int pos;
195 int *marker;
196
197 marker = hypre_CTAlloc(int, ncols_A);
198 C_i = hypre_CTAlloc(int, nrows_A+1);
199 for (ia = 0; ia < ncols_A; ia++)
200 marker[ia] = -1;
201 num_nonzeros = 0;
202 C_i[0] = 0;
203 for (ic = 0; ic < nrows_A; ic++) {
204 for (ia = A_i[ic]; ia < A_i[ic+1]; ia++) {
205 jcol = A_j[ia];
206 marker[jcol] = ic;
207 num_nonzeros++;
208 }
209 for (ib = B_i[ic]; ib < B_i[ic+1]; ib++) {
210 jcol = B_j[ib];
211 if (marker[jcol] != ic) {
212 marker[jcol] = ic;
213 num_nonzeros++;
214 }
215 }
216 C_i[ic+1] = num_nonzeros;
217 }
218
219 C = hypre_CSRMatrixCreate(nrows_A, ncols_A, num_nonzeros);
220 hypre_CSRMatrixI(C) = C_i;
221 hypre_CSRMatrixInitialize(C);
222 C_j = hypre_CSRMatrixJ(C);
223 C_data = hypre_CSRMatrixData(C);
224
225 for (ia = 0; ia < ncols_A; ia++)
226 marker[ia] = -1;
227 pos = 0;
228
229 /*
230#pragma TASS joint invariant ic==spec.i;
231#pragma TASS joint invariant \
232 forall {int i | 0<=i<ic} \
233 forall {int j | 0<=j<ncols_A} \
234 spec.C[i][j] = (exists k.(C_i[i]<=k && k<C_i[i+1] && j=C_j[k])) ? C_data[k] : 0.;
235 */
236
237 for (ic = 0; ic < nrows_A; ic++) {
238 for (ia = A_i[ic]; ia < A_i[ic+1]; ia++) {
239 jcol = A_j[ia];
240 C_j[pos] = jcol;
241 C_data[pos] = A_data[ia];
242 marker[jcol] = pos;
243 pos++;
244 }
245 for (ib = B_i[ic]; ib < B_i[ic+1]; ib++) {
246 jcol = B_j[ib];
247 if (marker[jcol] < C_i[ic]) {
248 C_j[pos] = jcol;
249 C_data[pos] = B_data[ib];
250 marker[jcol] = pos;
251 pos++;
252 }
253 else {
254 C_data[marker[jcol]] += B_data[ib];
255 }
256 }
257 }
258 hypre_TFree(marker);
259 return C;
260}
Note: See TracBrowser for help on using the repository browser.