# cuLaunchKernel with variable size parameters

**URL:** <https://forums.developer.nvidia.com/t/culaunchkernel-with-variable-size-parameters/158691>\
**Category:** CUDA Programming and Performance\
**Created:** [November 3, 2020, 5:33pm UTC](https://forums.developer.nvidia.com/t/culaunchkernel-with-variable-size-parameters/158691 "2020-11-03T17:33:03Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![andrea.capirchio](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@andrea.capirchio](https://forums.developer.nvidia.com/u/andrea.capirchio)\
**Post date:** [November 3, 2020, 5:33pm UTC](https://forums.developer.nvidia.com/t/culaunchkernel-with-variable-size-parameters/158691/1 "2020-11-03T17:33:03Z")

</div>

Hello everyone,

I’m trying to develop a modular architecture on Jetson TX 2 (Jetpack 4.3) for running user pre-defined kernels, so I need my code to be the most generic as possible.  
I’m having issues in running a CUDA kernel by calling the “cuLaunchKernel” driver API function:

- I have different pre-compiled .cubin kernels
- all the kernels have a fixed number of parameters + a variable number of variable-size parameters

The kernels that do not require the variable part of the parameters are working fine.  
The kernels that require the variable part always return CUDA\_ERROR\_INVALID\_VALUE.

Here is a snippet of how the fixed-size parameters are assigned, and how the kernel is launched (using the “extra” parameter) as shown in the MatrixMulDrv.cpp sample.

```
*(reinterpret_cast<int *>(&argBuffer[offset])) = my_parameter;
 offset += sizeof(my_parameter);
//...
void *kernel_launch_config[5] = {
    CU_LAUNCH_PARAM_BUFFER_POINTER, static_cast<void*>(&argBuffer[0]),
    CU_LAUNCH_PARAM_BUFFER_SIZE, &offset,
    CU_LAUNCH_PARAM_END};
cuLaunchKernel(func, grid.x, grid.y, grid.z, block.x, block.y, block.z,
        sharedMemB, hStream, NULL, reinterpret_cast<void**>(kernel_launch_config));

```

Since this is not applicable for variable-size parameters, I’m using a std::vector of chars, which was resized to the total size of the user parameters, and copying it into the buffer:

```
copy(userparams.begin(), userparams.begin() + usersize, *(reinterpret_cast<char**>(&argBuffer[offset])));

```

But as explained this makes a sort of malformation of the parameters and the kernel launch fails.  
Why is this happening??  
Do you have any suggestion on how to implement this variable-size parameter launch?

Please help!  
Thanks,  
Andrea
