Converting - for loop to Cuarray dot syntax

Hello Everyone,

I need help to convert the below code to carry dot syntax

From

for i=1:j

a[i] = a[i] + a[i+j]

end

I converted it to a[1:j].=a[1:j].+a[1:j+j] , but it is not working

You are adding arrays of different lengths.

1 Like

I got it, but is there any way to implement this in Cuarray dot syntax?

You may be looking for a[1:j].=a[1:j].+a[(1:j).+j]. + takes precedence over :, so 1:j+j is equivalent to 1:(j+j) and therefore probably not what you meant.

1 Like

Your error is in 1:j+j. This evaluates to 1:(j+j). You can solve it by writing it as (1:j).+j.

1 Like

What do you mean by CuArray dot syntax?

It already works with CuArrays:

julia> n = 5

julia> a = CuArray(fill(1, 2n));

julia> a[1:n] .+= a[n+1:2n];

julia> a
10-element CuArray{Int64, 1}:
 2
 2
 2
 2
 2
 1
 1
 1
 1
 1
1 Like

Perfect, thanks it is working:)

Thanks it is also working:)