Single Thread Run Time:
Multi-threaded running time;
Code:
1#include <stdlib.h>2#include <stdio.h>3#include <process.h>4#include <Windows.h>5#include <time.h>6 7 #defineN 10008 9 DoubleM1[n][n] = {0 };Ten DoubleM2[n][n] = {0 }; One DoubleRes[n][n] = {0 }; A - //multithreaded functions, p indicates rows - voidProcessvoid*p) the { - int*row = (int*) p; - - //M2 each column + for(intj =0; J < N; J + +) - { + intTMP =0; A //traversing an element of a M2 column at for(intK =0; K < N; k++) - { -TMP + = m1[*row][k] *M2[k][j]; - } -RES[*ROW][J] =tmp; - } in } - to //Initialize Matrix + voidInitDoubleNum[n][n]) - { the for(inti =0; i < N; i++) * { $ for(intj =0; J < N; J + +)Panax Notoginseng { -NUM[I][J] = rand ()%5 ; the } + } A } the + //Display Matrix - voidShowDoubleNum[n][n]) $ { $ for(inti =0; i < N; i++) - { - for(intj =0; J < N; J + +) the { -printf"%.2f", Num[i][j]);Wuyi } theprintf"\ n"); - } Wu } - About voidMain () $ { - //Statistical Time - clock_t start, finish; - DoubleDuration =0; A +Start =clock (); the init (M1); - init (M2); $ //Show (M1); the //Show (M2); the intNUM1 =0; the the ////Multithreading - for(inti =0; i < N; i++) in { theHANDLE HD = (HANDLE) _beginthread (process,0, &i); the //WaitForSingleObject (HD, INFINITE); About } the the //Single Thread the /*for (int i = 0; i < N; i++) + { - process (&i); the }*/Bayi thefinish =clock (); theDuration = (Double) (Finish-start)/clocks_per_sec; -printf"%f seconds\n", duration); - theSystem"Pause"); the}
40. Multithreading to achieve matrix multiplication