# Mapslices very slow

**URL:** https://discourse.julialang.org/t/mapslices-very-slow/86826
**Category:** GPU
**Created:** [September 6, 2022, 2:12am UTC](https://discourse.julialang.org/t/mapslices-very-slow/86826 "2022-09-06T02:12:50Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Lincoln\_Hannah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lincoln_hannah/32/19198_2.png) [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)
#### Post date: [September 6, 2022, 2:12am UTC](https://discourse.julialang.org/t/mapslices-very-slow/86826/1 "2022-09-06T02:12:50Z")

</div>

new to CUDA

`mapsilces` seems to run very slow.  
`reduce` is very fast but requires an accumulator (e.g. +) instead of a function (e.g. sum).

I’d like to apply the `linear_interpolation` function to slices of a matrix (or tensor).  
Can a GPU do this faster than CPU?

Would I have to re-write the `linear_interpolation` function as a custom Kernel?  
I’d rather not do this

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [September 6, 2022, 7:13am UTC](https://discourse.julialang.org/t/mapslices-very-slow/86826/2 "2022-09-06T07:13:29Z")

</div>

`mapslices` is known to be slow, at least on CPU

> <https://github.com/JuliaLang/julia/issues/26868>
>
> Consider the following piece of code:
> \`\`\`
> n=10^6;
> X = randn(1,n);
> V(x) = 0.5… \* dot(x,x);
> f(V, X) = \[V((@view X\[:,n\])) for n = 1:size(X,2) \]
> \`\`\`
> Timing on this reveals:
> \`\`\`
> julia\> @time mapslices(V, X, 1);
> 1.177690 seconds (12.00 M allocations: 267.024 MiB, 6.91% gc time)
> julia\> @time f(V,X);
> 0.047745 seconds (1.00 M allocations: 53.406 MiB, 5.58% gc time)
> \`\`\`
> which is substantially different.  
> 
> Note that in this simple example, my data could be a column vector; however, this is a surrogate for problems where I have time series data and \`\`X\`\` is d x n with 1\< d \<\< n.

---

<div class="post-metadata">

### Author: ![maleadt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maleadt/32/10097_2.png) [@maleadt](https://discourse.julialang.org/u/maleadt)
#### Post date: [September 8, 2022, 6:18am UTC](https://discourse.julialang.org/t/mapslices-very-slow/86826/3 "2022-09-08T06:18:25Z")

</div>

`*slices` (mapslices, eachslice) functions are not supported, because they take a function that transforms a slice and as such cannot be compiled into a single kernel. Instead, we need to call the function N times (for every slice) which results in many small kernels being launched.

---

<div class="post-metadata">

### Author: ![Lincoln\_Hannah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lincoln_hannah/32/19198_2.png) [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)
#### Post date: [September 13, 2022, 11:56am UTC](https://discourse.julialang.org/t/mapslices-very-slow/86826/4 "2022-09-13T11:56:53Z")

</div>

many thanks:)
