e-gcc virtual memory exhausted

Discussion about Parallella (and Epiphany) Software Development

Moderators: amylaar, jeremybennett, simoncook

e-gcc virtual memory exhausted

Postby jimmystone » Sat Mar 11, 2017 1:53 am

Hi, All

I am building a blake2b kernel on Parallella with e-core speedup
But my blake2b kernel compile fail when using e-gcc with this error
virtual memory exhausted: Cannot allocate memory


I watch the memory usage is almost 100% when compile fails.
Could anyone help?
Why such small hash kernel need over 1GB memory while it is compiling, and should I a more swap space for this issue?

My compile script is
Code: Select all
#!/bin/bash
ESDK=${EPIPHANY_HOME}
#ELDF=${ESDK}/bsps/current/fast.ldf
ELDF=${ESDK}/bsps/current/legacy.ldf

echo "Compiling Epiphany binary"
e-gcc -mfp-mode=int -O2 -T ${ELDF} ./kernel/blake2b.epi.c -o ./kernel/blake2b.elf -le-lib
echo "done"
exit 0




My source code is
Code: Select all
#include <stdint.h>
#include <e_lib.h>


#define ROTR64(x, y)  (((x) >> (y)) ^ ((x) << (64 - (y))))

#define G(r,i,a,b,c,d) \
{   \
  a = a + b + m[ blake2b_sigma[r][2*i] ]; \
   d = ROTR64(d ^ a, 32); \
   c = c + d; \
   b = ROTR64(b ^ c, 24); \
   a = a + b + m[ blake2b_sigma[r][2*i+1] ]; \
   d = ROTR64(d ^ a, 16); \
   c = c + d; \
  b = ROTR64(b ^ c, 63); \
}

#define ROUND(r)                    \
   G(r,0,v[ 0],v[ 4],v[ 8],v[12]); \
   G(r,1,v[ 1],v[ 5],v[ 9],v[13]); \
   G(r,2,v[ 2],v[ 6],v[10],v[14]); \
   G(r,3,v[ 3],v[ 7],v[11],v[15]); \
   G(r,4,v[ 0],v[ 5],v[10],v[15]); \
   G(r,5,v[ 1],v[ 6],v[11],v[12]); \
   G(r,6,v[ 2],v[ 7],v[ 8],v[13]); \
   G(r,7,v[ 3],v[ 4],v[ 9],v[14]); \


#define SWAP4(x) \
   ((((uint32_t)(x) >> 24) & 0x000000FF) | \
    (((uint32_t)(x) >> 8 ) & 0x0000FF00) | \
    (((uint32_t)(x) << 8 ) & 0x00FF0000) | \
    (((uint32_t)(x) << 24) & 0xFF000000)   \
   )

#define SWAP8(x) \
   ((((uint64_t)(x) >> 56) & 0x00000000000000FF) | \
    (((uint64_t)(x) >> 40) & 0x000000000000FF00) | \
    (((uint64_t)(x) >> 24) & 0x0000000000FF0000) | \
    (((uint64_t)(x) >> 8 ) & 0x00000000FF000000) | \
    (((uint64_t)(x) << 8 ) & 0x000000FF00000000) | \
    (((uint64_t)(x) << 24) & 0x0000FF0000000000) | \
    (((uint64_t)(x) << 40) & 0x00FF000000000000) | \
    (((uint64_t)(x) << 56) & 0xFF00000000000000)   \
   )

volatile uint8_t  *data   = (void *) 0x7000;
volatile uint32_t *target = (void *) 0x7100;
volatile uint32_t *offset = (void *) 0x7104;
volatile uint32_t *nonce  = (void *) 0x7108;
volatile uint8_t  *start  = (void *) 0x710C;
volatile uint8_t  *found  = (void *) 0x710D;
volatile uint8_t  *done   = (void *) 0x710E;

const uint8_t blake2b_sigma[12][16] = {
   { 0,  1,  2,  3,  4,  5,  6,  7,  8,  9,  10, 11, 12, 13, 14, 15 } ,
   { 14, 10, 4,  8,  9,  15, 13, 6,  1,  12, 0,  2,  11, 7,  5,  3  } ,
   { 11, 8,  12, 0,  5,  2,  15, 13, 10, 14, 3,  6,  7,  1,  9,  4  } ,
   { 7,  9,  3,  1,  13, 12, 11, 14, 2,  6,  5,  10, 4,  0,  15, 8  } ,
   { 9,  0,  5,  7,  2,  4,  10, 15, 14, 1,  11, 12, 6,  8,  3,  13 } ,
   { 2,  12, 6,  10, 0,  11, 8,  3,  4,  13, 7,  5,  15, 14, 1,  9  } ,
   { 12, 5,  1,  15, 14, 13, 4,  10, 0,  7,  6,  3,  9,  2,  8,  11 } ,
   { 13, 11, 7,  14, 12, 1,  3,  9,  5,  0,  15, 4,  8,  6,  2,  10 } ,
   { 6,  15, 14, 9,  11, 3,  0,  8,  12, 2,  13, 7,  1,  4,  10, 5  } ,
   { 10, 2,  8,  4,  7,  6,  1,  5,  15, 11, 9,  14, 3,  12, 13, 0  } ,
   { 0,  1,  2,  3,  4,  5,  6,  7,  8,  9,  10, 11, 12, 13, 14, 15 } ,
   { 14, 10, 4,  8,  9,  15, 13, 6,  1,  12, 0,  2,  11, 7,  5,  3  } };
   
