cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
1,388 Views
Registered: ‎09-08-2016

Issue in parallel thread program in Xilinx SDK with Xilkernel and 2 Microblaze cores

Hi,

 

I use Xilinx Vivado embedded development kit. In the design, I use two 32-bit Microblaze processors. I use Xilkernel in SDK for writing the multi-thread program. I write parallel thread program as follows.

 

#include "xmk.h"
#include "sys/init.h"
#include "platform.h"
#include <stdio.h>
#include "xparameters.h"
#include "xil_printf.h"
#include "xil_exception.h"
#include "xtmrctr.h"
#include "xuartlite.h"
#include "xtmrctr_i.h"
#include "xil_io.h"
#include "xintc.h"
#include "xstatus.h"
#include "xdebug.h"
#include "xil_cache.h"

XTmrCtr TMRInst;
#define TIMER_COUNTER_0     0

void print(char *str);

void *thread_function(unsigned long int *arg);
void *thread_function1(unsigned long int *arg);

int main()
{

    init_platform();

    pthread_t a_thread;
    pthread_t b_thread;

    void *thread_result;
    void *thread_result1;

    unsigned long int res;
    unsigned long int res1;

    unsigned long int a[4]={1,2,3,4},b[4]={6,7,8,9};

    u32 Init_status = 0;
    int start = 0;
    int stop = 0;

    xilkernel_init();

    xil_printf("Before thread : a=%x;%x;%x;%x\n\r",a[0],a[1],a[2],a[3]);
    xil_printf("Before thread : b=%x;%x;%x;%x\n\r",b[0],b[1],b[2],b[3]);

    Init_status = XTmrCtr_Initialize(&TMRInst, XPAR_AXI_TIMER_0_DEVICE_ID);
        if ( Init_status != XST_SUCCESS ) {
                xil_printf("XTmrCtr_Initialize() failed.\r\n");
        }
    XTmrCtr_SetResetValue(&TMRInst, TIMER_COUNTER_0, 0);
    XTmrCtr_SetOptions(&TMRInst, TIMER_COUNTER_0, XTC_ENABLE_ALL_OPTION | XTC_AUTO_RELOAD_OPTION);
    XTmrCtr_Start(&TMRInst, TIMER_COUNTER_0);

    start=XTmrCtr_GetValue(&TMRInst, TIMER_COUNTER_0);

       res=pthread_create(&a_thread,NULL,thread_function,(unsigned long int *)a);
       res1=pthread_create(&b_thread,NULL,thread_function1,(unsigned long int *)b);

       res=pthread_join(a_thread,&thread_result);
       res1=pthread_join(b_thread,&thread_result1);

    //xilkernel_start();//should be always commented

    stop=XTmrCtr_GetValue(&TMRInst, TIMER_COUNTER_0);

    xil_printf("start=%d\n\r",start);
    xil_printf("stop=%d\n\r",stop);
    xil_printf("delay=%d\n\r",stop-start);
    xil_printf("Completed\n\r");
      xil_printf("After thread : a=%d;%d;%d;%d\n\r",a[0],a[1],a[2],a[3]);
      xil_printf("After thread : b=%d;%d;%d;%d\n\r",b[0],b[1],b[2],b[3]);

    cleanup_platform();

    return 0;
}

void *thread_function(unsigned long int *arg){

     //xil_printf("thread 1 is called \n");
    arg[0]=arg[0]*7;
    arg[1]=arg[1]*7;
    arg[2]=arg[2]*7;
    arg[3]=arg[3]*7;

}

void *thread_function1(unsigned long int *argg){

     //xil_printf("thread 2 is called \n");

    argg[0]=argg[0]*7;
    argg[1]=argg[1]*7;
    argg[2]=argg[2]*7;
    argg[3]=argg[3]*7;

}

 

///////////////////////////////////////////////////////

 

The single thread program is as follows.

 

#include <stdio.h>
#include "platform.h"
#include "xparameters.h"
#include "xil_printf.h"
#include "xil_exception.h"
#include "xtmrctr.h"
#include "xuartlite.h"
#include "xtmrctr_i.h"
#include "xil_io.h"
#include "xintc.h"
#include "xstatus.h"
#include "xdebug.h"
 
XTmrCtr TMRInst;
#define TIMER_COUNTER_0     0
 
void print(char *str);
 
 
int main()
{
    init_platform();
 
    unsigned long int a[4]={1,2,3,4},b[4]={6,7,8,9};
 
    u32 Init_status = 0;
    int start = 0, stop=0;
 
    unsigned int i=0;
 
    xil_printf("Hello World\n\r");
    xil_printf("Before : a=%d;%d;%d;%x\n\r",a[0],a[1],a[2],a[3]);
    xil_printf("Before : b=%d;%d;%d;%d\n\r",b[0],b[1],b[2],b[3]);
 
    Init_status = XTmrCtr_Initialize(&TMRInst, XPAR_AXI_TIMER_0_DEVICE_ID);
        if ( Init_status != XST_SUCCESS ) {
                xil_printf("XTmrCtr_Initialize() failed.\r\n");
        }
    XTmrCtr_SetResetValue(&TMRInst, TIMER_COUNTER_0, 0);
    XTmrCtr_SetOptions(&TMRInst, TIMER_COUNTER_0, XTC_ENABLE_ALL_OPTION | XTC_AUTO_RELOAD_OPTION);
    XTmrCtr_Start(&TMRInst, TIMER_COUNTER_0);
 
    start=XTmrCtr_GetValue(&TMRInst, TIMER_COUNTER_0);
 
////////////////////////////////////////////////////////
    a[0]=a[0]*7;
    a[1]=a[1]*7;
    a[2]=a[2]*7;
    a[3]=a[3]*7;
 
    b[0]=b[0]*7;
    b[1]=b[1]*7;
    b[2]=b[2]*7;
    b[3]=b[3]*7;
 
    stop=XTmrCtr_GetValue(&TMRInst, TIMER_COUNTER_0);
 
    xil_printf("start=%d\n\r",start);
    xil_printf("stop=%d\n\r",stop);
    xil_printf("delay=%d\n\r",stop-start);
    xil_printf("Completed\n\r");
    xil_printf("After : a=%d;%d;%d;%d\n\r",a[0],a[1],a[2],a[3]);
    xil_printf("After : b=%d;%d;%d;%d\n\r",b[0],b[1],b[2],b[3]);
 
    cleanup_platform();
    return 0;
}

 

