Loop count parfor

57 views
Skip to first unread message

vali...@gmail.com

unread,
Apr 4, 2015, 3:50:32 PM4/4/15
to qu...@googlegroups.com
Hi,

I have a simple question (since I am not skilled with python), is there a simple way to implement a loop count when I use parfor?

Thanks!

Pol

Morgan Hedges

unread,
Apr 4, 2015, 5:51:29 PM4/4/15
to qu...@googlegroups.com, vali...@gmail.com
Hi Pol,

do you mean that you want to keep track of how many iterations of your loop have been completed?

If that's what you mean, then the simplest thing I usually do is just make the called function print out a '*' or a relevant number

e.g.

import sys
import qutip as q
import time
def f(a):
    #do something with a
    time.sleep(1)
    print "*",
    sys.stdout.flush()

q.parfor(f, arange(10))


Then I get *'s appearing as each function call finishes.
Of course there's fancier things one can do too.

Is that what you meant?

Morgan

vali...@gmail.com

unread,
Apr 4, 2015, 9:18:25 PM4/4/15
to qu...@googlegroups.com, vali...@gmail.com
Hi Morgan,

Thanks for helping out! What you describe is what I meant, except I would like a counter. Usually on a for loop I can put something like i = i +1 and then print(i) at the end of the loop. But for some reason that doesn't work with parfor.
Any ideas?

Best,

Pol

Paul Nation

unread,
Apr 4, 2015, 9:37:53 PM4/4/15
to qu...@googlegroups.com
Pass an extra arg and set it with arange(N) where N is the the number of loops. Then print at the end of the func that you passed. 

Paul

--
You received this message because you are subscribed to the Google Groups "QuTiP: Quantum Toolbox in Python" group.
To unsubscribe from this group and stop receiving emails from it, send an email to qutip+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Pol Forn Diaz

unread,
Apr 6, 2015, 11:00:40 AM4/6/15
to qu...@googlegroups.com
Hi Paul,

Thanks for your advice!

In the end I did the following:

def task(args):

    m, omega = args

    count = arange(m)
    print("Step # " + str(count[-1:]) + "/" str(len(omega_list)))

    ...

Although the output is something like "Step # [45]/100", I would like to get rid of the [] but I can live with that.


Best,

Pol
You received this message because you are subscribed to a topic in the Google Groups "QuTiP: Quantum Toolbox in Python" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/qutip/CWhNbWS87O8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to qutip+un...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages