Mentions légales du service

Skip to content
Snippets Groups Projects
Commit 99f74a7e authored by Tony Delarue's avatar Tony Delarue
Browse files

Remove duplicated lines, clean spm.c

parent 93f12bd8
No related branches found
No related tags found
1 merge request!42Sonarqube_smells
...@@ -165,36 +165,19 @@ spmInit( spmatrix_t *spm ) ...@@ -165,36 +165,19 @@ spmInit( spmatrix_t *spm )
void void
spmAlloc( spmatrix_t *spm ) spmAlloc( spmatrix_t *spm )
{ {
spm_int_t colsize, rowsize, valsize, dofsize; spm_int_t colsize = (spm->fmttype == SpmCSC) ? spm->n + 1 : spm->nnz;
spm_int_t rowsize = (spm->fmttype == SpmCSR) ? spm->n + 1 : spm->nnz;
switch(spm->fmttype){
case SpmCSC:
colsize = spm->n + 1;
rowsize = spm->nnz;
valsize = spm->nnzexp;
dofsize = spm->gN + 1;
break;
case SpmCSR:
colsize = spm->nnz;
rowsize = spm->n + 1;
valsize = spm->nnzexp;
dofsize = spm->gN + 1;
break;
case SpmIJV:
default:
colsize = spm->nnz;
rowsize = spm->nnz;
valsize = spm->nnzexp;
dofsize = spm->gN + 1;
}
spm->colptr = (spm_int_t*)malloc( colsize * sizeof(spm_int_t) ); spm->colptr = (spm_int_t*)malloc( colsize * sizeof(spm_int_t) );
spm->rowptr = (spm_int_t*)malloc( rowsize * sizeof(spm_int_t) ); spm->rowptr = (spm_int_t*)malloc( rowsize * sizeof(spm_int_t) );
if ( spm->dof < 1 ) { if ( spm->dof < 1 ) {
spm_int_t dofsize = spm->gN + 1;
spm->dofs = (spm_int_t*)malloc( dofsize * sizeof(spm_int_t) ); spm->dofs = (spm_int_t*)malloc( dofsize * sizeof(spm_int_t) );
} }
if(spm->flttype != SpmPattern) { if(spm->flttype != SpmPattern) {
valsize = valsize * spm_size_of( spm->flttype ); spm_int_t valsize = spm->nnzexp * spm_size_of( spm->flttype );
spm->values = malloc(valsize); spm->values = malloc(valsize);
} }
} }
...@@ -260,7 +243,7 @@ spmBase( spmatrix_t *spm, ...@@ -260,7 +243,7 @@ spmBase( spmatrix_t *spm,
int baseval ) int baseval )
{ {
spm_int_t baseadj; spm_int_t baseadj;
spm_int_t i, n, nnz; spm_int_t i, n, nnz, colsize, rowsize;
/* Parameter checks */ /* Parameter checks */
if ( spm == NULL ) { if ( spm == NULL ) {
...@@ -281,39 +264,20 @@ spmBase( spmatrix_t *spm, ...@@ -281,39 +264,20 @@ spmBase( spmatrix_t *spm,
} }
baseadj = baseval - spmFindBase( spm ); baseadj = baseval - spmFindBase( spm );
if (baseadj == 0) if (baseadj == 0) {
return; return;
}
n = spm->n; n = spm->n;
nnz = spm->nnz; nnz = spm->nnz;
colsize = (spm->fmttype == SpmCSC) ? n + 1 : nnz;
switch(spm->fmttype) rowsize = (spm->fmttype == SpmCSR) ? n + 1 : nnz;
{
case SpmCSC:
assert( nnz == (spm->colptr[n] - spm->colptr[0]) );
for (i = 0; i <= n; i++) {
spm->colptr[i] += baseadj;
}
for (i = 0; i < nnz; i++) {
spm->rowptr[i] += baseadj;
}
break;
case SpmCSR: for (i = 0; i < colsize; i++) {
assert( nnz == (spm->rowptr[n] - spm->rowptr[0]) ); spm->colptr[i] += baseadj;
for (i = 0; i <= n; i++) { }
spm->rowptr[i] += baseadj; for (i = 0; i < rowsize; i++) {
} spm->rowptr[i] += baseadj;
for (i = 0; i < nnz; i++) {
spm->colptr[i] += baseadj;
}
break;
case SpmIJV:
for (i = 0; i < nnz; i++) {
spm->rowptr[i] += baseadj;
spm->colptr[i] += baseadj;
}
} }
if (spm->loc2glob != NULL) { if (spm->loc2glob != NULL) {
...@@ -508,10 +472,6 @@ spmNorm( spm_normtype_t ntype, ...@@ -508,10 +472,6 @@ spmNorm( spm_normtype_t ntype,
{ {
double norm = -1.; double norm = -1.;
if ( spm->flttype == SpmPattern ) {
return norm;
}
switch (spm->flttype) { switch (spm->flttype) {
case SpmFloat: case SpmFloat:
norm = (double)s_spmNorm( ntype, spm ); norm = (double)s_spmNorm( ntype, spm );
...@@ -531,7 +491,7 @@ spmNorm( spm_normtype_t ntype, ...@@ -531,7 +491,7 @@ spmNorm( spm_normtype_t ntype,
case SpmPattern: case SpmPattern:
default: default:
; return norm;
} }
return norm; return norm;
...@@ -807,26 +767,10 @@ spmCopy( const spmatrix_t *spm ) ...@@ -807,26 +767,10 @@ spmCopy( const spmatrix_t *spm )
memcpy( newspm, spm, sizeof(spmatrix_t)); memcpy( newspm, spm, sizeof(spmatrix_t));
switch(spm->fmttype){ colsize = (spm->fmttype == SpmCSC) ? spm->n + 1 : spm->nnz;
case SpmCSC: rowsize = (spm->fmttype == SpmCSR) ? spm->n + 1 : spm->nnz;
colsize = spm->n + 1; valsize = spm->nnzexp;
rowsize = spm->nnz; dofsize = spm->gN + 1;
valsize = spm->nnzexp;
dofsize = spm->gN + 1;
break;
case SpmCSR:
colsize = spm->nnz;
rowsize = spm->n + 1;
valsize = spm->nnzexp;
dofsize = spm->gN + 1;
break;
case SpmIJV:
default:
colsize = spm->nnz;
rowsize = spm->nnz;
valsize = spm->nnzexp;
dofsize = spm->gN + 1;
}
if(spm->colptr != NULL) { if(spm->colptr != NULL) {
newspm->colptr = (spm_int_t*)malloc( colsize * sizeof(spm_int_t) ); newspm->colptr = (spm_int_t*)malloc( colsize * sizeof(spm_int_t) );
...@@ -1038,6 +982,7 @@ spmPrintRHS( const spmatrix_t *spm, ...@@ -1038,6 +982,7 @@ spmPrintRHS( const spmatrix_t *spm,
z_spmPrintRHS( stream, spm, nrhs, x, ldx ); z_spmPrintRHS( stream, spm, nrhs, x, ldx );
break; break;
case SpmDouble: case SpmDouble:
default:
d_spmPrintRHS( stream, spm, nrhs, x, ldx ); d_spmPrintRHS( stream, spm, nrhs, x, ldx );
} }
...@@ -1145,10 +1090,6 @@ spmMatVec( spm_trans_t trans, ...@@ -1145,10 +1090,6 @@ spmMatVec( spm_trans_t trans,
return SPM_ERR_BADPARAMETER; return SPM_ERR_BADPARAMETER;
} }
if ( spm->dof != 1 ) {
espm = malloc( sizeof(spmatrix_t) );
spmExpand( spm, espm );
}
switch (spm->flttype) { switch (spm->flttype) {
case SpmFloat: case SpmFloat:
rc = spm_sspmv( trans, alpha, espm, x, 1, beta, y, 1 ); rc = spm_sspmv( trans, alpha, espm, x, 1, beta, y, 1 );
...@@ -1631,50 +1572,50 @@ spm_get_distribution( const spmatrix_t *spm ) ...@@ -1631,50 +1572,50 @@ spm_get_distribution( const spmatrix_t *spm )
{ {
int distribution = 0; int distribution = 0;
/* The matrix is not distributed */
if( (spm->loc2glob == NULL) || (spm->n == spm->gN) ) { if( (spm->loc2glob == NULL) || (spm->n == spm->gN) ) {
distribution = ( SpmDistByColumn | SpmDistByRow ); distribution = ( SpmDistByColumn | SpmDistByRow );
return distribution;
}
if( spm->fmttype == SpmCSC ){
distribution = SpmDistByColumn;
}
else if ( spm->fmttype == SpmCSR ) {
distribution = SpmDistByRow;
} }
else { else {
if( spm->fmttype == SpmCSC ){ spm_int_t i, baseval;
distribution = SpmDistByColumn; spm_int_t *colptr = spm->colptr;
} spm_int_t *glob2loc = spm->glob2loc;
else if ( spm->fmttype == SpmCSR ) {
distribution = SpmDistByRow; baseval = spmFindBase( spm );
} distribution = 1;
else { assert( glob2loc != NULL );
spm_int_t i, baseval; for ( i = 0; i < spm->nnz; i++, colptr++ )
spm_int_t *colptr = spm->colptr; {
spm_int_t *glob2loc = spm->glob2loc; /*
* If the global index is not in the local colptr
baseval = spmFindBase( spm ); * -> row distribution
distribution = 1; */
assert( glob2loc != NULL ); if( glob2loc[ *colptr - baseval ] < 0 ) {
for ( i = 0; i < spm->nnz; i++, colptr++ ) distribution = SpmDistByRow;
{ break;
/*
* If the global index is not in the local colptr
* -> row distribution
*/
if( glob2loc[ *colptr - baseval ] < 0 ) {
distribution = SpmDistByRow;
break;
}
} }
}
#if defined(SPM_WITH_MPI) #if defined(SPM_WITH_MPI)
{ {
int check = 0; int check = 0;
MPI_Allreduce( &distribution, &check, 1, MPI_INT, MPI_Allreduce( &distribution, &check, 1, MPI_INT,
MPI_BOR, spm->comm ); MPI_BOR, spm->comm );
/* /*
* If a matrix is distributed * If a matrix is distributed
* it cannot be distributed by row AND column * it cannot be distributed by row AND column
*/ */
assert( check != ( SpmDistByColumn | SpmDistByRow ) ); assert( check != ( SpmDistByColumn | SpmDistByRow ) );
assert( distribution == check ); assert( distribution == check );
}
#endif
} }
#endif
} }
assert(distribution > 0); assert(distribution > 0);
return distribution; return distribution;
......
...@@ -231,7 +231,7 @@ spm_scatter_csx_get_locals( const spmatrix_t *oldspm, ...@@ -231,7 +231,7 @@ spm_scatter_csx_get_locals( const spmatrix_t *oldspm,
root, newspm->comm ); root, newspm->comm );
} }
/* Syore the local information */ /* Store the local information */
newspm->nnz = allcounts[ newspm->clustnum * 3 + 1 ]; newspm->nnz = allcounts[ newspm->clustnum * 3 + 1 ];
newspm->nnzexp = allcounts[ newspm->clustnum * 3 + 2 ]; newspm->nnzexp = allcounts[ newspm->clustnum * 3 + 2 ];
...@@ -339,7 +339,7 @@ spm_scatter_ijv_get_locals( const spmatrix_t *oldspm, ...@@ -339,7 +339,7 @@ spm_scatter_ijv_get_locals( const spmatrix_t *oldspm,
root, newspm->comm ); root, newspm->comm );
} }
/* Syore the local information */ /* Store the local information */
newspm->nnz = allcounts[ newspm->clustnum * 3 + 1 ]; newspm->nnz = allcounts[ newspm->clustnum * 3 + 1 ];
newspm->nnzexp = allcounts[ newspm->clustnum * 3 + 2 ]; newspm->nnzexp = allcounts[ newspm->clustnum * 3 + 2 ];
......
...@@ -62,7 +62,6 @@ z_spm_dof_extend_update_values( spm_complex64_t *newval, ...@@ -62,7 +62,6 @@ z_spm_dof_extend_update_values( spm_complex64_t *newval,
} }
} }
} }
else { else {
for(jj=0; jj<dofj; jj++) for(jj=0; jj<dofj; jj++)
{ {
...@@ -189,6 +188,4 @@ z_spmDofExtend( spmatrix_t *spm ) ...@@ -189,6 +188,4 @@ z_spmDofExtend( spmatrix_t *spm )
else { else {
z_spm_dof_extend_ijv( spm ); z_spm_dof_extend_ijv( spm );
} }
return;
} }
...@@ -355,6 +355,7 @@ z_spmFrobeniusNorm( const spmatrix_t *spm ) ...@@ -355,6 +355,7 @@ z_spmFrobeniusNorm( const spmatrix_t *spm )
break; break;
case SpmIJV: case SpmIJV:
default:
z_spmFrobeniusNorm_ijv( spm, data ); z_spmFrobeniusNorm_ijv( spm, data );
} }
} }
...@@ -622,10 +623,10 @@ z_spm_oneinf_elt( const spm_mtxtype_t mtxtype, ...@@ -622,10 +623,10 @@ z_spm_oneinf_elt( const spm_mtxtype_t mtxtype,
} }
/** /**
* @brief Compute the one/inf norm of an spm CSX structure. * @brief Compute the one/inf norm of an spm CSC structure.
*/ */
static inline void static inline void
z_spmOneInfNorm_csx( spm_normtype_t ntype, z_spmOneInfNorm_csc( spm_normtype_t ntype,
const spmatrix_t *spm, const spmatrix_t *spm,
double *sumtab, double *sumtab,
spm_int_t baseval ) spm_int_t baseval )
...@@ -635,8 +636,8 @@ z_spmOneInfNorm_csx( spm_normtype_t ntype, ...@@ -635,8 +636,8 @@ z_spmOneInfNorm_csx( spm_normtype_t ntype,
spm_int_t *colptr, *rowptr, *loc2glob, *dofs; spm_int_t *colptr, *rowptr, *loc2glob, *dofs;
spm_complex64_t *valptr; spm_complex64_t *valptr;
colptr = (spm->fmttype == SpmCSC) ? spm->colptr : spm->rowptr; colptr = spm->colptr;
rowptr = (spm->fmttype == SpmCSC) ? spm->rowptr : spm->colptr; rowptr = spm->rowptr;
valptr = (spm_complex64_t*)(spm->values); valptr = (spm_complex64_t*)(spm->values);
loc2glob = spm->loc2glob; loc2glob = spm->loc2glob;
dofs = spm->dofs; dofs = spm->dofs;
...@@ -673,6 +674,58 @@ z_spmOneInfNorm_csx( spm_normtype_t ntype, ...@@ -673,6 +674,58 @@ z_spmOneInfNorm_csx( spm_normtype_t ntype,
} }
} }
/**
* @brief Compute the one/inf norm of an spm CSR structure.
*/
static inline void
z_spmOneInfNorm_csr( spm_normtype_t ntype,
const spmatrix_t *spm,
double *sumtab,
spm_int_t baseval )
{
spm_int_t i, j, ig, jg, col, row;
spm_int_t dofi, dofj, dof;
spm_int_t *colptr, *rowptr, *loc2glob, *dofs;
spm_complex64_t *valptr;
colptr = spm->colptr;
rowptr = spm->rowptr;
valptr = (spm_complex64_t*)(spm->values);
loc2glob = spm->loc2glob;
dofs = spm->dofs;
dof = spm->dof;
for(i=0; i<spm->n; i++, rowptr++, loc2glob++)
{
ig = (spm->loc2glob == NULL) ? i : (*loc2glob) - baseval;
if ( dof > 0 ) {
dofi = dof;
row = dof * ig;
}
else {
dofi = dofs[ig+1] - dofs[ig];
row = dofs[ig] - baseval;
}
for(j=rowptr[0]; j<rowptr[1]; j++, colptr++)
{
jg = (*colptr - baseval);
if ( dof > 0 ) {
dofj = dof;
col = dof * jg;
}
else {
dofj = dofs[jg+1] - dofs[jg];
col = dofs[jg] - baseval;
}
z_spm_oneinf_elt( spm->mtxtype, spm->layout,
row, dofi, col, dofj, valptr,
ntype, sumtab );
valptr += dofi * dofj;
}
}
}
/** /**
* @brief Compute the one/inf norm of an spm IJV structure. * @brief Compute the one/inf norm of an spm IJV structure.
*/ */
...@@ -750,11 +803,19 @@ z_spmOneInfNorm( spm_normtype_t ntype, ...@@ -750,11 +803,19 @@ z_spmOneInfNorm( spm_normtype_t ntype,
baseval = spmFindBase( spm ); baseval = spmFindBase( spm );
if( spm->fmttype != SpmIJV ){ switch (spm->fmttype)
z_spmOneInfNorm_csx( ntype, spm, sumtab, baseval ); {
} case SpmCSC:
else{ z_spmOneInfNorm_csc( ntype, spm, sumtab, baseval );
z_spmOneInfNorm_ijv( ntype, spm, sumtab, baseval ); break;
case SpmCSR:
z_spmOneInfNorm_csr( ntype, spm, sumtab, baseval );
break;
case SpmIJV:
default:
z_spmOneInfNorm_ijv( ntype, spm, sumtab, baseval );
} }
#if defined(SPM_WITH_MPI) #if defined(SPM_WITH_MPI)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment