Previous | Next --- Slide 45 of 52
Back to Lecture Thumbnails
cwswanso

So an example really helped me out here. If you have your input be [1.2, 0.2, 4.1, 3.5], and your indices be [1,3,0,2], then I believe gather would result with an output of [0.2, 3.5, 1.2, 4.1], and scatter would result with [4.1, 1.2, 3.5, 0.2].

So in my mind gather takes the set of indices and says, okay at index i I want the input element from indices[i], whereas scatter would say take the input from index i and put it in the output at index indices[i]. (now that I write this, I realize it's probably not much better than the code... but the example might help!)

kayvonf

Question: Another way to understand gather and scatter is for someone to want to write sequential C-code for:

void gather(float* src, int* indices, float* dst);
void scatter(float* src, int* indices, float* dst); 
cwswanso

I'll give it a go! Assuming they all have equal length N

void gather(float* src, int* indices, float* dst) {
    for (int i = 0; i < N; i++) {
        dst[i] = src[indices[i]];
    }
}

void scatter(float* src, int* indices, float* dst) {
    for (int i = 0; i < N; i++) {
        dst[indices[i]] = src[i];
    }
}
cardiff

Each of these programs seems to use only one of either gather or scatter. Is there an example that would use both operations?