Блочное умножение cpp определяет точку входа для консольного приложени

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Блочное умножение.cpp: определяет точку входа для консольного приложения.
//
#include "stdafx.h"
#include <pthread.h>
#include <iostream>
#include <locale.h>
#include <time.h>
using namespace std;
pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
int n=4;
int m=4;
int blN = 2, blM = 2;
int p = blN*blM;
pthread_t **threads;
int *b, *c;
int **a;
long times = clock();
struct Block
{
int begN;
int endN;
int begM;
int endM;
};
void multiple(int begN, int endN, int begM, int endM)
{
int *tmp = (int*)malloc((endN-begN)*sizeof(int));
for (int i = 0; i < endN - begN; i++)
tmp[i] = 0;
for (int i = begN; i < endN; i++)
for (int j = begM; j < endM; j++)
tmp[i - begN]+= a[i][j] * b[j];
for (int j = begN; j < endN; j++)
{
pthread_mutex_lock(&mut);
c[j] += tmp[j - begN];
cout << "c[" << j << "]=" << c[j] << "\n";
pthread_mutex_unlock(&mut);
}
delete[] tmp;
}
void *process_function(void *pa)
{
Block bl = *((struct Block *)pa);
multiple(bl.begN, bl.endN, bl.begM, bl.endM);
delete (struct Block *)pa;
return 0;
}
int main()
{
setlocale(LC_ALL, "Rus");
a = (int**)malloc(n*sizeof(int));
for (int i = 0; i < n; i++)
a[i] = (int*)malloc(m*sizeof(int));
b = (int*)malloc(m*sizeof(int));
c = (int*)malloc(n*sizeof(int));
threads = new pthread_t*[blN];
for (int i = 0; i<blN; i++)
threads[i] = new pthread_t[blM];
for (int i = 0; i < n; i++)
c[i] = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
{
a[i][j] = rand() % 5;
cout << a[i][j] << ' ';
if (j == m - 1) cout << "\n";
}
cout << endl;
for (int i = 0; i < m; i++)
{
b[i] = rand() % 5;
cout << b[i] << "\n";
}
cout << endl;
int locM = m / blM;
int locN = n / blN;
int ostM = m%blM;
int ostN = n%blN;
int endM = 0, endN = 0;
for (int i = 0; i < blN; i++)
{
Block *bl = new Block;
bl->begN = endN;
if (ostN > 0)
bl->endN = bl->begN + locN + 1;
else
bl->endN = bl->begN + locN;
endN = bl->endN;
endM = 0;
ostM = n%blM;
for (int j = 0; j < blM; j++)
{
Block *block = new Block;
block = bl;
block->begM = endM;
if (ostM > 0)
block->endM = block->begM + locM + 1;
else
block->endM = block->begM + locM;
endM = block->endM;
pthread_create(&threads[i][j], NULL, process_function, block);
}
free(bl);
}
for (int i = 0; i<blN; i++)
for (int j = 0; j<blM; j++)
if (pthread_join(threads[i][j], NULL))
fprintf(stderr, "wait thread #d!\n", i,j);
cout << "Результат: \n";
for (int i = 0; i<n; i++)
cout << c[i] << '\n';
cout << "Время выполнения:" << times << endl;
for (int i = 0; i < n; i++)
free(a[i]);
free(a);
free(b);
free(c);
return 0;
}