void printMNASparse (hashTable *names, mnaSpSystem *systemMna) { int i; char *str="matrixG.txt"; char *str1="matrixC.txt"; FILE *bfile; printf( "\tINFO : Group 2 elements:\n\t\t-Voltage Sources: %d\n\t\t-Inductors: %d\n", vnumber, lnumber ); printf( "\tINFO : Group 1 elements:\n\t\t-Resistors: %d\n\t\t-Current Sources: %d\n", rnumber, inumber ); printf( "\tINFO : Number of nodes of sub-Matrix G: %d\n", names->currPos ); //Sparse cs_print(systemMna->array_A,str,0); cs_print(systemMna->array_C,str1,0); bfile = fopen ( "b_vector.txt", "w" ); fprintf( bfile, "\tINFO : Group 2 elements:\n\t\t-Voltage Sources: %d\n\t\t-Inductors: %d\n", vnumber, lnumber ); fprintf( bfile, "\tINFO : Group 1 elements:\n\t\t-Resistors: %d\n\t\t-Current Sources: %d\n", rnumber, inumber ); fprintf( bfile, "\tINFO : Number of nodes of sub-Matrix G: %d\n", names->currPos ); fprintf( bfile, "\n\n\n\n" ); fprintf ( bfile, "\t -----B Vector----- \n\n" ); for ( i=0; i<( names->currPos + vnumber + lnumber ); i++ ) { fprintf ( bfile, "pos(%d)\t\t\t%.10f\n", i, systemMna->vector_B[i] ); } fclose ( bfile ); }
void display(const NumericsMatrix* const m) { if (! m) { fprintf(stderr, "Numerics, NumericsMatrix display failed, NULL input.\n"); exit(EXIT_FAILURE); } printf("\n ========== Numerics Matrix\n"); printf("\n ========== storageType = %i\n", m->storageType); switch (m->storageType) { case NM_DENSE: { displayMat(m->matrix0, m->size0, m->size1, m->size0); break; } case NM_SPARSE_BLOCK: { assert(m->matrix1); printSBM(m->matrix1); break; } case NM_SPARSE: { assert(m->matrix2); if (m->matrix2->triplet) { cs_print(m->matrix2->triplet, 0); } else if (m->matrix2->csc) { cs_print(m->matrix2->csc, 0); } else if (m->matrix2->trans_csc) { cs_print(m->matrix2->trans_csc, 0); } else { fprintf(stderr, "display for sparse matrix: no matrix found!\n"); } break; } default: { fprintf(stderr, "display for NumericsMatrix: matrix type %d not supported!\n", m->storageType); } } }
int main(void) { int res; printf("========= Starts SBM tests 4 for SBM ========= \n"); SparseBlockStructuredMatrix M; FILE *file = fopen("data/SBM2.dat", "r"); newFromFileSBM(&M, file); printSBM(&M); fclose(file); /*alloc enough memory */ CSparseMatrix sparseMat; res = SBMtoSparseInitMemory(&M, &sparseMat); if (res) { printf("========= Failed SBM tests 4 for SBM ========= \n"); return 1; } res = SBMtoSparse(&M, &sparseMat); if (res) { printf("========= Failed SBM tests 4 for SBM ========= \n"); return 1; } cs_print(&sparseMat, 1); cs_spfree_on_stack(&sparseMat); int n = M.blocksize0[M.blocknumber0 - 1]; int m = M.blocksize1[M.blocknumber1 - 1]; double * denseMat = (double *)malloc(n * m * sizeof(double)); SBMtoDense(&M, denseMat); if (res) { printf("========= Failed SBM tests 4 for SBM ========= \n"); return 1; } printf("["); for (int i = 0; i < n * m; i++) { printf("%lf ", denseMat[i]); if ((i + 1) % m == 0) printf("\n"); } printf("]"); printf("\n (warning: column-major) \n"); free(denseMat); printf("NUMERICS_SBM_FREE_BLOCK value %d", NUMERICS_SBM_FREE_BLOCK); SBMfree(&M, NUMERICS_SBM_FREE_BLOCK); printf("\n========= Succed SBM tests 4 for SBM ========= \n"); return 0; }
void test_ppp_add(char const * A_file, char const * B_file, char const * C_file) { cs * A = ppp_load_ccf(A_file); cs * B = ppp_load_ccf(B_file); cs *C = NULL, *D = NULL; if(C_file) C = ppp_load_ccf(C_file); cs * R = cs_spalloc(A->m, A->n, A->p[A->n] + B->p[B->n], 1, 0); /* allocate result*/ csi mn = A->n > A->m ? A->n : A->m; ppp_init_cs(mn); ppp_add(R, A, B, 1); /* Test equivalence */ if(C_file){ D = cs_add(C,R,1,-1); if(cs_norm(D) > cs_norm(C) * PPP_EPSILON){ fprintf(stderr, "Diff found [%s + %s != %s]:\n", A_file, B_file, C_file); fprintf(stdout, "Diff found [%s + %s != %s]:\n", A_file, B_file, C_file); fprintf(stdout, "Expected:\n"); cs_print(C,0); fprintf(stdout, "Obtained:\n"); cs_print(R,0); fprintf(stdout, "Diff:\n"); cs_print(D,0); }else{ fprintf(stdout, "Success!\n"); } } /* clean up*/ cs_spfree(A); cs_spfree(B); cs_spfree(C); cs_spfree(R); cs_spfree(D); ppp_done(); }
/* create an empty triplet matrix, insert 2 elements, print and free */ int main() { CSparseMatrix *m = cs_spalloc(0,0,0,0,1); /* coo format */ int info1 = 1-cs_entry(m, 3, 4, 1.0); int info2 = 1-cs_entry(m, 1, 2, 2.0); int info3 = 1-cs_print(m, 0); m=cs_spfree(m); int info4 = 1-(m==NULL); return info1+info2+info3+info4; }
/* cs_print: print the contents of a sparse matrix. */ void mexFunction ( int nargout, mxArray *pargout [ ], int nargin, const mxArray *pargin [ ] ) { cs Amatrix, *A ; int brief ; if (nargout > 0 || nargin < 1 || nargin > 2) { mexErrMsgTxt ("Usage: cs_print(A,brief)") ; } A = cs_mex_get_sparse (&Amatrix, 0, 1, pargin [0]) ; /* get A */ brief = (nargin < 2) ? 0 : mxGetScalar (pargin [1]) ; /* get brief */ cs_print (A, brief) ; /* print A */ }
int main ( void ) { cs *A; cs *AT; cs *C; cs *D; cs *Eye; int i; int m; cs *T; printf ( "\n" ); printf ( "CS_DEMO1:\n" ); printf ( " Demonstration of the CSPARSE package.\n" ); /* Load the triplet matrix T from standard input. */ T = cs_load ( stdin ); /* Print T. */ printf ( "T:\n" ); cs_print ( T, 0 ); /* A = compressed-column form of T. */ A = cs_triplet ( T ); printf ( "A:\n" ); cs_print ( A, 0 ); /* Clear T. */ cs_spfree ( T ); /* AT = A'. */ AT = cs_transpose ( A, 1 ); printf ( "AT:\n" ); cs_print ( AT, 0 ); /* M = number of rows of A. */ m = A->m; /* Create triplet identity matrix. */ T = cs_spalloc ( m, m, m, 1, 1 ); for ( i = 0; i < m; i++ ) { cs_entry ( T, i, i, 1 ); } /* Eye = speye ( m ) */ Eye = cs_triplet ( T ); cs_spfree ( T ); /* Compute C = A * A'. */ C = cs_multiply ( A, AT ); /* Compute D = C + Eye * norm (C,1). */ D = cs_add ( C, Eye, 1, cs_norm ( C ) ); printf ( "D:\n" ); cs_print ( D, 0 ); /* Clear A, AT, C, D, Eye, */ cs_spfree ( A ); cs_spfree ( AT ); cs_spfree ( C ); cs_spfree ( D ); cs_spfree ( Eye ); /* Terminate. */ printf ( "\n" ); printf ( "CS_DEMO1:\n" ); printf ( " Normal end of execution.\n" ); return ( 0 ); }
void mda_dump_extra( XDR *xdrs) { enum PV_TYPES { DBR_STRING=0, DBR_CTRL_CHAR=32, DBR_CTRL_SHORT=29, DBR_CTRL_LONG=33, DBR_CTRL_FLOAT=30, DBR_CTRL_DOUBLE=34 }; int i, j; short int pvs, type, count; count = 0; print( "\n\nExtra PV Offset = %li", xdr_getpos( xdrs) ); pvs = si_print( xdrs, "\n\nNumber of Extra PV's = %i.\n"); if( pvs < 0) { fflush(stdout); exit(1); } for( i = 0 ; i < pvs; i++) { print( "\nExtra PV #%i:\n", i+1); cs_print( xdrs, " Name = %s\n"); cs_print( xdrs, " Description = %s\n"); if( !xdr_short(xdrs, &type) ) return; if( (type != DBR_STRING) && (type != DBR_CTRL_CHAR) && (type != DBR_CTRL_SHORT) && (type != DBR_CTRL_LONG) && (type != DBR_CTRL_FLOAT) && (type != DBR_CTRL_DOUBLE)) { print( " Type = %i (UNKNOWN)\n", type); print( "\nExiting......\n"); exit(2); } if( type != DBR_STRING) { count = si_print( xdrs, " Count = %i\n"); cs_print( xdrs, " Unit = %s\n"); } switch(type) { case DBR_STRING: print( " Type = %i (DBR_STRING)\n", type); cs_print( xdrs, " Value = \"%s\"\n"); break; case DBR_CTRL_CHAR: { unsigned int size, maxsize; char *bytes; print( " Type = %i (DBR_CTRL_CHAR)\n", type); print( " Value%s = ", (count == 1) ? "" : "s"); maxsize = count; if( !xdr_bytes( xdrs, &(bytes), &size, maxsize ) ) return; count = size; for( j = 0; j < count; j++) { if( j) print( ", "); print( "%i", bytes[j]); } print( "\n"); } break; case DBR_CTRL_SHORT: { short int *shorts; print( " Type = %i (DBR_CTRL_SHORT\n", type); print( " Value%s = ", (count == 1) ? "" : "s"); shorts = (short int *) malloc( count * sizeof(short)); if( !xdr_vector( xdrs, (char *) shorts, count, sizeof( short), (xdrproc_t) xdr_short)) return; for( j = 0; j < count; j++) { if( j) print( ", "); print( "%i", shorts[j]); } print( "\n"); free (shorts); } break; case DBR_CTRL_LONG: { long int *longs; print( " Type = %i (DBR_CTRL_LONG)\n", type); print( " Value%s = ", (count == 1) ? "" : "s"); longs = (long int *) malloc( count * sizeof(long)); if( !xdr_vector( xdrs, (char *) longs, count, sizeof( long), (xdrproc_t) xdr_long)) return ; for( j = 0; j < count; j++) { if( j) print( ", "); print( "%li", longs[j]); } print( "\n"); free( longs); } break; case DBR_CTRL_FLOAT: { float *floats; print( " Type = %i (DBR_CTRL_FLOAT)\n", type); print( " Value%s = ", (count == 1) ? "" : "s"); floats = (float *) malloc( count * sizeof(float)); if( !xdr_vector( xdrs, (char *) floats, count, sizeof( float), (xdrproc_t) xdr_float)) return ; for( j = 0; j < count; j++) { if( j) print( ", "); print( "%.9g", floats[j]); } print( "\n"); free( floats); } break; case DBR_CTRL_DOUBLE: { double *doubles; print( " Type = %i (DBR_CTRL_DOUBLE)\n", type); print( " Value%s = ", (count == 1) ? "" : "s"); doubles = (double *) malloc( count * sizeof(double)); if( !xdr_vector( xdrs, (char *) doubles, count, sizeof( double), (xdrproc_t) xdr_double)) return; for( j = 0; j < count; j++) { if( j) print( ", "); print( "%.9lg", doubles[j]); } print( "\n"); free( doubles); } break; } } }
void mda_dump_scan( XDR *xdrs) { int i, j; short int rank, num_pos, num_det, num_trg; long int req_pts, cmp_pts; long int *offsets; offsets = NULL; rank = si_print( xdrs, "This scan's rank = %i\n"); req_pts = li_print( xdrs, "Number of requested points = %li\n"); cmp_pts = li_print( xdrs, "Last completed point = %li\n"); if( (rank <= 0) || (req_pts <= 0) || (cmp_pts < 0) ) { fflush(stdout); exit(1); } if( rank > 1) { offsets = (long int *) malloc( sizeof( long int) * req_pts); print("Offsets to lower scans = "); for( i = 0; i < req_pts; i++) { if( i) print(", "); offsets[i] = li_print( xdrs, "%li"); } print("\n"); } cs_print( xdrs, "Scan name = \"%s\"\n"); cs_print( xdrs, "Scan time = \"%s\"\n"); num_pos = si_print( xdrs, "Number of positioners = %i\n"); num_det = si_print( xdrs, "Number of detectors = %i\n"); num_trg = si_print( xdrs, "Number of triggers = %i\n"); if( num_pos < 0) { fflush(stdout); exit(1); } for( i = 0; i < num_pos; i++) { print( "\nPositioner Data Set #%i\n", i+1); si_print( xdrs, " Positioner: %i\n"); cs_print( xdrs, " Name: %s\n"); cs_print( xdrs, " Description: %s\n"); cs_print( xdrs, " Step Mode: %s\n"); cs_print( xdrs, " Unit: %s\n"); cs_print( xdrs, " Readback Name: %s\n"); cs_print( xdrs, " Readback Description: %s\n"); cs_print( xdrs, " Readback Unit: %s\n"); } if( num_det < 0) { fflush(stdout); exit(1); } for( i = 0; i < num_det; i++) { print( "\nDetector Data Set #%i\n", i+1); si_print( xdrs, " Detector: %2i\n"); cs_print( xdrs, " Name: %s\n"); cs_print( xdrs, " Description: %s\n"); cs_print( xdrs, " Unit: %s\n"); } if( num_trg < 0) { fflush(stdout); exit(1); } for( i = 0; i < num_trg; i++) { print( "\nTrigger #%i\n", i+1); si_print( xdrs, " Trigger: %i\n"); cs_print( xdrs, " Name: %s\n"); f_print( xdrs, " Command: %g\n"); } for( i = 0; i < num_pos; i++) { print( "\nPositioner #%i data:\n", i+1); for( j = 0; j < req_pts; j++) d_print( xdrs, "%.9lg "); print( "\n"); } for( i = 0; i < num_det; i++) { print( "\nDetector #%i data:\n", i+1); for( j = 0; j < req_pts; j++) f_print( xdrs, "%.9g "); print( "\n"); } if( rank > 1) for( i = 0; i < req_pts; i++) { if( offsets[i] == 0) break; print( "\n\n%i-D Subscan #%i\n", rank - 1, i+1); print( "Offset = %li\n\n", xdr_getpos( xdrs) ); mda_dump_scan( xdrs); } }
/* Returns the number of iterations */ int conjugate_gradient(cs const * A, cs const * b, cs * x, int imax, double epsilon) { int i; cs *At, *r, *d, *y, *q; double delnew, delold, del0, alpha, beta; /* Check input */ if(!CS_CSC(A) || !CS_CSC(b) || !CS_CSC(x)) REPORT_ERR(PPP_EOP); if(imax < 0 || epsilon <= 0 || epsilon >= 1) REPORT_ERR(PPP_EOP); if(A->n != x->m || A->m != b->m || x->n != 1 || b->n != 1) REPORT_ERR(PPP_EOP); if(A->n != A->m) REPORT_ERR(PPP_EOP); /* Allocate memory for all cs */ At = cs_spalloc(A->n, A->m, A->nzmax, 1, 0); r = cs_spalloc(b->m, b->n, b->m, 1, 0); d = cs_spalloc(b->m, b->n, b->m, 1, 0); y = cs_spalloc(b->m, b->n, b->m, 1, 0); q = cs_spalloc(b->m, b->n, b->m, 1, 0); At = cs_transpose(A, 1); i = 0; /* r <= b - Ax */ ppp_gaty(y, At, x); ppp_add(r, b, y, -1); /* d <= r */ ppp_copy(d, r); /* delnew <= r'r */ ppp_dotproduct(&delnew, r, r); del0 = delnew; while(i < imax && delnew > epsilon * epsilon * del0){ #if defined(LOG_ALGO) printf("[Round %d, Line %d]\n", i, 0); printf("At:\n"); cs_print(At, 0); printf("del0: %lf\n", del0); printf("x:\n"); cs_print(x, 0); printf("r:\n"); cs_print(r, 0); printf("d:\n"); cs_print(d, 0); printf("y:\n"); cs_print(y, 0); printf("q:\n"); cs_print(q, 0); printf("delnew: %lf\n", delnew); #endif /* q <= Ad */ ppp_gaty(q, At, d); #if defined(LOG_ALGO) printf("[Round %d, Line %d]\n", i, 1); printf("q:\n"); cs_print(q, 0); #endif /* alpha <= delnew / (d'q) */ ppp_dotproduct(&alpha, d, q); alpha = delnew / alpha; #if defined(LOG_ALGO) printf("[Round %d, Line %d]\n", i, 2); printf("alpha: %lf\n", alpha); #endif /* x <= x + alpha d */ ppp_add(y, x, d, alpha); ppp_copy(x, y); #if defined(LOG_ALGO) printf("[Round %d, Line %d]\n", i, 3); printf("x:\n"); cs_print(x, 0); #endif if((i+1) % 50 == 0){ /* r <= b - Ax */ ppp_gaty(y, A, x); ppp_add(r, b, y, -1); #if defined(LOG_ALGO) printf("[Round %d, Line %d]\n", i, 4); printf("r:\n"); cs_print(r, 0); #endif }else{ /* r <= r - alpha q */ ppp_add(y, r, q, -1 * alpha); ppp_copy(r, y); #if defined(LOG_ALGO) printf("[Round %d, Line %d]\n", i, 5); printf("r:\n"); cs_print(r, 0); #endif } delold = delnew; #if defined(LOG_ALGO) printf("[Round %d, Line %d]\n", i, 6); printf("delold: %lf\n", delold); #endif /* delnew = r'r */ ppp_dotproduct(&delnew, r, r); #if defined(LOG_ALGO) printf("[Round %d, Line %d]\n", i, 7); printf("delnew: %lf\n", delnew); #endif beta = delnew / delold; #if defined(LOG_ALGO) printf("[Round %d, Line %d]\n", i, 8); printf("beta: %lf\n", beta); #endif /* d <= r + beta d */ ppp_add(y, r, d, beta); ppp_copy(d, y); #if defined(LOG_ALGO) printf("[Round %d, Line %d]\n", i, 9); printf("d:\n"); cs_print(d, 0); #endif i++; } cs_spfree(At); cs_spfree(r); cs_spfree(d); cs_spfree(y); cs_spfree(q); return i; }
void computeMNASparse(){ int i,j; int b=1; int n=hashNode_num-1; sizeA = (hashNode_num-1)+m2; //Initialize and Allocate memory for A,B,x A_sparse = cs_spalloc((hashNode_num-1)+m2,(hashNode_num-1)+m2, sizeA_sparse, 1, 1); if(TRAN==1) D_sparse = cs_spalloc((hashNode_num-1)+m2,(hashNode_num-1)+m2, sizeD_sparse, 1, 1); sizeB = (hashNode_num-1)+m2; B_sparse = (double *)calloc(sizeB,sizeof(double)); x_sparse = (double *)calloc(sizeB,sizeof(double)); //Check Resistors, compute the 1st (n-1 x n-1) part of A ResistorT *runnerR=rootR; while(runnerR!=NULL) { i=atoi(ht_get(hashtable,runnerR->pos_term)); j=atoi(ht_get(hashtable,runnerR->neg_term)); if(i!=0) { cs_entry(A_sparse,i-1,i-1,1/runnerR->value); } if(j!=0) { cs_entry(A_sparse,j-1,j-1,1/runnerR->value); } if(i!=0&&j!=0) { cs_entry(A_sparse,i-1,j-1,-1/runnerR->value); cs_entry(A_sparse,j-1,i-1,-1/runnerR->value); } runnerR=runnerR->next; } //Check Voltage Sources, compute 2nd (n-1 x m2) part of A and 2nd (m2x1) part of B VoltageSourceT *runnerV=rootV; while(runnerV != NULL) { i=atoi(ht_get(hashtable,runnerV->pos_term)); //get nodes of Voltage Source j=atoi(ht_get(hashtable,runnerV->neg_term)); if(i!=0) { cs_entry(A_sparse,n-1+b,i-1,1.000); cs_entry(A_sparse,i-1,n-1+b,1.000); } if(j!=0) { cs_entry(A_sparse,n-1+b,j-1,-1.000); cs_entry(A_sparse,j-1,n-1+b,-1.000); } if((i!=0)||(j!=0)){B_sparse[n-1+b]=runnerV->value;} //Voltage value at B b++; runnerV= runnerV ->next; } //Check Inductors, compute 2nd (n-1 x m2) part of A InductorT *runnerL=rootL; while(runnerL != NULL) { i = atoi(ht_get(hashtable,runnerL->pos_term)); //get nodes of Inductor j = atoi(ht_get(hashtable,runnerL->neg_term)); if(i!=0) { cs_entry(A_sparse,n-1+b,i-1,1.000); cs_entry(A_sparse,i-1,n-1+b,1.000); } if(j!=0) { cs_entry(A_sparse,n-1+b,j-1,-1.000); cs_entry(A_sparse,j-1,n-1+b,-1.000); } if(TRAN==1){ cs_entry(D_sparse,n-1+b,n-1+b,-runnerL->value); cs_entry(D_sparse,n-1+b,n-1+b,-runnerL->value); } b++; runnerL= runnerL ->next; } //Check Current Source, compute 1st (n-1 x 1) part of B CurrentSourceT *runnerI=rootI; while(runnerI != NULL) { i = atoi(ht_get(hashtable,runnerI->pos_term)); //get nodes of Current Source j = atoi(ht_get(hashtable,runnerI->neg_term)); if(i!=0) { B_sparse[i-1]-=runnerI->value; } if(j!=0) { B_sparse[j-1]+=runnerI->value; } runnerI = runnerI ->next; } //Diatrexoume ti lista twn puknwtwn kai simplirwnoume katallila to 1o n-1 * n-1 kommati tou pinaka C if (TRAN==1){ CapacitorT *runnerC=rootC; while(runnerC!=NULL){ i=atoi(ht_get(hashtable,runnerC->pos_term)); j=atoi(ht_get(hashtable,runnerC->neg_term)); if(i!=0){ cs_entry(D_sparse,i-1,i-1,runnerC->value); } if(j!=0){ cs_entry(D_sparse,j-1,j-1,runnerC->value); } if(i!=0&&j!=0){ cs_entry(D_sparse,i-1,j-1,-runnerC->value); cs_entry(D_sparse,j-1,i-1,-runnerC->value); } runnerC=runnerC->next; } } cs_print(A_sparse, "./SparseFiles/A_Sparse.txt", 0); if (TRAN==1) cs_print(D_sparse, "./SparseFiles/D_Sparse.txt", 0); //Afou exoume ftiaksei ton A (mna) se triplet morfi, ton metatrepoume se //compressed-column morfi kai eleutherwnoume ton xwro me tin palia morfi kai //sigxwneuoume ta diaforetika non zeros pou vriskontai stin idia thesi C_sparse = cs_compress(A_sparse); cs_spfree(A_sparse); cs_dupl(C_sparse); cs_print(C_sparse, "./SparseFiles/C_Sparse.txt", 0); if (TRAN==1){ E_sparse = cs_compress(D_sparse); cs_spfree(D_sparse); cs_dupl(E_sparse); cs_print(E_sparse, "./SparseFiles/E_Sparsel.txt", 0); } }