tensor_interface.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011-2021 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 "../test_interfaces.h"
  18. #include "../../../helper.h"
  19. #define NX 4
  20. #define NY NX
  21. #define NZ NX
  22. #define NT NX
  23. /* Prototypes */
  24. static void register_data(void);
  25. static void unregister_data(void);
  26. void test_tensor_cpu_func(void *buffers[], void *args);
  27. #ifdef STARPU_USE_CUDA
  28. extern void test_tensor_cuda_func(void *buffers[], void *_args);
  29. #endif
  30. #ifdef STARPU_USE_OPENCL
  31. extern void test_tensor_opencl_func(void *buffers[], void *args);
  32. #endif
  33. static starpu_data_handle_t _tensor_handle;
  34. static starpu_data_handle_t _tensor2_handle;
  35. struct test_config tensor_config =
  36. {
  37. .cpu_func = test_tensor_cpu_func,
  38. #ifdef STARPU_USE_CUDA
  39. .cuda_func = test_tensor_cuda_func,
  40. #endif
  41. #ifdef STARPU_USE_OPENCL
  42. .opencl_func = test_tensor_opencl_func,
  43. #endif
  44. .handle = &_tensor_handle,
  45. .dummy_handle = &_tensor2_handle,
  46. .copy_failed = SUCCESS,
  47. .name = "tensor_interface"
  48. };
  49. static int _tensor[NX*NY*NZ*NT];
  50. static int _tensor2[NX*NY*NZ*NT];
  51. static void
  52. register_data(void)
  53. {
  54. /* Initializing data */
  55. int val = 0;
  56. int i, j, k, l;
  57. for (l = 0; l < NT; l++)
  58. for (k = 0; k < NZ; k++)
  59. for (j = 0; j < NY; j++)
  60. for (i = 0; i < NX; i++)
  61. _tensor[(l*NX*NY*NZ)+(k*NX*NY)+(j*NX)+i] = val++;
  62. /* Registering data */
  63. starpu_tensor_data_register(&_tensor_handle,
  64. STARPU_MAIN_RAM,
  65. (uintptr_t)_tensor,
  66. NX,
  67. NX * NY,
  68. NX * NY * NZ,
  69. NX,
  70. NY,
  71. NZ,
  72. NT,
  73. sizeof(_tensor[0]));
  74. starpu_tensor_data_register(&_tensor2_handle,
  75. STARPU_MAIN_RAM,
  76. (uintptr_t)_tensor2,
  77. NX,
  78. NX * NY,
  79. NX * NY * NZ,
  80. NX,
  81. NY,
  82. NZ,
  83. NT,
  84. sizeof(_tensor2[0]));
  85. }
  86. static void
  87. unregister_data(void)
  88. {
  89. starpu_data_unregister(_tensor_handle);
  90. starpu_data_unregister(_tensor2_handle);
  91. }
  92. void test_tensor_cpu_func(void *buffers[], void *args)
  93. {
  94. STARPU_SKIP_IF_VALGRIND;
  95. int factor = *(int*)args;
  96. int nx = STARPU_TENSOR_GET_NX(buffers[0]);
  97. int ny = STARPU_TENSOR_GET_NY(buffers[0]);
  98. int nz = STARPU_TENSOR_GET_NZ(buffers[0]);
  99. int nt = STARPU_TENSOR_GET_NT(buffers[0]);
  100. unsigned ldy = STARPU_TENSOR_GET_LDY(buffers[0]);
  101. unsigned ldz = STARPU_TENSOR_GET_LDZ(buffers[0]);
  102. unsigned ldt = STARPU_TENSOR_GET_LDT(buffers[0]);
  103. int *tensor = (int *) STARPU_TENSOR_GET_PTR(buffers[0]);
  104. int i, j, k, l;
  105. int val = 0;
  106. tensor_config.copy_failed = SUCCESS;
  107. for (l = 0; l < nt; l++)
  108. {
  109. for (k = 0; k < nz; k++)
  110. {
  111. for (j = 0; j < ny; j++)
  112. {
  113. for (i = 0; i < nx; i++)
  114. {
  115. if (tensor[(l*ldt)+(k*ldz)+(j*ldy)+i] != factor * val)
  116. {
  117. tensor_config.copy_failed = FAILURE;
  118. return;
  119. }
  120. else
  121. {
  122. tensor[(l*ldt)+(k*ldz)+(j*ldy)+i] *= -1;
  123. val++;
  124. }
  125. }
  126. }
  127. }
  128. }
  129. }
  130. int
  131. main(int argc, char **argv)
  132. {
  133. struct data_interface_test_summary summary;
  134. struct starpu_conf conf;
  135. starpu_conf_init(&conf);
  136. conf.ncuda = 2;
  137. conf.nopencl = 1;
  138. int ret = starpu_initialize(&conf, &argc, &argv);
  139. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  140. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  141. if(starpu_cpu_worker_get_count() == 0)
  142. {
  143. starpu_shutdown();
  144. return STARPU_TEST_SKIPPED;
  145. }
  146. register_data();
  147. run_tests(&tensor_config, &summary);
  148. unregister_data();
  149. starpu_shutdown();
  150. data_interface_test_summary_print(stderr, &summary);
  151. return data_interface_test_summary_success(&summary);
  152. enodev:
  153. return STARPU_TEST_SKIPPED;
  154. }