bcsr_interface.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010, 2011, 2012 Centre National de la Recherche Scientifique
  4. * Copyright (C) 2011 Institut National de Recherche en Informatique et Automatique
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. #include <config.h>
  18. #include <starpu.h>
  19. #include "../test_interfaces.h"
  20. #include "../../../helper.h"
  21. /*
  22. * XXX : These values should not be changed. If you really understand all that
  23. * BCSR stuff, feel free to write a better example :)
  24. */
  25. /* Size of the matrix */
  26. #define WIDTH 4
  27. #define HEIGHT 4
  28. #define SIZE (WIDTH * HEIGHT)
  29. /* Size of the blocks */
  30. #define R 2
  31. #define C 2
  32. #define BLOCK_SIZE (R*C)
  33. /* The matrix is simply 0 1 2... There are SIZE-1 non zero values... */
  34. #define NNZ (SIZE-1)
  35. /* ... and SIZE/BLOCK_SIZE non zero blocks */
  36. #define NNZ_BLOCKS (SIZE/BLOCK_SIZE)
  37. #ifdef STARPU_USE_CPU
  38. static void test_bcsr_cpu_func(void *buffers[], void *args);
  39. #endif /* !STARPU_USE_CPU */
  40. #ifdef STARPU_USE_CUDA
  41. extern void test_bcsr_cuda_func(void *buffers[], void *_args);
  42. #endif
  43. #ifdef STARPU_USE_OPENCL
  44. extern void test_bcsr_opencl_func(void *buffers[], void *args);
  45. #endif
  46. static int nzval[NNZ];
  47. static int nzval2[NNZ];
  48. static uint32_t colind[NNZ_BLOCKS];
  49. static uint32_t colind2[NNZ_BLOCKS];
  50. static uint32_t rowptr[1+WIDTH/R];
  51. static uint32_t rowptr2[1+WIDTH/R];
  52. static starpu_data_handle_t bcsr_handle;
  53. static starpu_data_handle_t bcsr2_handle;
  54. struct test_config bcsr_config =
  55. {
  56. #ifdef STARPU_USE_CPU
  57. .cpu_func = test_bcsr_cpu_func,
  58. #endif /* !STARPU_USE_CPU */
  59. #ifdef STARPU_USE_CUDA
  60. .cuda_func = test_bcsr_cuda_func,
  61. #endif /* !STARPU_USE_CUDA */
  62. #ifdef STARPU_USE_OPENCL
  63. .opencl_func = test_bcsr_opencl_func,
  64. #endif /* !STARPU_USE_OPENCL */
  65. .handle = &bcsr_handle,
  66. .dummy_handle = &bcsr2_handle,
  67. .copy_failed = 0,
  68. .name = "bcsr_interface"
  69. };
  70. static void
  71. register_data(void)
  72. {
  73. int i;
  74. for (i = 0; i < NNZ; i++)
  75. nzval[i] = i;
  76. colind[0] = 0;
  77. colind[1] = 2;
  78. colind[2] = 0;
  79. colind[3] = 2;
  80. rowptr[0] = 0;
  81. rowptr[1] = 2;
  82. rowptr[2] = 4;
  83. starpu_bcsr_data_register(&bcsr_handle,
  84. 0,
  85. NNZ_BLOCKS,
  86. HEIGHT/R,
  87. (uintptr_t) nzval,
  88. colind,
  89. rowptr,
  90. 0,
  91. R,
  92. C,
  93. sizeof(nzval[0]));
  94. starpu_bcsr_data_register(&bcsr2_handle,
  95. 0,
  96. NNZ_BLOCKS,
  97. HEIGHT/R,
  98. (uintptr_t) nzval2,
  99. colind2,
  100. rowptr2,
  101. 0,
  102. R,
  103. C,
  104. sizeof(nzval2[0]));
  105. }
  106. static void
  107. unregister_data(void)
  108. {
  109. starpu_data_unregister(bcsr_handle);
  110. starpu_data_unregister(bcsr2_handle);
  111. }
  112. static void
  113. test_bcsr_cpu_func(void *buffers[], void *args)
  114. {
  115. STARPU_SKIP_IF_VALGRIND;
  116. int *val;
  117. int factor;
  118. int i;
  119. uint32_t nnz = STARPU_BCSR_GET_NNZ(buffers[0]);
  120. val = (int *) STARPU_BCSR_GET_NZVAL(buffers[0]);
  121. factor = *(int *) args;
  122. for (i = 0; i < nnz; i++)
  123. {
  124. if (val[i] != i * factor)
  125. {
  126. bcsr_config.copy_failed = 1;
  127. return;
  128. }
  129. val[i] *= -1;
  130. }
  131. /* Check colind */
  132. uint32_t *col = STARPU_BCSR_GET_COLIND(buffers[0]);
  133. for (i = 0; i < NNZ_BLOCKS; i++)
  134. if (col[i] != colind[i])
  135. bcsr_config.copy_failed = 1;
  136. /* Check rowptr */
  137. uint32_t *row = STARPU_BCSR_GET_ROWPTR(buffers[0]);
  138. for (i = 0; i < 1 + WIDTH/R; i++)
  139. if (row[i] != rowptr[i])
  140. bcsr_config.copy_failed = 1;
  141. }
  142. int
  143. main(void)
  144. {
  145. data_interface_test_summary *summary;
  146. struct starpu_conf conf =
  147. {
  148. .ncpus = -1,
  149. .ncuda = 2,
  150. .nopencl = 1
  151. };
  152. if (starpu_init(&conf) == -ENODEV)
  153. return STARPU_TEST_SKIPPED;
  154. register_data();
  155. summary = run_tests(&bcsr_config);
  156. if (!summary)
  157. exit(EXIT_FAILURE);
  158. unregister_data();
  159. starpu_shutdown();
  160. data_interface_test_summary_print(stderr, summary);
  161. return data_interface_test_summary_success(summary);
  162. }