What is your current code?
What is your current code?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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...
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.
This is my current full code:
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.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; }
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?
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.Originally Posted by boy
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.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.
Thanks everyone for your help, mission accomplished and submitted.
boy
Good to hear that. Out of curiosity, what were your final fixes to make your program work?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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; }
What about using another loop for the 100 manual sums ?
Oh, good thing I asked. This:
should be:Code:double *a = malloc(sizeof(double)); a = array;
EDIT:Code:double *a = array;
That would be pointless since one might as well make the inner loop to do just that, which of course means no loop unrolling.Originally Posted by manasij7479
Last edited by laserlight; 08-19-2011 at 12:43 AM.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Or boy, since your professor wants pre-emptively optimized code for some reason, it would be funny if you used Duff's device.
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
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.