Ifpack Package Browser (Single Doxygen Collection)  Development
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Vec_dh.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 "Vec_dh.h"
45 #include "Mem_dh.h"
46 #include "SubdomainGraph_dh.h"
47 #include "io_dh.h"
48 
49 #undef __FUNC__
50 #define __FUNC__ "Vec_dhCreate"
51 void
53 {
55  struct _vec_dh *tmp =
56  (struct _vec_dh *) MALLOC_DH (sizeof (struct _vec_dh));
58  *v = tmp;
59  tmp->n = 0;
60  tmp->vals = NULL;
62 
63 #undef __FUNC__
64 #define __FUNC__ "Vec_dhDestroy"
65 void
67 {
68  START_FUNC_DH if (v->vals != NULL)
69  FREE_DH (v->vals);
71  FREE_DH (v);
74 
75 
76 #undef __FUNC__
77 #define __FUNC__ "Vec_dhInit"
78 void
79 Vec_dhInit (Vec_dh v, int size)
80 {
81  START_FUNC_DH v->n = size;
82  v->vals = (double *) MALLOC_DH (size * sizeof (double));
85 
86 #undef __FUNC__
87 #define __FUNC__ "Vec_dhCopy"
88 void
90 {
91  START_FUNC_DH if (x->vals == NULL)
92  SET_V_ERROR ("x->vals is NULL");
93  if (y->vals == NULL)
94  SET_V_ERROR ("y->vals is NULL");
95  if (x->n != y->n)
96  SET_V_ERROR ("x and y are different lengths");
97  memcpy (y->vals, x->vals, x->n * sizeof (double));
99 
100 
101 #undef __FUNC__
102 #define __FUNC__ "Vec_dhDuplicate"
103 void
105 {
106  START_FUNC_DH Vec_dh tmp;
107  int size = v->n;
108  if (v->vals == NULL)
109  SET_V_ERROR ("v->vals is NULL");
110  Vec_dhCreate (out);
112  tmp = *out;
113  tmp->n = size;
114  tmp->vals = (double *) MALLOC_DH (size * sizeof (double));
117 
118 #undef __FUNC__
119 #define __FUNC__ "Vec_dhSet"
120 void
121 Vec_dhSet (Vec_dh v, double value)
122 {
123  START_FUNC_DH int i, m = v->n;
124  double *vals = v->vals;
125  if (v->vals == NULL)
126  SET_V_ERROR ("v->vals is NULL");
127  for (i = 0; i < m; ++i)
128  vals[i] = value;
130 
131 #undef __FUNC__
132 #define __FUNC__ "Vec_dhSetRand"
133 void
135 {
136  START_FUNC_DH int i, m = v->n;
137  double max = 0.0;
138  double *vals = v->vals;
139 
140  if (v->vals == NULL)
141  SET_V_ERROR ("v->vals is NULL");
142 
143 #ifdef WIN32
144  for (i = 0; i < m; ++i)
145  vals[i] = rand ();
146 #else
147  for (i = 0; i < m; ++i)
148  vals[i] = rand ();
149 #endif
150 
151  /* find largest value in vector, and scale vector,
152  * so all values are in [0.0,1.0]
153  */
154  for (i = 0; i < m; ++i)
155  max = MAX (max, vals[i]);
156  for (i = 0; i < m; ++i)
157  vals[i] = vals[i] / max;
159 
160 
161 #undef __FUNC__
162 #define __FUNC__ "Vec_dhPrint"
163 void
164 Vec_dhPrint (Vec_dh v, SubdomainGraph_dh sg, char *filename)
165 {
166  START_FUNC_DH double *vals = v->vals;
167  int pe, i, m = v->n;
168  FILE *fp;
169 
170  if (v->vals == NULL)
171  SET_V_ERROR ("v->vals is NULL");
172 
173  /*--------------------------------------------------------
174  * case 1: no permutation information
175  *--------------------------------------------------------*/
176  if (sg == NULL)
177  {
178  for (pe = 0; pe < np_dh; ++pe)
179  {
180  MPI_Barrier (comm_dh);
181  if (pe == myid_dh)
182  {
183  if (pe == 0)
184  {
185  fp = openFile_dh (filename, "w");
187  }
188  else
189  {
190  fp = openFile_dh (filename, "a");
192  }
193 
194  for (i = 0; i < m; ++i)
195  fprintf (fp, "%g\n", vals[i]);
196 
197  closeFile_dh (fp);
199  }
200  }
201  }
202 
203  /*--------------------------------------------------------
204  * case 2: single mpi task, multiple subdomains
205  *--------------------------------------------------------*/
206  else if (np_dh == 1)
207  {
208  int i, j;
209 
210  fp = openFile_dh (filename, "w");
212 
213  for (i = 0; i < sg->blocks; ++i)
214  {
215  int oldBlock = sg->n2o_sub[i];
216  int beg_row = sg->beg_rowP[oldBlock];
217  int end_row = beg_row + sg->row_count[oldBlock];
218 
219  printf ("seq: block= %i beg= %i end= %i\n", oldBlock, beg_row,
220  end_row);
221 
222 
223  for (j = beg_row; j < end_row; ++j)
224  {
225  fprintf (fp, "%g\n", vals[j]);
226  }
227  }
228  }
229 
230  /*--------------------------------------------------------
231  * case 3: multiple mpi tasks, one subdomain per task
232  *--------------------------------------------------------*/
233  else
234  {
235  int id = sg->o2n_sub[myid_dh];
236  for (pe = 0; pe < np_dh; ++pe)
237  {
238  MPI_Barrier (comm_dh);
239  if (id == pe)
240  {
241  if (pe == 0)
242  {
243  fp = openFile_dh (filename, "w");
245  }
246  else
247  {
248  fp = openFile_dh (filename, "a");
250  }
251 
252  fprintf (stderr, "par: block= %i\n", id);
253 
254  for (i = 0; i < m; ++i)
255  {
256  fprintf (fp, "%g\n", vals[i]);
257  }
258 
259  closeFile_dh (fp);
261  }
262  }
263  }
265 
266 
267 #undef __FUNC__
268 #define __FUNC__ "Vec_dhPrintBIN"
269 void
270 Vec_dhPrintBIN (Vec_dh v, SubdomainGraph_dh sg, char *filename)
271 {
272  START_FUNC_DH if (np_dh > 1)
273  {
274  SET_V_ERROR ("only implemented for a single MPI task");
275  }
276  if (sg != NULL)
277  {
278  SET_V_ERROR ("not implemented for reordered vector; ensure sg=NULL");
279  }
280 
281  io_dh_print_ebin_vec_private (v->n, 0, v->vals, NULL, NULL, NULL, filename);
284 
285 #define MAX_JUNK 200
286 
287 #undef __FUNC__
288 #define __FUNC__ "Vec_dhRead"
289 void
290 Vec_dhRead (Vec_dh * vout, int ignore, char *filename)
291 {
292  START_FUNC_DH Vec_dh tmp;
293  FILE *fp;
294  int items, n, i;
295  double *v, w;
296  char junk[MAX_JUNK];
297 
298  Vec_dhCreate (&tmp);
300  *vout = tmp;
301 
302  if (np_dh > 1)
303  {
304  SET_V_ERROR ("only implemented for a single MPI task");
305  }
306 
307  fp = openFile_dh (filename, "w");
309 
310  /* skip over file lines */
311  if (ignore)
312  {
313  printf ("Vec_dhRead:: ignoring following header lines:\n");
314  printf
315  ("--------------------------------------------------------------\n");
316  for (i = 0; i < ignore; ++i)
317  {
318  fgets (junk, MAX_JUNK, fp);
319  printf ("%s", junk);
320  }
321  printf
322  ("--------------------------------------------------------------\n");
323  }
324 
325  /* count floating point entries in file */
326  n = 0;
327  while (!feof (fp))
328  {
329  items = fscanf (fp, "%lg", &w);
330  if (items != 1)
331  {
332  break;
333  }
334  ++n;
335  }
336 
337  printf ("Vec_dhRead:: n= %i\n", n);
338 
339  /* allocate storage */
340  tmp->n = n;
341  v = tmp->vals = (double *) MALLOC_DH (n * sizeof (double));
343 
344  /* reset file, and skip over header again */
345  rewind (fp);
346  rewind (fp);
347  for (i = 0; i < ignore; ++i)
348  {
349  fgets (junk, MAX_JUNK, fp);
350  }
351 
352  /* read values */
353  for (i = 0; i < n; ++i)
354  {
355  items = fscanf (fp, "%lg", v + i);
356  if (items != 1)
357  {
358  sprintf (msgBuf_dh, "failed to read value %i of %i", i + 1, n);
359  }
360  }
361 
362  closeFile_dh (fp);
365 
366 #undef __FUNC__
367 #define __FUNC__ "Vec_dhReadBIN"
368 extern void
369 Vec_dhReadBIN (Vec_dh * vout, char *filename)
370 {
371  START_FUNC_DH Vec_dh tmp;
372 
373  Vec_dhCreate (&tmp);
375  *vout = tmp;
376  io_dh_read_ebin_vec_private (&tmp->n, &tmp->vals, filename);
void Vec_dhSetRand(Vec_dh v)
Definition: Vec_dh.c:134
#define CHECK_V_ERROR
Definition: macros_dh.h:138
void Vec_dhCopy(Vec_dh x, Vec_dh y)
Definition: Vec_dh.c:89
#define MAX_JUNK
Definition: Vec_dh.c:285
void Vec_dhDestroy(Vec_dh v)
Definition: Vec_dh.c:66
void Vec_dhSet(Vec_dh v, double value)
Definition: Vec_dh.c:121
void Vec_dhDuplicate(Vec_dh v, Vec_dh *out)
Definition: Vec_dh.c:104
#define END_FUNC_DH
Definition: macros_dh.h:187
#define MALLOC_DH(s)
void io_dh_read_ebin_vec_private(int *n, double **vals, char *filename)
Definition: io_dh.c:100
int n
Definition: Vec_dh.h:54
void Vec_dhInit(Vec_dh v, int size)
Definition: Vec_dh.c:79
#define MAX(a, b)
Definition: macros_dh.h:51
int np_dh
Definition: globalObjects.c:62
double * vals
Definition: Vec_dh.h:55
void Vec_dhPrint(Vec_dh v, SubdomainGraph_dh sg, char *filename)
Definition: Vec_dh.c:164
void Vec_dhReadBIN(Vec_dh *vout, char *filename)
Definition: Vec_dh.c:369
void Vec_dhRead(Vec_dh *vout, int ignore, char *filename)
Definition: Vec_dh.c:290
void io_dh_print_ebin_vec_private(int n, int beg_row, double *vals, int *n2o, int *o2n, Hash_i_dh hash, char *filename)
Definition: io_dh.c:93
MPI_Comm comm_dh
Definition: globalObjects.c:64
#define SET_V_ERROR(msg)
Definition: macros_dh.h:126
#define START_FUNC_DH
Definition: macros_dh.h:181
Definition: Vec_dh.h:52
#define max(x, y)
Definition: scscres.c:46
void closeFile_dh(FILE *fpIN)
Definition: io_dh.c:69
void Vec_dhCreate(Vec_dh *v)
Definition: Vec_dh.c:52
int myid_dh
Definition: globalObjects.c:63
FILE * openFile_dh(const char *filenameIN, const char *modeIN)
Definition: io_dh.c:54
char msgBuf_dh[MSG_BUF_SIZE_DH]
Definition: globalObjects.c:61
#define FREE_DH(p)
void Vec_dhPrintBIN(Vec_dh v, SubdomainGraph_dh sg, char *filename)
Definition: Vec_dh.c:270