矩阵转置
// kernel __kernel void transpose(__global float4 *g_mat, __local float4 *l_mat, uint size) { __global float4 *src, *dst; /* Determine row and column location */ int col = get_global_id(0); int row = 0; while(col >= size) { col -= size--; row++; } col += row; size += row; /* Read source block into local memory */ src = g_mat + row * size * 4 + col; l_mat += get_local_id(0)*8; l_mat[0] = src[0]; l_mat[1] = src[size]; l_mat[2] = src[2*size]; l_mat[3] = src[3*size]; /* Process block on diagonal */ if(row == col) { src[0] = (float4)(l_mat[0].x, l_mat[1].x, l_mat[2].x, l_mat[3].x); src[size] = (float4)(l_mat[0].y, l_mat[1].y, l_mat[2].y, l_mat[3].y); src[2*size] = (float4)(l_mat[0].z, l_mat[1].z, l_mat[2].z, l_mat[3].z); src[3*size] = (float4)(l_mat[0].w, l_mat[1].w, l_mat[2].w, l_mat[3].w); } /* Process block off diagonal */ else { /* Read destination block into local memory */ dst = g_mat + col * size * 4 + row; l_mat[4] = dst[0]; l_mat[5] = dst[size]; l_mat[6] = dst[2*size]; l_mat[7] = dst[3*size]; /* Set elements of destination block */ dst[0] = (float4)(l_mat[0].x, l_mat[1].x, l_mat[2].x, l_mat[3].x); dst[size] = (float4)(l_mat[0].y, l_mat[1].y, l_mat[2].y, l_mat[3].y); dst[2*size] = (float4)(l_mat[0].z, l_mat[1].z, l_mat[2].z, l_mat[3].z); dst[3*size] = (float4)(l_mat[0].w, l_mat[1].w, l_mat[2].w, l_mat[3].w); /* Set elements of source block */ src[0] = (float4)(l_mat[4].x, l_mat[5].x, l_mat[6].x, l_mat[7].x); src[size] = (float4)(l_mat[4].y, l_mat[5].y, l_mat[6].y, l_mat[7].y); src[2*size] = (float4)(l_mat[4].z, l_mat[5].z, l_mat[6].z, l_mat[7].z); src[3*size] = (float4)(l_mat[4].w, l_mat[5].w, l_mat[6].w, l_mat[7].w); } }
// 主机程序
#define _CRT_SECURE_NO_WARNINGS #define PROGRAM_FILE "transpose.cl" #define KERNEL_FUNC "transpose" #define MATRIX_DIM 64 #include <stdio.h> #include <stdlib.h> #include <string.h> #ifdef MAC #include <OpenCL/cl.h> #else #include <CL/cl.h> #endif /* Find a GPU or CPU associated with the first available platform */ cl_device_id create_device() { cl_platform_id platform; cl_device_id dev; int err; /* Identify a platform */ err = clGetPlatformIDs(1, &platform, NULL); if(err < 0) { perror("Couldn't identify a platform"); exit(1); } /* Access a device */ err = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 1, &dev, NULL); if(err == CL_DEVICE_NOT_FOUND) { err = clGetDeviceIDs(platform, CL_DEVICE_TYPE_CPU, 1, &dev, NULL); } if(err < 0) { perror("Couldn't access any devices"); exit(1); } return dev; } /* Create program from a file and compile it */ cl_program build_program(cl_context ctx, cl_device_id dev, const char* filename) { cl_program program; FILE *program_handle; char *program_buffer, *program_log; size_t program_size, log_size; int err; /* Read program file and place content into buffer */ program_handle = fopen(filename, "r"); if(program_handle == NULL) { perror("Couldn't find the program file"); exit(1); } fseek(program_handle, 0, SEEK_END); program_size = ftell(program_handle); rewind(program_handle); program_buffer = (char*)malloc(program_size + 1); program_buffer[program_size] = '