From WikiTemp, the GBAtemp wiki
(OpenCL C++)
m
 
(One intermediate revision by one other user not shown)
Line 1: Line 1:
== Welcome ==
+
This is my dev blog for the DSI OpenCL…  Right know i am more focused on the OpenCL library and the openCL language that is used to create the kernel.
 
+
 
+
This is my dev wiki for the DSI OpenCL…  Right know i am more focused on the OpenCL library and the openCL language that is used to create the kernel.
+
  
 
I will make sure that i explain how it works and what is happining so that if anyone wants to compile it on their computer they will be confident that it will work correctly
 
I will make sure that i explain how it works and what is happining so that if anyone wants to compile it on their computer they will be confident that it will work correctly
  
 
+
[[Category:Members of GBAtemp]]
 
+
 
+
 
+
 
+
''== '''THIS CODE DOES NOT RUN BUT DOES COMPILE USE AT OWN RISK''' ==''
+
 
+
 
+
== OpenCL C++ ==
+
 
+
<code>
+
#include <oclUtils.h>
+
#include <cstdlib>
+
#include <time.h>
+
 
+
const char* cSourceFile = "KeyPattern.cl";
+
 
+
cl_uchar16 encTitleKey;
+
cl_uchar16 encData;
+
cl_uchar16 decData;
+
cl_uchar16 IV = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
+
cl_uchar16 answer = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
+
cl_uint iter;
+
 
+
cl_mem encTitleKeyMem;
+
cl_mem encDataMem;
+
cl_mem decDataMem;
+
cl_mem IVMem;
+
cl_mem answerMem;
+
 
+
cl_context cxGPUContext;        // OpenCL context
+
cl_command_queue cqCommandQue;  // OpenCL command que
+
cl_device_id* cdDevices;        // OpenCL device list   
+
cl_program cpProgram;          // OpenCL program
+
cl_kernel ckKernel;            // OpenCL kernel
+
size_t szGlobalWorkSize = 4294967295; //Max Integer
+
size_t szLocalWorkSize=256;     // 1D var for # of work items in the work group
+
size_t szParmDataBytes; // Byte size of context information
+
size_t szKernelLength; // Byte size of kernel code
+
char* cPathAndName = NULL;      // var for full paths to data, src, etc.
+
char* cSourceCL = NULL;        // Buffer to hold source for compilation
+
 
+
cl_int Err1;
+
cl_int Err2;
+
cl_int Err3;
+
cl_int Err4;
+
cl_int Err5;
+
 
+
bool checkAnswer(cl_uchar16);
+
void Cleanup (int iExitCode);
+
 
