bypass parallel usage rule in XC

Technical questions regarding the XTC tools and programming with XMOS.
User avatar
fabriceo
XCore Addict
Posts: 186
Joined: Mon Jan 08, 2018 4:14 pm

bypass parallel usage rule in XC

Post by fabriceo »

Hello,
This is to share a solution to bypass the verification of parallel usage of memory location across tasks.
Sometime you need to share data across task and don't want to use channel to share data.
one way is to write the function in C instead of XC.
but it is possible to make it in XC by creating an alternative symbol, being at the same address:

example:

Code: Select all

int array[2];
extern int array2[5];

void t1(int arr[]) {
   arr[1] = 12345;
   arr[0] = 1;
}

void t2(int arr[]) {
   while (arr[0] == 0);
   printf("arr[1]=%d\n", arr[1]);
}

int main(){
par {
   on tile[0] : t1(array);
   on tile[0] : { asm(".linkset array2, array"); t2(array2);}
}
compiling and checking the generated assembly code in -O3 is fine
still using variable across task requires some specific handling especially in -O3.
volatile statement doesn't exist as such in XC (only for unsafe pointers).
using the memory barrier instruction is helpful : asm("":::"memory"); to force the compiler to flush and reload variables.

it is possible to declare the "globals variable" in a C file and use a .h file declaring them external.
then the asm .linkset statements can all be put in a single C file.

for experimentation and feedbacks


User avatar
dsteinwe
XCore Addict
Posts: 144
Joined: Wed Jun 29, 2016 8:59 am

Post by dsteinwe »

Hello,

I have just thought about volatile keyword for variables and found this thread.

Until now, I wasn't aware that a memory barrier is created by the compiler. Actually, this is not surprising when you think about it. Since I want to understand your assembly code better: Can you tell me where this instruction is documented? Searching for "memory" in the PDFs is not effective.

I guess you already know this thread about memory sharing: https://www.xcore.com/viewtopic.php?t=3061

The document https://www.xmos.com/download/Programmi ... 9577A).pdf contains a section about sharing variables (3.2 Thread Disjointness Rules):
In other words, a group of threads can have shared read-only access to a variable,
but only a single thread can have exclusive read-write access to a variable. These
rules guarantee that each thread has a well-defined meaning that is independent of
the order in which instructions in other threads are scheduled. Interaction between
threads takes place explicitly using inputs and outputs on channels (see §3.3).
I hope I was able to give you new informations.

Best regards!
User avatar
fabriceo
XCore Addict
Posts: 186
Joined: Mon Jan 08, 2018 4:14 pm

Post by fabriceo »

dsteinwe wrote: Thu Feb 01, 2024 1:06 pm Hello,

I have just thought about volatile keyword for variables and found this thread.

Until now, I wasn't aware that a memory barrier is created by the compiler. Actually, this is not surprising when you think about it. Since I want to understand your assembly code better: Can you tell me where this instruction is documented? Searching for "memory" in the PDFs is not effective.
Hi!

you will find it in page 289 of document xTIMEcomposer-User-Guide-14_14.x.pdf

thanks for reminding us the topic about memory sharing
User avatar
dsteinwe
XCore Addict
Posts: 144
Joined: Wed Jun 29, 2016 8:59 am

Post by dsteinwe »

Thanks for the reference to the document. Have a nice day!