Shop OBEX P1 Docs P2 Docs Learn Events
Processors, tasks and threads — Parallax Forums

Processors, tasks and threads

inakiinaki Posts: 262
edited 2008-08-02 21:08 in Propeller 1
Several questions about multitasking/multiprocessing in Propeller: (by the way, is this stuff explained anywhere ?)

- Is·there a·multithread core or there is an affinity between tasks and processor(I mean 1 processor=1 task) ?
- Is it possible to have more threads/tasks than processors at the same time (#tasks>=#processors)·?
- Do the same tasks runs all the time in the same processor·?
- Can I create several threads per task ? If so, can these threads run at the same time on several processors ?
- Is there any kind of synchronization between tasks or they run alone without interaction ?
- Is there a way to perform a call from one task/thread to another ?
- Does exist the concept of shared and private areas or to the contrary is it shared all the space ?

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔

Comments

  • Paul BakerPaul Baker Posts: 6,351
    edited 2006-02-23 16:21
    Ok, the propeller is not a multi-threaded microcontroller, it is a multi-processor microcontroller (Read http://forums.parallax.com/showthread.php?p=572215·for some explanation on this), as such many of the questions you posed are moot. Don't think of the processor as being like your PC where you have 1 processor with many programs running simultaneously, think of it as a specially design computer that has 8 seperate processors on it, each of which can communicate with the others.

    There is no multithreaded control built into the propeller, but under special circumstances when programming in assembly you can pass control between 2 or more procedures on a single processor but it must be done manually. This process control is abit advanced, and some discussion of it has occured in another thread, so I invite you to look for it if you want more information (hint: the talk is about the JMPRET assembly command), but we have explained its function about as best we can without posting too much detail (Parallax has ask those of us who have seen·the chip not to post the documents since they are not complete), so if how its done is a little over your head, you'll have to wait until the product is availible (with complete documentation) and we can show you real-world examples of its use.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·1+1=10

    Post Edited (Paul Baker) : 2/23/2006 5:14:32 PM GMT
  • inakiinaki Posts: 262
    edited 2006-02-23 18:29
    I·understand.
    When you create a program, all the processors code is contained in a single program or·you must load a single·program·for each processor ?·Can you·use·only the processors you need or there is always something (like some 'do nothing' loop)·running in all the processors ?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
  • Paul BakerPaul Baker Posts: 6,351
    edited 2006-02-23 18:51
    You control what gets run, whenever a cog (processor) isn't running anything it shuts down and consumes minimal current. On startup all cogs are shut down and cog0 is loaded with a boot loader, it aquires the program from a connected PC, or an external EEPROM (in that order). Once the internal memory is loaded, cog0 loads a spin interpretor and starts executing your program, your program can optionally start other cogs with either a·spin interpretor or assembly code. If any cog runs out of instructions to do (say you didn't loop), it shuts itself down. You also have the capability of a cog shuting down any cog (including itself).

    The running out of code feature is nice, it permits you to run "background" tasks that stop the processor they are run·on·once they are done, freeing up the processor to be allocated to other tasks (this is good for outwards communications, like a SHIFTOUT-like function. You start up a processor, it sends out the data, then shuts itself down when it's finished). Meanwhile your main program is free to continue running other tasks while the other processor handles the communication.

    If all cogs end up being shutdown after your program starts executing (either through explicitly stopping a cog, or·a cog running out of instructions), the entire chip's processors are shut down, and a reset must occur to get it running again.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·1+1=10

    Post Edited (Paul Baker) : 2/23/2006 7:00:39 PM GMT
  • danieldaniel Posts: 231
    edited 2006-02-23 19:03
    Paul Baker said...
    ...your program can optionally start other cogs with either a·spin interpretor or assembly code. If any cog runs out of instructions to do (say you didn't loop), it shuts itself down. You also have the capability of a cog shuting down any cog (including itself)...
    Can you outline·what information about all the cogs are available to any given·cog at any instant?· I'm thinking along the lines of idle, executing, waiting on interrupt, etc.--the sort of things one might want to know about a task in a multi-tasking environment.

    Daniel
  • Paul BakerPaul Baker Posts: 6,351
    edited 2006-02-23 19:12
    There is no mechanisim for a cog to spy on the staus of another cog, in fact if you use the auto-allocate method of starting cogs, you don't even know which cog its run on (actually the function may return the cog its run on, I can't remember exactly and I dont have the appropriate document on hand to verify this). But that said, you can still keep tabs by having the program thats executing store its status in global memory (on the hub), so that other cogs can see what it is doing, but this is all done by the program you start on the cog. In addition, a cog can get it's own cog number and post that to hub memory for the other cogs to know where it's being run.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·1+1=10
  • inakiinaki Posts: 262
    edited 2006-02-23 19:17
    You say you can shutdown a cog, but can you awake a cog that is·idle ?

    Suppose you have a processor that is idle (in the shut down status) then, in your 'main' program·you·get something interesting to do, for example·you got·some new·data·from a digital·input. Can you then launch·the processor that was idle to perform a new task ?

    In other words, while there are available processors can you 'launch processors' to perform tasks·anytime or they must be launched at·boot time only ?

















    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
  • Paul BakerPaul Baker Posts: 6,351
    edited 2006-02-23 19:20
    inaki said...

    You say you can shutdown a cog, but can you awake a cog that is·idle ?

    Suppose you have a processor that is idle (in the shut down status) then, in your 'main' program·you·get something interesting to do, for example·you got·some new·data·from a digital·input. Can you then launch·the processor that was idle to perform a new task ?

    In other words, while there are available processors can you 'launch processors' to perform tasks·anytime or they must be launched at·boot time only ?

    As long as you have availible processors, you can start new tasks on a shutdown cog at anytime, both spin and assembly have a COGNEW function which starts another cog, it does not have to be at boot time (in the literal sense, at boot time, only cog0 is not shutdown, cogs1-7 are all shutdown waiting to be started whenever directed to do so).

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·1+1=10

    Post Edited (Paul Baker) : 2/23/2006 7:24:11 PM GMT
  • danieldaniel Posts: 231
    edited 2006-02-23 19:28
    Assume a MasterCog, and seven SlaveCogs.· The MasterCog assigns tasks to a next-available SlaveCog by starting it with an arbitrary·routine.

    1. So there is no built-in to know if all COGS are busy?

    2. What happens if a COG is running and another COG "starts" that running COG?· Any errors reported to the initiator COG? Does target COG continue current task or stop immediately and start new task?

    3. What is the COG startup latency (how long does it take the target COG to go from idle to executing first instruction), if absolute time is unavailable, what about in terms of HUB rotations?· Does this answer change if the COG is restarting a previously loaded routine or is assigned a new routine?

    I understand that using assigned-purpose SlaveCogs might be a possible solution.

    Daniel


    Post Edited (daniel) : 2/23/2006 7:35:10 PM GMT
  • Paul BakerPaul Baker Posts: 6,351
    edited 2006-02-23 19:43
    1. If all cogs are already running and another COGNEW is performed, the return value will indicate an error (this I know is in the return value).

    2. A cog that is already running cannot be overrun by another COGNEW function, see the answer to your first question.

    3. Im not exactly sure, the started cog either loads the interpretor or a block of 512 longs from the hub memory, so the latency will be at least 512 hub rotations, Im not sure of what other latency would be involved.

    There are two different ways of starting a task on another cog, the first is COGNEW, which is the suggested method, this will not trample on any already running processes. The second is COGSTART which you specify the cog number, and if there is a process already running on that cog, it will trample it (stop the process and start the newly specified process). The COGSTART method of starting cogs in not the suggested method of doing things, because most objects to be incorporated will be based on the COGNEW method of cog starting. If you rely upon these objects and your program then goes and tramples an already running process, you will experience irratic application behaviour. And dubugging multi-processor applications is a heck of alot harder than single process debugging, so its always best to write programs where your processes behave nicely with other running processes.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·1+1=10

    Post Edited (Paul Baker) : 2/23/2006 7:47:44 PM GMT
  • danieldaniel Posts: 231
    edited 2006-02-23 19:59
    Paul Baker said...
    ...debugging multi-processor applications is a heck of a lot harder than single process debugging, so its always best to write programs where your processes behave nicely with other running processes....
    So true blush.gif, but it is part of my day-job.

    Daniel
  • Paul BakerPaul Baker Posts: 6,351
    edited 2006-02-24 13:14
    Correction: the command isn't COGSTART, its COGINIT, sorry for the typo, I was writing from memory.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·1+1=10
  • danieldaniel Posts: 231
    edited 2006-02-24 17:10
    It would appear from the prelim doc that COGINIT effectively blocks further activity of the·COG that calls that instruction until the named COG gets its data loaded (for at least 512 hub rotations).· Is this correct?

    Does COGINIT with D[noparse][[/noparse]3] == 0 just (restart) the named COG, or does it also poke the 512 longwords into the COG?·

    If not, is there a way of reactivating a stopped COG without disturbing·its current program state or without incurring the memory transfer latency?· (The negative answer to this question seems to be implied by the COGSTOP description.)

    Daniel




    Post Edited (daniel) : 2/26/2006 5:07:45 AM GMT
  • EricREricR Posts: 35
    edited 2008-08-02 19:52
    My experience today was that when the Spin cognew statement is used to start an assembly cog, it will tranple existing processes.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Eric
  • Mike GreenMike Green Posts: 23,101
    edited 2008-08-02 20:07
    Eric,
    Other than stopping a running cog (with COGSTOP) or reinitializing a running cog with a new program (with COGINIT/COGNEW), there is no way to externally control a cog. You can write a program that runs on a cog and looks for a signal to copy into the cog's ram a new program, then the cog transfers control to it, but there's no way to force this from outside the cog.
  • EricREricR Posts: 35
    edited 2008-08-02 21:08
    Mike, thanks for your comments.

    Perhaps I just think it terminated another cog.· What I see is the hires VGA dispaly vanish when I try to add another cog.· I suppose there are other reasons the display could go dark.

    So that I could post code about this problem, I just submitted an object, "MonVarsVGA", to the Propeller Object Exchange.· This is my first formally posted object.· It is used by what I am attaching now, "CogStart.zip".· I think I might be able to create a more minimal demo of the problem, but I wanted to put this out right now.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Eric
Sign In or Register to comment.