Commit cb4be8a1 authored by hasan  khaddour's avatar hasan khaddour

Update openmp Cdoe

parent 7bfcf87c
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <math.h> #include <math.h>
#include <omp.h> #include <omp.h>
...@@ -7,37 +8,128 @@ double lnFunction(double x) { ...@@ -7,37 +8,128 @@ double lnFunction(double x) {
return log(x) / x; return log(x) / x;
} }
double calculate_integral(int num_rectangles, double start, double end) { // this is the first approach
double width = (end - start) / num_rectangles; double calculateIntegral(int numRectangles, int start, int end) {
double total_area = 0.0;
// 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 // Midpoint for rectangle
double x = start + (i + 0.5) * width; double xMid = start + (i + 0.5) * width;
double area = lnFunction(x) * width; // calcute F (x)
total_area += area; 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_area; return totalArea;
} }
int main(int argc, char *argv[]) { 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 // Adjust for higher precision
int num_rectangles = 1000000; int numRectangles = 1000000;
// number of threads
int numThreads = 2;
// integration interval // 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("+\tResult\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 // this is an old code for the main
double start_time = omp_get_wtime(); /*
double total_area = calculate_integral(num_rectangles, start, end); 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 // Calculate the integral
double end_time = omp_get_wtime(); double result = calculateIntegral(numRectangles , start , end );
// End timing the execution
double endTime = omp_get_wtime();
printf("Calculated integral: %.10f\n", total_area); // Output the results
printf("Execution time: %.4f seconds\n", end_time - start_time); printf("Calculated integral: %.10f\n", result);
printf("Execution time: %.4f seconds with %d threads\n", endTime - startTime, numThreads);
*/
return 0; return 0;
} }
#include "iostream" #include <stdio.h>
#include <stdlib.h>
#include <complex> #include <complex>
#include <omp.h> #include <omp.h>
#include <cstdlib>
bool isInMandelbrotSet(double real, double imag, int max_iterations) { using namespace std ;
std::complex<double> c(real, imag); // global varibles
std::complex<double> z = 0;
for (int i = 0; i < max_iterations; ++i) { // 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; 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; return true;
} }
double calculateMandelbrotArea(int num_points, int max_iterations, bool parallel) { // function to estimate the area of the Mandelbrot set
int in_set_count = 0; // using Monte Carlo method
double calculateMandelbrotArea(int numIteration) {
int inSetCount = 0;
double real, imag; double real, imag;
#pragma omp parallel for reduction(+:in_set_count) private(real, imag) if (parallel) // Parallelize the loop
for (int i = 0; i < num_points; ++i) { #pragma omp parallel for reduction(+:inSetCount) private(real, imag)
real = (rand() / (double)RAND_MAX) * 3.5 - 2.5; // Real part in range [-2.5, 1] for (int i = 0; i < numIteration; ++i) {
imag = (rand() / (double)RAND_MAX) * 2.0 - 1.0; // Imaginary part in range [-1, 1]
if (isInMandelbrotSet(real, imag, max_iterations)) { // generate a random point within the specified complex plane region
in_set_count++;
// 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; return area;
} }
int main() { int main(int argc, char *argv[]) {
int num_points = 1000000; // Number of random points for Monte Carlo //
int max_iterations = 5000; // Maximum iterations for Mandelbrot check double startTime, endTime ;
// Sequential run // number of iteration for Monte Carlo method
double start_time = omp_get_wtime(); int numIteration = 1000000;
double area_seq = calculateMandelbrotArea(num_points, max_iterations, false); // default number of threads
double end_time = omp_get_wtime(); int numThreads = 4;
std::cout << "Sequential Mandelbrot area: " << area_seq << std::endl;
std::cout << "Sequential time: " << end_time - start_time << " seconds\n"; // check if the number of threads is provided as a command line argument
if (argc > 1) {
// Parallel run // Convert argument to integer
start_time = omp_get_wtime(); numThreads = atoi(argv[1]);
double area_par = calculateMandelbrotArea(num_points, max_iterations, true); }
end_time = omp_get_wtime();
std::cout << "Parallel Mandelbrot area: " << area_par << std::endl; // set the number of threads for OpenMP
std::cout << "Parallel time: " << end_time - start_time << " seconds\n"; 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; return 0;
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment