# Repeat CuArray (subtract 3 Vector from every row of nx3 matrix)

**URL:** <https://discourse.julialang.org/t/repeat-cuarray-subtract-3-vector-from-every-row-of-nx3-matrix/90291>\
**Category:** Performance\
**Tags:** cuda\
**Created:** [November 15, 2022, 1:48pm UTC](https://discourse.julialang.org/t/repeat-cuarray-subtract-3-vector-from-every-row-of-nx3-matrix/90291 "2022-11-15T13:48:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![LeN3rd](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/len3rd/32/34329_2.png) [@LeN3rd](https://discourse.julialang.org/u/LeN3rd)\
**Post date:** [November 15, 2022, 1:48pm UTC](https://discourse.julialang.org/t/repeat-cuarray-subtract-3-vector-from-every-row-of-nx3-matrix/90291/1 "2022-11-15T13:48:55Z")

</div>

How can i repeat a CuArray in a fast way? I have a 3 vector that is supposed to be subtracted from every row in a n x 3 matrix. The CPU code just transposes and repeats it, but this takes up to 3 seconds on GPU and takes up 1.7 GB. for a 100000x3 matrix while only taking 12 ms on cpu and allocating 22.9 mb.

```julia
offset = Array([0.0, 0.0, 0.0])
cuda_offset = CuArray([0.0, 0.0, 0.0])
sx = 1000000
@btime repeat(transpose(cuda_offset), outer = [sx,1]) # takes 3.7 s
@btime repeat(transpose(offset), outer = [sx,1]) # takes 12 ms

```

Indexing every row in a loop is even slower (20s on gpu and 94.127ms on cpu).

Is there any other way to subtract a vector form every row of a matrix in a fast way, without writing a new kernel (Be differentiable with Zygote)?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [November 15, 2022, 2:12pm UTC](https://discourse.julialang.org/t/repeat-cuarray-subtract-3-vector-from-every-row-of-nx3-matrix/90291/2 "2022-11-15T14:12:38Z")

</div>

`

> [@LeN3rd](#):
>
> Is there any other way to subtract a vector form every row of a matrix in a fast way, without writing a new kernel (Be differentiable with Zygote)?

Use broadcasting? e.g. `A .- [1 2 3]` subtracts `[1 2 3]` from each row of an m \times 3 matrix `A`.

---

<div class="post-metadata">

**Author:** ![LeN3rd](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/len3rd/32/34329_2.png) [@LeN3rd](https://discourse.julialang.org/u/LeN3rd)\
**Post date:** [November 15, 2022, 2:17pm UTC](https://discourse.julialang.org/t/repeat-cuarray-subtract-3-vector-from-every-row-of-nx3-matrix/90291/3 "2022-11-15T14:17:12Z")

</div>

Well ill be damned. Thanks, that solves my problem at this time. I think i need to read up a little more on that. How would it work if i would have it apply to the middle dimension of my mx3xn array A? What is the default behavior for the . operator, when the arrays don’t have the same dimensions? I assumed it was only for arrays of the same size.

Nvm found the docs:  
so Julia provides [`broadcast`](https://docs.julialang.org/en/v1/base/arrays/#Base.Broadcast.broadcast), which expands singleton dimensions in array arguments to match the corresponding dimension in the other array without using extra memory, and applies the given function elementwise:

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [November 15, 2022, 8:36pm UTC](https://discourse.julialang.org/t/repeat-cuarray-subtract-3-vector-from-every-row-of-nx3-matrix/90291/4 "2022-11-15T20:36:22Z")

</div>

```julia

A=rand(1:10,4,3,2)

A.-[1;; 2;;3] # 3-element array for dim 2 (2 ';') ;; between elements

A.-[1;;;2] # 2-element array for dim 3 (3';') ;;; between elements

```
