void taucs_dtl(vec_axpby) (int n, taucs_real_datatype a, taucs_datatype * x, taucs_real_datatype b, taucs_datatype * y, taucs_datatype * axpby) { int i; for (i = 0; i < n; i++) { #ifdef TAUCS_CORE_COMPLEX axpby[i] = taucs_complex_create(a * taucs_re(x[i]) + b * taucs_re(y[i]), a * taucs_im(x[i]) + b * taucs_im(y[i])); /* * taucs_re(axpby[i]) = a * taucs_re(x[i]) + b * taucs_re(y[i]); taucs_im(axpby[i]) = a * taucs_im(x[i]) + b * * taucs_im(y[i]); */ #else axpby[i] = a * x[i] + b * y[i]; #endif } }
taucs_ccs_matrix* taucs_dtl(ccs_read_ccs)(char* filename,int flags) { FILE* f; taucs_ccs_matrix* m; /* int* clen; int* is; int* js; taucs_datatype* vs; int ncols, nrows, nnz; int i,ip,j,k,n; */ /* taucs_datatype dv;*/ /* double di,dj;*/ int i,ip,j,N,*pointers; f = fopen(filename ,"r"); if (f == NULL) { taucs_printf("taucs_ccs_read_ccs: could not open ccs file %s\n",filename); return NULL; } fscanf(f,"%d",&N); pointers = (int*) taucs_malloc((N+1)*sizeof(int)); for(i=0; i<N+1; ++i) { fscanf(f,"%d",&pointers[i]); } m = taucs_dtl(ccs_create)(N, N, pointers[N]); for (i=0; i<=N; i++) (m->colptr)[i] = pointers[i]; for(i=0; i<pointers[N]; ++i) fscanf(f,"%d",(m->rowind)+i); #ifdef TAUCS_CORE_DOUBLE for(i=0; i<pointers[N]; ++i) fscanf(f,"%lg",(m->taucs_values)+i); #endif #ifdef TAUCS_CORE_SINGLE for(i=0; i<pointers[N]; ++i) fscanf(f,"%g",(m->taucs_values)+i); #endif #ifdef TAUCS_CORE_DCOMPLEX for(i=0; i<pointers[N]; ++i) { taucs_real_datatype dv_r; taucs_real_datatype dv_i; fscanf(f,"%lg+%lgi",&dv_r,&dv_i); (m->taucs_values)[i] = taucs_complex_create(dv_r,dv_i); } #endif #ifdef TAUCS_CORE_SCOMPLEX for(i=0; i<pointers[N]; ++i) { taucs_real_datatype dv_r; taucs_real_datatype dv_i; fscanf(f,"%g+%gi",&dv_r,&dv_i); (m->taucs_values)[i] = taucs_complex_create(dv_r,dv_i); } #endif if (flags & TAUCS_SYMMETRIC) { m->flags = TAUCS_SYMMETRIC | TAUCS_LOWER; for (j=0; j<N; j++) { for (ip=(m->colptr)[j]; ip<(m->colptr)[j+1]; ip++) { i = (m->rowind)[ip]; assert(i >= j); } } } else m->flags = 0; #ifdef TAUCS_CORE_DOUBLE m->flags |= TAUCS_DOUBLE; #endif #ifdef TAUCS_CORE_SINGLE m->flags |= TAUCS_SINGLE; #endif #ifdef TAUCS_CORE_DCOMPLEX m->flags |= TAUCS_DCOMPLEX; #endif #ifdef TAUCS_CORE_SCOMPLEX m->flags |= TAUCS_SCOMPLEX; #endif taucs_free(pointers); taucs_printf("taucs_ccs_read_ccs: read %s, n=%d\n",filename,m->n); return m; }
taucs_ccs_matrix* taucs_dtl(ccs_read_mtx)(char* filename,int flags) { FILE* f; taucs_ccs_matrix* m; int* clen; int* is; int* js; taucs_datatype* vs; int ncols, nrows, nnz; int i,j,k,n; double di,dj; taucs_datatype dv; f = fopen (filename , "r"); if (f == NULL) { taucs_printf("taucs_ccs_read_mtx: could not open mtx file %s\n",filename); return NULL; } if (fscanf(f, "%d %d %d", &nrows, &ncols, &nnz) != 3) { taucs_printf("taucs_ccs_read_mtx: wrong header\n"); return NULL; } n = 10000; is = (int*) taucs_malloc(n*sizeof(int)); js = (int*) taucs_malloc(n*sizeof(int)); vs = (taucs_datatype*) taucs_malloc(n*sizeof(taucs_datatype)); if (!is || !js || !vs) { taucs_printf("taucs_ccs_read_mtx: out of memory\n"); taucs_free(is); taucs_free(js); taucs_free(vs); return NULL; } nnz = 0; nrows = ncols = 0; while (!feof(f)) { if (nnz == n) { n = (int) ( 1.25 * (double) n); taucs_printf("taucs_ccs_read_mtx: allocating %d ijv's\n",n); is = (int*) taucs_realloc(is,n*sizeof(int)); js = (int*) taucs_realloc(js,n*sizeof(int)); vs = (taucs_datatype*) taucs_realloc(vs,n*sizeof(taucs_datatype)); if (!is || !js || !vs) { taucs_printf("taucs_ccs_read_mtx: out of memory\n"); taucs_free(is); taucs_free(js); taucs_free(vs); return NULL; } } #ifdef TAUCS_CORE_DOUBLE if (fscanf(f, "%lg %lg %lg", &di, &dj, &dv) != 3) break; #endif #ifdef TAUCS_CORE_SINGLE if (fscanf(f, "%lg %lg %g", &di, &dj, &dv) != 3) break; #endif #ifdef TAUCS_CORE_COMPLEX { taucs_real_datatype dv_i; taucs_real_datatype dv_r; #ifdef TAUCS_CORE_DCOMPLEX if (fscanf(f, "%lg %lg %lg+%lgi", &di, &dj, &dv_r,&dv_i) != 4) break; #endif #ifdef TAUCS_CORE_SCOMPLEX if (fscanf(f, "%lg %lg %g+%gi", &di, &dj, &dv_r,&dv_i) != 4) break; #endif dv = taucs_complex_create(dv_r,dv_i); } #endif is[nnz] = (int)di; js[nnz] = (int)dj; vs[nnz] = dv;/*omer*/ /* upper or lower might be stored, we use lower */ if ((flags & TAUCS_SYMMETRIC) && is[nnz] < js[nnz]) { int t = is[nnz]; is[nnz] = js[nnz]; js[nnz] = t; } if (flags & TAUCS_PATTERN) { #ifdef TAUCS_CORE_DOUBLE if (is[nnz] == js[nnz]) vs[nnz] = (double) (nrows+1); else vs[nnz] = -1.0; #endif #ifdef TAUCS_CORE_SINGLE if (is[nnz] == js[nnz]) vs[nnz] = (float) (nrows+1); else vs[nnz] = -1.0; #endif #ifdef TAUCS_CORE_DCOMPEX assert(0); #endif #ifdef TAUCS_CORE_SCOMPLEX assert(0); #endif } nrows = max(is[nnz],nrows); ncols = max(js[nnz],ncols); nnz++; } fclose ( f ); m = (taucs_ccs_matrix*) taucs_malloc(sizeof(taucs_ccs_matrix)); if (!m) { taucs_printf("taucs_ccs_read_mtx: out of memory\n"); taucs_free(is); taucs_free(js); taucs_free(vs); return NULL; } m->n = nrows; m->m = ncols; if (flags & TAUCS_SYMMETRIC) m->flags = TAUCS_SYMMETRIC | TAUCS_LOWER; else m->flags = 0; #ifdef TAUCS_CORE_DOUBLE m->flags |= TAUCS_DOUBLE; #endif #ifdef TAUCS_CORE_SINGLE m->flags |= TAUCS_SINGLE; #endif #ifdef TAUCS_CORE_DCOMPLEX m->flags |= TAUCS_DCOMPLEX; #endif #ifdef TAUCS_CORE_SCOMPLEX m->flags |= TAUCS_SCOMPLEX; #endif clen = (int*) taucs_malloc((ncols+1) * sizeof(int)); m->colptr = (int*) taucs_malloc((ncols+1) * sizeof(int)); m->rowind = (int*) taucs_malloc(nnz * sizeof(int)); m->taucs_values = (taucs_datatype*) taucs_malloc(nnz * sizeof(taucs_datatype)); if (!clen || !(m->colptr) || !(m->rowind) || !(m->rowind)) { taucs_printf("taucs_ccs_read_mtx: out of memory: ncols=%d nnz=%d\n",ncols,nnz); taucs_free(clen); taucs_free(m->colptr); taucs_free(m->rowind); taucs_free(m->taucs_values); taucs_free (m); taucs_free(is); taucs_free(js); taucs_free(vs); return NULL; } for (j=0; j<ncols; j++) clen[j] = 0; for (k=0; k<nnz; k++) { i = is[k] - 1; /* make it 1-based */ j = js[k] - 1; /* make it 1-based */ ( clen[j] )++; } /* just check */ k = 0; for (j=0; j<ncols; j++) k += clen[j]; assert(k == nnz); /* now compute column pointers */ k = 0; for (j=0; j<ncols; j++) { int tmp; tmp = clen[j]; clen[j] = (m->colptr[j]) = k; k += tmp; } clen[ncols] = (m->colptr[ncols]) = k; assert(clen[ncols] == nnz); /* now read matrix into data structure */ for (k=0; k<nnz; k++) { i = is[k] - 1; /* make it 1-based */ j = js[k] - 1; /* make it 1-based */ assert(i < nrows); assert(j < ncols); (m->taucs_values)[ clen[j] ] = vs[k]; (m->rowind)[ clen[j] ] = i; clen[j] ++; } taucs_free(clen); taucs_free(vs); taucs_free(js); taucs_free(is); taucs_printf("taucs_ccs_read_mtx: read %s, n=%d\n",filename,m->n); return m; }
taucs_ccs_matrix* taucs_dtl(ccs_read_ijv)(char* ijvfilename,int flags,int base) { /* omer - base is 0 when the ijv file is 1-based and 1 when the ijv file is 0-based. we add base to the i's and j's found.*/ FILE* f; taucs_ccs_matrix* m; int* clen; int* is; int* js; taucs_datatype* vs; int ncols, nrows, nnz; int i,j,k,n; double di,dj; taucs_datatype dv; f = fopen (ijvfilename , "r"); if (f == NULL) { taucs_printf("taucs_ccs_read_ijv: could not open ijv file %s\n",ijvfilename); return NULL; } n = 10000; is = (int*) taucs_malloc(n*sizeof(int)); js = (int*) taucs_malloc(n*sizeof(int)); vs = (taucs_datatype*) taucs_malloc(n*sizeof(taucs_datatype)); if (!is || !js || !vs) { taucs_printf("symccs_read_ijv: out of memory\n"); taucs_free(is); taucs_free(js); taucs_free(vs); return NULL; } nnz = 0; nrows = ncols = 0; while (!feof(f)) { if (nnz == n) { n = (int) ( 1.25 * (double) n); taucs_printf("taucs_ccs_read_ijv: allocating %d ijv's\n",n); is = (int*) taucs_realloc(is,n*sizeof(int)); js = (int*) taucs_realloc(js,n*sizeof(int)); vs = (taucs_datatype*) taucs_realloc(vs,n*sizeof(taucs_datatype)); if (!is || !js || !vs) { taucs_printf("taucs_ccs_read_ijv: out of memory\n"); taucs_free(is); taucs_free(js); taucs_free(vs); return NULL; } } #ifdef TAUCS_CORE_DOUBLE if (fscanf(f, "%lg %lg %lg", &di, &dj, &dv) != 3) break; #endif #ifdef TAUCS_CORE_SINGLE if (fscanf(f, "%lg %lg %g", &di, &dj, &dv) != 3) break; #endif #ifdef TAUCS_CORE_COMPLEX { taucs_real_datatype dv_i; taucs_real_datatype dv_r; #ifdef TAUCS_CORE_DCOMPLEX if (fscanf(f, "%lg %lg %lg+%lgi", &di, &dj, &dv_r,&dv_i) != 4) break; #endif #ifdef TAUCS_CORE_SCOMPLEX if (fscanf(f, "%lg %lg %g+%gi", &di, &dj, &dv_r, &dv_i) != 4) break; #endif dv = taucs_complex_create(dv_r,dv_i); } #endif is[nnz] = (int)di+base; js[nnz] = (int)dj+base; vs[nnz] = dv;/*omer*/ /* we read the lower part */ if ((flags & TAUCS_SYMMETRIC) && is[nnz] < js[nnz]) continue; if ((flags & TAUCS_HERMITIAN) && is[nnz] < js[nnz]) continue; nrows = max(is[nnz],nrows); ncols = max(js[nnz],ncols); nnz++; } fclose ( f ); m = (taucs_ccs_matrix*) taucs_malloc(sizeof(taucs_ccs_matrix)); if (!m) { taucs_printf("taucs_ccs_read_ijv: out of memory\n"); taucs_free(is); taucs_free(js); taucs_free(vs); return NULL; } m->n = ncols; m->m = nrows; m->flags = 0; if (flags & TAUCS_SYMMETRIC) m->flags = TAUCS_SYMMETRIC | TAUCS_LOWER; if (flags & TAUCS_HERMITIAN) m->flags = TAUCS_HERMITIAN | TAUCS_LOWER; #ifdef TAUCS_CORE_DOUBLE m->flags |= TAUCS_DOUBLE; #endif #ifdef TAUCS_CORE_SINGLE m->flags |= TAUCS_SINGLE; #endif #ifdef TAUCS_CORE_DCOMPLEX m->flags |= TAUCS_DCOMPLEX; #endif #ifdef TAUCS_CORE_SCOMPLEX m->flags |= TAUCS_SCOMPLEX; #endif clen = (int*) taucs_malloc((ncols+1) * sizeof(int)); m->colptr = (int*) taucs_malloc((ncols+1) * sizeof(int)); m->rowind = (int*) taucs_malloc(nnz * sizeof(int)); m->taucs_values = (taucs_datatype*) taucs_malloc(nnz * sizeof(taucs_datatype)); if (!clen || !(m->colptr) || !(m->rowind) || !(m->rowind)) { taucs_printf("taucs_ccs_read_ijv: out of memory: ncols=%d nnz=%d\n",ncols,nnz); taucs_free(clen); taucs_free(m->colptr); taucs_free(m->rowind); taucs_free(m->taucs_values); taucs_free (m); taucs_free(is); taucs_free(js); taucs_free(vs); return NULL; } for (j=0; j<ncols; j++) clen[j] = 0; for (k=0; k<nnz; k++) { i = is[k] - 1; /* make it 1-based */ j = js[k] - 1; /* make it 1-based */ if ( j<0 || j>ncols ) taucs_printf("j=%d k=%d\n",j,k); ( clen[j] )++; } /* just check */ k = 0; for (j=0; j<ncols; j++) k += clen[j]; if (k!=nnz) taucs_printf("k=%d, nnz=%d\n",k,nnz); assert(k == nnz); /* now compute column pointers */ k = 0; for (j=0; j<ncols; j++) { int tmp; tmp = clen[j]; clen[j] = (m->colptr[j]) = k; k += tmp; } clen[ncols] = (m->colptr[ncols]) = k; assert(clen[ncols] == nnz); /* now read matrix into data structure */ for (k=0; k<nnz; k++) { i = is[k] - 1; /* make it 1-based */ j = js[k] - 1; /* make it 1-based */ assert(i < nrows); assert(j < ncols); (m->taucs_values)[ clen[j] ] = vs[k]; (m->rowind)[ clen[j] ] = i; clen[j] ++; } taucs_free(clen); taucs_free(vs); taucs_free(js); taucs_free(is); taucs_printf("taucs_ccs_read_ijv: read %s, n=%d\n",ijvfilename,m->n); return m; }