Skip to content

Instantly share code, notes, and snippets.

@zoq
Created March 28, 2022 16:32
Show Gist options
  • Save zoq/11101e2022d11527854a263061a3939b to your computer and use it in GitHub Desktop.
Save zoq/11101e2022d11527854a263061a3939b to your computer and use it in GitHub Desktop.
copy-test.hpp
mat src(4, 1);
src.randn();
mat dst(4, 1);
dst.randu();
src.print();
dst.print();
// Copy the first three elements from src to dst.
cudaMemcpy((double*)dst.get_dev_mem().cuda_mem_ptr, src.get_dev_mem().cuda_mem_ptr, sizeof(double) * 3, cudaMemcpyDeviceToDevice);
// Copy first element from src to last element in dst.
cudaMemcpy((double*)dst.get_dev_mem().cuda_mem_ptr + 3, src.get_dev_mem().cuda_mem_ptr, sizeof(double) * 1, cudaMemcpyDeviceToDevice);
src.print();
dst.print();
@heisenbuug
Copy link

heisenbuug commented Mar 28, 2022

X1 2 3    // size
0 1 2 
1 2 3 

X2 3 3    // size
0 1 2 
1 2 3 
2 3 4 

Result 5 3
0 3 2 
1 0 3 
1 1 2 
2 2 3 
2 1 4

And below is my code

cudaMemcpy((eT1*)out.cuda_mem_ptr, mem1.cuda_mem_ptr, sizeof(eT1) * 6, cudaMemcpyDeviceToDevice);
cudaMemcpy((eT1*)out.cuda_mem_ptr+6, mem2.cuda_mem_ptr, sizeof(eT1) * 9, cudaMemcpyDeviceToDevice);

@heisenbuug
Copy link

Okay I got what's happening here, it is basically column-wise but look at one whole matrix at a time, so

0 2 4
1 3 5

becomes

0 5
1
2
3
4

and rest is getting filled in the same fashion.

0 3 6
1 4 7
2 5 8

gets filled as

0 5 4
1 0 5
2 1 6
3 2 7
4 3 8

@zoq
Copy link
Author

zoq commented Mar 28, 2022

that is right

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment