bcsr.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-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. #include <starpu.h>
  17. #include "../helper.h"
  18. static starpu_data_handle_t bcsr_handle;
  19. void cpu_show_bcsr(void *descr[], void *arg)
  20. {
  21. (void)arg;
  22. struct starpu_bcsr_interface *iface = descr[0];
  23. uint32_t nnz = iface->nnz;
  24. uint32_t nrow = iface->nrow;
  25. int *nzval = (int *)iface->nzval;
  26. uint32_t *colind = iface->colind;
  27. uint32_t *rowptr = iface->rowptr;
  28. uint32_t firstentry = iface->firstentry;
  29. uint32_t r = iface->r;
  30. uint32_t c = iface->c;
  31. uint32_t elemsize = iface->elemsize;
  32. uint32_t i, j, y, x;
  33. static starpu_pthread_mutex_t mutex = STARPU_PTHREAD_MUTEX_INITIALIZER;
  34. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  35. printf("nnz %d elemsize %d\n", nnz, elemsize);
  36. for (i = 0; i < nrow; i++) {
  37. uint32_t row_start = rowptr[i] - firstentry;
  38. uint32_t row_end = rowptr[i+1] - firstentry;
  39. printf("row %d\n", i);
  40. for (j = row_start; j < row_end; j++)
  41. {
  42. int *block = nzval + j * r*c;
  43. printf( " column %d\n", colind[j]);
  44. for (y = 0; y < r; y++)
  45. {
  46. for (x = 0; x < c; x++)
  47. printf(" %d", block[y*c+x]);
  48. printf("\n");
  49. }
  50. }
  51. }
  52. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  53. }
  54. struct starpu_codelet show_cl =
  55. {
  56. .cpu_funcs = { cpu_show_bcsr },
  57. .nbuffers = 1,
  58. .modes = { STARPU_R },
  59. };
  60. /*
  61. * In this test, we use the following matrix:
  62. *
  63. * +----------------+
  64. * | 0 1 0 0 |
  65. * | 2 3 0 0 |
  66. * | 4 5 8 9 |
  67. * | 6 7 10 11 |
  68. * +----------------+
  69. *
  70. * nzval = [0, 1, 2, 3] ++ [4, 5, 6, 7] ++ [8, 9, 10, 11]
  71. * colind = [0, 0, 1]
  72. * rowptr = [0, 1, 3 ]
  73. * r = c = 2
  74. */
  75. /* Size of the blocks */
  76. #define R 2
  77. #define C 2
  78. #define NNZ_BLOCKS 3 /* out of 4 */
  79. #define NZVAL_SIZE (R*C*NNZ_BLOCKS)
  80. #define NROWS 2
  81. static int nzval[NZVAL_SIZE] =
  82. {
  83. 0, 1, 2, 3, /* First block */
  84. 4, 5, 6, 7, /* Second block */
  85. 8, 9, 10, 11 /* Third block */
  86. };
  87. static uint32_t colind[NNZ_BLOCKS] = { 0, 0, 1 };
  88. static uint32_t rowptr[NROWS+1] = { 0, 1, NNZ_BLOCKS };
  89. int main(int argc, char **argv)
  90. {
  91. struct starpu_conf conf;
  92. starpu_conf_init(&conf);
  93. if (starpu_initialize(&conf, &argc, &argv) == -ENODEV || starpu_cpu_worker_get_count() == 0)
  94. return STARPU_TEST_SKIPPED;
  95. starpu_bcsr_data_register(&bcsr_handle,
  96. STARPU_MAIN_RAM,
  97. NNZ_BLOCKS,
  98. NROWS,
  99. (uintptr_t) nzval,
  100. colind,
  101. rowptr,
  102. 0, /* firstentry */
  103. R,
  104. C,
  105. sizeof(nzval[0]));
  106. starpu_task_insert(&show_cl, STARPU_R, bcsr_handle, 0);
  107. struct starpu_data_filter filter = {
  108. .filter_func = starpu_bcsr_filter_vertical_block,
  109. .nchildren = 2,
  110. };
  111. starpu_data_partition(bcsr_handle, &filter);
  112. starpu_task_insert(&show_cl, STARPU_R, starpu_data_get_sub_data(bcsr_handle, 1, 0), 0);
  113. starpu_task_insert(&show_cl, STARPU_R, starpu_data_get_sub_data(bcsr_handle, 1, 1), 0);
  114. starpu_data_unpartition(bcsr_handle, STARPU_MAIN_RAM);
  115. starpu_data_unregister(bcsr_handle);
  116. starpu_shutdown();
  117. return 0;
  118. }