Utilizing multiple threads

Technical discussions around xCORE processors (e.g. xcore-200 & xcore.ai).
User avatar
jonecm
Member++
Posts: 23
Joined: Tue Oct 11, 2011 4:36 am

Utilizing multiple threads

Post by jonecm »

Hey guys, I've been using FPGA's and Microcontrollers for quite some time but have lately been eyeing your XMOS devices but had some questions regarding utilizing all, say 8 cores on a device.
How would someone go about doing that in XC, I've done a fair bit of multi-threaded programming for pc's but havent seen any good examples of this accomplished on one of your processors.

Thanks,
Jon


User avatar
phalt
Respected Member
Posts: 298
Joined: Thu May 12, 2011 11:14 am

Post by phalt »

I've been learning XC all weekend and I can tell you now that using the cores on an XMOS chip is very, very easy.

The following snippet of code is taken from the tutorial for an XC-1A development board that we sell which is a good way to start programming with multi cores.

Code: Select all

int main (void) {
    par {
        on stdcore [0]: //do something
        on stdcore [1]: //do something
        on stdcore [2]: //do something
        on stdcore [3]: //do something
    }
    return 0;
}
The par keyword (thanks Al) allows you to run concurrent threads with very little code, you can then tell each core (stdcore) what you want to do!

Take a read through the programming XC on XMOS devices guide.

I'm fairly new to the language myself, but we have XMOS staff on the forum here to help if you get stuck with anything.
User avatar
Folknology
XCore Legend
Posts: 1274
Joined: Thu Dec 10, 2009 10:20 pm

Post by Folknology »

Just a small correction Paul

'par' is a keyword not a function, to the compiler it marks the following block as parallel rule statements/expressions rather than the C default of sequential code, basically it says do all these things at once rather than one after the other. which is normal in C. It also hijacks some function parameter passing in the case of channel ends in a none C like way to automate channel end allocation.

regards
Al
User avatar
phalt
Respected Member
Posts: 298
Joined: Thu May 12, 2011 11:14 am

Post by phalt »

Your XCore expert forum title isn't just for show is it? :D
Thanks for the correction.
User avatar
jonathan
Respected Member
Posts: 377
Joined: Thu Dec 10, 2009 6:07 pm

Post by jonathan »

Additionally, just a quick point. Paul's code shows an example of using the multi-core nature of e.g. the G4 chips. I believe the original question related to using the multi-threading capabilities of each core.

On an L1 chip, you will only have one core but still up to eight hardware threads...

As Paul says, there are examples and explanations in section 3 of the following document:

https://www.xmos.com/download/public/Pr ... 1.0%29.pdf

Slightly modifying Paul's code, on a two-core device (or more than 2!) you can write...

Code: Select all

int main (void) {
    par {
        on stdcore [0]: foo1();
        on stdcore [0]: foo2();
        on stdcore [0]: foo3();
        on stdcore [1]: foo4();
    }
    return 0;
}
This will put three threads on core 0 and one thread on core 1.

When using a single core device you can omit the "on stdcore [x]:" part, as the selection of core is unambiguous anyway.

You can use par statements anywhere in your program, including nesting them. One thing to bear in mind though is that there are only eight physical threads available per core, and a thread which attempts to exceed this limit won't work. (So parallel recursion might not be a smart plan ;-))

I suggest you read section 3 in that programming guide linked above for more info.
Image
User avatar
Interactive_Matter
XCore Addict
Posts: 216
Joined: Wed Feb 10, 2010 10:26 am

Post by Interactive_Matter »

If you then add to the picture that threads communicate over channels (more or less just data tunnels) by sending, receiving and waiting for data. And if you add that by that the most software architectures in XMOS chips are either event driven (think oaf agents passing messages) or pipe-and-filter architectures (working mostly on data streams).

Then you got a quite god picture what multithreading looks like in XMOS

(At least that is how I explain this world to me ;) )
User avatar
Folknology
XCore Legend
Posts: 1274
Joined: Thu Dec 10, 2009 10:20 pm

Post by Folknology »

There is one other useful form of par:

Code: Select all

chan c[4];
par (int i=0; i<4; i++) on stdcore[i] : myparfunc(i,c[i],c [(i+1)%4] ,params);
This allows you to run parallel versions of myparfunc (4 in this case) across different cores/threads, in XC parlance its a replicator (syntactic sugar), the channel stuff is to allow the threads to communicate. This is good for running the same code across many threads.

Basically 'par' is XC's way of saying your not in Kansas any-more Dorothy.

regards
Al
User avatar
phalt
Respected Member
Posts: 298
Joined: Thu May 12, 2011 11:14 am

Post by phalt »

Basically 'par' is XC's way of saying your not in Kansas any-more Dorothy.
This should be your signature.
User avatar
jonecm
Member++
Posts: 23
Joined: Tue Oct 11, 2011 4:36 am

Post by jonecm »

Thanks for the help guys, I'll be buying a few silicone chips to play around with
User avatar
phalt
Respected Member
Posts: 298
Joined: Thu May 12, 2011 11:14 am

Post by phalt »

If you want something to develop on, I'd suggest the XK-1A or the XC-1A development boards. They have everything you need to start learning XC and how to program XMOS devices.