Threading - On the propeller chip
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:
/* * function : this function can be executed as a thread. */ int function(int argc, char *argv[]) { while (1) { /* do stuff here */ } return 0; }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.
/** * @file toggle.c * This program demonstrates starting threads to * toggle pins, using the pthreads interface * The cog makes all IO except 30/31 toggle. * * Copyright (c) 2011 Parallax, Inc. * MIT Licensed (see at end of file for exact terms) */ #include <stdio.h> #include <propeller.h> #include <pthread.h> /* * here's the toggle code that runs in another cog */ void * do_toggle(void *arg) { unsigned int pins = (unsigned int)arg; /* pin the thread to this particular cog this is necessary because we rely on the DIRA hardware register, which is per cog pthread_set_affinity_thiscog_np is non-portable (hence the _np suffix) and is a Propeller extension */ pthread_set_affinity_thiscog_np(); /* nobody will be waiting for us to finish */ pthread_detach(pthread_self()); /* set up the hardware registers */ _DIRA |= pins; _OUTA |= pins; for(;;) { _OUTA ^= pins; /* update the pins */ printf("toggled %x on cog %d\n", pins, __builtin_propeller_cogid()); sleep(1); /* nap for 1 second */ } } /* * main code * This is the main thread * It launches othre threads to actually toggle * the pins (one thread per pin). */ pthread_t thr[32]; void main (int argc, char* argv[]) { int i; int result; unsigned int pins; printf("hello, world!\n"); for (i = 0; i < 30; i++) { /* set up the parameters for the C cog */ pins = (1U<<i); pthread_create(&thr[i], NULL, do_toggle, (void *)pins); } printf("done launching threads!\n"); for(;;) { sleep(30); } } /* +-------------------------------------------------------------------- * ¦ TERMS OF USE: MIT License * +-------------------------------------------------------------------- * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files * (the "Software"), to deal in the Software without restriction, * including without limitation the rights to use, copy, modify, merge, * publish, distribute, sublicense, and/or sell copies of the Software, * and to permit persons to whom the Software is furnished to do so, * subject to the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * +-------------------------------------------------------------------- */What? Threading? YES.. RUN AWAY....
I warned you all, its just harmless threading you said....
I completely agree
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