Ifpack Package Browser (Single Doxygen Collection) Development
Loading...
Searching...
No Matches
read_coo.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
47void read_coo(char *data_file, int *proc_config,
48 int *N_global, int *n_nonzeros,
49 double **val, int **bindx,
50 double **x, double **b, double **xexact)
51#undef DEBUG
52 /* read ASCII data file:
53 line 1: N_global, number of entries (%d,%d)
54 line 2-...: i,j,real (%d, %d, %f)
55 */
56
57{
58 FILE *data ;
59
60
61 int i, n_entries, N_columns;
62 int ii, jj ;
63 int kk = 0;
64 int max_ii = 0, max_jj = 0;
65 int ione = 1;
66 double value;
67 double *cnt;
68 int *pntr, *indx1, *pntr1;
69 double *val1;
70
71 int MAXBLOCKSIZE = 25;
72
73 if(proc_config[PAZ_node] == 0)
74 {
75
76
77 data = fopen(data_file,"r") ;
78
79 fscanf(data, "%d %d %d", N_global, &N_columns, &n_entries) ;
80 if (N_columns != *N_global)
81 perror("Matrix dimensions must be the same");
82 printf("Reading from file: %s\n",data_file);
83 printf("Number of equations = %d\n",*N_global);
84 printf("Number of entries = %d\n",n_entries);
85
86
87 *bindx = (int *) calloc(n_entries+1,sizeof(int)) ;
88 *val = (double *) calloc(n_entries+1,sizeof(double)) ;
89
90 pntr1 = (int *) calloc(n_entries+1,sizeof(int)) ;
91 indx1 = (int *) calloc(n_entries+1,sizeof(int)) ;
92 val1 = (double *) calloc(n_entries+1,sizeof(double)) ;
93
94 pntr = (int *) calloc(n_entries+1,sizeof(int)) ;
95
96 if ((pntr) == NULL)
97 perror("Error: Not enough space to create matrix");
98
99 while(!feof(data))
100 {
101 fscanf(data, "%d %d %lf", &ii, &jj, &value) ;
102 max_ii = max(max_ii,ii);
103 max_jj = max(max_jj,jj);
104#ifdef DEBUG
105 printf("Entry %d, %d = %lf.\n",ii,jj,value);
106#endif
107 (*bindx)[kk] = ii;
108 pntr[kk] = jj;
109 (*val)[kk] = value;
110 kk++;
111 }
112 *n_nonzeros = kk-1;
113 *N_global = max_ii;
114 if (max_ii != max_jj) perror("Error: Number of rows and columns not equal");
115
116 printf("Number of nonzeros = %d\n",*n_nonzeros);
117
118 /* Convert real part in the following way:
119 - Convert COO to CSR
120 - CSR to CSC
121 - CSC to CSR (columns are now in ascending order)
122 - CSR to MSR
123 */
124 coocsr_(N_global,n_nonzeros, *val, *bindx, pntr, val1, indx1, pntr1);
125
126 csrcsc_(N_global,&ione,&ione,
127 val1,indx1,pntr1,
128 *val,*bindx,pntr);
129
130 csrcsc_(N_global,&ione,&ione,
131 *val,*bindx,pntr,
132 val1,indx1,pntr1);
133
134 csrmsr_(N_global,val1,indx1,pntr1,
135 *val,*bindx,
136 *val,*bindx);
137
138 /* Finally, convert bindx vectors to zero base */
139
140 for (i=0;i<*n_nonzeros+1;i++)
141 (*bindx)[i] -= 1;
142
143 *b = (double *) calloc((*N_global)*MAXBLOCKSIZE,sizeof(double)) ;
144 *x = (double *) calloc((*N_global)*MAXBLOCKSIZE,sizeof(double)) ;
145
146 if ((*x) == NULL)
147 perror("Error: Not enough space to create matrix");
148
149
150 /* Set RHS to a random vector, initial guess to zero */
151 for (i=0;i<*N_global;i++)
152 {
153 (*b)[i] = drand48();
154 (*x)[i] = 0.0;
155 }
156 }
157
158 /* Release unneeded space */
159
160 free((void *) pntr);
161 free((void *) val1);
162 free((void *) indx1);
163 free((void *) pntr1);
164
165
166 /* end read_coo */
167}
#define perror(str)
Definition cc_main.cc:55
void read_coo(char *data_file, int *proc_config, int *N_global, int *n_nonzeros, double **val, int **bindx, double **x, double **b, double **xexact)
Definition read_coo.c:47
#define max(x, y)
Definition scscres.c:46