#include #include #include /// #include "InputOutput.h" #include "ScalarVectors.h" #include "hb_io.h" #include "SparseProduct.h" /*********************************************************************************/ // This routine creates a sparseMatrix from the next parameters // * numR defines the number of rows // * numC defines the number of columns // * numE defines the number of nonzero elements // * msr indicates if the MSR is the format used to the sparse matrix // If msr is actived, numE doesn't include the diagonal elements // The parameter index indicates if 0-indexing or 1-indexing is used. void CreateSparseMatrix (ptr_SparseMatrix p_spr, int index, int numR, int numC, int numE, int msr) { // printf (" index = %d , numR = %d , numC = %d , numE = %d\n", index, numR, numC, numE); // The scalar components of the structure are initiated p_spr->dim1 = numR; p_spr->dim2 = numC; // Only one malloc is made for the vectors of indices CreateInts (&(p_spr->vptr), numE+numR+1); // The first component of the vectors depends on the used format *(p_spr->vptr) = ((msr)? (numR+1): 0) + index; p_spr->vpos = p_spr->vptr + ((msr)? 0: (numR+1)); // The number of nonzero elements depends on the format used CreateDoubles (&(p_spr->vval), numE+(numR+1)*msr); } // This routine creates the first part of a sparseMatrix from the next parameters // * numR defines the number of rows // * numC defines the number of columns // * msr indicates if the MSR is the format used to the sparse matrix // If msr is actived, numE doesn't include the diagonal elements // The parameter index indicates if 0-indexing or 1-indexing is used. void CreateSparseMatrixVptr (ptr_SparseMatrix spr, int numR, int numC, int msr) { spr->dim1 = numR; spr->dim2 = numC; CreateInts (&(spr->vptr), numR+1); *(spr->vptr) = ((msr)? (numR+1): 0); } // This routine creates the second part of a sparseMatrix from the next parameters // * numR defines the number of rows // * numC defines the number of columns // * numE defines the number of nonzero elements // * msr indicates if the MSR is the format used to the sparse matrix // If msr is actived, numE doesn't include the diagonal elements // The parameter index indicates if 0-indexing or 1-indexing is used. void CreateSparseMatrixValues (ptr_SparseMatrix spr, int numR, int numC, int numE, int msr) { CreateInts (&(spr->vpos), numE+(numR+1)*msr); CreateDoubles (&(spr->vval), numE+(numR+1)*msr); } // This routine liberates the memory related to matrix spr void RemoveSparseMatrix (ptr_SparseMatrix spr) { // First the scalar are initiated spr->dim1 = -1; spr->dim2 = -1; // The vectors are liberated RemoveInts (&(spr->vptr)); RemoveDoubles (&(spr->vval)); } // This routine liberates the memory related to matrix spr when // vptr and vpos have been allocated separetely void RemoveSparseMatrix2 (ptr_SparseMatrix spr) { spr->dim1 = -1; spr->dim2 = -1; RemoveInts (&(spr->vptr)); RemoveInts (&(spr->vpos)); RemoveDoubles (&(spr->vval)); } /*********************************************************************************/ // This routine creates de sparse matrix dst from the symmetric matrix spr. // The parameters indexS and indexD indicate, respectivaly, if 0-indexing or 1-indexing is used // to store the sparse matrices. void DesymmetrizeSparseMatrices (SparseMatrix src, int indexS, ptr_SparseMatrix dst, int indexD) { int n = src.dim1, nnz = 0; int *sizes = NULL; int *pp1 = NULL, *pp2 = NULL, *pp3 = NULL, *pp4 = NULL, *pp5 = NULL; int i, j, dim, indexDS = indexD - indexS; double *pd3 = NULL, *pd4 = NULL; // The vector sizes is created and initiated CreateInts (&sizes, n); InitInts (sizes, n, 0, 0); // This loop counts the number of elements in each row pp1 = src.vptr; pp3 = src.vpos + *pp1 - indexS; pp2 = pp1 + 1 ; pp4 = sizes - indexS; for (i=indexS; i<(n+indexS); i++) { // The size of the corresponding row is accumulated dim = (*pp2 - *pp1); pp4[i] += dim; // Now each component of the row is analyzed for (j=0; jvptr) + 1, n); dst->vptr[0] = indexD; TransformLengthtoHeader (dst->vptr, n); // The vector sizes is initiated with the beginning of each row CopyInts (dst->vptr, sizes, n); // This loop fills the contents of vector vpos pp1 = src.vptr; pp3 = src.vpos + *pp1 - indexS; pp2 = pp1 + 1 ; pp4 = dst->vpos - indexD; pp5 = sizes - indexS; pd3 = src.vval + *pp1 - indexS; pd4 = dst->vval - indexD; for (i=indexS; i<(n+indexS); i++) { dim = (*pp2 - *pp1); for (j=0; jvptr) + 1, n); dst->vptr[0] = indexD; TransformLengthtoHeader (dst->vptr, n); // The vector sizes is initiated with the beginning of each row CopyInts (dst->vptr, sizes, n); // This loop fills the contents of vector vpos pp1 = src.vptr; pp3 = src.vpos + *pp1 - indexS; pp2 = pp1 + 1 ; pp4 = dst->vpos - indexD; pp5 = sizes - indexS; pd3 = src.vval + *pp1 - indexS; pd4 = dst->vval - indexD; for (i=indexS; i<(n+indexS); i++) { dim = (*pp2 - *pp1); for (j=0; jdim1 = nrow ; p_spr->dim2 = ncol ; p_spr->vptr = colptr; p_spr->vpos = rowind; p_spr->vval = values; // Memory liberation free (exact ); free (guess ); free (indfmt); free (key ); free (mxtype); free (ptrfmt); free (rhsfmt); free (rhsind); free (rhsptr); free (rhstyp); free (rhsval); free (rhsvec); free (title ); free (valfmt); return 0; } #define length1 82 #define length2 82 FILE *OpenFile (char *name, char *attr) { FILE *fich; if ((fich = fopen (name, attr)) == NULL) { printf ("File %s not exists \n", name); exit(1); } return fich; } void ReadStringFile (FILE *file, char *string, int length) { char *s = NULL; if ((s = fgets (string, length, file)) == NULL) { printf ("Error en lectura \n"); exit (1); } } void CreateSparseMatrixHB (char *nameFile, ptr_SparseMatrix spr, int FtoC) { FILE *file; char string[length1], *s = NULL; int i, j, k = 0, shft = (FtoC)?-1:0; int *vptr = NULL, *vpos = NULL; double *vval = NULL; int lines[5], dim[4], formats[10]; file = OpenFile (nameFile, "r"); ReadStringFile (file, string, length1); ReadStringFile (file, string, length1); GetIntsFromString (string, lines, 5, 14, 0); ReadStringFile (file, string, length1); GetIntsFromString ((string+14), dim, 4, 14, 0); CreateSparseMatrix (spr, 0, dim[0], dim[1], dim[2], 0); vptr = spr->vptr; vpos = spr->vpos; vval = spr->vval; ReadStringFile (file, string, length1); GetFormatsFromString (string, formats, 2, 16); GetFormatsFromString ((string+32), (formats+4), 1+(lines[4] > 0), 20); if (lines[4] > 0) ReadStringFile (file, string, length1); j = 0; for (i = 0; i < lines[1]; i++) { ReadStringFile (file, string, length2); k = ((dim[0] + 1) - j); if (k > formats[0]) k = formats[0]; GetIntsFromString (string, (vptr+j), k, formats[1], shft); j+=formats[0]; } j = 0; for (i = 0; i < lines[2]; i++) { ReadStringFile (file, string, length2); k = (dim[2] - j); if (k > formats[2]) k = formats[2]; GetIntsFromString (string, (vpos+j), k, formats[3], shft); j+=formats[2]; } j = 0; for (i = 0; i < lines[3]; i++) { ReadStringFile (file, string, length2); k = (dim[2] - j); if (k > formats[4]) k = formats[4]; GetDoublesFromString (string, (vval+j), k, formats[5]); j+=formats[4]; } fclose (file); } /*********************************************************************************/ // This routine computes the product { res += spr * vec }. // The parameter index indicates if 0-indexing or 1-indexing is used, void ProdSparseMatrixVector2 (SparseMatrix spr, int index, double *vec, double *res) { int i, j; int *pp1 = spr.vptr, *pp2 = pp1+1, *pi1 = spr.vpos + *pp1 - index; double aux, *pvec = vec - index, *pd2 = res; double *pd1 = spr.vval + *pp1 - index; // If the MSR format is used, first the diagonal has to be processed if (spr.vptr == spr.vpos) VvecDoubles (1.0, spr.vval, vec, 1.0, res, spr.dim1); for (i=0; i 0) && (*pi1 < (i+shft))) { pi1++; pd1++; j--; } *(pd2++) = ((j > 0) && (*pi1 == (i+shft))) ? *pd1: 0.0; //*(pi2++) = ((j > 0) && (*pi1 == (i+shft))) ? *pp2-j: -1; pi1 += j; pd1 += j; pp1 = (pp2++); j = (*pp2-*pp1); } } } /*********************************************************************************/