2015-02-04 2 views
-3

단위 질량의 10^10 입자에 대한 질량 분포 코드를 시뮬레이트하고 싶습니다.float 배열의 최대 인덱싱 범위는 무엇입니까?

그러나이 코드는 입자 수가 10^8 인 경우에만 작동합니다.

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

및 크기로 진행이 에러를 나타낸다^9 10 10^10

하다 "재배치 맞게 절단"

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

방법 float 배열의 가능한 최대 인덱싱 범위를 찾으려면?

#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); 
} 
+0

namesake입니까? 어떤 이유로'malloc '을 사용하지 않습니까? – usr2564301

+4

스택 제한을 맞추기 위해'malloc'과 힙 메모리를 사용합니다. – meskobalazs

+0

@meskobalazs 응답 시간! – Quentin

답변

2

당신은 당신의 할당과 스택 제한을 타격, 당신은 힙 (일명, 동적) 메모리 할당을 수행 malloc을 사용해야합니다. 그런 다음 OS에서 허용하는만큼 많은 메모리를 사용할 수 있습니다.

다음과 같은 구문을 사용할 수 있습니다 : 심지어 Wikipedia article에 대한 malloc과 유사한 기능이

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

. 사이드 참고로

는 이러한 유형의 오류는 바로이 사이트 :) 할 때까지 더 이상 적합을 배 이상 다른

+0

나는 이것을 항상 C++과 혼합하여 고쳤다. – meskobalazs

+1

그는 로컬 범위가 아닌 파일 범위에서 배열을 정의했기 때문에 스택에 배치해서는 안됩니다. –

+0

@vlad_tepesch 내가 간과 한 것처럼 보입니다. 당신 말이 맞습니다. – meskobalazs