Cuda matrix multiplication calculation Verification

Source: Internet
Author: User

We have discussed how to determine whether the calculation is correct by using Cuda for matrix multiplication, because the matrix is large. Some data is sampled and output to the text to view the result.

Add the following test to the matrix multiplication function:

Add the following code to the extern "C" Void matrixmultiplication_cuda (const float * m, const float * n, float * P, int width) function. Note that the code is added after the data of the video card device is copied to the memory.

1 // The test results are calculated using 2 file * file1 = fopen ("test_m.txt", "W"); 3 File * file2 = fopen ("test_n.txt", "W "); 4 file * file3 = fopen ("test_p.txt", "W"); 5 for (INT I = 0; I <1024; I ++) // output the first 1024 elements 6 {7 fprintf (file1, "% F", M [I]); // output the first line of M 8 fprintf (file2, "% F", N [I * 1024]); // output the first column of N 9 fprintf (file3, "% F", P [I]); 10 if (0 = (I + 1) % 5) 11 {12 fprintf (file1, "\ n"); 13 fprintf (file2, "\ n "); 14 fprintf (file3, "\ n"); 15} 16} 17 fclose (file1); 18 fclose (file2); 19 fclose (file3 );

To facilitate testing, assign 1.5 to each element of the matrix M and N to be calculated.

The result is as follows:

The element size of array P can be calculated as follows: 1.5*1.5*1024 = 2304. Therefore, the test shows that the GPU device is accurate in computing. However, the GPU device only does not support double data. If the precision requirement is high, it may be a problem.

 

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.