cancel
Showing results for 
Search instead for 
Did you mean: 

Archives Discussions

FangQ
Adept I

seg fault when allocating 1D array or stream

is this related to the maximum stream size?

I am running a Monte-Carlo simulation using Brook+. The code seems to be working ok, except I can not use larger domains. My machine has a Radeon 4650 video card with 512M memory. I compiled the program using the newly released SDK 1.4 on a Ubuntu 8.10 desktop.

The following code contains the minimum lines to replicate the problem:

#include <stdio.h>

// dimension of the target domain
#define DIMX 128
#define DIMY 128
#define DIMZ 128
#define DIMXYZ (DIMX*DIMY*DIMZ)
#define MAX_PROP   256

int main (int argc, char *argv[]) {
     int    nthread=1024;   /*the default total thread number*/

     // comment the following two lines will save it from crash
     uchar  media[DIMXYZ];
     float  field[DIMXYZ];

     uchar  gmedia<DIMXYZ>;
     float  gfield<DIMXYZ>;
     float4 gPpos<nthread>;
     float4 gPdir<nthread>;
     float3 gPlen<nthread>;
     float3 gPseed<nthread>;
    
     float4 gPpos2<nthread>;
     float4 gPdir2<nthread>;
     float3 gPlen2<nthread>;
     float3 gPseed2<nthread>;
    
     float3 gproperty<MAX_PROP>;

     return 0;
}

Let DIMX=DIMY=DIMZ=d, and when d<=118, the program runs ok. But as long as d>118, the program will crash with "Segmentation fault".

If I comment out the two array definition, i.e. "uchar  media[DIMXYZ];" and "float  field[DIMXYZ];" then it does not crash any more.

It is strange that if you count the total stream memory, its only around 10M, far less than the memory on the video card. Is this a bug of the compiler? or I missed something in the definition?

thanks

0 Likes
4 Replies
FangQ
Adept I

a follow up, if I do malloc/free (see below), the code will run fine, but just can not declare as array.

     float * field;
     field=(float*)malloc(sizeof(float)*DIMXYZ);
     free(field);

a side question,if I change the value of the variable governing the stream dimension after the declearations of streams, will the stream be allocated with the updated values?

     int    nthread=1024;   /*the default total thread number*/
     float4 gPpos<nthread>;
     nthread=atoi(argv[1]);

 

0 Likes

The segmentation fault you get is becuse of stack overflow. You are trying to allocate ~10MB memory on stack, that is not possible.

0 Likes

thank you for pointing this out, I did not pay attention to the stack limit on my machine.

0 Likes

a side question,if I change the value of the variable governing the stream dimension after the declearations of streams, will the stream be allocated with the updated values?

 

     int    nthread=1024;   /*the default total thread number*/      float4 gPpos;      nthread=atoi(argv[1]);

 

 

No, this won't be possible, declaration of stream allocates memory on GPU and it's not possible to change it's size by tracking the change in dimension variable declared on application side.



0 Likes