Shop OBEX P1 Docs P2 Docs Learn Events
Threading - On the propeller chip — Parallax Forums

Threading - On the propeller chip

John BoardJohn Board Posts: 371
edited 2012-10-25 08:17 in Propeller 1
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

Comments

  • David BetzDavid Betz Posts: 14,516
    edited 2012-10-24 06:13
    John Board wrote: »
    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
    Propeller GCC and Catalina C both support threading on the Propeller.
  • John BoardJohn Board Posts: 371
    edited 2012-10-24 06:15
    Interesting - how many threads can it support / core?

    I found this interesting

    http://forums.parallax.com/showthread.php?120461-Single-Cog-Multi-Threading-Made-Easy-(assembler)
  • mindrobotsmindrobots Posts: 6,506
    edited 2012-10-24 06:15
    PropGCC offers pthread support, so you can use cooperative multi-tasking based on the standard pthread model.

    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 C. JohnsonDuane C. Johnson Posts: 955
    edited 2012-10-24 10:03
    Please guys, What is "Threading"?

    Duane J
  • Martin_HMartin_H Posts: 4,051
    edited 2012-10-24 10:24
    Please guys, What is "Threading"?

    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.
  • Mike GreenMike Green Posts: 23,101
    edited 2012-10-24 10:30
    It's essentially cooperative multitasking where you have state storage for each thread consisting of copies of the program counter and any registers. In the code, there are periodic calls to a context switching routine. When ever there would be a timed pause or wait for some I/O status change, the context switching routine is called. The thread either polls the timer status or I/O status or the context switching routine takes care of that.

    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.
  • jazzedjazzed Posts: 11,803
    edited 2012-10-24 10:40
    Please guys, What is "Threading"?

    Duane J

    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.
  • ersmithersmith Posts: 6,054
    edited 2012-10-24 11:27
    John Board wrote: »
    Interesting - how many threads can it support / core?
    PropGCC's pthreads library doesn't have a limit on threads per core (although each thread does take up memory, of course, so eventually you'll run out of RAM for stacks and such).

    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
  • User NameUser Name Posts: 1,451
    edited 2012-10-24 12:35
    BTW, threading has reached a high art on Intel's recent hyperthreaded processors. After a lot of practical benchmarking, I'm satisfied that hyperthreading is very nearly as effective as another core.

    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.
  • RossHRossH Posts: 5,463
    edited 2012-10-24 14:00
    David Betz wrote: »
    Propeller GCC and Catalina C both support threading on the Propeller.

    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:
    thread_id = _thread_start(&function, &stack, argc, argv);
    

    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 BoardJohn Board Posts: 371
    edited 2012-10-24 20:33
    RossH: That looks fantastic.... I'll should look more closely into PropGCC and Catalina...

    -John
  • jazzedjazzed Posts: 11,803
    edited 2012-10-24 22:12
    John Board wrote: »
    RossH: That looks fantastic.... I'll should look more closely into PropGCC and Catalina...

    -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.
     * +--------------------------------------------------------------------
     */
    
    
  • Clock LoopClock Loop Posts: 2,069
    edited 2012-10-24 23:13
    DONT DO IT MAN.. RUN AWAY... RUN AWAY...

    What? Threading? YES.. RUN AWAY....



    I warned you all, its just harmless threading you said....
  • RossHRossH Posts: 5,463
    edited 2012-10-25 00:42
    Clock Loop wrote: »
    DONT DO IT MAN.. RUN AWAY... RUN AWAY...

    What? Threading? YES.. RUN AWAY....

    I warned you all, its just harmless threading you said....

    I completely agree :smile:. 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.
  • prof_brainoprof_braino Posts: 4,313
    edited 2012-10-25 08:13
    Please guys, What is "Threading"?

    Duane J

    software multitasking is one option
  • prof_brainoprof_braino Posts: 4,313
    edited 2012-10-25 08:17
    an early version of forth on the prop claimed 140 tasks per core on the prpo.
    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
Sign In or Register to comment.