summaryrefslogtreecommitdiffstats
path: root/moveArrays.cu
diff options
context:
space:
mode:
Diffstat (limited to 'moveArrays.cu')
-rw-r--r--moveArrays.cu37
1 files changed, 37 insertions, 0 deletions
diff --git a/moveArrays.cu b/moveArrays.cu
new file mode 100644
index 0000000..de41014
--- /dev/null
+++ b/moveArrays.cu
@@ -0,0 +1,37 @@
+#include <stdio.h>
+#include <assert.h>
+#include <cuda.h>
+
+int main(void)
+{
+ float *a_h, *b_h; // pointers to host memory
+ float *a_d, *b_d; // pointers to device memory
+ int N = 14;
+ int i;
+ // allocate arrays on host
+ a_h = (float *)malloc(sizeof(float)*N);
+ b_h = (float *)malloc(sizeof(float)*N);
+ // allocate arrays on device
+ cudaMalloc((void **) &a_d, sizeof(float)*N);
+ cudaMalloc((void **) &b_d, sizeof(float)*N);
+ // initialize host data
+ for (i=0; i<N; i++) {
+ a_h[i] = 10.f+i;
+ b_h[i] = 0.f;
+ }
+ // send data from host to device: a_h to a_d
+ cudaMemcpy(a_d, a_h, sizeof(float)*N, cudaMemcpyHostToDevice);
+ // copy data within device: a_d to b_d
+ cudaMemcpy(b_d, a_d, sizeof(float)*N, cudaMemcpyDeviceToDevice);
+ // retrieve data from device: b_d to b_h
+ cudaMemcpy(b_h, b_d, sizeof(float)*N, cudaMemcpyDeviceToHost);
+ // check result
+ for (i=0; i<N; i++) {
+ printf("%.2f\n", a_h[i]);
+ printf("%.2f\n\n", b_h[i]);
+ assert(a_h[i] == b_h[i]);
+ }
+ // cleanup
+ free(a_h); free(b_h);
+ cudaFree(a_d); cudaFree(b_d);
+}