50aac757d032ae6a0a5fbd53ab148831cd712204
[parallel_computing.git] / src / cuda_by_example / cudaMallocAndMemcpy.cu
1 /*
2  * Copyright 1993-2008 NVIDIA Corporation.  All rights reserved.
3  *
4  * NOTICE TO USER:
5  *
6  * This source code is subject to NVIDIA ownership rights under U.S. and
7  * international Copyright laws.  Users and possessors of this source code
8  * are hereby granted a nonexclusive, royalty-free license to use this code
9  * in individual and commercial software.
10  *
11  * NVIDIA MAKES NO REPRESENTATION ABOUT THE SUITABILITY OF THIS SOURCE
12  * CODE FOR ANY PURPOSE.  IT IS PROVIDED "AS IS" WITHOUT EXPRESS OR
13  * IMPLIED WARRANTY OF ANY KIND.  NVIDIA DISCLAIMS ALL WARRANTIES WITH
14  * REGARD TO THIS SOURCE CODE, INCLUDING ALL IMPLIED WARRANTIES OF
15  * MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE.
16  * IN NO EVENT SHALL NVIDIA BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL,
17  * OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
18  * OF USE, DATA OR PROFITS,  WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
19  * OR OTHER TORTIOUS ACTION,  ARISING OUT OF OR IN CONNECTION WITH THE USE
20  * OR PERFORMANCE OF THIS SOURCE CODE.
21  *
22  * U.S. Government End Users.   This source code is a "commercial item" as
23  * that term is defined at  48 C.F.R. 2.101 (OCT 1995), consisting  of
24  * "commercial computer  software"  and "commercial computer software
25  * documentation" as such terms are  used in 48 C.F.R. 12.212 (SEPT 1995)
26  * and is provided to the U.S. Government only as a commercial end item.
27  * Consistent with 48 C.F.R.12.212 and 48 C.F.R. 227.7202-1 through
28  * 227.7202-4 (JUNE 1995), all U.S. Government End Users acquire the
29  * source code with only those rights set forth herein.
30  *
31  * Any use of this source code in individual and commercial software must
32  * include, in the user documentation and internal comments to the code,
33  * the above Disclaimer and U.S. Government End Users Notice.
34  */
35
36 // includes, system
37 #include <stdio.h>
38 #include <assert.h>
39
40 // Simple utility function to check for CUDA runtime errors
41 void checkCUDAError(const char *msg);
42
43 ///////////////////////////////////////////////////////////////////////////////
44 // Program main
45 ///////////////////////////////////////////////////////////////////////////////
46 int main(int argc, char **argv)
47 {
48
49         // pointer and dimension for host memory
50         int n, dimA;
51         float *h_a;
52
53         // pointers for device memory
54         float *d_a, *d_b;
55
56         // allocate and initialize host memory
57         // Bonus: try using cudaMallocHost in place of malloc
58         dimA = 8;
59         h_a = (float *)malloc(dimA * sizeof(float));
60         for (n = 0; n < dimA; n++) {
61                 h_a[n] = (float)n;
62         }
63         // Part 1 of 5: allocate device memory
64         size_t memSize = dimA * sizeof(float);
65         cudaMalloc((void **)&d_a, memSize);
66         cudaMalloc((void **)&d_b, memSize);
67
68         // Part 2 of 5: host to device memory copy
69         cudaMemcpy(d_a, h_a, memSize, cudaMemcpyHostToDevice);
70
71         // Part 3 of 5: device to device memory copy
72         cudaMemcpy(d_b, d_a, memSize, cudaMemcpyDeviceToDevice);
73
74         // clear host memory
75         for (n = 0; n < dimA; n++) {
76                 h_a[n] = 0.f;
77         }
78
79         // Part 4 of 5: device to host copy
80         cudaMemcpy(h_a, d_b, memSize, cudaMemcpyDeviceToHost);
81
82         // Check for any CUDA errors
83         checkCUDAError("cudaMemcpy calls");
84
85         // verify the data on the host is correct
86         for (n = 0; n < dimA; n++) {
87                 assert(h_a[n] == (float)n);
88         }
89         // Part 5 of 5: free device memory pointers d_a and d_b
90         cudaFree(d_b);
91         cudaFree(d_a);
92
93         // Check for any CUDA errors
94         checkCUDAError("cudaFree");
95
96         // free host memory pointer h_a
97         // Bonus: be sure to use cudaFreeHost for memory allocated with cudaMallocHost
98         free(h_a);
99
100         // If the program makes it this far, then the results are correct and
101         // there are no run-time errors.  Good work!
102         printf("Correct!\n");
103         return 0;
104 }
105
106 void checkCUDAError(const char *msg)
107 {
108         cudaError_t err = cudaGetLastError();
109         if (cudaSuccess != err) {
110                 fprintf(stderr, "Cuda error: %s: %s.\n", msg,
111                         cudaGetErrorString(err));
112                 exit(-1);
113         }
114 }