# Cuda Thrust Custom function

**URL:** <https://forums.developer.nvidia.com/t/cuda-thrust-custom-function/24387>\
**Category:** CUDA Programming and Performance\
**Created:** [September 20, 2011, 12:25pm UTC](https://forums.developer.nvidia.com/t/cuda-thrust-custom-function/24387 "2011-09-20T12:25:27Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![iassael](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/iassael/32/46850_2.png) [@iassael](https://forums.developer.nvidia.com/u/iassael)\
**Post date:** [September 20, 2011, 12:25pm UTC](https://forums.developer.nvidia.com/t/cuda-thrust-custom-function/24387/1 "2011-09-20T12:25:27Z")

</div>

How can I impliment this function in Thrust?

```auto
for (i=0;i<n;i++)

    if (i==pos)

        h1[i]=1/h1[i];

    else

        h1[i]=-h1[i]/value;

```

In CUDA I did it like:

```auto
__global__ void inverse_1(double* h1, double value, int pos, int N)

{

    int i = blockDim.x * blockIdx.x + threadIdx.x;

    if (i < N){

        if (i == pos)

            h1[i] = 1 / h1[i];

        else

            h1[i] = -h1[i] / value;

    }

}

```

Thanks!

---

<div class="post-metadata">

**Author:** ![MarkusM](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@MarkusM](https://forums.developer.nvidia.com/u/MarkusM)\
**Post date:** [September 21, 2011, 8:07am UTC](https://forums.developer.nvidia.com/t/cuda-thrust-custom-function/24387/2 "2011-09-21T08:07:05Z")

</div>

First you define a Functor for the transform, that includes pos and value and works with one h1-element and it’s index i as parameters.

```auto
struct InvertFunctor: thrust::binary_function<double, int, double> {

	int pos;

	double value;

	InvertFunctor(int pos_, double value_) :

		pos(pos_), value(value_) {

	}

	__host__  __device__

	double operator()(double h1, int i) {

		if (i == pos)

			return 1 / h1;

		else

			return -h1 / value;

	}

};

```

You use it with thrust’s binary transform on a device\_vector h1 for example. A counting\_iterator provides the indices.

```auto
thrust::transform(h1.begin(), h1.end(), thrust::make_counting_iterator(0), h1.begin(), InvertFunctor(pos, value));

```

---

<div class="post-metadata">

**Author:** ![iassael](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/iassael/32/46850_2.png) [@iassael](https://forums.developer.nvidia.com/u/iassael)\
**Post date:** [September 21, 2011, 8:13am UTC](https://forums.developer.nvidia.com/t/cuda-thrust-custom-function/24387/3 "2011-09-21T08:13:44Z")

</div>

> [@](#):
>
> First you define a Functor for the transform, that includes pos and value and works with one h1-element and it’s index i as parameters.
> 
> ```auto
> struct InvertFunctor: thrust::binary_function<double, int, double> {
> 
> int pos;
> 
> double value;
> 
> InvertFunctor(int pos_, double value_) :
> 
> pos(pos_), value(value_) {
> 
> }
> 
> __host__  __device__
> 
> double operator()(double h1, int i) {
> 
> if (i == pos)
> 
> return 1 / h1;
> 
> else
> 
> return -h1 / value;
> 
> }
> 
> };
> 
> ```
> 
> You use it with thrust’s binary transform on a device\_vector h1 for example. A counting\_iterator provides the indices.
> 
> ```auto
> thrust::transform(h1.begin(), h1.end(), thrust::make_counting_iterator(0), h1.begin(), InvertFunctor(pos, value));
> 
> ```

thank you very much!!! One last thing if I wanted to pass another vector to copy values such as this?

for (i=0;i\<n;i++)

```
    for (j=0;j<n;j++)

        y[i*n+j]=h1[i]*a1[pos*n+j];

```

Its still not clear to me how to pass values to the function created Thanks!

---

<div class="post-metadata">

**Author:** ![MarkusM](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@MarkusM](https://forums.developer.nvidia.com/u/MarkusM)\
**Post date:** [September 21, 2011, 12:48pm UTC](https://forums.developer.nvidia.com/t/cuda-thrust-custom-function/24387/4 "2011-09-21T12:48:31Z")

</div>

Once you want to input/output even more vectors it gets complicated. The [zip\_iterator](http://code.google.com/p/thrust/wiki/QuickStartGuide#zip_iterator) is what you need. (Especially note the two examples linked in that section.)  
However you might want to ponder first whether it’s really worth the effort at this level of complexity. A handwritten kernel will probably be much easier and possibly faster. You can even use it with your thrust vectors by [unwrapping their pointers](http://code.google.com/p/thrust/source/browse/examples/unwrap_pointer.cu).

---

<div class="post-metadata">

**Author:** ![iassael](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/iassael/32/46850_2.png) [@iassael](https://forums.developer.nvidia.com/u/iassael)\
**Post date:** [September 21, 2011, 5:33pm UTC](https://forums.developer.nvidia.com/t/cuda-thrust-custom-function/24387/5 "2011-09-21T17:33:20Z")

</div>

> [@](#):
>
> Once you want to input/output even more vectors it gets complicated. The zip\_iterator is what you need. (Especially note the two examples linked in that section.)
> 
> However you might want to ponder first whether it’s really worth the effort at this level of complexity. A handwritten kernel will probably be much easier and possibly faster. You can even use it with your thrust vectors by unwrapping their pointers.

I have it in a Cuda kernel however I want to make my program use Thrust.

Yes but the arrays have different lengths

---

<div class="post-metadata">

**Author:** ![MarkusM](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@MarkusM](https://forums.developer.nvidia.com/u/MarkusM)\
**Post date:** [September 22, 2011, 10:29am UTC](https://forums.developer.nvidia.com/t/cuda-thrust-custom-function/24387/6 "2011-09-22T10:29:40Z")

</div>

Okay, if you insist. I’d say this version with permutation\_iterators should be clearest. Note: idx = i \* n + j.

First a Functor to calculate h1’s indices i = idx/n.

```auto
struct Indexh1Functor: thrust::unary_function<int, int> {

	int n;

	Indexh1Functor(int n_) :

		n(n_) {

	}

	__host__  __device__

	int operator()(int idx) {

		return idx / n;

	}

};

```

Second a Functor to calculate a1’s indices pos \* n + j with j = idx%n.

```auto
struct Indexa1Functor: thrust::unary_function<int, int> {

	int n;

	int pos;

	Indexa1Functor(int n_, int pos_) :

		n(n_), pos(pos_) {

	}

	__host__  __device__

	int operator()(int idx) {

		return pos * n + idx % n;

	}

};

```

But the call is probably quite obscure.

```auto
thrust::transform(

		thrust::make_permutation_iterator(

				h1.begin(),

				thrust::make_transform_iterator(

						thrust::make_counting_iterator(0),

						Indexh1Functor(n))),

		thrust::make_permutation_iterator(

				h1.begin(),

				thrust::make_transform_iterator(

						thrust::make_counting_iterator(0),

						Indexh1Functor(n))) + n * n,

		thrust::make_permutation_iterator(

				a1.begin(),

				thrust::make_transform_iterator(

						thrust::make_counting_iterator(0),

						Indexa1Functor(n, pos))),

		y.begin(),

		thrust::multiplies<double>());

```

The permutation\_iterators choose the elements from h1 and a1 by the indices we calculate with the Functors. The multiplication can then simply be done with thrust’s multiplies and outputted directly to y.

---

<div class="post-metadata">

**Author:** ![iassael](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/iassael/32/46850_2.png) [@iassael](https://forums.developer.nvidia.com/u/iassael)\
**Post date:** [September 25, 2011, 11:09am UTC](https://forums.developer.nvidia.com/t/cuda-thrust-custom-function/24387/7 "2011-09-25T11:09:14Z")

</div>

Thanks mate!  
I m giving it a try right now [External Image](http://forums.nvidia.com/public/style_emoticons/<#EMO_DIR#>/wink.gif "Image hosted on another site. Click to open in a new tab.")
