Issue with 'unsafe chanend assignment' Occupying Extra Core during Compilation Topic is solved

Technical questions regarding the XTC tools and programming with XMOS.
User avatar
vergil19
Member++
Posts: 21
Joined: Thu Jan 20, 2022 3:54 am

Issue with 'unsafe chanend assignment' Occupying Extra Core during Compilation

Post by vergil19 »

Hey guys,

While I was using the unsafe channel in USER_MAIN_CORES, similar to the example in `extra_i2s`,
it appears that the assignment of `uc_vol_to_rgb` consumes an individual logical core in the compilation result.

Code: Select all

extern unsafe chanend uc_vol_to_rgb;
#define USER_MAIN_DECLARATIONS chan c_vol_to_rgb;
#define USER_MAIN_CORES                              \
    on tile[0] : unsafe                                       \
    {                                                                \
        uc_vol_to_rgb = (chanend)c_vol_to_rgb; \
    }
I'm wondering if it indeed consumes a core, or if I can safely ignore this. Any help would be appreciated.


View Solution
User avatar
fabriceo
XCore Addict
Posts: 186
Joined: Mon Jan 08, 2018 4:14 pm

Post by fabriceo »

as it is written, the compiler allocate a task resource and launch it. your code is executed and then task resource is deallocated/free.
so yes, requires a core but this doesn't impact overall task timing as this task is not kept in the scheduler list.
sooner or later you will prefer modifying the main.xc file rather than using the #define USER_MAIN_xx :)
User avatar
vergil19
Member++
Posts: 21
Joined: Thu Jan 20, 2022 3:54 am

Post by vergil19 »

fabriceo wrote: Thu Nov 30, 2023 3:31 pm as it is written, the compiler allocate a task resource and launch it. your code is executed and then task resource is deallocated/free.
so yes, requires a core but this doesn't impact overall task timing as this task is not kept in the scheduler list.
sooner or later you will prefer modifying the main.xc file rather than using the #define USER_MAIN_xx :)
Thank you for your explanation! It's very valuable to me.

In fact, for many years we have been modifying main.xc.

However, when it comes to communicating these codes across version changes or with new colleagues, the cost of understanding for other colleagues is often significant.

As a result, I am refactoring our project's public code to make it easier for us to obtain updates from XMOS and to be more helpful to our team, especially these new guys :)
User avatar
Ross
XCore Expert
Posts: 968
Joined: Thu Dec 10, 2009 9:20 pm
Location: Bristol, UK

Post by Ross »

Agreed, this is a bit annoying. The compiler checks will mean you can never really run 8 cores with this in place. I will try and find a solution for you when I get some time. Likely involves the C based lib_xcore to do some trickery..
User avatar
vergil19
Member++
Posts: 21
Joined: Thu Jan 20, 2022 3:54 am

Post by vergil19 »

Ross wrote: Fri Dec 01, 2023 11:16 am Agreed, this is a bit annoying. The compiler checks will mean you can never really run 8 cores with this in place. I will try and find a solution for you when I get some time. Likely involves the C based lib_xcore to do some trickery..
Combining the chanend assignment code with other code that running forever should work. But in this case, we lose the flexibility here. Here is what I did in the `user_main.h`

Code: Select all

#if VOL_TO_RGB_EN
#include "visualize_volume_impl.h"
extern unsafe chanend uc_vol_to_rgb;
#define VOL_TO_RGB_DECLAR chan c_vol_to_rgb;
#define VOL_TO_RGB_CORE                                           \
    on tile[0] : unsafe                                           \
    {                                                             \
        uc_vol_to_rgb = (chanend)c_vol_to_rgb;                    \
    }                                                             \
    par                                                           \
    {                                                             \
        on tile[1].core[1] : update_rgb_cache_loop(c_vol_to_rgb); \
        on tile[1].core[1] : rgb_effect_handle_loop();            \
    }

#else
#define VOL_TO_RGB_DECLAR
#define VOL_TO_RGB_CORE
#endif // VOL_TO_RGB_EN

