0

I have a text char array, filled with words from a text file.

For example
text[0] = "one";,
text[1] = "two";
and so on.

All array elements equal to thread count. I want to print all text array to screen, using CUDA. I try to print using following code, but it does not work. I'm pretty confused how to pass an array like text[][] to a CUDA kernel function.

#define MAX_SIZE 100   
#define elements 20 

__global__ void calculate(char *d_text) {
  int idx = threadIdx.x;
  printf("test %s /n", d_text[idx]);
}

int main() {
  char text[MAX_SIZE][MAX_SIZE]; // have text array with words
  char *d_text;

  cudaMalloc((void **)&d_data, DATA_BYTES);

  cudaMemcpy(d_text, text, STRING_BYTES, cudaMemcpyHostToDevice);

  calculate << < 1, elements >> > (d_text);

  cudaDeviceSynchronize();
}
talonmies
  • 70,661
  • 34
  • 192
  • 269
Simas
  • 97
  • 1
  • 12

1 Answers1

2

multidimensional arrays in CUDA often require "deep copy" operations when moving data between host and device. However, in this case we can take advantage of the fact that the width of your array is fixed (known at compile-time) at MAX_SIZE. We can define a type of this width, to make 2D array handling approximately as simple as 1D array handling:

$ cat t426.cu
#include <stdio.h>

const char s1[] = "one\0";
const char s2[] = "two\0";
const int MAX_SIZE = 10;
typedef char carr[MAX_SIZE];

__global__ void calculate(carr *text) {
  int idx = threadIdx.x;
  printf("test %s \n", text[idx]);
}

int main() {
  char text[MAX_SIZE][MAX_SIZE]; // have text array with words
  memcpy(text[0], s1, sizeof(s1));
  memcpy(text[1], s2, sizeof(s2));
  carr *d_text;

  cudaMalloc((void **)&d_text, MAX_SIZE*MAX_SIZE);

  cudaMemcpy(d_text, text, MAX_SIZE*MAX_SIZE, cudaMemcpyHostToDevice);

  calculate << < 1, 2 >> > (d_text);

  cudaDeviceSynchronize();
}
$ nvcc -arch=sm_61 -o t426 t426.cu
$ cuda-memcheck ./t426
========= CUDA-MEMCHECK
test one
test two
========= ERROR SUMMARY: 0 errors
$

For a more general treatment of 2D or multidimensional arrays in CUDA, refer to this answer.

Robert Crovella
  • 143,785
  • 11
  • 213
  • 257
  • I think it is worth pointing out that multi-dimensional arrays in CUDA work the same as they work in C++ (not surprising, given that CUDA is a programming language in the C++ family), and from that perspective there is no "special" handling. – njuffa Sep 30 '17 at 15:41
  • Thank you! This is what I was looking for! – Simas Sep 30 '17 at 15:53
  • I edited the answer to remove the reference to "special" handling. – Robert Crovella Sep 30 '17 at 16:19