1 /*  init.c  */
2 
3 #include "../DenseMtx.h"
4 
5 #define MYDEBUG 1
6 
7 /*--------------------------------------------------------------------*/
8 /*
9    ------------------------------------------------------------
10    return the number of bytes needed for an object of this size
11 
12    created -- 98may02, cca
13    ------------------------------------------------------------
14 */
15 int
DenseMtx_nbytesNeeded(int type,int nrow,int ncol)16 DenseMtx_nbytesNeeded (
17    int   type,
18    int   nrow,
19    int   ncol
20 ) {
21 int   nbytes, ndouble, nint ;
22 /*
23    ---------------
24    check the input
25    ---------------
26 */
27 if (  nrow < 0 || ncol < 0 ) {
28    fprintf(stderr,
29            "\n fatal error in DenseMtx_nbytesNeeded(%d,%d,%d)"
30            "\n bad input\n", type, nrow, ncol) ;
31    exit(-1) ;
32 }
33 nint = 7 + nrow + ncol ;
34 if ( type == SPOOLES_REAL ) {
35    ndouble = nrow*ncol ;
36 } else if ( type == SPOOLES_COMPLEX ) {
37    ndouble = 2*nrow*ncol ;
38 } else {
39    fprintf(stderr,
40            "\n fatal error in DenseMtx_nbytesNeeded(%d,%d,%d)"
41            "\n bad type %d\n", type, nrow, ncol, type) ;
42    exit(-1) ;
43 }
44 if ( sizeof(int) == sizeof(double) ) {
45    nbytes = nint*sizeof(int) + ndouble*sizeof(double) ;
46 } else if ( 2*sizeof(int) == sizeof(double) ) {
47    nbytes = ((nint + 1)/2 + ndouble)*sizeof(double) ;
48 } else {
49    fprintf(stderr, "\n error in DenseMtx_nbytesNeeded(%d,%d)"
50            "\n sizeof(int) = %d, sizeof(double) = %d",
51            nrow, ncol, sizeof(int), sizeof(double)) ;
52    exit(-1) ;
53 }
54 return(nbytes) ; }
55 
56 /*--------------------------------------------------------------------*/
57 /*
58    ----------------------------------------------------------------
59    return the number of bytes in the workspace owned by this object
60 
61    created -- 98may02, cca
62    ----------------------------------------------------------------
63 */
64 int
DenseMtx_nbytesInWorkspace(DenseMtx * mtx)65 DenseMtx_nbytesInWorkspace (
66    DenseMtx   *mtx
67 ) {
68 if ( mtx == NULL ) {
69    fprintf(stderr, "\n fatal error in DenseMtx_nbytesInWorkspace(%p)"
70            "\n bad input\n", mtx) ;
71    exit(-1) ;
72 }
73 return(sizeof(double)*DV_maxsize(&mtx->wrkDV)) ; }
74 
75 /*--------------------------------------------------------------------*/
76 /*
77    -------------------------------------------------------------
78    set the number of bytes in the workspace owned by this object
79 
80    created -- 98may02, cca
81    -------------------------------------------------------------
82 */
83 void
DenseMtx_setNbytesInWorkspace(DenseMtx * mtx,int nbytes)84 DenseMtx_setNbytesInWorkspace (
85    DenseMtx   *mtx,
86    int        nbytes
87 ) {
88 if ( mtx == NULL ) {
89    fprintf(stderr,
90            "\n fatal error in DenseMtx_setNbytesInWorkspace(%p)"
91            "\n bad input\n", mtx) ;
92    exit(-1) ;
93 }
94 DV_setSize(&mtx->wrkDV, nbytes/sizeof(double)) ;
95 
96 return ; }
97 
98 /*--------------------------------------------------------------------*/
99 /*
100    ---------------------------------------
101    purpose -- set the fields of the object
102 
103    created -- 98may02, cca
104    ---------------------------------------
105 */
106 void
DenseMtx_setFields(DenseMtx * mtx,int type,int rowid,int colid,int nrow,int ncol,int inc1,int inc2)107 DenseMtx_setFields (
108    DenseMtx   *mtx,
109    int         type,
110    int         rowid,
111    int         colid,
112    int         nrow,
113    int         ncol,
114    int         inc1,
115    int         inc2
116 ) {
117 double   *dbuffer ;
118 int      *ibuffer ;
119 /*
120    ---------------
121    check the input
122    ---------------
123 */
124 if (  mtx == NULL || nrow < 0 || ncol < 0
125    || !((inc1 == ncol && inc2 == 1) || (inc1 == 1 && inc2 == nrow)) ) {
126    fprintf(stderr,
127            "\n fatal error in DenseMtx_setFields(%p,%d,%d,%d,%d,%d,%d)"
128            "\n bad input\n",
129            mtx, rowid, colid, nrow, ncol, inc1, inc2) ;
130    exit(-1) ;
131 }
132 dbuffer = DV_entries(&mtx->wrkDV) ;
133 ibuffer = (int *) dbuffer ;
134 /*
135    ---------------------
136    set the scalar fields
137    ---------------------
138 */
139 mtx->type  = ibuffer[0] = type  ;
140 mtx->rowid = ibuffer[1] = rowid ;
141 mtx->colid = ibuffer[2] = colid ;
142 mtx->nrow  = ibuffer[3] = nrow  ;
143 mtx->ncol  = ibuffer[4] = ncol  ;
144 mtx->inc1  = ibuffer[5] = inc1  ;
145 mtx->inc2  = ibuffer[6] = inc2  ;
146 /*
147    -------------------
148    set up the pointers
149    -------------------
150 */
151 mtx->rowind = ibuffer + 7 ;
152 mtx->colind = mtx->rowind + nrow ;
153 if ( sizeof(int) == sizeof(double) ) {
154    mtx->entries = dbuffer + 7 + nrow + ncol ;
155 } else if ( 2*sizeof(int) == sizeof(double) ) {
156    mtx->entries = dbuffer + (8 + nrow + ncol)/2 ;
157 }
158 /*
159 fprintf(stdout,
160         "\n rowind - ibuffer = %d"
161         "\n colind - rowind  = %d"
162         "\n entries - dbuffer = %d",
163         mtx->rowind - ibuffer,
164         mtx->colind - mtx->rowind,
165         mtx->entries - dbuffer) ;
166 */
167 
168 return ; }
169 
170 /*--------------------------------------------------------------------*/
171 /*
172    ----------------------------
173    purpose -- basic initializer
174 
175    created -- 98may02, cca
176    ----------------------------
177 */
178 void
DenseMtx_init(DenseMtx * mtx,int type,int rowid,int colid,int nrow,int ncol,int inc1,int inc2)179 DenseMtx_init (
180    DenseMtx   *mtx,
181    int        type,
182    int        rowid,
183    int        colid,
184    int        nrow,
185    int        ncol,
186    int        inc1,
187    int        inc2
188 ) {
189 int   nbytes ;
190 /*
191    ---------------
192    check the input
193    ---------------
194 */
195 if (  mtx == NULL || nrow < 0 || ncol < 0
196    || !((inc1 == ncol && inc2 == 1) || (inc1 == 1 && inc2 == nrow)) ) {
197    fprintf(stderr,
198            "\n fatal error in DenseMtx_init(%p,%d,%d,%d,%d,%d,%d)"
199            "\n bad input\n",
200            mtx, rowid, colid, nrow, ncol, inc1, inc2) ;
201    exit(-1) ;
202 }
203 switch ( type ) {
204 case SPOOLES_REAL :
205 case SPOOLES_COMPLEX :
206    break ;
207 default :
208    fprintf(stderr,
209            "\n fatal error in DenseMtx_init(%p,%d,%d,%d,%d,%d,%d,%d)"
210            "\n bad type %d\n",
211            mtx, type, rowid, colid, nrow, ncol, inc1, inc2, type) ;
212    exit(-1) ;
213    break ;
214 }
215 /*
216    -------------------------------------------------------
217    get and set the number of bytes needed in the workspace
218    -------------------------------------------------------
219 */
220 nbytes = DenseMtx_nbytesNeeded(type, nrow, ncol) ;
221 DenseMtx_setNbytesInWorkspace(mtx, nbytes) ;
222 /*
223    --------------
224    set the fields
225    --------------
226 */
227 DenseMtx_setFields(mtx, type, rowid, colid, nrow, ncol, inc1, inc2) ;
228 if ( nrow > 0 ) {
229    IVramp(nrow, mtx->rowind, 0, 1) ;
230 }
231 if ( ncol > 0 ) {
232    IVramp(ncol, mtx->colind, 0, 1) ;
233 }
234 return ; }
235 
236 /*--------------------------------------------------------------------*/
237 /*
238    ---------------------------------------------------------
239    purpose -- initialize the object from its working storage
240               used when the object is a MPI message
241 
242    created -- 98may02, cca
243    ---------------------------------------------------------
244 */
245 void
DenseMtx_initFromBuffer(DenseMtx * mtx)246 DenseMtx_initFromBuffer (
247    DenseMtx   *mtx
248 ) {
249 int   *ibuffer ;
250 /*
251    ---------------
252    check the input
253    ---------------
254 */
255 if (  mtx == NULL ) {
256    fprintf(stderr, "\n fatal error in DenseMtx_initFromBuffer(%p)"
257            "\n bad input\n", mtx) ;
258    exit(-1) ;
259 }
260 ibuffer   = (int *) DV_entries(&mtx->wrkDV) ;
261 DenseMtx_setFields(mtx, ibuffer[0], ibuffer[1], ibuffer[2],
262                    ibuffer[3], ibuffer[4], ibuffer[5], ibuffer[6]) ;
263 
264 return ; }
265 
266 /*--------------------------------------------------------------------*/
267 /*
268    ------------------------------------
269    purpose -- initializer with pointers
270 
271    created -- 98may02, cca
272    ------------------------------------
273 */
274 void
DenseMtx_initWithPointers(DenseMtx * mtx,int type,int rowid,int colid,int nrow,int ncol,int inc1,int inc2,int * rowind,int * colind,double * entries)275 DenseMtx_initWithPointers (
276    DenseMtx   *mtx,
277    int         type,
278    int         rowid,
279    int         colid,
280    int         nrow,
281    int         ncol,
282    int         inc1,
283    int         inc2,
284    int         *rowind,
285    int         *colind,
286    double      *entries
287 ) {
288 /*
289    ---------------
290    check the input
291    ---------------
292 */
293 if (  mtx == NULL || nrow <= 0 || ncol <= 0 || inc1 < 0 || inc2 < 0
294    || (inc1 != 1 && inc2 != 1)
295    || entries == NULL || colind == NULL || rowind == NULL ) {
296    fprintf(stderr,
297            "\n fatal error in DenseMtx_initWithPointers()"
298            "\n mtx = %p, rowid = %d, colid = %d"
299            "\n nrow = %d, ncol = %d, inc1 = %d, inc2 = %d"
300            "\n rowind = %p, colind = %p, entries = %p "
301            "\n bad input\n",
302            mtx, rowid, colid, nrow, ncol, inc1, inc2,
303            rowind, colind, entries) ;
304    exit(-1) ;
305 }
306 switch ( type ) {
307 case SPOOLES_REAL :
308 case SPOOLES_COMPLEX :
309    break ;
310 default :
311    fprintf(stderr, "\n fatal error in DenseMtx_initWithPointers()"
312            "\n bad type %d\n", type) ;
313    break ;
314 }
315 
316 /*
317    ---------------------
318    set the scalar fields
319    ---------------------
320 */
321 mtx->type  = type  ;
322 mtx->rowid = rowid ;
323 mtx->colid = colid ;
324 mtx->nrow  = nrow  ;
325 mtx->ncol  = ncol  ;
326 mtx->inc1  = inc1  ;
327 mtx->inc2  = inc2  ;
328 /*
329    --------------------------
330    set up the working storage
331    --------------------------
332 */
333 mtx->rowind  = rowind  ;
334 mtx->colind  = colind  ;
335 mtx->entries = entries ;
336 
337 return ; }
338 
339 /*--------------------------------------------------------------------*/
340 /*
341    -----------------------------------
342    this method initializes a A2 object
343    to point into the entries
344 
345    created -- 98may02, cca
346    -----------------------------------
347 */
348 void
DenseMtx_setA2(DenseMtx * mtx,A2 * a2)349 DenseMtx_setA2 (
350    DenseMtx   *mtx,
351    A2         *a2
352 ) {
353 /*
354    ---------------
355    check the input
356    ---------------
357 */
358 if (  mtx == NULL || a2 == NULL ) {
359    fprintf(stderr, "\n fatal error in DenseMtx_setZA2(%p,%p)"
360            "\n bad input\n", mtx, a2) ;
361    exit(-1) ;
362 }
363 /*
364 if ( DENSEMTX_IS_REAL(mtx) ) {
365    A2_init(a2, SPOOLES_REAL, mtx->nrow, mtx->ncol, mtx->inc1, mtx->inc2,
366            mtx->entries) ;
367 } else if ( DENSEMTX_IS_COMPLEX(mtx) ) {
368    A2_init(a2, SPOOLES_COMPLEX, mtx->nrow, mtx->ncol, mtx->inc1, mtx->inc2,
369            mtx->entries) ;
370 }
371 */
372 A2_init(a2, mtx->type, mtx->nrow, mtx->ncol,
373         mtx->inc1, mtx->inc2, mtx->entries) ;
374 return ; }
375 
376 /*--------------------------------------------------------------------*/
377