cxx11_tensor_sycl.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2016
  5. // Mehdi Goli Codeplay Software Ltd.
  6. // Ralph Potter Codeplay Software Ltd.
  7. // Luke Iwanski Codeplay Software Ltd.
  8. // Contact: <eigen@codeplay.com>
  9. // Benoit Steiner <benoit.steiner.goog@gmail.com>
  10. //
  11. // This Source Code Form is subject to the terms of the Mozilla
  12. // Public License v. 2.0. If a copy of the MPL was not distributed
  13. // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
  14. #define EIGEN_TEST_NO_LONGDOUBLE
  15. #define EIGEN_TEST_NO_COMPLEX
  16. #define EIGEN_DEFAULT_DENSE_INDEX_TYPE int64_t
  17. #define EIGEN_USE_SYCL
  18. #include "main.h"
  19. #include <unsupported/Eigen/CXX11/Tensor>
  20. using Eigen::array;
  21. using Eigen::SyclDevice;
  22. using Eigen::Tensor;
  23. using Eigen::TensorMap;
  24. template <typename DataType, int DataLayout, typename IndexType>
  25. void test_sycl_mem_transfers(const Eigen::SyclDevice &sycl_device) {
  26. IndexType sizeDim1 = 5;
  27. IndexType sizeDim2 = 5;
  28. IndexType sizeDim3 = 1;
  29. array<IndexType, 3> tensorRange = {{sizeDim1, sizeDim2, sizeDim3}};
  30. Tensor<DataType, 3, DataLayout, IndexType> in1(tensorRange);
  31. Tensor<DataType, 3, DataLayout, IndexType> out1(tensorRange);
  32. Tensor<DataType, 3, DataLayout, IndexType> out2(tensorRange);
  33. Tensor<DataType, 3, DataLayout, IndexType> out3(tensorRange);
  34. in1 = in1.random();
  35. DataType* gpu_data1 = static_cast<DataType*>(sycl_device.allocate(in1.size()*sizeof(DataType)));
  36. DataType* gpu_data2 = static_cast<DataType*>(sycl_device.allocate(out1.size()*sizeof(DataType)));
  37. TensorMap<Tensor<DataType, 3, DataLayout, IndexType>> gpu1(gpu_data1, tensorRange);
  38. TensorMap<Tensor<DataType, 3, DataLayout, IndexType>> gpu2(gpu_data2, tensorRange);
  39. sycl_device.memcpyHostToDevice(gpu_data1, in1.data(),(in1.size())*sizeof(DataType));
  40. sycl_device.memcpyHostToDevice(gpu_data2, in1.data(),(in1.size())*sizeof(DataType));
  41. gpu1.device(sycl_device) = gpu1 * 3.14f;
  42. gpu2.device(sycl_device) = gpu2 * 2.7f;
  43. sycl_device.memcpyDeviceToHost(out1.data(), gpu_data1,(out1.size())*sizeof(DataType));
  44. sycl_device.memcpyDeviceToHost(out2.data(), gpu_data1,(out2.size())*sizeof(DataType));
  45. sycl_device.memcpyDeviceToHost(out3.data(), gpu_data2,(out3.size())*sizeof(DataType));
  46. sycl_device.synchronize();
  47. for (IndexType i = 0; i < in1.size(); ++i) {
  48. // std::cout << "SYCL DATA : " << out1(i) << " vs CPU DATA : " << in1(i) * 3.14f << "\n";
  49. VERIFY_IS_APPROX(out1(i), in1(i) * 3.14f);
  50. VERIFY_IS_APPROX(out2(i), in1(i) * 3.14f);
  51. VERIFY_IS_APPROX(out3(i), in1(i) * 2.7f);
  52. }
  53. sycl_device.deallocate(gpu_data1);
  54. sycl_device.deallocate(gpu_data2);
  55. }
  56. template <typename DataType, int DataLayout, typename IndexType>
  57. void test_sycl_mem_sync(const Eigen::SyclDevice &sycl_device) {
  58. IndexType size = 20;
  59. array<IndexType, 1> tensorRange = {{size}};
  60. Tensor<DataType, 1, DataLayout, IndexType> in1(tensorRange);
  61. Tensor<DataType, 1, DataLayout, IndexType> in2(tensorRange);
  62. Tensor<DataType, 1, DataLayout, IndexType> out(tensorRange);
  63. in1 = in1.random();
  64. in2 = in1;
  65. DataType* gpu_data = static_cast<DataType*>(sycl_device.allocate(in1.size()*sizeof(DataType)));
  66. TensorMap<Tensor<DataType, 1, DataLayout, IndexType>> gpu1(gpu_data, tensorRange);
  67. sycl_device.memcpyHostToDevice(gpu_data, in1.data(),(in1.size())*sizeof(DataType));
  68. sycl_device.synchronize();
  69. in1.setZero();
  70. sycl_device.memcpyDeviceToHost(out.data(), gpu_data, out.size()*sizeof(DataType));
  71. sycl_device.synchronize();
  72. for (IndexType i = 0; i < in1.size(); ++i) {
  73. VERIFY_IS_APPROX(out(i), in2(i));
  74. }
  75. sycl_device.deallocate(gpu_data);
  76. }
  77. template <typename DataType, int DataLayout, typename IndexType>
  78. void test_sycl_mem_sync_offsets(const Eigen::SyclDevice &sycl_device) {
  79. using tensor_type = Tensor<DataType, 1, DataLayout, IndexType>;
  80. IndexType full_size = 32;
  81. IndexType half_size = full_size / 2;
  82. array<IndexType, 1> tensorRange = {{full_size}};
  83. tensor_type in1(tensorRange);
  84. tensor_type out(tensorRange);
  85. DataType* gpu_data = static_cast<DataType*>(sycl_device.allocate(full_size * sizeof(DataType)));
  86. TensorMap<tensor_type> gpu1(gpu_data, tensorRange);
  87. in1 = in1.random();
  88. // Copy all data to device, then permute on copy back to host
  89. sycl_device.memcpyHostToDevice(gpu_data, in1.data(), full_size * sizeof(DataType));
  90. sycl_device.memcpyDeviceToHost(out.data(), gpu_data + half_size, half_size * sizeof(DataType));
  91. sycl_device.memcpyDeviceToHost(out.data() + half_size, gpu_data, half_size * sizeof(DataType));
  92. for (IndexType i = 0; i < half_size; ++i) {
  93. VERIFY_IS_APPROX(out(i), in1(i + half_size));
  94. VERIFY_IS_APPROX(out(i + half_size), in1(i));
  95. }
  96. in1 = in1.random();
  97. out.setZero();
  98. // Permute copies to device, then copy all back to host
  99. sycl_device.memcpyHostToDevice(gpu_data + half_size, in1.data(), half_size * sizeof(DataType));
  100. sycl_device.memcpyHostToDevice(gpu_data, in1.data() + half_size, half_size * sizeof(DataType));
  101. sycl_device.memcpyDeviceToHost(out.data(), gpu_data, full_size * sizeof(DataType));
  102. for (IndexType i = 0; i < half_size; ++i) {
  103. VERIFY_IS_APPROX(out(i), in1(i + half_size));
  104. VERIFY_IS_APPROX(out(i + half_size), in1(i));
  105. }
  106. in1 = in1.random();
  107. out.setZero();
  108. DataType* gpu_data_out = static_cast<DataType*>(sycl_device.allocate(full_size * sizeof(DataType)));
  109. TensorMap<tensor_type> gpu2(gpu_data_out, tensorRange);
  110. // Copy all to device, permute copies on device, then copy all back to host
  111. sycl_device.memcpyHostToDevice(gpu_data, in1.data(), full_size * sizeof(DataType));
  112. sycl_device.memcpy(gpu_data_out + half_size, gpu_data, half_size * sizeof(DataType));
  113. sycl_device.memcpy(gpu_data_out, gpu_data + half_size, half_size * sizeof(DataType));
  114. sycl_device.memcpyDeviceToHost(out.data(), gpu_data_out, full_size * sizeof(DataType));
  115. for (IndexType i = 0; i < half_size; ++i) {
  116. VERIFY_IS_APPROX(out(i), in1(i + half_size));
  117. VERIFY_IS_APPROX(out(i + half_size), in1(i));
  118. }
  119. sycl_device.deallocate(gpu_data_out);
  120. sycl_device.deallocate(gpu_data);
  121. }
  122. template <typename DataType, int DataLayout, typename IndexType>
  123. void test_sycl_memset_offsets(const Eigen::SyclDevice &sycl_device) {
  124. using tensor_type = Tensor<DataType, 1, DataLayout, IndexType>;
  125. IndexType full_size = 32;
  126. IndexType half_size = full_size / 2;
  127. array<IndexType, 1> tensorRange = {{full_size}};
  128. tensor_type cpu_out(tensorRange);
  129. tensor_type out(tensorRange);
  130. cpu_out.setZero();
  131. std::memset(cpu_out.data(), 0, half_size * sizeof(DataType));
  132. std::memset(cpu_out.data() + half_size, 1, half_size * sizeof(DataType));
  133. DataType* gpu_data = static_cast<DataType*>(sycl_device.allocate(full_size * sizeof(DataType)));
  134. TensorMap<tensor_type> gpu1(gpu_data, tensorRange);
  135. sycl_device.memset(gpu_data, 0, half_size * sizeof(DataType));
  136. sycl_device.memset(gpu_data + half_size, 1, half_size * sizeof(DataType));
  137. sycl_device.memcpyDeviceToHost(out.data(), gpu_data, full_size * sizeof(DataType));
  138. for (IndexType i = 0; i < full_size; ++i) {
  139. VERIFY_IS_APPROX(out(i), cpu_out(i));
  140. }
  141. sycl_device.deallocate(gpu_data);
  142. }
  143. template <typename DataType, int DataLayout, typename IndexType>
  144. void test_sycl_computations(const Eigen::SyclDevice &sycl_device) {
  145. IndexType sizeDim1 = 100;
  146. IndexType sizeDim2 = 10;
  147. IndexType sizeDim3 = 20;
  148. array<IndexType, 3> tensorRange = {{sizeDim1, sizeDim2, sizeDim3}};
  149. Tensor<DataType, 3,DataLayout, IndexType> in1(tensorRange);
  150. Tensor<DataType, 3,DataLayout, IndexType> in2(tensorRange);
  151. Tensor<DataType, 3,DataLayout, IndexType> in3(tensorRange);
  152. Tensor<DataType, 3,DataLayout, IndexType> out(tensorRange);
  153. in2 = in2.random();
  154. in3 = in3.random();
  155. DataType * gpu_in1_data = static_cast<DataType*>(sycl_device.allocate(in1.size()*sizeof(DataType)));
  156. DataType * gpu_in2_data = static_cast<DataType*>(sycl_device.allocate(in2.size()*sizeof(DataType)));
  157. DataType * gpu_in3_data = static_cast<DataType*>(sycl_device.allocate(in3.size()*sizeof(DataType)));
  158. DataType * gpu_out_data = static_cast<DataType*>(sycl_device.allocate(out.size()*sizeof(DataType)));
  159. TensorMap<Tensor<DataType, 3, DataLayout, IndexType>> gpu_in1(gpu_in1_data, tensorRange);
  160. TensorMap<Tensor<DataType, 3, DataLayout, IndexType>> gpu_in2(gpu_in2_data, tensorRange);
  161. TensorMap<Tensor<DataType, 3, DataLayout, IndexType>> gpu_in3(gpu_in3_data, tensorRange);
  162. TensorMap<Tensor<DataType, 3, DataLayout, IndexType>> gpu_out(gpu_out_data, tensorRange);
  163. /// a=1.2f
  164. gpu_in1.device(sycl_device) = gpu_in1.constant(1.2f);
  165. sycl_device.memcpyDeviceToHost(in1.data(), gpu_in1_data ,(in1.size())*sizeof(DataType));
  166. sycl_device.synchronize();
  167. for (IndexType i = 0; i < sizeDim1; ++i) {
  168. for (IndexType j = 0; j < sizeDim2; ++j) {
  169. for (IndexType k = 0; k < sizeDim3; ++k) {
  170. VERIFY_IS_APPROX(in1(i,j,k), 1.2f);
  171. }
  172. }
  173. }
  174. printf("a=1.2f Test passed\n");
  175. /// a=b*1.2f
  176. gpu_out.device(sycl_device) = gpu_in1 * 1.2f;
  177. sycl_device.memcpyDeviceToHost(out.data(), gpu_out_data ,(out.size())*sizeof(DataType));
  178. sycl_device.synchronize();
  179. for (IndexType i = 0; i < sizeDim1; ++i) {
  180. for (IndexType j = 0; j < sizeDim2; ++j) {
  181. for (IndexType k = 0; k < sizeDim3; ++k) {
  182. VERIFY_IS_APPROX(out(i,j,k),
  183. in1(i,j,k) * 1.2f);
  184. }
  185. }
  186. }
  187. printf("a=b*1.2f Test Passed\n");
  188. /// c=a*b
  189. sycl_device.memcpyHostToDevice(gpu_in2_data, in2.data(),(in2.size())*sizeof(DataType));
  190. gpu_out.device(sycl_device) = gpu_in1 * gpu_in2;
  191. sycl_device.memcpyDeviceToHost(out.data(), gpu_out_data,(out.size())*sizeof(DataType));
  192. sycl_device.synchronize();
  193. for (IndexType i = 0; i < sizeDim1; ++i) {
  194. for (IndexType j = 0; j < sizeDim2; ++j) {
  195. for (IndexType k = 0; k < sizeDim3; ++k) {
  196. VERIFY_IS_APPROX(out(i,j,k),
  197. in1(i,j,k) *
  198. in2(i,j,k));
  199. }
  200. }
  201. }
  202. printf("c=a*b Test Passed\n");
  203. /// c=a+b
  204. gpu_out.device(sycl_device) = gpu_in1 + gpu_in2;
  205. sycl_device.memcpyDeviceToHost(out.data(), gpu_out_data,(out.size())*sizeof(DataType));
  206. sycl_device.synchronize();
  207. for (IndexType i = 0; i < sizeDim1; ++i) {
  208. for (IndexType j = 0; j < sizeDim2; ++j) {
  209. for (IndexType k = 0; k < sizeDim3; ++k) {
  210. VERIFY_IS_APPROX(out(i,j,k),
  211. in1(i,j,k) +
  212. in2(i,j,k));
  213. }
  214. }
  215. }
  216. printf("c=a+b Test Passed\n");
  217. /// c=a*a
  218. gpu_out.device(sycl_device) = gpu_in1 * gpu_in1;
  219. sycl_device.memcpyDeviceToHost(out.data(), gpu_out_data,(out.size())*sizeof(DataType));
  220. sycl_device.synchronize();
  221. for (IndexType i = 0; i < sizeDim1; ++i) {
  222. for (IndexType j = 0; j < sizeDim2; ++j) {
  223. for (IndexType k = 0; k < sizeDim3; ++k) {
  224. VERIFY_IS_APPROX(out(i,j,k),
  225. in1(i,j,k) *
  226. in1(i,j,k));
  227. }
  228. }
  229. }
  230. printf("c= a*a Test Passed\n");
  231. //a*3.14f + b*2.7f
  232. gpu_out.device(sycl_device) = gpu_in1 * gpu_in1.constant(3.14f) + gpu_in2 * gpu_in2.constant(2.7f);
  233. sycl_device.memcpyDeviceToHost(out.data(),gpu_out_data,(out.size())*sizeof(DataType));
  234. sycl_device.synchronize();
  235. for (IndexType i = 0; i < sizeDim1; ++i) {
  236. for (IndexType j = 0; j < sizeDim2; ++j) {
  237. for (IndexType k = 0; k < sizeDim3; ++k) {
  238. VERIFY_IS_APPROX(out(i,j,k),
  239. in1(i,j,k) * 3.14f
  240. + in2(i,j,k) * 2.7f);
  241. }
  242. }
  243. }
  244. printf("a*3.14f + b*2.7f Test Passed\n");
  245. ///d= (a>0.5? b:c)
  246. sycl_device.memcpyHostToDevice(gpu_in3_data, in3.data(),(in3.size())*sizeof(DataType));
  247. gpu_out.device(sycl_device) =(gpu_in1 > gpu_in1.constant(0.5f)).select(gpu_in2, gpu_in3);
  248. sycl_device.memcpyDeviceToHost(out.data(), gpu_out_data,(out.size())*sizeof(DataType));
  249. sycl_device.synchronize();
  250. for (IndexType i = 0; i < sizeDim1; ++i) {
  251. for (IndexType j = 0; j < sizeDim2; ++j) {
  252. for (IndexType k = 0; k < sizeDim3; ++k) {
  253. VERIFY_IS_APPROX(out(i, j, k), (in1(i, j, k) > 0.5f)
  254. ? in2(i, j, k)
  255. : in3(i, j, k));
  256. }
  257. }
  258. }
  259. printf("d= (a>0.5? b:c) Test Passed\n");
  260. sycl_device.deallocate(gpu_in1_data);
  261. sycl_device.deallocate(gpu_in2_data);
  262. sycl_device.deallocate(gpu_in3_data);
  263. sycl_device.deallocate(gpu_out_data);
  264. }
  265. template<typename Scalar1, typename Scalar2, int DataLayout, typename IndexType>
  266. static void test_sycl_cast(const Eigen::SyclDevice& sycl_device){
  267. IndexType size = 20;
  268. array<IndexType, 1> tensorRange = {{size}};
  269. Tensor<Scalar1, 1, DataLayout, IndexType> in(tensorRange);
  270. Tensor<Scalar2, 1, DataLayout, IndexType> out(tensorRange);
  271. Tensor<Scalar2, 1, DataLayout, IndexType> out_host(tensorRange);
  272. in = in.random();
  273. Scalar1* gpu_in_data = static_cast<Scalar1*>(sycl_device.allocate(in.size()*sizeof(Scalar1)));
  274. Scalar2 * gpu_out_data = static_cast<Scalar2*>(sycl_device.allocate(out.size()*sizeof(Scalar2)));
  275. TensorMap<Tensor<Scalar1, 1, DataLayout, IndexType>> gpu_in(gpu_in_data, tensorRange);
  276. TensorMap<Tensor<Scalar2, 1, DataLayout, IndexType>> gpu_out(gpu_out_data, tensorRange);
  277. sycl_device.memcpyHostToDevice(gpu_in_data, in.data(),(in.size())*sizeof(Scalar1));
  278. gpu_out.device(sycl_device) = gpu_in. template cast<Scalar2>();
  279. sycl_device.memcpyDeviceToHost(out.data(), gpu_out_data, out.size()*sizeof(Scalar2));
  280. out_host = in. template cast<Scalar2>();
  281. for(IndexType i=0; i< size; i++)
  282. {
  283. VERIFY_IS_APPROX(out(i), out_host(i));
  284. }
  285. printf("cast Test Passed\n");
  286. sycl_device.deallocate(gpu_in_data);
  287. sycl_device.deallocate(gpu_out_data);
  288. }
  289. template<typename DataType, typename dev_Selector> void sycl_computing_test_per_device(dev_Selector s){
  290. QueueInterface queueInterface(s);
  291. auto sycl_device = Eigen::SyclDevice(&queueInterface);
  292. test_sycl_mem_transfers<DataType, RowMajor, int64_t>(sycl_device);
  293. test_sycl_computations<DataType, RowMajor, int64_t>(sycl_device);
  294. test_sycl_mem_sync<DataType, RowMajor, int64_t>(sycl_device);
  295. test_sycl_mem_sync_offsets<DataType, RowMajor, int64_t>(sycl_device);
  296. test_sycl_memset_offsets<DataType, RowMajor, int64_t>(sycl_device);
  297. test_sycl_mem_transfers<DataType, ColMajor, int64_t>(sycl_device);
  298. test_sycl_computations<DataType, ColMajor, int64_t>(sycl_device);
  299. test_sycl_mem_sync<DataType, ColMajor, int64_t>(sycl_device);
  300. test_sycl_cast<DataType, int, RowMajor, int64_t>(sycl_device);
  301. test_sycl_cast<DataType, int, ColMajor, int64_t>(sycl_device);
  302. }
  303. EIGEN_DECLARE_TEST(cxx11_tensor_sycl) {
  304. for (const auto& device :Eigen::get_sycl_supported_devices()) {
  305. CALL_SUBTEST(sycl_computing_test_per_device<float>(device));
  306. }
  307. }