ROT13
ROT13

Reputation: 3

ROT13 with CUDA - passing char array to kernel

I'm trying to make some ROT13 encoder with CUDA, but I have problem with passing char array to kernel. Could anyone tell me what I'm doing wrong?

#include <iostream>
#include <conio.h>
#include <string>
#include <cuda.h>
#define CIPHER_NUMBER 13

using namespace std;

__global__ void ROT13(char* text, int length)
{
    for (unsigned int i = 0; i < length; i++)
    {
        if ((text[i] >= 'A' && text[i] <= 'M') || (text[i] >= 'a' && text[i] <= 'm'))
            text[i] += CIPHER_NUMBER;
        else if ((text[i] >= 'N' && text[i] <= 'Z') || (text[i] >= 'n' && text[i] <= 'z'))
            text[i] -= CIPHER_NUMBER;
    }
}

int main()
{
    char* text = "Hello world!";
    char* d_text;
    cudaMalloc(&d_text, sizeof(char*));
    cudaMemcpy(d_text, &text, sizeof(char*), cudaMemcpyHostToDevice);
    ROT13 <<<1, 1>>>(d_text, 12);
    cudaMemcpy(&text, d_text, sizeof(char*), cudaMemcpyDeviceToHost);
    cout << "The answer is: " << text << endl;
    cudaFree(d_text);
    getch();
    return 0;
}

Console should print: "Uryyb jbeyq!", but it prints: "Hello world!".

Upvotes: 0

Views: 298

Answers (1)

Robert Crovella
Robert Crovella

Reputation: 151859

If you're having trouble with a CUDA code, you should run your code with cuda-memcheck and also use proper cuda error checking.

Anyway your usage of character arrays is wrong in several ways.

The main issue is that you are not copying a pointer from host to device and back, you are copying a character string. This string is 12 characters long, not sizeof(char*) which is equal to 8.

The following code has these issues fixed:

$ cat t1179.cu
#include <iostream>
#include <string>
#define CIPHER_NUMBER 13

using namespace std;

__global__ void ROT13(char* text, int length)
{
    for (unsigned int i = 0; i < length; i++)
    {
        if ((text[i] >= 'A' && text[i] <= 'M') || (text[i] >= 'a' && text[i] <= 'm'))
            text[i] += CIPHER_NUMBER;
        else if ((text[i] >= 'N' && text[i] <= 'Z') || (text[i] >= 'n' && text[i] <= 'z'))
            text[i] -= CIPHER_NUMBER;
    }
}

int main()
{
    char text[] = "Hello world!";
    char* d_text;
    cudaMalloc(&d_text, 12*sizeof(char));
    cudaMemcpy(d_text, text, 12*sizeof(char), cudaMemcpyHostToDevice);
    ROT13 <<<1, 1>>>(d_text, 12);
    cudaMemcpy(text, d_text, 12*sizeof(char), cudaMemcpyDeviceToHost);
    cout << "The answer is: " << text << endl;
    cudaFree(d_text);
    return 0;
}
$ nvcc -o t1179 t1179.cu
$ cuda-memcheck ./t1179
========= CUDA-MEMCHECK
The answer is: Uryyb jbeyq!
========= ERROR SUMMARY: 0 errors
$

Upvotes: 3

Related Questions