Using a Variable acrost cores.

Non-technical related questions should go here.
Post Reply
User avatar
seulater
Member++
Posts: 28
Joined: Sat Jan 09, 2010 11:04 pm

Using a Variable acrost cores.

Post by seulater »

I am reading and reading, but it's just not clear to me yet how i can share a variable acrost all 4 cores.

Maybe my concept of the multi core is messed up. I am playing with the XC-1A demo board. There are 12 leds and i wanted to create a program to simply make a led go around and around.
Since 3 leds are on each core i had to create 4 tasks for each one to control the 3 leds on it.
i had that working, but when it came time to sync them is where it fell all apart for me.

I have included what i have so far. It should be clear what i am trying to do. I need to share the led variable. Or another way, which is eluding me.


Thanks.
Attachments
main.zip
This is what i have so far.
(605 Bytes) Downloaded 332 times
main.zip
This is what i have so far.
(605 Bytes) Downloaded 332 times


Heater
Respected Member
Posts: 296
Joined: Thu Dec 10, 2009 10:33 pm

Post by Heater »

I'm not sure how the LEDs are wired up there but if as you say there are 4 cores with 3 LEDs each it would be enough for a thread on core 0, say, to hold a variable representing the state of all the LEDS. That is 12 bits of an integer. Core 0 would be responsible for updating the required display pattern of the LEDs in that integer.

Then every time the pattern is changed use it to update the 3 LEDs on core 0 and also have the update thread send it to three threads one on each core 1 to 3. The threads that receive the new pattern via a channel extract the relevant bits of the pattern for their particular LEDS from the integer.

In summary, have one thread on core 0 maintain the required pattern in an integer and send that integer to the three other threads to use for their LEDS.
User avatar
seulater
Member++
Posts: 28
Joined: Sat Jan 09, 2010 11:04 pm

Post by seulater »

That is exactly how i was trying to do it. however, i could not figure out how to allow the other threads to take note of this global variable.
User avatar
paul
XCore Addict
Posts: 169
Joined: Fri Jan 08, 2010 12:13 am
Contact:

Post by paul »

The memory for each core is not available to any of the other cores. Data is passed round using channels. So to implement the above example naivly (the code could be tidied using replication, but thats beyond the scop of this doc):

Code: Select all

#include <xs1.h>
#include <platform.h>

// imagine 16 LEDs - 4 per core, 1 on each bit of a 4 bit port on each core
on stdcore[0]: out port LEDS_Core0 = XS1_PORT_4A; 
on stdcore[1]: out port LEDS_Core1 = XS1_PORT_4A;
on stdcore[2]: out port LEDS_Core2 = XS1_PORT_4A;
on stdcore[3]: out port LEDS_Core3 = XS1_PORT_4A;

void LED_master( chanend c[] );
void LED_slave( chanend c , out port led_port, unsigned mask, unsigned shift);

int main( )
{

chanend c[4];

par
{
    // LED master process that issues the status of the LED's - has the array of channels to them
    on stdcore[0]: LED_master( c );

    // LED slave processes to output LED data - each have 1 channel going to them
    on stdcore[0]: LED_slave( c[0], LEDS_Core0, 0x000F, 0 );
    on stdcore[1]: LED_slave( c[1], LEDS_Core1, 0x00F0, 4 );
    on stdcore[2]: LED_slave( c[2], LEDS_Core2, 0x0F00, 8 );
    on stdcore[3]: LED_slave( c[3], LEDS_Core3, 0xF000, 12 );
}

}

void LED_master( chanend c[] )
{
unsigned led_status = 1;
unsigned count = 0;
timer t;
unsigned ts;

while (1)
{
    // send out updated LEDs
    for (int i = 0; i < 4; i++)
        c[i] <: led_status;

   // insert a bit at the end of each cycle
   if (count == 16) {
       count = 0;
       led_status |= 1;
    } else led_stat <<= 1; // shift up the bit

    // implement a delay of 0.25s
    t :> ts;
    t when timerafter(ts + 25000000) :> ts;


}

}


void LED_slave( chanend c , out port led_port, unsigned mask, unsigned shift)
{
    unsigned local_led_status = 0;
 
    led_port <: 0; // initialise LED port to all off
   
    while (1)
    {
    // get led status in - will wait until data is sent to us
    c :> local_led_status;

    // prepare appropriate data for input
    local_led_status &= mask;
    local_led_status >>= shift;

    led_port <: local_led_status;
    }
}
Note - there may be some subtle bugs, I haven't compiled or simulated/run this and it will need adapting for the XC1, but should show the general principle. The XMOS tutorials and example projects supplied with the XDE should provide a good started as well! Also check out the XC programming book (available free online http://www.xmos.com/published/xc_en) for a more comprehensive overview.
Paul

On two occasions I have been asked, 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.
User avatar
seulater
Member++
Posts: 28
Joined: Sat Jan 09, 2010 11:04 pm

Post by seulater »

Thanks for the example.
Heater
Respected Member
Posts: 296
Joined: Thu Dec 10, 2009 10:33 pm

Post by Heater »

Great example. I'm glad we are thinking of this "simple" variable sharing because I have an application that needs more RAM than a single core can supply. So I want it to be able to use RAM space from the 3 other cores on a G4 via channels.
Post Reply