0

I want to use a string matching in managedCuda. But how can I initialized it?

i've tried using C# version, here's the examples:

stringAr = new List<string>();
        stringAr.Add("you");
        stringAr.Add("your");
        stringAr.Add("he");
        stringAr.Add("she");
        stringAr.Add("shes");

for the string matching, i've used this code:

bool found = false;
        for (int i = 0; i < stringAr.Count; i++)
        {
            found = (stringAr[i]).IndexOf(textBox2.Text) > -1;
            if (found) break;
        }
        if (found && textBox2.Text != "")
        {
            label1.Text = "Found!";
        }
        else
        {
            label1.Text = "Not Found!";
        }

I also allocate input h_A in host memory

            string[] h_B = new string[N];

When i want to allocate in device memory and copy vectors from host memory to device memory

        CudaDeviceVariable<string[]> d_B = h_B;

It gave me this error

The type 'string[]' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'CudaDeviceVariable<T>'

any help?

Thomas Yap
  • 15
  • 3

1 Answers1

1

Based on the documentation and your error message, only non-nullable value type can be used with CudaDeviceVariable.

Change stringAr list to a char[] (or byte[]) array, and then allocate it on device by using CudaDeviceVariable with generic parameter char (or byte).

EDIT1 Here is the code that change stringAr to byte[] array:

byte[] stringArAsBytes = stringAr
                .SelectMany(s => System.Text.Encoding.ASCII.GetBytes(s))
                .ToArray();

then try something like this:

CudaDeviceVariable<byte> d_data = stringArAsBytes;
  • Caution, char is not a blittable type in .net: It's representation in managed and unmanaged memory is not the same. So convert you string to byte[] to be on the safe side. – kunzmi Aug 08 '18 at 15:30
  • @kunzmi how to convert on safe side? can you show me some example? – Thomas Yap Aug 08 '18 at 16:52
  • @Slawomir Orlowski i've tried it, but it got same result.. how can i allocate it with generic parameter? got any example? – Thomas Yap Aug 08 '18 at 16:55
  • @Slawomir Orlowski Thanks mate :) works like charm.. if i want to return it to string, how can i do it? – Thomas Yap Aug 09 '18 at 15:25
  • @ThomasYap You're welcome :) I'm not very familiar with managedCuda. If you can access device buffer `d_data` directly, then try this: `string s = System.Text.Encoding.ASCII.GetString(d_data, 0, d_data.Length);`. I'm not quite sure if `d_data.Length` is a proper way to obtain length of `d_data`. Replace it if necessary. – Slawomir Orlowski Aug 10 '18 at 06:33
  • @SlawomirOrlowski thankyou very much.. But i found thread that discuss about it [here](https://stackoverflow.com/questions/18627304/convert-ascii-in-a-byte-array-to-string). Hope it can be reference to everyone. :) – Thomas Yap Aug 10 '18 at 11:57