sha1 with cloo

Help
2010-09-28
2012-12-21
  • dasha oblenko
    dasha oblenko
    2010-09-28

    hi,
    i tried a sha1 application with cloo like that..
    kernel builds successfulyy with clootils.exe and here too.
    but it crashes after the   digest = Queue.Read<byte>(bufV3, null);

    would you please help with it?

    the code is like that:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Cloo;

    namespace forum
    {
        class Program
        {
            static void Main(string args)
            {
                //Number of Platforms
                int numPlats = ComputePlatform.Platforms.Count;
                ComputeContextPropertyList Properties = new ComputeContextPropertyList(ComputePlatform.Platforms); ComputeContext context = new ComputeContext(ComputeDeviceTypes.All, Properties, null, IntPtr.Zero);

                //Vector sum source code
                string vecSum = @"
    #pragma OPENCL EXTENSION cl_khr_byte_addressable_store : enable
    // Roger Pau Monné
    // 2009-09-08
    // royger@gmail.com

    #define K0  0x5A827999
    #define K1  0x6ED9EBA1
    #define K2  0x8F1BBCDC
    #define K3  0xCA62C1D6

    #define H1 0x67452301
    #define H2 0xEFCDAB89
    #define H3 0x98BADCFE
    #define H4 0x10325476
    #define H5 0xC3D2E1F0

    uint rotateLeft(uint x, int n)
    {
        return  (x << n) | (x >> (32-n));
    }
    void sha1_set_len(__global char *word, int len)
    {
      uint ulen = len & 0xFFFFFFFF;

      word = ulen >> 24;
      word = ulen >> 16;
      word = ulen >> 8;
      word = ulen;
    }
    void memset_user(__global char *s, int c, int size)
    {
        int i;
        for(i = 0; i < size; i++)
        {
            s_ = (char) c;
        }
        return;
    }
    __kernel void sha1(__global char *msg, __global const int *len, __global char *digest)
    {
        int t, word_pad, gid, i, j, gid41;
        uint W, A, temp, number;
        char hexChars = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
       
        gid = get_global_id(0);
        gid41 = gid * 41;
        word_pad = gid * 64;
        memset_user(&msg[word_pad + len], 0, 64 - len);
        msg[word_pad + len] = (char) 0x80;
        sha1_set_len(&msg, len * 8);

        A = H1;
        A = H2;
        A = H3;
        A = H4;
        A = H5;
       
        for (t = 0; t < 16; t++)
        {
            W = ((uchar) msg) << 24;
            W |= ((uchar) msg) << 16;
            W |= ((uchar) msg) << 8;
            W |= (uchar) msg;
        }
       
        for(i = 16; i < 80; i++)
        {
            W = rotateLeft(W ^ W ^ W ^ W, 1);
        }
       
        for(i = 0; i < 20; i++)
        {
            temp = rotateLeft(A,5) + ((A & A) | ((~ A) & A)) + A + W + K0;
            A = A;
            A = A;
            A = rotateLeft(A, 30);
            A = A;
            A = temp;
        }
       
        for(i = 20; i < 40; i++)
        {
            temp = rotateLeft(A, 5) + (A ^ A ^ A) + A + W + K1;
            A = A;
            A = A;
            A = rotateLeft(A, 30);
            A = A;
            A = temp;
        }

        for(i = 40; i < 60; i++)
        {
            temp = rotateLeft(A, 5) + ((A & A) | (A & A) | (A & A)) + A + W + K2;
            A = A;
            A = A;
            A = rotateLeft(A, 30);
            A = A;
            A = temp;
        }

        for(i = 60; i < 80; i++)
        {
            temp = rotateLeft(A, 5) + (A ^ A ^ A)  + A + W + K3;
            A = A;
            A = A;
            A = rotateLeft(A, 30);
            A = A;
            A = temp;
        }
        A += H1;
        A += H2;
        A += H3;
        A += H4;
        A += H5;

        for(j = 0; j < 5; j++)
        {
            number = A;
            for(i = 0; i < 8; i++)
            {
                digest = hexChars;
                number /= 16;
            }
        }
        digest = '\0';
    }
    "; //Get a list of devices
                List<ComputeDevice> Devs = new List<ComputeDevice>();
                Devs.Add(ComputePlatform.Platforms.Devices);
                //Devs.Add(ComputePlatform.Platforms.Devices);
                Devs.Add(ComputePlatform.Platforms.Devices);

                //Create a new OpenCL program
                ComputeProgram prog = null;
                try
                {
                    prog = new ComputeProgram(context, vecSum); prog.Build(Devs, "", null, IntPtr.Zero);

                }

                catch

                { }
                //Create the kernel
                ComputeKernel kernelVecSum = prog.CreateKernel("sha1");

                //In case you want to create all kernels at the same time
                ICollection<ComputeKernel> Kernels = prog.CreateAllKernels();
                foreach (ComputeKernel k in Kernels)
                {
                }

                //Creates host variables to pass to device memory
              
              
                byte data = { 41, 41, 41, 41, 41, 41, 41, 41 };
                byte digest = { 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41 };
             
            
                int len = new int;
          
                len = digest.Length;
                ComputeBuffer<byte> bufV1 = new ComputeBuffer<byte>(context, ComputeMemoryFlags.WriteOnly | ComputeMemoryFlags.UseHostPointer, data);
                ComputeBuffer<int> bufV2 = new ComputeBuffer<int>(context, ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.UseHostPointer, len);
                ComputeBuffer<byte> bufV3 = new ComputeBuffer<byte>(context, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.UseHostPointer, digest);
       
        
                //You have to set kernels arguments by manually assigning them
                //This has a API-like fashion
                kernelVecSum.SetMemoryArgument(0, bufV1);
                kernelVecSum.SetMemoryArgument(1, bufV2);
                kernelVecSum.SetMemoryArgument(2, bufV3);

             
                //Create the command queue
                ComputeCommandQueue Queue = new ComputeCommandQueue(context, ComputePlatform.Platforms.Devices, ComputeCommandQueueFlags.None);

                //Enqueue the Execute command.
                Queue.Execute(kernelVecSum, null, new long { digest.Length }, null, null);

                //Enqueue read command.
                digest = Queue.Read<byte>(bufV3, null);

            }
        }
    }
    _

     
  • laterafter
    laterafter
    2011-06-02

    have you tried something like this ?

    byte[] v3 = new byte[digest.Length];
    Queue.ReadFromBuffer(bufV3,ref v3, true,null);