Thread Disjointness Rules for Arrays

Technical questions regarding the XTC tools and programming with XMOS.
User avatar
lilltroll
XCore Expert
Posts: 956
Joined: Fri Dec 11, 2009 3:53 am
Location: Sweden, Eskilstuna

Thread Disjointness Rules for Arrays

Post by lilltroll »

What is my options for sharing the data in an array beween to functions in a par statement (Two threads on the same core) ?

Chapter 3.2, Programming XC on XMOS Devices
1) If thread Tx contains any modification to variable Vy then none of the other threads are allowed to use Vx.
2) If thread Tx contains a reference to variable Vy then none of the other threads are allowed to modify Vx

AND Chapter 1.4 Programming XC on XMOS Devices
Arrays are implicitly passed by reference

Does that mean that arrays cannot been shared between threads ?

What I need (Adaptive filtering):
One thread is updating an large array with filter coeffs.
Another thread is using that array with filter coeff to filter a real time signal.

If I use a channel to transfer the filtercoeff - the needed bandwith would get very high - but that is maybe ok. If so should/could I transfer the array integer by integer or the hole array over the channel.


Probably not the most confused programmer anymore on the XCORE forum.
User avatar
Berni
Respected Member
Posts: 363
Joined: Thu Dec 10, 2009 10:17 pm

Post by Berni »

I hit the same problem with the buffer in my audio player. Sadly you can't share a array so you really need to use channels to get it in the next thread.

Meaby you could cheat the compiler with a little inline assembler.
User avatar
lilltroll
XCore Expert
Posts: 956
Joined: Fri Dec 11, 2009 3:53 am
Location: Sweden, Eskilstuna

Post by lilltroll »

Berni wrote:I hit the same problem with the buffer in my audio player. Sadly you can't share a array so you really need to use channels to get it in the next thread.

Meaby you could cheat the compiler with a little inline assembler.
Thanks - I am trying with channels now to check the performance - but as you said: It must be easy to write it in ASM as an alternativ.

Well lets see how this runs at 500 MHz.

XTA

Code: Select all

        for(k=0;k<ntaps;k++)
        {
            c_W    :>    W;
                     0x100d4 	in (2r) r5, res[r2]
            {h,l}=macs(x,W,h,l);
                     0x100d6 	maccs (l4r) r3, r4, r6, r5
        }
Probably not the most confused programmer anymore on the XCORE forum.
Heater
Respected Member
Posts: 296
Joined: Thu Dec 10, 2009 10:33 pm

Post by Heater »

Doesn't "#pragma unsafe arrays" allow you to share an array across threads ?

Or why not just define your array in C and call C functions from XC to access it ?
User avatar
Berni
Respected Member
Posts: 363
Joined: Thu Dec 10, 2009 10:17 pm

Post by Berni »

Well channels inside the same device should be as fast as the CPU. Might be worth giving that pragma a try, i never saw it before.
Heater
Respected Member
Posts: 296
Joined: Thu Dec 10, 2009 10:33 pm

Post by Heater »

This at least compiles, sharing the array "unsafe":

Code: Select all

#include <platform.h>

int unsafe[100];

void thread_1()
{
#pragma unsafe arrays
	unsafe[1] = 1;
}

void thread_2()
{
#pragma unsafe arrays
	unsafe[1] = 1;
}

int main()
{
	par
	{
		on stdcore[0]: thread_1();
		
		on stdcore[0]: thread_2();
		
	}
	return (0);
}
User avatar
Berni
Respected Member
Posts: 363
Joined: Thu Dec 10, 2009 10:17 pm

Post by Berni »

U nice i gotta remember that one! :D
User avatar
oar_some
Member
Posts: 8
Joined: Thu Feb 11, 2010 7:46 pm

Post by oar_some »

No, the unsafe pragma is not for sharing arrays between threads, but disabling bounds checking.
The above does (should) not compile.
Heater
Respected Member
Posts: 296
Joined: Thu Dec 10, 2009 10:33 pm

Post by Heater »

Ah but it does !

I know in the manual it only mentions turning off bounds checking and not thread sharing. But one could interpret what the manual says to mean not checking that the entire array is out of bounds to a thread because the array can be in use by another thread:)

Problem for me is that I still don't have an XMOS device to test it on so it may not actually work. However I have a very strong suspicion that it will. Some please try it.

P.S. One might want to put endless loops around those accesses to "unsafe" making them in to long lived threads.
User avatar
Woody
XCore Addict
Posts: 165
Joined: Wed Feb 10, 2010 2:32 pm

Post by Woody »

You can share an array across threads with the help of a C routine.

I've done this in the classD code which seems to have similarities with your adaptive filter. Two threads interpolate the left and right channels separately, putting the interpolated sample values into arrays read by a C function. The arrays are then read by the third thread which performs the PWM.

The code is on xmos.com -> Support -> Software -> Class D Audio Power Amplifier.

The function audioDac (executed on both L and R channels) sends the base address of the array over a channel to the pwm thread. The arrays are then read by pwmThread() using the C function in fifoRead.c. Note that CAST32 is an inline assembler macro defined in pwmDefines.h.