Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
openMP-HW
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
hasan.bahjat
openMP-HW
Commits
cb4be8a1
You need to sign in or sign up before continuing.
Commit
cb4be8a1
authored
Nov 04, 2024
by
hasan khaddour
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update openmp Cdoe
parent
7bfcf87c
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
189 additions
and
51 deletions
+189
-51
integral.c
integral.c
+110
-18
mandlebrot.c
mandlebrot.c
+79
-33
No files found.
integral.c
View file @
cb4be8a1
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <omp.h>
...
...
@@ -7,37 +8,128 @@ double lnFunction(double x) {
return
log
(
x
)
/
x
;
}
double
calculate_integral
(
int
num_rectangles
,
double
start
,
double
end
)
{
double
width
=
(
end
-
start
)
/
num_rectangles
;
double
total_area
=
0
.
0
;
// this is the first approach
double
calculateIntegral
(
int
numRectangles
,
int
start
,
int
end
)
{
// the width of aa single rectangle
double
width
=
(
end
-
start
)
/
(
numRectangles
*
1
.
0
);
// total area
double
totalArea
=
0
.
0
;
#pragma omp parallel for reduction(+:totalArea)
for
(
int
i
=
0
;
i
<
numRectangles
;
i
++
)
{
#pragma omp parallel for reduction(+:total_area)
for
(
int
i
=
0
;
i
<
num_rectangles
;
i
++
)
{
// Midpoint for rectangle
double
x
=
start
+
(
i
+
0
.
5
)
*
width
;
double
area
=
lnFunction
(
x
)
*
width
;
total_area
+=
area
;
double
xMid
=
start
+
(
i
+
0
.
5
)
*
width
;
// calcute F (x)
double
area
=
lnFunction
(
xMid
)
*
width
;
totalArea
+=
area
;
}
return
totalArea
;
}
// anpother appraoch to calc the itegral using openmp
// by using an atomic operation for the (+=)
double
calculateIntegralTrapezoidal
(
int
numRectangles
,
int
start
,
int
end
)
{
double
width
=
(
end
-
start
)
/
(
numRectangles
*
1
.
0
);
double
totalArea
=
0
.
0
;
// Calculate the area using the in parallel
#pragma omp parallel
{
double
localSum
=
0
.
0
;
#pragma omp for
for
(
int
i
=
0
;
i
<
numRectangles
;
i
++
)
{
// Midpoint for rectangle
double
xMid
=
start
+
(
i
+
0
.
5
)
*
width
;
// calcute F (x)
double
area
=
lnFunction
(
xMid
)
*
width
;
localSum
+=
area
;
}
// Reduce local sums into total area
#pragma omp atomic
totalArea
+=
localSum
;
}
return
total
_a
rea
;
return
total
A
rea
;
}
int
main
(
int
argc
,
char
*
argv
[])
{
// * note => (
// the two parameter nume of threads and num of rectangle
// we read them from the argument of the program
// )
// Adjust for higher precision
int
num_rectangles
=
1000000
;
int
numRectangles
=
1000000
;
// number of threads
int
numThreads
=
2
;
// integration interval
double
start
=
1
.
0
,
end
=
10
.
0
;
int
start
=
1
,
end
=
10
;
// read the number of threads and rectangles from argument if it passed
if
(
argc
<
3
)
{
printf
(
"+ Usage: %s <num_threads> <num_rectangles>
\n
"
,
argv
[
0
]);
printf
(
"+ Using default values: %d threads, %d rectangles.
\n
"
,
numThreads
,
numRectangles
);
}
else
{
// Convert command line argument to integer for threads
numThreads
=
atoi
(
argv
[
1
]);
// Convert command line argument to integer for rectangles
numRectangles
=
atoi
(
argv
[
2
]);
}
// Set the number o threads
omp_set_num_threads
(
numThreads
);
// start the timer
double
startTime
=
omp_get_wtime
();
double
result
=
calculateIntegral
(
numRectangles
,
start
,
end
);
// stop the timer
double
endTime
=
omp_get_wtime
();
// Print results in a good way
printf
(
"
\n
+----------------------------------------+
\n
"
);
printf
(
"+
\t
Result
\t\t\n
"
);
printf
(
"+------------------------------------------+
\n
"
);
printf
(
"[+] Calculated integral: %.10f
\n
"
,
result
);
printf
(
"[+] Execution time: %.4f seconds
\n
"
,
endTime
-
startTime
);
printf
(
"[+] Number of threads: %d
\n
"
,
numThreads
);
printf
(
"[+] Number of rectangles: %d
\n
"
,
numRectangles
);
printf
(
"+------------------------------------------+
\n
"
);
// Start time for performance measurement
double
start_time
=
omp_get_wtime
();
double
total_area
=
calculate_integral
(
num_rectangles
,
start
,
end
);
// this is an old code for the main
/*
int ad ;
int numThreads; //number of threads
printf("Enter the number of threads: ");
ad =scanf("%d", &numThreads);
// Set the number of threads for OpenMP
omp_set_num_threads(numThreads);
// Start timing the execution
double startTime = omp_get_wtime();
// End time for performance measurement
double
end_time
=
omp_get_wtime
();
// Calculate the integral
double result = calculateIntegral(numRectangles , start , end );
// End timing the execution
double endTime = omp_get_wtime();
printf
(
"Calculated integral: %.10f
\n
"
,
total_area
);
printf
(
"Execution time: %.4f seconds
\n
"
,
end_time
-
start_time
);
// Output the results
printf("Calculated integral: %.10f\n", result);
printf("Execution time: %.4f seconds with %d threads\n", endTime - startTime, numThreads);
*/
return
0
;
}
mandlebrot.c
View file @
cb4be8a1
#include "iostream"
#include <stdio.h>
#include <stdlib.h>
#include <complex>
#include <omp.h>
#include <cstdlib>
bool
isInMandelbrotSet
(
double
real
,
double
imag
,
int
max_iterations
)
{
std
::
complex
<
double
>
c
(
real
,
imag
);
std
::
complex
<
double
>
z
=
0
;
for
(
int
i
=
0
;
i
<
max_iterations
;
++
i
)
{
using
namespace
std
;
// global varibles
// define the interval for the complex plane region
const
double
REAL_MIN
=
-
2
.
5
;
const
double
REAL_MAX
=
1
.
0
;
const
double
IMAG_MIN
=
-
1
.
0
;
const
double
IMAG_MAX
=
1
.
0
;
// define the max iteration for check the mandlebrot set
const
int
MAXITERATION
=
3000
;
// function to check if a point is in the Mandelbrot set
bool
isInMandelbrotSet
(
double
real
,
double
imag
)
{
complex
<
double
>
c
(
real
,
imag
);
complex
<
double
>
z
=
0
;
// iterate to check if the point diverges
for
(
int
i
=
0
;
i
<
MAXITERATION
;
++
i
)
{
z
=
z
*
z
+
c
;
if
(
std
::
abs
(
z
)
>
2
.
0
)
return
false
;
// if the magnitude of z exceeds 2, it escapes the set
if
(
abs
(
z
)
>
2
.
0
)
return
false
;
}
// if max_iterations reached without escaping, it s not in the set
return
true
;
}
double
calculateMandelbrotArea
(
int
num_points
,
int
max_iterations
,
bool
parallel
)
{
int
in_set_count
=
0
;
// function to estimate the area of the Mandelbrot set
// using Monte Carlo method
double
calculateMandelbrotArea
(
int
numIteration
)
{
int
inSetCount
=
0
;
double
real
,
imag
;
#pragma omp parallel for reduction(+:in_set_count) private(real, imag) if (parallel)
for
(
int
i
=
0
;
i
<
num_points
;
++
i
)
{
real
=
(
rand
()
/
(
double
)
RAND_MAX
)
*
3
.
5
-
2
.
5
;
// Real part in range [-2.5, 1]
imag
=
(
rand
()
/
(
double
)
RAND_MAX
)
*
2
.
0
-
1
.
0
;
// Imaginary part in range [-1, 1]
if
(
isInMandelbrotSet
(
real
,
imag
,
max_iterations
))
{
in_set_count
++
;
// Parallelize the loop
#pragma omp parallel for reduction(+:inSetCount) private(real, imag)
for
(
int
i
=
0
;
i
<
numIteration
;
++
i
)
{
// generate a random point within the specified complex plane region
// Real part in range [REAL_MIN, REAL_MAX]
real
=
(
rand
()
/
(
double
)
RAND_MAX
)
*
(
REAL_MAX
-
REAL_MIN
)
+
REAL_MIN
;
// Imagina part in range [IMAG_MIN, IMAG_MAX]
imag
=
(
rand
()
/
(
double
)
RAND_MAX
)
*
(
IMAG_MAX
-
IMAG_MIN
)
+
IMAG_MIN
;
// check if the point is within the mandelbrot set
if
(
isInMandelbrotSet
(
real
,
imag
))
{
inSetCount
++
;
}
}
double
area
=
(
3
.
5
*
2
.
0
)
*
(
in_set_count
/
(
double
)
num_points
);
// calculate the estimated area based on the fraction of points in the set
double
area
=
((
REAL_MAX
-
REAL_MIN
)
*
(
IMAG_MAX
-
IMAG_MIN
))
*
(
inSetCount
/
(
double
)
numIteration
);
return
area
;
}
int
main
()
{
int
num_points
=
1000000
;
// Number of random points for Monte Carlo
int
max_iterations
=
5000
;
// Maximum iterations for Mandelbrot check
// Sequential run
double
start_time
=
omp_get_wtime
();
double
area_seq
=
calculateMandelbrotArea
(
num_points
,
max_iterations
,
false
);
double
end_time
=
omp_get_wtime
();
std
::
cout
<<
"Sequential Mandelbrot area: "
<<
area_seq
<<
std
::
endl
;
std
::
cout
<<
"Sequential time: "
<<
end_time
-
start_time
<<
" seconds
\n
"
;
// Parallel run
start_time
=
omp_get_wtime
();
double
area_par
=
calculateMandelbrotArea
(
num_points
,
max_iterations
,
true
);
end_time
=
omp_get_wtime
();
std
::
cout
<<
"Parallel Mandelbrot area: "
<<
area_par
<<
std
::
endl
;
std
::
cout
<<
"Parallel time: "
<<
end_time
-
start_time
<<
" seconds
\n
"
;
int
main
(
int
argc
,
char
*
argv
[])
{
//
double
startTime
,
endTime
;
// number of iteration for Monte Carlo method
int
numIteration
=
1000000
;
// default number of threads
int
numThreads
=
4
;
// check if the number of threads is provided as a command line argument
if
(
argc
>
1
)
{
// Convert argument to integer
numThreads
=
atoi
(
argv
[
1
]);
}
// set the number of threads for OpenMP
omp_set_num_threads
(
numThreads
);
// execution
// Start timer
startTime
=
omp_get_wtime
();
// Calculate area
double
area_par
=
calculateMandelbrotArea
(
numIteration
);
// End timer
endTime
=
omp_get_wtime
();
printf
(
"[+] Mandelbrot area: %.10f
\n
"
,
area_par
);
printf
(
"[+] time: %.4f seconds with %d threads
\n
"
,
endTime
-
startTime
,
numThreads
);
return
0
;
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment