Advertisement
Guest User

Untitled

a guest
Feb 5th, 2012
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.89 KB | None | 0 0
  1. #--------------------------------------------#
  2. # pyclaes_ecb.py
  3. #--------------------------------------------#
  4.  
  5. import numpy
  6. import pyopencl as cl
  7.  
  8. class claes_ecb(object):
  9.  
  10.     def __init__(self):
  11.         ''
  12.         self._initOpenCL();
  13.  
  14.  
  15.     # TODO: raise proper exceptions
  16.     def _initOpenCL(self):
  17.         ''
  18.         # Get available platforms and choose the first one
  19.         # TODO: select among available platforms?
  20.         platforms = cl.get_platforms();
  21.         if len(platforms) == 0:
  22.             print("Failed to find any OpenCL platform")
  23.             return None
  24.        
  25.         # Get avaible GPU devices and choose the first one
  26.         # TODO: select among available gpus?
  27.         devices = platforms[0].get_devices(cl.device_type.GPU)
  28.         if len(devices) == 0:
  29.             print("Could not find GPU devices")
  30.             return None
  31.         self.gpuDevice = devices[0]
  32.  
  33.         # Retrieve useful device informations
  34.         self.wgSize = self.gpuDevice.get_info(cl.device_info.MAX_WORK_GROUP_SIZE)
  35.         self.compUnits = self.gpuDevice.get_info(cl.device_info.MAX_COMPUTE_UNITS)
  36.         self.maxWiSizes = self.gpuDevice.get_info(cl.device_info.MAX_WORK_ITEM_SIZES)
  37.         # TODO: identify Nvidia platform for getting warp size
  38.         #self.warpSize = self.gpuDevice.get_info(cl.device_info.WARP_SIZE_NV)
  39.         self.globMemSize = self.gpuDevice.get_info(cl.device_info.GLOBAL_MEM_SIZE)
  40.         self.locMemSize = self.gpuDevice.get_info(cl.device_info.LOCAL_MEM_SIZE)
  41.         self.constBuffSize = self.gpuDevice.get_info(cl.device_info.MAX_CONSTANT_BUFFER_SIZE)
  42.         self.addrBits = self.gpuDevice.get_info(cl.device_info.ADDRESS_BITS)
  43.  
  44.         # Create a context using the first GPU device found
  45.         self.context = cl.Context([self.gpuDevice])
  46.  
  47.         # Finally create the command queue
  48.         self.cmdQueue = cl.CommandQueue(self.context, self.gpuDevice)
  49.            
  50.        
  51.     def encrypt(self, key, data):
  52.         ''
  53.         # Get key length and buffer size
  54.         keyLen = len(key)
  55.         buffSize = len(data)
  56.  
  57.         # Choose optimal parameters
  58.         # TODO: automate choice of:
  59.         # - bulk size;
  60.         # - local work items size;
  61.         # - global work items size
  62.  
  63.         # Build the kernel
  64.         # TODO: perform only once
  65.         kernelFile = open('aes_ecb.cl', 'r')
  66.         kernelSrc = kernelFile.read()
  67.        
  68.         program = cl.Program(self.context, kernelSrc)
  69.         program.build(devices=[self.gpuDevice])
  70.         print program
  71.  
  72.         # Load key and data into device memory
  73.         keyBuffer = cl.Buffer(self.context, cl.mem_flags.READ_ONLY|cl.mem_flags.COPY_HOST_PTR, hostbuf=key)
  74.         inBuffer = cl.Buffer(self.context, cl.mem_flags.READ_WRITE|cl.mem_flags.COPY_HOST_PTR, hostbuf=data)
  75.  
  76.         # Launch the kernel (awesome :''D)
  77.         # TODO: just testing the correctness of the encryption kernel. Execute only one workitem.
  78.         # After correctness is ensured, use the proper global and local shapes
  79.         program.aes_ecb(self.cmdQueue, (1,1), None, keyBuffer, inBuffer).wait()
  80.        
  81.         # Read encryption result
  82.         result = numpy.empty_like(inBuffer)
  83.         #result = numpy.zeros(176<<3, dtype=numpy.uint8)
  84.         cl.enqueue_copy(self.cmdQueue, result, inBuffer)
  85.         print result
  86.         return result
  87.        
  88.  
  89.  
  90.  
  91. if __name__ == '__main__':
  92.     key = numpy.zeros(128, dtype=numpy.uint8)
  93.     data = numpy.zeros(176<<3, dtype=numpy.uint8)
  94.  
  95.  
  96.     foo = claes_ecb()
  97.     print('Device info:')
  98.     print('\twgSize: ' + str(foo.wgSize))
  99.     print('\tcompUnits: ' + str(foo.compUnits))
  100.     print('\tmaxWiSizes: ' + str(foo.maxWiSizes))
  101.     print('\tglobMemSize: ' + str(foo.globMemSize))
  102.     print('\tlocMemSize: ' + str(foo.locMemSize))
  103.     print('\tconstBuffSize: ' + str(foo.constBuffSize))
  104.     print('\taddrBits: ' + str(foo.addrBits))
  105.    
  106.     foo.encrypt(key, data)
  107.  
  108.     exit(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement