Output of cuda program is not what was expected

前端 未结 3 1117
隐瞒了意图╮
隐瞒了意图╮ 2020-12-12 06:31
#include
#include
#include
#include


__global__ void setVal(char **c){

c[(blockIdx.y * gridDim.x         


        
3条回答
  •  不知归路
    2020-12-12 07:08

    The main problem with your code is that you're not allocating any device memory for the setValues call. You can't pass it a pointer to host memory (char *x[6]) and expect that to work; the CUDA kernels have to operate on CUDA memory. You create that memory, then operate on it, then copy it back:

    #include 
    #include 
    #include 
    #include 
    
    __global__ void setValues(char *arr){
        arr[blockIdx.y * gridDim.x + blockIdx.x] = '4';
    }
    
    int main() {
        const int NCHARS=6;
        char *xd;
    
        cudaMalloc(&xd, NCHARS);
        dim3 grid(3,2);
        setValues<<>>(xd);
    
        char *p;
        p = (char*) malloc(20*sizeof(char));
        strcpy(p,"");
    
        cudaMemcpy(p, xd, NCHARS, cudaMemcpyDeviceToHost);
        p[NCHARS]='\0';
    
        printf("<%s>\n", p);
        getchar();
    
        cudaFree(xd);
    
        return 0;
    }
    

提交回复
热议问题