ROT13与CUDA - 将char数组传递给内核

时间:2016-07-03 17:06:05

标签: cuda

我试图用CUDA制作一些ROT13编码器,但是我将char数组传递给内核有问题。谁能告诉我我做错了什么?

#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;
}

控制台应该打印:&#34; Uryyb jbeyq!&#34;,但它打印出来:&#34; Hello world!&#34;。

1 个答案:

答案 0 :(得分:3)

如果您在使用CUDA代码时遇到问题,则应使用sizeof(char*)运行代码并使用proper cuda error checking

无论如何,你对字符数组的使用在几个方面都是错误的。

主要问题是您没有将指针从主机复制到设备并返回,您正在复制字符串。此字符串长度为12个字符,而不是$ 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 $ ,等于8。

以下代码修复了这些问题:

<?import javafx.scene.control.ChoiceBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.RadioButton?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="320.0" prefWidth="480.0" style="-fx-background-color: SteelBlue;" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="simulazione.esame.ControllerPannello">
    <children>
      <Label fx:id="orario" layoutX="32.0" layoutY="65.0" prefHeight="25.0" prefWidth="247.0" text="dunno" />
      <ChoiceBox fx:id="settimana" layoutX="14.0" layoutY="116.0" prefWidth="150.0" />
      <Label layoutX="195.0" layoutY="108.0" text="Stato Impianto" />
      <RadioButton fx:id="onRadio" layoutX="207.0" layoutY="129.0" mnemonicParsing="false" text="on" />
      <RadioButton fx:id="offRadio" layoutX="263.0" layoutY="129.0" mnemonicParsing="false" text="off" />
      <AnchorPane layoutX="181.0" layoutY="180.0" prefHeight="135.0" prefWidth="289.0">
         <children>
            <Label layoutX="14.0" layoutY="23.0" text="Temp per raffreddamento" />
            <Label layoutX="14.0" layoutY="74.0" text="Temp per riscaldamento" />
            <TextField layoutX="176.0" layoutY="19.0" prefHeight="25.0" prefWidth="79.0" />
            <TextField layoutX="176.0" layoutY="70.0" prefHeight="25.0" prefWidth="79.0" />
         </children>
      </AnchorPane>

    </children>
</AnchorPane>