bcsr_interface.c 4.3 KB

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