<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Generator" CONTENT="MS Exchange Server version 6.5.7654.12">
<TITLE>RE: [Cbe-oss-dev] Memalign and free doesn't work</TITLE>
</HEAD>
<BODY>
<!-- Converted from text/plain format -->
<P><FONT SIZE=2>Phil,<BR>
<BR>
If you're trying to program the Cell's SPEs, I would suggest using the functions malloc_align() and free_align(). You'll have to include the headers <spu_intrinsics.h> and <libmisc.h> for them to work. Take a look at the description of malloc_align() below that I copied directly from the comment block in the malloc_align.h file.<BR>
<BR>
/* Function<BR>
*<BR>
* void * malloc_align(size_t size, unsigned int log2_align)<BR>
*<BR>
* Description<BR>
* The malloc_align routine allocates a memory buffer of <size><BR>
* bytes aligned to the power of 2 alignment specified by <log2_align>.<BR>
* For example, malloc_align(4096, 7) will allocate a memory heap<BR>
* buffer of 4096 bytes aligned on a 128 byte boundary.<BR>
*<BR>
* The aligned malloc routine allocates an enlarged buffer<BR>
* from the standard memory heap. Space for the real allocated memory<BR>
* pointer is reserved on the front of the memory buffer.<BR>
*<BR>
* ----------------- <--- start of allocated memory<BR>
* | pad 0 to |<BR>
* |(1<<log2_align)-1 |<BR>
* | bytes |<BR>
* |-----------------|<BR>
* | allocation size |<BR>
* |-----------------|<BR>
* | real buffer ptr |<BR>
* |-----------------|<---- returned aligned memory pointer<BR>
* | |<BR>
* | requested |<BR>
* | memory |<BR>
* | buffer |<BR>
* | size |<BR>
* | bytes |<BR>
* |______________________|<BR>
*<BR>
* Memory allocated by this routine must be freed using the free_align<BR>
* routine.<BR>
*<BR>
* The size of the allocation is saved for special cases where the<BR>
* data must be mored during a realloc_align.<BR>
*/<BR>
<BR>
Also, please notice that this function does not take the alignment size in bytes as the argument directly. Rather, it takes in an integer power of a base-two number system. So, for an alignment of 16, you would use 4, since 2^4 is 16. For 128 you would use 7, since 2^7 is 128, and so on.<BR>
<BR>
In addition, as the diagram above shows, the function does allocate a bit more memory than requested. The space required has to do with the extra information it stores. It's primarily used in case the realloc_align() function is called. Either way, just don't be surprised if you have a little more memory allocated than anticipated. This also answers your original question for memalign allocating 68848 bytes.<BR>
<BR>
My source code follows below. I have included the Makefile used. Copy the text labelled Makefile to a file called, well... you guessed it, "Makefile". Also, create a C file called "spu_mem.c" in the same directory, and copy my source code into it. Now, just run the command "make <ENTER>" from the command line and it should build the program "spu_mem" that you can run directly.<BR>
<BR>
code: Makefile<BR>
============================================<BR>
PROGRAM_spu := spu_mem<BR>
IMPORTS = -lmisc<BR>
include /opt/cell/sdk/buildutils/make.footer<BR>
============================================<BR>
<BR>
code: spu_mem.c<BR>
============================================<BR>
#include <stdio.h><BR>
#include <spu_intrinsics.h><BR>
#include <libmisc.h><BR>
<BR>
// Register 1 in SPU provides difference between heap and stack pointers<BR>
// Heap pointer not provided in SPU, but stack pointer is in first element<BR>
// of Register 1, and difference between heap and stack is in second<BR>
// element of Register 1.<BR>
// Set reg_1 equal to Register 1.<BR>
register volatile vector unsigned int reg_1 asm("1");<BR>
<BR>
int main(unsigned long long spe_id, unsigned long long<BR>
program_data_ea, unsigned long long env)<BR>
{<BR>
int before_alloc, after_alloc;<BR>
void *triangles_local_mem;<BR>
<BR>
// Print differences in stack and heap BEFORE memory allocation.<BR>
// First, extract second element from Register 1, then print it.<BR>
before_alloc = spu_extract(reg_1, 1);<BR>
printf("Before malloc_align allocation: stack - heap = %#x\n",<BR>
before_alloc);<BR>
<BR>
// Allocate roughly 64k of space in SPU using malloc_align.<BR>
triangles_local_mem = (void *)malloc_align(65536, 4);<BR>
<BR>
// Print differences in stack and heap AFTER memory allocation.<BR>
// First, extract second element from Register 1, then print it.<BR>
after_alloc = spu_extract(reg_1, 1);<BR>
printf("After malloc_align allocation: stack - heap = %#x\n",<BR>
after_alloc);<BR>
<BR>
// Show the difference in space available in memory.<BR>
printf("Space allocated by malloc_align: %d bytes\n",<BR>
before_alloc - after_alloc);<BR>
<BR>
// Free allocated memory in SPU using free_align.<BR>
free_align(triangles_local_mem);<BR>
return 0;<BR>
}<BR>
============================================<BR>
<BR>
The full sequence of commands to compile and run the program, plus the output from the spulet above follow:<BR>
<BR>
[user@ps3 spu]# ls<BR>
Makefile spu_mem.c<BR>
<BR>
[user@ps3 spu]# make<BR>
/usr/bin/ccache /usr/bin/spu-gcc -W -Wall -Winline -I. -I /opt/cell/sdk/usr/spu/include -O3 -c spu_mem.c<BR>
spu_mem.c: In function 'main':<BR>
spu_mem.c:12: warning: unused parameter 'spe_id'<BR>
spu_mem.c:13: warning: unused parameter 'program_data_ea'<BR>
spu_mem.c:13: warning: unused parameter 'env'<BR>
/usr/bin/ccache /usr/bin/spu-gcc -o spu_mem spu_mem.o -L/opt/cell/sdk/usr/spu/lib -Wl,-N -lmisc<BR>
/usr/bin/ppu-embedspu -m32 spu_mem spu_mem spu_mem-embed.o<BR>
/usr/bin/ppu-ar -qcs spu_mem.a spu_mem-embed.o<BR>
<BR>
[user@ps3 spu]# ./spu_mem<BR>
Before malloc_align allocation: stack - heap = 0x3e5d0<BR>
After malloc_align allocation: stack - heap = 0x2e3b0<BR>
Space allocated by malloc_align: 66080 bytes<BR>
<BR>
As you can see above, the function malloc_align() actually allocated 66080 instead of the 65536 expected. The difference is due to that bit of extra information it also stores in the process, as we discussed previously.<BR>
<BR>
A great resource in case you're learning to program for the Cell B.E. is Matthew Scarpino's book "Programming the Cell Processor: For Games, Graphics, and Computation."<BR>
<BR>
I hope this helps,<BR>
Gunnar<BR>
<BR>
-----Original Message-----<BR>
From: cbe-oss-dev-bounces+vikbergg=msoe.edu@lists.ozlabs.org on behalf of Phil Pratt-Szeliga<BR>
Sent: Sun 10/11/09 9:18<BR>
To: W<BR>
Cc: cbe-oss-dev@lists.ozlabs.org<BR>
Subject: Re: [Cbe-oss-dev] Memalign and free doesn't work<BR>
<BR>
Well this is causing me to run out of memory. Isn't this wrong in<BR>
standard unix then too?<BR>
<BR>
Phil<BR>
<BR>
On Sun, Oct 11, 2009 at 2:36 AM, W <w.l.fischer@googlemail.com> wrote:<BR>
> Hi Phillip,<BR>
><BR>
> nearly the same on standard linux:<BR>
><BR>
> $ ./a.out<BR>
> MemUsage: 0<BR>
> MemUsage: 135168<BR>
> $ uname -a<BR>
> Linux z4 2.6.26-1-686 #1 SMP Fri Mar 13 18:08:45 UTC 2009 i686 GNU/Linux<BR>
><BR>
> Willem<BR>
><BR>
> On Sat, Oct 10, 2009 at 3:13 PM, Philip Pratt-Szeliga<BR>
> <phil.pratt.szeliga@gmail.com> wrote:<BR>
>> Hello,<BR>
>><BR>
>> I am noticing that memalign and free doesn't work on the ps3.<BR>
>><BR>
>> code:<BR>
>> =======================<BR>
>> #include <malloc.h><BR>
>><BR>
>> static void printMemUsage()<BR>
>> {<BR>
>> struct mallinfo mi;<BR>
>> mi = mallinfo();<BR>
>> printf("MemUsage: %d\n", mi.arena);<BR>
>> }<BR>
>><BR>
>> int main(unsigned long long spe_id, unsigned long long<BR>
>> program_data_ea, unsigned long long env)<BR>
>> {<BR>
>> printMemUsage();<BR>
>> void * triangles_local_mem = memalign(16, 65535);<BR>
>> free(triangles_local_mem);<BR>
>> printMemUsage();<BR>
>> return 0;<BR>
>> }<BR>
>> =======================<BR>
>><BR>
>> compile:<BR>
>> spu-gcc cell_function.c<BR>
>><BR>
>> results:<BR>
>> #./a.out<BR>
>> MemUsage: 0<BR>
>> MemUsage: 68848<BR>
>> #<BR>
>><BR>
>> Am I doing something wrong?<BR>
>><BR>
>> -Phil<BR>
>> _______________________________________________<BR>
>> cbe-oss-dev mailing list<BR>
>> cbe-oss-dev@lists.ozlabs.org<BR>
>> <A HREF="https://lists.ozlabs.org/listinfo/cbe-oss-dev">https://lists.ozlabs.org/listinfo/cbe-oss-dev</A><BR>
>><BR>
><BR>
_______________________________________________<BR>
cbe-oss-dev mailing list<BR>
cbe-oss-dev@lists.ozlabs.org<BR>
<A HREF="https://lists.ozlabs.org/listinfo/cbe-oss-dev">https://lists.ozlabs.org/listinfo/cbe-oss-dev</A><BR>
<BR>
<BR>
<BR>
</FONT>
</P>
</BODY>
</HTML>