Commits

brunoro committed 8bca758

compiling kernel stub YAY

Comments (0)

Files changed (4)

 CC = clang
 CFLAGS = -Wall -g
-LFLAGS = -lm -framework opencl
+LFLAGS = -lm -lOpenCL
+#LFLAGS = -lm -framework opencl
 RM = rm -f
 SRC = $(wildcard *.c)
 OBJS = $(SRC:.c=.o)
 /**
  * Expand the cipher key into the encryption key schedule.
  */
-int AES_set_encrypt_key(const unsigned char *userKey, const int bits,
-      AES_KEY *key) {
-
+int AES_set_encrypt_key(const unsigned char *userKey, const int bits, AES_KEY *key) {
   u32 *rk;
-     int i = 0;
+  int i = 0;
   u32 temp;
 
   if (!userKey || !key)
 /**
  * Expand the cipher key into the decryption key schedule.
  */
-int AES_set_decrypt_key(const unsigned char *userKey, const int bits,
-       AES_KEY *key) {
-
-        u32 *rk;
+int AES_set_decrypt_key(const unsigned char *userKey, const int bits, AES_KEY *key) {
+  u32 *rk;
   int i, j, status;
   u32 temp;
 
  * Encrypt a single block
  * in and out can overlap
  */
-void AES_encrypt(const unsigned char *in, unsigned char *out,
-     const AES_KEY *key) {
-
+void AES_encrypt(const unsigned char *in, unsigned char *out, const AES_KEY *key) {
   const u32 *rk;
   u32 s0, s1, s2, s3, t0, t1, t2, t3;
   int r;
  * Decrypt a single block
  * in and out can overlap
  */
-void AES_decrypt(const unsigned char *in, unsigned char *out,
-     const AES_KEY *key) {
+void AES_decrypt(const unsigned char *in, unsigned char *out, const AES_KEY *key) {
 
   const u32 *rk;
   u32 s0, s1, s2, s3, t0, t1, t2, t3;

opencl/aes_test

Binary file modified.
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdbool.h>
+#include <assert.h>
 
 #include "aes.h"
 
   #include "CL/cl.h"
 #endif
 
-cl_context initialize_opencl() {
+typedef struct condev {
+  cl_context context;
+  cl_device_id device;
+};
+
+struct condev initialize_opencl() {
   // query platforms
   cl_uint platformIdCount = 0;
   clGetPlatformIDs (0, NULL, &platformIdCount);
   cl_context context = clCreateContext(contextProperties, deviceIdCount, 
 				       deviceIds, NULL, NULL, &error);
 
-  if (error != CL_SUCCESS) {
-    printf("clCreateContext error!\n");
-    exit(1);
-  }
+  assert(error == CL_SUCCESS);
+
+  struct condev cd;
+  cd.context = context;
+  cd.device = deviceIds[0];
+  return cd;
+}
+
+char* oclLoadProgSource(const char* cFilename, const char* cPreamble, size_t* szFinalLength)
+{
+    // locals 
+    FILE* pFileStream = NULL;
+    size_t szSourceLength;
+
+    // open the OpenCL source code file
+    #ifdef _WIN32   // Windows version
+        if(fopen_s(&pFileStream, cFilename, "rb") != 0) 
+        {       
+            return NULL;
+        }
+    #else           // Linux version
+        pFileStream = fopen(cFilename, "rb");
+        if(pFileStream == 0) 
+        {       
+            return NULL;
+        }
+    #endif
+
+    size_t szPreambleLength = strlen(cPreamble);
 
-  return context;
+    // get the length of the source code
+    fseek(pFileStream, 0, SEEK_END); 
+    szSourceLength = ftell(pFileStream);
+    fseek(pFileStream, 0, SEEK_SET); 
+
+    // allocate a buffer for the source code string and read it in
+    char* cSourceString = (char *)malloc(szSourceLength + szPreambleLength + 1); 
+    memcpy(cSourceString, cPreamble, szPreambleLength);
+    if (fread((cSourceString) + szPreambleLength, szSourceLength, 1, pFileStream) != 1)
+    {
+        fclose(pFileStream);
+        free(cSourceString);
+        return 0;
+    }
+
+    // close the file and return the total length of the combined (preamble + source) string
+    fclose(pFileStream);
+    if(szFinalLength != 0)
+    {
+        *szFinalLength = szSourceLength + szPreambleLength;
+    }
+    cSourceString[szSourceLength + szPreambleLength] = '\0';
+
+    return cSourceString;
+}
+
+cl_kernel compile_kernel(cl_context context, cl_device_id device) {
+  cl_int error;
+
+  // uses NVIDIA helper functions to get the code string and it's size (in bytes)
+  size_t src_size = 0;
+  const char* path = "aes.cl";
+  const char* source = oclLoadProgSource(path, "", &src_size);
+  cl_program program = clCreateProgramWithSource(context, 1, &source, &src_size, &error);
+  assert(error == CL_SUCCESS);
+  
+  // builds the program
+  error = clBuildProgram(program, 1, &device, NULL, NULL, NULL);
+  assert(error == CL_SUCCESS);
+  
+  // shows the log
+  char* build_log;
+  size_t log_size;
+
+  // first call to know the proper size
+  clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, 0, NULL, &log_size);
+  build_log = NEW_ARRAY(char, log_size + 1);
+
+  // second call to get the log
+  clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, log_size, build_log, NULL);
+  build_log[log_size] = '\0';
+
+  printf("%s\n", build_log);
+  free(build_log);
+  
+  // extracting the kernel
+  cl_kernel kernel = clCreateKernel(program, "aes_encode", &error);
+  assert(error == CL_SUCCESS);
+
+  return kernel;
 }
 
 int main(int argc, char** argv) {
-  cl_context context = initialize_opencl();
+  struct condev cd = initialize_opencl();
+  cl_context context = cd.context;
+  cl_device_id device = cd.device;
+  cl_kernel kernel = compile_kernel(context, device);
 
   AES_KEY key; 
+  
+  // host memory
   const unsigned char *passwd = "aes_passwd",
 		      *input  = "gustavo";
   unsigned char* enc = NEW_ARRAY(unsigned char, BUFFER_LENGTH);
   unsigned char* dec = NEW_ARRAY(unsigned char, BUFFER_LENGTH);
 
+  // device memory
+  cl_int error;
+  cl_mem d_key = clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, 
+				sizeof(AES_KEY), &key, &error);
+  assert(error == CL_SUCCESS);
+  cl_mem d_input = clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, 
+				  strlen(input) * sizeof(char), input, &error);
+  assert(error == CL_SUCCESS);
+  cl_mem d_enc = clCreateBuffer(context, CL_MEM_READ_WRITE, 
+				BUFFER_LENGTH * sizeof(char), NULL, &error);
+  assert(error == CL_SUCCESS);
+  cl_mem d_dec = clCreateBuffer(context, CL_MEM_READ_WRITE, 
+				BUFFER_LENGTH * sizeof(char), NULL, &error);
+  assert(error == CL_SUCCESS);
+
+  /*
   AES_set_encrypt_key((const unsigned char*) passwd, 256, &key); 
   AES_encrypt((const unsigned char*) input, enc, (const AES_KEY*) &key); 
 
   printf("input:  %s\n", input);
   printf("enc:    %s\n", enc);
   printf("dec:    %s\n", dec);
+  */
   
   return 0;
 }