-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathomp12.c
More file actions
44 lines (35 loc) · 1.26 KB
/
omp12.c
File metadata and controls
44 lines (35 loc) · 1.26 KB
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
// 12. Написать программу, в которой, объявить и заполнить случайными значениями массив целых чисел.
// Используя возможности OpenMP найти максимальное числовое значение элементов массива, кратное 7.
// Длину массива и количество потоков определить самостоятельно. Результат выдать на экран.
// Для синхронизации числовых значений максимума используется механизм критических секций. (3 балла)
#include <omp.h>
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define MAGIC 300
#define DIVIDER 7
int main() {
srand(time(NULL));
int a[MAGIC];
int base_max = -1;
for (int i = 0; i < MAGIC; i++){
a[i] = rand() % MAGIC;
if (a[i] % DIVIDER == 0 && base_max < a[i]) {
base_max = a[i];
}
}
int max = -1;
#pragma omp parallel for shared(max)
for (int i = 0; i < MAGIC; i++) {
if (a[i] % DIVIDER == 0 && max < a[i]) {
#pragma omp critical
{
max = a[i];
}
}
}
printf("%d\n", max);
if (base_max == max) {
printf("correct\n");
}
}