Threading - On the propeller chip
John Board
Posts: 371
G'day,
Well, I'm kind of tired, and my mind is warping, and I'm seeing visions of possible threading on the propeller chip...
I'm curious to whether there are any systems avaliable for software generated threading on the propeller chip... I know it'd be a streach....
Why? Well, I don't know, I've just been pondering about it for a while. I'm wondering if it's possible, and how it would be acheived if so...
-John
Well, I'm kind of tired, and my mind is warping, and I'm seeing visions of possible threading on the propeller chip...
I'm curious to whether there are any systems avaliable for software generated threading on the propeller chip... I know it'd be a streach....
Why? Well, I don't know, I've just been pondering about it for a while. I'm wondering if it's possible, and how it would be acheived if so...
-John
Comments
I found this interesting
http://forums.parallax.com/showthread.php?120461-Single-Cog-Multi-Threading-Made-Easy-(assembler)
Peter van der Zee (pjv on forum) has written a RTOS for the Propeller (honorable mention in 2009/2010 Propeller contest) that supports threading.
There are some other threads around that talk about how to implement various forms of "threaded" operations.
This is a start.
Duane J
It is a concurrent programming technique where multiple program counters exist in the same virtual address space. As opposed to the Unix fork which copies the existing process into another address space for a separate execution context. There's also a small area of thread local storage for the thread stack and thread local storage.
A propeller chip is actually a pretty good match for the concept. Hub ram is the shared address space, cog ram is thread local storage, and the cognew command creates a new thread of execution.
The Propeller has very little implicit state for each thread. Typically there's just the program counter and the flags. Often the state of the flags doesn't need to be preserved and context switches can usually be arranged so that the flags don't need to be saved and restored. The JMPRET instruction already handles the saving of the program counter and switching to another, so there may not be a need for anything more for context switching.
Threading is typically a way to make a processor look like more than one processor by time-sharing tasks. That is, more than one application or server program can run on a single core of processor. This is often accomplished with an interrupt mechanism set off by a hardware timer. The interrupt can save a given context (stack and register states) for one task and start another task. Obviously this can cause problems if not handled very well by programmers.
Propeller makes the need for threaded programming go away partially by allowing up to 8 tasks to run as one task per core. Obviously, Propeller cogs can also thread by time-slicing, but such is not as complicated as the general case.
There are ways to make Propeller use threads to effectively run many more than 8 tasks. OpenMP and pthread-like multi-tasking are features supported by propeller-gcc. Generally one should have experience with GCC (used on Linux and MacOS mainly) before trying propeller-gcc threading. To date, OpenMP has only been successfully used on Propeller with propeller-gcc.
Another interesting feature of pthreads is that the threads can migrate between COGs; any time a thread goes to sleep the COG running it looks for a new thread to run, which may previously have been running on another COG. You can optionally lock a thread to run on a particular COG if you need to (e.g. if it's using hardware resources).
Eric
Of course it's not another core, and chip manufacturers have been very up-front with the facts. Pity XMOS marketing figures we're too dumb to appreciate threading. Maybe this deception has to do with a last-ditch effort to meet profitability goals.
Correct. Catalina can run between 150 and 200 threads on a "bare bones" propeller. The limitation is the available memory, since each thread requires a block of RAM to save the current state of each thread during context switching. So if your program code or data is large you probably cannot have many threads, but if it is small you can have quite a few.
The Catalina threading library is very simple - you can just run any C function as a thread. The function can accept parameters using the same mechanism as a C "main" function (i.e. argc/argv). Here is a simple thread function:
Here is an example of a call to the library function to start an instance of it:
There are other functions available - e.g. to terminate a thread, coordinate between threads etc. But it is all really all very simple.
Ross.
-John
Below is a propeller-gcc example from the toggle demos (demos/pthreads_toggle/toggle.c).
In CMM/LMM mode this example runs 30 threads spread over 8 COGs.
Other examples are available in the toggle demos.
What? Threading? YES.. RUN AWAY....
I warned you all, its just harmless threading you said....
I completely agree . One of the attractions of the Propeller is that you shouldn't need to get into all the complexity of thing like Posix threads. On the Propeller you should be able to simply run things on different cogs.
Fortunately, with Catalina you can do this too. But sometimes you just run out of cogs ...
Ross.
software multitasking is one option
propforth had this, but it was removed. turns out it doesnt get you much, and it way nicer to add more more physical propchips. this currently supported and very effective