mmio.c 12 KB

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