4 Replies Latest reply on Mar 15, 2009 4:17 PM by gaurav.garg

    seg fault when allocating 1D array or stream

    FangQ
      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:

      [code]#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;
      }[/code]

      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

        • seg fault when allocating 1D array or stream
          FangQ

          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]);