#include "b3LauncherCL.h" bool gDebugLauncherCL = false; b3LauncherCL::b3LauncherCL(cl_command_queue queue, cl_kernel kernel, const char* name) :m_commandQueue(queue), m_kernel(kernel), m_idx(0), m_enableSerialization(false), m_name(name) { if (gDebugLauncherCL) { static int counter = 0; printf("[%d] Prepare to launch OpenCL kernel %s\n", counter++, name); } m_serializationSizeInBytes = sizeof(int); } b3LauncherCL::~b3LauncherCL() { for (int i=0;i int b3LauncherCL::deserializeArgs(unsigned char* buf, int bufSize, cl_context ctx) { int index=0; int numArguments = *(int*) &buf[index]; index+=sizeof(int); for (int i=0;im_isBuffer) { b3OpenCLArray* clData = new b3OpenCLArray(ctx,m_commandQueue, arg->m_argSizeInBytes); clData->resize(arg->m_argSizeInBytes); clData->copyFromHostPointer(&buf[index], arg->m_argSizeInBytes); arg->m_clBuffer = clData->getBufferCL(); m_arrays.push_back(clData); cl_int status = clSetKernelArg( m_kernel, m_idx++, sizeof(cl_mem), &arg->m_clBuffer); b3Assert( status == CL_SUCCESS ); index+=arg->m_argSizeInBytes; } else { cl_int status = clSetKernelArg( m_kernel, m_idx++, arg->m_argSizeInBytes, &arg->m_argData); b3Assert( status == CL_SUCCESS ); } b3KernelArgData b; memcpy(&b,arg,sizeof(b3KernelArgDataUnaligned)); m_kernelArguments.push_back(b); } m_serializationSizeInBytes = index; return index; } int b3LauncherCL::validateResults(unsigned char* goldBuffer, int goldBufferCapacity, cl_context ctx) { int index=0; int numArguments = *(int*) &goldBuffer[index]; index+=sizeof(int); if (numArguments != m_kernelArguments.size()) { printf("failed validation: expected %d arguments, found %d\n",numArguments, m_kernelArguments.size()); return -1; } for (int ii=0;iim_argSizeInBytes) { printf("failed validation: argument %d sizeInBytes expected: %d, found %d\n",ii, argGold->m_argSizeInBytes, m_kernelArguments[ii].m_argSizeInBytes); return -2; } { int expected = argGold->m_isBuffer; int found = m_kernelArguments[ii].m_isBuffer; if (expected != found) { printf("failed validation: argument %d isBuffer expected: %d, found %d\n",ii,expected, found); return -3; } } index+=sizeof(b3KernelArgData); if (argGold->m_isBuffer) { unsigned char* memBuf= (unsigned char*) malloc(m_kernelArguments[ii].m_argSizeInBytes); unsigned char* goldBuf = &goldBuffer[index]; for (int j=0;jm_argSizeInBytes; } else { //compare content for (int b=0;bm_argData[b]; int found =m_kernelArguments[ii].m_argData[b]; if (expected != found) { printf("failed validation: argument %d const data at byte position %d expected: %d, found %d\n", ii, b, expected, found); return -5; } } } } return index; } int b3LauncherCL::serializeArguments(unsigned char* destBuffer, int destBufferCapacity) { //initialize to known values for (int i=0;i=m_serializationSizeInBytes); //todo: use the b3Serializer for this to allow for 32/64bit, endianness etc int numArguments = m_kernelArguments.size(); int curBufferSize = 0; int* dest = (int*)&destBuffer[curBufferSize]; *dest = numArguments; curBufferSize += sizeof(int); for (int i=0;im_kernelArguments.size();i++) { b3KernelArgData* arg = (b3KernelArgData*) &destBuffer[curBufferSize]; *arg = m_kernelArguments[i]; curBufferSize+=sizeof(b3KernelArgData); if (arg->m_isBuffer==1) { //copy the OpenCL buffer content cl_int status = 0; status = clEnqueueReadBuffer( m_commandQueue, arg->m_clBuffer, 0, 0, arg->m_argSizeInBytes, &destBuffer[curBufferSize], 0,0,0 ); b3Assert( status==CL_SUCCESS ); clFinish(m_commandQueue); curBufferSize+=arg->m_argSizeInBytes; } } return curBufferSize; } void b3LauncherCL::serializeToFile(const char* fileName, int numWorkItems) { int num = numWorkItems; int buffSize = getSerializationBufferSize(); unsigned char* buf = new unsigned char[buffSize+sizeof(int)]; for (int i=0;i