Giter VIP home page Giter VIP logo

Comments (4)

TillAlex avatar TillAlex commented on September 17, 2024 2

The problem is that char is defined differently in C# and OpenCL:

In C# char is 16 bit: https://docs.microsoft.com/en-us/dotnet/api/system.char?view=netframework-4.8

In OpenCL char is 8 bit: https://www.khronos.org/registry/OpenCL/sdk/1.0/docs/man/xhtml/scalarDataTypes.html

Your code works if you replace char by byte in the host part. After casting the byte array to char the output is correct.

from cloo.

TillAlex avatar TillAlex commented on September 17, 2024

Can you post some more code? From your example it seems that you are not executing your kernel, but only setting the argument. Also if you perform a non blocking read you will have to synchronize before checking the content of the host buffer.

from cloo.

parxal avatar parxal commented on September 17, 2024

Full Code

            try
            {
                char[] pw_cracked = new char[5];
                var kernelCl = File.ReadAllText("kernel.cl");
                ComputePlatform platform = ComputePlatform.Platforms[0];
                ComputeContextPropertyList properties = new ComputeContextPropertyList(platform);
                ComputeContext context = new ComputeContext(platform.Devices, properties, null, IntPtr.Zero);
                ComputeBuffer<char> cracked_pw_mem_obj = new ComputeBuffer<char>(context, ComputeMemoryFlags.WriteOnly | ComputeMemoryFlags.CopyHostPointer, pw_cracked);

                ComputeProgram program = new ComputeProgram(context, kernelCl);
                program.Build(null, null, null, IntPtr.Zero);
                ComputeKernel kernel = program.CreateKernel("vector_add");
                kernel.SetMemoryArgument(0, cracked_pw_mem_obj);
                ComputeEventList eventList = new ComputeEventList();
                ComputeCommandQueue commands = new ComputeCommandQueue(context, context.Devices[0], ComputeCommandQueueFlags.None);
                commands.Execute(kernel, null, new long[] { GLOBAL_ITEM_SIZE }, new long[] { LOCAL_ITEM_SIZE }, eventList);
                commands.ReadFromBuffer(cracked_pw_mem_obj, ref pw_cracked, false, eventList);
                commands.Finish();
                Console.WriteLine("Cracked Password: " + String.Join("", pw_cracked));

                commands.Dispose();

                foreach (ComputeEventBase eventBase in eventList)
                {
                    eventBase.Dispose();
                }

                eventList.Clear();
                kernel.Dispose();
                program.Dispose();
                cracked_pw_mem_obj.Dispose();

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.ReadKey();

from cloo.

parxal avatar parxal commented on September 17, 2024

Yep! it was that :)

byte[] pw_cracked = new byte[maxlen + 1];
ComputeBuffer<byte> cracked_pw_mem_obj = new ComputeBuffer<byte>(context, ComputeMemoryFlags.WriteOnly | ComputeMemoryFlags.CopyHostPointer, pw_cracked);
Console.WriteLine("Cracked Password: " + String.Join("", pw_cracked.Select( p => Convert.ToChar(p))));

from cloo.

Related Issues (11)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.