adding opencl convolution benchmark

This commit is contained in:
Blaise Tine
2023-11-14 22:31:30 -08:00
parent 4e7a536918
commit 61e3442ef8
16 changed files with 490 additions and 170 deletions

View File

@@ -7,43 +7,41 @@ __kernel void matmul(__global float *A,
{
int globalRow = get_global_id(1);
int globalCol = get_global_id(0);
int localRow = get_local_id(1);
int localCol = get_local_id(0);
int localRow = get_local_id(1);
int localCol = get_local_id(0);
int localSize = get_local_size(0); // assuming square local size
float sum = 0.0f;
// Load initial blocks of A and B into local memory
int k = 0;
localA[localRow * localSize + localCol] = A[globalRow * N + k + localCol];
localB[localRow * localSize + localCol] = B[(k + localRow) * N + globalCol];
// Loop over all blocks of both matrices
for (int k = 0; k < N; k += localSize) {
// Load block of matrix A to local memory
localA[localRow * localSize + localCol] = A[globalRow * N + k + localCol];
// Iterate over blocks
for (k = 0; k < N; k += 16) {
// Ensure the initial block is loaded
// Load block of matrix B to local memory, adjusting for column-major access
localB[localRow * localSize + localCol] = B[(k + localRow) * N + globalCol];
// Synchronize to make sure the tiles are loaded
barrier(CLK_LOCAL_MEM_FENCE);
// Compute multiplication for this block
for (int j = 0; j < 16; j++) {
// Multiply the two matrix blocks and accumulate result
for (int j = 0; j < localSize; j++) {
sum += localA[localRow * localSize + j] * localB[j * localSize + localCol];
}
// Load the next block of matrix A into local memory
if (k + 16 < N) {
localA[localRow * localSize + localCol] = A[globalRow * N + k + 16 + localCol];
localB[localRow * localSize + localCol] = B[(k + 16 + localRow) * N + globalCol];
}
}
C[globalRow * N + globalCol] = sum;
}
/*__kernel void matmul(__global float *A, __global float *B, __global float *C, const unsigned int N)
/*__kernel void matmul(__global float *A,
__global float *B,
__global float *C,
const unsigned int N)
{
int globalRow = get_global_id(1);
int globalCol = get_global_id(0);
int localRow = get_local_id(1);
int localCol = get_local_id(0);
int localRow = get_local_id(1);
int localCol = get_local_id(0);
// Static local memory declaration
__local float localA[16][16];
@@ -51,26 +49,21 @@ __kernel void matmul(__global float *A,
float sum = 0.0f;
// Load initial blocks of A and B into local memory
int k = 0;
localA[localRow][localCol] = A[globalRow * N + k + localCol];
localB[localRow][localCol] = B[(k + localRow) * N + globalCol];
// Iterate over blocks
for (k = 0; k < N; k += 16) {
// Ensure the initial block is loaded
for (int k = 0; k < N; k += 16) {
// Load a block of matrix A into local memory
localA[localRow][localCol] = A[globalRow * N + k + localCol];
// Load a block of matrix B into local memory
localB[localRow][localCol] = B[(k + localRow) * N + globalCol];
// Ensure the entire block is loaded
barrier(CLK_LOCAL_MEM_FENCE);
// Compute multiplication for this block
for (int j = 0; j < 16; j++) {
sum += localA[localRow][j] * localB[j][localCol];
}
// Load the next block of matrix A into local memory
if (k + 16 < N) {
localA[localRow][localCol] = A[globalRow * N + k + 16 + localCol];
localB[localRow][localCol] = B[(k + 16 + localRow) * N + globalCol];
}
}
C[globalRow * N + globalCol] = sum;