0

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!".

2
  • Why do you think you are doing something wrong? What issues are you experiencing? Which kind of errors etc. Commented Jul 3, 2016 at 17:25
  • Additional information added to post. Commented Jul 3, 2016 at 17:31

1 Answer 1

3

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
$
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.