# How to convert this code to GPU

**URL:** <https://forums.developer.nvidia.com/t/how-to-convert-this-code-to-gpu/47179>\
**Category:** CUDA Programming and Performance\
**Created:** [January 9, 2017, 11:09am UTC](https://forums.developer.nvidia.com/t/how-to-convert-this-code-to-gpu/47179 "2017-01-09T11:09:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![CharlieCheng](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@CharlieCheng](https://forums.developer.nvidia.com/u/CharlieCheng)\
**Post date:** [January 9, 2017, 11:09am UTC](https://forums.developer.nvidia.com/t/how-to-convert-this-code-to-gpu/47179/1 "2017-01-09T11:09:43Z")

</div>

I have some c code like this:

```auto
int m=0;
for (int i=0; i < num_loop; i++) 
{
   if (condition)
   	addr[m++] = i;
}

```

how can I parallelize it using a kernal for the “m” is not the same as “i”

---

<div class="post-metadata">

**Author:** ![tera](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@tera](https://forums.developer.nvidia.com/u/tera)\
**Post date:** [January 9, 2017, 11:19am UTC](https://forums.developer.nvidia.com/t/how-to-convert-this-code-to-gpu/47179/2 "2017-01-09T11:19:33Z")

</div>

This operation is called “stream compaction”. [Thrust](https://thrust.github.io/doc/group __stream__ compaction.html) e.g. implements it in it’s [copy\_if](https://thrust.github.io/doc/group __stream__ compaction.html) methods.  
The main operation needed to implement it is called a “prefix sum”. Google finds several good texts on both.

---

<div class="post-metadata">

**Author:** ![CharlieCheng](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@CharlieCheng](https://forums.developer.nvidia.com/u/CharlieCheng)\
**Post date:** [January 9, 2017, 12:38pm UTC](https://forums.developer.nvidia.com/t/how-to-convert-this-code-to-gpu/47179/3 "2017-01-09T12:38:33Z")

</div>

Thx, your answer really helps me a lot.

---

<div class="post-metadata">

**Author:** ![CharlieCheng](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@CharlieCheng](https://forums.developer.nvidia.com/u/CharlieCheng)\
**Post date:** [January 10, 2017, 1:59am UTC](https://forums.developer.nvidia.com/t/how-to-convert-this-code-to-gpu/47179/4 "2017-01-10T01:59:40Z")

</div>

> [@](#):
>
> This operation is called “stream compaction”. Thrust e.g. implements it in it’s copy\_if methods.  
> The main operation needed to implement it is called a “prefix sum”. Google finds several good texts on both.

Thank you again for your answer. I try to parallelize the method today but one problem occurs when the “if condition” is a function of i and m.

```auto
int m=0;
for (int i=0; i < num_loop; i++) 
{
   if (func(i, m))
   	addr[m++] = i;
}

```

how can I implement with “prefix sum”?

---

<div class="post-metadata">

**Author:** ![tera](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@tera](https://forums.developer.nvidia.com/u/tera)\
**Post date:** [January 10, 2017, 3:23am UTC](https://forums.developer.nvidia.com/t/how-to-convert-this-code-to-gpu/47179/5 "2017-01-10T03:23:37Z")

</div>

That would need some deeper thought but my immediate intuitive reply would be that this seems impossible to do in a work-efficient way in the general case.

Can you be more specific about func(i, m)?

---

<div class="post-metadata">

**Author:** ![CharlieCheng](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@CharlieCheng](https://forums.developer.nvidia.com/u/CharlieCheng)\
**Post date:** [January 10, 2017, 6:15am UTC](https://forums.developer.nvidia.com/t/how-to-convert-this-code-to-gpu/47179/6 "2017-01-10T06:15:20Z")

</div>

Hello tera:  
I have the Code shows as below:

```auto
int input[INPUT_SIZE] = [0 , 0 , 1, 2, 2, 3, 3, 3, 4, 5, 5, 6, 6, 7, 8,...];
int m = 1, Sm = 0;
for (int i =0; i < num_loop; i++)
{   
    m = 1;
    while (Sm + m < num_loop && input[Sm] == input[Sm + m]) 
        m++;
    Sm += m;
    output[i] = Sm;
}

```

The input is a sequence of elements, every one of them repeates n times, n \> 1 and n \< INPUT\_SIZE.
