Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)
Paste
Pasted as C++ by Armen ( 15 years ago )
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#define SIZE_ID 500
#define SIZE_JD 500
float X[SIZE_ID][SIZE_JD];
float S[SIZE_ID][SIZE_JD];
float R[SIZE_ID][SIZE_JD];
int SIZE_I;
int SIZE_J;
int all = 0;
struct DATA_
{
double x;
int i;
int j;
};
typedef struct DATA_ DATA;
pthread_mutex_t lock; //Исключающая блокировка
void *mulmatr(void *arg)
{
for(int i = 0; i < SIZE_I; i++)
for(int j = 0; j < SIZE_J; j++)
{
for(int k = 0; k < SIZE_J; k++)
// R[SIZE_I][SIZE_J] += (X[SIZE_I][k] * X[k][SIZE_J]);
R[j] += (X[k] * S[k][j]);
}
// устанавливаем блокировку
pthread_mutex_lock(&lock;);
// изменяем глобальную переменную
++all;
// снимаем блокировку
pthread_mutex_unlock(&lock;);
}
// Потоковая функция для ввода
void *input(void *arg)
{
DATA* a = (DATA*) arg;
X[a->i][a->j] = rand();
S[a->i][a->j] = rand();
free(a);
return NULL;
}
int main()
{
DATA *arg;
// Ввод
int n;
FILE *file;
printf("Размерность желательно больше 10!!!: ");
scanf("%d", &n);
SIZE_I=n;
SIZE_J=n;
for (int n=1;n<6;n++)
{
clock_t start=clock();
//массив идентификаторов потоков
pthread_t thr[ SIZE_I + SIZE_J ];
//инициализация исключающей блокировки
pthread_mutex_init(&lock;, NULL);
for (int i=0;i<SIZE_I; ++i)
{
for (int j=0; j<SIZE_J; ++j)
{
arg = malloc(sizeof(DATA));
arg->i = i; arg->j = j;
//создаем поток для ввода
input(arg);
}
}
//printf("Start calculationn");
//for (int i=0;i<SIZE_I; ++i)
//{
//for (int z=0; z<SIZE_J; ++z)
//{
//arg = malloc(sizeof(DATA));
//arg->i = i; arg->z = z;
//arg->x = S[z];
//mulmatr(arg);
//Вычисления
printf("Start calculationn");
for (int i=0;i<SIZE_I; ++i)
{
for (int j=0; j<SIZE_J; ++j)
{
pthread_t thread;
//создаем поток для вычислений
pthread_create(&thread;, NULL, mulmatr, (void *)arg);
// переводим в отсоединенный режим
pthread_detach(thread);
}
}
clock_t finish=clock();
do
{
// Основной процесс "засыпает" на 1с
sleep(1);
// Все-ли завершились?
printf("finished %d threads.n", all);
}while(all < SIZE_I+SIZE_J);
//}
//}
printf("Серия № %d ", n);
printf("n");
//Печать результатов
printf("Первая матрица:n");
for (int i=0;i<SIZE_I; ++i)
{
for (int j=0; j<SIZE_J; ++j)
{
printf("%.ft",X[j]);
}
printf("n");
}
printf("Вторая матрица:n");
for (int i=0;i<SIZE_I; ++i)
{
for (int j=0; j<SIZE_J; ++j)
{
printf("%.ft",S[j]);
}
printf("n");
}
printf("Результат:n");
for (int i=0;i<SIZE_I; ++i)
{
for (int j=0; j<SIZE_J; ++j)
{
printf("%.ft",R[j]);
}
printf("n");
}
int workTime=(int)(finish-start) ;
printf("Time %d",workTime);
pthread_mutex_destroy(&lock;);
printf("n");
SIZE_I=SIZE_I+5;
SIZE_J=SIZE_J+5;
if ((file = fopen("1.csv","a+")) == NULL)
printf ("Файл невозможно открыть или создатьn");
else
{
fprintf(file,"%d;",all);
fprintf(file,"%dn",workTime);
}
fclose(file);
}
//system(" oocalc "macro:///standard.mod1.OpenCSV(/home/aleksandr/c/1.csv)" ");
return 0;
}
Revise this Paste