TensorContractionGpu.h
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2014-2015 Benoit Steiner <benoit.steiner.goog@gmail.com>
5 // Copyright (C) 2015 Navdeep Jaitly <ndjaitly@google.com>
6 // Copyright (C) 2014 Eric Martin <eric@ericmart.in>
7 //
8 // This Source Code Form is subject to the terms of the Mozilla
9 // Public License v. 2.0. If a copy of the MPL was not distributed
10 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
11 
12 #ifndef EIGEN_CXX11_TENSOR_TENSOR_CONTRACTION_GPU_H
13 #define EIGEN_CXX11_TENSOR_TENSOR_CONTRACTION_GPU_H
14 
15 #if defined(EIGEN_USE_GPU) && defined(EIGEN_GPUCC)
16 
17 #include "./InternalHeaderCheck.h"
18 
19 namespace Eigen {
20 
21 template<typename Scalar, typename Index, typename LhsMapper,
22  typename RhsMapper, typename OutputMapper, bool needs_edge_check>
23 __device__ EIGEN_STRONG_INLINE void
24 EigenContractionKernelInternal(const LhsMapper lhs, const RhsMapper rhs,
25  const OutputMapper output, Scalar* lhs_shmem, Scalar* rhs_shmem,
26  const Index m_size, const Index n_size, const Index k_size) {
27 
28  const Index m_block_idx = blockIdx.x;
29  const Index n_block_idx = blockIdx.y;
30 
31  const Index base_m = 64 * m_block_idx;
32  const Index base_n = 64 * n_block_idx;
33 
34  // declare and initialize 64 registers for output 8x8 block
35 
36  // prefetch registers
37  Scalar lhs_pf0;
38  Scalar lhs_pf1;
39  Scalar lhs_pf2;
40  Scalar lhs_pf3;
41  Scalar lhs_pf4;
42  Scalar lhs_pf5;
43  Scalar lhs_pf6;
44  Scalar lhs_pf7;
45 
46  Scalar rhs_pf0;
47  Scalar rhs_pf1;
48  Scalar rhs_pf2;
49  Scalar rhs_pf3;
50  Scalar rhs_pf4;
51  Scalar rhs_pf5;
52  Scalar rhs_pf6;
53  Scalar rhs_pf7;
54 
55  // shared memory is formatted
56  // (contract idx in block, nocontract idx in block, block idx)
57  // where block idx is column major. This transposition limits the number of
58  // bank conflicts when reading the LHS. The core idea is that since the contracting
59  // index is shared by both sides, then the contracting index should be in threadIdx.x.
60 
61  // On the LHS, we pad each row inside of each block with an extra element. This makes
62  // each block 8 rows of 9 elements, which is 72 elements. This gives no bank conflicts
63  // on writes and very few 2-way conflicts on reads. There is an 8x8 grid of these blocks.
64 
65  // On the RHS we just add 8 padding elements to the end of each block. This gives no bank
66  // conflicts on writes and also none on reads.
67 
68  // storage indices
69  const Index lhs_store_idx_base = threadIdx.y * 72 + threadIdx.x * 9 + threadIdx.z;
70  const Index rhs_store_idx_base = threadIdx.y * 72 + threadIdx.z * 8 + threadIdx.x;
71 
72  const Index lhs_store_idx_0 = lhs_store_idx_base + 576 * 0;
73  const Index lhs_store_idx_1 = lhs_store_idx_base + 576 * 1;
74  const Index lhs_store_idx_2 = lhs_store_idx_base + 576 * 2;
75  const Index lhs_store_idx_3 = lhs_store_idx_base + 576 * 3;
76  const Index lhs_store_idx_4 = lhs_store_idx_base + 576 * 4;
77  const Index lhs_store_idx_5 = lhs_store_idx_base + 576 * 5;
78  const Index lhs_store_idx_6 = lhs_store_idx_base + 576 * 6;
79  const Index lhs_store_idx_7 = lhs_store_idx_base + 576 * 7;
80 
81  const Index rhs_store_idx_0 = rhs_store_idx_base + 576 * 0;
82  const Index rhs_store_idx_1 = rhs_store_idx_base + 576 * 1;
83  const Index rhs_store_idx_2 = rhs_store_idx_base + 576 * 2;
84  const Index rhs_store_idx_3 = rhs_store_idx_base + 576 * 3;
85  const Index rhs_store_idx_4 = rhs_store_idx_base + 576 * 4;
86  const Index rhs_store_idx_5 = rhs_store_idx_base + 576 * 5;
87  const Index rhs_store_idx_6 = rhs_store_idx_base + 576 * 6;
88  const Index rhs_store_idx_7 = rhs_store_idx_base + 576 * 7;
89 
90  // in the loading code, the following variables are important:
91  // threadIdx.x: the vertical position in an 8x8 block
92  // threadIdx.y: the vertical index of the 8x8 block in the grid
93  // threadIdx.z: the horizontal position in an 8x8 block
94  // k: the horizontal index of the 8x8 block in the grid
95  //
96  // The k parameter is implicit (it was the loop counter for a loop that went
97  // from 0 to <8, but now that loop is unrolled in the below code.
98 
99  const Index load_idx_vert = threadIdx.x + 8 * threadIdx.y;
100  const Index lhs_vert = base_m + load_idx_vert;
101 
102 #define prefetchIntoRegisters(base_k) \
103  { \
104  lhs_pf0 = conv(0); \
105  lhs_pf1 = conv(0); \
106  lhs_pf2 = conv(0); \
107  lhs_pf3 = conv(0); \
108  lhs_pf4 = conv(0); \
109  lhs_pf5 = conv(0); \
110  lhs_pf6 = conv(0); \
111  lhs_pf7 = conv(0); \
112  \
113  rhs_pf0 = conv(0); \
114  rhs_pf1 = conv(0); \
115  rhs_pf2 = conv(0); \
116  rhs_pf3 = conv(0); \
117  rhs_pf4 = conv(0); \
118  rhs_pf5 = conv(0); \
119  rhs_pf6 = conv(0); \
120  rhs_pf7 = conv(0); \
121  \
122  if (!needs_edge_check || lhs_vert < m_size) { \
123  const Index lhs_horiz_0 = base_k + threadIdx.z + 0 * 8; \
124  const Index lhs_horiz_1 = base_k + threadIdx.z + 1 * 8; \
125  const Index lhs_horiz_2 = base_k + threadIdx.z + 2 * 8; \
126  const Index lhs_horiz_3 = base_k + threadIdx.z + 3 * 8; \
127  const Index lhs_horiz_4 = base_k + threadIdx.z + 4 * 8; \
128  const Index lhs_horiz_5 = base_k + threadIdx.z + 5 * 8; \
129  const Index lhs_horiz_6 = base_k + threadIdx.z + 6 * 8; \
130  const Index lhs_horiz_7 = base_k + threadIdx.z + 7 * 8; \
131  \
132  if (!needs_edge_check || lhs_horiz_7 < k_size) { \
133  lhs_pf0 = lhs(lhs_vert, lhs_horiz_0); \
134  lhs_pf1 = lhs(lhs_vert, lhs_horiz_1); \
135  lhs_pf2 = lhs(lhs_vert, lhs_horiz_2); \
136  lhs_pf3 = lhs(lhs_vert, lhs_horiz_3); \
137  lhs_pf4 = lhs(lhs_vert, lhs_horiz_4); \
138  lhs_pf5 = lhs(lhs_vert, lhs_horiz_5); \
139  lhs_pf6 = lhs(lhs_vert, lhs_horiz_6); \
140  lhs_pf7 = lhs(lhs_vert, lhs_horiz_7); \
141  } else if (lhs_horiz_6 < k_size) { \
142  lhs_pf0 = lhs(lhs_vert, lhs_horiz_0); \
143  lhs_pf1 = lhs(lhs_vert, lhs_horiz_1); \
144  lhs_pf2 = lhs(lhs_vert, lhs_horiz_2); \
145  lhs_pf3 = lhs(lhs_vert, lhs_horiz_3); \
146  lhs_pf4 = lhs(lhs_vert, lhs_horiz_4); \
147  lhs_pf5 = lhs(lhs_vert, lhs_horiz_5); \
148  lhs_pf6 = lhs(lhs_vert, lhs_horiz_6); \
149  } else if (lhs_horiz_5 < k_size) { \
150  lhs_pf0 = lhs(lhs_vert, lhs_horiz_0); \
151  lhs_pf1 = lhs(lhs_vert, lhs_horiz_1); \
152  lhs_pf2 = lhs(lhs_vert, lhs_horiz_2); \
153  lhs_pf3 = lhs(lhs_vert, lhs_horiz_3); \
154  lhs_pf4 = lhs(lhs_vert, lhs_horiz_4); \
155  lhs_pf5 = lhs(lhs_vert, lhs_horiz_5); \
156  } else if (lhs_horiz_4 < k_size) { \
157  lhs_pf0 = lhs(lhs_vert, lhs_horiz_0); \
158  lhs_pf1 = lhs(lhs_vert, lhs_horiz_1); \
159  lhs_pf2 = lhs(lhs_vert, lhs_horiz_2); \
160  lhs_pf3 = lhs(lhs_vert, lhs_horiz_3); \
161  lhs_pf4 = lhs(lhs_vert, lhs_horiz_4); \
162  } else if (lhs_horiz_3 < k_size) { \
163  lhs_pf0 = lhs(lhs_vert, lhs_horiz_0); \
164  lhs_pf1 = lhs(lhs_vert, lhs_horiz_1); \
165  lhs_pf2 = lhs(lhs_vert, lhs_horiz_2); \
166  lhs_pf3 = lhs(lhs_vert, lhs_horiz_3); \
167  } else if (lhs_horiz_2 < k_size) { \
168  lhs_pf0 = lhs(lhs_vert, lhs_horiz_0); \
169  lhs_pf1 = lhs(lhs_vert, lhs_horiz_1); \
170  lhs_pf2 = lhs(lhs_vert, lhs_horiz_2); \
171  } else if (lhs_horiz_1 < k_size) { \
172  lhs_pf0 = lhs(lhs_vert, lhs_horiz_0); \
173  lhs_pf1 = lhs(lhs_vert, lhs_horiz_1); \
174  } else if (lhs_horiz_0 < k_size) { \
175  lhs_pf0 = lhs(lhs_vert, lhs_horiz_0); \
176  } \
177  } \
178  \
179  const Index rhs_vert = base_k + load_idx_vert; \
180  if (!needs_edge_check || rhs_vert < k_size) { \
181  const Index rhs_horiz_0 = base_n + threadIdx.z + 0 * 8; \
182  const Index rhs_horiz_1 = base_n + threadIdx.z + 1 * 8; \
183  const Index rhs_horiz_2 = base_n + threadIdx.z + 2 * 8; \
184  const Index rhs_horiz_3 = base_n + threadIdx.z + 3 * 8; \
185  const Index rhs_horiz_4 = base_n + threadIdx.z + 4 * 8; \
186  const Index rhs_horiz_5 = base_n + threadIdx.z + 5 * 8; \
187  const Index rhs_horiz_6 = base_n + threadIdx.z + 6 * 8; \
188  const Index rhs_horiz_7 = base_n + threadIdx.z + 7 * 8; \
189  \
190  if (rhs_horiz_7 < n_size) { \
191  rhs_pf0 = rhs(rhs_vert, rhs_horiz_0); \
192  rhs_pf1 = rhs(rhs_vert, rhs_horiz_1); \
193  rhs_pf2 = rhs(rhs_vert, rhs_horiz_2); \
194  rhs_pf3 = rhs(rhs_vert, rhs_horiz_3); \
195  rhs_pf4 = rhs(rhs_vert, rhs_horiz_4); \
196  rhs_pf5 = rhs(rhs_vert, rhs_horiz_5); \
197  rhs_pf6 = rhs(rhs_vert, rhs_horiz_6); \
198  rhs_pf7 = rhs(rhs_vert, rhs_horiz_7); \
199  } else if (rhs_horiz_6 < n_size) { \
200  rhs_pf0 = rhs(rhs_vert, rhs_horiz_0); \
201  rhs_pf1 = rhs(rhs_vert, rhs_horiz_1); \
202  rhs_pf2 = rhs(rhs_vert, rhs_horiz_2); \
203  rhs_pf3 = rhs(rhs_vert, rhs_horiz_3); \
204  rhs_pf4 = rhs(rhs_vert, rhs_horiz_4); \
205  rhs_pf5 = rhs(rhs_vert, rhs_horiz_5); \
206  rhs_pf6 = rhs(rhs_vert, rhs_horiz_6); \
207  } else if (rhs_horiz_5 < n_size) { \
208  rhs_pf0 = rhs(rhs_vert, rhs_horiz_0); \
209  rhs_pf1 = rhs(rhs_vert, rhs_horiz_1); \
210  rhs_pf2 = rhs(rhs_vert, rhs_horiz_2); \
211  rhs_pf3 = rhs(rhs_vert, rhs_horiz_3); \
212  rhs_pf4 = rhs(rhs_vert, rhs_horiz_4); \
213  rhs_pf5 = rhs(rhs_vert, rhs_horiz_5); \
214  } else if (rhs_horiz_4 < n_size) { \
215  rhs_pf0 = rhs(rhs_vert, rhs_horiz_0); \
216  rhs_pf1 = rhs(rhs_vert, rhs_horiz_1); \
217  rhs_pf2 = rhs(rhs_vert, rhs_horiz_2); \
218  rhs_pf3 = rhs(rhs_vert, rhs_horiz_3); \
219  rhs_pf4 = rhs(rhs_vert, rhs_horiz_4); \
220  } else if (rhs_horiz_3 < n_size) { \
221  rhs_pf0 = rhs(rhs_vert, rhs_horiz_0); \
222  rhs_pf1 = rhs(rhs_vert, rhs_horiz_1); \
223  rhs_pf2 = rhs(rhs_vert, rhs_horiz_2); \
224  rhs_pf3 = rhs(rhs_vert, rhs_horiz_3); \
225  } else if (rhs_horiz_2 < n_size) { \
226  rhs_pf0 = rhs(rhs_vert, rhs_horiz_0); \
227  rhs_pf1 = rhs(rhs_vert, rhs_horiz_1); \
228  rhs_pf2 = rhs(rhs_vert, rhs_horiz_2); \
229  } else if (rhs_horiz_1 < n_size) { \
230  rhs_pf0 = rhs(rhs_vert, rhs_horiz_0); \
231  rhs_pf1 = rhs(rhs_vert, rhs_horiz_1); \
232  } else if (rhs_horiz_0 < n_size) { \
233  rhs_pf0 = rhs(rhs_vert, rhs_horiz_0); \
234  } \
235  } \
236  } \
237 
238 #define writeRegToShmem() \
239  lhs_shmem[lhs_store_idx_0] = lhs_pf0; \
240  rhs_shmem[rhs_store_idx_0] = rhs_pf0; \
241  \
242  lhs_shmem[lhs_store_idx_1] = lhs_pf1; \
243  rhs_shmem[rhs_store_idx_1] = rhs_pf1; \
244  \
245  lhs_shmem[lhs_store_idx_2] = lhs_pf2; \
246  rhs_shmem[rhs_store_idx_2] = rhs_pf2; \
247  \
248  lhs_shmem[lhs_store_idx_3] = lhs_pf3; \
249  rhs_shmem[rhs_store_idx_3] = rhs_pf3; \
250  \
251  lhs_shmem[lhs_store_idx_4] = lhs_pf4; \
252  rhs_shmem[rhs_store_idx_4] = rhs_pf4; \
253  \
254  lhs_shmem[lhs_store_idx_5] = lhs_pf5; \
255  rhs_shmem[rhs_store_idx_5] = rhs_pf5; \
256  \
257  lhs_shmem[lhs_store_idx_6] = lhs_pf6; \
258  rhs_shmem[rhs_store_idx_6] = rhs_pf6; \
259  \
260  lhs_shmem[lhs_store_idx_7] = lhs_pf7; \
261  rhs_shmem[rhs_store_idx_7] = rhs_pf7; \
262 
263  // declare and initialize result array
264 #define res(i, j) _res_##i##j
265 #define initResultRow(i) \
266  Scalar res(i, 0) = conv(0); \
267  Scalar res(i, 1) = conv(0); \
268  Scalar res(i, 2) = conv(0); \
269  Scalar res(i, 3) = conv(0); \
270  Scalar res(i, 4) = conv(0); \
271  Scalar res(i, 5) = conv(0); \
272  Scalar res(i, 6) = conv(0); \
273  Scalar res(i, 7) = conv(0); \
274 
275  internal::scalar_cast_op<int, Scalar> conv;
276  initResultRow(0);
277  initResultRow(1);
278  initResultRow(2);
279  initResultRow(3);
280  initResultRow(4);
281  initResultRow(5);
282  initResultRow(6);
283  initResultRow(7);
284 #undef initResultRow
285 
286  for (Index base_k = 0; base_k < k_size; base_k += 64) {
287  // wait for previous iteration to finish with shmem. Despite common sense,
288  // the code is a bit faster with this here then at bottom of loop
289  __syncthreads();
290 
291  prefetchIntoRegisters(base_k);
292  writeRegToShmem();
293 
294  #undef prefetchIntoRegisters
295  #undef writeRegToShmem
296 
297  // wait for shared mem packing to be done before starting computation
298  __syncthreads();
299 
300  // compute 8x8 matrix product by outer product. This involves packing one column
301  // of LHS and one row of RHS into registers (takes 16 registers).
302 
303 #define lcol(i) _lcol##i
304  Scalar lcol(0);
305  Scalar lcol(1);
306  Scalar lcol(2);
307  Scalar lcol(3);
308  Scalar lcol(4);
309  Scalar lcol(5);
310  Scalar lcol(6);
311  Scalar lcol(7);
312 
313 #define rrow(j) _rrow##j
314  Scalar rrow(0);
315  Scalar rrow(1);
316  Scalar rrow(2);
317  Scalar rrow(3);
318  Scalar rrow(4);
319  Scalar rrow(5);
320  Scalar rrow(6);
321  Scalar rrow(7);
322 
323  // Now x corresponds to k, y to m, and z to n
324  const Scalar* lhs_block = &lhs_shmem[threadIdx.x + 9 * threadIdx.y];
325  const Scalar* rhs_block = &rhs_shmem[threadIdx.x + 8 * threadIdx.z];
326 
327 #define lhs_element(i, j) lhs_block[72 * ((i) + 8 * (j))]
328 #define rhs_element(i, j) rhs_block[72 * ((i) + 8 * (j))]
329 
330 #define loadData(i, j) \
331  lcol(0) = lhs_element(0, j); \
332  rrow(0) = rhs_element(i, 0); \
333  lcol(1) = lhs_element(1, j); \
334  rrow(1) = rhs_element(i, 1); \
335  lcol(2) = lhs_element(2, j); \
336  rrow(2) = rhs_element(i, 2); \
337  lcol(3) = lhs_element(3, j); \
338  rrow(3) = rhs_element(i, 3); \
339  lcol(4) = lhs_element(4, j); \
340  rrow(4) = rhs_element(i, 4); \
341  lcol(5) = lhs_element(5, j); \
342  rrow(5) = rhs_element(i, 5); \
343  lcol(6) = lhs_element(6, j); \
344  rrow(6) = rhs_element(i, 6); \
345  lcol(7) = lhs_element(7, j); \
346  rrow(7) = rhs_element(i, 7); \
347 
348 #define computeCol(j) \
349  res(0, j) += lcol(0) * rrow(j); \
350  res(1, j) += lcol(1) * rrow(j); \
351  res(2, j) += lcol(2) * rrow(j); \
352  res(3, j) += lcol(3) * rrow(j); \
353  res(4, j) += lcol(4) * rrow(j); \
354  res(5, j) += lcol(5) * rrow(j); \
355  res(6, j) += lcol(6) * rrow(j); \
356  res(7, j) += lcol(7) * rrow(j); \
357 
358 #define computePass(i) \
359  loadData(i, i); \
360  \
361  computeCol(0); \
362  computeCol(1); \
363  computeCol(2); \
364  computeCol(3); \
365  computeCol(4); \
366  computeCol(5); \
367  computeCol(6); \
368  computeCol(7); \
369 
370  computePass(0);
371  computePass(1);
372  computePass(2);
373  computePass(3);
374  computePass(4);
375  computePass(5);
376  computePass(6);
377  computePass(7);
378 
379 #undef lcol
380 #undef rrow
381 #undef lhs_element
382 #undef rhs_element
383 #undef loadData
384 #undef computeCol
385 #undef computePass
386  } // end loop over k
387 
388  // we've now iterated over all of the large (ie width 64) k blocks and
389  // accumulated results in registers. At this point thread (x, y, z) contains
390  // the sum across all big k blocks of the product of little k block of index (x, y)
391  // with block of index (y, z). To compute the final output, we need to reduce
392  // the 8 threads over y by summation.
393 #if defined(EIGEN_HIPCC) || (defined(EIGEN_CUDA_SDK_VER) && EIGEN_CUDA_SDK_VER < 90000)
394 #define shuffleInc(i, j, mask) res(i, j) += __shfl_xor(res(i, j), mask)
395 #else
396 #define shuffleInc(i, j, mask) res(i, j) += __shfl_xor_sync(0xFFFFFFFF, res(i, j), mask)
397 #endif
398 
399 #define reduceRow(i, mask) \
400  shuffleInc(i, 0, mask); \
401  shuffleInc(i, 1, mask); \
402  shuffleInc(i, 2, mask); \
403  shuffleInc(i, 3, mask); \
404  shuffleInc(i, 4, mask); \
405  shuffleInc(i, 5, mask); \
406  shuffleInc(i, 6, mask); \
407  shuffleInc(i, 7, mask); \
408 
409 #define reduceMatrix(mask) \
410  reduceRow(0, mask); \
411  reduceRow(1, mask); \
412  reduceRow(2, mask); \
413  reduceRow(3, mask); \
414  reduceRow(4, mask); \
415  reduceRow(5, mask); \
416  reduceRow(6, mask); \
417  reduceRow(7, mask); \
418 
419  // actually perform the reduction, now each thread of index (_, y, z)
420  // contains the correct values in its registers that belong in the output
421  // block
422  reduceMatrix(1);
423  reduceMatrix(2);
424  reduceMatrix(4);
425 
426 #undef shuffleInc
427 #undef reduceRow
428 #undef reduceMatrix
429 
430  // now we need to copy the 64 values into main memory. We can't split work
431  // among threads because all variables are in registers. There's 2 ways
432  // to do this:
433  // (1) have 1 thread do 64 writes from registers into global memory
434  // (2) have 1 thread do 64 writes into shared memory, and then 8 threads
435  // each do 8 writes into global memory. We can just overwrite the shared
436  // memory from the problem we just solved.
437  // (2) is slightly faster than (1) due to less branching and more ILP
438 
439  // TODO: won't yield much gain, but could just use currently unused shared mem
440  // and then we won't have to sync
441  // wait for shared mem to be out of use
442  __syncthreads();
443 
444 #define writeResultShmem(i, j) \
445  lhs_shmem[i + 8 * threadIdx.y + 64 * threadIdx.z + 512 * j] = res(i, j); \
446 
447 #define writeRow(i) \
448  writeResultShmem(i, 0); \
449  writeResultShmem(i, 1); \
450  writeResultShmem(i, 2); \
451  writeResultShmem(i, 3); \
452  writeResultShmem(i, 4); \
453  writeResultShmem(i, 5); \
454  writeResultShmem(i, 6); \
455  writeResultShmem(i, 7); \
456 
457  if (threadIdx.x == 0) {
458  writeRow(0);
459  writeRow(1);
460  writeRow(2);
461  writeRow(3);
462  writeRow(4);
463  writeRow(5);
464  writeRow(6);
465  writeRow(7);
466  }
467 #undef writeResultShmem
468 #undef writeRow
469 
470  const int max_i_write = numext::mini((int)((m_size - base_m - threadIdx.y + 7) / 8), 8);
471  const int max_j_write = numext::mini((int)((n_size - base_n - threadIdx.z + 7) / 8), 8);
472 
473  if (threadIdx.x < max_i_write) {
474  if (max_j_write == 8) {
475  // TODO: can i trade bank conflicts for coalesced writes?
476  Scalar val0 = lhs_shmem[threadIdx.x + 8 * threadIdx.y + 64 * threadIdx.z + 512 * 0];
477  Scalar val1 = lhs_shmem[threadIdx.x + 8 * threadIdx.y + 64 * threadIdx.z + 512 * 1];
478  Scalar val2 = lhs_shmem[threadIdx.x + 8 * threadIdx.y + 64 * threadIdx.z + 512 * 2];
479  Scalar val3 = lhs_shmem[threadIdx.x + 8 * threadIdx.y + 64 * threadIdx.z + 512 * 3];
480  Scalar val4 = lhs_shmem[threadIdx.x + 8 * threadIdx.y + 64 * threadIdx.z + 512 * 4];
481  Scalar val5 = lhs_shmem[threadIdx.x + 8 * threadIdx.y + 64 * threadIdx.z + 512 * 5];
482  Scalar val6 = lhs_shmem[threadIdx.x + 8 * threadIdx.y + 64 * threadIdx.z + 512 * 6];
483  Scalar val7 = lhs_shmem[threadIdx.x + 8 * threadIdx.y + 64 * threadIdx.z + 512 * 7];
484 
485  output(base_m + threadIdx.y + 8 * threadIdx.x, base_n + threadIdx.z + 8 * 0) = val0;
486  output(base_m + threadIdx.y + 8 * threadIdx.x, base_n + threadIdx.z + 8 * 1) = val1;
487  output(base_m + threadIdx.y + 8 * threadIdx.x, base_n + threadIdx.z + 8 * 2) = val2;
488  output(base_m + threadIdx.y + 8 * threadIdx.x, base_n + threadIdx.z + 8 * 3) = val3;
489  output(base_m + threadIdx.y + 8 * threadIdx.x, base_n + threadIdx.z + 8 * 4) = val4;
490  output(base_m + threadIdx.y + 8 * threadIdx.x, base_n + threadIdx.z + 8 * 5) = val5;
491  output(base_m + threadIdx.y + 8 * threadIdx.x, base_n + threadIdx.z + 8 * 6) = val6;
492  output(base_m + threadIdx.y + 8 * threadIdx.x, base_n + threadIdx.z + 8 * 7) = val7;
493  } else {
494 #pragma unroll 7
495  for (int j = 0; j < max_j_write; j++) {
496  Scalar val = lhs_shmem[threadIdx.x + 8 * threadIdx.y + 64 * threadIdx.z + 512 * j];
497  output(base_m + threadIdx.y + 8 * threadIdx.x, base_n + threadIdx.z + 8 * j) = val;
498  }
499  }
500  }
501 #undef res
502 }
503 
504 
505 template<typename Scalar, typename Index, typename LhsMapper,
506  typename RhsMapper, typename OutputMapper>
507 __global__ void
508 #if defined(EIGEN_HIPCC)
509 __launch_bounds__(512, 1)
510 #else
511 __launch_bounds__(512)
512 #endif
513 EigenContractionKernel(const LhsMapper lhs, const RhsMapper rhs,
514  const OutputMapper output,
515  const Index m_size, const Index n_size, const Index k_size) {
516  __shared__ Scalar lhs_shmem[72 * 64];
517  __shared__ Scalar rhs_shmem[72 * 64];
518 
519  const Index m_block_idx = blockIdx.x;
520  const Index n_block_idx = blockIdx.y;
521 
522  const Index base_m = 64 * m_block_idx;
523  const Index base_n = 64 * n_block_idx;
524 
525  if (base_m + 63 < m_size && base_n + 63 < n_size) {
526  EigenContractionKernelInternal<Scalar, Index, LhsMapper, RhsMapper, OutputMapper, false>(lhs, rhs, output, lhs_shmem, rhs_shmem, m_size, n_size, k_size);
527  } else {
528  EigenContractionKernelInternal<Scalar, Index, LhsMapper, RhsMapper, OutputMapper, true>(lhs, rhs, output, lhs_shmem, rhs_shmem, m_size, n_size, k_size);
529  }
530 }
531 
532 
533 template<typename Index, typename LhsMapper,
534  typename RhsMapper, typename OutputMapper, bool CHECK_LHS_BOUNDARY,
535  bool CHECK_RHS_BOUNDARY>
536 __device__ __forceinline__ void
537 EigenFloatContractionKernelInternal16x16(const LhsMapper lhs, const RhsMapper rhs,
538  const OutputMapper output, float2 lhs_shmem2[][16],
539  float2 rhs_shmem2[][8], const Index m_size,
540  const Index n_size, const Index k_size,
541  const Index base_m, const Index base_n) {
542 
543  // prefetch registers
544  float4 lhs_pf0, rhs_pf0;
545 
546  float4 results[4];
547  for (int i=0; i < 4; i++) {
548  results[i].x = results[i].y = results[i].z = results[i].w = 0;
549  }
550 
551 #define prefetch_lhs(reg, row, col) \
552  if (!CHECK_LHS_BOUNDARY) { \
553  if (col < k_size) { \
554  reg =lhs.template loadPacket<float4,Unaligned>(row, col); \
555  } \
556  } else { \
557  if (col < k_size) { \
558  if (row + 3 < m_size) { \
559  reg =lhs.template loadPacket<float4,Unaligned>(row, col); \
560  } else if (row + 2 < m_size) { \
561  reg.x =lhs(row + 0, col); \
562  reg.y =lhs(row + 1, col); \
563  reg.z =lhs(row + 2, col); \
564  } else if (row + 1 < m_size) { \
565  reg.x =lhs(row + 0, col); \
566  reg.y =lhs(row + 1, col); \
567  } else if (row < m_size) { \
568  reg.x =lhs(row + 0, col); \
569  } \
570  } \
571  } \
572 
573  Index lhs_vert = base_m+threadIdx.x*4;
574 
575  for (Index k = 0; k < k_size; k += 16) {
576 
577  lhs_pf0 = internal::pset1<float4>(0);
578  rhs_pf0 = internal::pset1<float4>(0);
579 
580  Index lhs_horiz = threadIdx.y+k;
581  prefetch_lhs(lhs_pf0, lhs_vert, lhs_horiz)
582 
583  Index rhs_vert = k+(threadIdx.x%4)*4;
584  Index rhs_horiz0 = (threadIdx.x>>2)+threadIdx.y*4+base_n;
585 
586  if (!CHECK_RHS_BOUNDARY) {
587  if ((rhs_vert + 3) < k_size) {
588  // just CHECK_RHS_BOUNDARY
589  rhs_pf0 = rhs.template loadPacket<float4,Unaligned>(rhs_vert, rhs_horiz0);
590  } else if (rhs_vert + 2 < k_size) {
591  // just CHECK_RHS_BOUNDARY
592  rhs_pf0.x = rhs(rhs_vert, rhs_horiz0);
593  rhs_pf0.y = rhs(rhs_vert + 1, rhs_horiz0);
594  rhs_pf0.z = rhs(rhs_vert + 2, rhs_horiz0);
595  } else if (rhs_vert + 1 < k_size) {
596  rhs_pf0.x = rhs(rhs_vert, rhs_horiz0);
597  rhs_pf0.y = rhs(rhs_vert + 1, rhs_horiz0);
598  } else if (rhs_vert < k_size) {
599  rhs_pf0.x = rhs(rhs_vert, rhs_horiz0);
600  }
601  } else {
602  if (rhs_horiz0 < n_size) {
603  if ((rhs_vert + 3) < k_size) {
604  rhs_pf0 = rhs.template loadPacket<float4,Unaligned>(rhs_vert, rhs_horiz0);
605  } else if ((rhs_vert + 2) < k_size) {
606  rhs_pf0.x = rhs(rhs_vert, rhs_horiz0);
607  rhs_pf0.y = rhs(rhs_vert + 1, rhs_horiz0);
608  rhs_pf0.z = rhs(rhs_vert + 2, rhs_horiz0);
609  } else if ((rhs_vert + 1) < k_size) {
610  rhs_pf0.x = rhs(rhs_vert, rhs_horiz0);
611  rhs_pf0.y = rhs(rhs_vert + 1, rhs_horiz0);
612  } else if (rhs_vert < k_size) {
613  rhs_pf0.x = rhs(rhs_vert, rhs_horiz0);
614  }
615  }
616  }
617  float x1, x2 ;
618  // the following can be a bitwise operation..... some day.
619  if((threadIdx.x%8) < 4) {
620  x1 = rhs_pf0.y;
621  x2 = rhs_pf0.w;
622  } else {
623  x1 = rhs_pf0.x;
624  x2 = rhs_pf0.z;
625  }
626  #if defined(EIGEN_HIPCC) || (defined(EIGEN_CUDA_SDK_VER) && EIGEN_CUDA_SDK_VER < 90000)
627  x1 = __shfl_xor(x1, 4);
628  x2 = __shfl_xor(x2, 4);
629  #else
630  x1 = __shfl_xor_sync(0xFFFFFFFF, x1, 4);
631  x2 = __shfl_xor_sync(0xFFFFFFFF, x2, 4);
632  #endif
633  if((threadIdx.x%8) < 4) {
634  rhs_pf0.y = x1;
635  rhs_pf0.w = x2;
636  } else {
637  rhs_pf0.x = x1;
638  rhs_pf0.z = x2;
639  }
640 
641  // We have 64 features.
642  // Row 0 -> times (0, 4, 8, 12, 1, 5, 9, 13) for features 0, 1.
643  // Row 1 -> times (0, 4, 8, 12, 1, 5, 9, 13) for features 2, 3.
644  // ...
645  // Row 31 -> times (0, 4, 8, 12, 1, 5, 9, 13) for features 62, 63
646  // Row 32 -> times (2, 6, 10, 14, 3, 7, 11, 15) for features 0, 1
647  // ...
648  rhs_shmem2[(threadIdx.x>>3)+ threadIdx.y*2][threadIdx.x%8] = make_float2(rhs_pf0.x, rhs_pf0.y);
649  rhs_shmem2[(threadIdx.x>>3)+ threadIdx.y*2+32][threadIdx.x%8] = make_float2(rhs_pf0.z, rhs_pf0.w);
650 
651  // Row 0 (time 0) -> features (0, 1), (4, 5), .. (28, 29), (32, 33), .. (60, 61)
652  // Row 1 (time 1) -> features (0, 1), (4, 5), .. (28, 29), (32, 33), .. (60, 61)
653  // ...
654  // Row 15 (time 15) -> features (0, 1), (4, 5), .. (28, 29), (32, 33), .. (60, 61)
655  // Row 16 (time 0) -> features (2, 3), (6, 7), .. (30, 31), (34, 35), .. (62, 63)
656  // ...
657 
658  lhs_shmem2[threadIdx.y][threadIdx.x] = make_float2(lhs_pf0.x, lhs_pf0.y);
659  lhs_shmem2[threadIdx.y+16][threadIdx.x] = make_float2(lhs_pf0.z, lhs_pf0.w);
660 
661 
662 #define add_vals(fl1, fl2, fr1, fr2)\
663  results[0].x += fl1.x * fr1.x;\
664  results[0].y += fl1.y * fr1.x;\
665  results[0].z += fl2.x * fr1.x;\
666  results[0].w += fl2.y * fr1.x;\
667 \
668  results[1].x += fl1.x * fr1.y;\
669  results[1].y += fl1.y * fr1.y;\
670  results[1].z += fl2.x * fr1.y;\
671  results[1].w += fl2.y * fr1.y;\
672 \
673  results[2].x += fl1.x * fr2.x;\
674  results[2].y += fl1.y * fr2.x;\
675  results[2].z += fl2.x * fr2.x;\
676  results[2].w += fl2.y * fr2.x;\
677 \
678  results[3].x += fl1.x * fr2.y;\
679  results[3].y += fl1.y * fr2.y;\
680  results[3].z += fl2.x * fr2.y;\
681  results[3].w += fl2.y * fr2.y;\
682 
683  __syncthreads();
684 
685  // Do the multiplies.
686  #pragma unroll
687  for (int koff = 0; koff < 16; koff ++) {
688  // 32 x threads.
689  float2 fl1 = lhs_shmem2[koff][threadIdx.x];
690  float2 fl2 = lhs_shmem2[koff + 16][threadIdx.x];
691 
692  int start_feature = threadIdx.y * 4;
693  float2 fr1 = rhs_shmem2[(start_feature>>1) + 32*((koff%4)/2)][koff/4 + (koff%2)*4];
694  float2 fr2 = rhs_shmem2[(start_feature>>1) + 1 + 32*((koff%4)/2)][koff/4 + (koff%2)*4];
695 
696  add_vals(fl1, fl2, fr1, fr2)
697  }
698  __syncthreads();
699  }
700 
701 #undef prefetch_lhs
702 #undef add_vals
703 
704  Index horiz_base = threadIdx.y*4+base_n;
705  if (!CHECK_LHS_BOUNDARY && !CHECK_RHS_BOUNDARY) {
706  for (int i = 0; i < 4; i++) {
707  output(lhs_vert, horiz_base + i) = results[i].x;
708  output(lhs_vert + 1, horiz_base + i) = results[i].y;
709  output(lhs_vert + 2, horiz_base + i) = results[i].z;
710  output(lhs_vert + 3, horiz_base + i) = results[i].w;
711  }
712  } else if (!CHECK_RHS_BOUNDARY) {
713  // CHECK LHS
714  if (lhs_vert + 3 < m_size) {
715  for (int i = 0; i < 4; i++) {
716  output(lhs_vert, horiz_base + i) = results[i].x;
717  output(lhs_vert + 1, horiz_base + i) = results[i].y;
718  output(lhs_vert + 2, horiz_base + i) = results[i].z;
719  output(lhs_vert + 3, horiz_base + i) = results[i].w;
720  }
721  } else if (lhs_vert + 2 < m_size) {
722  for (int i = 0; i < 4; i++) {
723  output(lhs_vert, horiz_base + i) = results[i].x;
724  output(lhs_vert + 1, horiz_base + i) = results[i].y;
725  output(lhs_vert + 2, horiz_base + i) = results[i].z;
726  }
727  } else if (lhs_vert + 1 < m_size) {
728  for (int i = 0; i < 4; i++) {
729  output(lhs_vert, horiz_base + i) = results[i].x;
730  output(lhs_vert + 1, horiz_base + i) = results[i].y;
731  }
732  } else if (lhs_vert < m_size) {
733  for (int i = 0; i < 4; i++) {
734  output(lhs_vert, horiz_base + i) = results[i].x;
735  }
736  }
737  } else if (!CHECK_LHS_BOUNDARY) {
738  // CHECK RHS
739  /*
740  int ncols_rem = fminf(n_size- horiz_base, 4);
741  for (int i = 0; i < ncols_rem; i++) {
742  output(lhs_vert, horiz_base + i) = results[i].x;
743  output(lhs_vert + 1, horiz_base + i) = results[i].y;
744  output(lhs_vert + 2, horiz_base + i) = results[i].z;
745  output(lhs_vert + 3, horiz_base + i) = results[i].w;
746  }*/
747  for (int i = 0; i < 4; i++) {
748  if (horiz_base+i < n_size) {
749  output(lhs_vert, horiz_base + i) = results[i].x;
750  output(lhs_vert + 1, horiz_base + i) = results[i].y;
751  output(lhs_vert + 2, horiz_base + i) = results[i].z;
752  output(lhs_vert + 3, horiz_base + i) = results[i].w;
753  }
754  }
755  } else {
756  // CHECK both boundaries.
757  for (int i = 0; i < 4; i++) {
758  if (horiz_base+i < n_size) {
759  if (lhs_vert < m_size)
760  output(lhs_vert, horiz_base + i) = results[i].x;
761  if (lhs_vert + 1 < m_size)
762  output(lhs_vert + 1, horiz_base + i) = results[i].y;
763  if (lhs_vert + 2 < m_size)
764  output(lhs_vert + 2, horiz_base + i) = results[i].z;
765  if (lhs_vert + 3 < m_size)
766  output(lhs_vert + 3, horiz_base + i) = results[i].w;
767  }
768  }
769  }
770 }
771 
772 
773 template<typename Index, typename LhsMapper,
774  typename RhsMapper, typename OutputMapper, bool CHECK_LHS_BOUNDARY,
775  bool CHECK_RHS_BOUNDARY>
776 __device__ __forceinline__ void
777 EigenFloatContractionKernelInternal(const LhsMapper lhs, const RhsMapper rhs,
778  const OutputMapper output, float2 lhs_shmem2[][32],
779  float2 rhs_shmem2[][8], const Index m_size,
780  const Index n_size, const Index k_size,
781  const Index base_m, const Index base_n) {
782 
783  // prefetch registers
784  float4 lhs_pf0, lhs_pf1, lhs_pf2, lhs_pf3;
785  float4 rhs_pf0, rhs_pf1;
786 
787  float4 results[8];
788  for (int i=0; i < 8; i++) {
789  results[i].x = results[i].y = results[i].z = results[i].w = 0;
790  }
791 
792  Index lhs_vert = base_m+threadIdx.x*4+(threadIdx.y%4)*32;
793  for (Index k = 0; k < k_size; k += 32) {
794  lhs_pf0 = internal::pset1<float4>(0);
795  lhs_pf1 = internal::pset1<float4>(0);
796  lhs_pf2 = internal::pset1<float4>(0);
797  lhs_pf3 = internal::pset1<float4>(0);
798 
799  rhs_pf0 = internal::pset1<float4>(0);
800  rhs_pf1 = internal::pset1<float4>(0);
801 
802  if (!CHECK_LHS_BOUNDARY) {
803  if ((threadIdx.y/4+k+24) < k_size) {
804  lhs_pf0 =lhs.template loadPacket<float4,Unaligned>(lhs_vert, (threadIdx.y/4+k));
805  lhs_pf1 =lhs.template loadPacket<float4,Unaligned>(lhs_vert, (threadIdx.y/4+k+8));
806  lhs_pf2 =lhs.template loadPacket<float4,Unaligned>(lhs_vert, (threadIdx.y/4+k+16));
807  lhs_pf3 =lhs.template loadPacket<float4,Unaligned>(lhs_vert, (threadIdx.y/4+k+24));
808  } else if ((threadIdx.y/4+k+16) < k_size) {
809  lhs_pf0 =lhs.template loadPacket<float4,Unaligned>(lhs_vert, (threadIdx.y/4+k));
810  lhs_pf1 =lhs.template loadPacket<float4,Unaligned>(lhs_vert, (threadIdx.y/4+k+8));
811  lhs_pf2 =lhs.template loadPacket<float4,Unaligned>(lhs_vert, (threadIdx.y/4+k+16));
812  } else if ((threadIdx.y/4+k+8) < k_size) {
813  lhs_pf0 =lhs.template loadPacket<float4,Unaligned>(lhs_vert, (threadIdx.y/4+k));
814  lhs_pf1 =lhs.template loadPacket<float4,Unaligned>(lhs_vert, (threadIdx.y/4+k+8));
815  } else if ((threadIdx.y/4+k) < k_size) {
816  lhs_pf0 =lhs.template loadPacket<float4,Unaligned>(lhs_vert, (threadIdx.y/4+k));
817  }
818  } else {
819  // just CHECK_LHS_BOUNDARY
820  if (lhs_vert + 3 < m_size) {
821  if ((threadIdx.y/4+k+24) < k_size) {
822  lhs_pf0 =lhs.template loadPacket<float4,Unaligned>(lhs_vert, (threadIdx.y/4+k));
823  lhs_pf1 =lhs.template loadPacket<float4,Unaligned>(lhs_vert, (threadIdx.y/4+k+8));
824  lhs_pf2 =lhs.template loadPacket<float4,Unaligned>(lhs_vert, (threadIdx.y/4+k+16));
825  lhs_pf3 =lhs.template loadPacket<float4,Unaligned>(lhs_vert, (threadIdx.y/4+k+24));
826  } else if ((threadIdx.y/4+k+16) < k_size) {
827  lhs_pf0 =lhs.template loadPacket<float4,Unaligned>(lhs_vert, (threadIdx.y/4+k));
828  lhs_pf1 =lhs.template loadPacket<float4,Unaligned>(lhs_vert, (threadIdx.y/4+k+8));
829  lhs_pf2 =lhs.template loadPacket<float4,Unaligned>(lhs_vert, (threadIdx.y/4+k+16));
830  } else if ((threadIdx.y/4+k+8) < k_size) {
831  lhs_pf0 =lhs.template loadPacket<float4,Unaligned>(lhs_vert, (threadIdx.y/4+k));
832  lhs_pf1 =lhs.template loadPacket<float4,Unaligned>(lhs_vert, (threadIdx.y/4+k+8));
833  } else if ((threadIdx.y/4+k) < k_size) {
834  lhs_pf0 =lhs.template loadPacket<float4,Unaligned>(lhs_vert, (threadIdx.y/4+k));
835  }
836  } else if (lhs_vert + 2 < m_size) {
837  if ((threadIdx.y/4+k+24) < k_size) {
838  lhs_pf0.x =lhs(lhs_vert + 0, (threadIdx.y/4+k));
839  lhs_pf0.y =lhs(lhs_vert + 1, (threadIdx.y/4+k));
840  lhs_pf0.z =lhs(lhs_vert + 2, (threadIdx.y/4+k));
841  lhs_pf1.x =lhs(lhs_vert + 0, (threadIdx.y/4+k+8));
842  lhs_pf1.y =lhs(lhs_vert + 1, (threadIdx.y/4+k+8));
843  lhs_pf1.z =lhs(lhs_vert + 2, (threadIdx.y/4+k+8));
844  lhs_pf2.x =lhs(lhs_vert + 0, (threadIdx.y/4+k+16));
845  lhs_pf2.y =lhs(lhs_vert + 1, (threadIdx.y/4+k+16));
846  lhs_pf2.z =lhs(lhs_vert + 2, (threadIdx.y/4+k+16));
847  lhs_pf3.x =lhs(lhs_vert + 0, (threadIdx.y/4+k+24));
848  lhs_pf3.y =lhs(lhs_vert + 1, (threadIdx.y/4+k+24));
849  lhs_pf3.z =lhs(lhs_vert + 2, (threadIdx.y/4+k+24));
850  } else if ((threadIdx.y/4+k+16) < k_size) {
851  lhs_pf0.x =lhs(lhs_vert + 0, (threadIdx.y/4+k));
852  lhs_pf0.y =lhs(lhs_vert + 1, (threadIdx.y/4+k));
853  lhs_pf0.z =lhs(lhs_vert + 2, (threadIdx.y/4+k));
854  lhs_pf1.x =lhs(lhs_vert + 0, (threadIdx.y/4+k+8));
855  lhs_pf1.y =lhs(lhs_vert + 1, (threadIdx.y/4+k+8));
856  lhs_pf1.z =lhs(lhs_vert + 2, (threadIdx.y/4+k+8));
857  lhs_pf2.x =lhs(lhs_vert + 0, (threadIdx.y/4+k+16));
858  lhs_pf2.y =lhs(lhs_vert + 1, (threadIdx.y/4+k+16));
859  lhs_pf2.z =lhs(lhs_vert + 2, (threadIdx.y/4+k+16));
860  } else if ((threadIdx.y/4+k+8) < k_size) {
861  lhs_pf0.x =lhs(lhs_vert + 0, (threadIdx.y/4+k));
862  lhs_pf0.y =lhs(lhs_vert + 1, (threadIdx.y/4+k));
863  lhs_pf0.z =lhs(lhs_vert + 2, (threadIdx.y/4+k));
864  lhs_pf1.x =lhs(lhs_vert + 0, (threadIdx.y/4+k+8));
865  lhs_pf1.y =lhs(lhs_vert + 1, (threadIdx.y/4+k+8));
866  lhs_pf1.z =lhs(lhs_vert + 2, (threadIdx.y/4+k+8));
867  } else if ((threadIdx.y/4+k) < k_size) {
868  lhs_pf0.x =lhs(lhs_vert + 0, (threadIdx.y/4+k));
869  lhs_pf0.y =lhs(lhs_vert + 1, (threadIdx.y/4+k));
870  lhs_pf0.z =lhs(lhs_vert + 2, (threadIdx.y/4+k));
871  }
872  } else if (lhs_vert + 1 < m_size) {
873  if ((threadIdx.y/4+k+24) < k_size) {
874  lhs_pf0.x =lhs(lhs_vert + 0, (threadIdx.y/4+k));
875  lhs_pf0.y =lhs(lhs_vert + 1, (threadIdx.y/4+k));
876  lhs_pf1.x =lhs(lhs_vert + 0, (threadIdx.y/4+k+8));
877  lhs_pf1.y =lhs(lhs_vert + 1, (threadIdx.y/4+k+8));
878  lhs_pf2.x =lhs(lhs_vert + 0, (threadIdx.y/4+k+16));
879  lhs_pf2.y =lhs(lhs_vert + 1, (threadIdx.y/4+k+16));
880  lhs_pf3.x =lhs(lhs_vert + 0, (threadIdx.y/4+k+24));
881  lhs_pf3.y =lhs(lhs_vert + 1, (threadIdx.y/4+k+24));
882  } else if ((threadIdx.y/4+k+16) < k_size) {
883  lhs_pf0.x =lhs(lhs_vert + 0, (threadIdx.y/4+k));
884  lhs_pf0.y =lhs(lhs_vert + 1, (threadIdx.y/4+k));
885  lhs_pf1.x =lhs(lhs_vert + 0, (threadIdx.y/4+k+8));
886  lhs_pf1.y =lhs(lhs_vert + 1, (threadIdx.y/4+k+8));
887  lhs_pf2.x =lhs(lhs_vert + 0, (threadIdx.y/4+k+16));
888  lhs_pf2.y =lhs(lhs_vert + 1, (threadIdx.y/4+k+16));
889  } else if ((threadIdx.y/4+k+8) < k_size) {
890  lhs_pf0.x =lhs(lhs_vert + 0, (threadIdx.y/4+k));
891  lhs_pf0.y =lhs(lhs_vert + 1, (threadIdx.y/4+k));
892  lhs_pf1.x =lhs(lhs_vert + 0, (threadIdx.y/4+k+8));
893  lhs_pf1.y =lhs(lhs_vert + 1, (threadIdx.y/4+k+8));
894  } else if ((threadIdx.y/4+k) < k_size) {
895  lhs_pf0.x =lhs(lhs_vert + 0, (threadIdx.y/4+k));
896  lhs_pf0.y =lhs(lhs_vert + 1, (threadIdx.y/4+k));
897  }
898  } else if (lhs_vert < m_size) {
899  if ((threadIdx.y/4+k+24) < k_size) {
900  lhs_pf0.x =lhs(lhs_vert + 0, (threadIdx.y/4+k));
901  lhs_pf1.x =lhs(lhs_vert + 0, (threadIdx.y/4+k+8));
902  lhs_pf2.x =lhs(lhs_vert + 0, (threadIdx.y/4+k+16));
903  lhs_pf3.x =lhs(lhs_vert + 0, (threadIdx.y/4+k+24));
904  } else if ((threadIdx.y/4+k+16) < k_size) {
905  lhs_pf0.x =lhs(lhs_vert + 0, (threadIdx.y/4+k));
906  lhs_pf1.x =lhs(lhs_vert + 0, (threadIdx.y/4+k+8));
907  lhs_pf2.x =lhs(lhs_vert + 0, (threadIdx.y/4+k+16));
908  } else if ((threadIdx.y/4+k+8) < k_size) {
909  lhs_pf0.x =lhs(lhs_vert + 0, (threadIdx.y/4+k));
910  lhs_pf1.x =lhs(lhs_vert + 0, (threadIdx.y/4+k+8));
911  } else if ((threadIdx.y/4+k) < k_size) {
912  lhs_pf0.x =lhs(lhs_vert + 0, (threadIdx.y/4+k));
913  }
914  }
915  }
916  __syncthreads();
917  Index rhs_vert = k+threadIdx.x*4;
918  Index rhs_horiz0 = threadIdx.y*2+base_n;
919  Index rhs_horiz1 = threadIdx.y*2+1+base_n;
920  if (!CHECK_RHS_BOUNDARY) {
921  if ((rhs_vert + 3) < k_size) {
922  // just CHECK_RHS_BOUNDARY
923  rhs_pf0 = rhs.template loadPacket<float4,Unaligned>(rhs_vert, rhs_horiz0);
924  rhs_pf1 = rhs.template loadPacket<float4,Unaligned>(rhs_vert, rhs_horiz1);
925  } else if (rhs_vert + 2 < k_size) {
926  // just CHECK_RHS_BOUNDARY
927  rhs_pf0.x = rhs(rhs_vert, rhs_horiz0);
928  rhs_pf0.y = rhs(rhs_vert + 1, rhs_horiz0);
929  rhs_pf0.z = rhs(rhs_vert + 2, rhs_horiz0);
930  rhs_pf1.x = rhs(rhs_vert, rhs_horiz1);
931  rhs_pf1.y = rhs(rhs_vert + 1, rhs_horiz1);
932  rhs_pf1.z = rhs(rhs_vert + 2, rhs_horiz1);
933  } else if (rhs_vert + 1 < k_size) {
934  rhs_pf0.x = rhs(rhs_vert, rhs_horiz0);
935  rhs_pf0.y = rhs(rhs_vert + 1, rhs_horiz0);
936  rhs_pf1.x = rhs(rhs_vert, rhs_horiz1);
937  rhs_pf1.y = rhs(rhs_vert + 1, rhs_horiz1);
938  } else if (rhs_vert < k_size) {
939  rhs_pf0.x = rhs(rhs_vert, rhs_horiz0);
940  rhs_pf1.x = rhs(rhs_vert, rhs_horiz1);
941  }
942  } else {
943  if (rhs_horiz1 < n_size) {
944  if ((rhs_vert + 3) < k_size) {
945  // just CHECK_RHS_BOUNDARY
946  rhs_pf0 = rhs.template loadPacket<float4,Unaligned>(rhs_vert, rhs_horiz0);
947  rhs_pf1 = rhs.template loadPacket<float4,Unaligned>(rhs_vert, rhs_horiz1);
948  } else if (rhs_vert + 2 < k_size) {
949  // just CHECK_RHS_BOUNDARY
950  rhs_pf0.x = rhs(rhs_vert, rhs_horiz0);
951  rhs_pf0.y = rhs(rhs_vert + 1, rhs_horiz0);
952  rhs_pf0.z = rhs(rhs_vert + 2, rhs_horiz0);
953  rhs_pf1.x = rhs(rhs_vert, rhs_horiz1);
954  rhs_pf1.y = rhs(rhs_vert + 1, rhs_horiz1);
955  rhs_pf1.z = rhs(rhs_vert + 2, rhs_horiz1);
956  } else if (k+threadIdx.x*4 + 1 < k_size) {
957  rhs_pf0.x = rhs(rhs_vert, rhs_horiz0);
958  rhs_pf0.y = rhs(rhs_vert + 1, rhs_horiz0);
959  rhs_pf1.x = rhs(rhs_vert, rhs_horiz1);
960  rhs_pf1.y = rhs(rhs_vert + 1, rhs_horiz1);
961  } else if (k+threadIdx.x*4 < k_size) {
962  rhs_pf0.x = rhs(rhs_vert, rhs_horiz0);
963  rhs_pf1.x = rhs(rhs_vert, rhs_horiz1);
964  }
965  } else if (rhs_horiz0 < n_size) {
966  if ((rhs_vert + 3) < k_size) {
967  // just CHECK_RHS_BOUNDARY
968  rhs_pf0 = rhs.template loadPacket<float4,Unaligned>(rhs_vert, rhs_horiz0);
969  } else if ((rhs_vert + 2) < k_size) {
970  // just CHECK_RHS_BOUNDARY
971  rhs_pf0.x = rhs(rhs_vert, rhs_horiz0);
972  rhs_pf0.y = rhs(rhs_vert + 1, rhs_horiz0);
973  rhs_pf0.z = rhs(rhs_vert + 2, rhs_horiz0);
974  } else if ((rhs_vert + 1) < k_size) {
975  rhs_pf0.x = rhs(rhs_vert, rhs_horiz0);
976  rhs_pf0.y = rhs(rhs_vert + 1, rhs_horiz0);
977  } else if (rhs_vert < k_size) {
978  rhs_pf0.x = rhs(rhs_vert, rhs_horiz0);
979  }
980  }
981  }
982  __syncthreads();
983  // Loaded. Do computation
984  // Row 0 -> times (0, 4, 8, .. 28) for features 0, 1.
985  // Row 1 -> times (0, 4, 8, .. 28) for features 2, 3.
986  // ..
987  // Row 31 -> times (0, 4, 8, .. 28) for features 62, 63
988  rhs_shmem2[threadIdx.y][threadIdx.x] = make_float2(rhs_pf0.x, rhs_pf1.x);
989  // Row 32 -> times (1, 5, 9, .. 29) for features 0, 1.
990  // Row 33 -> times (1, 5, 9, .. 29) for features 2, 3.
991  // ..
992  rhs_shmem2[threadIdx.y+32][threadIdx.x] = make_float2(rhs_pf0.y, rhs_pf1.y);
993  // Row 64 -> times (2, 6, 10, .. 30) for features 0, 1.
994  // Row 65 -> times (2, 6, 10, .. 30) for features 2, 3.
995  rhs_shmem2[threadIdx.y+64][threadIdx.x] = make_float2(rhs_pf0.z, rhs_pf1.z);
996  // Row 96 -> times (3, 7, 11, .. 31) for features 0, 1.
997  // Row 97 -> times (3, 7, 11, .. 31) for features 2, 3.
998  rhs_shmem2[threadIdx.y+96][threadIdx.x] = make_float2(rhs_pf0.w, rhs_pf1.w);
999 
1000  // LHS.
1001  // Row 0 (time 0) -> features (0, 1), (4, 5), .. (28, 29), (32, 33), .. (60, 61) .. (124, 125)
1002  // Row 1 (time 1) -> features (0, 1), (4, 5), .. (28, 29), (32, 33), .. (60, 61) .. (124, 125)
1003  // ...
1004  // Row 8 (time 0) -> features (2, 3), (6, 7), .. (30, 31), (34, 35), .. (62, 63) .. (126, 127)
1005  // Row 15 (time 7) -> features (2, 3), (6, 7), .. (30, 31), (34, 35), .. (62, 63) .. (126, 127)
1006 
1007 
1008 #define add_vals(a_feat1, a_feat2, f1, f2, f3, f4)\
1009  results[0].x += a_feat1.x * f1.x;\
1010  results[1].x += a_feat1.x * f1.y;\
1011  results[2].x += a_feat1.x * f2.x;\
1012  results[3].x += a_feat1.x * f2.y;\
1013  results[4].x += a_feat1.x * f3.x;\
1014  results[5].x += a_feat1.x * f3.y;\
1015  results[6].x += a_feat1.x * f4.x;\
1016  results[7].x += a_feat1.x * f4.y;\
1017 \
1018  results[0].y += a_feat1.y * f1.x;\
1019  results[1].y += a_feat1.y * f1.y;\
1020  results[2].y += a_feat1.y * f2.x;\
1021  results[3].y += a_feat1.y * f2.y;\
1022  results[4].y += a_feat1.y * f3.x;\
1023  results[5].y += a_feat1.y * f3.y;\
1024  results[6].y += a_feat1.y * f4.x;\
1025  results[7].y += a_feat1.y * f4.y;\
1026 \
1027  results[0].z += a_feat2.x * f1.x;\
1028  results[1].z += a_feat2.x * f1.y;\
1029  results[2].z += a_feat2.x * f2.x;\
1030  results[3].z += a_feat2.x * f2.y;\
1031  results[4].z += a_feat2.x * f3.x;\
1032  results[5].z += a_feat2.x * f3.y;\
1033  results[6].z += a_feat2.x * f4.x;\
1034  results[7].z += a_feat2.x * f4.y;\
1035 \
1036  results[0].w += a_feat2.y * f1.x;\
1037  results[1].w += a_feat2.y * f1.y;\
1038  results[2].w += a_feat2.y * f2.x;\
1039  results[3].w += a_feat2.y * f2.y;\
1040  results[4].w += a_feat2.y * f3.x;\
1041  results[5].w += a_feat2.y * f3.y;\
1042  results[6].w += a_feat2.y * f4.x;\
1043  results[7].w += a_feat2.y * f4.y;\
1044 
1045  lhs_shmem2[threadIdx.y/4][threadIdx.x+(threadIdx.y%4)*8] = make_float2(lhs_pf0.x, lhs_pf0.y);
1046  lhs_shmem2[threadIdx.y/4+8][threadIdx.x+(threadIdx.y%4)*8] = make_float2(lhs_pf1.x, lhs_pf1.y);
1047  lhs_shmem2[threadIdx.y/4+16][threadIdx.x+(threadIdx.y%4)*8] = make_float2(lhs_pf2.x, lhs_pf2.y);
1048  lhs_shmem2[threadIdx.y/4+24][threadIdx.x+(threadIdx.y%4)*8] = make_float2(lhs_pf3.x, lhs_pf3.y);
1049 
1050  lhs_shmem2[threadIdx.y/4 + 32][threadIdx.x+(threadIdx.y%4)*8] = make_float2(lhs_pf0.z, lhs_pf0.w);
1051  lhs_shmem2[threadIdx.y/4 + 40][threadIdx.x+(threadIdx.y%4)*8] = make_float2(lhs_pf1.z, lhs_pf1.w);
1052  lhs_shmem2[threadIdx.y/4 + 48][threadIdx.x+(threadIdx.y%4)*8] = make_float2(lhs_pf2.z, lhs_pf2.w);
1053  lhs_shmem2[threadIdx.y/4 + 56][threadIdx.x+(threadIdx.y%4)*8] = make_float2(lhs_pf3.z, lhs_pf3.w);
1054 
1055  __syncthreads();
1056 
1057  // Do the multiplies.
1058  #pragma unroll
1059  for (int koff = 0; koff < 32; koff ++) {
1060  float2 a3 = lhs_shmem2[koff][threadIdx.x + (threadIdx.y % 4) * 8];
1061  float2 a4 = lhs_shmem2[koff + 32][threadIdx.x + (threadIdx.y % 4) * 8];
1062 
1063  // first feature is at (threadIdx.y/4) * 8 last is at start + 8.
1064  int start_feature = (threadIdx.y / 4) * 8;
1065 
1066  float2 br1 = rhs_shmem2[start_feature/2 + (koff % 4) * 32][koff/4];
1067  float2 br2 = rhs_shmem2[start_feature/2 + 1 + (koff % 4) * 32][koff/4];
1068  float2 br3 = rhs_shmem2[start_feature/2 + 2 + (koff % 4) * 32][koff/4];
1069  float2 br4 = rhs_shmem2[start_feature/2 + 3 + (koff % 4) * 32][koff/4];
1070 
1071  add_vals(a3, a4, br1, br2, br3, br4)
1072  }
1073  __syncthreads();
1074  } // end loop over k
1075 
1076  __syncthreads();
1077  Index horiz_base = (threadIdx.y/4)*8+base_n;
1078  if (!CHECK_LHS_BOUNDARY && !CHECK_RHS_BOUNDARY) {
1079  for (int i = 0; i < 8; i++) {
1080  output(lhs_vert, horiz_base + i) = results[i].x;
1081  output(lhs_vert + 1, horiz_base + i) = results[i].y;
1082  output(lhs_vert + 2, horiz_base + i) = results[i].z;
1083  output(lhs_vert + 3, horiz_base + i) = results[i].w;
1084  }
1085  } else if (!CHECK_RHS_BOUNDARY) {
1086  if (lhs_vert + 3 < m_size) {
1087  for (int i = 0; i < 8; i++) {
1088  output(lhs_vert, horiz_base + i) = results[i].x;
1089  output(lhs_vert + 1, horiz_base + i) = results[i].y;
1090  output(lhs_vert + 2, horiz_base + i) = results[i].z;
1091  output(lhs_vert + 3, horiz_base + i) = results[i].w;
1092  }
1093  } else if (lhs_vert + 2 < m_size) {
1094  for (int i = 0; i < 8; i++) {
1095  output(lhs_vert, horiz_base + i) = results[i].x;
1096  output(lhs_vert + 1, horiz_base + i) = results[i].y;
1097  output(lhs_vert + 2, horiz_base + i) = results[i].z;
1098  }
1099  } else if (lhs_vert + 1 < m_size) {
1100  for (int i = 0; i < 8; i++) {
1101  output(lhs_vert, horiz_base + i) = results[i].x;
1102  output(lhs_vert + 1, horiz_base + i) = results[i].y;
1103  }
1104  } else if (lhs_vert < m_size) {
1105  for (int i = 0; i < 8; i++) {
1106  output(lhs_vert, horiz_base + i) = results[i].x;
1107  }
1108  }
1109  } else if (!CHECK_LHS_BOUNDARY) {
1110  // CHECK BOUNDARY_B
1111  for (int i = 0; i < 8; i++) {
1112  if (horiz_base + i < n_size) {
1113  output(lhs_vert, horiz_base + i) = results[i].x;
1114  output(lhs_vert + 1, horiz_base + i) = results[i].y;
1115  output(lhs_vert + 2, horiz_base + i) = results[i].z;
1116  output(lhs_vert + 3, horiz_base + i) = results[i].w;
1117  }
1118  }
1119  } else {
1120  // CHECK both boundaries.
1121  for (int i = 0; i < 8; i++) {
1122  if (horiz_base + i < n_size) {
1123  if (lhs_vert < m_size)
1124  output(lhs_vert, horiz_base + i) = results[i].x;
1125  if (lhs_vert + 1 < m_size)
1126  output(lhs_vert + 1, horiz_base + i) = results[i].y;
1127  if (lhs_vert + 2 < m_size)
1128  output(lhs_vert + 2, horiz_base + i) = results[i].z;
1129  if (lhs_vert + 3 < m_size)
1130  output(lhs_vert + 3, horiz_base + i) = results[i].w;
1131  }
1132  }
1133  }
1134 }
1135 
1136 
1137 template<typename Index, typename LhsMapper,
1138  typename RhsMapper, typename OutputMapper>
1139 __global__ void
1140 #if defined(EIGEN_HIPCC)
1141 __launch_bounds__(256, 1)
1142 #else
1143 __launch_bounds__(256)
1144 #endif
1145 EigenFloatContractionKernel(const LhsMapper lhs, const RhsMapper rhs,
1146  const OutputMapper output,
1147  const Index m_size, const Index n_size, const Index k_size) {
1148  __shared__ float2 lhs_shmem[64*32];
1149  __shared__ float2 rhs_shmem[128*8];
1150 
1151  typedef float2 LHS_MEM[64][32];
1152  typedef float2 RHS_MEM[128][8];
1153 
1154  const Index m_block_idx = blockIdx.x;
1155  const Index n_block_idx = blockIdx.y;
1156 
1157  const Index base_m = 128 * m_block_idx;
1158  const Index base_n = 64 * n_block_idx;
1159 
1160  bool check_rhs = (base_n + 63) >= n_size;
1161  bool check_lhs128 = (base_m + 127) >= m_size;
1162 
1163  if (!check_rhs) {
1164  if (!check_lhs128) {
1165  // >= 128 rows left
1166  EigenFloatContractionKernelInternal<Index, LhsMapper, RhsMapper, OutputMapper, false, false>(
1167  lhs, rhs, output, *((LHS_MEM *) lhs_shmem), *((RHS_MEM *) rhs_shmem), m_size, n_size, k_size, base_m, base_n);
1168  } else {
1169  EigenFloatContractionKernelInternal<Index, LhsMapper, RhsMapper, OutputMapper, true, false>(
1170  lhs, rhs, output, *((LHS_MEM *) lhs_shmem), *((RHS_MEM *) rhs_shmem), m_size, n_size, k_size, base_m, base_n);
1171  }
1172  } else {
1173  if (!check_lhs128) {
1174  // >= 128 rows left
1175  EigenFloatContractionKernelInternal<Index, LhsMapper, RhsMapper, OutputMapper, false, true>(
1176  lhs, rhs, output, *((LHS_MEM *) lhs_shmem), *((RHS_MEM *) rhs_shmem), m_size, n_size, k_size, base_m, base_n);
1177  } else {
1178  EigenFloatContractionKernelInternal<Index, LhsMapper, RhsMapper, OutputMapper, true, true>(
1179  lhs, rhs, output, *((LHS_MEM *) lhs_shmem), *((RHS_MEM *) rhs_shmem), m_size, n_size, k_size, base_m, base_n);
1180  }
1181  }
1182 }
1183 
1184 template<typename Index, typename LhsMapper,
1185  typename RhsMapper, typename OutputMapper>
1186 __global__ void
1187 #if defined(EIGEN_HIPCC)
1188 __launch_bounds__(256, 1)
1189 #else
1190 __launch_bounds__(256)
1191 #endif
1192 EigenFloatContractionKernel16x16(const LhsMapper lhs, const RhsMapper rhs,
1193  const OutputMapper output,
1194  const Index m_size, const Index n_size, const Index k_size) {
1195  __shared__ float2 lhs_shmem[32][16];
1196  __shared__ float2 rhs_shmem[64][8];
1197 
1198  const Index m_block_idx = blockIdx.x;
1199  const Index n_block_idx = blockIdx.y;
1200 
1201  const Index base_m = 64 * m_block_idx;
1202  const Index base_n = 64 * n_block_idx;
1203 
1204  if (base_m + 63 < m_size) {
1205  if (base_n + 63 < n_size) {
1206  EigenFloatContractionKernelInternal16x16<Index, LhsMapper, RhsMapper, OutputMapper, false, false>(lhs, rhs, output, lhs_shmem, rhs_shmem, m_size, n_size, k_size, base_m, base_n);
1207  } else {
1208  EigenFloatContractionKernelInternal16x16<Index, LhsMapper, RhsMapper, OutputMapper, false, true>(lhs, rhs, output, lhs_shmem, rhs_shmem, m_size, n_size, k_size, base_m, base_n);
1209  }
1210  } else {
1211  if (base_n + 63 < n_size) {
1212  EigenFloatContractionKernelInternal16x16<Index, LhsMapper, RhsMapper, OutputMapper, true, false>(lhs, rhs, output, lhs_shmem, rhs_shmem, m_size, n_size, k_size, base_m, base_n);
1213  } else {
1214  EigenFloatContractionKernelInternal16x16<Index, LhsMapper, RhsMapper, OutputMapper, true, true>(lhs, rhs, output, lhs_shmem, rhs_shmem, m_size, n_size, k_size, base_m, base_n);
1215  }
1216  }
1217 }
1218 
1219 
1220 template<typename Indices, typename LeftArgType, typename RightArgType, typename OutputKernelType>
1221 struct TensorEvaluator<const TensorContractionOp<Indices, LeftArgType, RightArgType, OutputKernelType>, GpuDevice> :
1222  public TensorContractionEvaluatorBase<TensorEvaluator<const TensorContractionOp<Indices, LeftArgType, RightArgType, OutputKernelType>, GpuDevice> > {
1223 
1224  typedef GpuDevice Device;
1225 
1226  typedef TensorEvaluator<const TensorContractionOp<Indices, LeftArgType, RightArgType, OutputKernelType>, Device> Self;
1227  typedef TensorContractionEvaluatorBase<Self> Base;
1228 
1229  typedef TensorContractionOp<Indices, LeftArgType, RightArgType, OutputKernelType> XprType;
1230  typedef std::remove_const_t<typename XprType::Scalar> Scalar;
1231  typedef typename XprType::Index Index;
1232  typedef typename XprType::CoeffReturnType CoeffReturnType;
1234 
1236 
1237  // Most of the code is assuming that both input tensors are ColMajor. If the
1238  // inputs are RowMajor, we will "cheat" by swapping the LHS and RHS:
1239  // If we want to compute A * B = C, where A is LHS and B is RHS, the code
1240  // will pretend B is LHS and A is RHS.
1241  typedef std::conditional_t<Layout == static_cast<int>(ColMajor), LeftArgType, RightArgType> EvalLeftArgType;
1242  typedef std::conditional_t<Layout == static_cast<int>(ColMajor), RightArgType, LeftArgType> EvalRightArgType;
1243 
1244  static constexpr int LDims =
1245  internal::array_size<typename TensorEvaluator<EvalLeftArgType, Device>::Dimensions>::value;
1246  static constexpr int RDims =
1247  internal::array_size<typename TensorEvaluator<EvalRightArgType, Device>::Dimensions>::value;
1248  static constexpr int ContractDims = internal::array_size<Indices>::value;
1249 
1250  typedef array<Index, LDims> left_dim_mapper_t;
1251  typedef array<Index, RDims> right_dim_mapper_t;
1252 
1253  typedef array<Index, ContractDims> contract_t;
1254  typedef array<Index, LDims - ContractDims> left_nocontract_t;
1255  typedef array<Index, RDims - ContractDims> right_nocontract_t;
1256 
1257  static constexpr int NumDims = LDims + RDims - 2 * ContractDims;
1258 
1259  typedef DSizes<Index, NumDims> Dimensions;
1260 
1261  // typedefs needed in evalTo
1262  typedef std::remove_const_t<typename EvalLeftArgType::Scalar> LhsScalar;
1263  typedef std::remove_const_t<typename EvalRightArgType::Scalar> RhsScalar;
1264 
1265  typedef TensorEvaluator<EvalLeftArgType, Device> LeftEvaluator;
1266  typedef TensorEvaluator<EvalRightArgType, Device> RightEvaluator;
1267 
1268  typedef typename LeftEvaluator::Dimensions LeftDimensions;
1269  typedef typename RightEvaluator::Dimensions RightDimensions;
1270 
1271  TensorEvaluator(const XprType& op, const Device& device) :
1272  Base(op, device)
1273  {
1274  EIGEN_STATIC_ASSERT( (internal::is_same<OutputKernelType, const NoOpOutputKernel>::value),
1275  GPU_TENSOR_CONTRACTION_DOES_NOT_SUPPORT_OUTPUT_KERNELS);
1276  }
1277 
1278  // We need to redefine this method to make nvcc happy
1279  EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(Scalar* data) {
1280  this->m_leftImpl.evalSubExprsIfNeeded(NULL);
1281  this->m_rightImpl.evalSubExprsIfNeeded(NULL);
1282  if (data) {
1283  evalTo(data);
1284  return false;
1285  } else {
1286  this->m_result = static_cast<Scalar *>(this->m_device.allocate(this->dimensions().TotalSize() * sizeof(Scalar)));
1287  evalTo(this->m_result);
1288  return true;
1289  }
1290  }
1291 
1292  void evalTo(Scalar* buffer) const {
1293  if (this->m_lhs_inner_dim_contiguous) {
1294  if (this->m_rhs_inner_dim_contiguous) {
1295  if (this->m_rhs_inner_dim_reordered) {
1296  evalTyped<true, true, true, Unaligned>(buffer);
1297  }
1298  else {
1299  evalTyped<true, true, false, Unaligned>(buffer);
1300  }
1301  }
1302  else {
1303  if (this->m_rhs_inner_dim_reordered) {
1304  evalTyped<true, false, true, Unaligned>(buffer);
1305  }
1306  else {
1307  evalTyped<true, false, false, Unaligned>(buffer);
1308  }
1309  }
1310  }
1311  else {
1312  if (this->m_rhs_inner_dim_contiguous) {
1313  if (this->m_rhs_inner_dim_reordered) {
1314  evalTyped<false, true, true, Unaligned>(buffer);
1315  }
1316  else {
1317  evalTyped<false, true, false, Unaligned>(buffer);
1318  }
1319  }
1320  else {
1321  if (this->m_rhs_inner_dim_reordered) {
1322  evalTyped<false, false, true, Unaligned>(buffer);
1323  }
1324  else {
1325  evalTyped<false, false, false, Unaligned>(buffer);
1326  }
1327  }
1328  }
1329  }
1330 
1331  template <typename LhsScalar, typename RhsScalar, typename Index, typename LhsMapper, typename RhsMapper, typename OutputMapper> struct LaunchKernels {
1332  static void Run(const LhsMapper& lhs, const RhsMapper& rhs, const OutputMapper& output, Index m, Index n, Index k, const GpuDevice& device) {
1333  const Index m_blocks = (m + 63) / 64;
1334  const Index n_blocks = (n + 63) / 64;
1335  const dim3 num_blocks(m_blocks, n_blocks, 1);
1336  const dim3 block_size(8, 8, 8);
1337  LAUNCH_GPU_KERNEL((EigenContractionKernel<Scalar, Index, LhsMapper, RhsMapper, OutputMapper>), num_blocks, block_size, 0, device, lhs, rhs, output, m, n, k);
1338  }
1339  };
1340 
1341  template <typename Index, typename LhsMapper, typename RhsMapper, typename OutputMapper> struct LaunchKernels<float, float, Index, LhsMapper, RhsMapper, OutputMapper> {
1342  static void Run(const LhsMapper& lhs, const RhsMapper& rhs, const OutputMapper& output, Index m, Index n, Index k, const GpuDevice& device) {
1343  if (m < 768 || n < 768) {
1344  const Index m_blocks = (m + 63) / 64;
1345  const Index n_blocks = (n + 63) / 64;
1346  const dim3 num_blocks(m_blocks, n_blocks, 1);
1347  const dim3 block_size(16, 16, 1);
1348  LAUNCH_GPU_KERNEL((EigenFloatContractionKernel16x16<Index, LhsMapper, RhsMapper, OutputMapper>), num_blocks, block_size, 0, device, lhs, rhs, output, m, n, k);
1349  } else {
1350  const Index m_blocks = (m + 127) / 128;
1351  const Index n_blocks = (n + 63) / 64;
1352  const dim3 num_blocks(m_blocks, n_blocks, 1);
1353  const dim3 block_size(8, 32, 1);
1354  LAUNCH_GPU_KERNEL((EigenFloatContractionKernel<Index, LhsMapper, RhsMapper, OutputMapper>), num_blocks, block_size, 0, device, lhs, rhs, output, m, n, k);
1355  }
1356  }
1357  };
1358 
1359  template <bool lhs_inner_dim_contiguous, bool rhs_inner_dim_contiguous, bool rhs_inner_dim_reordered, int Alignment>
1360  void evalTyped(Scalar* buffer) const {
1361  // columns in left side, rows in right side
1362  const Index k = this->m_k_size;
1364 
1365  // rows in left side
1366  const Index m = this->m_i_size;
1367 
1368  // columns in right side
1369  const Index n = this->m_j_size;
1370 
1371  // zero out the result buffer (which must be of size at least m * n * sizeof(Scalar))
1372  this->m_device.fill(buffer, buffer + m * n, Scalar(0));
1373 
1374  typedef internal::TensorContractionInputMapper<LhsScalar, Index, internal::Lhs,
1375  LeftEvaluator, left_nocontract_t,
1376  contract_t, 4,
1377  lhs_inner_dim_contiguous,
1378  false, Unaligned> LhsMapper;
1379 
1380  typedef internal::TensorContractionInputMapper<RhsScalar, Index, internal::Rhs,
1381  RightEvaluator, right_nocontract_t,
1382  contract_t, 4,
1383  rhs_inner_dim_contiguous,
1384  rhs_inner_dim_reordered, Unaligned> RhsMapper;
1385 
1386  typedef internal::blas_data_mapper<Scalar, Index, ColMajor> OutputMapper;
1387 
1388 
1389  // initialize data mappers
1390  LhsMapper lhs(this->m_leftImpl, this->m_left_nocontract_strides, this->m_i_strides,
1391  this->m_left_contracting_strides, this->m_k_strides);
1392 
1393  RhsMapper rhs(this->m_rightImpl, this->m_right_nocontract_strides, this->m_j_strides,
1394  this->m_right_contracting_strides, this->m_k_strides);
1395 
1396  OutputMapper output(buffer, m);
1397 
1398 #if defined(EIGEN_USE_HIP)
1399  setGpuSharedMemConfig(hipSharedMemBankSizeEightByte);
1400 #else
1401  setGpuSharedMemConfig(cudaSharedMemBankSizeEightByte);
1402 #endif
1403 
1404  LaunchKernels<LhsScalar, RhsScalar, Index, LhsMapper, RhsMapper, OutputMapper>::Run(lhs, rhs, output, m, n, k, this->m_device);
1405  }
1406 };
1407 
1408 } // end namespace Eigen
1409 
1410 #endif // EIGEN_USE_GPU and EIGEN_GPUCC
1411 #endif // EIGEN_CXX11_TENSOR_TENSOR_CONTRACTION_GPU_H
Matrix3f m
int n
int i
if((m *x).isApprox(y))
#define EIGEN_UNUSED_VARIABLE(var)
#define EIGEN_STATIC_ASSERT(X, MSG)
EIGEN_ALWAYS_INLINE T mini(const T &x, const T &y)
: TensorContractionSycl.h, provides various tensor contraction kernel for SYCL backend
std::array< T, N > array
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
internal::packet_traits< Scalar >::type type
Definition: TensorMeta.h:55
const Dimensions & dimensions() const
static constexpr int Layout
Derived::Scalar Scalar
const Device EIGEN_DEVICE_REF m_device
TensorEvaluator(const Derived &m, const Device &device)
EvaluatorPointerType data() const
Derived::Scalar CoeffReturnType
bool evalSubExprsIfNeeded(EvaluatorPointerType dest)
PacketType< CoeffReturnType, Device >::type PacketReturnType
Derived::Dimensions Dimensions
std::ptrdiff_t j