Search code examples
cudaswap

CUDA - swapping two float* arrays


I have two matrices of type float*, size B*B, stored in a flat array. I want to swap them after I complete a computation.

This is the code I have now:

    Btemp = B0;
    B0 = B2;
    B2 = Btemp;*

When this code executes, B0 and B2's elements remain unchanged. I expect them to be swapped with one another. Why is this, and what's the best way to perform this swap?


Solution

  • You need to swap the sequence of bytes in memory addresses B0 and B2. What you are doing is swapping the addresses that variables B0 and B2 store. Probably.

    something like this;

    for (i=0; i < length(B0); ++i) {
       float temp = B0[i];
       B0[i] = B2[i];
       B2[i] = temp;
    }
    

    This is assuming that they have the same length (also pseudo code). With cuda you can do a much better implementation that uses the architecture.