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.