const uint64_t iv[16] = {
  0x6a09e667f2bdc928, 0xbb67ae8584caa73b, 0x3c6ef372fe94f82b, 0xa54ff53a5f1d36f1,
   0x510e527fade682d1, 0x9b05688c2b3e6c1f, 0x1f83d9abfb41bd6b, 0x5be0cd19137e2179,
   0x6a09e667f3bcc908, 0xbb67ae8584caa73b, 0x3c6ef372fe94f82b, 0xa54ff53a5f1d36f1,
   0x510e527fade68281, 0x9b05688c2b3e6c1f, 0xe07c265404be4294, 0x5be0cd19137e2179
};

int main(){
  int i, j;
   uint64_t m[16], v[16];
  uint32_t count = 0;

  while(!start){
    // wait until host start e-core
  }

  m[0] = *(data + 0);
   m[1] = *(data + 8);
   m[2] = *(data + 16);
   m[3] = *(data + 24);
   m[4] = (uint64_t)((*offset) + (e_get_coreid() << 20));
   m[5] = *(data + 40);
   m[6] = *(data + 48);
   m[7] = *(data + 56);
   m[8] = *(data + 64);
   m[9] = *(data + 72);
   m[10] = m[11] = m[12] = m[13] = m[14] = m[15] = 0;

  while(*done){
    for(i=0; i<16; i++){
      v[i] = iv[i];
    }

    ROUND( 0 );
    ROUND( 1 );
    ROUND( 2 );
    ROUND( 3 );
    ROUND( 4 );
    ROUND( 5 );
    ROUND( 6 );
    ROUND( 7 );
    ROUND( 8 );
    ROUND( 9 );
    ROUND( 10 );
    ROUND( 11 );

    (*found) = (SWAP8(0x6a09e667f2bdc928 ^ v[0] ^ v[8]) <= (*target));
    if(*found){
      (*nonce) = SWAP4((uint32_t)m[4]);
      (*done) = 1;
    }else{
      m[4]++;
      count++;
      if(count>>20){
        (*done) = 1;
      }
    }
  }

  return 0;
}


Last edited by jimmystone on Sun Mar 12, 2017 1:17 am, edited 1 time in total.
jimmystone
 
Posts: 48
Joined: Tue Sep 24, 2013 12:09 pm

Re: e-gcc virtual memory exhausted

Postby jar » Sat Mar 11, 2017 4:31 pm

I haven't used the SREC object code format in some time. You should be able to use the ELF format. Maybe that has something to do with it?
User avatar
jar
 
Posts: 265
Joined: Mon Dec 17, 2012 3:27 am

Re: e-gcc virtual memory exhausted

Postby jimmystone » Sun Mar 12, 2017 1:20 am

jar wrote:I haven't used the SREC object code format in some time. You should be able to use the ELF format. Maybe that has something to do with it?


I have update my script in case confuse others. The problem I met is using the e-gcc compiling my kernel.
And it's output is ELF format, I think.
jimmystone
 
Posts: 48
Joined: Tue Sep 24, 2013 12:09 pm

Re: e-gcc virtual memory exhausted

Postby jar » Sun Mar 12, 2017 4:59 am

I dug in and spent about an hour on this. It's not looking good for you if you want optimizations (but -O0 will compile)

First, change the variable "start" to something else ("notstart"). There's another internal function called start and this is causing a problem.

Second, it looks like this code results in an internal compiler bug and/or memory leak within GCC. This is a serious problem. You should email Ola.

The compilation is stuck in cc1 with gradually increasing memory usage. I only let it run a few minutes. It may eventually return, but it sounds like you let it go until you ran out of memory.

As far as I can tell, this is the offending line:
Code: Select all
(*found) = (SWAP8(0x6a09e667f2bdc928 ^ v[0] ^ v[8]) <= (*target));


So I broke that out to just:
Code: Select all
uint64_t x = 0x6a09e667f2bdc928 ^ v[0] ^ v[8];


Then I tried to perform the uint32_t equivalent code:
Code: Select all
uint32_t v0a = (uint32_t)(v[0] >> 32);
uint32_t v8a = (uint32_t)(v[8] >> 32);
uint32_t v0b = (uint32_t)(*(((uint32_t*)&v[0])+1));  // This and the next line seem to be problems
uint32_t v8b = (uint32_t)(*(((uint32_t*)&v[8])+1));
uint64_t xa = 0x6a09e667;
uint32_t xb = 0xf2bdc928;
xa ^= v0a; xa ^= v8a;
xb ^= v0b; xb ^= v8b;
uint64_t x = (xa << 32) + xb;


This will hang just like the others. Sorry I couldn't help more.
User avatar
jar
 
Posts: 265
Joined: Mon Dec 17, 2012 3:27 am

Re: e-gcc virtual memory exhausted

Postby jimmystone » Wed Mar 15, 2017 1:02 pm

Hi, Jar

I finally found out that, this below used too much code space, and cause the e-gcc fail. I have rewrote this marco into a reuse function
Everything goes well again, Thanks for your help
Code: Select all
    ROUND( 0 );
    ROUND( 1 );
    ROUND( 2 );
    ROUND( 3 );
    ROUND( 4 );
    ROUND( 5 );
    ROUND( 6 );
    ROUND( 7 );
    ROUND( 8 );
    ROUND( 9 );
    ROUND( 10 );
    ROUND( 11 );
jimmystone
 
Posts: 48
Joined: Tue Sep 24, 2013 12:09 pm

Re: e-gcc virtual memory exhausted

Postby olajep » Fri Mar 17, 2017 6:32 pm

You could try to create a swap file on the sdcard (slow):
https://wiki.archlinux.org/index.php/sw ... e_creation

... or create a swap partition backed by zram:
https://askubuntu.com/questions/625405/ ... -partition

// Ola
_start = 266470723;
olajep
 
Posts: 118
Joined: Mon Dec 17, 2012 3:24 am
Location: Sweden


Return to Programming Q & A

Who is online

Users browsing this forum: No registered users and 4 guests