+
int main(int argc, char **argv)
+
{
+
int itime = time(NULL);
+
srand(itime);
+
// start logs
+
    shrSetLogFileName ("oclVectorAdd.txt");
+
    shrLog(LOGBOTH, 0.0, "starting TheKeyMaker running %i at a time with a seed of %i ", szGlobalWorkSize,itime);
+
 
+
 
+
cxGPUContext = clCreateContextFromType(0, CL_DEVICE_TYPE_GPU, NULL, NULL, &Err1);
+
shrLog(LOGBOTH, 0.0, "clCreateContextFromType...\n");
+
    if (Err1 != CL_SUCCESS)
+
    {
+
 
+
        shrLog(LOGBOTH, 0.0, "Error in clCreateContextFromType, Line %u in file %s !!!\n\n", __LINE__, __FILE__);
+
        Cleanup(EXIT_FAILURE);
+
    }
+
 
+
Err1 = clGetContextInfo(cxGPUContext, CL_CONTEXT_DEVICES, 0, NULL, &szParmDataBytes);
+
    cdDevices = (cl_device_id*)malloc(szParmDataBytes);
+
    Err1 |= clGetContextInfo(cxGPUContext, CL_CONTEXT_DEVICES, szParmDataBytes, cdDevices, NULL);
+
   
+
shrLog(LOGBOTH, 0.0, "clGetContextInfo...\n");
+
if (Err1 != CL_SUCCESS)
+
    {
+
        shrLog(LOGBOTH, 0.0, "Error in clGetContextInfo, Line %u in file %s !!!\n\n", __LINE__, __FILE__);
+
        Cleanup(EXIT_FAILURE);
+
    }
+
 
+
cqCommandQue = clCreateCommandQueue(cxGPUContext, cdDevices[0], 0, &Err1);
+
    shrLog(LOGBOTH, 0.0, "clCreateCommandQueue...\n");
+
    if (Err1 != CL_SUCCESS)
+
    {
+
        shrLog(LOGBOTH, 0.0, "Error in clCreateCommandQueue, Line %u in file %s !!!\n\n", __LINE__, __FILE__);
+
        Cleanup(EXIT_FAILURE);
+
    }
+
/*
+
cl_mem encTitleKeyMem;
+
cl_mem encDataMem;
+
cl_mem decDataMem;
+
cl_mem IVMem;
+
cl_mem answerMem;
+
*/
+
 
+
encTitleKeyMem = clCreateBuffer(cxGPUContext, CL_MEM_READ_ONLY, sizeof(cl_uchar16) , NULL, &Err1);
+
    encDataMem = clCreateBuffer(cxGPUContext, CL_MEM_READ_ONLY, sizeof(cl_uchar16),  NULL, &Err2);
+
    decDataMem = clCreateBuffer(cxGPUContext, CL_MEM_READ_ONLY, sizeof(cl_uchar16) , NULL, &Err3);
+
IVMem = clCreateBuffer(cxGPUContext, CL_MEM_READ_ONLY, sizeof(cl_uchar16),  NULL, &Err4);
+
    answerMem = clCreateBuffer(cxGPUContext, CL_MEM_WRITE_ONLY, sizeof(cl_uchar16) , NULL, &Err5);
+
 
+
Err1|=Err2;
+
Err1|=Err3;
+
Err1|=Err4;
+
Err1|=Err5;
+
 
+
if (Err1 != CL_SUCCESS)
+
    {
+
        shrLog(LOGBOTH, 0.0, "Error in clCreateBuffer, Line %u in file %s !!!\n\n", __LINE__, __FILE__);
+
        Cleanup(EXIT_FAILURE);
+
    }
+
 
+
shrLog(LOGBOTH, 0.0, "oclLoadProgSource (%s)...\n", cSourceFile);
+
cPathAndName = shrFindFilePath(cSourceFile, argv[0]);
+
    cSourceCL = oclLoadProgSource(cPathAndName, "", &szKernelLength);
+
shrLog(LOGBOTH, 0.0, "Source\n \n %s \n",cSourceCL);
+
 
+
cpProgram = clCreateProgramWithSource(cxGPUContext, 1, (const char **)&cSourceCL, &szKernelLength, &Err1);
+
shrLog(LOGBOTH, 0.0, "clCreateProgramWithSource...\n");
+
    if (Err1 != CL_SUCCESS)
+
    {
+
        shrLog(LOGBOTH, 0.0, "Error in clCreateProgramWithSource, Line %u in file %s !!!\n\n", __LINE__, __FILE__);
+
        Cleanup(EXIT_FAILURE);
+
    }   
+
 
+
clBuildProgram(cpProgram, 0, NULL, NULL, NULL, &Err1);
+
    shrLog(LOGBOTH, 0.0, "clBuildProgram...\n");
+
    if (Err1 != CL_SUCCESS)
+
    {
+
        shrLog(LOGBOTH, 0.0, "Error in clBuildProgram, Line %u in file %s !!!\n\n", __LINE__, __FILE__);
+
        Cleanup(EXIT_FAILURE);
+
    }
+
 
+
ckKernel = clCreateKernel(cpProgram, "program", &Err1);
+
shrLog(LOGBOTH, 0.0, "clCreateKernel (program)...\n");
+
    if (Err1 != CL_SUCCESS)
+
    {
+
        shrLog(LOGBOTH, 0.0, "Error in clCreateKernel, Line %u in file %s !!!\n\n", __LINE__, __FILE__);
+
Cleanup(EXIT_FAILURE);
+
    }
+
 
+
//cl_mem encTitleKeyMem;
+
//cl_mem encDataMem;
+
//cl_mem decDataMem;
+
//cl_mem IVMem;
+
//cl_mem answerMem;
+
 
+
iter = rand();
+
Err1 = clSetKernelArg(ckKernel, 0, sizeof(cl_mem), (void*)&encTitleKeyMem);
+
    Err1 |= clSetKernelArg(ckKernel, 1, sizeof(cl_mem), (void*)&encDataMem);
+
    Err1 |= clSetKernelArg(ckKernel, 2, sizeof(cl_mem), (void*)&decDataMem);
+
    Err1 |= clSetKernelArg(ckKernel, 3, sizeof(cl_mem), (void*)&IVMem);
+
    Err1 |= clSetKernelArg(ckKernel, 4, sizeof(cl_mem), (void*)&answerMem);
+
    Err1 |= clSetKernelArg(ckKernel, 5, sizeof(cl_int), (void*)&iter);
+
 
+
shrLog(LOGBOTH, 0.0, "clSetKernelArg 0 - 5...\n\n");
+
    if (Err1 != CL_SUCCESS)
+
    {
+
        shrLog(LOGBOTH, 0.0, "Error in clSetKernelArg, Line %u in file %s !!!\n\n", __LINE__, __FILE__);
+
        Cleanup(EXIT_FAILURE);
+
    }
+
/*
+
cl_mem encTitleKeyMem;
+
cl_mem encDataMem;
+
cl_mem decDataMem;
+
cl_mem IVMem;
+
*/
+
 
+
Err1 = clEnqueueWriteBuffer(cqCommandQue, encTitleKeyMem, CL_FALSE, 0, sizeof(cl_uchar16), encTitleKey, 0, NULL, NULL);
+
    Err1 |= clEnqueueWriteBuffer(cqCommandQue, encDataMem, CL_FALSE, 0, sizeof(cl_uchar16), encData, 0, NULL, NULL);
+
    Err1 |= clEnqueueWriteBuffer(cqCommandQue, decDataMem, CL_FALSE, 0, sizeof(cl_uchar16), decData, 0, NULL, NULL);
+
    Err1 |= clEnqueueWriteBuffer(cqCommandQue, IVMem, CL_FALSE, 0, sizeof(cl_uchar16), IV, 0, NULL, NULL);
+
    Err1 |= clEnqueueWriteBuffer(cqCommandQue, answerMem, CL_FALSE, 0, sizeof(cl_uchar16), answer, 0, NULL, NULL);
+
shrLog(LOGBOTH, 0.0, "clEnqueueWriteBuffer (SrcA and SrcB)...\n");
+
    if (Err1 != CL_SUCCESS)
+
    {
+
        shrLog(LOGBOTH, 0.0, "Error in clEnqueueWriteBuffer, Line %u in file %s !!!\n\n", __LINE__, __FILE__);
+
        Cleanup(EXIT_FAILURE);
+
    }
+
 
+
shrLog(LOGBOTH, 0.0, "running...");
+
while(true){
+
 
+
clEnqueueNDRangeKernel(cqCommandQue, ckKernel, 1, NULL, &szGlobalWorkSize, &szLocalWorkSize, 0, NULL, NULL);
+
 
+
 
+
clEnqueueReadBuffer(cqCommandQue, answerMem, CL_TRUE, 0, sizeof(cl_uchar16), answer, 0, NULL, NULL);
+
if(checkAnswer(answer))
+
break;
+
 
+
iter = rand();
+
clSetKernelArg(ckKernel, 5, sizeof(cl_int), (void*)&iter);
+
}
+
 
+
if(checkAnswer(answer))
+
shrLog(LOGBOTH, 0.0, "found");
+
+
Cleanup(EXIT_SUCCESS);
+
}
+
 
