COPRTHR-2 How much free local memory do you have?

Moderator: dar

COPRTHR-2 How much free local memory do you have?

Postby nickoppen » Sun Mar 19, 2017 10:07 am

From the paper, "Advances in Run-Time Performance and Interoperability for the Adapteva Epiphany Coprocessor" I note that Free Local Memory resides between the end of usrcore and stack memory (section 3.3).

Is there any way of calculating now much free memory there is? From a previous thread, I'm thinking that the call base = coprthr_tls_sbrk(0) will return you the beginning of free memory but is there any way of finding the end or how much memory is used by the stack? It would be great if it was as simple as 0x8000 - stacksize - base.

I'm working on an image process in application and being able to use all of the available memory will be quite important to getting the best performance.

Any advice would be greatly appreciated.

nick
Sharing is what makes the internet Great!
User avatar
nickoppen
 
Posts: 259
Joined: Mon Dec 17, 2012 3:21 am
Location: Sydney NSW, Australia

Re: COPRTHR-2 How much free local memory do you have?

Postby jar » Sun Mar 19, 2017 2:27 pm

Nick,

I haven't tested this but it should just be sp-base. To get the stack pointer value, you will need one line of inline assembly.

This may not work like you expect. If you call any routine that increases he stack (decrements the stack pointer) you will overwrite the the end of your data allocation. This may be sort of a trial and error thing when you're working on the margins like this. I wish there was a better way, but there is not (at the moment).
User avatar
jar
 
Posts: 284
Joined: Mon Dec 17, 2012 3:27 am

Re: COPRTHR-2 How much free local memory do you have?

Postby jar » Sun Mar 19, 2017 2:27 pm

Nick,

I haven't tested this but it should just be sp-base. To get the stack pointer value, you will need one line of inline assembly.

This may not work like you expect. If you call any routine that increases he stack (decrements the stack pointer) you will overwrite the the end of your data allocation. This may be sort of a trial and error thing when you're working on the margins like this. I wish there was a better way, but there is not (at the moment).
User avatar
jar
 
Posts: 284
Joined: Mon Dec 17, 2012 3:27 am

Re: COPRTHR-2 How much free local memory do you have?

Postby nickoppen » Sun Mar 19, 2017 11:29 pm

Thanks jar,

That actually sounds ok. I'll do some experiments with some function calls and see how that changes the stack pointer and then leave myself a small buffer zone. I don't want the stack to overwrite my data but equally, I'm pretty sure that overwriting the stack will not lead to happiness either.

Does it have to be in assembly though? What about e_reg_read(E_REG_R13)? I thought that the stack pointer is kept in general purpose register 13. Maybe I'm missing something but register 13 does not seem to change on repeated function calls. It seems stuck at: 0x808f0034.

nick
Sharing is what makes the internet Great!
User avatar
nickoppen
 
Posts: 259
Joined: Mon Dec 17, 2012 3:21 am
Location: Sydney NSW, Australia

Re: COPRTHR-2 How much free local memory do you have?

Postby jar » Mon Mar 20, 2017 3:14 am

You can use e_reg_read if you want, but that's ~52 bytes of of unnecessary instructions plus the subroutine call setup. I figured you'd rather save the space for your data.

Just look what e_reg_read actually does:
https://github.com/adapteva/epiphany-li ... reg_read.c

The first two case labels are irrelevant for your task. The third takes an unusual route to get a register value. First, it retrieves the special config register value. Then it performs the global address calculation for the memory mapped address. Then it dereferences the address.

Now consider this code, which compiles to one instruction, four bytes.
Code: Select all
register uintptr_t sp_val;
__asm__ __volatile__(
   "mov %[sp_val], sp"
   : [sp_val] "=r" (sp_val)
);


Your function may not be decreasing the stack pointer if it's not a leaf function.
User avatar
jar
 
Posts: 284
Joined: Mon Dec 17, 2012 3:27 am