///////////////////////////////////////////////////////

 

Since I use two 32-bit Microblaze processors, latency for the aforementioned multi thread program should be less than the single thread, but it seems to be in the reverse order. Can any one tell what would be the issue with this? I have attached the screen shot of design with two Microblaze cores.

two_cores_vivado.png
0 Kudos
7 Replies
Highlighted
Observer
Observer
1,369 Views
Registered: ‎12-14-2013

Re: Issue in parallel thread program in Xilinx SDK with Xilkernel and 2 Microblaze cores

The additional delay that you're seeing is caused by the time to create threads and switch between them (well call this overhead).

Also, multi-threading doesn't reduce computation time, it extends it via overheads.

So, in the multi-threaded program you're measuring computation time + overhead, in the single-threaded on you're measuring computation time.

 

I'll try to explain it via math:

 

Ct - computation time
Oh - overhead
N - number of processors in the system

 

Reducing overall time means:

Multi-threaded execution time < Single threaded execution time or
Ct/N + Oh < Ct or
Oh < Ct(N-1)/N

Let's take an example (Ct takes 2us, Oh is 20us and N is 2)

 

Single-threaded performance = Ct = 2us
Multi-threaded performance = Ct / 2 + Oh = 1us + 20us = 21us

Multi-threading doesn't help us here.

 

If we were to increase the complexity of the task by a lot (Ct = 200us) then

 

Single-threaded performance = Ct = 200us
Multi-threaded performance = Ct / 2 + Oh = 100us + 20us = 120us

Multi-threading helps here!

 

So, the first thing to look out for is the complexity of the task, if it's a simple task, it's not worth parallelizing, if it's a complex task then yes, it's worth doing it in parallel.

 

The second thing to look out for is that this ONLY WORKS when your threads run in parallel on DIFFERENT processors (ie. thread A on processor 1 and thread B on processor 2). I'm suspecting that both threads are run by the same processor, let me know otherwise.

 

Good luck!

 

 

 

0 Kudos
Highlighted
1,363 Views
Registered: ‎09-08-2016

Re: Issue in parallel thread program in Xilinx SDK with Xilkernel and 2 Microblaze cores

Thanks for your reply. Yes, both the threads are running on Micorblaze_0, but I have connected two Microblazes. Please let me know how to make one thread to run on Micorblaze_0 and another thread to run on Micorblaze_1. In SDK, I do not find any way to include both the cores Micorblaze_0 and Micorblaze_1 on the particular project, where one of the cores will be selected from the processor option. How to keep both the cores on the project.

 

Please find the attachment for more details.

sdk_microblaze.png
0 Kudos
Highlighted
Moderator
Moderator
1,328 Views
Registered: ‎09-12-2007

Re: Issue in parallel thread program in Xilinx SDK with Xilkernel and 2 Microblaze cores

Whst yiu are trying to do isnt supported in the SDK. However, you can use multi microblaze with a mailbox ip to communicate between the two.

One mb could be the master and have two apps running on each processor.

AMP is supported on the zynq.

0 Kudos
Highlighted
1,308 Views
Registered: ‎09-08-2016

Re: Issue in parallel thread program in Xilinx SDK with Xilkernel and 2 Microblaze cores

Could please you send the block diagram for this connection.

0 Kudos
1,300 Views
Registered: ‎09-08-2016

Re: Issue in parallel thread program in Xilinx SDK with Xilkernel and 2 Microblaze cores

I have connected both the cores with mailbox using xl_concat and axi_uartlite separately. Now, I created two programs with respect to each of the cores. Now, I search the code for (mailbox that connects both the cores) executing both the programs. Please let me know if u have.

0 Kudos
Highlighted
Moderator
Moderator
1,295 Views
Registered: ‎09-12-2007

Re: Issue in parallel thread program in Xilinx SDK with Xilkernel and 2 Microblaze cores

No, I dont have a design. However, there is example code for the mbox here:

https://github.com/Xilinx/embeddedsw/tree/master/XilinxProcessorIPLib/drivers/mbox/examples

 

This is also delivered in the SDK install in the drivers folder

0 Kudos
Highlighted
1,286 Views
Registered: ‎09-08-2016

Re: Issue in parallel thread program in Xilinx SDK with Xilkernel and 2 Microblaze cores

In SDK, I have created the new project with Microblaze_0 and Xilkernel successfully. I try to create another project with Microblaze_1 and Xilkernel, but it shows the "empty project" instead "Helloword". I have attached the system design, where the connections are as follows.

 

(1) Interrupt_0 of mailbox_0 is connected with microblaze_0_xlconcat

(2) Interrupt_1 of mailbox_0 is connected with microblaze_1_xlconcat

 

I use only one mailbox IP. In both the microblazes 0 and 1, interrupt has been enabled.

 

Please let me know the issues.

mbox_ub.png
0 Kudos