Advertisement
Guest User

Stuff

a guest
Feb 16th, 2019
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.01 KB | None | 0 0
  1. package kaptainwutax;
  2.  
  3. import static org.lwjgl.opencl.CL10.CL_DEVICE_GLOBAL_MEM_SIZE;
  4. import static org.lwjgl.opencl.CL10.CL_DEVICE_LOCAL_MEM_SIZE;
  5. import static org.lwjgl.opencl.CL10.CL_DEVICE_MAX_CLOCK_FREQUENCY;
  6. import static org.lwjgl.opencl.CL10.CL_DEVICE_MAX_COMPUTE_UNITS;
  7. import static org.lwjgl.opencl.CL10.CL_DEVICE_NAME;
  8. import static org.lwjgl.opencl.CL10.CL_DEVICE_TYPE;
  9. import static org.lwjgl.opencl.CL10.CL_DEVICE_TYPE_ALL;
  10. import static org.lwjgl.opencl.CL10.CL_DEVICE_TYPE_GPU;
  11. import static org.lwjgl.opencl.CL10.CL_MEM_COPY_HOST_PTR;
  12. import static org.lwjgl.opencl.CL10.CL_MEM_READ_ONLY;
  13. import static org.lwjgl.opencl.CL10.CL_MEM_WRITE_ONLY;
  14. import static org.lwjgl.opencl.CL10.CL_PLATFORM_NAME;
  15. import static org.lwjgl.opencl.CL10.CL_QUEUE_PROFILING_ENABLE;
  16. import static org.lwjgl.opencl.CL10.clBuildProgram;
  17. import static org.lwjgl.opencl.CL10.clCreateBuffer;
  18. import static org.lwjgl.opencl.CL10.clCreateCommandQueue;
  19. import static org.lwjgl.opencl.CL10.clCreateKernel;
  20. import static org.lwjgl.opencl.CL10.clCreateProgramWithSource;
  21. import static org.lwjgl.opencl.CL10.clEnqueueNDRangeKernel;
  22. import static org.lwjgl.opencl.CL10.clEnqueueReadBuffer;
  23. import static org.lwjgl.opencl.CL10.clEnqueueWriteBuffer;
  24. import static org.lwjgl.opencl.CL10.clFinish;
  25. import static org.lwjgl.opencl.CL10.clReleaseCommandQueue;
  26. import static org.lwjgl.opencl.CL10.clReleaseMemObject;
  27.  
  28. import java.io.IOException;
  29. import java.nio.IntBuffer;
  30. import java.nio.LongBuffer;
  31. import java.util.List;
  32. import java.util.Locale;
  33.  
  34. import org.lwjgl.BufferUtils;
  35. import org.lwjgl.LWJGLException;
  36. import org.lwjgl.PointerBuffer;
  37. import org.lwjgl.opencl.CL;
  38. import org.lwjgl.opencl.CLCommandQueue;
  39. import org.lwjgl.opencl.CLContext;
  40. import org.lwjgl.opencl.CLDevice;
  41. import org.lwjgl.opencl.CLKernel;
  42. import org.lwjgl.opencl.CLMem;
  43. import org.lwjgl.opencl.CLPlatform;
  44. import org.lwjgl.opencl.CLProgram;
  45. import org.lwjgl.opencl.Util;
  46.  
  47. public class CLNextFloatState {
  48.    
  49.     static long SMAX = Long.MAX_VALUE;
  50.    
  51.     // Data buffers to store the input and result data in
  52.     static IntBuffer position = UtilCL.toIntBuffer(new int[3]);
  53.     static LongBuffer answer = BufferUtils.createLongBuffer(1000000);
  54.     static CLMem answerMem = null;
  55.    
  56.     static CLCommandQueue queue = null;
  57.     static CLKernel kernel = null;
  58.     static PointerBuffer kernel1DGlobalWorkSize = null;
  59.     static CLPlatform platform;
  60.     static List<CLDevice> devices;
  61.     static CLContext context;
  62.     static String source;
  63.    
  64.     public static void main(String[] args) {
  65.         initializeCL();
  66.         getValidSeed();
  67.     }
  68.    
  69.     public static void initializeCL() {
  70.         if(!CL.isCreated())
  71.             try {CL.create();} catch (LWJGLException e) {e.printStackTrace();}
  72.        
  73.         displayInfo();
  74.        
  75.         platform = CLPlatform.getPlatforms().get(0);
  76.         devices = platform.getDevices(CL_DEVICE_TYPE_GPU);
  77.         context = null;
  78.         try {
  79.             context = CLContext.create(platform, devices, null, null, null);
  80.         } catch (LWJGLException e1) {
  81.             e1.printStackTrace();
  82.         }
  83.        
  84.        
  85.         // Load the source from a resource file
  86.         try {
  87.             source = UtilCL.getResourceAsString("cl/bruteforce.cl");
  88.         } catch (IOException e) {
  89.             e.printStackTrace();
  90.         }    
  91.     }
  92.    
  93.     public static void getValidSeed() {
  94.         // Create our program and kernel
  95.         CLProgram program = clCreateProgramWithSource(context, source, null);
  96.         Util.checkCLError(clBuildProgram(program, devices.get(0), "", null));
  97.         //try{Util.checkCLError(clBuildProgram(program, devices.get(0), "", null));} catch(Exception e) {e.printStackTrace();};
  98.         // sum has to match a kernel method name in the OpenCL source
  99.         kernel = clCreateKernel(program, "crack", null);    
  100.         answer = BufferUtils.createLongBuffer(1000000);    
  101.         for(int seedPos = 0; seedPos < 65536; ++seedPos) {/*65536*/
  102.             //if((seedPos % 1023) == 0)
  103.             System.out.println("Starting " + seedPos + ". " + System.currentTimeMillis());
  104.             position = UtilCL.toIntBuffer(new int[] {seedPos, Integer.MAX_VALUE, 0});          
  105.             queue = clCreateCommandQueue(context, devices.get(0), CL_QUEUE_PROFILING_ENABLE, null);
  106.             answer = BufferUtils.createLongBuffer(1000000);
  107.             // Allocate memory for our two input buffers and our result buffer
  108.             CLMem seedPosMem = clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, position, null);
  109.             clEnqueueWriteBuffer(queue, seedPosMem, 1, 0, position, null, null);
  110.             answerMem = clCreateBuffer(context, CL_MEM_WRITE_ONLY | CL_MEM_COPY_HOST_PTR, answer, null);           
  111.             clFinish(queue);
  112.            
  113.             // Execution our kernel
  114.             kernel1DGlobalWorkSize = BufferUtils.createPointerBuffer(1);
  115.             kernel1DGlobalWorkSize.put(0, Integer.MAX_VALUE);
  116.             kernel.setArg(0, seedPosMem);
  117.             kernel.setArg(1, answerMem);
  118.            
  119.             clEnqueueNDRangeKernel(queue, kernel, 1, null, kernel1DGlobalWorkSize, null, null, null);
  120.    
  121.             // Read the results memory back into our result buffer
  122.             clEnqueueReadBuffer(queue, answerMem, 1, 0, answer, null, null);
  123.             clFinish(queue);
  124.             UtilCL.print(answer, true);
  125.             clReleaseMemObject(seedPosMem);
  126.             clReleaseMemObject(answerMem);
  127.             clReleaseCommandQueue(queue);
  128.         }
  129.         return;
  130.         //clReleaseContext(context);
  131.         //CL.destroy();
  132.        
  133.     }
  134.  
  135.     public static void displayInfo() {
  136.         for (int platformIndex = 0; platformIndex < CLPlatform.getPlatforms().size(); platformIndex++) {
  137.             CLPlatform platform = CLPlatform.getPlatforms().get(platformIndex);
  138.             System.out.println("Platform #" + platformIndex + ":" + platform.getInfoString(CL_PLATFORM_NAME));
  139.             List<CLDevice> devices = platform.getDevices(CL_DEVICE_TYPE_ALL);
  140.             for (int deviceIndex = 0; deviceIndex < devices.size(); deviceIndex++) {
  141.                 CLDevice device = devices.get(deviceIndex);
  142.                 System.out.printf(Locale.ENGLISH, "Device #%d(%s):%s\n",
  143.                         deviceIndex,
  144.                         UtilCL.getDeviceType(device.getInfoInt(CL_DEVICE_TYPE)),
  145.                         device.getInfoString(CL_DEVICE_NAME));
  146.                 System.out.printf(Locale.ENGLISH, "\tCompute Units: %d @ %d mghtz\n",
  147.                         device.getInfoInt(CL_DEVICE_MAX_COMPUTE_UNITS), device.getInfoInt(CL_DEVICE_MAX_CLOCK_FREQUENCY));
  148.                 System.out.printf(Locale.ENGLISH, "\tLocal memory: %s\n",
  149.                         UtilCL.formatMemory(device.getInfoLong(CL_DEVICE_LOCAL_MEM_SIZE)));
  150.                 System.out.printf(Locale.ENGLISH, "\tGlobal memory: %s\n",
  151.                         UtilCL.formatMemory(device.getInfoLong(CL_DEVICE_GLOBAL_MEM_SIZE)));
  152.                 System.out.println();
  153.             }
  154.         }
  155.     }
  156.    
  157. }
  158.  
  159. package kaptainwutax;
  160.  
  161. import static org.lwjgl.opencl.CL10.CL_DEVICE_TYPE_ACCELERATOR;
  162. import static org.lwjgl.opencl.CL10.CL_DEVICE_TYPE_CPU;
  163. import static org.lwjgl.opencl.CL10.CL_DEVICE_TYPE_DEFAULT;
  164. import static org.lwjgl.opencl.CL10.CL_DEVICE_TYPE_GPU;
  165.  
  166. import java.io.BufferedReader;
  167. import java.io.File;
  168. import java.io.FileWriter;
  169. import java.io.IOException;
  170. import java.io.InputStream;
  171. import java.io.InputStreamReader;
  172. import java.nio.FloatBuffer;
  173. import java.nio.IntBuffer;
  174. import java.nio.LongBuffer;
  175. import java.text.DecimalFormat;
  176.  
  177. import org.lwjgl.BufferUtils;
  178.  
  179. /**
  180.  * Some utilities for OpenCL
  181.  */
  182. public class UtilCL {
  183.  
  184.     /**
  185.      * Private constructor.
  186.      */
  187.     private UtilCL() {
  188.  
  189.     }
  190.  
  191.     /**
  192.      * Format a number to a memory size.
  193.      * @param size The size.
  194.      * @return The formatted size!
  195.      */
  196.     public static String formatMemory(long size) {
  197.         if(size <= 0) return "0";
  198.         final String[] units = new String[] { "B", "KB", "MB", "GB", "TB" };
  199.         int digitGroups = (int) (Math.log10(size)/Math.log10(1024));
  200.         return new DecimalFormat("#,##0.#").format(size/Math.pow(1024, digitGroups)) + " " + units[digitGroups];
  201.     }
  202.  
  203.     /**
  204.      * Get the device type.
  205.      * @param i The device type id.
  206.      * @return The device type.
  207.      */
  208.     public static String getDeviceType(int i) {
  209.         switch(i) {
  210.             case CL_DEVICE_TYPE_DEFAULT: return "DEFAULT";
  211.             case CL_DEVICE_TYPE_CPU: return "CPU";
  212.             case CL_DEVICE_TYPE_GPU: return "GPU";
  213.             case CL_DEVICE_TYPE_ACCELERATOR: return "ACCELERATOR";
  214.         }
  215.         return "?";
  216.     }
  217.  
  218.     /**
  219.      * Utility method to convert float array to float buffer
  220.      *
  221.      * @param floats - the float array to convert
  222.      * @return a float buffer containing the input float array
  223.      */
  224.     public static FloatBuffer toFloatBuffer(float[] floats) {
  225.         FloatBuffer buf = BufferUtils.createFloatBuffer(floats.length).put(floats);
  226.         buf.rewind();
  227.         return buf;
  228.     }
  229.  
  230.     public static LongBuffer toLongBuffer(long[] longs) {
  231.         LongBuffer buf = BufferUtils.createLongBuffer(longs.length).put(longs);
  232.         buf.rewind();
  233.         return buf;
  234.     }
  235.    
  236.     public static IntBuffer toIntBuffer(int[] ints) {
  237.         IntBuffer buf = BufferUtils.createIntBuffer(ints.length).put(ints);
  238.         buf.rewind();
  239.         return buf;
  240.     }
  241.    
  242.     /**
  243.      * Utility method to print a float buffer
  244.      *
  245.      * @param buffer - the float buffer to print to System.out
  246.      */
  247.     public static void print(FloatBuffer buffer) {
  248.         for(int i = 0; i < buffer.capacity(); i++) {
  249.             System.out.print(buffer.get(i) + " ");
  250.         }
  251.         System.out.println("");
  252.     }
  253.  
  254.     public static void print(LongBuffer buffer, boolean file) {
  255.         if(buffer.get(0) == 0)return;
  256.         if(!file) {
  257.             for(int i = 0; i < buffer.capacity(); i++) {
  258.                 if(buffer.get(i) == 0)break;
  259.                     System.out.print((buffer.get(i)) + " ");
  260.             }
  261.         } else {
  262.             try {
  263.                 FileWriter writer = new FileWriter(new File("bruteforce_result"), true);
  264.                
  265.                 for(int i = 0; i < buffer.capacity(); i++) {
  266.                         if(buffer.get(i) == 0)break;
  267.                         writer.write(buffer.get(i) + "\n");
  268.                 }
  269.                
  270.                 writer.flush();
  271.                 writer.close();
  272.             } catch (IOException e) {
  273.                 // TODO Auto-generated catch block
  274.                 e.printStackTrace();
  275.             }
  276.         }
  277.     }
  278.     /**
  279.      * Read a resource into a string.
  280.      * @param filePath The resource to read.
  281.      * @return The resource as a string.
  282.      * @throws java.io.IOException
  283.      */
  284.     public static String getResourceAsString(String filePath) throws IOException {
  285.         InputStream is = UtilCL.class.getClassLoader().getResourceAsStream(filePath);
  286.         if (is == null) {
  287.             throw new IOException("Can't find resource: " + filePath);
  288.         }
  289.         BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
  290.         StringBuilder sb = new StringBuilder();
  291.         String line;
  292.         while ((line = br.readLine()) != null) {
  293.             sb.append(line);
  294.         }
  295.         return sb.toString();
  296.     }
  297. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement