Shop OBEX P1 Docs P2 Docs Learn Events
Knowing how much stack to allocate — Parallax Forums

Knowing how much stack to allocate

CassLanCassLan Posts: 586
edited 2009-03-14 02:38 in Propeller 1
Hello, I guess up until now I've been running everything in one cog, but its time to use the propeller the way it was meant to be used.

I've been reading the manual, and it seems pretty straight forward, in this example from the manual 6 longs are allocated for stackspace for the cog, how does one know how much to allocate?
VAR
long SqStack[noparse][[/noparse]6] 'Stack space for Square cog
PUB Main | X
X := 2 'Initialize X
cognew(Square(@X), @SqStack) 'Launch square cog
<check X here> 'Loop here and check X
PUB Square(XAddr)
'Square the value at XAddr
repeat 'Repeat the following endlessly
long[noparse][[/noparse]XAddr] *= long[noparse][[/noparse]XAddr] ' Square value, store back
waitcnt(2_000_000 + cnt) ' Wait 2 million cycles


Also, how do people know how many cogs they have left? I have seen this in screenshots, but havent found it in the manual.

Thanks,

Rick

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2009-03-13 14:04
    1a) You can analyze your program to determine what routines in the program call what other routines and when and how much each routine uses at most. This is tedious and often inaccurate, can be done by an optimizing compiler that has the mechanism internally for use in optimization, but is not done by any existing Spin compiler.

    1b) You can add a piece to your program that initializes the stack space to some unique pattern, then you exercise your program and look to see how much of this pattern gets overwritten. For this, you have to estimate the largest possible stack area needed and add a "fudge factor".

    1c) You can just estimate the largest possible stack area needed and add a "fudge factor".

    2) You attempt to start a cog (with an assembly program that just consists of a jump instruction to itself). If successful, you save the cog number and do it again. If it fails, you know how many cogs can be started and you go back through the saved list of cog numbers and stop each one so they're available again.
  • CassLanCassLan Posts: 586
    edited 2009-03-13 14:26
    Thanks Mike,

    That makes allot of sense...if I write all "FF"s and then give the program access to all the space, I can observe how many address "FF"s get overwritten..Is there a way to see this in action? Or would that require ViewPort?

    Rick
  • Mike GreenMike Green Posts: 23,101
    edited 2009-03-13 14:34
    There's a program in the Object Exchange ("Stack Length") that starts a separate cog to monitor this. To actually watch it dynamically, I think you'd need ViewPort or you'd need to write something yourself that would do it.

    There's also an object in the Exchange to count the number of available cogs.
  • Oldbitcollector (Jeff)Oldbitcollector (Jeff) Posts: 8,091
    edited 2009-03-13 18:18
    Watch for Jeff's Webinar video, he did a bit on how to use the program that Mike mentioned.
    (It's about half-way into the Webinar.)

    OBC

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    New to the Propeller?

    Check out: Protoboard Introduction , Propeller Cookbook 1.4 & Software Index
    Updates to the Cookbook are now posted to: Propeller.warrantyvoid.us
    Got an SD card connected? - PropDOS
  • Jeff MartinJeff Martin Posts: 760
    edited 2009-03-14 02:38
    Hi CassLan,

    Check out the StackLength demo program (in Propeller Library Demos from the pull-down above the folder view in the Propeller Tool's Integrated Explorer).

    As OBC mentioned, we talked about this today in my webinar.· A number of video clips (including that one) will be available as soon as possible on the archive page for our webinars... unfortunately, we couldn't get those clips up there today (sorry), but we will make them available soon.




    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    --Jeff Martin

    · Sr. Software Engineer
    · Parallax, Inc.
Sign In or Register to comment.