Shop OBEX P1 Docs P2 Docs Learn Events
Stack Length Calculations for Cogs — Parallax Forums

Stack Length Calculations for Cogs

JoeFLJoeFL Posts: 10
edited 2013-01-15 09:42 in Propeller 1
I've viewed the stack length demo spn code and I seem to understand it.
But, my program calls 4 methods in 4 different cogs from the top method.

Question: Can the stack length demo code be applied to determine my code's optimum
stack length for each cog?

Pub Main
coginit(2, Method1, @stack[100])
coginit(3, Method2, @stack[200])
coginit(4, Method3, @stack[300])
coginit(5, Method4, @stack[400])
pgm code
Pub Method1
pgm code
Pub Method2
pgm code
Pub Method3
pgm code
Pub Method4
pgm code

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2013-01-14 20:59
    The Stack Length Demo Code only gives you an estimate of the amount of stack space needed. To know the worst case stack space needed, you have to do a static simulation of each cog (and its method call) assigning a worst case stack space to each execution path in the program. That's pretty complex, but not very different from what's done in some optimizing compilers (not Spin though). Depending on how your program is written it may be easy to get a rough estimate just looking at what methods call other methods and adding up the rough static needs of each of the methods (base stack size + space for parameters + space for local variables + fudge factor for statement execution).
  • MagIO2MagIO2 Posts: 2,243
    edited 2013-01-14 23:16
    As long as you don't run out of HUB-RAM, I would not waste to much time with finding out the required stack-size. Just give it enough!

    You did not give us the whole code, so it's maybe OK, but on the first glance it looks like there could be some misunderstandings:
    The range of COG-IDs is 0-7. Usually your main is started in COG 0. So, in your coginits you could go ahead with 1-4.
    BUT: In general you should not use COGINIT without a damn good reason! In nearly all cases it is better to use COGNEW!

    I don't know how you defined stack, but your code looks odd to me in regards of stack usage.
    The main does not need any room in your stack-array, because it is using the whole remaining HUB-RAM as stack-space (The whole memory marked as free when you hit F8 in the propeller tool).
    So, in the first coginit you can start Method1 with @stack[0]. Giving each method a stack with 100 longs, the size for the stack-array would be stack[400]. Please remember, the SPIN language has a stack which grows buttom-up and not top-down as in many other systems.
  • JoeFLJoeFL Posts: 10
    edited 2013-01-15 06:37
    MagIO2 wrote: »
    As long as you don't run out of HUB-RAM, I would not waste to much time with finding out the required stack-size. Just give it enough!

    You did not give us the whole code, so it's maybe OK, but on the first glance it looks like there could be some misunderstandings:
    The range of COG-IDs is 0-7. Usually your main is started in COG 0. So, in your coginits you could go ahead with 1-4.
    BUT: In general you should not use COGINIT without a damn good reason! In nearly all cases it is better to use COGNEW!

    I don't know how you defined stack, but your code looks odd to me in regards of stack usage.
    The main does not need any room in your stack-array, because it is using the whole remaining HUB-RAM as stack-space (The whole memory marked as free when you hit F8 in the propeller tool).
    So, in the first coginit you can start Method1 with @stack[0]. Giving each method a stack with 100 longs, the size for the stack-array would be stack[400]. Please remember, the SPIN language has a stack which grows buttom-up and not top-down as in many other systems.
    Thanks for your reply, my code is too long to post it, so i did emulate it and sorry
    it does look a little strange. In my top method I do call another method which handles some serial communication which uses about 50 longs in the stack, thus the reason cog2 starts using the stack @ 100, Because the serial communication is utilized in cog1, thus the reason my method1 starts in cog2. If I'm not enabling and/or disabling cogs, why are you saying its wrong to use coginit ? Thanks
  • MagIO2MagIO2 Posts: 2,243
    edited 2013-01-15 06:59
    It's not wrong, it's bad practice! For a program it does not make a difference in which COG it runs. If you write good code, you want to split things into different SPIN-files which are later on reusable in other projects. So, having COG number dependencies is NOT what you want for reusable code, because sooner or later it might happen that you use 2 different SPIN-files developed years back which by accident start some code with the same COG number -> BOOM.

    Especially if you want to put code into the Object-Exchange it's an absolute NOGO to have fixed COG-numbers in this object.
  • JoeFLJoeFL Posts: 10
    edited 2013-01-15 07:55
    MagIO2 wrote: »
    It's not wrong, it's bad practice! For a program it does not make a difference in which COG it runs. If you write good code, you want to split things into different SPIN-files which are later on reusable in other projects. So, having COG number dependencies is NOT what you want for reusable code, because sooner or later it might happen that you use 2 different SPIN-files developed years back which by accident start some code with the same COG number -> BOOM.

    Especially if you want to put code into the Object-Exchange it's an absolute NOGO to have fixed COG-numbers in this object.
    I understand ... thanks
  • prof_brainoprof_braino Posts: 4,313
    edited 2013-01-15 09:42
    JoeFL wrote: »
    my code is too long to post it

    You can enclose it in [code] tags from advance edit and it will be scroolable, or you can attach it as a zip. Its easier to fix when we can look at it.
Sign In or Register to comment.