use local memory

This commit is contained in:
Elena Gvozdeva 2014-08-27 14:58:01 +04:00 committed by ElenaGvozdeva
parent d78bc3c321
commit 2d89df1804
3 changed files with 33 additions and 13 deletions

View File

@ -782,6 +782,7 @@ static bool ocl_gemm( InputArray matA, InputArray matB, double alpha,
{ {
int depth = matA.depth(), cn = matA.channels(); int depth = matA.depth(), cn = matA.channels();
int type = CV_MAKETYPE(depth, cn); int type = CV_MAKETYPE(depth, cn);
const int block_size = 16;
CV_Assert( type == matB.type() && (type == CV_32FC1 || type == CV_64FC1 || type == CV_32FC2 || type == CV_64FC2) ); CV_Assert( type == matB.type() && (type == CV_32FC1 || type == CV_64FC1 || type == CV_32FC2 || type == CV_64FC2) );
@ -807,8 +808,8 @@ static bool ocl_gemm( InputArray matA, InputArray matB, double alpha,
CV_Assert( matB.type() == type && (!haveC || matC.type() == type) ); CV_Assert( matB.type() == type && (!haveC || matC.type() == type) );
CV_Assert( sizeA.width == sizeB.height && (!haveC || sizeC == sizeD) ); CV_Assert( sizeA.width == sizeB.height && (!haveC || sizeC == sizeD) );
String opts = format("-D T=%s -D T1=%s -D cn=%d %s %s", String opts = format("-D T=%s -D T1=%s -D cn=%d -D LOCAL_SIZE=%d %s %s",
ocl::typeToStr(type), ocl::typeToStr(depth), cn, ocl::typeToStr(type), ocl::typeToStr(depth), cn, block_size,
haveC ? "-D HAVE_C" : "", haveC ? "-D HAVE_C" : "",
doubleSupport ? " -D DOUBLE_SUPPORT" : ""); doubleSupport ? " -D DOUBLE_SUPPORT" : "");
@ -843,7 +844,8 @@ static bool ocl_gemm( InputArray matA, InputArray matB, double alpha,
sizeA.width, (float)alpha, (float)beta); sizeA.width, (float)alpha, (float)beta);
size_t globalsize[2] = { sizeD.width, sizeD.height}; size_t globalsize[2] = { sizeD.width, sizeD.height};
return k.run(2, globalsize, NULL, false); size_t localsize[2] = { block_size, block_size};
return k.run(2, globalsize, localsize, false);
} }
#endif #endif

View File

@ -28,7 +28,7 @@
sum.y += fma(a.x, b.y, a.y * b.x);\ sum.y += fma(a.x, b.y, a.y * b.x);\
} }
#else #else
#define MUL(i, a, b) sum += a * b #define MUL(i, a, b) sum = fma(a, b, sum);
#endif #endif
@ -40,16 +40,34 @@ __kernel void gemm(__global const uchar * A_ptr, int A_step, int A_offset,
int x = get_global_id(0); int x = get_global_id(0);
int y = get_global_id(1); int y = get_global_id(1);
if (x < D_cols && y < D_rows) int lidx = get_local_id(0);
{ int lidy = get_local_id(1);
__global const T* A = (__global const T*)(A_ptr + IND_A); __global const T* A = (__global const T*)(A_ptr + IND_A);
__global const T* B = (__global const T*)(B_ptr + IND_B); __global const T* B = (__global const T*)(B_ptr + IND_B);
T sum = (T)(0); T sum = (T)(0);
__local T a_local[LOCAL_SIZE*LOCAL_SIZE];
__local T b_local[LOCAL_SIZE*LOCAL_SIZE];
for (int i = 0; i < n; ++i) for (int p = 0; p < (n+LOCAL_SIZE-1)/LOCAL_SIZE; ++p)
MUL(i, A[i*STEP_A], B[i*STEP_B]); {
a_local[mad24(lidy, LOCAL_SIZE, lidx)] = A[mad24(p, LOCAL_SIZE, lidx)];
b_local[mad24(lidy, LOCAL_SIZE, lidx)] = B[mad24(p, LOCAL_SIZE, lidy)*STEP_B];
barrier(CLK_LOCAL_MEM_FENCE);
if (x < D_cols && y < D_rows)
{
for (int i = 0; i < LOCAL_SIZE && p * LOCAL_SIZE + i < n; ++i)
MUL(i, a_local[mad24(lidy, LOCAL_SIZE, i)], b_local[mad24(i, LOCAL_SIZE, lidx)]);
}
barrier(CLK_LOCAL_MEM_FENCE);
}
if (x < D_cols && y < D_rows)
{
__global T* D = (__global T*)(D_ptr + mad24(y, D_step, mad24(x, TSIZE, D_offset))); __global T* D = (__global T*)(D_ptr + mad24(y, D_step, mad24(x, TSIZE, D_offset)));
#if HAVE_C #if HAVE_C
D[0] = mad(alpha, sum, D[0]*beta); D[0] = mad(alpha, sum, D[0]*beta);

View File

@ -48,7 +48,7 @@ namespace ocl {
using namespace cv; using namespace cv;
int test_loop_times = 1; // TODO Read from command line / environment int test_loop_times = 10; // TODO Read from command line / environment
#ifdef HAVE_OPENCL #ifdef HAVE_OPENCL