-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpinnedMemory_bis.cpp
37 lines (29 loc) · 950 Bytes
/
pinnedMemory_bis.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
int main(void) {
int * host_p; /*< Host data allocated as pinned memory */
int * dev_ptr_p; /*< this pointer resides on the host */
int ns = 32;
int data_size = ns * sizeof(int);
checkCudaErrors(
cudaHostAlloc((void**) &host_p, data_size, cudaHostAllocMapped));
/* host_p = {1, 2, 3, ..., ns}*/
for (int i = 0; i < ns; i++)
host_p[i] = i + 1;
/*
* we can pass the address of `host_p`,
* namely `dev_ptr_p` to the kernel. This address
* is retrieved using cudaHostGetDevicePointer:
* */
checkCudaErrors(cudaHostGetDevicePointer(&dev_ptr_p, host_p, 0));
kernel<<<1, ns>>>(dev_ptr_p);
/*
* The following line is necessary for the host
* to be able to "see" the changes that have been done
* on `host_p`
*/
checkCudaErrors(cudaDeviceSynchronize());
for (int i = 0; i < ns; i++)
printf("host_p[%d] = %d\n", i, host_p[i]);
/* Free the page-locked memory */
checkCudaErrors(cudaFreeHost(host_p));
return 0;
}