Cuda Async function

Source: Internet
Author: User

To improve cuda efficiency the use of asynchronous functions is a very general choice, but asynchronous functions are not as intelligent as I have imagined.

It wants the data that you want to transfer asynchronously on the host side (hosts) cannot be changed, that is, the asynchronous function just indicates the location of a pointer, and does not cache the data, to the real need to go to the host memory to find this value. So when doing asynchronous, make sure that the host side of the asynchronous transfer cannot be modified before the asynchronous completion (or copy completion). Otherwise, the data will be modified.

The experiment code is as follows

#include <stdio.h>#include<cuda_runtime.h>__global__voidAsync_kernel (int*x) {printf ("%d\n", x[0]);}intMainintargcChar*argv[]) {    int*x,*xd;    Cudastream_t STM; Cudastreamcreate (&STM); Cudamallochost (&x,sizeof(int)); Cudamalloc (&xd,sizeof(int)); printf ("start\n"); x[0]=1; Cudamemcpyasync (Xd,x,sizeof(int), cudamemcpyhosttodevice,stm); x[0]=Ten; Async_kernel<<<1,1,0,stm>>>(XD);        cudadevicesynchronize (); printf ("end\n");    Fflush (stdout); }

The execution effect is as follows

Of course, don't forget the synchronization function cudadevicesynchronize ();

Cuda Async function

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.