Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package jogampjoclhelloworld;
- import com.jogamp.opencl.CLBuffer;
- import com.jogamp.opencl.CLCommandQueue;
- import com.jogamp.opencl.CLContext;
- import com.jogamp.opencl.CLDevice;
- import com.jogamp.opencl.CLDevice.Type;
- import com.jogamp.opencl.CLEventList;
- import com.jogamp.opencl.CLKernel;
- import com.jogamp.opencl.CLMemory.Mem;
- import com.jogamp.opencl.CLPlatform;
- import com.jogamp.opencl.CLProgram;
- import java.io.IOException;
- import java.nio.ByteBuffer;
- public class Main {
- public static void main(String[] args) throws IOException {
- String source = "#pragma OPENCL EXTENSION cl_khr_byte_addressable_store : enable\n__constant uchar hw[] = "
- + "\"Uryyb Jbeyq!\\n\";\n\n__kernel void copyCipher(__global uchar * out) {\n size_t tid = "
- + "get_global_id(0);\n out[tid] = hw[tid];\n}\n\n__kernel void decipher(__global uchar * in, "
- + "__global uchar * out) {\n size_t tid = get_global_id(0);\n\n uchar letter = in[tid];\n "
- + "if ((letter <= 'Z') && (letter >= 'A')) {\n letter += 13;\n if (letter > 'Z') {\n"
- + " letter -= 'Z' - 'A' + 1;\n }\n } else if ((letter <= 'z') && (letter >= 'a')) "
- + "{\n letter += 13;\n if (letter > 'z') {\n letter -= 'z' - 'a' + 1;\n "
- + "}\n }\n\n out[tid] = letter;\n}";
- byte[] helloWorld = new byte["Hello World!\n".length() + 1];
- ByteBuffer cipherBuffer = ByteBuffer.allocateDirect(helloWorld.length);
- ByteBuffer decodedBuffer = ByteBuffer.allocateDirect(helloWorld.length);
- CLPlatform clPlatform = CLPlatform.getDefault();
- CLContext clContext = CLContext.create(clPlatform, Type.GPU);
- CLBuffer clCipherBuffer = clContext.createBuffer(cipherBuffer, new Mem[]{Mem.READ_WRITE});
- CLBuffer clDecodedBuffer = clContext.createBuffer(decodedBuffer, new Mem[]{Mem.WRITE_ONLY});
- CLDevice clDevice = clContext.getMaxFlopsDevice();
- CLProgram clProgram = clContext.createProgram(source);
- clProgram.build();
- CLKernel clCopyKernel = clProgram.createCLKernel("copyCipher");
- clCopyKernel.setArg(0, clCipherBuffer);
- CLKernel clDecipherKernel = clProgram.createCLKernel("decipher");
- clDecipherKernel.setArg(0, clCipherBuffer);
- clDecipherKernel.setArg(1, clDecodedBuffer);
- CLCommandQueue clCommandQueue = clDevice.createCommandQueue();
- CLEventList eventList = new CLEventList(1);
- clCommandQueue.put1DRangeKernel(clCopyKernel, 0, helloWorld.length, 1, eventList);
- clCommandQueue.put1DRangeKernel(clDecipherKernel, 0, helloWorld.length, 1, eventList, null);
- clCommandQueue.putReadBuffer(clDecodedBuffer, true);
- decodedBuffer.get(helloWorld);
- System.out.println(new String(helloWorld));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement