1 // This file is part of OpenCV project.
2 // It is subject to the license terms in the LICENSE file found in the top-level directory
3 // of this distribution and at http://opencv.org/license.html.
5 // Copyright (C) 2014, Itseez, Inc., all rights reserved.
6 // Third party copyrights are property of their respective owners.
10 #pragma OPENCL EXTENSION cl_amd_fp64:enable
11 #elif defined (cl_khr_fp64)
12 #pragma OPENCL EXTENSION cl_khr_fp64:enable
16 #define TSIZE (int)sizeof(T)
17 #define WTSIZE (int)sizeof(WT)
19 #define IND_A mad24(y, A_step, A_offset)
20 #define IND_B mad24(x, WTSIZE, B_offset)
21 #define STEP_B B_step / WTSIZE
27 sum.x += fma(a.x, b.x, - a.y * b.y);\
28 sum.y += fma(a.x, b.y, a.y * b.x);\
33 sum.x += fma(a.x, b.x, - a.y * b.y);\
34 sum.y += fma(a.x, b.y, a.y * b.x);\
35 sum.z += fma(a.x, b.z, - a.y * b.w);\
36 sum.w += fma(a.x, b.w, a.y * b.z);\
40 #define MUL(i, a, b) sum = fma(a, b, sum);
44 __kernel void gemm(__global const uchar * A_ptr, int A_step, int A_offset,
45 __global const uchar * B_ptr, int B_step, int B_offset,
46 __global uchar * D_ptr, int D_step, int D_offset, int D_rows, int D_cols,
47 int n, T1 alpha, T1 beta)
49 int x = get_global_id(0);
50 int y = get_global_id(1);
52 int lidx = get_local_id(0);
53 int lidy = get_local_id(1);
55 __global const T* A = (__global const T*)(A_ptr + IND_A);
56 __global const WT* B = (__global const WT*)(B_ptr + IND_B);
62 if (x < D_cols && y < D_rows)
64 for (int i = 0; i < n; ++i)
65 MUL(i, A[i], B[i*STEP_B]);
68 __local T a_local[LOCAL_SIZE*LOCAL_SIZE];
69 __local WT b_local[LOCAL_SIZE*LOCAL_SIZE];
73 reps = (n + LOCAL_SIZE-1)/LOCAL_SIZE;
78 for (int p = 0; p < reps; ++p)
80 if (p * LOCAL_SIZE + lidx < n && y < D_rows)
81 a_local[mad24(lidy, LOCAL_SIZE, lidx)] = A[mad24(p, LOCAL_SIZE, lidx)];
82 if (p * LOCAL_SIZE + lidy < n && x < D_cols)
83 b_local[mad24(lidy, LOCAL_SIZE, lidx)] = B[mad24(p, LOCAL_SIZE, lidy)*STEP_B];
85 barrier(CLK_LOCAL_MEM_FENCE);
87 if (x < D_cols && y < D_rows)
89 for (int i = 0; i < LOCAL_SIZE
91 && p * LOCAL_SIZE + i < n
94 MUL(i, a_local[mad24(lidy, LOCAL_SIZE, i)], b_local[mad24(i, LOCAL_SIZE, lidx)]);
97 barrier(CLK_LOCAL_MEM_FENCE);
100 if (x < D_cols && y < D_rows)
103 __global WT* D = (__global WT*)(D_ptr + mad24(y, D_step, mad24(x, WTSIZE, D_offset)));
105 D[0] = mad(alpha, sum, D[0]*beta);