Advertisement
Guest User

Untitled

a guest
Apr 3rd, 2011
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.90 KB | None | 0 0
  1. package jogampjoclhelloworld;
  2.  
  3. import com.jogamp.opencl.CLBuffer;
  4. import com.jogamp.opencl.CLCommandQueue;
  5. import com.jogamp.opencl.CLContext;
  6. import com.jogamp.opencl.CLDevice;
  7. import com.jogamp.opencl.CLDevice.Type;
  8. import com.jogamp.opencl.CLEventList;
  9. import com.jogamp.opencl.CLKernel;
  10. import com.jogamp.opencl.CLMemory.Mem;
  11. import com.jogamp.opencl.CLPlatform;
  12. import com.jogamp.opencl.CLProgram;
  13. import java.io.IOException;
  14. import java.nio.ByteBuffer;
  15.  
  16. public class Main {
  17.  
  18.     public static void main(String[] args) throws IOException {
  19.  
  20.         String source = "#pragma OPENCL EXTENSION cl_khr_byte_addressable_store : enable\n__constant uchar hw[] = "
  21.                 + "\"Uryyb Jbeyq!\\n\";\n\n__kernel void copyCipher(__global uchar * out) {\n    size_t tid = "
  22.                 + "get_global_id(0);\n    out[tid] = hw[tid];\n}\n\n__kernel void decipher(__global uchar * in, "
  23.                 + "__global uchar * out) {\n    size_t tid = get_global_id(0);\n\n    uchar letter = in[tid];\n    "
  24.                 + "if ((letter <= 'Z') && (letter >= 'A')) {\n        letter += 13;\n        if (letter > 'Z') {\n"
  25.                 + "            letter -= 'Z' - 'A' + 1;\n        }\n    } else if ((letter <= 'z') && (letter >= 'a')) "
  26.                 + "{\n        letter += 13;\n        if (letter > 'z') {\n            letter -= 'z' - 'a' + 1;\n        "
  27.                 + "}\n    }\n\n    out[tid] = letter;\n}";
  28.  
  29.         byte[] helloWorld = new byte["Hello World!\n".length() + 1];
  30.         ByteBuffer cipherBuffer = ByteBuffer.allocateDirect(helloWorld.length);
  31.         ByteBuffer decodedBuffer = ByteBuffer.allocateDirect(helloWorld.length);
  32.  
  33.         CLPlatform clPlatform = CLPlatform.getDefault();
  34.  
  35.         CLContext clContext = CLContext.create(clPlatform, Type.GPU);
  36.  
  37.         CLBuffer clCipherBuffer = clContext.createBuffer(cipherBuffer, new Mem[]{Mem.READ_WRITE});
  38.         CLBuffer clDecodedBuffer = clContext.createBuffer(decodedBuffer, new Mem[]{Mem.WRITE_ONLY});
  39.  
  40.         CLDevice clDevice = clContext.getMaxFlopsDevice();
  41.  
  42.         CLProgram clProgram = clContext.createProgram(source);
  43.         clProgram.build();
  44.  
  45.         CLKernel clCopyKernel = clProgram.createCLKernel("copyCipher");
  46.         clCopyKernel.setArg(0, clCipherBuffer);
  47.         CLKernel clDecipherKernel = clProgram.createCLKernel("decipher");
  48.         clDecipherKernel.setArg(0, clCipherBuffer);
  49.         clDecipherKernel.setArg(1, clDecodedBuffer);
  50.  
  51.         CLCommandQueue clCommandQueue = clDevice.createCommandQueue();
  52.         CLEventList eventList = new CLEventList(1);
  53.         clCommandQueue.put1DRangeKernel(clCopyKernel, 0, helloWorld.length, 1, eventList);
  54.         clCommandQueue.put1DRangeKernel(clDecipherKernel, 0, helloWorld.length, 1, eventList, null);
  55.         clCommandQueue.putReadBuffer(clDecodedBuffer, true);
  56.  
  57.         decodedBuffer.get(helloWorld);
  58.  
  59.         System.out.println(new String(helloWorld));
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement