bcsr.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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("\nnnz %d elemsize %d\n", nnz, elemsize);
  36. for (i = 0; i < nrow; i++)
  37. {
  38. uint32_t row_start = rowptr[i] - firstentry;
  39. uint32_t row_end = rowptr[i+1] - firstentry;
  40. printf("row %d\n", i);
  41. for (j = row_start; j < row_end; j++)
  42. {
  43. int *block = nzval + j * r*c;
  44. printf( " column %d\n", colind[j]);
  45. for (y = 0; y < r; y++)
  46. {
  47. for (x = 0; x < c; x++)
  48. printf(" %d", block[y*c+x]);
  49. printf("\n");
  50. }
  51. }
  52. }
  53. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  54. }
  55. struct starpu_codelet show_cl =
  56. {
  57. .cpu_funcs = { cpu_show_bcsr },
  58. .nbuffers = 1,
  59. .modes = { STARPU_R },
  60. };
  61. /*
  62. * In this test, we use the following matrix:
  63. *
  64. * +----------------+
  65. * | 0 1 0 0 |
  66. * | 2 3 0 0 |
  67. * | 4 5 8 9 |
  68. * | 6 7 10 11 |
  69. * | 0 0 0 0 |
  70. * | 0 0 0 0 |
  71. * +----------------+
  72. *
  73. * nzval = [0, 1, 2, 3] ++ [4, 5, 6, 7] ++ [8, 9, 10, 11]
  74. * colind = [0, 0, 1] (column index of each non-zero block)
  75. * rowptr = [0, 1, 3] (index of first non-zero block for each row)
  76. * r = c = 2
  77. */
  78. /* Size of the blocks */
  79. #define R 2
  80. #define C 2
  81. #define NNZ_BLOCKS 3 /* out of 6 */
  82. #define NZVAL_SIZE (R*C*NNZ_BLOCKS)
  83. #define NROWS 3
  84. static int nzval[NZVAL_SIZE] =
  85. {
  86. 0, 1, 2, 3, /* First block */
  87. 4, 5, 6, 7, /* Second block */
  88. 8, 9, 10, 11 /* Third block */
  89. };
  90. static uint32_t colind[NNZ_BLOCKS] = { 0, 0, 1 };
  91. static uint32_t rowptr[NROWS+1] = { 0, 1, NNZ_BLOCKS, NNZ_BLOCKS };
  92. int main(int argc, char **argv)
  93. {
  94. struct starpu_conf conf;
  95. starpu_conf_init(&conf);
  96. conf.precedence_over_environment_variables = 1;
  97. conf.ncuda = 0;
  98. conf.nopencl = 0;
  99. conf.nmic = 0;
  100. if (starpu_initialize(&conf, &argc, &argv) == -ENODEV)
  101. return STARPU_TEST_SKIPPED;
  102. if (starpu_cpu_worker_get_count() == 0 || starpu_memory_nodes_get_count() > 1) {
  103. starpu_shutdown();
  104. return STARPU_TEST_SKIPPED;
  105. }
  106. starpu_bcsr_data_register(&bcsr_handle,
  107. STARPU_MAIN_RAM,
  108. NNZ_BLOCKS,
  109. NROWS,
  110. (uintptr_t) nzval,
  111. colind,
  112. rowptr,
  113. 0, /* firstentry */
  114. R,
  115. C,
  116. sizeof(nzval[0]));
  117. starpu_task_insert(&show_cl, STARPU_R, bcsr_handle, 0);
  118. struct starpu_data_filter filter =
  119. {
  120. .filter_func = starpu_bcsr_filter_vertical_block,
  121. .nchildren = 3,
  122. };
  123. starpu_data_partition(bcsr_handle, &filter);
  124. starpu_task_insert(&show_cl, STARPU_R, starpu_data_get_sub_data(bcsr_handle, 1, 0), 0);
  125. starpu_task_insert(&show_cl, STARPU_R, starpu_data_get_sub_data(bcsr_handle, 1, 1), 0);
  126. starpu_task_insert(&show_cl, STARPU_R, starpu_data_get_sub_data(bcsr_handle, 1, 2), 0);
  127. starpu_data_unpartition(bcsr_handle, STARPU_MAIN_RAM);
  128. starpu_data_unregister(bcsr_handle);
  129. starpu_shutdown();
  130. return 0;
  131. }