Matrix Multiplication-Parallel Computing

Source: Internet
Author: User

C = A * B
It is divided by C. Assume that the number of threads is m and the matrix dimension is n * n. Then the number of elements for each thread to calculate C is n * n/m;

/* Matrix parallel computing C = A * B --- C (I, j) is equal to row I of A multiplied by column j */# include <stdio. h> # include <time. h> # include <stdlib. h> # include <math. h> # include <windows. h>/* generate n * n matrix */void GenerateMatrix (float * m, int n); void PrintMatrix (float * p, int n); void GeneralMul (float *, float * B, float * C, int n); void ClearMatrix (float * m, int n);/* matrix parallel computing */void ParallelCacul (float *, float * B, float * C, int n, int thread_num);/* error of two matrices */float d Iff (float * C1, float * C0, int n); struct ARG {float * A; float * B; float * C; int cx, cy; // The coordinate int m of the first element; // The number of elements in C to be calculated by the current thread int n;}; int main (int argc, char ** argv) {if (argc! = 3) {printf ("Usage: % s N thread_num \ n", argv [0]); return 0 ;}int n = atoi (argv [1]); int thread_num = atoi (argv [2]); float * A = new float [n * n]; float * B = new float [n * n]; float * C = new float [n * n]; float * C0 = new float [n * n]; GenerateMatrix (A, n); GenerateMatrix (B, n ); clock_t start; float time_used; ClearMatrix (C0, n); start = clock (); GeneralMul (A, B, C0, n); time_used = static_cast <float> (clock () -start)/CLOCKS_PER_SEC * 1000; printf ("General: time = % f \ n", time_used); ClearMatrix (C, n); start = clock (); parallelCacul (A, B, C, n, thread_num); time_used = static_cast <float> (clock ()-start)/CLOCKS_PER_SEC * 1000; printf ("Block: time = % f \ n ", time_used); printf (" Difference of two result: % f \ n ", diff (C0, C, n); delete []; delete [] B; delete [] C; delete [] C0; return 0;} void ClearMatrix (float * m, int n) {for (int I = 0; I <n; I ++) {for (int j = 0; j <n; j ++) m [I * n + j] = 0.0 ;}} /* Common matrix multiplication */void GeneralMul (float * A, float * B, float * C, int n) {for (int I = 0; I <n; I ++) {for (int j = 0; j <n; j ++) {float * p = C + I * n + j; for (int k = 0; k <n; k ++) {* p + = A [I * n + k] * B [k * n + j] ;}}} dword winapi Mul_Fun (LPVOID arg) {struct ARG * p = (struct ARG *) arg; float * A = p-> A; float * B = p-> B; float * C = p-> C; int m = p-> m; int n = p-> n; for (int I = p-> cx; I <n; I ++) {int j; if (I = p-> cx) j = p-> cy; else j = 0; for (; j <n; j ++) {float * t = C + I * n + j; for (int k = 0; k <n; k ++) {* t + = A [I * n + k] * B [k * n + j];} m --; if (m = 0) return 0 ;}} return 0;} void ParallelCacul (float * A, float * B, float * C, int n, int thread_num) {int m = n * n/thread_num; // The number of elements to be calculated for each thread. struct ARG * args = new struct ARG [thread_num]; HANDLE * h = new HANDLE [thread_num] is not considered. int I; for (I = 0; I <thread_num; I ++) {args [I]. A = A; args [I]. B = B; args [I]. C = C; args [I]. n = n; args [I]. m = m ;}for (I = 0; I <thread_num; I ++) {args [I]. cx = I * m/n; args [I]. cy = I * m % n; h [I] = CreateThread (NULL, 0, Mul_Fun, (LPVOID) (& args [I]), 0, 0 );} for (I = 0; I <thread_num; I ++) {WaitForMultipleObjects (thread_num, & h [I], TRUE, INFINITE) ;}} void GenerateMatrix (float * p, int n) {srand (time (NULL) + rand (); for (int I = 0; I <n * n; I ++) {* p = static_cast <float> (rand ()/(static_cast <float> (rand () + static_cast <float> (0.55); p ++ ;}} float diff (float * C1, float * C0, int n) {float rst = 0.0; float t; for (int I = 0; I <n; I ++) {for (int j = 0; j <n; j ++) {t = C1 [I * n + j]-C0 [I * n + j]; if (t <0) t =-t; rst + = t ;}} return rst;} void PrintMatrix (float * p, int n) {for (int I = 0; I <n; I ++) {for (int j = 0; j <n; j ++) {printf ("%. 2f \ t ", p [I * n + j]);} printf (" \ n ");} printf (" \ n ");}

 

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.