1 /*  init.c  */
2 
3 #include "../InpMtx.h"
4 
5 /*--------------------------------------------------------------------*/
6 /*
7    ------------------------------------------------------------------
8    initialize the object
9 
10    coordType -- coordinate type, input supported for types 1, 2 and 3
11       1 -- row triples (i, j, a_{i,j})
12       2 -- column triples (i, j, a_{j,i})
13       3 -- chevron triples
14            (i, k, a_{i,i+k}) if k >= 0
15            (i, k, a_{i-k,i}) if k < 0
16            i is the chevron, k is the offset
17       4 -- custom coordinate, e.g., one could store (I, k, a_{i,j})
18            where I is the front where a_{i,j} will be assembled
19            and k is the offset into the vector that holds the
20            entries in the front
21    inputMode -- mode for input
22       0 --> indices only
23       1 --> indices and real entries
24       2 --> indices and complex entries
25    maxnent  -- upper bound on the number of entries,
26       also equal to the workspace, so if the assembly includes
27       overlapping data, give enough elbow room for efficiency.
28    maxnvector -- upper bound on the number of vectors to be supported.
29       this may not be known ahead of time (e.g., the number of vectors
30       may be the number of fronts which is not known before the
31       ordering is done and front tree constructed).
32 
33    created -- 98jan28, cca
34    ------------------------------------------------------------------
35 */
36 void
InpMtx_init(InpMtx * inpmtx,int coordType,int inputMode,int maxnent,int maxnvector)37 InpMtx_init (
38   InpMtx   *inpmtx,
39   int      coordType,
40   int      inputMode,
41   int      maxnent,
42   int      maxnvector
43 ) {
44 /*
45    ---------------
46    check the input
47    ---------------
48 */
49 if (  inpmtx == NULL ) {
50    fprintf(stderr, "\n fatal error in InpMtx_init(%p,%d,%d,%d,%d)"
51            "\n inpmtx is NULL \n",
52            inpmtx, coordType, inputMode, maxnent, maxnvector) ;
53    exit(-1) ;
54 }
55 if ( ! ( INPMTX_IS_BY_ROWS(inpmtx)
56       || INPMTX_IS_BY_COLUMNS(inpmtx)
57       || INPMTX_IS_BY_CHEVRONS(inpmtx) ) ) {
58    fprintf(stderr, "\n fatal error in InpMtx_init(%p,%d,%d,%d,%d)"
59            "\n bad coordType \n",
60            inpmtx, coordType, inputMode, maxnent, maxnvector) ;
61    exit(-1) ;
62 }
63 if ( ! (INPMTX_IS_INDICES_ONLY(inpmtx)
64    ||   INPMTX_IS_REAL_ENTRIES(inpmtx)
65    ||   INPMTX_IS_COMPLEX_ENTRIES(inpmtx) ) ) {
66    fprintf(stderr, "\n fatal error in InpMtx_init(%p,%d,%d,%d,%d)"
67            "\n bad inputMode \n",
68            inpmtx, coordType, inputMode, maxnent, maxnvector) ;
69    exit(-1) ;
70 }
71 if ( maxnent < 0 || maxnvector < 0 ) {
72    fprintf(stderr, "\n fatal error in InpMtx_init(%p,%d,%d,%d,%d)"
73            "\n maxnent = %d, maxnvector = %d \n",
74            inpmtx, coordType, inputMode, maxnent, maxnvector,
75            maxnent, maxnvector) ;
76    exit(-1) ;
77 }
78 /*
79    ------------------------
80    clear the data structure
81    ------------------------
82 */
83 InpMtx_clearData(inpmtx) ;
84 /*
85    -------------------------------------------------------------------
86    if maxnent > 0 then allocate the ivec1[], ivec2[] and dvec[] vectors
87    -------------------------------------------------------------------
88 */
89 inpmtx->coordType = coordType ;
90 inpmtx->inputMode = inputMode ;
91 if ( maxnent > 0 ) {
92    InpMtx_setMaxnent(inpmtx, maxnent) ;
93 }
94 /*
95    ---------------------------------------
96    if nvector > 0 initialize the vecids[],
97    sizes[] and offsets[] vectors.
98    ---------------------------------------
99 */
100 if ( maxnvector > 0 ) {
101    InpMtx_setMaxnvector(inpmtx, maxnvector) ;
102 }
103 return ; }
104 
105 /*--------------------------------------------------------------------*/
106 /*
107    --------------------------
108    change the coordinate type
109 
110    created -- 98jan28, cca
111    --------------------------
112 */
113 void
InpMtx_changeCoordType(InpMtx * inpmtx,int newType)114 InpMtx_changeCoordType (
115    InpMtx   *inpmtx,
116    int       newType
117 ) {
118 int   chev, col, i, nent, off, oldType, row, temp ;
119 int   *ivec1, *ivec2 ;
120 /*
121    ---------------
122    check the input
123    ---------------
124 */
125 if ( inpmtx == NULL ) {
126    fprintf(stderr,
127            "\n fatal error in InpMtx_changeCoordType(%p,%d)"
128            "\n bad input\n", inpmtx, newType) ;
129    exit(-1) ;
130 }
131 if (  newType != INPMTX_BY_ROWS && newType != INPMTX_BY_COLUMNS
132    && newType != INPMTX_BY_CHEVRONS && newType != INPMTX_CUSTOM ) {
133    fprintf(stderr,
134            "\n fatal error in InpMtx_changeCoordType(%p,%d)"
135            "\n bad new coordType\n", inpmtx, newType) ;
136    exit(-1) ;
137 }
138 if ( ! ( INPMTX_IS_BY_ROWS(inpmtx)
139       || INPMTX_IS_BY_COLUMNS(inpmtx)
140       || INPMTX_IS_BY_CHEVRONS(inpmtx) ) ) {
141    fprintf(stderr,
142            "\n fatal error in InpMtx_changeCoordType(%p,%d)"
143            "\n bad existing coordType\n", inpmtx, newType) ;
144    exit(-1) ;
145 }
146 oldType = inpmtx->coordType ;
147 if ( oldType == newType ) {
148    return ;
149 }
150 if ( newType == INPMTX_CUSTOM ) {
151 /*
152    ----------------------------------------------------
153    custom type, just set the coordType field and return
154    ----------------------------------------------------
155 */
156    inpmtx->coordType = INPMTX_CUSTOM ;
157    return ;
158 }
159 nent  = inpmtx->nent ;
160 ivec1 = InpMtx_ivec1(inpmtx) ;
161 ivec2 = InpMtx_ivec2(inpmtx) ;
162 if ( oldType == INPMTX_BY_ROWS ) {
163    if ( newType == INPMTX_BY_COLUMNS ) {
164 /*
165       ----------------------------------
166       row coordType --> column coordType
167       ----------------------------------
168 */
169       for ( i = 0 ; i < nent ; i++ ) {
170          temp = ivec1[i] ;
171          ivec1[i] = ivec2[i] ;
172          ivec2[i] = temp ;
173       }
174       inpmtx->coordType   = INPMTX_BY_COLUMNS ;
175       inpmtx->storageMode = INPMTX_RAW_DATA   ;
176    } else if ( newType == INPMTX_BY_CHEVRONS ) {
177 /*
178       -----------------------------------
179       row coordType --> chevron coordType
180       -----------------------------------
181 */
182       for ( i = 0 ; i < nent ; i++ ) {
183          row = ivec1[i] ;
184          col = ivec2[i] ;
185          if ( row <= col ) {
186             ivec1[i] = row ;
187             ivec2[i] = col - row ;
188          } else {
189             ivec1[i] = col ;
190             ivec2[i] = col - row ;
191          }
192       }
193       inpmtx->coordType   = INPMTX_BY_CHEVRONS ;
194       inpmtx->storageMode = INPMTX_RAW_DATA    ;
195    }
196 } else if ( oldType == INPMTX_BY_COLUMNS ) {
197    if ( newType == INPMTX_BY_ROWS ) {
198 /*
199       ----------------------------------
200       column coordType --> row coordType
201       ----------------------------------
202 */
203       for ( i = 0 ; i < nent ; i++ ) {
204          temp = ivec1[i] ;
205          ivec1[i] = ivec2[i] ;
206          ivec2[i] = temp ;
207       }
208       inpmtx->coordType   = INPMTX_BY_ROWS  ;
209       inpmtx->storageMode = INPMTX_RAW_DATA ;
210    } else if ( newType == INPMTX_BY_CHEVRONS ) {
211 /*
212       --------------------------------------
213       column coordType --> chevron coordType
214       --------------------------------------
215 */
216       for ( i = 0 ; i < nent ; i++ ) {
217          col = ivec1[i] ;
218          row = ivec2[i] ;
219          if ( row <= col ) {
220             ivec1[i] = row ;
221             ivec2[i] = col - row ;
222          } else {
223             ivec1[i] = col ;
224             ivec2[i] = col - row ;
225          }
226       }
227       inpmtx->coordType   = INPMTX_BY_CHEVRONS ;
228       inpmtx->storageMode = INPMTX_RAW_DATA    ;
229    }
230 } else if ( oldType == INPMTX_BY_CHEVRONS ) {
231    if ( newType == INPMTX_BY_ROWS ) {
232 /*
233       -----------------------------------
234       chevron coordType --> row coordType
235       -----------------------------------
236 */
237       for ( i = 0 ; i < nent ; i++ ) {
238          chev = ivec1[i] ;
239          off  = ivec2[i] ;
240          if ( off >= 0 ) {
241             row = chev ;
242             col = chev + off ;
243          } else {
244             col = chev ;
245             row = chev - off ;
246          }
247          ivec1[i] = row ;
248          ivec2[i] = col ;
249       }
250       inpmtx->coordType   = INPMTX_BY_ROWS  ;
251       inpmtx->storageMode = INPMTX_RAW_DATA ;
252    } else if ( newType == INPMTX_BY_COLUMNS ) {
253 /*
254       --------------------------------------
255       chevron coordType --> column coordType
256       --------------------------------------
257 */
258       for ( i = 0 ; i < nent ; i++ ) {
259          chev = ivec1[i] ;
260          off  = ivec2[i] ;
261          if ( off >= 0 ) {
262             row = chev ;
263             col = chev + off ;
264          } else {
265             col = chev ;
266             row = chev - off ;
267          }
268          ivec1[i] = col ;
269          ivec2[i] = row ;
270       }
271       inpmtx->coordType   = INPMTX_BY_COLUMNS ;
272       inpmtx->storageMode = INPMTX_RAW_DATA   ;
273    }
274 }
275 return ; }
276 
277 /*--------------------------------------------------------------------*/
278 /*
279    -----------------------
280    change the storage mode
281 
282    created -- 98jan28, cca
283    -----------------------
284 */
285 void
InpMtx_changeStorageMode(InpMtx * inpmtx,int newMode)286 InpMtx_changeStorageMode (
287    InpMtx   *inpmtx,
288    int       newMode
289 ) {
290 int   oldMode ;
291 /*
292    ---------------
293    check the input
294    ---------------
295 */
296 if ( inpmtx == NULL ) {
297    fprintf(stderr, "\n fatal error in InpMtx_changeStorageMode(%p,%d)"
298            "\n inpmtx is NULL\n", inpmtx, newMode) ;
299    exit(-1) ;
300 }
301 if (  newMode != INPMTX_RAW_DATA && newMode != INPMTX_SORTED
302    && newMode != INPMTX_BY_VECTORS ) {
303    fprintf(stderr, "\n fatal error in InpMtx_changeStorageMode(%p,%d)"
304            "\n bad new storage mode\n", inpmtx, newMode) ;
305    exit(-1) ;
306 }
307 oldMode = inpmtx->storageMode ;
308 if ( oldMode == newMode ) {
309    return ;
310 }
311 if ( oldMode == INPMTX_RAW_DATA ) {
312    if ( newMode == INPMTX_SORTED ) {
313 /*
314       -------------------------------------
315       raw triples --> sorted and compressed
316       -------------------------------------
317 */
318       InpMtx_sortAndCompress(inpmtx) ;
319       inpmtx->storageMode = INPMTX_SORTED ;
320    } else if ( newMode == INPMTX_BY_VECTORS ) {
321 /*
322       -----------------------
323       raw triples --> vectors
324       -----------------------
325 */
326       InpMtx_sortAndCompress(inpmtx) ;
327       InpMtx_convertToVectors(inpmtx) ;
328       inpmtx->storageMode = INPMTX_BY_VECTORS ;
329    }
330 } else if ( oldMode == INPMTX_SORTED ) {
331    if ( newMode == INPMTX_RAW_DATA ) {
332 /*
333       --------------------------------------
334       sorted and compressed --> raw triples,
335       simply set the storageMode field
336       --------------------------------------
337 */
338       inpmtx->storageMode = INPMTX_RAW_DATA ;
339    } else if ( newMode == INPMTX_BY_VECTORS ) {
340 /*
341       ---------------------------------
342       sorted and compressed --> vectors
343       ---------------------------------
344 */
345       InpMtx_convertToVectors(inpmtx) ;
346       inpmtx->storageMode = INPMTX_BY_VECTORS ;
347    }
348 } else if ( oldMode == INPMTX_BY_VECTORS ) {
349    if ( newMode == INPMTX_RAW_DATA || newMode == INPMTX_SORTED ) {
350 /*
351       --------------------------------
352       vectors --> triples,
353       simply set the storageMode field
354       --------------------------------
355 */
356       inpmtx->storageMode = newMode ;
357    }
358 }
359 return ; }
360 
361 /*--------------------------------------------------------------------*/
362