How to measure the time taken by a part of the code excluding the disk operations

I am trying to write a code that deals with repeated deserialization of tensors followed by contracting them and storing the result in another tensor. Here is an example of how the code looks like :

    I_vvoo = zeros(Float64, nv, nv, no, no)
    g_oooo = deserialize("g_oooo.jlbin")
    INTpp_vvoo = deserialize("INTpp_vvoo.jlbin")
    @tensor I_vvoo[a_1, a_2, i_1, i_2] += 1 / 2 * g_oooo[i_3, i_4, i_1, i_2] * INTpp_vvoo[a_1, a_2, i_3, i_4]
    INTpp_vvoo = nothing
    g_oooo = nothing
    g_vvoo = deserialize("g_vvoo.jlbin")
    @tensor I_vvoo[a_1, a_2, i_1, i_2] += 1 / 2 * g_vvoo[a_1, a_2, i_1, i_2]
    g_vvoo = nothing
    g_vvvo = deserialize("g_vvvo.jlbin")
    T1_vo = deserialize("T1_vo.jlbin")
    @tensor I_vvoo[a_1, a_2, i_1, i_2] += g_vvvo[a_2, a_1, a_3, i_1] * T1_vo[a_3, i_2]
    T1_vo = nothing
    g_vvvo = nothing
    g_voov = deserialize("g_voov.jlbin")
    T2_vvoo = deserialize("T2_vvoo.jlbin")
    @tensor I_vvoo[a_1, a_2, i_1, i_2] += -1 * g_voov[a_2, i_3, i_2, a_3] * T2_vvoo[a_1, a_3, i_3, i_1]

This things will be done repeatedly in a loop. The problem is - timing the loop would measure the time of everything inside the loop. I only want to measure the time taken for the tensor contractions that are happening. Any ideas on how to achieve the same?

A possible solution could be using TimerOutputs.jl.

That is indeed correct. However, as far as I understand it, we have to label each section whose time consumed we want to monitor. Wouldn’t that require me to label every deserialize call with @timeit to "IO" ... ? It would become extremely tedious.

In principle yes you need to label every place. How else should Julia know which parts of the code you are interested in? I think this is just the amount of information the code needs to contain. I would recommend wrapping this in a helper function to save on some typing :slight_smile:

I understand. I was hoping for something that could detect the function call to deserialize and automatically measure the amount of time the cumulative deserialize takes.

just do something like:

const TIMER = TimerOutput()

function my_deserialize(file)
    @timeit TIMER "IO" deserialize(file)
end

and then search and replace all other calls to deserialize. Does not get more automatic than this I think.

1 Like