mmio.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010 Centre National de la Recherche Scientifique
  4. *
  5. * StarPU is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * StarPU is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. /*
  17. * Matrix Market I/O library for ANSI C
  18. *
  19. * See http://math.nist.gov/MatrixMarket for details.
  20. *
  21. *
  22. */
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include <stdlib.h>
  26. #include <ctype.h>
  27. #include "mmio.h"
  28. int mm_read_unsymmetric_sparse(const char *fname, int *M_, int *N_, int *nz_,
  29. double **val_, int **I_, int **J_)
  30. {
  31. FILE *f;
  32. MM_typecode matcode;
  33. int M, N, nz;
  34. int i;
  35. double *val;
  36. int *I, *J;
  37. if ((f = fopen(fname, "r")) == NULL)
  38. return -1;
  39. if (mm_read_banner(f, &matcode) != 0)
  40. {
  41. printf("mm_read_unsymetric: Could not process Matrix Market banner ");
  42. printf(" in file [%s]\n", fname);
  43. return -1;
  44. }
  45. if ( !(mm_is_real(matcode) && mm_is_matrix(matcode) &&
  46. mm_is_sparse(matcode)))
  47. {
  48. fprintf(stderr, "Sorry, this application does not support ");
  49. fprintf(stderr, "Market Market type: [%s]\n",
  50. mm_typecode_to_str(matcode));
  51. return -1;
  52. }
  53. /* find out size of sparse matrix: M, N, nz .... */
  54. if (mm_read_mtx_crd_size(f, &M, &N, &nz) !=0)
  55. {
  56. fprintf(stderr, "read_unsymmetric_sparse(): could not parse matrix size.\n");
  57. return -1;
  58. }
  59. *M_ = M;
  60. *N_ = N;
  61. *nz_ = nz;
  62. /* reseve memory for matrices */
  63. I = (int *) malloc(nz * sizeof(int));
  64. J = (int *) malloc(nz * sizeof(int));
  65. val = (double *) malloc(nz * sizeof(double));
  66. *val_ = val;
  67. *I_ = I;
  68. *J_ = J;
  69. /* NOTE: when reading in doubles, ANSI C requires the use of the "l" */
  70. /* specifier as in "%lg", "%lf", "%le", otherwise errors will occur */
  71. /* (ANSI C X3.159-1989, Sec. 4.9.6.2, p. 136 lines 13-15) */
  72. for (i=0; i<nz; i++)
  73. {
  74. fscanf(f, "%d %d %lg\n", &I[i], &J[i], &val[i]);
  75. I[i]--; /* adjust from 1-based to 0-based */
  76. J[i]--;
  77. }
  78. fclose(f);
  79. return 0;
  80. }
  81. int mm_is_valid(MM_typecode matcode)
  82. {
  83. if (!mm_is_matrix(matcode)) return 0;
  84. if (mm_is_dense(matcode) && mm_is_pattern(matcode)) return 0;
  85. if (mm_is_real(matcode) && mm_is_hermitian(matcode)) return 0;
  86. if (mm_is_pattern(matcode) && (mm_is_hermitian(matcode) ||
  87. mm_is_skew(matcode))) return 0;
  88. return 1;
  89. }
  90. int mm_read_banner(FILE *f, MM_typecode *matcode)
  91. {
  92. char line[MM_MAX_LINE_LENGTH];
  93. char banner[MM_MAX_TOKEN_LENGTH];
  94. char mtx[MM_MAX_TOKEN_LENGTH];
  95. char crd[MM_MAX_TOKEN_LENGTH];
  96. char data_type[MM_MAX_TOKEN_LENGTH];
  97. char storage_scheme[MM_MAX_TOKEN_LENGTH];
  98. char *p;
  99. mm_clear_typecode(matcode);
  100. if (fgets(line, MM_MAX_LINE_LENGTH, f) == NULL)
  101. return MM_PREMATURE_EOF;
  102. if (sscanf(line, "%s %s %s %s %s", banner, mtx, crd, data_type,
  103. storage_scheme) != 5)
  104. return MM_PREMATURE_EOF;
  105. for (p=mtx; *p!='\0'; *p=tolower(*p),p++); /* convert to lower case */
  106. for (p=crd; *p!='\0'; *p=tolower(*p),p++);
  107. for (p=data_type; *p!='\0'; *p=tolower(*p),p++);
  108. for (p=storage_scheme; *p!='\0'; *p=tolower(*p),p++);
  109. /* check for banner */
  110. if (strncmp(banner, MatrixMarketBanner, strlen(MatrixMarketBanner)) != 0)
  111. return MM_NO_HEADER;
  112. /* first field should be "mtx" */
  113. if (strcmp(mtx, MM_MTX_STR) != 0)
  114. return MM_UNSUPPORTED_TYPE;
  115. mm_set_matrix(matcode);
  116. /* second field describes whether this is a sparse matrix (in coordinate
  117. storgae) or a dense array */
  118. if (strcmp(crd, MM_SPARSE_STR) == 0)
  119. mm_set_sparse(matcode);
  120. else
  121. if (strcmp(crd, MM_DENSE_STR) == 0)
  122. mm_set_dense(matcode);
  123. else
  124. return MM_UNSUPPORTED_TYPE;
  125. /* third field */
  126. if (strcmp(data_type, MM_REAL_STR) == 0)
  127. mm_set_real(matcode);
  128. else
  129. if (strcmp(data_type, MM_COMPLEX_STR) == 0)
  130. mm_set_complex(matcode);
  131. else
  132. if (strcmp(data_type, MM_PATTERN_STR) == 0)
  133. mm_set_pattern(matcode);
  134. else
  135. if (strcmp(data_type, MM_INT_STR) == 0)
  136. mm_set_integer(matcode);
  137. else
  138. return MM_UNSUPPORTED_TYPE;
  139. /* fourth field */
  140. if (strcmp(storage_scheme, MM_GENERAL_STR) == 0)
  141. mm_set_general(matcode);
  142. else
  143. if (strcmp(storage_scheme, MM_SYMM_STR) == 0)
  144. mm_set_symmetric(matcode);
  145. else
  146. if (strcmp(storage_scheme, MM_HERM_STR) == 0)
  147. mm_set_hermitian(matcode);
  148. else
  149. if (strcmp(storage_scheme, MM_SKEW_STR) == 0)
  150. mm_set_skew(matcode);
  151. else
  152. return MM_UNSUPPORTED_TYPE;
  153. return 0;
  154. }
  155. int mm_write_mtx_crd_size(FILE *f, int M, int N, int nz)
  156. {
  157. if (fprintf(f, "%d %d %d\n", M, N, nz) != 3)
  158. return MM_COULD_NOT_WRITE_FILE;
  159. else
  160. return 0;
  161. }
  162. int mm_read_mtx_crd_size(FILE *f, int *M, int *N, int *nz )
  163. {
  164. char line[MM_MAX_LINE_LENGTH];
  165. int num_items_read;
  166. /* set return null parameter values, in case we exit with errors */
  167. *M = *N = *nz = 0;
  168. /* now continue scanning until you reach the end-of-comments */
  169. do
  170. {
  171. if (fgets(line,MM_MAX_LINE_LENGTH,f) == NULL)
  172. return MM_PREMATURE_EOF;
  173. }while (line[0] == '%');
  174. /* line[] is either blank or has M,N, nz */
  175. if (sscanf(line, "%d %d %d", M, N, nz) == 3)
  176. return 0;
  177. else
  178. do
  179. {
  180. num_items_read = fscanf(f, "%d %d %d", M, N, nz);
  181. if (num_items_read == EOF) return MM_PREMATURE_EOF;
  182. }
  183. while (num_items_read != 3);
  184. return 0;
  185. }
  186. int mm_read_mtx_array_size(FILE *f, int *M, int *N)
  187. {
  188. char line[MM_MAX_LINE_LENGTH];
  189. int num_items_read;
  190. /* set return null parameter values, in case we exit with errors */
  191. *M = *N = 0;
  192. /* now continue scanning until you reach the end-of-comments */
  193. do
  194. {
  195. if (fgets(line,MM_MAX_LINE_LENGTH,f) == NULL)
  196. return MM_PREMATURE_EOF;
  197. }while (line[0] == '%');
  198. /* line[] is either blank or has M,N, nz */
  199. if (sscanf(line, "%d %d", M, N) == 2)
  200. return 0;
  201. else /* we have a blank line */
  202. do
  203. {
  204. num_items_read = fscanf(f, "%d %d", M, N);
  205. if (num_items_read == EOF) return MM_PREMATURE_EOF;
  206. }
  207. while (num_items_read != 2);
  208. return 0;
  209. }
  210. int mm_write_mtx_array_size(FILE *f, int M, int N)
  211. {
  212. if (fprintf(f, "%d %d\n", M, N) != 2)
  213. return MM_COULD_NOT_WRITE_FILE;
  214. else
  215. return 0;
  216. }
  217. /*-------------------------------------------------------------------------*/
  218. /******************************************************************/
  219. /* use when I[], J[], and val[]J, and val[] are already allocated */
  220. /******************************************************************/
  221. int mm_read_mtx_crd_data(FILE *f, int M __attribute__ ((unused)) ,
  222. int N __attribute__ ((unused)) , int nz, int I[], int J[],
  223. double val[], MM_typecode matcode)
  224. {
  225. int i;
  226. if (mm_is_complex(matcode))
  227. {
  228. for (i=0; i<nz; i++)
  229. if (fscanf(f, "%d %d %lg %lg", &I[i], &J[i], &val[2*i], &val[2*i+1])
  230. != 4) return MM_PREMATURE_EOF;
  231. }
  232. else if (mm_is_real(matcode))
  233. {
  234. for (i=0; i<nz; i++)
  235. {
  236. if (fscanf(f, "%d %d %lg\n", &I[i], &J[i], &val[i])
  237. != 3) return MM_PREMATURE_EOF;
  238. }
  239. }
  240. else if (mm_is_pattern(matcode))
  241. {
  242. for (i=0; i<nz; i++)
  243. if (fscanf(f, "%d %d", &I[i], &J[i])
  244. != 2) return MM_PREMATURE_EOF;
  245. }
  246. else
  247. return MM_UNSUPPORTED_TYPE;
  248. return 0;
  249. }
  250. int mm_read_mtx_crd_entry(FILE *f, int *I, int *J,
  251. double *real, double *imag, MM_typecode matcode)
  252. {
  253. if (mm_is_complex(matcode))
  254. {
  255. if (fscanf(f, "%d %d %lg %lg", I, J, real, imag)
  256. != 4) return MM_PREMATURE_EOF;
  257. }
  258. else if (mm_is_real(matcode))
  259. {
  260. if (fscanf(f, "%d %d %lg\n", I, J, real)
  261. != 3) return MM_PREMATURE_EOF;
  262. }
  263. else if (mm_is_pattern(matcode))
  264. {
  265. if (fscanf(f, "%d %d", I, J) != 2) return MM_PREMATURE_EOF;
  266. }
  267. else
  268. return MM_UNSUPPORTED_TYPE;
  269. return 0;
  270. }
  271. /************************************************************************
  272. mm_read_mtx_crd() fills M, N, nz, array of values, and return
  273. type code, e.g. 'MCRS'
  274. if matrix is complex, values[] is of size 2*nz,
  275. (nz pairs of real/imaginary values)
  276. ************************************************************************/
  277. int mm_read_mtx_crd(char *fname, int *M, int *N, int *nz, int **I, int **J,
  278. double **val, MM_typecode *matcode)
  279. {
  280. int ret_code;
  281. FILE *f;
  282. if (strcmp(fname, "stdin") == 0) f=stdin;
  283. else
  284. if ((f = fopen(fname, "r")) == NULL)
  285. return MM_COULD_NOT_READ_FILE;
  286. if ((ret_code = mm_read_banner(f, matcode)) != 0)
  287. return ret_code;
  288. if (!(mm_is_valid(*matcode) && mm_is_sparse(*matcode) &&
  289. mm_is_matrix(*matcode)))
  290. return MM_UNSUPPORTED_TYPE;
  291. if ((ret_code = mm_read_mtx_crd_size(f, M, N, nz)) != 0)
  292. return ret_code;
  293. *I = (int *) malloc(*nz * sizeof(int));
  294. *J = (int *) malloc(*nz * sizeof(int));
  295. *val = NULL;
  296. if (mm_is_complex(*matcode))
  297. {
  298. *val = (double *) malloc(*nz * 2 * sizeof(double));
  299. ret_code = mm_read_mtx_crd_data(f, *M, *N, *nz, *I, *J, *val,
  300. *matcode);
  301. if (ret_code != 0) return ret_code;
  302. }
  303. else if (mm_is_real(*matcode))
  304. {
  305. *val = (double *) malloc(*nz * sizeof(double));
  306. ret_code = mm_read_mtx_crd_data(f, *M, *N, *nz, *I, *J, *val,
  307. *matcode);
  308. if (ret_code != 0) return ret_code;
  309. }
  310. else if (mm_is_pattern(*matcode))
  311. {
  312. ret_code = mm_read_mtx_crd_data(f, *M, *N, *nz, *I, *J, *val,
  313. *matcode);
  314. if (ret_code != 0) return ret_code;
  315. }
  316. if (f != stdin) fclose(f);
  317. return 0;
  318. }
  319. int mm_write_banner(FILE *f, MM_typecode matcode)
  320. {
  321. char *str = mm_typecode_to_str(matcode);
  322. int ret_code;
  323. ret_code = fprintf(f, "%s %s\n", MatrixMarketBanner, str);
  324. free(str);
  325. if (ret_code !=2 )
  326. return MM_COULD_NOT_WRITE_FILE;
  327. else
  328. return 0;
  329. }
  330. int mm_write_mtx_crd(char fname[], int M, int N, int nz, int I[], int J[],
  331. double val[], MM_typecode matcode)
  332. {
  333. FILE *f;
  334. int i;
  335. if (strcmp(fname, "stdout") == 0)
  336. f = stdout;
  337. else
  338. if ((f = fopen(fname, "w")) == NULL)
  339. return MM_COULD_NOT_WRITE_FILE;
  340. /* print banner followed by typecode */
  341. fprintf(f, "%s ", MatrixMarketBanner);
  342. fprintf(f, "%s\n", mm_typecode_to_str(matcode));
  343. /* print matrix sizes and nonzeros */
  344. fprintf(f, "%d %d %d\n", M, N, nz);
  345. /* print values */
  346. if (mm_is_pattern(matcode))
  347. for (i=0; i<nz; i++)
  348. fprintf(f, "%d %d\n", I[i], J[i]);
  349. else
  350. if (mm_is_real(matcode))
  351. for (i=0; i<nz; i++)
  352. fprintf(f, "%d %d %20.16g\n", I[i], J[i], val[i]);
  353. else
  354. if (mm_is_complex(matcode))
  355. for (i=0; i<nz; i++)
  356. fprintf(f, "%d %d %20.16g %20.16g\n", I[i], J[i], val[2*i],
  357. val[2*i+1]);
  358. else
  359. {
  360. if (f != stdout) fclose(f);
  361. return MM_UNSUPPORTED_TYPE;
  362. }
  363. if (f !=stdout) fclose(f);
  364. return 0;
  365. }
  366. /**
  367. * Create a new copy of a string s. mm_strdup() is a common routine, but
  368. * not part of ANSI C, so it is included here. Used by mm_typecode_to_str().
  369. *
  370. */
  371. char *mm_strdup(const char *s)
  372. {
  373. int len = strlen(s);
  374. char *s2 = (char *) malloc((len+1)*sizeof(char));
  375. return strcpy(s2, s);
  376. }
  377. char *mm_typecode_to_str(MM_typecode matcode)
  378. {
  379. char buffer[MM_MAX_LINE_LENGTH];
  380. char *types[4];
  381. /* char *mm_strdup(const char *); */
  382. int error =0;
  383. /* check for MTX type */
  384. if (mm_is_matrix(matcode))
  385. types[0] = MM_MTX_STR;
  386. else
  387. error=1;
  388. /* check for CRD or ARR matrix */
  389. if (mm_is_sparse(matcode))
  390. types[1] = MM_SPARSE_STR;
  391. else
  392. if (mm_is_dense(matcode))
  393. types[1] = MM_DENSE_STR;
  394. else
  395. return NULL;
  396. /* check for element data type */
  397. if (mm_is_real(matcode))
  398. types[2] = MM_REAL_STR;
  399. else
  400. if (mm_is_complex(matcode))
  401. types[2] = MM_COMPLEX_STR;
  402. else
  403. if (mm_is_pattern(matcode))
  404. types[2] = MM_PATTERN_STR;
  405. else
  406. if (mm_is_integer(matcode))
  407. types[2] = MM_INT_STR;
  408. else
  409. return NULL;
  410. /* check for symmetry type */
  411. if (mm_is_general(matcode))
  412. types[3] = MM_GENERAL_STR;
  413. else
  414. if (mm_is_symmetric(matcode))
  415. types[3] = MM_SYMM_STR;
  416. else
  417. if (mm_is_hermitian(matcode))
  418. types[3] = MM_HERM_STR;
  419. else
  420. if (mm_is_skew(matcode))
  421. types[3] = MM_SKEW_STR;
  422. else
  423. return NULL;
  424. sprintf(buffer,"%s %s %s %s", types[0], types[1], types[2], types[3]);
  425. return mm_strdup(buffer);
  426. }