Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package kaptainwutax;
- import static org.lwjgl.opencl.CL10.CL_DEVICE_GLOBAL_MEM_SIZE;
- import static org.lwjgl.opencl.CL10.CL_DEVICE_LOCAL_MEM_SIZE;
- import static org.lwjgl.opencl.CL10.CL_DEVICE_MAX_CLOCK_FREQUENCY;
- import static org.lwjgl.opencl.CL10.CL_DEVICE_MAX_COMPUTE_UNITS;
- import static org.lwjgl.opencl.CL10.CL_DEVICE_NAME;
- import static org.lwjgl.opencl.CL10.CL_DEVICE_TYPE;
- import static org.lwjgl.opencl.CL10.CL_DEVICE_TYPE_ALL;
- import static org.lwjgl.opencl.CL10.CL_DEVICE_TYPE_GPU;
- import static org.lwjgl.opencl.CL10.CL_MEM_COPY_HOST_PTR;
- import static org.lwjgl.opencl.CL10.CL_MEM_READ_ONLY;
- import static org.lwjgl.opencl.CL10.CL_MEM_WRITE_ONLY;
- import static org.lwjgl.opencl.CL10.CL_PLATFORM_NAME;
- import static org.lwjgl.opencl.CL10.CL_QUEUE_PROFILING_ENABLE;
- import static org.lwjgl.opencl.CL10.clBuildProgram;
- import static org.lwjgl.opencl.CL10.clCreateBuffer;
- import static org.lwjgl.opencl.CL10.clCreateCommandQueue;
- import static org.lwjgl.opencl.CL10.clCreateKernel;
- import static org.lwjgl.opencl.CL10.clCreateProgramWithSource;
- import static org.lwjgl.opencl.CL10.clEnqueueNDRangeKernel;
- import static org.lwjgl.opencl.CL10.clEnqueueReadBuffer;
- import static org.lwjgl.opencl.CL10.clEnqueueWriteBuffer;
- import static org.lwjgl.opencl.CL10.clFinish;
- import static org.lwjgl.opencl.CL10.clReleaseCommandQueue;
- import static org.lwjgl.opencl.CL10.clReleaseMemObject;
- import java.io.IOException;
- import java.nio.IntBuffer;
- import java.nio.LongBuffer;
- import java.util.List;
- import java.util.Locale;
- import org.lwjgl.BufferUtils;
- import org.lwjgl.LWJGLException;
- import org.lwjgl.PointerBuffer;
- import org.lwjgl.opencl.CL;
- import org.lwjgl.opencl.CLCommandQueue;
- import org.lwjgl.opencl.CLContext;
- import org.lwjgl.opencl.CLDevice;
- import org.lwjgl.opencl.CLKernel;
- import org.lwjgl.opencl.CLMem;
- import org.lwjgl.opencl.CLPlatform;
- import org.lwjgl.opencl.CLProgram;
- import org.lwjgl.opencl.Util;
- public class CLNextFloatState {
- static long SMAX = Long.MAX_VALUE;
- // Data buffers to store the input and result data in
- static IntBuffer position = UtilCL.toIntBuffer(new int[3]);
- static LongBuffer answer = BufferUtils.createLongBuffer(1000000);
- static CLMem answerMem = null;
- static CLCommandQueue queue = null;
- static CLKernel kernel = null;
- static PointerBuffer kernel1DGlobalWorkSize = null;
- static CLPlatform platform;
- static List<CLDevice> devices;
- static CLContext context;
- static String source;
- public static void main(String[] args) {
- initializeCL();
- getValidSeed();
- }
- public static void initializeCL() {
- if(!CL.isCreated())
- try {CL.create();} catch (LWJGLException e) {e.printStackTrace();}
- displayInfo();
- platform = CLPlatform.getPlatforms().get(0);
- devices = platform.getDevices(CL_DEVICE_TYPE_GPU);
- context = null;
- try {
- context = CLContext.create(platform, devices, null, null, null);
- } catch (LWJGLException e1) {
- e1.printStackTrace();
- }
- // Load the source from a resource file
- try {
- source = UtilCL.getResourceAsString("cl/bruteforce.cl");
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public static void getValidSeed() {
- // Create our program and kernel
- CLProgram program = clCreateProgramWithSource(context, source, null);
- Util.checkCLError(clBuildProgram(program, devices.get(0), "", null));
- //try{Util.checkCLError(clBuildProgram(program, devices.get(0), "", null));} catch(Exception e) {e.printStackTrace();};
- // sum has to match a kernel method name in the OpenCL source
- kernel = clCreateKernel(program, "crack", null);
- answer = BufferUtils.createLongBuffer(1000000);
- for(int seedPos = 0; seedPos < 65536; ++seedPos) {/*65536*/
- //if((seedPos % 1023) == 0)
- System.out.println("Starting " + seedPos + ". " + System.currentTimeMillis());
- position = UtilCL.toIntBuffer(new int[] {seedPos, Integer.MAX_VALUE, 0});
- queue = clCreateCommandQueue(context, devices.get(0), CL_QUEUE_PROFILING_ENABLE, null);
- answer = BufferUtils.createLongBuffer(1000000);
- // Allocate memory for our two input buffers and our result buffer
- CLMem seedPosMem = clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, position, null);
- clEnqueueWriteBuffer(queue, seedPosMem, 1, 0, position, null, null);
- answerMem = clCreateBuffer(context, CL_MEM_WRITE_ONLY | CL_MEM_COPY_HOST_PTR, answer, null);
- clFinish(queue);
- // Execution our kernel
- kernel1DGlobalWorkSize = BufferUtils.createPointerBuffer(1);
- kernel1DGlobalWorkSize.put(0, Integer.MAX_VALUE);
- kernel.setArg(0, seedPosMem);
- kernel.setArg(1, answerMem);
- clEnqueueNDRangeKernel(queue, kernel, 1, null, kernel1DGlobalWorkSize, null, null, null);
- // Read the results memory back into our result buffer
- clEnqueueReadBuffer(queue, answerMem, 1, 0, answer, null, null);
- clFinish(queue);
- UtilCL.print(answer, true);
- clReleaseMemObject(seedPosMem);
- clReleaseMemObject(answerMem);
- clReleaseCommandQueue(queue);
- }
- return;
- //clReleaseContext(context);
- //CL.destroy();
- }
- public static void displayInfo() {
- for (int platformIndex = 0; platformIndex < CLPlatform.getPlatforms().size(); platformIndex++) {
- CLPlatform platform = CLPlatform.getPlatforms().get(platformIndex);
- System.out.println("Platform #" + platformIndex + ":" + platform.getInfoString(CL_PLATFORM_NAME));
- List<CLDevice> devices = platform.getDevices(CL_DEVICE_TYPE_ALL);
- for (int deviceIndex = 0; deviceIndex < devices.size(); deviceIndex++) {
- CLDevice device = devices.get(deviceIndex);
- System.out.printf(Locale.ENGLISH, "Device #%d(%s):%s\n",
- deviceIndex,
- UtilCL.getDeviceType(device.getInfoInt(CL_DEVICE_TYPE)),
- device.getInfoString(CL_DEVICE_NAME));
- System.out.printf(Locale.ENGLISH, "\tCompute Units: %d @ %d mghtz\n",
- device.getInfoInt(CL_DEVICE_MAX_COMPUTE_UNITS), device.getInfoInt(CL_DEVICE_MAX_CLOCK_FREQUENCY));
- System.out.printf(Locale.ENGLISH, "\tLocal memory: %s\n",
- UtilCL.formatMemory(device.getInfoLong(CL_DEVICE_LOCAL_MEM_SIZE)));
- System.out.printf(Locale.ENGLISH, "\tGlobal memory: %s\n",
- UtilCL.formatMemory(device.getInfoLong(CL_DEVICE_GLOBAL_MEM_SIZE)));
- System.out.println();
- }
- }
- }
- }
- package kaptainwutax;
- import static org.lwjgl.opencl.CL10.CL_DEVICE_TYPE_ACCELERATOR;
- import static org.lwjgl.opencl.CL10.CL_DEVICE_TYPE_CPU;
- import static org.lwjgl.opencl.CL10.CL_DEVICE_TYPE_DEFAULT;
- import static org.lwjgl.opencl.CL10.CL_DEVICE_TYPE_GPU;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.nio.FloatBuffer;
- import java.nio.IntBuffer;
- import java.nio.LongBuffer;
- import java.text.DecimalFormat;
- import org.lwjgl.BufferUtils;
- /**
- * Some utilities for OpenCL
- */
- public class UtilCL {
- /**
- * Private constructor.
- */
- private UtilCL() {
- }
- /**
- * Format a number to a memory size.
- * @param size The size.
- * @return The formatted size!
- */
- public static String formatMemory(long size) {
- if(size <= 0) return "0";
- final String[] units = new String[] { "B", "KB", "MB", "GB", "TB" };
- int digitGroups = (int) (Math.log10(size)/Math.log10(1024));
- return new DecimalFormat("#,##0.#").format(size/Math.pow(1024, digitGroups)) + " " + units[digitGroups];
- }
- /**
- * Get the device type.
- * @param i The device type id.
- * @return The device type.
- */
- public static String getDeviceType(int i) {
- switch(i) {
- case CL_DEVICE_TYPE_DEFAULT: return "DEFAULT";
- case CL_DEVICE_TYPE_CPU: return "CPU";
- case CL_DEVICE_TYPE_GPU: return "GPU";
- case CL_DEVICE_TYPE_ACCELERATOR: return "ACCELERATOR";
- }
- return "?";
- }
- /**
- * Utility method to convert float array to float buffer
- *
- * @param floats - the float array to convert
- * @return a float buffer containing the input float array
- */
- public static FloatBuffer toFloatBuffer(float[] floats) {
- FloatBuffer buf = BufferUtils.createFloatBuffer(floats.length).put(floats);
- buf.rewind();
- return buf;
- }
- public static LongBuffer toLongBuffer(long[] longs) {
- LongBuffer buf = BufferUtils.createLongBuffer(longs.length).put(longs);
- buf.rewind();
- return buf;
- }
- public static IntBuffer toIntBuffer(int[] ints) {
- IntBuffer buf = BufferUtils.createIntBuffer(ints.length).put(ints);
- buf.rewind();
- return buf;
- }
- /**
- * Utility method to print a float buffer
- *
- * @param buffer - the float buffer to print to System.out
- */
- public static void print(FloatBuffer buffer) {
- for(int i = 0; i < buffer.capacity(); i++) {
- System.out.print(buffer.get(i) + " ");
- }
- System.out.println("");
- }
- public static void print(LongBuffer buffer, boolean file) {
- if(buffer.get(0) == 0)return;
- if(!file) {
- for(int i = 0; i < buffer.capacity(); i++) {
- if(buffer.get(i) == 0)break;
- System.out.print((buffer.get(i)) + " ");
- }
- } else {
- try {
- FileWriter writer = new FileWriter(new File("bruteforce_result"), true);
- for(int i = 0; i < buffer.capacity(); i++) {
- if(buffer.get(i) == 0)break;
- writer.write(buffer.get(i) + "\n");
- }
- writer.flush();
- writer.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- /**
- * Read a resource into a string.
- * @param filePath The resource to read.
- * @return The resource as a string.
- * @throws java.io.IOException
- */
- public static String getResourceAsString(String filePath) throws IOException {
- InputStream is = UtilCL.class.getClassLoader().getResourceAsStream(filePath);
- if (is == null) {
- throw new IOException("Can't find resource: " + filePath);
- }
- BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
- StringBuilder sb = new StringBuilder();
- String line;
- while ((line = br.readLine()) != null) {
- sb.append(line);
- }
- return sb.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement