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