Re: COPRTHR-2 How much free local memory do you have?

Postby nickoppen » Mon Mar 20, 2017 6:44 am

Thanks jar,

Indeed, I would prefer 4 bytes rather than 52.

So what I have is:

Code: Select all
    void * baseAddr = coprthr_tls_sbrk(0);   /// beginning of free space
    uintptr_t baseLowOrder = (int)baseAddr & 0x0000FFFF;
    int localSize = sp_val - baseLowOrder - 0x40;       /// leave 64 bytes as a buffer


where sp_val is as per your code above. The numbers seem to work out ok.

nick
Sharing is what makes the internet Great!
User avatar
nickoppen
 
Posts: 259
Joined: Mon Dec 17, 2012 3:21 am
Location: Sydney NSW, Australia

Re: COPRTHR-2 How much free local memory do you have?

Postby jar » Mon Mar 20, 2017 2:46 pm

I'm glad you figured it out despite my numerous errors. I was in a rush to reply yesterday.

I'm not sure why you have to AND the baseAddr with 0xFFFF because coprthr_tls_sbrk(0) should return a local address. The tls in coprthr_tls_sbrk means Thread Local Storage. Every returned address should be between 0x0000 (realistically, 0x0400) and 0x7FFF (realistically, something lower).
User avatar
jar
 
Posts: 284
Joined: Mon Dec 17, 2012 3:27 am

Re: COPRTHR-2 How much free local memory do you have?

Postby nickoppen » Mon Mar 20, 2017 10:11 pm

Hi jar,

Your input is always fantastic and I appreciate it greatly.

I did the bit mask because the value returned from coprthr_tls_sbrk looked too big. The value of baseAddr was 0x2050b while the stack pointer was 0x7b18. Subtracting baseAddr from sp_val gave 0x6970 (or 26,992 in decimal) which is about what I would expect. Therefore I assumed that the high order bits of baseAddr were being stripped off in the subtraction as a side effect. My CS lecturer in 1984 said, "Side effects are a BAD THING" so I thought I'd explicitly strip them off myself. baseLowOrder is 0x11a8 which looks much better.

You might have noticed that this low level stuff is not really my strong point.

nick
Sharing is what makes the internet Great!
User avatar
nickoppen
 
Posts: 259
Joined: Mon Dec 17, 2012 3:21 am
Location: Sydney NSW, Australia

Re: COPRTHR-2 How much free local memory do you have?

Postby nickoppen » Sat Mar 25, 2017 5:13 am

I've now finished the basic algorithm using two 8k buffers.

The next step is to use as much og yhe available memory as prudent. As you can see I left a 64byte gap between the stack pointer and the end of my "free" memory. This was a little too small. As soon as I run my kernel the core locked up. I'm assuming that the stack was overwritten and everything came to a grinding halt.

Is there a way of estimating how much stack is used per in the subsequent calls? I'd prefer to make an educated guess rather than suck it and see.

Nick
Sharing is what makes the internet Great!
User avatar
nickoppen
 
Posts: 259
Joined: Mon Dec 17, 2012 3:21 am
Location: Sydney NSW, Australia

Re: COPRTHR-2 How much free local memory do you have?

Postby jar » Sat Mar 25, 2017 6:01 am

Nick,

You might try printing out the stack pointer. If you know exactly how your code operates, you should be able to predict how much stack space is needed by analyzing the assembly. A compiler cannot analyze the code to determine stack usage. And there's the possibility of interrupts being called in some cases.

If you're really close to being able to fit an appealing amount of data, you might look at removing some code. You can use "coprcc-info" on your binary. If you're using the -fhost trick, run "coprcc --extract" on your binary to pull out the Epiphany ELF.
User avatar
jar
 
Posts: 284
Joined: Mon Dec 17, 2012 3:27 am

Next

Return to OpenCL

Who is online

Users browsing this forum: No registered users and 1 guest

cron