# Max() for massive arrays?

**URL:** <https://forums.developer.nvidia.com/t/max-for-massive-arrays/9918>\
**Category:** CUDA Programming and Performance\
**Created:** [June 4, 2009, 1:59pm UTC](https://forums.developer.nvidia.com/t/max-for-massive-arrays/9918 "2009-06-04T13:59:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![madmaze](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@madmaze](https://forums.developer.nvidia.com/u/madmaze)\
**Post date:** [June 4, 2009, 1:59pm UTC](https://forums.developer.nvidia.com/t/max-for-massive-arrays/9918/1 "2009-06-04T13:59:30Z")

</div>

Hello everyone,

I have an array of 5mil+ elements and I need to find the largest value.

at the moment I run a loop for every 256elements and always return the max of every 256 elements… then i do it over until i get the final one…

anyone have something better… maybe not including loops =P

any tips or pointers welcome

thanks

---

<div class="post-metadata">

**Author:** ![cbuchner1](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/cbuchner1/32/14168_2.png) [@cbuchner1](https://forums.developer.nvidia.com/u/cbuchner1)\
**Post date:** [June 4, 2009, 2:17pm UTC](https://forums.developer.nvidia.com/t/max-for-massive-arrays/9918/2 "2009-06-04T14:17:34Z")

</div>

> [@](#):
>
> I have an array of 5mil+ elements and I need to find the largest value.

Just modify the CUDA reduction sample to compute the max instead of the sum.

UPDATE: you can also make use of the open sourced Thrust template library, it should have operators to do this - you don’t even have to program a CUDA kernel yourself.

---

<div class="post-metadata">

**Author:** ![madmaze](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@madmaze](https://forums.developer.nvidia.com/u/madmaze)\
**Post date:** [June 4, 2009, 2:41pm UTC](https://forums.developer.nvidia.com/t/max-for-massive-arrays/9918/3 "2009-06-04T14:41:08Z")

</div>

> [@](#):
>
> Just modify the CUDA reduction sample to compute the max instead of the sum.
> 
> UPDATE: you can also make use of the open sourced Thrust template library, it should have operators to do this - you don’t even have to program a CUDA kernel yourself.

Hmm i was thinking about doing it with one of the reduction kernels…

Ill check out the Thrust libs… I guess that would be my chance to upgrade to cuda 2.2 aswell

---

<div class="post-metadata">

**Author:** ![JaredHoberock](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/jaredhoberock/32/166135_2.png) [@JaredHoberock](https://forums.developer.nvidia.com/u/JaredHoberock)\
**Post date:** [June 4, 2009, 3:29pm UTC](https://forums.developer.nvidia.com/t/max-for-massive-arrays/9918/4 "2009-06-04T15:29:14Z")

</div>

> [@](#):
>
> Hmm i was thinking about doing it with one of the reduction kernels…
> 
> Ill check out the Thrust libs… I guess that would be my chance to upgrade to cuda 2.2 aswell

thrust::max\_element() should do what you want.
