user3213307
user3213307

Reputation: 9

why invalid argument for cudamalloc for (long long **)

I'm trying to pass an array of ptrs to a a number of long long arrays. The code compiles okay but I get an execution error of invalid argument when I try to cudalloc the device spce for the ptr array ??? How should I be doing that ? My code snippet follows:

        long long**  my_d_bit_LL_ptrs;
        long long**  my_d_cs_LL_ptrs;
        long long**  my_d_bit_LL_regs;
        long long**  my_d_cs_LL_regs;

                // allocate a common original array, org by Bits, that has both Bits & CS ptr info 
        cuda_rtn_err = cudaMalloc(&d_LL_reg, ((points+1)<<4)*sizeof(long long));
        if ( cudaSuccess != cuda_rtn_err ) {
             printf("CUDA Error! %s, line=%d\n",cudaGetErrorString(cuda_rtn_err), __LINE__ );
        } 
        cuda_rtn_err = cudaMemcpy(d_LL_reg, i_regs, ((points+1)<<4)*sizeof(long long), cudaMemcpyHostToDevice);
        if ( cudaSuccess != cuda_rtn_err ) {
             printf("CUDA Error! %s, line=%d\n",cudaGetErrorString(cuda_rtn_err), __LINE__ );
        } 
            // allocate an array of ptrs, then allocate a by Bits array for each one
        my_d_bit_LL_ptrs = (long long**)calloc( NUM_BLOCKS, sizeof(long long *) ); // Bit LL_Reg, ordered by bit#, we allow 16 CS/bit entry
            // then allocate a by Bits array, one for each Blk & set the ptrs to them
        for (i=0; i<NUM_BLOCKS; i++) { // points
            cuda_rtn_err = cudaMalloc(&my_d_bit_LL_ptrs[i], ((points+1)<<4)*sizeof(long long) ); // Bit LL_Reg, ordered by bit#, we allow 16 CS/bit entry
            if ( cudaSuccess != cuda_rtn_err ) {
                printf("CUDA Error! %s, line=%d\n",cudaGetErrorString(cuda_rtn_err), __LINE__ );
            } 
            printf("my_d_bit_LL_ptrs[%d]= %d (x%p) \n", i, my_d_bit_LL_ptrs[i], my_d_bit_LL_ptrs[i] );
        }

---> XEQ ERROR

        cuda_rtn_err = cudaMalloc(my_d_bit_LL_regs, NUM_BLOCKS*sizeof(long long *) ); // Allocate a CUDA array of ptrs
        if ( cudaSuccess != cuda_rtn_err ) {
             printf("CUDA Error! %s, line=%d\n",cudaGetErrorString(cuda_rtn_err), __LINE__ );
        }               // copy the CPU ptr array to the CUDA array
        cuda_rtn_err = cudaMemcpy(my_d_bit_LL_regs, my_d_bit_LL_ptrs, NUM_BLOCKS*sizeof(long long *), cudaMemcpyHostToDevice);
        if ( cudaSuccess != cuda_rtn_err ) {
             printf("CUDA Error! %s, line=%d\n",cudaGetErrorString(cuda_rtn_err), __LINE__ );
        } 

I've substituted void for long long & get the same response.

Thanks in advance for any help, Alan

Upvotes: 0

Views: 423

Answers (1)

Robert Crovella
Robert Crovella

Reputation: 152173

Pay attention to the warnings the compiler is giving you.

This line:

cuda_rtn_err = cudaMalloc(my_d_bit_LL_regs, NUM_BLOCKS*sizeof(long long *) );

should be like this:

cuda_rtn_err = cudaMalloc(&my_d_bit_LL_regs, NUM_BLOCKS*sizeof(long long *) );

When I tried to compile your code, the compiler told me:

t306.cu(145): warning: variable "my_d_bit_LL_regs" is used before its value is set

Upvotes: 1

Related Questions