CUDA .Net compare strings

692 views Asked by At

I'm trying to compare lists of strings (huge amount of elements in each of lists). Could somebody help me to do it using cudafy? I guess that in that case I should use jagged arrays of char but I've got an CudafyCompileException - expression must have class type (tried this approach). It worked just for two strings (char[]). So any ideas how I can do that?

Code sample for 2 strings:

var km = CudafyTranslator.Cudafy();
_gpu = CudafyHost.GetDevice();
_gpu.LoadModule(km);
var strFirst = "Hello, world";
var strSecond = "Hi world";
var devResult = _gpu.Allocate<char>(strFirst.Length);
var first = strFirst.ToCharArray();
var second = strSecond.ToCharArray();
var result = new char[strFirst.Length];
var devFirst = _gpu.CopyToDevice(first);
var devSecond = _gpu.CopyToDevice(second);
_gpu.Launch(N, 1).CompareStrings(devFirst, devSecond, devResult);
_gpu.CopyFromDevice(devResult, result);
var hostStr = new string(result);
Console.WriteLine(hostStr);

And the method itself:

[Cudafy]
public static void CompareStrings(GThread thread, char[] c, char[] b, char[] result)
{
    int tid = thread.blockIdx.x;
    if (tid < c.Length)
    {
        if (c[tid] == b[tid])
        {
            result[tid] = c[tid];
        }
     }
}
1

There are 1 answers

0
becike On
  • instead c.Length pass the length as a parameter
  • be careful if you useing Unicode character, that 2 bytes