Debian Codegraph @

packages/aztecoo/example/azoo_iterate_hb/create_vbr.c

raw
1 /*
2 //@HEADER
3 // ***********************************************************************
4 //
5 // AztecOO: An Object-Oriented Aztec Linear Solver Package
6 // Copyright (2002) Sandia Corporation
7 //
8 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
9 // license for use of this work by or on behalf of the U.S. Government.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Michael A. Heroux ([email protected])
39 //
40 // ***********************************************************************
41 //@HEADER
42 */
43
44 #include <stdlib.h>
45 #include <stdio.h>
46 #include "az_aztec.h"
47
48 void create_vbr(char *partition_file, int *proc_config,
49 int *N_global, int *N_blk_global,
50 int *n_nonzeros, int *n_blk_nonzeros,
51 int *N_update, int **update,
52 int *bindx_msr, double *val_msr,
53 double **val, int **indx, int **rpntr, int **cpntr,
54 int **bpntr, int **bindx)
55 #undef DEBUG
56 /* read ASCII data file:
57 line 1: N_global, number of entries (%d,%d)
58 line 2-...: i,j,real (%d, %d, %f)
59 */
60
61 {
62 FILE *data ;
63
64
65 int i, n_entries, N_columns;
66 int ii, jj ;
67 int kk = 0;
68 int max_ii = 0, max_jj = 0;
69 int ione = 1;
70 double value;
71 double *cnt;
72 int *pntr, *indx1, *pntr1;
73 double *val1;
74 int blocksize, N_blk_equations, N_block_entries, n_vbr_nonzeros, n_msr_nonzeros;
75 int total_msr_storage, total_vbr_storage;
76 int variable_block_size, blk_type;
77 int cur_blk_ptr=0, prev_blk_ptr;
78
79 if(proc_config[AZ_node] == 0)
80 {
81
82 /* Do case where command line argument is an integer.
83 Interpret integer as the constant block size */
84 printf("***************************************************************\n");
85 if (partition_file[0] >='0' && partition_file[0] <='9')
86 {
87 blocksize = atoi(partition_file);
88 printf("Using block size of %d to convert from MSR to VBR\n",blocksize);
89 N_blk_equations = *N_global/blocksize;
90
91 /* Allocate memory for cpntr */
92 *cpntr = (int *) calloc(N_blk_equations+2,sizeof(int)) ;
93
94 /* Define block sizes for all but last equation */
95 for (i=0; i<N_blk_equations; i++) (*cpntr)[i] = blocksize;
96
97 /* Check if number of equations is multiple of blocksize */
98 variable_block_size = *N_global%blocksize;
99 blk_type = blocksize;
100
101 if (variable_block_size)
102 {
103 N_blk_equations ++;
104 (*cpntr)[N_blk_equations-1] = variable_block_size;
105 blk_type = -blocksize;
106 }
107 }
108 else
109 {
110 /* Otherwise command line arg is a file name containing partition
111 information.
112 The first line of the file must be the integer value zero.
113 The last line of the file must equal the number of global equations,
114 i.e., N_global.
115 Lines in between are incremented by the number of equations per
116 block row.
117 */
118 /* This should be a short file, so read once to get number of block
119 equations, then read again to fill values */
120 printf("Using partition from %s to convert from MSR to VBR\n",
121 partition_file);
122 data = fopen(partition_file,"r") ;
123 N_blk_equations = 0;
124 while(cur_blk_ptr !=*N_global)
125 {
126 fscanf(data, "%d", &cur_blk_ptr);
127 N_blk_equations++;
128 }
129 close(data);
130
131 /* Allocate memory for cpntr */
132 *cpntr = (int *) calloc(N_blk_equations+1,sizeof(int)) ;
133
134 N_blk_equations = 0;
135 data = fopen(partition_file,"r") ;
136 fscanf(data, "%d", &prev_blk_ptr);
137 cur_blk_ptr = 0;
138 while(cur_blk_ptr !=*N_global)
139 {
140 fscanf(data, "%d", &cur_blk_ptr);
141 (*cpntr)[N_blk_equations] = cur_blk_ptr - prev_blk_ptr;
142 prev_blk_ptr = cur_blk_ptr;
143 N_blk_equations++;
144 }
145 close(data);
146 blk_type = -1; /* assume variable block for now */
147
148 }
149
150 /* Estimate storage needed for VBR and allocate space */
151
152 N_block_entries = *n_nonzeros;
153 n_vbr_nonzeros = AZ_MIN(abs(*n_nonzeros * blocksize * blocksize),
154 420000000/8);
155 *N_blk_global = N_blk_equations;
156
157 printf("\nEstimated Storage parameters for VBR:\n");
158 printf(" Number of block equations = %d\n",N_blk_equations);
159 printf(" Number of block entries = %d\n",N_block_entries);
160 printf(" Number of scalar entries = %d\n",n_vbr_nonzeros);
161
162
163 *bpntr = (int *) calloc(N_blk_equations+1,sizeof(int)) ;
164 *rpntr = (int *) calloc(N_blk_equations+1,sizeof(int)) ;
165 *bindx = (int *) calloc(N_block_entries+1,sizeof(int)) ;
166 *indx = (int *) calloc(N_block_entries+1,sizeof(int)) ;
167 *val = (double *) calloc(n_vbr_nonzeros+1, sizeof(double)) ;
168
169
170 while (n_vbr_nonzeros >= *n_nonzeros && (*val) == NULL)
171 {
172 printf("Error: Unable to allocate %d bytes to create VBR matrix.\n",
173 n_vbr_nonzeros*sizeof(double));
174 printf(" Trying to allocate %d bytes.\n",
175 n_vbr_nonzeros*sizeof(double)/2);
176 n_vbr_nonzeros /= 2;
177 *val = (double *) calloc(n_vbr_nonzeros+1, sizeof(double)) ;
178 }
179
180 AZ_msr2vbr(*val, *indx, *rpntr, *cpntr, *bpntr, *bindx, bindx_msr,val_msr,
181 N_blk_equations, N_blk_equations, N_block_entries,
182 n_vbr_nonzeros, blk_type);
183
184 n_msr_nonzeros = *n_nonzeros;
185
186 *n_nonzeros = (*indx)[(*bpntr)[*N_blk_global]];
187 *n_blk_nonzeros = (*bpntr)[*N_blk_global];
188
189 *bindx = (int *) realloc((void *) (*bindx),
190 (*n_blk_nonzeros+1)*sizeof(int)) ;
191 *indx = (int *) realloc((void *) (*indx),
192 (*n_blk_nonzeros+1)*sizeof(int)) ;
193 *val = (double *) realloc((void *) (*val),
194 (*n_nonzeros+1)*sizeof(double)) ;
195 printf("\nActual Storage parameters for VBR:\n");
196 printf(" Number of block equations = %d\n",N_blk_equations);
197 printf(" Number of block entries = %d\n",*n_blk_nonzeros);
198 printf(" Number of scalar entries = %d\n",*n_nonzeros);
199
200 total_msr_storage = 4* (n_msr_nonzeros+1) + 8*(n_msr_nonzeros+1);
201 total_vbr_storage = 4*3*(N_blk_equations+1) + 4*2*(*n_blk_nonzeros+1) +
202 8*(*n_nonzeros);
203 printf("\nTotal MSR storage (bytes) = %d\n",total_msr_storage);
204 printf( "Total VBR storage (bytes) = %d\n",total_vbr_storage);
205 printf( "Ratio of VBR to MSR storage = %5.2f\n",
206 (float)total_vbr_storage/(float)total_msr_storage);
207
208
209 printf("***************************************************************\n");
210 }
211 /* end create_vbr */
212 }