-3

I want to simulate mass distribution code for 10^10 particle of unit mass.

But this code works only if number of particle is 10^8.

long par = 85000000;
float p[85000000][2];

and this shows error if size goes to 10^9 or 10^10

  • "Relocation truncated to fit"

that is

long par = 85000000;
float p[85000000][2];

how to find the maximum possible indexing range of float array?

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <sys/time.h>

long double net[4096][4096];
int max = 4000;
long par = 85000000;
float p[85000000][2];

int main(int argc, char *argv[]) {
    int i;
    for( i = 0; i < par/1000; i++) {
        p[i][0]= ((long double)rand()/(long double)(RAND_MAX) * (long double)max);
        p[i][1]= ((long double)rand()/(long double)(RAND_MAX) * (long double)max);
    }

    clock_t begin = clock();

    for ( i = 0; i < par/1000; i++) {
        double x = p[i][0];
        double y = p[i][1];

        int left = (int)floor(x);
        int right = left + 1;

        if ( left >= 4095)
            printf("out of bound left is %d/n", left);

        int bottom = (int)floor(y);
        int top = bottom +1;

        double fL = x - left;
        double fR = 1 - fL;

        double fB = y - bottom;
        double fT = 1 - fB;

        net[left][bottom]   =   net[left][bottom]   +( fT * fR ) ;
        net[right][bottom]  =   net[right][bottom]  +( fT * fL ) ;
        net[left][top]      =   net[left][top]      +( fB * fR ) ;
        net[right][top]     =   net[right][top]     +( fB * fL ) ;
    }

    clock_t close = clock();

    FILE *f = fopen("file.txt", "a");
    if (!f) {
            printf("Error opening file!\n");
            exit(1);
    }

    fprintf (f,"Computation time for grid size %d X %d and %ld particle is "
        "%4.6lf ,\n",max, max,par,(double)(close-begin) );

    fclose(f);
}
Deduplicator
  • 44,692
  • 7
  • 66
  • 118

1 Answers1

2

You are hitting the stack limit with your allocation, you should use malloc, which does the allocation on the heap (a.k.a. dynamic) memory. Then you can use as much memory, as your OS allows you to.

You can use it with the following syntax:

float p[];
p = malloc(85000000 * sizeof(float));

There is even a Wikipedia article about malloc and the similar functions.

As a side-note: this type of error is the namesake of this very site :)

meskobalazs
  • 15,741
  • 2
  • 40
  • 63