mandelbrot calculation and strange behavior in PICO

RP2040 based microcontroller boards running MicroPython.
Target audience: MicroPython users with an RP2040 boards.
This does not include conventional Linux-based Raspberry Pi boards.
Post Reply
shaoziyang
Posts: 353
Joined: Sun Apr 17, 2016 1:55 pm

mandelbrot calculation and strange behavior in PICO

Post by shaoziyang » Wed Feb 17, 2021 7:49 am

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-<){}++~~~~~~=========--
-------------------------==================~~~~~~~++}}{>.(|< ++~~~~==========----

shaoziyang
Posts: 353
Joined: Sun Apr 17, 2016 1:55 pm

Re: mandelbrot calculation and strange behavior in PICO

Post by shaoziyang » Wed Feb 17, 2021 7:53 am

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?

shaoziyang
Posts: 353
Joined: Sun Apr 17, 2016 1:55 pm

Re: mandelbrot calculation and strange behavior in PICO

Post by shaoziyang » Fri Feb 19, 2021 12:57 pm

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

User avatar
yeti
Posts: 9
Joined: Fri Jul 03, 2015 1:10 am

Re: mandelbrot calculation and strange behavior in PICO

Post by yeti » Fri Feb 19, 2021 3:55 pm

Can you add a garbage collection run before running the test?
I have a natural instinct for science. — D.J. Trump
"Don't we all wait for SOMETHING-ELSE-1.0?" — yeti
"Stay OmmmMMMmmmPtimistic!" — yeti

shaoziyang
Posts: 353
Joined: Sun Apr 17, 2016 1:55 pm

Re: mandelbrot calculation and strange behavior in PICO

Post by shaoziyang » Sat Feb 20, 2021 1:08 am

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.

User avatar
pythoncoder
Posts: 4935
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: mandelbrot calculation and strange behavior in PICO

Post by pythoncoder » Sat Feb 20, 2021 10:23 am

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.
Peter Hinch

Richardm
Posts: 4
Joined: Mon Feb 08, 2021 7:11 am

Re: mandelbrot calculation and strange behavior in PICO

Post by Richardm » Mon Feb 22, 2021 8:44 pm

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.

Post Reply