3

What is 'stride' in C and how can it be used?

user438383
  • 5,716
  • 8
  • 28
  • 43
codester_09
  • 5,622
  • 2
  • 5
  • 27
  • 2
    In a bitmap image, the stride is the actual width of the array, which can be greater than the width of the image. – Weather Vane Jun 01 '22 at 19:00
  • @WeatherVane Can you show me how to use this in my code? – codester_09 Jun 01 '22 at 19:01
  • I as just looking to see if the row width *used* is less than the array width *defined*. – Weather Vane Jun 01 '22 at 19:02
  • 2
    Perhaps the teacher is referring to the way the array access is stepping through memory. The loop is per column, row, which will perhaps have more cache misses than per row, column. So switch the nested loop sequence so that `for (long j = 0; j < ROWS; j++)` is the outer loop. – Weather Vane Jun 01 '22 at 19:05
  • 2
    Well, if that is an exercise for a programming class, maybe this comes down to Memoization (saving the result after the first calculation for later iterations)? – PhilMasteG Jun 01 '22 at 19:05
  • @PhilMasteG My teacher gives me an assignment inside there are two projects I get the first one but this one is hard for me. – codester_09 Jun 01 '22 at 19:07
  • 1
    Well, actually thinking about it, you never said, that your teacher said to even run the code. If you google "C programming stride", you find a definition on the microsoft docs about "stride" and how to determine it. So, actually no need to optimize I guess? – PhilMasteG Jun 01 '22 at 19:11
  • @KamilCuk They say don't remove it. – codester_09 Jun 01 '22 at 19:11
  • 1
    If you don't know what “stride” is in this context, **[look it up](https://en.wikipedia.org/wiki/Stride)**. This is computing, and it's about an array, so the meaning is the [stride of an array](https://en.wikipedia.org/wiki/Stride_of_an_array). – Gilles 'SO- stop being evil' Jun 01 '22 at 19:18
  • @codester_09 please do not edit the answer and thank the person into the question, it is not needed. Also whether or not you know C and learnt python first is irrelevant to the question. – user438383 Jun 23 '23 at 10:26

1 Answers1

8

Generally, stride is the distance steps take through something.

In the addition routine, we have these loops:

for (long i = 0; i < COLS; i++)
        for (long j = 0; j < ROWS; j++) {
            sum += table[j][i];
        }

In successive iterations of the innermost loop with j equal to x in the first iteration, one iteration accesses table[x][i], and the next accesses table[x+1][i]. The distance between these two accesses is the size of one table[j], which is COLS (2000) elements of short (likely two bytes), so likely 4000 bytes. So the stride is 4000 bytes.

This is generally bad for the cache memory on typical processors, as cache memory is designed mostly for memory accesses that are close to each other (small strides). This is the cause of the program’s slow performance.

Since the operation in the loop, sum += table[j][i];, is independent of the order it is executed in for all the i and j, we can easily remedy this problem by swapping the two for statements:

    for (long j = 0; j < ROWS; j++)
        for (long i = 0; i < COLS; i++)
            sum += table[j][i];

Then successive iterations of the innermost loop will access table[j][x] and table[j][x+1], which have a stride of one short, likely two bytes.

On my system, the program runs about twenty times faster with this change.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
  • Thanks for the information your code is made my code from 12s to 8s. can you make my code more faster. – codester_09 Jun 01 '22 at 19:31
  • 1
    A little bit faster might be: `long addition(short table[][COLS]) { long sum = 0; short *p = &table[0][0]; short *end = &table[ROWS-1][COLS-1]; while (p < end+1) {sum += *p++;} return sum;}`. And increase the optimization level for the compiler, e.g. gcc -O2 – Franck Jun 01 '22 at 19:42