#define USER_MAIN_DECLARATIONS \
    VOL_TO_RGB_DECLAR

#define USER_MAIN_CORES \
    VOL_TO_RGB_CORE
I do want to make the code more maintainable, without lossing a lot of perfermance in doing so..
User avatar
fabriceo
XCore Addict
Posts: 186
Joined: Mon Jan 08, 2018 4:14 pm

Post by fabriceo »

good stuff!

Ross, why not enhancing lib_xua module_build_info with 2 optional headers:

Code: Select all

OPTIONAL_HEADERS += user_cores.h user_decl.h
and then in the lib_xua modify main.xc like:

Code: Select all

#ifdef __user_decl_h_exists__
#include "user_decl.h"
#endif

    USER_MAIN_DECLARATIONS
...

#ifdef __user_cores_h_exists__
#include "user_cores.h"
#endif
    USER_MAIN_CORES
this would bring much flexibility than the 2 defines USER_MAIN_CORES and USER_MAIN_DECLARATIONS while keeping compatibility

thx
Last edited by fabriceo on Sun Dec 03, 2023 12:25 pm, edited 1 time in total.
User avatar
vergil19
Member++
Posts: 21
Joined: Thu Jan 20, 2022 3:54 am

Post by vergil19 »

fabriceo wrote: Sun Dec 03, 2023 9:19 am good stuff!

Ross, why not enhancing lib_xua module_build_info with 2 optional headers:
This looks great! It seems like these changes are unlikely to break the existing compatibility.
User avatar
Ross
XCore Expert
Posts: 968
Joined: Thu Dec 10, 2009 9:20 pm
Location: Bristol, UK

Post by Ross »

fabriceo wrote: Sun Dec 03, 2023 9:19 am good stuff!

Ross, why not enhancing lib_xua module_build_info with 2 optional headers:

Code: Select all

OPTIONAL_HEADERS += user_cores.h user_decl.h
and then in the lib_xua modify main.xc like:

Code: Select all

#ifdef __user_decl_h_exists__
#include "user_decl.h"
#endif

    USER_MAIN_DECLARATIONS
...

#ifdef __user_cores_h_exists__
#include "user_cores.h"
#endif
    USER_MAIN_CORES
this would bring much flexibility than the 2 defines USER_MAIN_CORES and USER_MAIN_DECLARATIONS while keeping compatibility

thx
Yes we can add this (feel free to issue a PR). We used this trick recently for HID descriptors. Note, any PR would have to support xcommon_cmake also.
User avatar
vergil19
Member++
Posts: 21
Joined: Thu Jan 20, 2022 3:54 am

Post by vergil19 »

Yes we can add this (feel free to issue a PR). We used this trick recently for HID descriptors. Note, any PR would have to support xcommon_cmake also.
[/quote]

I finally resolve this issue by add an additional related to `AUDIO_IO_TILE` define in main.xc.

Cause they were actually used in audio related weak function like `UserBufferManagement` and `AudioHwConfig`

Code: Select all

        on tile[AUDIO_IO_TILE]:
        {
+            #ifndef AUDIO_UNSAFE_RESRC
+            #define AUDIO_UNSAFE_RESRC
+            #endif
+            AUDIO_UNSAFE_RESRC
            /* Audio I/O task, includes mixing etc */
User avatar
Ross
XCore Expert
Posts: 968
Joined: Thu Dec 10, 2009 9:20 pm
Location: Bristol, UK

Post by Ross »

I think this is the way to do it in XC. I think if you want to use C/lib_xcore you can do something like:

Code: Select all

static chanend_t g_c;

void UserBufferManagementInit()
{
}

void UserBufferManagement(unsigned sampsFromUsbToAudio[], unsigned sampsFromAudioToUsb[])
{
    // Do something with g_c
}

// Function that was called in the main par via user_main.h 
void myFunc() 
{
    channel_t c = chan_alloc();
    g_c = c.end_a;
    // do something with c.end_b
}