Commit 6aec8ce4 authored by Mohamad Bashar Desoki's avatar Mohamad Bashar Desoki

Lab 3 - OpenMP

parent 74bda77b
package org.multithreading;
public class ExecuterEX {
}
# Makefile
# Source file
SRC = $(Pr)
# Compiler
CC = gcc
# Executable name
EXE = ./out
all: $(EXE)
$(EXE): $(SRC)
$(CC) -fopenmp -o $(EXE) $(SRC)
clean:
rm -f $(EXE)
run:
$(EXE)
#include <omp.h>
#include <stdio.h>
int main() {
int count = 0;
#pragma omp parallel for num_threads(5)
for (int i = 0; i < 100000; i++) {
#pragma omp atomic
count++;
}
printf("Final count: %d\n", count);
return 0;
}
#include <omp.h>
#include <stdio.h>
int main() {
// #pragma omp parallel
// {
// printf("Hello from thread %d\n", omp_get_thread_num());
// }
// Set the number of threads globally for all parallel regions
omp_set_num_threads(3);
// Parallel region with 3 threads (as set by omp_set_num_threads)
#pragma omp parallel
{
printf("Thread with previous set %d out of %d\n", omp_get_thread_num(), omp_get_num_threads());
}
// Parallel region with 5 threads (overrides global setting with num_threads clause)
#pragma omp parallel num_threads(5)
{
printf("Thread %d out of %d\n", omp_get_thread_num(), omp_get_num_threads());
}
return 0;
}
#include <omp.h>
#include <stdio.h>
int main() {
int count = 0;
omp_lock_t lock;
// Initialize the lock
omp_init_lock(&lock);
// Parallel region with lock to control access to 'count'
#pragma omp parallel for num_threads(5)
for (int i = 0; i < 100000; i++) {
// Acquire the lock before modifying 'count'
omp_set_lock(&lock);
count++; // Critical section
// Release the lock after modification
omp_unset_lock(&lock);
}
// Destroy the lock after use
omp_destroy_lock(&lock);
printf("Final count: %d\n", count);
return 0;
}
#include <omp.h>
#include <stdio.h>
int main() {
int i;
omp_set_num_threads(3);
#pragma omp parallel for
for(i = 0; i < 10; i++) {
printf("Thread %d: i = %d\n", omp_get_thread_num(), i);
}
return 0;
}
#include <stdio.h>
#include <omp.h>
#define ARRAY_SIZE 1000000 // 1 million elements
int main() {
int array[ARRAY_SIZE];
long long sum = 0;
// Initialize array with values
for (int i = 0; i < ARRAY_SIZE; i++) {
array[i] = i + 1; // Example: Summing numbers from 1 to ARRAY_SIZE
}
// Measure time for parallel sum
double start = omp_get_wtime();
// Parallel sum calculation
#pragma omp parallel for reduction(+:sum) num_threads(5)
for (int i = 0; i < ARRAY_SIZE; i++) {
sum += array[i];
}
double end = omp_get_wtime();
printf("Parallel Sum: %lld\n", sum);
printf("Time taken (Parallel): %f seconds\n", end - start);
return 0;
}
#include <omp.h>
#include <stdio.h>
int main() {
int count = 0;
#pragma omp parallel for num_threads(5)
for (int i = 0; i < 100000; i++) {
count++; // Potential race condition
// printf("THread Num:%d\n",omp_get_thread_num());
}
printf("Final count: %d\n", count);
//int x = 2;
// #pragma omp parallel num_threads(6)
//{
// int threadId = omp_get_thread_num();
//
// if (threadId == 0)
// x = 5;
// else
// printf("I am Thread %d and for me x=%d \n", threadId, x);
// }
return 0;
}
#include <omp.h>
#include <stdio.h>
int main() {
int sum = 0;
//#pragma omp parallel for reduction(+:sum)
#pragma omp parallel for num_threads(5)
for (int i = 0; i < 100000; i++) {
sum +=i; // Potential race condition
}
printf("Final sum: %d\n", sum);
return 0;
}
#include <stdio.h>
#include <omp.h>
#define ARRAY_SIZE 1000000 // million elements
int main() {
int array[ARRAY_SIZE];
long long sum = 0;
// Initialize array with values
for (int i = 0; i < ARRAY_SIZE; i++) {
array[i] = i + 1; // Example: Summing numbers from 1 to ARRAY_SIZE
}
// Measure time for sequential sum
double start = omp_get_wtime();
// Sequential sum calculation
for (int i = 0; i < ARRAY_SIZE; i++) {
sum += array[i];
}
double end = omp_get_wtime();
printf("Sequential Sum: %lld\n", sum);
printf("Time taken (Sequential): %f seconds\n", end - start);
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