14 void *ptr = malloc(sz);
17 printf (
"Error: can't allocate %d bytes\n", sz);
21 printf(
"allocating %d bytes to %p\n", sz, ptr);
31 r = ((high-low)*
drand48()) + low;
40 return ((high-low)*
drand48()) + low;
60 while (*str >=
'0' && *str <=
'9'){
61 v = v*10 + *str -
'0'; str++;
64 if (*str ==
'm' || *str ==
'M'){
67 else if (*str ==
'k' || *str ==
'K'){
104 if (sscanf(line,
"%s %s %s %s %s", banner, mtx, crd, data_type,
105 storage_scheme) != 5)
108 for (p=mtx; *p!=
'\0'; *p=tolower(*p),p++);
109 for (p=crd; *p!=
'\0'; *p=tolower(*p),p++);
110 for (p=data_type; *p!=
'\0'; *p=tolower(*p),p++);
111 for (p=storage_scheme; *p!=
'\0'; *p=tolower(*p),p++);
178 if (fprintf(f,
"%d %d %d\n", M, N, nz) != 3)
197 }
while (line[0] ==
'%');
200 if (sscanf(line,
"%d %d %d", M, N, nz) == 3)
206 num_items_read = fscanf(f,
"%d %d %d", M, N, nz);
209 while (num_items_read != 3);
229 }
while (line[0] ==
'%');
232 if (sscanf(line,
"%d %d %*d", M, N) == 2)
238 num_items_read = fscanf(f,
"%d %d %*d", M, N);
241 while (num_items_read != 2);
248 if (fprintf(f,
"%d %d\n", M, N) != 2)
269 if (fscanf(f,
"%d %d %lg %lg", &I[i], &J[i], &val[2*i], &val[2*i+1])
276 if (fscanf(f,
"%d %d %lg\n", &I[i], &J[i], &val[i])
285 if (fscanf(f,
"%d %d", &I[i], &J[i])
300 if (fscanf(f,
"%d %d %lg %lg", I, J, real, imag)
305 if (fscanf(f,
"%d %d %lg\n", I, J, real)
336 if (strcmp(fname,
"stdin") == 0) f=stdin;
338 if ((f = fopen(fname,
"r")) == NULL)
353 *I = (
int *) malloc(*nz *
sizeof(
int));
354 *J = (
int *) malloc(*nz *
sizeof(
int));
359 *val = (
double *) malloc(*nz * 2 *
sizeof(
double));
362 if (ret_code != 0)
return ret_code;
366 *val = (
double *) malloc(*nz *
sizeof(
double));
369 if (ret_code != 0)
return ret_code;
376 if (ret_code != 0)
return ret_code;
379 if (f != stdin) fclose(f);
402 if (strcmp(fname,
"stdout") == 0)
405 if ((f = fopen(fname,
"w")) == NULL)
413 fprintf(f,
"%d %d %d\n", M, N, nz);
418 fprintf(f,
"%d %d\n", I[i], J[i]);
422 fprintf(f,
"%d %d %20.16g\n", I[i], J[i], val[i]);
426 fprintf(f,
"%d %d %20.16g %20.16g\n", I[i], J[i], val[2*i],
430 if (f != stdout) fclose(f);
434 if (f !=stdout) fclose(f);
495 sprintf(buffer,
"%s %s %s %s", types[0], types[1], types[2], types[3]);
496 return strdup(buffer);
503 void csr2csc(
int n,
int m,
int nz,
double *a,
int *col_idx,
int *row_start,
504 double *csc_a,
int *row_idx,
int *col_start);
505 void coo2csr_in(
int n,
int nz,
double *a,
int *i_idx,
int *j_idx);
506 void coo2csr(
int n,
int nz,
double *a,
int *i_idx,
int *j_idx,
507 double *csr_a,
int *col_idx,
int *row_start);
521 int *row_start,
int *col_idx,
double *a)
526 if ((f = fopen(fn,
"w")) == NULL){
527 printf (
"can't open file <%s> \n", fn);
531 fprintf (f,
"%s %d %d %d\n",
"%", m, n, nz);
532 fprintf (f,
"%s 0\n",
"%");
535 fprintf(f,
"%d %d\n", i, row_start[i+1]-row_start[i]);
537 for (j=row_start[i]; j<row_start[i+1]; j++){
539 fprintf(f,
"%d %20.19g\n", col_idx[j], a[j]);
541 fprintf(f,
"%d\n", col_idx[j]);
554 int **i_idx,
int **j_idx,
double **a)
561 if ((f = fopen(fn,
"r")) == NULL) {
562 printf (
"can't open file <%s> \n", fn);
566 printf(
"Could not process Matrix Market banner.\n");
574 printf(
"Sorry, this application does not support ");
581 fscanf(f,
"%*s %d %d %d", m, n, nz);
592 *i_idx = (
int *)
my_malloc(*nz *
sizeof(
int));
593 *j_idx = (
int *)
my_malloc(*nz *
sizeof(
int));
594 *a = (
double *)
my_malloc(*nz *
sizeof(
double));
596 for (i=0; i<*nz; i++) {
603 if (!(*i_idx) || !(*j_idx) || !(*a)){
604 printf (
"cannot allocate memory for %d, %d, %d sparse matrix\n", *m, *n, *nz);
613 for (i=0; i<*nz; i++) {
615 fscanf(f,
"%d %d", &(*i_idx)[i], &(*j_idx)[i]);
639 for (i=0; i<*nz; i++) {
648 #define INPUT_WIDTH 80
651 int **row_start,
int **col_idx,
double **a)
660 int *col_start, *row_idx;
664 int start_input_repeat, start_input_width;
665 int idx_input_repeat, idx_input_width;
666 int start_lines, idx_lines, l;
668 if ((f = fopen(fn,
"r")) == NULL) {
669 printf (
"can't open file <%s> \n", fn);
674 sscanf(buffer,
"%*d %d %d %*d %d", &start_lines, &idx_lines, &rhs);
676 sscanf(buffer,
"%s %d %d %d", mat_format, m, n, nz);
677 printf(
"type=%s m=%d n=%d nz=%d rhs=%d\n", mat_format, *m, *n, *nz, rhs);
679 sscanf(buffer,
"%s %s", start_format, idx_format);
680 sscanf(start_format,
"(%d%*c%d)", &start_input_repeat, &start_input_width);
681 sscanf(idx_format,
"(%d%*c%d)", &idx_input_repeat, &idx_input_width);
682 printf(
"%d start_input lines %d repeats %d: %d col_input lines %d repeats %d\n",
683 start_lines, start_input_width, start_input_repeat,
684 idx_lines, idx_input_width, idx_input_repeat);
693 switch (mat_format[0]){
707 printf (
"<%s> is not Harwell Boeing matrix format %s\n", fn, mat_format);
711 switch (mat_format[1]){
731 printf (
"<%s> is not Harwell Boeing matrix format %s\n", fn, mat_format);
735 switch (mat_format[2]){
741 printf (
"<%s> is not unassembled Harwell Boeing matrix format %s\n",
745 printf (
"<%s> is not Harwell Boeing matrix format %s\n", fn, mat_format);
750 *row_start = (
int *)
my_malloc((*m+1) *
sizeof(int));
754 i_idx = (
int *)
my_malloc(*nz *2 *
sizeof(
int));
755 j_idx = (
int *)
my_malloc(*nz *2 *
sizeof(
int));
756 coo_a = (
double *)
my_malloc(*nz *2 *
sizeof(
double));
759 col_start = (
int *)
my_malloc((*n+1) *
sizeof(int));
760 row_idx = (
int *)
my_malloc(*nz *
sizeof(
int));
762 *col_idx = (
int *)
my_malloc(*nz *
sizeof(
int));
763 *a = (
double *)
my_malloc(*nz *
sizeof(
double));
768 for (i=l=0; l<start_lines; l++){
770 for (field=0; (field<start_input_repeat) && (i<=*m); field++){
771 for(j=0; j<start_input_width; j++)
772 start_format[j] = buffer[field*start_input_width+j];
775 (*row_start)[i++] = atoi(start_format)-1;
779 for (i=l=k=row=0; l<idx_lines; l++){
781 for (field=0; (field<idx_input_repeat) && (i<*nz); field++, i++){
782 for(j=0; j<idx_input_width; j++)
783 idx_format[j] = buffer[field*idx_input_width+j];
786 if (i==(*row_start)[row+1]) row++;
788 j_idx[i] = atoi(idx_format)-1;
792 if ( i_idx[i] != j_idx[i] ){
793 i_idx[*nz+k] = j_idx[i];
794 j_idx[*nz+k] = i_idx[i];
795 coo_a[*nz+k] = coo_a[i];
802 *col_idx = (
int *)
my_malloc(*nz *
sizeof(
int));
803 *a = (
double *)
my_malloc(*nz *
sizeof(
double));
804 coo2csr (*m, *nz, coo_a, i_idx, j_idx, *a, *col_idx, *row_start);
812 for (i=l=0; l<start_lines; l++){
814 for (field=0; (field<start_input_repeat) && (i<=*n); field++){
815 for(j=0; j<start_input_width; j++)
816 start_format[j] = buffer[field*start_input_width+j];
819 col_start[i++] = atoi(start_format)-1;
824 for (i=l=0; l<idx_lines; l++){
826 for (field=0; (field<idx_input_repeat) && (i<*nz); field++, i++){
827 for(j=0; j<idx_input_width; j++)
828 idx_format[j] = buffer[field*idx_input_width+j];
831 row_idx[i] = atoi(idx_format)-1;
837 csr2csc(*n, *m, *nz, NULL, row_idx, col_start, NULL, *col_idx, *row_start);
848 void sort(
int *col_idx,
double *a,
int start,
int end)
853 for (i=end-1; i>start; i--)
854 for(j=start; j<i; j++)
855 if (col_idx[j] > col_idx[j+1]){
863 col_idx[j]=col_idx[j+1];
875 void coo2csr_in(
int n,
int nz,
double *a,
int *i_idx,
int *j_idx)
879 int init, i_next, j_next, i_pos;
882 row_start = (
int *)malloc((n+1)*
sizeof(int));
884 printf (
"coo2csr_in: cannot allocate temporary memory\n");
887 for (i=0; i<=n; i++) row_start[i] = 0;
890 printf(
"\n idx \n" );
891 for (i=0; i<nz; i++){
892 printf(
"\n %d", i_idx[i]);
897 for (i=0; i<nz; i++) {
899 row_start[i_idx[i]+1]++;
905 printf(
"\n row Start \n" );
907 printf(
"\n %d", row_start[i]);
911 for (i=0; i<n; i++) row_start[i+1] += row_start[i];
915 printf(
"\n row Start \n" );
918 printf(
"\n %d", row_start[i]);
923 for (init=0; init<nz; ){
930 i_pos = row_start[i];
932 i_next = i_idx[i_pos];
933 j_next = j_idx[i_pos];
939 if (i_next < 0)
break;
946 while ((i_idx[init] < 0) && (init < nz)) init++;
951 for (i=0; i<n; i++) i_idx[i+1] = row_start[i];
955 printf(
"\n row Start \n" );
958 printf(
"\n %d", i_idx[i]);
962 sort (j_idx, a, i_idx[i], i_idx[i+1]);
972 void coo2csr(
int n,
int nz,
double *a,
int *i_idx,
int *j_idx,
973 double *csr_a,
int *col_idx,
int *row_start)
977 for (i=0; i<=n; i++) row_start[i] = 0;
980 for (i=0; i<nz; i++) row_start[i_idx[i]+1]++;
983 for (i=0; i<n; i++) row_start[i+1] += row_start[i];
987 for (l=0; l<nz; l++){
988 i = row_start[i_idx[l]];
990 col_idx[i] = j_idx[l];
991 row_start[i_idx[l]]++;
995 for (i=n; i>0; i--) row_start[i] = row_start[i-1];
1000 sort (col_idx, csr_a, row_start[i], row_start[i+1]);
1012 void csr2csc(
int n,
int m,
int nz,
double *a,
int *col_idx,
int *row_start,
1013 double *csc_a,
int *row_idx,
int *col_start)
1018 for (i=0; i<=
m; i++) col_start[i] = 0;
1021 for (i=0; i<nz; i++) col_start[col_idx[i]+1]++;
1023 for (i=0; i<
m; i++) col_start[i+1] += col_start[i];
1028 for (i=0, ptr=row_start; i<n; i++, ptr++)
1029 for (j=*ptr; j<*(ptr+1); j++){
1033 if (a) csc_a[l] = a[j];
1037 for (i=m; i>0; i--) col_start[i] = col_start[i-1];
int mm_write_mtx_crd_size(FILE *f, int M, int N, int nz)
#define mm_is_pattern(typecode)
int mm_write_mtx_crd(char fname[], int M, int N, int nz, int I[], int J[], double val[], MM_typecode matcode)
#define MM_MAX_LINE_LENGTH
#define mm_clear_typecode(typecode)
#define mm_set_real(typecode)
#define mm_set_pattern(typecode)
#define mm_set_coordinate(typecode)
#define MM_UNSUPPORTED_TYPE
#define mm_set_sparserow(typecode)
int random_integer(int low, int high)
#define mm_set_complex(typecode)
int mm_write_banner(FILE *f, MM_typecode matcode)
#define mm_set_matrix(typecode)
int mm_read_mtx_array_size(FILE *f, int *M, int *N)
#define mm_set_dense(typecode)
#define mm_is_dense(typecode)
#define mm_is_sparserow(typecode)
#define MM_COORDINATE_STR
void read_hb_matrix(char *fn, int *m, int *n, int *nz, int **row_start, int **col_idx, double **a)
#define mm_is_skew(typecode)
#define MatrixMarketBanner
int mm_read_mtx_crd_data(FILE *f, int M, int N, int nz, int I[], int J[], double val[], MM_typecode matcode)
int mm_read_mtx_crd_entry(FILE *f, int *I, int *J, double *real, double *imag, MM_typecode matcode)
void coo2csr(int n, int nz, double *a, int *i_idx, int *j_idx, double *csr_a, int *col_idx, int *row_start)
#define mm_set_hermitian(typecode)
#define MM_MAX_TOKEN_LENGTH
void write_csr(char *fn, int m, int n, int nz, int *row_start, int *col_idx, double *a)
int mm_write_mtx_array_size(FILE *f, int M, int N)
double random_double(double low, double high)
#define mm_set_skew(typecode)
#define mm_set_general(typecode)
void coo2csr_in(int n, int nz, double *a, int *i_idx, int *j_idx)
char * mm_typecode_to_str(MM_typecode matcode)
#define mm_is_general(typecode)
#define mm_is_real(typecode)
int mm_read_banner(FILE *f, MM_typecode *matcode)
#define mm_is_coordinate(typecode)
#define mm_is_sparse(typecode)
#define mm_is_hermitian(typecode)
void sort(int *col_idx, double *a, int start, int end)
#define MM_COULD_NOT_READ_FILE
void read_mm_matrix(char *fn, int *m, int *n, int *nz, int **i_idx, int **j_idx, double **a)
int mm_read_mtx_crd(char *fname, int *M, int *N, int *nz, int **I, int **J, double **val, MM_typecode *matcode)
int str_to_mem_unit(char *str)
#define mm_is_integer(typecode)
#define mm_is_matrix(typecode)
int mm_is_valid(MM_typecode matcode)
#define mm_set_symmetric(typecode)
#define MM_COULD_NOT_WRITE_FILE
#define mm_is_complex(typecode)
void csr2csc(int n, int m, int nz, double *a, int *col_idx, int *row_start, double *csc_a, int *row_idx, int *col_start)
#define mm_is_symmetric(typecode)
int mm_read_mtx_crd_size(FILE *f, int *M, int *N, int *nz)
#define mm_set_integer(typecode)