+
void Cleanup (int iExitCode)
+
{
+
    // Cleanup allocated objects
+
    shrLog(LOGBOTH, 0.0, "Starting Cleanup...\n\n");
+
    if(cdDevices)free(cdDevices);
+
    if(cPathAndName)free(cPathAndName);
+
    if(cSourceCL)free(cSourceCL);
+
if(ckKernel)clReleaseKernel(ckKernel); 
+
    if(cpProgram)clReleaseProgram(cpProgram);
+
    if(cqCommandQue)clReleaseCommandQueue(cqCommandQue);
+
    if(cxGPUContext)clReleaseContext(cxGPUContext);
+
 
+
/*
+
cl_mem encTitleKeyMem;
+
cl_mem encDataMem;
+
cl_mem decDataMem;
+
cl_mem IVMem;
+
cl_mem answerMem;
+
*/
+
    if(encTitleKeyMem)clReleaseMemObject(encTitleKeyMem);
+
    if(encDataMem)clReleaseMemObject(encDataMem);
+
    if(decDataMem)clReleaseMemObject(decDataMem);
+
    if(IVMem)clReleaseMemObject(IVMem);
+
    if(answerMem)clReleaseMemObject(answerMem);
+
 
+
 
+
    shrLog(LOGBOTH | CLOSELOG, 0.0, "oclVectorAdd.exe Exiting...\nPress <Enter> to Quit\n");
+
    getchar();
+
   
+
    exit (iExitCode);
+
}
+
 
+
bool checkAnswer(cl_uchar16 answer){
+
for(int i =0;i<16;i++)
+
if(answer[i]!=0)
+
return true;
+
return false;
+
}
+
</code>
+
 
+
== OpenCL CL ==
+
 
+
 
+
<nowiki>
+
__kernel void keyMaker(__global uchar16 encTitleKey,__global uchar16 encData, __global uchar16 decData,__global uchar16 IV,__global uchar16 answer,__global int iterator){
+
 
+
 
+
 
+
 
+
}
+
 
+
</nowiki>
+

Latest revision as of 13:41, 17 February 2010

This is my dev blog for the DSI OpenCL… Right know i am more focused on the OpenCL library and the openCL language that is used to create the kernel.

I will make sure that i explain how it works and what is happining so that if anyone wants to compile it on their computer they will be confident that it will work correctly