Parallel task speed weirdness

Haku

Senior Member
I finally have a use for the parallel task capabilites on a 20m2 but during coding I realised something odd was going on, so I did some simple parallel task tests.

With 6 LEDs wired into the 20m2 I ran this code:
Code:
start0:
 do
  for b0=0 to 255
  next b0
  toggle b.0
  toggle b.0
 loop

start1:
 do
  for b1=0 to 255
  next b1
  toggle b.1
  toggle b.1
 loop

start2:
 do
  for b2=0 to 255
  next b2
  toggle b.2
  toggle b.2
 loop

start3:
 do
  for b3=0 to 255
  next b3
  toggle b.3
  toggle b.3
 loop

start4:
 do
  for b4=0 to 255
  next b4
  toggle b.4
  toggle b.4
 loop

start5:
 do
  for b5=0 to 255
  next b5
  toggle b.5
  toggle b.5
 loop
And as expected all 6 LEDs blinked in unison.


However if you don't use start0 and simply increase the number of all the start lines by any number, such as:
Code:
start1:
 do
  for b0=0 to 255
  next b0
  toggle b.0
  toggle b.0
 loop

start2:
 do
  for b1=0 to 255
  next b1
  toggle b.1
  toggle b.1
 loop

start3:
 do
  for b2=0 to 255
  next b2
  toggle b.2
  toggle b.2
 loop

start4:
 do
  for b3=0 to 255
  next b3
  toggle b.3
  toggle b.3
 loop

start5:
 do
  for b4=0 to 255
  next b4
  toggle b.4
  toggle b.4
 loop

start6:
 do
  for b5=0 to 255
  next b5
  toggle b.5
  toggle b.5
 loop
You'll discover the first parallel task (in the code, its number is irrelevent in this case) is running twice as fast as all the others as the LED blinks twice for each blink of the other LEDs.

But if you were to use start tasks in an order such as 1,3,4,5,6,7 then all 6 will blink in unison, only when you don't use start0 and the remaining task numbers don't have a gap in them will you get the first task running at double speed.


How weird is that?

Maybe someone might find a use for it :)



edit: I did one further test with this code:
Code:
start1:
 do
  toggle b.0
 loop

start2:
 do
  toggle b.1
 loop

start3:
 do
  toggle b.2
 loop

start4:
 do
  toggle b.3
 loop

start5:
 do
  toggle b.4
 loop

start6:
 do
  toggle b.5
 loop
and found with my multimeter the first LED was blinking at 292hz and the rest were all blinking at 146hz. And when I used start task numbers of 0,1,2,3,4,5 , all LEDs were blinking in unison at 170hz.
 
Last edited:

bpowell

Senior Member
Interesting. I imagine you're "Start1:" program is getting double the attention since the PICAXE is looking to service START0: and then START1:...so START1 is basically getting its instructions processed twice per rotation...this could be useful if you're multi-tasking but want one program to have a priority over the others...good find!
 

Haku

Senior Member
I think you're on to something, by eliminating start0: the chip automatically thinks start0 is at the beginning, so it allocates twice the amount of time to the start1 code.

Taking that idea a step further I tried this piece of code:
Code:
start0:
start1:
start2:
start3:
 do
  toggle b.0
 loop

start4:
 do
  toggle b.1
 loop
The LED on b.0 blinks at 800hz and the LED on b.1 blinks at 200hz.

Ok now that's cool.


Hey now we know how to use multiple starts to increase the processing time allocated to certain code. This could actually be useful!
 

Technical

Technical Support
Staff member
Actually you now have 4 multiple tasks all doing the same thing, not more time allocated to a single task. After a while the first 4 tasks will drift apart by a few us and your b.0 LED will start to flicker erratically.

In the first case (missing start0: ) the compiler automatically adds in the missing start0 (it has to be there by default) and so you end up with 2 tasks doing the same thing (0 and 1, not just 1).
 

Haku

Senior Member
Ok I understand now about 4 tasks doing the same thing after doing some more tests, the for..next loop in the test code of my first post was sped up because both tasks were increasing the same variable involved with the for..next.

It certainly explains why the code I was originally working on appeared to run at double speed.

But what I don't understand is how the tasks can drift apart, aren't they being driven by the same clock? And how long will it take to drift, minutes/hours/days?
 

Technical

Technical Support
Staff member
Tasks process one command from each task in rotation. However as some commands are quicker to process than others, 4 loops doing exactly the same thing will eventually drift out of sync due to the fact that certain BASIC commands take longer than others to process (e.g. toggle and goto do not take exactly the same time to process). So to start with the 4 tasks will go toggle-toggle-toggle-toggle then goto-goto-goto-goto. Eventually you may get goto-toggle-tooggle-toggle then toggle-goto-goto-goto etc. This effect is much more noticeable when you add 'pauses' into the program, as they yield processing during idle times to the other tasks.
 
Top