mmio.c 13 KB

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