Shop OBEX P1 Docs P2 Docs Learn Events
How many cogs run — Parallax Forums

How many cogs run

Ray0665Ray0665 Posts: 231
edited 2013-11-19 08:13 in Propeller 1
I have a program lets call it main, it uses the F32 object which runs in
another cog. Now lets say I have another object lets call it mod1 which also uses F32


Mod1 runs as part of main, that is, main does not start mod1 in another cog


As I understand it there is only one copy of F32 in hub ram but how many cogs are running F32?


If main starts mod1 in another cog what happens with F32?

Comments

  • Mike GMike G Posts: 2,702
    edited 2013-11-19 07:12
    It would be better if you posted an example as the description is a bit confusing. Main usually refers to the Pub Main method running in the top level object of an application. Main spins up other COGs that are either referenced in the OBJ block, PASM, or a method in the top level object.

    The accepted practice is to use a PUB Start method if an object starts a COG. Otherwise use PUB Init. F32 documents the following.
    PUB start
    {{
      Start start floating point engine in a new cog.
      Returns:     True (non-zero) if cog started, or False (0) if no cog is available.
    }}
      stop
      f32_Cmd := 0
      return cog := cognew(@f32_entry, @f32_Cmd) + 1
    

    It looks like each object the has an OBJ reference to F32 and invokes the F32.Start method causes a new COG to start. If that's what you're asking.
  • MagIO2MagIO2 Posts: 2,243
    edited 2013-11-19 08:01
    I don't know F32 by now, but you can check it by yourself. Which way does F32 define it's buffer for communicating with the PASM code running in the COG?
    Is it defined in VAR or in DAT section?

    If it is in VAR it means that the variable is only known in an instance of the object. Another instance of the same object has it's own version. This means that you have to "start" the object twice to function properly. (That is how I'd expect it to work)
    If the buffer is in a DAT section, there is only one copy used by all instances of the object. This means that only one COG has to run F32.
    But in that case multiple other COGs might have the idea to use it simultaneously. This is no problem if these COGs use Locks.

    If it is like I expect, you should be fine moving all variables into the DAT section and both parts (main and mod1 ) would then use the same F32.
  • AribaAriba Posts: 2,690
    edited 2013-11-19 08:13
    Ray0665 wrote: »
    I have a program lets call it main, it uses the F32 object which runs in
    another cog. Now lets say I have another object lets call it mod1 which also uses F32

    Mod1 runs as part of main, that is, main does not start mod1 in another cog
    Only the Assembly part of F32 runs in another cog. The Spin methodes of F32 run in the cog that call these methodes. It's the same as with the methodes of your "mod1".
    As I understand it there is only one copy of F32 in hub ram but how many cogs are running F32?

    If main starts mod1 in another cog what happens with F32?
    Yes there is only one copy, but the Spin methodes can be run from several cogs, and also the one PASM code can be started in several cogs.
    If you use an object like F32 from different cogs, you have two options:
    1) start the object only once (in Main) and use locks to make sure that not 2 cogs call an F32 methode at the same time.
    2) start another instance of the object for every cog that needs F32 (call the F32 start methode at begin of "mod1" which is started in a new cog). This needs more cogs, but you can run different F32 functions at the same time, one per cog.

    Not all objects allow both options, some are made to run as a single instance and implement the locking scheme right in the Spin interface methodes. There are for example versions of the FullDuplexSerial which can be called from several cogs but send and receive only thru one RX and TX pin.

    If this is the begin of a bigger project, you should be careful with resources like cogs and memory, so it may be the best to structure your code so that only one cog makes all the floating point calculations with the help of one F32 cog.

    Andy
Sign In or Register to comment.