larry burns (5) Mar 20, 2010 at 8:07pm UTC
so my current program uses two array variables, each of size 250x250, and I use the ofstream function to output the stuff on Notepad. When I first ran it with each array variable of size 50x50, I didn't have any problems and it didnt take too long to run. But when I increased the size of each of them to 250x250, I'm having all sorts of problems. When I ran the simulation (each simulation runs the program) too many times, the program crashed. I then tried eliminating one of the variables, and while the program runs, it takes ridiculously long to run. It would take 23 hours! to do the number of simulations I want, whereas I think it only took 10 minutes for the 50x50 case.
I attached the relevant part of the program in this message. The parts including:
if ((x_value)<0.25)
{x--;
LeftCount++;
For the left, right, down, and up cases are negligible since they don't affect the 'island[][]' array variable
So what can I possibly do to speed up the program? Do I have no choice but to use a smaller array size?
The program is here:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
if (n_walks!=0)
{up=0, down=0, left=0, right=0;
UpCount=0, DownCount=0, LeftCount=0, RightCount=0;
x_value = static_cast <double >(rand())/RAND_MAX;
for (double f=0; f<1; f=f+.004)
{if ((x_value)<f+.004 && (x_value)>=f)
{x=-(n_size-(2*n_size*f)-1);}
}
y_value = static_cast <double >(rand())/RAND_MAX;
for (double f=0; f<1; f=f+.004)
{if ((y_value)<f+.004 && (y_value)>=f)
{y=-(n_size-(2*n_size*f)-1);}
}
cout<<"ORIGIN (" <<x<<"," <<y<<")\n" ;
ofstream LEFTposition("4sink.dat" , ios::app);
LEFTposition << "random number" << x_value << " " << y_value << "\n" ;
LEFTposition << "ORIGIN " << x << " " << y << "\n" ;
void close();
for (int i=0;i<n_walks;i++)
{int t=0;
x_value = static_cast <double >(rand())/RAND_MAX;
cout<<"random value " <<x_value<<" for x\n" ;
if ((x_value)<0.25)
{x--;
LeftCount++;
ofstream RIGHTposition("4sink.dat" , ios::app);
RIGHTposition<<"left movement for x\n" ;
void close();
{if (x==-n_size)
{x=n_size;}
else cout<< "ok " ;
}
RIGHTposition << "the particle is at " << x << " " << y << " " << "\n" ;
}
if ((x_value)<0.5 && (x_value)>=0.25)
{x++;
RightCount++;
ofstream RIGHTposition("4sink.dat" , ios::app);
RIGHTposition<<"right movement for x\n" ;
void close();
{if (x==n_size+1)
{x=1-n_size;}
else cout<< "ok " ;
}
RIGHTposition << "the particle is at " << x << " " << y << " " << "\n" ;
}
if ((x_value)<0.75 && (x_value)>=0.5)
{y--;
DownCount++;
ofstream RIGHTposition("4sink.dat" , ios::app);
RIGHTposition<<"down movement for x\n" ;
void close();
{if (y==-n_size)
{y=n_size;}
else cout<< "ok " ;
}
RIGHTposition << "the particle is at " << x << " " << y << " " << "\n" ;
}
if ((x_value)<1 && (x_value)>=0.75)
{y++;
UpCount++;
ofstream RIGHTposition("4sink.dat" , ios::app);
RIGHTposition<<"up movement for x\n" ;
void close();
{if (y==n_size+1)
{y=1-n_size;}
else cout<< "ok " ;
}
RIGHTposition << "the particle is at " << x << " " << y << " " << "\n" ;
}
for (int f=125; f>-116; f=f-10)//sinks AFTER left right up or down movement
{for (int g=125; g>-116; g=g-10)
{if (x==g && y==f)
{int t=1;
for (int a=0; a<250; a++)
{for (int b=0; b<250; b++)
{ if (island[125-y][x+124]>island[a][b])
{t=0;}
}
}
if (t!=0)
{i=n_walks;
island[125-y][x+124]++;
x_value=2;
ofstream RIGHTposition("4sink.dat" , ios::app);
RIGHTposition << x << " " << y << "\n" ;
RIGHTposition <<"x_value is " << x_value << "\n" ;
void close();
}
}
}
}
for (int f=125; f>-116; f=f-10)//after left right down or up movement
{for (int g=125; g>-116; g=g-10)
{
if (island[125-f][g+124]>0)
{
int t=0;
if (x>-124 && x<125 && y>-124 && y<125)//inner
{
if ( (island[125-y][x+124]<island[126-y][x+124]) || (island[125-y][x+124]<island[124-y][x+124]) || (island[125-y][x+124]<island[125-y][x+125]) || (island[125-y][x+124]<island[125-y][x+123]) )
{int t=1;}
}
if (x>-124 && x<125 && y==125)//boundary conds
{
if ( (island[125-y][x+124]<island[374-y][x+124]) || (island[125-y][x+124]<island[126-y][x+124]) || (island[125-y][x+124]<island[125-y][x+125]) || (island[125-y][x+124]<island[125-y][x+123]) )
{int t=1;}
}
if (x>-124 && x<125 && y==-124)
{
if ( (island[125-y][x+124]<island[-124-y][x+124]) || (island[125-y][x+124]<island[124-y][x+124]) || (island[125-y][x+124]<island[125-y][x+125]) || (island[125-y][x+124]<island[125-y][x+123]) )
{int t=1;}
}
if (y>-124 && y<125 && x==-124)
{
if ( (island[125-y][x+124]<island[124-y][x+124]) || (island[125-y][x+124]<island[126-y][x+124]) || (island[125-y][x+124]<island[125-y][x+125]) || (island[125-y][x+124]<island[125-y][x+373]) )
{int t=1;}
}
if (y>-124 && y<125 && x==125)
{
if ( (island[125-y][x+124]<island[124-y][x+124]) || (island[125-y][x+124]<island[126-y][x+124]) || (island[125-y][x+124]<island[125-y][x+125]) || (island[125-y][x+124]<island[125-y][x+123]) )
{int t=1;}
}
if (y==125 && x==-124)//conds for corners
{
if ( (island[125-y][x+124]<island[374-y][x+124]) || (island[125-y][x+124]<island[126-y][x+124]) || (island[125-y][x+124]<island[125-y][x+125]) || (island[125-y][x+124]<island[125-y][x+373]) )
{int t=1;}
}
if (y==-124 && x==-124)
{
if ( (island[125-y][x+124]<island[-124-y][x+124]) || (island[125-y][x+124]<island[124-y][x+124]) || (island[125-y][x+124]<island[125-y][x+125]) || (island[125-y][x+124]<island[125-y][x+373]) )
{int t=1;}
}
if (y==-124 && x==125)
{
if ( (island[125-y][x+124]<island[-124-y][x+124]) || (island[125-y][x+124]<island[124-y][x+124]) || (island[125-y][x+124]<island[125-y][x+123]) || (island[125-y][x+124]<island[125-y][x-125]) )
{int t=1;}
}
if (y==125 && x==125)
{
if ( (island[125-y][x+124]<island[374-y][x+124]) || (island[125-y][x+124]<island[126-y][x+124]) || (island[125-y][x+124]<island[125-y][x+123]) || (island[125-y][x+124]<island[125-y][x+125]) )
{int t=1;}
}
for (int a=0; a<250; a++)
{ for (int b=0; b<250; b++)
{ if (island[125-y][x+124]>island[a][b])
{t=0;}
}
}
if (t!=0)
{i=n_walks;
island[125-y][x+124]++;
x_value=2;
cout<<"the particle is at (" <<x<<"," <<y<<")\n" ;
cout << "height of sink " <<island[125-y][x+124]<<"\n" ;
ofstream RIGHTposition("4sink.dat" , ios::app);
RIGHTposition << x << " " << y << "\n" ;
RIGHTposition <<"x_value is " << x_value << "\n" ;
void close();}
}
}
}
}//for( i=0, i<n_walks, i++)
}//while(n_walks!=0)
kbw (1511) Mar 20, 2010 at 8:07pm UTC
My guess is that your program is writing outside of the array bounds. You need to check the index calculations to ensure that you don't index outside of the range of the array size.
depak (2) Mar 20, 2010 at 8:07pm UTC
hey larry u seems to be a experienced c++ programmer as i am a beginner so can't help u on this but want u to help me. it will be so kind of you if u can answer to my question, i asked in this forum with the heading "GETCH()".
actually i need its answer as soon as possible and no one has yet tried to solve it.
hope u will help.
bye and take care!
Zhuge (634) Mar 20, 2010 at 8:07pm UTC
@depak: Please don't derail topics with requests to look at your topic. Thanks.
writetonsharma (832) Mar 20, 2010 at 8:07pm UTC
Zhuge, he is not going to look this post again, write in his post.. :D
Zhuge (634) Mar 20, 2010 at 8:07pm UTC
Mmm, yeah, you're probably right...
larry burns (5) Mar 20, 2010 at 8:07pm UTC
i rechecked the index calculations, and fixed the errors, such as
1 2 3 4 5
if (y==125 && x==125)
{
if ( (island[125-y][x+124]<island[374-y][x+124]) || (island[125-y][x+124]<island[126-y][x+124]) || (island[125-y][x+124]<island[125-y][x+123]) || (island[125-y][x+124]<island[125-y][x+125]) )
{int t=1;}
}
since I can't have island[0][250] as the 1st row of island only runs from island[0][0] to island[0][249].
But even with the corrections, it runs only a little faster, and would still take about 16 hours!! to have 11,250 n_simulations, as my professor wants
Last edited on Mar 20, 2010 at 8:07pm UTC
Galik (42) Mar 20, 2010 at 8:07pm UTC
You seem to be opening and closing your output files all the time.
Why not open them all once at the beginning rather than in each "if" statement?
Disch (2148) Mar 20, 2010 at 8:07pm UTC
Right
Opening and closing files is slow. There's a huge amount of overhead.
You're better off opening the file once, writing everything, then closing after all the data is written.
Also I'm baffled at a lot of this code. Mainly because of the poor use of whitespace, but also because lines 116-168 appear to do absolutly nothing.
You keep redefining 't' in different scopes, which completely defeats what you're trying to do. The compiler will actually create separate variables and use the var with shortest scope:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
int t = 0; // this makes a variable "t0" and sets it to 0
if ( foobar )
{
int t = 1; // this makes another variable "t1" and sets it to 1
// note that t0 remains unchanged! it's still 0!
}
//------------------
// don't redeclare the variable every time. Just use the existing var:
int t = 0;
if ( foobar )
{
t = 1; // note: no "int"
}
larry burns (5) Mar 20, 2010 at 8:07pm UTC
thanks for the suggestions. I fixed the int t=0, and opening and closing files too much problems. I also noticed that 1 2 3 4 5 6 7 8 9
for (int a=0; a<250; a++)
{
for (int b=0; b<250; b++)
{
if (island[125-y][x+124]>island[a][b])
{t=0;}
}
}
could be shortened by setting b and a greater or equal to 250 after the 'if' statment is satisfied
But with all these changes, at the rate its currently running, the program would still take 20!!! hours to run. Aren't there more problems?
PanGalactic (778) Mar 20, 2010 at 8:07pm UTC
In that last snippet, once t=0 , you don't need to test any more. Make this a separate function and return once "t" is set. Otherwise, you are doing a heck of a lot more work than you need to.
Overall, 200 line functions are usually a good indication of a problem to start with. By breaking this into separate function, you could profile the code and know where to focus your attention.
larry burns (5) Mar 20, 2010 at 8:07pm UTC
which part should be a function? Just this part? 1 2 3 4 5 6 7 8 9
for ( int a = 0; a < 250; a++ )
{for ( int b = 0; b < 250; b++ )
{if ( island[125 - y][x + 124] > island[a][b] )
{t = 0;
b = 1000;
a = 1000;
}
}
}
Or this entire thing should be one function? Multiple functions?1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
for ( int f = 125; f > -116; f = f - 10 ) //check ORIGIN
{
for ( int g = 125; g > -116; g = g - 10 )
{
if ( x == g && y == f ) //sinks
{
int t = 1;
for ( int a = 0; a < 250; a++ )
{
for ( int b = 0; b < 250; b++ )
{
if ( island[125 - y][x + 124] > island[a][b] )
{
t = 0;
b = 1000;
a = 1000;
}
}
}
if ( t != 0 )
{
i = n_walks;
island[125 - y][x + 124]++;
x_value = 2;
cout << "falls in ORIGIN\n" ;
cout << "the particle is at (" << x << "," << y << ")\n" ;
cout << "island of sink " << island[125 - y][x + 124] << "\n" ;
RIGHTposition << "\nFALLS IN ORIGIN\n" ;
RIGHTposition << x << " " << y << "\n" ;
RIGHTposition << "x_value is " << x_value << "\n" ;
}
}
if ( island[125 - f][g + 124] > 0 ) //non-sinks
{
int t = 0;
if ( x > -124 && x < 125 && y > -124 && y < 125 ) //inner
{
if ( (island[125 - y][x + 124] < island[126 - y][x + 124]) || (island[125 - y][x + 124] < island[124 - y][x + 124])
|| (island[125 - y][x + 124] < island[125 - y][x + 125])
|| (island[125 - y][x + 124] < island[125 - y][x + 123]) )
{
t = 1;
}
}
if ( x > -124 && x < 125 && y == 125 ) //boundary conds
{
if ( (island[125 - y][x + 124] < island[374 - y][x + 124])
|| (island[125 - y][x + 124] < island[126 - y][x + 124])
|| (island[125 - y][x + 124] < island[125 - y][x + 125])
|| (island[125 - y][x + 124] < island[125 - y][x + 123]) )
{
t = 1;
}
}
if ( x > -124 && x < 125 && y == -124 )
{
if ( (island[125 - y][x + 124] < island[-124 - y][x + 124])
|| (island[125 - y][x + 124] < island[124 - y][x + 124])
|| (island[125 - y][x + 124] < island[125 - y][x + 125])
|| (island[125 - y][x + 124] < island[125 - y][x + 123]) )
{
t = 1;
}
}
if ( y > -124 && y < 125 && x == -124 )
{
if ( (island[125 - y][x + 124] < island[124 - y][x + 124])
|| (island[125 - y][x + 124] < island[126 - y][x + 124])
|| (island[125 - y][x + 124] < island[125 - y][x + 125])
|| (island[125 - y][x + 124] < island[125 - y][x + 373]) )
{
t = 1;
}
}
if ( y > -124 && y < 125 && x == 125 )
{
if ( (island[125 - y][x + 124] < island[124 - y][x + 124])
|| (island[125 - y][x + 124] < island[126 - y][x + 124])
|| (island[125 - y][x + 124] < island[125 - y][x - 125])
|| (island[125 - y][x + 124] < island[125 - y][x + 123]) )
{
t = 1;
}
}
if ( y == 125 && x == -124 ) //conds for corners
{
if ( (island[125 - y][x + 124] < island[374 - y][x + 124])
|| (island[125 - y][x + 124] < island[126 - y][x + 124])
|| (island[125 - y][x + 124] < island[125 - y][x + 125])
|| (island[125 - y][x + 124] < island[125 - y][x + 373]) )
{
t = 1;
}
}
if ( y == -124 && x == -124 )
{
if ( (island[125 - y][x + 124] < island[-124 - y][x + 124])
|| (island[125 - y][x + 124] < island[124 - y][x + 124])
|| (island[125 - y][x + 124] < island[125 - y][x + 125])
|| (island[125 - y][x + 124] < island[125 - y][x + 373]) )
{
t = 1;
}
}
if ( y == -124 && x == 125 )
{
if ( (island[125 - y][x + 124] < island[-124 - y][x + 124])
|| (island[125 - y][x + 124] < island[124 - y][x + 124])
|| (island[125 - y][x + 124] < island[125 - y][x + 123])
|| (island[125 - y][x + 124] < island[125 - y][x - 125]) )
{
t = 1;
}
}
if ( y == 125 && x == 125 )
{
if ( (island[125 - y][x + 124] < island[374 - y][x + 124])
|| (island[125 - y][x + 124] < island[126 - y][x + 124])
|| (island[125 - y][x + 124] < island[125 - y][x + 123])
|| (island[125 - y][x + 124] < island[125 - y][x - 125]) )
{
t = 1;
}
}
for ( int a = 0; a < 250; a++ )
{
for ( int b = 0; b < 250; b++ )
{
if ( island[125 - y][x + 124] > island[a][b] )
{
t = 0;
b = 1000;
a = 1000;
}
}
}
if ( t != 0 )
{
i = n_walks;
island[125 - y][x + 124]++;
x_value = 2;
cout << "falls in ORIGIN\n" ;
cout << "the particle is at (" << x << "," << y << ")\n" ;
cout << "island of sink " << island[125 - y][x + 124] << "\n" ;
ios::app);
RIGHTposition << "\nFALLS IN ORIGIN\n" ;
RIGHTposition << x << " " << y << "\n" ;
RIGHTposition << "x_value is " << x_value << "\n" ;
}
}
}
}
This topic is archived - New replies not allowed.