# Row-wise operation using \`map\`?

**URL:** <https://discourse.julialang.org/t/row-wise-operation-using-map/80339>\
**Category:** GPU\
**Tags:** question\
**Created:** [May 1, 2022, 5:58pm UTC](https://discourse.julialang.org/t/row-wise-operation-using-map/80339 "2022-05-01T17:58:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![HJW019](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hjw019/32/22102_2.png) [@HJW019](https://discourse.julialang.org/u/HJW019)\
**Post date:** [May 1, 2022, 5:58pm UTC](https://discourse.julialang.org/t/row-wise-operation-using-map/80339/1 "2022-05-01T17:58:29Z")

</div>

I am translating my CPU code to GPU. One of the things I did in CPU is to apply `map` to a `Vector{Matrix{Real}}` data for row-wise operations. For instance:

```julia
julia> aa = [[1. 2. 3.],
             [4. 5. 6.]]
2-element Vector{Matrix{Float64}}:
 [1.0 2.0 3.0]
 [4.0 5.0 6.0]

julia> map(x->sum(x), aa)
2-element Vector{Float64}:
  6.0
 15.0

```

. However, Cuda does not take `Vector{Matrix{..}}` type of arrays. I am wondering whether there are ways to work around the issue and allow me to use `map` for row-wise operations.

I know I can use `sum(aa, dims=2)` in Cuda instead of `map` to get the same result, but the `sum` in the above MWE is really just an example for illustrative purposes. In my real work, it’s more like `map(x -> myfun(x), aa)`, where `myfun()` takes in a vector and returns a scalar, and I want to apply it to every row of `aa`.

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)\
**Post date:** [May 1, 2022, 8:20pm UTC](https://discourse.julialang.org/t/row-wise-operation-using-map/80339/2 "2022-05-01T20:20:42Z")

</div>

How about `mapslices`

```julia
julia> aa = [1. 2. 3.; 4. 5. 6.]
2×3 Matrix{Float64}:
 1.0 2.0 3.0
 4.0 5.0 6.0

julia> mapslices(sum, aa; dims=2)
2×1 Matrix{Float64}:
  6.0
 15.0

```

---

<div class="post-metadata">

**Author:** ![HJW019](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hjw019/32/22102_2.png) [@HJW019](https://discourse.julialang.org/u/HJW019)\
**Post date:** [May 2, 2022, 8:04am UTC](https://discourse.julialang.org/t/row-wise-operation-using-map/80339/3 "2022-05-02T08:04:06Z")

</div>

It looks to be the answer. Thanks a lot!
