From 9d019d673fb19b9d7901ef084dc700a7da9e1db2 Mon Sep 17 00:00:00 2001 From: Anubhab Haldar Date: Mon, 20 Jun 2022 13:51:16 -0400 Subject: [PATCH 1/2] Attempt nonallocating nla kernels --- src/states.jl | 44 +++++++++++++++++--------------------------- 1 file changed, 17 insertions(+), 27 deletions(-) diff --git a/src/states.jl b/src/states.jl index 368f45db..655153b9 100644 --- a/src/states.jl +++ b/src/states.jl @@ -93,6 +93,12 @@ function nla(::NLADefault, x) return x end +function nla(nla_type, x_old) + x_new = similar(x_old) + nla!(nla_type, x_old, x_new) + return x_new +end + """ NLAT1() Applies the \$ \\text{T}_1 \$ transformation algorithm, as defined in [1] and [2]. @@ -107,15 +113,9 @@ Physical review letters 120.2 (2018): 024102. """ struct NLAT1 <: NonLinearAlgorithm end -function nla(::NLAT1, x_old) - x_new = copy(x_old) - for i in 1:size(x_new, 1) - if mod(i, 2)!=0 - x_new[i,:] = copy(x_old[i,:].*x_old[i,:]) - end - end - - return x_new +function nla!(::NLAT1, x_old, x_new) + x_new[2:2:end, :] = x_old[2:2:end, :] + x_new[1:2:end, :] = x_old[1:2:end, :].^2 end """ @@ -128,15 +128,10 @@ and RNN-LSTM._" (2019). """ struct NLAT2 <: NonLinearAlgorithm end -function nla(::NLAT2, x_old) - x_new = copy(x_old) - for i in 2:size(x_new, 1)-1 - if mod(i, 2)!=0 - x_new[i,:] = copy(x_old[i-1,:].*x_old[i-2,:]) - end - end - - return x_new +function nla!(::NLAT2, x_old, x_new) + x_new[1, :] = x_old[1, :] + x_new[2:2:end, :] = x_old[2:2:end, :] + x_new[3:2:end-1, :] = x_old[2:2:end-2, :].*x_old[1:2:end-3, :] end """ @@ -149,13 +144,8 @@ and RNN-LSTM._" (2019). """ struct NLAT3 <: NonLinearAlgorithm end -function nla(::NLAT3, x_old) - x_new = copy(x_old) - for i in 2:size(x_new, 1)-1 - if mod(i, 2)!=0 - x_new[i,:] = copy(x_old[i-1,:].*x_old[i+1,:]) - end - end - - return x_new +function nla!(::NLAT3, x_old, x_new) + x_new[1,:]= x_old[1, :] + x_new[2:2:end, :]= x_old[2:2:end, :] + x_new[3:2:end-1, :]= x_old[2:2:end-2, :].*x_old[4:2:end, :] end From 20bc3aee1312f8d69f04477a826f7e15030d4df3 Mon Sep 17 00:00:00 2001 From: Anubhab Haldar Date: Mon, 20 Jun 2022 16:03:44 -0400 Subject: [PATCH 2/2] Copy the last element in odd-sized arrays --- src/states.jl | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/states.jl b/src/states.jl index 655153b9..baf86cf4 100644 --- a/src/states.jl +++ b/src/states.jl @@ -93,7 +93,13 @@ function nla(::NLADefault, x) return x end -function nla(nla_type, x_old) +struct NLAT1 <: NonLinearAlgorithm end + +struct NLAT2 <: NonLinearAlgorithm end + +struct NLAT3 <: NonLinearAlgorithm end + +function nla(nla_type::Union{NLAT1, NLAT2, NLAT3}, x_old) x_new = similar(x_old) nla!(nla_type, x_old, x_new) return x_new @@ -111,7 +117,6 @@ ANN, and RNN-LSTM._" (2019). systems from data: A reservoir computing approach._" Physical review letters 120.2 (2018): 024102. """ -struct NLAT1 <: NonLinearAlgorithm end function nla!(::NLAT1, x_old, x_new) x_new[2:2:end, :] = x_old[2:2:end, :] @@ -126,10 +131,12 @@ Apply the \$ \\text{T}_2 \$ transformation algorithm, as defined in [1]. chaotic system using a hierarchy of deep learning methods: Reservoir computing, ANN, and RNN-LSTM._" (2019). """ -struct NLAT2 <: NonLinearAlgorithm end function nla!(::NLAT2, x_old, x_new) x_new[1, :] = x_old[1, :] + if mod(size(x_new, 1), 2) != 0 + x_new[end, :] = x_old[end, :] + end x_new[2:2:end, :] = x_old[2:2:end, :] x_new[3:2:end-1, :] = x_old[2:2:end-2, :].*x_old[1:2:end-3, :] end @@ -142,10 +149,12 @@ Apply the \$ \\text{T}_3 \$ transformation algorithm, as defined in [1]. chaotic system using a hierarchy of deep learning methods: Reservoir computing, ANN, and RNN-LSTM._" (2019). """ -struct NLAT3 <: NonLinearAlgorithm end function nla!(::NLAT3, x_old, x_new) - x_new[1,:]= x_old[1, :] + x_new[1, :] = x_old[1, :] + if mod(size(x_new, 1), 2) != 0 + x_new[end, :] = x_old[end, :] + end x_new[2:2:end, :]= x_old[2:2:end, :] x_new[3:2:end-1, :]= x_old[2:2:end-2, :].*x_old[4:2:end, :] end