10. Cuda cosnstant usage (I) -- GPU revolution

Source: Internet
Author: User

10. Cuda cosnstant usage (I) ------ GPU revolution

Preface: There have been a lot of recent things. I almost couldn't find my way home. I almost forgot the starting point of my departure. I calmed down and stayed up late, so there were more things, you must do everything well. If you do not do well, you will not be able to answer it. I think other people can accept it. My personal abilities are also limited. Sometimes, it is more time to listen to destiny than to make full use of human resources. But sometimes when you are too tired to do anything, you really want to go home and lie down for a few days. You don't have to think about anything, just be quiet. Let me go back to it. One thing is done, one thing is done, one thing is done, another is done, and confidence is given to myself, only when you can do one thing step by step is to give yourself the greatest confidence. Confidence is not given by others, but by yourself. Body: connect the book to article 9. the usage of Cuda shared mem describes the use of shared memory. Recently, several friends have been asking me about the use of cosntant. First, let's talk about the use of cosntant, the following is a simple code:

/*************************************** *****************************

* Cosntant_test.cu

* This is a example of the Cuda program.

* Author: Zhao. kaiyong (AT) gmail.com

**************************************** *****************************/

# Include

# Include

# Include

# Include

/*************************************** *********************************/

/* Init Cuda */

/*************************************** *********************************/

# If _ device_emulation __

Bool initcuda (void) {return true ;}

# Else

Bool initcuda (void)

{

Int COUNT = 0;

Int I = 0;

Cudagetdevicecount (& COUNT );

If (COUNT = 0 ){

Fprintf (stderr, "there is no device./N ");

Return false;

}

For (I = 0; I

Cudadeviceprop prop;

If (cudagetdeviceproperties (& prop, I) = cudasuccess ){

If (prop. Major> = 1 ){

Break;

}

}

}

If (I = count ){

Fprintf (stderr, "there is no device supporting Cuda./N ");

Return false;

}

Cudasetdevice (I );

Printf ("Cuda initialized./N ");

Return true;

}

# Endif

/*************************************** *********************************/

/* Example */

/*************************************** *********************************/

_ Constant _ char p_hellocuda [11]; // = "Hello Cuda! ";

_ Constant _ int t_hellocuda [11] = {, 10 };

_ Constant _ int num = 11;

_ Global _ static void hellocuda (char * result)

{

Int I = 0;

For (I = 0; I

Result [I] = p_hellocuda [I] + t_hellocuda [I];

}

}

/*************************************** *********************************/

/* Hellocuda */

/*************************************** *********************************/

Int main (INT argc, char * argv [])

{

If (! Initcuda ()){

Return 0;

}

Char hellocuda [] = "hdjik Cuda! ";

Char * device_result = 0;

Char host_result [12] = {0 };

Cuda_safe_call (cudamalloc (void **) & device_result, sizeof (char) * 11 ));

Cuda_safe_call (cudamemcpytosymbol (p_hellocuda, hellocuda, sizeof (char) * 11 ));

Unsigned int timer = 0;

Cut_safe_call (cutcreatetimer (& timer ));

Cut_safe_call (cutstarttimer (timer ));

Hellocuda> (device_result );

Cut_check_error ("kernel execution failed/N ");

Cuda_safe_call (cudathreadsynchronize ());

Cut_safe_call (cutstoptimer (timer ));

Printf ("Processing Time: % F (MS)/n", cutgettimervalue (timer ));

Cut_safe_call (cutdeletetimer (timer ));

Cuda_safe_call (cudamemcpy (& host_result, device_result, sizeof (char) * 11, cudamemcpydevicetohost ));

Printf ("% s/n", host_result );

Cuda_safe_call (cudafree (device_result ));

Cut_exit (argc, argv );

Return 0;

}

Here we have written two methods to use cosntant:

1. One way is to initialize constant directly during definition:

_ Constant _ int t_hellocuda [11] = {, 10 };

_ Constant _ int num = 11;

Use it directly in the kernel;

2. The second method is to define a cosntant array and then use the function to initialize it;

_ Constant _ char p_hellocuda [11]; // = "Hello Cuda! ";

Cuda_safe_call (cudamemcpytosymbol (p_hellocuda, hellocuda, sizeof (char) * 11 ));

The previous sentence defines constant, and the next sentence is to initialize this constant. You can use it in the defined way when using it in the kernel;

Of course, some friends want to define their own struct. Of course, it is okay, but during initialization, copy the corresponding struct. This is just a preliminary method of use, and I hope it will be useful to my family.

Next, we will make a simple analysis of the performance advantages embodied in the use of cosntant.

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.