0% found this document useful (0 votes)
8 views3 pages

pgm5-7 Ydf

Th Hhahasjsjsjdnndsn sjsjd d. Dhsjd s whe s sys e hw Hvfhhhhhh you baby tfghn M

Uploaded by

pcchethan2003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

pgm5-7 Ydf

Th Hhahasjsjsjdnndsn sjsjd d. Dhsjd s whe s sys e hw Hvfhhhhhh you baby tfghn M

Uploaded by

pcchethan2003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

add-vector https://tarikjaber.github.

io/Code-to-PDF/

1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <omp.h>
4
5 int addVectors(int v1[], int v2[], int res[], int n)
6 {
7 int opsCnt = 0;
8
9 #pragma omp parallel for
10 for(int i = 0; i < n; i��) {
11 res[i] = v1[i] + v2[i];
12 #pragma omp critical
13 {
14 opsCnt��;
15 }
16 }
17
18 return opsCnt;
19 }
20
21 int main()
22 {
23 int n;
24 printf("Enter value of n: ");
25 scanf("%d", &n);
26
27 int v1[n], v2[n], res[n];
28
29 for(int i = 0; i < n; i��) {
30 v1[i] = rand() % 100;
31 v2[i] = rand() % 1000;
32 }
33
34 int opsCnt = addVectors(v1, v2, res, n);
35
36 printf("Number of operations: %d", opsCnt);
37
38 return 0;
39 }

1 of 1 10/11/2024, 3:58 pm
num-thread https://tarikjaber.github.io/Code-to-PDF/

1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <omp.h>
4
5 int main()
6 {
7 int n, i, t, items[8];
8
9 printf("Enter number of items: ");
10 scanf("%d", &n);
11
12 for(i = 0; i < 8; i��) {
13 items[i] = rand() % 1000;
14 }
15
16 #pragma omp parallel for schedule(static, 3)
17 for(i = 0; i < n; i��) {
18 t = omp_get_thread_num();
19 printf("Thread %d, item: %d, value %d\n", t, i, items[t]);
20 }
21
22 return 0;
23 }

1 of 1 10/11/2024, 3:57 pm
sum https://tarikjaber.github.io/Code-to-PDF/

1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <omp.h>
4
5 int main()
6 {
7 int n, i;
8 int sum = 0;
9
10 printf("Enter value of n: ");
11 scanf("%d", &n);
12
13 #pragma omp parallel for
14 for(i = 1; i �� n; i��) {
15 #pragma omp critical
16 sum += i;
17 }
18
19 printf("Sum of f�rst %d natural numbers: %d\n", n, sum);
20
21 return 0;
22 }
23

1 of 1 10/11/2024, 3:50 pm

You might also like