#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#define MAX 100
#define TESTCOUNT 3
double start, end;
int* InitMas(int size)
{
int *mas = (int*)malloc(sizeof(int) * size);
for (int i = 0; i < size; ++i)
{
mas[i] = rand() % MAX;
}
return mas;
}
int DotProduct(int *a, int *b, int size)
{
int dotProduct = 0;
for (int i = 0; i < size; ++i)
{
dotProduct += a[i] * b[i];
}
return dotProduct;
}
int ParalDotProduct(int *a, int *b, int size, int chunk)
{
int dotProduct = 0;
start = omp_get_wtime();
#pragma omp parallel
{
#pragma omp for schedule(static, chunk) reduction(+:dotProduct)
for (int i = 0; i < size; ++i)
{
dotProduct += a[i] * b[i];
}
}
end = omp_get_wtime();
return dotProduct;
}
void Run(int *a, int *b, int size, int threadCount)
{
ParalDotProduct(a, b, size, size / threadCount);
printf("Thread count: %d\n", threadCount);
printf("%g s\n", end - start);
}
int main()
{
int *a, *b;
int threadCount;
double start, end;
srand(time(NULL));
int size = 1000000;
for (int i = 0; i < TESTCOUNT; ++i)
{
printf("Array size: %d\n", size);
a = InitMas(size);
b = InitMas(size);
InitMas(size);
start = omp_get_wtime();
DotProduct(a, b, size);
end = omp_get_wtime();
printf("Thread count: %d\n", 1);
printf("%g s\n", end - start);
Run(a, b, size, 2);
Run(a, b, size, 3);
Run(a, b, size, 4);
Run(a, b, size, 5);
Run(a, b, size, 6);
Run(a, b, size, 7);
Run(a, b, size, 8);
Run(a, b, size, 9);
Run(a, b, size, 10);
size *= 10;
}
system("PAUSE");
}