Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define SIZE 4
- #include <iostream>
- #include <thread>
- using namespace std;
- int x=4;
- int y=4;
- int matA[SIZE][SIZE];
- int matB[SIZE][SIZE];
- int matC[SIZE][SIZE];
- int matTemp[SIZE][SIZE];
- int matRes[SIZE][SIZE];
- void fun(int i,int j){
- //mnozysz wiersdz*kolumna
- int temp=0;
- for(int k=0;k<SIZE;k++)
- {
- temp+=matA[i][k]*matB[k][j];
- }
- //matTemp trzyma wyniki mnozenia A*B
- matTemp[i][j]=temp;
- //Przemnoz z C (matTemp*C => A*B*C)
- cout<<matTemp<<endl;
- for(int z=0; z<x; z++){
- temp+=matTemp[i][z]*matC[z][j];
- }
- matRes[i][j]=temp;
- }
- int main() {
- //Wstawienie do macierzy losowych wartosci
- for(int i=0;i<SIZE;i++)
- {
- for(int j=0;j<SIZE;j++)
- {
- matA[i][j] = rand() % 10;
- matB[i][j] = rand() % 10;
- matC[i][j] = rand() % 10;
- }
- }
- //Wypisanie macierzy
- for(int i=0;i<SIZE;i++)
- {
- for(int j=0;j<SIZE;j++)
- {
- cout<<matA[i][j]<<" ";
- }
- cout<<endl;
- }
- cout<<endl;
- for(int i=0;i<SIZE;i++)
- {
- for(int j=0;j<SIZE;j++)
- {
- cout<<matB[i][j]<<" ";
- }
- cout<<endl;
- }
- cout<<endl;
- for(int i=0;i<SIZE;i++)
- {
- for(int j=0;j<SIZE;j++)
- {
- cout<<matC[i][j]<<" ";
- }
- cout<<endl;
- }
- //jedno pole w macierzy wynikowej = jeden watek
- thread thr[SIZE][SIZE];
- for(int i=0;i<SIZE;i++)
- {
- for(int j=0;j<SIZE;j++)
- {
- thr[i][j]=thread(fun,i,j);
- }
- }
- //join
- for(int i=0;i<SIZE;i++)
- {
- for(int j=0;j<SIZE;j++)
- {
- thr[i][j].join();
- }
- }
- cout<<"\nResult: "<<endl;
- for(int i=0;i<SIZE;i++)
- {
- for(int j=0;j<SIZE;j++)
- {
- cout<<matRes[i][j]<<" ";
- }
- cout<<endl;
- }
- cout<<endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement