To create a new simple console application, the project name is Test00301, as shown in the following illustration:
Then create a new file named Test01.cu in your project, as shown in the following illustration:
Then select the item in Solution Explorer and right-click and select "Generate Custom ..." From the pop-up menu, as shown in the following figure:
Check CUDA4.2 (or other versions) on the pop-up Visual C + + Build Customization files form, as shown in the following illustration:
Then select File Test.cu in Solution Explorer and right-click, select Properties from the pop-up menu, as shown in the following image:
Set "Item Type" on the pop-up property page as Cuda C + +, as shown in the following illustration:
Finally, open the project's property page, as shown in the following illustration, to add Cudart.lib in the "Attachment dependencies" in "Configuration Properties"--"linker"--"input" (with semicolons in the middle), as shown in the following illustration:
At this point, you can add cuda code to the Test.cu file and compile it, and now TEST.CU add the following code (for getting the video card information):
#include <stdio.h> #include <iostream> #include <cuda_runtime.h>//cuda run-time Library header file using namespace std;
Display device information extern "C" void Showdeviceprop (void) {int i,count;
Cudadeviceprop prop;
cudaerror_t Cudastatus=cudagetdevicecount (&count);
if (Cudastatus = = cudasuccess) {cout<< "total number of units:" <<count<< "\ n"; if (count>0) {for (i=0;i<count;i++) {cudagetdeviceproperties (&prop,i);//Get device property information cout<< "\
n the "<<i+1<<" equipment information: \ n ";
cout<< "Device Name:" <<prop.name<< "\ n";
cout<< "Total Memory:" <<prop.totalGlobalMem/1048576<< "m\n";
cout<< "Constant Memory:" <<prop.totalConstMem<< "bytes \ n";
cout<< "Number of processors in the device:" <<prop.multiProcessorCount<< "\ n";
cout<< "Maximum number of threads per thread block:" <<prop.maxThreadsPerBlock<< "\ n"; cout<< "Number of thread blocks that can be contained in a single thread: i=" << prop.maxgridsize[0] << "j=" <<prop.maxGridSize[1]<< "k=" <<prop.maxgridsize[2]<< "\ n"; cout<< the maximum number of threads that can be contained in a multidimensional thread block: i= "<< prop.maxthreadsdim[0] <<" j= "<<prop.maxThreadsDim[1]<
< "k=" <<prop.maxThreadsDim[2]<< "\ n"; The device information was not acquired by}} else {cout<<. Check to see if your computer has a graphics device that supports CUDA and whether the Cuda driver version needs to be updated.
\ n ";
}
}
Then add an external reference to the function in the Cu file in Test00301.cpp, as follows:
Test00301.cpp: Defines the entry point for a console application.
//
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
extern "C" void Showdeviceprop ();
int _tmain (int argc, _tchar* argv[])
{
//Display device property information
Showdeviceprop ();
Suspend
System ("pause");
return 0;
}
Run the program with the results shown in the following illustration:
It is noteworthy that in a CPP or H file, a function defined in CU or cuh is invoked directly in include and then called temporarily, and the CUDA function defined in the CU or Cuh can be set as a public function in an extern manner. Then declare the extern function in CPP or H, and then call it where necessary in the CPP or H file.