GPU & Cuda: data transmission test between host and Device

Source: Internet
Author: User

Data transmission test: first transmitted from the host to the device, then transmitted within the device, and then from the device to the host.

H --> d

D --> d

D --> H

1 // movearrays. cu 2 // 3 // demonstrates Cuda interface to data allocation on device (GPU) 4 // and data movement between host (CPU) and device. 5 6 7 # include <stdio. h> 8 # include <assert. h> 9 # include <Cuda. h> 10 # include "cuda_runtime.h" 11 # include "device_launch_parameters.h" 12 INT main (void) 13 {14 float * a_h, * B _h; // pointer to host 15 float * a_d, * B _d; // pointer to device 16 17 int n = 14; 18 int I; 19 // apply for memory 20 a_h = (float *) malloc (sizeof (float) * n) on the host side; 21 B _h = (float *) malloc (sizeof (float) * n); 22 // apply to store 23 cudamalloc (void **) & a_d, sizeof (float) * n) on the device side; 24 cudamalloc (void **) & B _d, sizeof (float) * n); 25 // host data initialization 26 for (I = 0; I <n; I ++) {27 a_h [I] = 10.f + I; 28 B _h [I] = 0.f; 29} 30 // transmit data from the host to the device: a_h --> a_d31 cudamemcpy (a_d, a_h, sizeof (float) * n, cudamemcpyhosttodevice); 32 33 // transmit data within the device 34 cudamemcpy (B _d, a_d, sizeof (float) * n, cudamemcpydevicetodevice); 35 36 // transmit data from the device to the Host: B _d --> B _h37 cudamemcpy (B _h, B _d, sizeof (float) * n, cudamemcpydevicetohost ); 38 39 // check result 40 for (I = 0; I <n; I ++) 41 assert (a_h [I] = B _h [I]); 42 43 // release Host Storage 44 free (a_h); 45 free (B _h); 46 // release device storage 47 cudafree (a_d); 48 cudafree (B _d ); 49}

Test environment:

Win7 + vs2013 + cuda6.5

Download link

GPU & Cuda: data transmission test between host and Device

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.