Advertisement
Guest User

JOCL example problem

a guest
Apr 21st, 2012
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.30 KB | None | 0 0
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5.  
  6.  
  7. import com.jogamp.opencl.CLBuffer;
  8. import com.jogamp.opencl.CLCommandQueue;
  9. import com.jogamp.opencl.CLContext;
  10. import com.jogamp.opencl.CLDevice;
  11. import com.jogamp.opencl.CLKernel;
  12. import com.jogamp.opencl.CLPlatform;
  13. import com.jogamp.opencl.CLProgram;
  14. import java.io.IOException;
  15. import java.io.InputStream;
  16. import java.nio.FloatBuffer;
  17. import java.util.Random;
  18.  
  19. import static java.lang.System.*;
  20. import static com.jogamp.opencl.CLMemory.Mem.*;
  21. import java.io.File;
  22. import java.io.FileNotFoundException;
  23. import static java.lang.Math.*;
  24. import java.net.URLConnection;
  25. import java.nio.file.Files;
  26.  
  27. /**
  28. * Hello Java OpenCL example. Adds all elements of buffer A to buffer B
  29. * and stores the result in buffer C.<br/>
  30. * Sample was inspired by the Nvidia VectorAdd example written in C/C++
  31. * which is bundled in the Nvidia OpenCL SDK.
  32. * @author Michael Bien
  33. */
  34. public class HelloJOCL {
  35.  
  36. public static void main(String[] args) throws IOException {
  37.  
  38. CLPlatform[] clPl = CLPlatform.listCLPlatforms();
  39.  
  40. System.out.println("List Platforms");
  41. for(int i =0;i < clPl.length;i++){
  42. System.out.println(clPl[i].getName());
  43. System.out.println("Devices per platform :"+clPl[i].listCLDevices().length);
  44. System.out.println("Compute Devices on max flops device:"+
  45. clPl[i].getMaxFlopsDevice().getMaxComputeUnits());
  46. System.out.println(" Max flops device:"+
  47. clPl[i].getMaxFlopsDevice().getMaxClockFrequency());
  48. }
  49.  
  50.  
  51.  
  52. // set up (uses default CLPlatform and creates context for all devices)
  53. CLContext context = CLContext.create(clPl[0]);
  54. out.println("created "+context);
  55.  
  56. // always make sure to release the context under all circumstances
  57. // not needed for this particular sample but recommented
  58. try{
  59.  
  60. // select fastest device
  61. CLDevice device = context.getMaxFlopsDevice();
  62. out.println("using "+device);
  63.  
  64. // create command queue on device.
  65. CLCommandQueue queue = device.createCommandQueue();
  66.  
  67. int elementCount = 1444477; // Length of arrays to process
  68. int localWorkSize = min(device.getMaxWorkGroupSize(), 256); // Local work size dimensions
  69. int globalWorkSize = roundUp(localWorkSize, elementCount); // rounded up to the nearest multiple of the localWorkSize
  70.  
  71. String fname = "vector_add.cl";
  72. File f = new File(fname);
  73.  
  74. System.out.println("Current Path:"+f.getAbsolutePath());
  75. if(f.exists()){
  76. System.out.println("Found Kernel File");
  77. }else{
  78. System.out.println("Assuming running from IDE folder.");
  79. String fname2 = "./src/"+fname;
  80. f = new File(fname2);
  81. if(f.exists()){
  82. System.out.println("Found Kernel File");
  83. fname = "./src/"+fname;
  84. }else{
  85. System.out.println("Assuming running from Netbeans distribution folder.");
  86. fname2 = "../src/"+fname;
  87. f = new File(fname2);
  88. if(f.exists()){
  89. System.out.println("Found Kernel File");
  90. fname = "../src/"+fname;
  91. }else{
  92. System.out.println("Kernel File Not Found");}}
  93. }
  94.  
  95. System.out.println("fname:"+fname);
  96. //InputStream vec = HelloJOCL.class.getResourceAsStream(fname);
  97.  
  98. CLProgram program=null;
  99.  
  100. try{
  101. String sourceCode = readFile(fname);
  102.  
  103. // load sources, create and build program
  104. program = context.createProgram(sourceCode).build();
  105.  
  106. System.out.println("prog: "+program);
  107. }catch (Exception e) {
  108. e.printStackTrace();
  109. }
  110.  
  111. // A, B are input buffers, C is for the result
  112. CLBuffer<FloatBuffer> clBufferA = context.createFloatBuffer(globalWorkSize, READ_ONLY);
  113. CLBuffer<FloatBuffer> clBufferB = context.createFloatBuffer(globalWorkSize, READ_ONLY);
  114. CLBuffer<FloatBuffer> clBufferC = context.createFloatBuffer(globalWorkSize, WRITE_ONLY);
  115.  
  116. out.println("used device memory: "
  117. + (clBufferA.getCLSize()+clBufferB.getCLSize()+clBufferC.getCLSize())/1000000 +"MB");
  118.  
  119. float numA = 1.5f;
  120. float numB = 2.3f;
  121. System.out.println("Adding A: "+numA+"+ B: "+numB+" ="+(numA+numB));
  122. // fill input buffers with random numbers
  123. // (just to have test data; seed is fixed -> results will not change between runs).
  124. fillBuffer(clBufferA.getBuffer(), numA);
  125. fillBuffer(clBufferB.getBuffer(), numB);
  126.  
  127. System.out.println("Make kernel.");
  128. // get a reference to the kernel function with the name 'VectorAdd'
  129. // and map the buffers to its input parameters.
  130. CLKernel kernel = program.createCLKernel("vector_add");
  131. kernel.putArgs(clBufferA, clBufferB, clBufferC).putArg(elementCount);
  132.  
  133. System.out.println("Running kernel.");
  134. // asynchronous write of data to GPU device,
  135. // followed by blocking read to get the computed results back.
  136. long time = nanoTime();
  137. queue.putWriteBuffer(clBufferA, false)
  138. .putWriteBuffer(clBufferB, false)
  139. .put1DRangeKernel(kernel, 0, globalWorkSize, localWorkSize)
  140. .putReadBuffer(clBufferC, true);
  141. time = nanoTime() - time;
  142.  
  143. // print first few elements of the resulting buffer to the console.
  144. out.println("a+b=c results snapshot: ");
  145. for(int i = 0; i < 10; i++)
  146. out.print(clBufferC.getBuffer().get() + ", ");
  147. out.println("...; " + clBufferC.getBuffer().remaining() + " more");
  148.  
  149. out.println("computation took: "+(time/1000000)+"ms");
  150.  
  151. }finally{
  152. // cleanup all resources associated with this context.
  153. context.release();
  154. }
  155.  
  156. }
  157.  
  158. private static void fillBuffer(FloatBuffer buffer, float setf) {
  159. while(buffer.remaining() != 0)
  160. buffer.put(setf);
  161. buffer.rewind();
  162. }
  163.  
  164. private static int roundUp(int groupSize, int globalSize) {
  165. int r = globalSize % groupSize;
  166. if (r == 0) {
  167. return globalSize;
  168. } else {
  169. return globalSize + groupSize - r;
  170. }
  171. }
  172.  
  173. private static String readFile(String filename) {
  174. File f = new File(filename);
  175. try {
  176. byte[] bytes = Files.readAllBytes(f.toPath());
  177. return new String(bytes,"UTF-8");
  178. } catch (FileNotFoundException e) {
  179. e.printStackTrace();
  180. } catch (IOException e) {
  181. e.printStackTrace();
  182. }
  183. return "";
  184. }
  185.  
  186.  
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement