mm_to_bcsr.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010, 2011, 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. #include "mm_to_bcsr.h"
  17. /* Some debug functions */
  18. static void print_block(tmp_block_t *block, unsigned r, unsigned c)
  19. {
  20. printf(" **** block %d %d **** \n", block->i, block->j);
  21. unsigned i, j;
  22. for (j = 0; j < r; j++)
  23. {
  24. for (i = 0; i < c; i++)
  25. {
  26. printf("%2.2f\t", block->val[i + j*c]);
  27. }
  28. printf("\n");
  29. }
  30. }
  31. static void print_all_blocks(tmp_block_t *block_list, unsigned r, unsigned c)
  32. {
  33. tmp_block_t *current_block = block_list;
  34. while(current_block)
  35. {
  36. print_block(current_block, r, c);
  37. current_block = current_block->next;
  38. }
  39. }
  40. static void print_bcsr(bcsr_t *bcsr)
  41. {
  42. fprintf(stderr, "** BSCR **\n");
  43. fprintf(stderr, "non zero - blocks = %d\n", bcsr->nnz_blocks);
  44. fprintf(stderr, "nrows - blocks = %d\n", bcsr->nrows_blocks);
  45. fprintf(stderr, "block size : c %d r %d\n", bcsr->c, bcsr->r);
  46. }
  47. static unsigned count_blocks(tmp_block_t *block_list)
  48. {
  49. unsigned count = 0;
  50. tmp_block_t *current_block = block_list;
  51. while(current_block)
  52. {
  53. count++;
  54. current_block = current_block->next;
  55. }
  56. return count;
  57. }
  58. static unsigned count_row_blocks(tmp_block_t *block_list)
  59. {
  60. unsigned maxrow = 0;
  61. tmp_block_t *current_block = block_list;
  62. while(current_block)
  63. {
  64. if (current_block->j > maxrow)
  65. maxrow = current_block->j;
  66. current_block = current_block->next;
  67. }
  68. return (maxrow+1);
  69. }
  70. /* Find the block that corresponds to (i,j) if it exists in the list */
  71. static tmp_block_t *search_block(tmp_block_t *block_list, unsigned i, unsigned j)
  72. {
  73. tmp_block_t *current_block = block_list;
  74. /* printf("search %d %d\n", i, j); */
  75. while (current_block)
  76. {
  77. if ((current_block->i == i) && (current_block->j == j))
  78. {
  79. /* we found the block */
  80. return current_block;
  81. }
  82. current_block = current_block->next;
  83. };
  84. /* no entry was found ... */
  85. return NULL;
  86. }
  87. static tmp_block_t *create_block(unsigned c, unsigned r)
  88. {
  89. tmp_block_t *block;
  90. block = malloc(sizeof(tmp_block_t));
  91. block->val = calloc(c*r, sizeof(float));
  92. return block;
  93. }
  94. /* determine if next block is bigger in lexical order */
  95. static unsigned next_block_is_bigger(tmp_block_t *block, unsigned i, unsigned j)
  96. {
  97. tmp_block_t *next = block->next;
  98. if (next)
  99. {
  100. /* we evaluate lexical order */
  101. if (next->j < j)
  102. return 0;
  103. if (next->j > j)
  104. return 1;
  105. /* next->j == j */
  106. return (next->i > i);
  107. }
  108. /* this is the last block, so it's bigger */
  109. return 1;
  110. }
  111. /* we insert a block in the list, directly at the appropriate place */
  112. static void insert_block(tmp_block_t *block, tmp_block_t **block_list, unsigned i, unsigned j)
  113. {
  114. /* insert block at the beginning of the list */
  115. /*block->next = *block_list;
  116. *block_list = block; */
  117. /* insert the block in lexicographical order */
  118. /* first find an element that is bigger, then insert the block just before it */
  119. tmp_block_t *current_block = *block_list;
  120. if (!current_block)
  121. {
  122. /* list was empty */
  123. *block_list = block;
  124. block->next = NULL;
  125. return;
  126. }
  127. while (current_block)
  128. {
  129. if (next_block_is_bigger(current_block, i, j))
  130. {
  131. /* insert block here */
  132. block->next = current_block->next;
  133. current_block->next = block;
  134. return;
  135. }
  136. current_block = current_block->next;
  137. };
  138. /* should not be reached ! */
  139. }
  140. /* we add an element to the list of blocks, it is either added to an existing block or in a block specifically created if there was none */
  141. static void insert_elem(tmp_block_t **block_list, unsigned abs_i, unsigned abs_j, float val, unsigned c, unsigned r)
  142. {
  143. /* we are looking for the block that contains (abs_i, abs_j) (abs = absolute) */
  144. unsigned i,j;
  145. i = abs_i / c;
  146. j = abs_j / r;
  147. tmp_block_t *block;
  148. block = search_block(*block_list, i, j);
  149. if (!block)
  150. {
  151. /* the block does not exist yet */
  152. /* create it */
  153. block = create_block(c, r);
  154. block->i = i;
  155. block->j = j;
  156. /* printf("create block %d %d !\n", i, j); */
  157. /* insert it in the block list */
  158. insert_block(block, block_list, i, j);
  159. }
  160. /* now insert the value in the corresponding block */
  161. unsigned local_i, local_j, local_index;
  162. local_i = abs_i % c;
  163. local_j = abs_j % r;
  164. local_index = local_j * c + local_i;
  165. block->val[local_index] = val;
  166. }
  167. /* transform a list of values (with coordinates) into a list of blocks that are easily processed into BCSR */
  168. static tmp_block_t * mm_to_blocks(int nz, unsigned *I, unsigned *J, float *val, unsigned c, unsigned r)
  169. {
  170. int elem;
  171. /* at first, the list of block is empty */
  172. tmp_block_t *block_list = NULL;
  173. for (elem = 0; elem < nz; elem++)
  174. {
  175. insert_elem(&block_list, I[elem], J[elem], val[elem], c, r);
  176. }
  177. return block_list;
  178. }
  179. static void fill_bcsr(tmp_block_t *block_list, unsigned c, unsigned r, bcsr_t *bcsr)
  180. {
  181. unsigned block = 0;
  182. unsigned current_offset = 0;
  183. size_t block_size = c*r*sizeof(float);
  184. tmp_block_t *current_block = block_list;
  185. while(current_block)
  186. {
  187. /* copy the val from the block to the contiguous area in the BCSR */
  188. memcpy(&bcsr->val[current_offset], current_block->val, block_size);
  189. /* write the the index of the block
  190. * XXX should it be in blocks ? */
  191. bcsr->colind[block] = current_block->i;
  192. if ((bcsr->rowptr[current_block->j] == 0) && (current_block->j != 0))
  193. {
  194. /* this is the first element of the line */
  195. bcsr->rowptr[current_block->j] = block;
  196. }
  197. block++;
  198. current_offset = block*c*r;
  199. current_block = current_block->next;
  200. };
  201. /* for all lines where there were no block at all (XXX), fill the 0 in rowptr */
  202. /* the first row must start at 0 ? */
  203. bcsr->rowptr[0] = 0;
  204. unsigned row;
  205. for (row = 1; row < bcsr->nrows_blocks; row++)
  206. {
  207. if (bcsr->rowptr[row] == 0)
  208. bcsr->rowptr[row] = bcsr->rowptr[row-1];
  209. }
  210. bcsr->rowptr[bcsr->nrows_blocks] = bcsr->nnz_blocks;
  211. }
  212. static bcsr_t * blocks_to_bcsr(tmp_block_t *block_list, unsigned c, unsigned r)
  213. {
  214. unsigned nblocks;
  215. /* print_all_blocks(block_list, r, c); */
  216. nblocks = count_blocks(block_list);
  217. bcsr_t *bcsr = malloc(sizeof(bcsr_t));
  218. bcsr->nnz_blocks = nblocks;
  219. bcsr->r = r;
  220. bcsr->c = c;
  221. unsigned nrows_blocks = count_row_blocks(block_list);
  222. bcsr->nrows_blocks = nrows_blocks;
  223. bcsr->val = malloc(nblocks*r*c*sizeof(float));
  224. bcsr->colind = malloc(nblocks*sizeof(unsigned));
  225. bcsr->rowptr = calloc((nrows_blocks + 1), sizeof(unsigned));
  226. fill_bcsr(block_list, c, r, bcsr);
  227. return bcsr;
  228. }
  229. bcsr_t *mm_to_bcsr(unsigned nz, unsigned *I, unsigned *J, float *val, unsigned c, unsigned r)
  230. {
  231. bcsr_t *bcsr;
  232. tmp_block_t *block_list;
  233. block_list = mm_to_blocks(nz, I, J, val, c, r);
  234. bcsr = blocks_to_bcsr(block_list, c, r);
  235. print_bcsr(bcsr);
  236. return bcsr;
  237. }
  238. bcsr_t *mm_file_to_bcsr(char *filename, unsigned c, unsigned r)
  239. {
  240. FILE *f;
  241. MM_typecode matcode;
  242. int ret_code;
  243. int M, N;
  244. int nz;
  245. int i;
  246. unsigned *I, *J;
  247. float *val;
  248. bcsr_t *bcsr;
  249. if ((f = fopen(filename, "r")) == NULL)
  250. {
  251. fprintf(stderr, "File <%s> not found\n", filename);
  252. exit(1);
  253. }
  254. if (mm_read_banner(f, &matcode) != 0)
  255. {
  256. printf("Could not process Matrix Market banner.\n");
  257. exit(1);
  258. }
  259. /* This is how one can screen matrix types if their application */
  260. /* only supports a subset of the Matrix Market data types. */
  261. if (mm_is_complex(matcode) && mm_is_matrix(matcode) && mm_is_sparse(matcode) )
  262. {
  263. printf("Sorry, this application does not support ");
  264. printf("Market Market type: [%s]\n", mm_typecode_to_str(matcode));
  265. exit(1);
  266. }
  267. /* find out size of sparse matrix .... */
  268. if ((ret_code = mm_read_mtx_crd_size(f, &M, &N, &nz)) !=0)
  269. exit(1);
  270. /* reseve memory for matrices */
  271. I = malloc(nz * sizeof(unsigned));
  272. J = malloc(nz * sizeof(unsigned));
  273. /* XXX float ! */
  274. val = (float *) malloc(nz * sizeof(float));
  275. for (i=0; i<nz; i++)
  276. {
  277. fscanf(f, "%d %d %f\n", &I[i], &J[i], &val[i]);
  278. I[i]--; /* adjust from 1-based to 0-based */
  279. J[i]--;
  280. }
  281. if (f !=stdin) fclose(f);
  282. bcsr = mm_to_bcsr((unsigned)nz, I, J, val, c, r);
  283. free(I);
  284. free(J);
  285. free(val);
  286. return bcsr;
  287. }