CUDA.Net比较字符串

本文关键字:字符串 比较 Net CUDA | 更新日期: 2023-09-27 18:28:26

我正在尝试比较字符串列表(每个列表中都有大量元素)。有人能帮我用库达费吗?我想在这种情况下,我应该使用锯齿状的char数组,但我有一个CudafyCompileException-表达式必须具有类类型(尝试过这种方法)。它只适用于两个字符串(char[])。你知道我该怎么做吗?

2个字符串的代码示例:

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);

方法本身:

[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];
        }
     }
}

CUDA.Net比较字符串

  • 而是c.长度将长度作为参数传递
  • 如果使用Unicode字符,请小心,即2个字节