bcsr.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. {
  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. * +----------------+
  70. *
  71. * nzval = [0, 1, 2, 3] ++ [4, 5, 6, 7] ++ [8, 9, 10, 11]
  72. * colind = [0, 0, 1]
  73. * rowptr = [0, 1, 3 ]
  74. * r = c = 2
  75. */
  76. /* Size of the blocks */
  77. #define R 2
  78. #define C 2
  79. #define NNZ_BLOCKS 3 /* out of 4 */
  80. #define NZVAL_SIZE (R*C*NNZ_BLOCKS)
  81. #define NROWS 2
  82. static int nzval[NZVAL_SIZE] =
  83. {
  84. 0, 1, 2, 3, /* First block */
  85. 4, 5, 6, 7, /* Second block */
  86. 8, 9, 10, 11 /* Third block */
  87. };
  88. static uint32_t colind[NNZ_BLOCKS] = { 0, 0, 1 };
  89. static uint32_t rowptr[NROWS+1] = { 0, 1, NNZ_BLOCKS };
  90. int main(int argc, char **argv)
  91. {
  92. struct starpu_conf conf;
  93. starpu_conf_init(&conf);
  94. if (starpu_initialize(&conf, &argc, &argv) == -ENODEV || starpu_cpu_worker_get_count() == 0)
  95. return STARPU_TEST_SKIPPED;
  96. starpu_bcsr_data_register(&bcsr_handle,
  97. STARPU_MAIN_RAM,
  98. NNZ_BLOCKS,
  99. NROWS,
  100. (uintptr_t) nzval,
  101. colind,
  102. rowptr,
  103. 0, /* firstentry */
  104. R,
  105. C,
  106. sizeof(nzval[0]));
  107. starpu_task_insert(&show_cl, STARPU_R, bcsr_handle, 0);
  108. struct starpu_data_filter filter =
  109. {
  110. .filter_func = starpu_bcsr_filter_vertical_block,
  111. .nchildren = 2,
  112. };
  113. starpu_data_partition(bcsr_handle, &filter);
  114. starpu_task_insert(&show_cl, STARPU_R, starpu_data_get_sub_data(bcsr_handle, 1, 0), 0);
  115. starpu_task_insert(&show_cl, STARPU_R, starpu_data_get_sub_data(bcsr_handle, 1, 1), 0);
  116. starpu_data_unpartition(bcsr_handle, STARPU_MAIN_RAM);
  117. starpu_data_unregister(bcsr_handle);
  118. starpu_shutdown();
  119. return 0;
  120. }