bcsr_interface.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 <starpu.h>
  18. #include "../test_interfaces.h"
  19. #include "../../../helper.h"
  20. /*
  21. * XXX : These values should not be changed. If you really understand all that
  22. * BCSR stuff, feel free to write a better example :)
  23. */
  24. /* Size of the matrix */
  25. #define WIDTH 4
  26. #define HEIGHT 4
  27. #define SIZE (WIDTH * HEIGHT)
  28. /* Size of the blocks */
  29. #define R 2
  30. #define C 2
  31. #define BLOCK_SIZE (R*C)
  32. /* The matrix is simply 0 1 2... There are SIZE-1 non zero values... */
  33. #define NNZ (SIZE-1)
  34. /* ... and SIZE/BLOCK_SIZE non zero blocks */
  35. #define NNZ_BLOCKS (SIZE/BLOCK_SIZE)
  36. #ifdef STARPU_USE_CPU
  37. static void test_bcsr_cpu_func(void *buffers[], void *args);
  38. #endif /* !STARPU_USE_CPU */
  39. #ifdef STARPU_USE_CUDA
  40. extern void test_bcsr_cuda_func(void *buffers[], void *_args);
  41. #endif
  42. #ifdef STARPU_USE_OPENCL
  43. extern void test_bcsr_opencl_func(void *buffers[], void *args);
  44. #endif
  45. static int nzval[NNZ];
  46. static int nzval2[NNZ];
  47. static uint32_t colind[NNZ_BLOCKS];
  48. static uint32_t colind2[NNZ_BLOCKS];
  49. static uint32_t rowptr[1+WIDTH/R];
  50. static uint32_t rowptr2[1+WIDTH/R];
  51. static starpu_data_handle_t bcsr_handle;
  52. static starpu_data_handle_t bcsr2_handle;
  53. struct test_config bcsr_config =
  54. {
  55. #ifdef STARPU_USE_CPU
  56. .cpu_func = test_bcsr_cpu_func,
  57. #endif /* !STARPU_USE_CPU */
  58. #ifdef STARPU_USE_CUDA
  59. .cuda_func = test_bcsr_cuda_func,
  60. #endif /* !STARPU_USE_CUDA */
  61. #ifdef STARPU_USE_OPENCL
  62. .opencl_func = test_bcsr_opencl_func,
  63. #endif /* !STARPU_USE_OPENCL */
  64. .handle = &bcsr_handle,
  65. .dummy_handle = &bcsr2_handle,
  66. .copy_failed = 0,
  67. .name = "bcsr_interface"
  68. };
  69. static void
  70. register_data(void)
  71. {
  72. int i;
  73. for (i = 0; i < SIZE; i++)
  74. nzval[i] = i;
  75. colind[0] = 0;
  76. colind[1] = 2;
  77. colind[2] = 0;
  78. colind[3] = 2;
  79. rowptr[0] = 0;
  80. rowptr[1] = 2;
  81. rowptr[2] = 4;
  82. starpu_bcsr_data_register(&bcsr_handle,
  83. 0,
  84. NNZ_BLOCKS,
  85. HEIGHT/R,
  86. (uintptr_t) nzval,
  87. colind,
  88. rowptr,
  89. 0,
  90. R,
  91. C,
  92. sizeof(nzval[0]));
  93. starpu_bcsr_data_register(&bcsr2_handle,
  94. 0,
  95. NNZ_BLOCKS,
  96. HEIGHT/R,
  97. (uintptr_t) nzval2,
  98. colind2,
  99. rowptr2,
  100. 0,
  101. R,
  102. C,
  103. sizeof(nzval2[0]));
  104. }
  105. static void
  106. unregister_data(void)
  107. {
  108. starpu_data_unregister(bcsr_handle);
  109. starpu_data_unregister(bcsr2_handle);
  110. }
  111. static void
  112. test_bcsr_cpu_func(void *buffers[], void *args)
  113. {
  114. int *val;
  115. int factor;
  116. int i;
  117. uint32_t nnz = STARPU_BCSR_GET_NNZ(buffers[0]);
  118. val = (int *) STARPU_BCSR_GET_NZVAL(buffers[0]);
  119. factor = *(int *) args;
  120. for (i = 0; i < nnz; i++)
  121. {
  122. if (val[i] != i * factor)
  123. {
  124. bcsr_config.copy_failed = 1;
  125. return;
  126. }
  127. val[i] *= -1;
  128. }
  129. /* Check colind */
  130. uint32_t *col = STARPU_BCSR_GET_COLIND(buffers[0]);
  131. for (i = 0; i < NNZ_BLOCKS; i++)
  132. if (col[i] != colind[i])
  133. bcsr_config.copy_failed = 1;
  134. /* Check rowptr */
  135. uint32_t *row = STARPU_BCSR_GET_ROWPTR(buffers[0]);
  136. for (i = 0; i < 1 + WIDTH/R; i++)
  137. if (row[i] != rowptr[i])
  138. bcsr_config.copy_failed = 1;
  139. }
  140. int
  141. main(void)
  142. {
  143. data_interface_test_summary *summary;
  144. struct starpu_conf conf =
  145. {
  146. .ncpus = -1,
  147. .ncuda = 2,
  148. .nopencl = 1
  149. };
  150. if (starpu_init(&conf) == -ENODEV)
  151. return STARPU_TEST_SKIPPED;
  152. register_data();
  153. summary = run_tests(&bcsr_config);
  154. if (!summary)
  155. exit(EXIT_FAILURE);
  156. unregister_data();
  157. starpu_shutdown();
  158. data_interface_test_summary_print(stderr, summary);
  159. return data_interface_test_summary_success(summary);
  160. }