mm_to_bcsr.c 7.7 KB

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