Page 1 of 1

mandelbrot calculation and strange behavior in PICO

Posted: Wed Feb 17, 2021 7:49 am
by shaoziyang
I have modify [*]examples/mandel.py[*] for performance testing.

Code: Select all

from time import ticks_ms, ticks_diff

MAX_ITER = 60
MANDEL_CHAR = (
    ' ', '.', '`', ',', ':', ';', '|', 'o', '<', '>',
    '(', ')', '{', '}', '+', '~', '=', '-', '#', '@'
)

def run(func, param = None):
    t1 = ticks_ms()
    if param == None:
        func()
    else:
        func(param)
    t2 = ticks_ms()
    print('calc time:', ticks_diff(t2, t1), 'ms')


# @micropython.native
def calc_mandel(c):
    z = 0
    for i in range(MAX_ITER):
        z = z * z + c
        if abs(z) > 4:
            return i
    return MAX_ITER - 1

def mandelbrot():
    N = len(MANDEL_CHAR)
    print('')
    for v in range(31):
        for u in range(81):
            n = calc_mandel((u / 30 - 2) + (v / 15 - 1) * 1j)
            print(MANDEL_CHAR[(MAX_ITER - n - 1)%N], end='')
        print()

run(mandelbrot)
Running results

Code: Select all

-------------------------==================~~~~~~~++}}{>.(|< ++~~~~==========----
----------------------==================~~~~~~~~+++}}{)(o-<){}++~~~~~~=========--
-------------------==================~~~~~~~~++++}}{>oo:  ,>>(}+++~~~~~~========-
----------------=================~~~~~~~~++++++}}}{)<       .>{}}++++~~~~~=======
-------------================~~~~~~~~~~+++}}}{{{{))(o~      :(){{}}}+++++~~~=====
----------===============~~~~~~~~~~~++++})-@><((o>o :`=    .:|;< >){{{)`}+~~~====
-------==============~~~~~~~~~~~~+++++}}{)o.  ~,+                ,<<@:; :}+~~~===
-----=============~~~~~~~~~+++++++++}}}{)(>:                          `<{}+~~~~==
--=============~~~~~+++++++++++}}}}}}{)#:@`                          #o({}++~~~==
-===========~~~~~~++}{<({{}{{){{{{{{{)(<:;                             o>>)++~~==
==========~~~~~~~+++}{>o<<>(>`o((())((<=                                 #)++~~~=
========~~~~~~~+++}}}{)>|>  ;- ==~.<<<:-                                |(}++~~~=
=====~~~~~~~++++}}{{{(<| ;          ::{                                 <:}++~~~=
==~~~~~++++++}}}{)|>><;-             -                                  ~{}+~~~~=
~~~+++})}}}}{{{))>o=);-                                                >{}++~~~~=
                                                                    -o()}}++~~~~=
~~~+++})}}}}{{{))>o=);-                                                >{}++~~~~=
==~~~~~++++++}}}{)|>><;-             -                                  ~{}+~~~~=
=====~~~~~~~++++}}{{{(<| ;          ::{                                 <:}++~~~=
========~~~~~~~+++}}}{)>|>  ;- ==~.<<<:-                                |(}++~~~=
==========~~~~~~~+++}{>o<<>(>`o((())((<=                                 #)++~~~=
-===========~~~~~~++}{<({{}{{){{{{{{{)(<:;                             o>>)++~~==
--=============~~~~~+++++++++++}}}}}}{)#:@`                          #o({}++~~~==
-----=============~~~~~~~~~+++++++++}}}{)(>:                          `<{}+~~~~==
-------==============~~~~~~~~~~~~+++++}}{)o.  ~,+                ,<<@:; :}+~~~===
----------===============~~~~~~~~~~~++++})-@><((o>o :`=    .:|;< >){{{)`}+~~~====
-------------================~~~~~~~~~~+++}}}{{{{))(o~      :(){{}}}+++++~~~=====
----------------=================~~~~~~~~++++++}}}{)<       .>{}}++++~~~~~=======
-------------------==================~~~~~~~~++++}}{>oo:  ,>>(}+++~~~~~~========-
----------------------==================~~~~~~~~+++}}{)(o-<){}++~~~~~~=========--
-------------------------==================~~~~~~~++}}{>.(|< ++~~~~==========----

Re: mandelbrot calculation and strange behavior in PICO

Posted: Wed Feb 17, 2021 7:53 am
by shaoziyang
It works in PICO, but I have found a strange behavior in PICO, the calculation time increases each time, tens to hundreds of milliseconds each time. There is no such phenomenon on other hardware (such as pyboard, esp32, microbit).

Is this a bug in PICO firmware?

Re: mandelbrot calculation and strange behavior in PICO

Posted: Fri Feb 19, 2021 12:57 pm
by shaoziyang
calc time: 3526 ms
calc time: 3573 ms
calc time: 3627 ms
calc time: 3666 ms
calc time: 3719 ms
calc time: 3758 ms
calc time: 3812 ms
calc time: 3852 ms

Re: mandelbrot calculation and strange behavior in PICO

Posted: Fri Feb 19, 2021 3:55 pm
by yeti
Can you add a garbage collection run before running the test?

Re: mandelbrot calculation and strange behavior in PICO

Posted: Sat Feb 20, 2021 1:08 am
by shaoziyang
I have try to add gc.collect() before calculation, it will decrease the increase time slightly.

It appears to be caused by the print function, this problem only appear on PICO.

Re: mandelbrot calculation and strange behavior in PICO

Posted: Sat Feb 20, 2021 10:23 am
by pythoncoder
shaoziyang wrote:
Sat Feb 20, 2021 1:08 am
...It appears to be caused by the print function, this problem only appear on PICO.
Interesting. There seem to be a number of issues around the Pico USB interface.

Re: mandelbrot calculation and strange behavior in PICO

Posted: Mon Feb 22, 2021 8:44 pm
by Richardm
Prime candidate for multi core. I tried and got 5 rows to run in parallel. Will retry when core1 support is a bit further along.

Re: mandelbrot calculation and strange behavior in PICO

Posted: Thu Apr 22, 2021 2:36 pm
by Wylekat
Interesting. There seem to be a number of issues around the Pico USB interface.
My USB problems seem to be getting certain things to work, like switches and LEDs past pin 15. Switches just don't work. Even when I just put the 2 jumper pins together.

Rest of the board runs fine. Also, this is repeatable on each of the boards I own. I got several to fiddle with.

Re: mandelbrot calculation and strange behavior in PICO

Posted: Fri Apr 23, 2021 5:58 am
by pythoncoder
The USB problems I experienced have been fixed in V1.15. Have you updated?