IFPACK  Development
 All Classes Files Functions Variables Enumerations Friends
Vec_dh.c
00001 /*@HEADER
00002 // ***********************************************************************
00003 //
00004 //       Ifpack: Object-Oriented Algebraic Preconditioner Package
00005 //                 Copyright (2002) Sandia Corporation
00006 //
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 //
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
00038 //
00039 // ***********************************************************************
00040 //@HEADER
00041 */
00042 
00043 #include <stdlib.h>
00044 #include "Vec_dh.h"
00045 #include "Mem_dh.h"
00046 #include "SubdomainGraph_dh.h"
00047 #include "io_dh.h"
00048 
00049 #undef __FUNC__
00050 #define __FUNC__ "Vec_dhCreate"
00051 void
00052 Vec_dhCreate (Vec_dh * v)
00053 {
00054   START_FUNC_DH
00055     struct _vec_dh *tmp =
00056     (struct _vec_dh *) MALLOC_DH (sizeof (struct _vec_dh));
00057   CHECK_V_ERROR;
00058   *v = tmp;
00059   tmp->n = 0;
00060   tmp->vals = NULL;
00061 END_FUNC_DH}
00062 
00063 #undef __FUNC__
00064 #define __FUNC__ "Vec_dhDestroy"
00065 void
00066 Vec_dhDestroy (Vec_dh v)
00067 {
00068   START_FUNC_DH if (v->vals != NULL)
00069     FREE_DH (v->vals);
00070   CHECK_V_ERROR;
00071   FREE_DH (v);
00072   CHECK_V_ERROR;
00073 END_FUNC_DH}
00074 
00075 
00076 #undef __FUNC__
00077 #define __FUNC__ "Vec_dhInit"
00078 void
00079 Vec_dhInit (Vec_dh v, int size)
00080 {
00081   START_FUNC_DH v->n = size;
00082   v->vals = (double *) MALLOC_DH (size * sizeof (double));
00083   CHECK_V_ERROR;
00084 END_FUNC_DH}
00085 
00086 #undef __FUNC__
00087 #define __FUNC__ "Vec_dhCopy"
00088 void
00089 Vec_dhCopy (Vec_dh x, Vec_dh y)
00090 {
00091   START_FUNC_DH if (x->vals == NULL)
00092     SET_V_ERROR ("x->vals is NULL");
00093   if (y->vals == NULL)
00094     SET_V_ERROR ("y->vals is NULL");
00095   if (x->n != y->n)
00096     SET_V_ERROR ("x and y are different lengths");
00097   memcpy (y->vals, x->vals, x->n * sizeof (double));
00098 END_FUNC_DH}
00099 
00100 
00101 #undef __FUNC__
00102 #define __FUNC__ "Vec_dhDuplicate"
00103 void
00104 Vec_dhDuplicate (Vec_dh v, Vec_dh * out)
00105 {
00106   START_FUNC_DH Vec_dh tmp;
00107   int size = v->n;
00108   if (v->vals == NULL)
00109     SET_V_ERROR ("v->vals is NULL");
00110   Vec_dhCreate (out);
00111   CHECK_V_ERROR;
00112   tmp = *out;
00113   tmp->n = size;
00114   tmp->vals = (double *) MALLOC_DH (size * sizeof (double));
00115   CHECK_V_ERROR;
00116 END_FUNC_DH}
00117 
00118 #undef __FUNC__
00119 #define __FUNC__ "Vec_dhSet"
00120 void
00121 Vec_dhSet (Vec_dh v, double value)
00122 {
00123   START_FUNC_DH int i, m = v->n;
00124   double *vals = v->vals;
00125   if (v->vals == NULL)
00126     SET_V_ERROR ("v->vals is NULL");
00127   for (i = 0; i < m; ++i)
00128     vals[i] = value;
00129 END_FUNC_DH}
00130 
00131 #undef __FUNC__
00132 #define __FUNC__ "Vec_dhSetRand"
00133 void
00134 Vec_dhSetRand (Vec_dh v)
00135 {
00136   START_FUNC_DH int i, m = v->n;
00137   double max = 0.0;
00138   double *vals = v->vals;
00139 
00140   if (v->vals == NULL)
00141     SET_V_ERROR ("v->vals is NULL");
00142 
00143 #ifdef WIN32
00144   for (i = 0; i < m; ++i)
00145     vals[i] = rand ();
00146 #else
00147   for (i = 0; i < m; ++i)
00148     vals[i] = rand ();
00149 #endif
00150 
00151   /* find largest value in vector, and scale vector,
00152    * so all values are in [0.0,1.0]
00153    */
00154   for (i = 0; i < m; ++i)
00155     max = MAX (max, vals[i]);
00156   for (i = 0; i < m; ++i)
00157     vals[i] = vals[i] / max;
00158 END_FUNC_DH}
00159 
00160 
00161 #undef __FUNC__
00162 #define __FUNC__ "Vec_dhPrint"
00163 void
00164 Vec_dhPrint (Vec_dh v, SubdomainGraph_dh sg, char *filename)
00165 {
00166   START_FUNC_DH double *vals = v->vals;
00167   int pe, i, m = v->n;
00168   FILE *fp;
00169 
00170   if (v->vals == NULL)
00171     SET_V_ERROR ("v->vals is NULL");
00172 
00173   /*--------------------------------------------------------
00174    * case 1: no permutation information
00175    *--------------------------------------------------------*/
00176   if (sg == NULL)
00177     {
00178       for (pe = 0; pe < np_dh; ++pe)
00179     {
00180       MPI_Barrier (comm_dh);
00181       if (pe == myid_dh)
00182         {
00183           if (pe == 0)
00184         {
00185           fp = openFile_dh (filename, "w");
00186           CHECK_V_ERROR;
00187         }
00188           else
00189         {
00190           fp = openFile_dh (filename, "a");
00191           CHECK_V_ERROR;
00192         }
00193 
00194           for (i = 0; i < m; ++i)
00195         fprintf (fp, "%g\n", vals[i]);
00196 
00197           closeFile_dh (fp);
00198           CHECK_V_ERROR;
00199         }
00200     }
00201     }
00202 
00203   /*--------------------------------------------------------
00204    * case 2: single mpi task, multiple subdomains
00205    *--------------------------------------------------------*/
00206   else if (np_dh == 1)
00207     {
00208       int i, j;
00209 
00210       fp = openFile_dh (filename, "w");
00211       CHECK_V_ERROR;
00212 
00213       for (i = 0; i < sg->blocks; ++i)
00214     {
00215       int oldBlock = sg->n2o_sub[i];
00216       int beg_row = sg->beg_rowP[oldBlock];
00217       int end_row = beg_row + sg->row_count[oldBlock];
00218 
00219       printf ("seq: block= %i  beg= %i  end= %i\n", oldBlock, beg_row,
00220           end_row);
00221 
00222 
00223       for (j = beg_row; j < end_row; ++j)
00224         {
00225           fprintf (fp, "%g\n", vals[j]);
00226         }
00227     }
00228     }
00229 
00230   /*--------------------------------------------------------
00231    * case 3: multiple mpi tasks, one subdomain per task
00232    *--------------------------------------------------------*/
00233   else
00234     {
00235       int id = sg->o2n_sub[myid_dh];
00236       for (pe = 0; pe < np_dh; ++pe)
00237     {
00238       MPI_Barrier (comm_dh);
00239       if (id == pe)
00240         {
00241           if (pe == 0)
00242         {
00243           fp = openFile_dh (filename, "w");
00244           CHECK_V_ERROR;
00245         }
00246           else
00247         {
00248           fp = openFile_dh (filename, "a");
00249           CHECK_V_ERROR;
00250         }
00251 
00252           fprintf (stderr, "par: block= %i\n", id);
00253 
00254           for (i = 0; i < m; ++i)
00255         {
00256           fprintf (fp, "%g\n", vals[i]);
00257         }
00258 
00259           closeFile_dh (fp);
00260           CHECK_V_ERROR;
00261         }
00262     }
00263     }
00264 END_FUNC_DH}
00265 
00266 
00267 #undef __FUNC__
00268 #define __FUNC__ "Vec_dhPrintBIN"
00269 void
00270 Vec_dhPrintBIN (Vec_dh v, SubdomainGraph_dh sg, char *filename)
00271 {
00272   START_FUNC_DH if (np_dh > 1)
00273     {
00274       SET_V_ERROR ("only implemented for a single MPI task");
00275     }
00276   if (sg != NULL)
00277     {
00278       SET_V_ERROR ("not implemented for reordered vector; ensure sg=NULL");
00279     }
00280 
00281   io_dh_print_ebin_vec_private (v->n, 0, v->vals, NULL, NULL, NULL, filename);
00282   CHECK_V_ERROR;
00283 END_FUNC_DH}
00284 
00285 #define MAX_JUNK 200
00286 
00287 #undef __FUNC__
00288 #define __FUNC__ "Vec_dhRead"
00289 void
00290 Vec_dhRead (Vec_dh * vout, int ignore, char *filename)
00291 {
00292   START_FUNC_DH Vec_dh tmp;
00293   FILE *fp;
00294   int items, n, i;
00295   double *v, w;
00296   char junk[MAX_JUNK];
00297 
00298   Vec_dhCreate (&tmp);
00299   CHECK_V_ERROR;
00300   *vout = tmp;
00301 
00302   if (np_dh > 1)
00303     {
00304       SET_V_ERROR ("only implemented for a single MPI task");
00305     }
00306 
00307   fp = openFile_dh (filename, "w");
00308   CHECK_V_ERROR;
00309 
00310   /* skip over file lines */
00311   if (ignore)
00312     {
00313       printf ("Vec_dhRead:: ignoring following header lines:\n");
00314       printf
00315     ("--------------------------------------------------------------\n");
00316       for (i = 0; i < ignore; ++i)
00317     {
00318       fgets (junk, MAX_JUNK, fp);
00319       printf ("%s", junk);
00320     }
00321       printf
00322     ("--------------------------------------------------------------\n");
00323     }
00324 
00325   /* count floating point entries in file */
00326   n = 0;
00327   while (!feof (fp))
00328     {
00329       items = fscanf (fp, "%lg", &w);
00330       if (items != 1)
00331     {
00332       break;
00333     }
00334       ++n;
00335     }
00336 
00337   printf ("Vec_dhRead:: n= %i\n", n);
00338 
00339   /* allocate storage */
00340   tmp->n = n;
00341   v = tmp->vals = (double *) MALLOC_DH (n * sizeof (double));
00342   CHECK_V_ERROR;
00343 
00344   /* reset file, and skip over header again */
00345   rewind (fp);
00346   rewind (fp);
00347   for (i = 0; i < ignore; ++i)
00348     {
00349       fgets (junk, MAX_JUNK, fp);
00350     }
00351 
00352   /* read values */
00353   for (i = 0; i < n; ++i)
00354     {
00355       items = fscanf (fp, "%lg", v + i);
00356       if (items != 1)
00357     {
00358       sprintf (msgBuf_dh, "failed to read value %i of %i", i + 1, n);
00359     }
00360     }
00361 
00362   closeFile_dh (fp);
00363   CHECK_V_ERROR;
00364 END_FUNC_DH}
00365 
00366 #undef __FUNC__
00367 #define __FUNC__ "Vec_dhReadBIN"
00368 extern void
00369 Vec_dhReadBIN (Vec_dh * vout, char *filename)
00370 {
00371   START_FUNC_DH Vec_dh tmp;
00372 
00373   Vec_dhCreate (&tmp);
00374   CHECK_V_ERROR;
00375   *vout = tmp;
00376   io_dh_read_ebin_vec_private (&tmp->n, &tmp->vals, filename);
00377   CHECK_V_ERROR;
00378 END_FUNC_DH}
 All Classes Files Functions Variables Enumerations Friends