# Allocating single value for kernel argument

**URL:** <https://forums.developer.nvidia.com/t/allocating-single-value-for-kernel-argument/49588>\
**Category:** CUDA Programming and Performance\
**Created:** [April 27, 2017, 3:12pm UTC](https://forums.developer.nvidia.com/t/allocating-single-value-for-kernel-argument/49588 "2017-04-27T15:12:25Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![bha4395](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@bha4395](https://forums.developer.nvidia.com/u/bha4395)\
**Post date:** [April 27, 2017, 3:12pm UTC](https://forums.developer.nvidia.com/t/allocating-single-value-for-kernel-argument/49588/1 "2017-04-27T15:12:25Z")

</div>

I have a question regarding passing a single value to a kernel.

For example, assume I pass in an array and I want to add a value to every single index in that array but that value varies for each array (but is the same across the array) and I want to pass it in as an argument.

I’m pretty sure I know how to do the passing portion and setting it up as an argument, but my main question is how do you allocate the value.

```auto
float tmp;
cudaMallocHost(???, sizeof(float));

```

It is something like this but I’m not exactly sure. I’m assuming this is due to the fact that cudaMallocHost is looking for a void pointer. Therefore, if I want to pass a single value, should I just set it up as a pointer with the size of only 1 float?

---

<div class="post-metadata">

**Author:** ![Robert\_Crovella](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/robert_crovella/32/14043_2.png) [@Robert\_Crovella](https://forums.developer.nvidia.com/u/Robert_Crovella)\
**Post date:** [April 27, 2017, 3:21pm UTC](https://forums.developer.nvidia.com/t/allocating-single-value-for-kernel-argument/49588/2 "2017-04-27T15:21:30Z")

</div>

> [@](#):
>
> Therefore, if I want to pass a single value, should I just set it up as a pointer with the size of only 1 float?

Yes, if you want to pass the argument by pointer.

```auto
__global__ void kernel(float *tmp,...);

...
float *tmp;
cudaMallocHost(&tmp, sizeof(float));
...
kernel<<<...>>>(tmp, ...);

```

Or you could pass it as a kernel argument by value.

```auto
__global__ void kernel(float tmp,...);

float tmp;
...
kernel<<<...>>>(tmp, ...);

```

---

<div class="post-metadata">

**Author:** ![bha4395](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@bha4395](https://forums.developer.nvidia.com/u/bha4395)\
**Post date:** [April 27, 2017, 4:13pm UTC](https://forums.developer.nvidia.com/t/allocating-single-value-for-kernel-argument/49588/3 "2017-04-27T16:13:12Z")

</div>

Alright thanks txbob. That helps.

Another question, if I do pass it by value and it is called by say 1000 threads, I won’t run into any bank conflicts would I? The threads would only be reading the value.

Another question would be, would it be better to put this into shared memory, or into constant memory. Obviously, this is assuming I have more than 1 block.

---

<div class="post-metadata">

**Author:** ![Robert\_Crovella](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/robert_crovella/32/14043_2.png) [@Robert\_Crovella](https://forums.developer.nvidia.com/u/Robert_Crovella)\
**Post date:** [April 27, 2017, 4:29pm UTC](https://forums.developer.nvidia.com/t/allocating-single-value-for-kernel-argument/49588/4 "2017-04-27T16:29:21Z")

</div>

kernel parameters already end up in constant memory. This is covered in the programming guide.  
to a first order approximation, this is a good place for it, and I wouldn’t worry about moving it to shared memory

bank conflicts only apply to shared memory

---

<div class="post-metadata">

**Author:** ![bha4395](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@bha4395](https://forums.developer.nvidia.com/u/bha4395)\
**Post date:** [April 27, 2017, 4:32pm UTC](https://forums.developer.nvidia.com/t/allocating-single-value-for-kernel-argument/49588/5 "2017-04-27T16:32:34Z")

</div>

I knew that about kernel parameters. Maybe I should have clarified whether to go to shared memory or stay in constant/global memory. My apologies.

---

<div class="post-metadata">

**Author:** ![MutantJohn](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@MutantJohn](https://forums.developer.nvidia.com/u/MutantJohn)\
**Post date:** [April 27, 2017, 4:33pm UTC](https://forums.developer.nvidia.com/t/allocating-single-value-for-kernel-argument/49588/6 "2017-04-27T16:33:30Z")

</div>

> [@](#):
>
> For example, assume I pass in an array and I want to add a value to every single index in that array but that value varies for each array (but is the same across the array) and I want to pass it in as an argument.

Luckily, Thrust has your back too.

```auto
#include <thrust/device_vector.h>

namespace T = thrust;

int main(void)
{
  size_t const size = 1337;
  int const val = -1;

  T::device_vector<int> device_vals{size, val};

  int const* device_vals_data = device_vals.data().get();
  return 0;
}

```

---

<div class="post-metadata">

**Author:** ![Robert\_Crovella](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/robert_crovella/32/14043_2.png) [@Robert\_Crovella](https://forums.developer.nvidia.com/u/Robert_Crovella)\
**Post date:** [April 27, 2017, 4:39pm UTC](https://forums.developer.nvidia.com/t/allocating-single-value-for-kernel-argument/49588/7 "2017-04-27T16:39:34Z")

</div>

That doesn’t add val to an existing array.

---

<div class="post-metadata">

**Author:** ![bha4395](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@bha4395](https://forums.developer.nvidia.com/u/bha4395)\
**Post date:** [April 27, 2017, 4:56pm UTC](https://forums.developer.nvidia.com/t/allocating-single-value-for-kernel-argument/49588/8 "2017-04-27T16:56:50Z")

</div>

Yeah I pretty sure that just assigns -1 to every value in the array.

And I know I said this earlier, but adding a number to every value in the array is not the actual task I’m looking to perform. I was just using that as an example as it describes the general objective that I’m trying to do, which was pass a value as an argument into a kernel by reference.

---

<div class="post-metadata">

**Author:** ![MutantJohn](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@MutantJohn](https://forums.developer.nvidia.com/u/MutantJohn)\
**Post date:** [April 27, 2017, 5:11pm UTC](https://forums.developer.nvidia.com/t/allocating-single-value-for-kernel-argument/49588/9 "2017-04-27T17:11:31Z")

</div>

> [@](#):
>
> That doesn’t add val to an existing array.

I assumed the OP simply wanted an allocated region with an initial value :P

Otherwise there’s `thrust::fill` and `thrust::transform`.

> [@](#):
>
> And I know I said this earlier, but adding a number to every value in the array is not the actual task I’m looking to perform. I was just using that as an example as it describes the general objective that I’m trying to do, which was pass a value as an argument into a kernel by reference.

Ah, I see.

Then yeah, it’s easiest to use pointers. I kind of like to exploit single-element vectors as a RAII way of doing so.

I’m curious if Thrust has device equivalent of `std::unique_ptr`.

---

<div class="post-metadata">

**Author:** ![bha4395](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@bha4395](https://forums.developer.nvidia.com/u/bha4395)\
**Post date:** [April 27, 2017, 5:14pm UTC](https://forums.developer.nvidia.com/t/allocating-single-value-for-kernel-argument/49588/10 "2017-04-27T17:14:53Z")

</div>

Yeah no problem.

I would doubt it, I haven’t done much with thrust but I thought the only data structures you had where a host vector and a device vector.

---

<div class="post-metadata">

**Author:** ![MutantJohn](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@MutantJohn](https://forums.developer.nvidia.com/u/MutantJohn)\
**Post date:** [April 27, 2017, 5:29pm UTC](https://forums.developer.nvidia.com/t/allocating-single-value-for-kernel-argument/49588/11 "2017-04-27T17:29:49Z")

</div>

To be fair, what other data structures would you need in CUDA anyway? :P

A `thrust::unique_device_ptr` would be nice for situations like these. It’d also be a good time for Thrust to work on introducing move semantics into the library.

---

<div class="post-metadata">

**Author:** ![bha4395](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@bha4395](https://forums.developer.nvidia.com/u/bha4395)\
**Post date:** [April 27, 2017, 5:32pm UTC](https://forums.developer.nvidia.com/t/allocating-single-value-for-kernel-argument/49588/12 "2017-04-27T17:32:34Z")

</div>

I mean you don’t need any other tbh. An a unique\_device\_ptr is really just a vector with one index.
