Cuda development matrix multiplication test your GPU Efficiency

Source: Internet
Author: User

Create a Cuda project on vs2008, create the test. Cu file, copy the following code, compile and execute the code, and clearly see the difference between GPU running matrix multiplication and CPU efficiency. The following result is displayed on my PC. The GPU efficiency of matrix multiplication is improved by about an order of magnitude (relative to the CPU). The development environment is vs2008 + cuda5.x Development Kit + gt520m graphics card.

 

Program code (refer to the programmer to download the program, modify: http://download2.pudn.com/downloads245/sourcecode/windows/csharp/05872102CUDAMatrixMul.rar ):

/// // # Include
# Include
# Include "cuda_runtime.h"
# Include "device_launch_parameters.h"
# Include

# 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 <count; 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"); cudadeviceprop prop;
Cudagetdeviceproperties (& prop, I); printf ("device: \" % s \ "\ n", Prop. Name); Return true;
} # Endif # define aw 855.
# Define Ah 511
# Define BW 1013
# Define blocknum 32 // 32
# Define threadnum 256 // 256 typedef struct
{
Int width;
Int height;
Int * element;
} Matrix; matrix initmatrix (int w, int H)
{
Matrix T;
T. Element = (int *) malloc (w * H * sizeof (INT ));
For (INT I = 0; I <w * h; I ++)
T. Element [I] = rand () % 10;
T. width = W;
T. Height = h;
Return T;
} Matrix mm (matrix A, matrix B)
{
Matrix T;
T. Element = (int *) malloc (A. height * B. Width * sizeof (INT ));
T. width = B. width;
T. Height = A. height;
Int X;
Int y;
For (INT I = 0; I <t. Width * T. height; I ++)
{
X = I/T. Width * A. width;
Y = I-I/T. Width * T. width;
T. Element [I] = 0;
For (int K = 0; k <A. width; k ++)
{
T. Element [I] + = A. Element [x + k] * B. Element [Y + B. Width * k];
}
}
Return T;
}

_ Global _ static void matrixmul (int * ma, int * MB, int * MC, int * MP)
{
Int aw = MP [0];
Int BW = MP [2];
Int CW = MP [4];
Int CH = MP [5]; const int bid = blockidx. X;
Const int tid = threadidx. X;
Int I, X, Y;
 
For (I = bid * threadnum + tid; I <Cw * Ch; I + = threadnum * blocknum)
{
X = I/Cw * Aw;
Y = I-I/Cw * CW;
MC [I] = 0;
For (int K = 0; k <aw; k ++)
{
MC [I] + = ma [x + k] * MB [Y + K * BW];
}
}}

Int main (INT argc, char * argv [])
{If (! Initcuda ()){
Return 0;
} // Define the Matrix
// Int matrixa [N] [N], matrixb [N] [N], matrixc [N] [N], gpuresult [N] [N], matrixd [N] [N];
Matrix matrixa = initmatrix (AW, AH );
Matrix matrixb = initmatrix (BW, AW );
Matrix matrixc;
Matrix gpuresult = initmatrix (BW, AH );
 
Int matrixprop [6];
// Timing the CPU operation
Unsigned int timer1 = 0; // multiply the CPU Matrix
Int start = clock ();
Matrixc = mm (matrixa, matrixb );
Int finish = clock ();
Printf ("CPU time = % d \ n", finish-Start); Start = clock (); matrixprop [0] = matrixa. width;
Matrixprop [1] = matrixa. height;
Matrixprop [2] = matrixb. width;
Matrixprop [3] = matrixb. height;
Matrixprop [4] = matrixc. width;
Matrixprop [5] = matrixc. height;
 
// Apply for video storage
Int * ma, * MB, * MC, * MP;
Cudamalloc (void **) & Ma, sizeof (INT) * matrixa. Width * matrixa. Height );
Cudamalloc (void **) & MB, sizeof (INT) * matrixb. Width * matrixb. Height );
Cudamalloc (void **) & Mc, sizeof (INT) * matrixc. Width * matrixc. Height );
Cudamalloc (void **) & MP, sizeof (INT) * 6); // copy data to the memory
Cudamemcpy (MA, matrixa. element, sizeof (INT) * matrixa. Width * matrixa. Height, cudamemcpyhosttodevice );
Cudamemcpy (MB, matrixb. element, sizeof (INT) * matrixb. Width * matrixb. Height, cudamemcpyhosttodevice );
Cudamemcpy (MP, matrixprop, sizeof (INT) * 6, cudamemcpyhosttodevice); unsigned int timer2 = 0; // call the Cuda Function
Matrixmul <blocknum, threadnum, 0> (MA, MB, MC, MP); cudathreadsynchronize ();
// Cutilcheckerror (cutstoptimer (timer2); // copy the data from the video memory
Cudamemcpy (gpuresult. element, MC, sizeof (INT) * gpuresult. Width * gpuresult. Height, cudamemcpydevicetohost); finish = clock ();
Printf ("GPU time = % d \ n", finish-Start); For (INT I = 0; I <gpuresult. Width * gpuresult. height; I ++)
{
// Printf ("% d -- % d \ n", matrixc. Element [I], gpuresult. Element [I]);
If (matrixc. Element [I]! = Gpuresult. Element [I])
{
Printf ("error ");
}
} Cudafree (MA );
Cudafree (MB );
Cudafree (MC );
Cudafree (MP );
Return 0;
}

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.