Speed improvement [🇷 for BE/BA]

posted by PharmCat  – Russia, 2020-08-08 19:27 (1719 d 13:47 ago) – Posting: # 21840
Views: 21,647

❝ 30%...It ain't much but it's honest work. :-)


It is very good boost. I donn't know how matrix multipication works in R, but some improovments can be done if make function A = B'*C*B or A += B'*C*B. At each step of this function there are many intermediate products that can be avoided with direct computation. (Or make it in C)

Example incremantal function:

"""
A' * B * A -> +θ (cache)
A' * B * C -> +β
"""
function mulθβinc!(θ, β, A::AbstractMatrix, B::AbstractMatrix, C::Vector)
    q = size(B, 1)
    p = size(A, 2)
    c = zeros(eltype(B), q)
    for i = 1:p
        c .= 0
        for n = 1:q
            for m = 1:q
                @inbounds c[n] += B[m, n] * A[m, i]
            end
            @inbounds β[i] += c[n] * C[n]
        end
        for n = 1:p
            for m = 1:q
                @inbounds θ[i, n] += A[m, n] * c[m]
            end
        end
    end
    θ, β
end

function mulsimple!(θ, β, A::AbstractMatrix, B::AbstractMatrix, C::Vector)
    AB = A'*B
    θ .+ AB*A, β .+ AB*C
end

θ = zeros(4, 4)
β = zeros(4)

A = [1 3 4 3; 2 3 2 3; 3 4 5 6; 3 4 5 6] B = [1 3 4 3; 2 3 2 3; 3 4 5 6; 3 4 5 6] C = [1, 3, 4 ,3]


julia> @time mulsimple!(θ, β, A, B, C);
0.000007 seconds (10 allocations: 1.172 KiB)

julia> @time mulθβinc!(θ, β, A, B, C);
0.000004 seconds (2 allocations: 144 bytes)

eightfold memory savings

Complete thread:

UA Flag
Activity
 Admin contact
23,424 posts in 4,927 threads, 1,668 registered users;
24 visitors (0 registered, 24 guests [including 1 identified bots]).
Forum time: 09:14 CEST (Europe/Vienna)

Most scientists today are devoid of ideas, full of fear, intent on
producing some paltry result so that they can add to the flood
of inane papers that now constitutes “scientific progress”
in many areas.    Paul Feyerabend

The Bioequivalence and Bioavailability Forum is hosted by
BEBAC Ing. Helmut Schütz
HTML5