Skip to content
This repository was archived by the owner on Sep 29, 2022. It is now read-only.

Commit c1a0d79

Browse files
author
OSWA00
committed
A01274570-homework-03
1 parent fa1305e commit c1a0d79

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

labs/07/openmp_practice/main.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* @file main.c
3+
* @author Oswaldo Hernandez
4+
* @brief Calculates trapezoidal AUC using OPENMP
5+
* @version 0.1
6+
* @date 2021-12-01
7+
*/
8+
#include <stdio.h>
9+
#include <omp.h>
10+
#include <math.h>
11+
12+
#define f(x) 1/(1+pow(x,2))
13+
14+
int main(){
15+
16+
/* Variables */
17+
float k;
18+
int i;
19+
int lower = 0;
20+
int upper = 1;
21+
int subInterval = 6;
22+
23+
/* Calculation */
24+
25+
/* Calculate step size */
26+
float stepSize = (float)(upper - lower) / subInterval;
27+
28+
/* Finding integration Value */
29+
float integration = f(lower) + f(upper);
30+
31+
int thread_count = 16;
32+
33+
int iterPerThread = subInterval / thread_count;
34+
35+
# pragma omp parallel num_threads(thread_count) shared(integration, k) private(i)
36+
{
37+
#pragma omp for
38+
for (i = 1; i <= subInterval - 1; i++)
39+
{
40+
k = lower + i * stepSize;
41+
integration = integration + 2 * f(k);
42+
}
43+
}
44+
45+
integration = integration * stepSize/2;
46+
printf("AUC approximation: %.5f\n", integration);
47+
return 0;
48+
}

0 commit comments

Comments
 (0)