Thread: running through a loop using pointers

  1. #31
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is your current code?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #32
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by boy View Post
    Oh, that was my mistake haha; I was shrinking the code for easy reading on here. My whole code is actually unrooling the loop 16x with 10 parallels.

    I switched the int to a double now my code works. But I am running into the problem where my code runs off the end of the array and throws a segmentation fault. I tried adding the two if statements to catch it, but it is still getting the fault, is my if statement wrong?
    Code:
     
    double *max = malloc(ARRAY_SIZE * sizeof(*max));	
    				
    do{
    	if((max-array) < 1000)
    	{
    		sum = sum + array[0] + array[1] + array[2] + array[3] + array[ 4] + array[5] + array[6] + array[7] + array[8] + array[9] +  array[10] + array[11] + array[12] + array[13] + array[14] + array[15] ;
    		array += 16;
    	}
    	else if ((max-array) < 30)
    	{
    		sum = sum + array[0] + array[1] + array[2];
    		array += 3;
    	}
    	else
    	{
    		sum = sum + array[0] + array[1] + array[2] + array[3] + array[ 4] + array[5] + array[6] + array[7] + array[8] + array[9] +  array[10] + array[11] + array[12] + array[13] + array[14] + array[15] ;  
    		sum = sum + array[16] + array[17] + array[18] + array[19] + array[20] + array[21] + array[22] + array[23] + array[24] + array[25] + array[26] + array[27] + array[28] + array[29] + array[30] + array[31]; 
    		sum = sum + array[32] + array[33] + array[34] + array[35] + array[36] + array[37] + array[38] + array[39] + array[40] + array[41] + array[42] + array[43] + array[44] + array[45] + array[46] + array[47]; 
    		sum = sum + array[48] + array[49] + array[50] + array[51] + array[52] + array[53] + array[54] + array[55] + array[56] + array[57] + array[58] + array[59] + array[60] + array[61] + array[62] + array[63];
    		sum = sum + array[64] + array[65] + array[66] + array[67] + array[68] + array[69] + array[70] + array[71] + array[72] + array[73] + array[74] + array[75] + array[76] + array[77] + array[78] + array[79];
    		sum = sum + array[80] + array[81] + array[82] + array[83] + array[84] + array[85] + array[86] + array[87] + array[88] + array[89] + array[90] + array[91] + array[92] + array[93] + array[94] + array[95];
    		sum = sum + array[96] + array[97] + array[98] + array[99] + array[100] + array[101] + array[102] + array[103] + array[104] + array[105] + array[106] + array[107] + array[108] + array[109] + array[110] + array[111];
    		sum = sum + array[112] + array[113] + array[114] + array[115] + array[116] + array[117] + array[118] + array[119] + array[120] + array[121] + array[122] + array[123] + array[124] + array[125] + array[126] + array[127];
    		sum = sum + array[128] + array[129] + array[130] + array[131] + array[132] + array[133] + array[134] + array[135] + array[136] + array[137] + array[138] + array[139] + array[140] + array[141] + array[142] + array[143];
    		sum = sum + array[144] + array[145] + array[146] + array[147] + array[148] + array[149] + array[150] + array[151] + array[152] + array[153] + array[154] + array[155] + array[156] + array[157] + array[158] + array[159];
    										
    		array += 160;
    		}	
    }	
    while(array < max);
    Dear god man, read up on the use of loops...

    You don't have to answer this but... about your C textbook... are you reading it in study fashion or trying to treate it as some kind of reference book for your own version of C... There is only one way to successfully learn C... Take your textbook, open it to page 1, read the words sequentially repeating as necessary, until you understand... Type up the examples and run them, play with the code, make changes, repeat as necessary until you understand... Do the quizzes, work them on your own, figure them out and get the code to work... now turn to page 2... then page 3 and so on until you reach the end of the book... THEN you can try writing code on your own. No offense, but what you got going on is simply not it...

  3. #33
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    It is because you are still going beyond the bounds of your array. Have you traced through your code? The value you change array by varies as the loop progresses, thus you cannot just subtract one value for your termination condition. You are going to have to adjust your logic accordingly.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  4. #34
    Registered User
    Join Date
    Aug 2011
    Posts
    16
    This is my current full code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    // the code you submit must have these two values
    #define N_TIMES     600000
    #define ARRAY_SIZE   10000
    
    int main (void)
    {
        double  *array = calloc(ARRAY_SIZE, sizeof(double));
        double  sum = 0;
        int     i;
    
        printf("CS201 - Asgmt 4 - Albert Le\n");
    
        for (i = 0; i < N_TIMES; i++) {
    
            // do not change anything above this comment,
            // except for your name
    				
    				int sum0 = 0;
    				int sum1 = 0; 
    				int sum2 = 0; 
    				int sum3 = 0;
    				int sum4 = 0;
    				int sum5 = 0; 
    				int sum6 = 0;
    				int sum7 = 0; 
    				int sum8 = 0; 
    				int sum9 = 0;
    				double *end = array +ARRAY_SIZE -160 ;	
    				
    				do{
    
    							sum0 = sum + array[0] + array[1] + array[2] + array[3] + array[ 4] + array[5] + array[6] + array[7] + array[8] + array[9] +  array[10] + array[11] + array[12] + array[13] + array[14] + array[15] ;  
    							sum1 = sum0 + array[16] + array[17] + array[18] + array[19] + array[20] + array[21] + array[22] + array[23] + array[24] + array[25] + array[26] + array[27] + array[28] + array[29] + array[30] + array[31]; 
    							sum2 = sum1+ array[32] + array[33] + array[34] + array[35] + array[36] + array[37] + array[38] + array[39] + array[40] + array[41] + array[42] + array[43] + array[44] + array[45] + array[46] + array[47]; 
    							sum3 = sum2 + array[48] + array[49] + array[50] + array[51] + array[52] + array[53] + array[54] + array[55] + array[56] + array[57] + array[58] + array[59] + array[60] + array[61] + array[62] + array[63];
    							 sum4 = sum3 + array[64] + array[65] + array[66] + array[67] + array[68] + array[69] + array[70] + array[71] + array[72] + array[73] + array[74] + array[75] + array[76] + array[77] + array[78] + array[79];
    							sum5 = sum4+ array[80] + array[81] + array[82] + array[83] + array[84] + array[85] + array[86] + array[87] + array[88] + array[89] + array[90] + array[91] + array[92] + array[93] + array[94] + array[95];
    							sum6 = sum5 + array[96] + array[97] + array[98] + array[99] + array[100] + array[101] + array[102] + array[103] + array[104] + array[105] + array[106] + array[107] + array[108] + array[109] + array[110] + array[111];
    							sum7 = sum6 + array[112] + array[113] + array[114] + array[115] + array[116] + array[117] + array[118] + array[119] + array[120] + array[121] + array[122] + array[123] + array[124] + array[125] + array[126] + array[127];
    							sum8 = sum7 + array[128] + array[129] + array[130] + array[131] + array[132] + array[133] + array[134] + array[135] + array[136] + array[137] + array[138] + array[139] + array[140] + array[141] + array[142] + array[143];
    							sum9 = sum8 + array[144] + array[145] + array[146] + array[147] + array[148] + array[149] + array[150] + array[151] + array[152] + array[153] + array[154] + array[155] + array[156] + array[157] + array[158] + array[159]; 
    							
    							sum = sum9;
    							
    							array = array + 160;
    				}	
    				while(array < end );
    									
    				
            // do not change anything below this comment
    
            }
        return 0;
    }
    Sorry if I didn't answer this earlier, but my goal for this assignment is to optimize the program that is the reason for all the unrool and splits.

    I just went through my debugger several times again and finally found out that my array was not incrementing at all, it was staying at the same start point, I am guessing that is why my code is getting a segmentation fault. I don't know how else I would be able to increment array otherwise, could someone help?

  5. #35
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by boy
    Sorry if I didn't answer this earlier, but my goal for this assignment is to optimize the program that is the reason for all the unrool and splits.
    Right, so basically the program is supposed to sum the elements of the array on each iteration of the outer loop, and you are trying to perform loop unrolling to optimise because that is what your instructor told you to do. Fair enough.

    Keep in mind:
    • If you want to unroll the loop to access 160 elements per iteration, then you must make sure that the number of elements in the array is congruent to 160. The array has 10000 elements, and 10000 != 0 (mod 160), so you are accessing the array out of bounds on the last iteration. Clarify with your instructor as to whether ARRAY_SIZE will be changed. If not, perhaps you should unroll the loop to access 100 or 200 elements per iteration.
    • You don't need sum0 to sum9. In fact, it may be wrong to use them since they are of type int and you want to sum elements of type double. Just re-use sum.
    • Use a for loop. A do while loop does not fit the loop structure here.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #36
    Registered User
    Join Date
    Aug 2011
    Posts
    16
    Thanks for your help laserlight, all variables values will be tested as is for the grade. I am actually even allowed to alter the outer loop at all.

    I changed all the sums to reuse sum, and change the iterations to be of 100, but I still get a segmentation fault.

  7. #37
    Registered User
    Join Date
    Aug 2011
    Posts
    16
    Thanks everyone for your help, mission accomplished and submitted.

    boy

  8. #38
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Good to hear that. Out of curiosity, what were your final fixes to make your program work?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #39
    Registered User
    Join Date
    Aug 2011
    Posts
    16
    I just created a new pointer to point to the array

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    // the code you submit must have these two values
    #define N_TIMES     600000
    #define ARRAY_SIZE   10000
    
    int main (void)
    {
        double  *array = calloc(ARRAY_SIZE, sizeof(double));
        double  sum = 0;
        int     i;
    
        printf("CS201 - Asgmt 4 - Albert Le\n");
    
        for (i = 0; i < N_TIMES; i++) {
    
            // do not change anything above this comment,
            // except for your name
    				
    				//int j = 0;
    				double *a = malloc(sizeof(double));
    				a = array;
    				double *end = array + ARRAY_SIZE;
    				
    				for(;a < end; a += 100)
    				{
    						sum +=  a[0] + a[1] + a[2] + a[3] + a[ 4] + a[5] + a[6] + a[7] + a[8] + a[9] +  a[10] + a[11] + a[12] + a[13] + a[14] + a[15] ;  
    						sum +=  a[16] + a[17] + a[18] + a[19] + a[20] + a[21] + a[22] + a[23] + a[24] + a[25] + a[26] + a[27] + a[28] + a[29] + a[30] + a[31]; 
    						sum +=  a[32] + a[33] + a[34] + a[35] + a[36] + a[37] + a[38] + a[39] + a[40] + a[41] + a[42] + a[43] + a[44] + a[45] + a[46] + a[47]; 
    						sum +=  a[48] + a[49] + a[50] + a[51] + a[52] + a[53] + a[54] + a[55] + a[56] + a[57] + a[58] + a[59] + a[60] + a[61] + a[62] + a[63];
    						sum += a[64] + a[65] + a[66] + a[67] + a[68] + a[69] + a[70] + a[71] + a[72] + a[73] + a[74] + a[75] + a[76] + a[77] + a[78] + a[79];
    						sum += a[80] + a[81] + a[82] + a[83] + a[84] + a[85] + a[86] + a[87] + a[88] + a[89] + a[90] + a[91] + a[92] + a[93] + a[94] + a[95] + a[96] + a[97] + a[98] + a[99];								
    				}
            // do not change anything below this comment
    
            }
        return 0;
    }

  10. #40
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    What about using another loop for the 100 manual sums ?

  11. #41
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Oh, good thing I asked. This:
    Code:
    double *a = malloc(sizeof(double));
    a = array;
    should be:
    Code:
    double *a = array;
    EDIT:
    Quote Originally Posted by manasij7479
    What about using another loop for the 100 manual sums ?
    That would be pointless since one might as well make the inner loop to do just that, which of course means no loop unrolling.
    Last edited by laserlight; 08-19-2011 at 12:43 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #42
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by manasij7479 View Post
    What about using another loop for the 100 manual sums ?
    Or boy, since your professor wants pre-emptively optimized code for some reason, it would be funny if you used Duff's device.

  13. #43
    Registered User
    Join Date
    Aug 2011
    Posts
    16
    Good thing I posted my code on here for you to see laserlight, cut .6 seconds off of my time with just that change, and was able to resubmit it with 3 minutes to spare. Thank you laserlight.
    Final time was 5.1 seconds average over 5 runs, which before was 5.65 average.

    lol whiteflags, i'm not sure how he would grade me if i implemented that

    Anyways, Thanks again for all your guys help, and quick replies

  14. #44
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Or you could have just told your teacher to compile with -O3 and saved yourself the trouble.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. For loop not running properly
    By Todd88 in forum C++ Programming
    Replies: 21
    Last Post: 04-04-2008, 05:27 PM
  2. Running a program in a loop
    By Robert_Sitter in forum C# Programming
    Replies: 7
    Last Post: 01-27-2006, 10:09 AM
  3. User input while loop is running
    By two31d in forum C++ Programming
    Replies: 7
    Last Post: 11-30-2005, 05:28 PM
  4. functions stops loop from running
    By a1pro in forum C++ Programming
    Replies: 4
    Last Post: 04-22-2005, 02:02 PM
  5. Running triangles through loop
    By shanedudddy2 in forum C++ Programming
    Replies: 2
    Last Post: 01-14-2003, 12:59 AM