mm_to_bcsr.c 8.4 KB

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