1. understand the concept of OpenMP and MPI, multiple-thread vs multiple-process
OpenMP:
Use OpenMP when working on a shared memory system and the parallelism can be easily expressed using threads.
It is often simpler to implement and can be very efficient for multi-core processors.
MPI:
Use MPI for large-scale distributed computing across multiple nodes.
It is essential for applications that require high scalability and involve complex communication patterns among processes.
Multiple Threads
Threads are the smallest unit of execution within a process.
Multiple threads within a single process share the same memory space and can communicate more efficiently than separate processes.
Each thread has its own stack but shares code, data, and file descriptors with other threads in the same process.
Multiple Processes
Processes are independent execution units that have their own memory space.
Multiple processes can run concurrently on different CPUs or cores and communicate via inter-process communication (IPC) mechanisms like pipes, sockets, shared memory, or message passing.
2. create scripts to run OpenMP helloworld and an example for parallel loop
#include <omp.h>
#include <stdio.h>
int main() {
// Parallel region
#pragma omp parallel
{
int thread_id = omp_get_thread_num();
printf("Hello World from thread %d\n", thread_id);
}
return 0;
}
gcc -fopenmp hello_openmp.c -o hello_openmp
./hello_openmp
This script demonstrates how to use OpenMP to parallelize a loop that calculates the square of each element in an array.
A process is an instance of a program that is being executed. It contains the program code and its current activity.
Each process has its own separate memory space. This means that processes do not share memory with each other. They have their own address space, stack, and heap.
Processes are isolated from each other. A crash in one process does not affect other processes. This makes them more robust for executing separate tasks.
Since processes do not share memory, they need to use IPC mechanisms like pipes, sockets, shared memory, or message passing to communicate with each other.
Creating and managing processes has more overhead compared to threads because each process requires its own memory space and system resources.
Examples: Web browsers (where each tab might be a separate process), operating system services, and servers
Thread:
A thread is the smallest unit of execution within a process. Multiple threads can exist within the same process and share the same memory space.
A thread is the smallest unit of execution within a process. Multiple threads can exist within the same process and share the same memory space.
Since threads share the same memory space, they can communicate with each other more easily and efficiently than processes. This allows for faster context switching and data sharing.
Creating and managing threads has less overhead compared to processes because threads share resources of the parent process.
Threads are not isolated from each other. A crash in one thread can potentially bring down the entire process, affecting all other threads within that process.
Multithreaded applications like web servers (where each thread handles a client request), GUI applications (where one thread handles user input while another performs background tasks), and parallel algorithms
Processes cannot run in true parallelism on a single-core system. On a single-core system, only one process can execute at a time. However, multiple processes can achieve concurrency through context switching, where the operating system rapidly switches between processes, giving the illusion that they are running simultaneously.
Threads within the same process share the same memory space, which is more efficient for communication but requires careful synchronization to avoid race conditions.
Similar to processes, there are operating system-imposed limits on the number of threads per process and the total memory that can be allocated.
Execution: True parallelism is only possible on multi-core systems, where different processes or threads can be executed on different cores at the same time.
Concurrency:
Definition: Managing multiple tasks at the same time.
Requirement: Can be achieved on single-core or multi-core systems.
Execution: On a single-core system, concurrency is achieved through context switching, where the CPU switches between processes or threads, giving the appearance of simultaneous execution.
Single-Core System
Concurrency: The operating system uses context switching to manage multiple processes. It rapidly switches between them, so each process gets a slice of CPU time.
Parallelism: True parallelism is not possible because there is only one CPU core available to execute instructions.
Multi-Core System
Concurrency: Concurrency is still achieved through context switching, but with multiple cores, some processes can run truly in parallel.
Parallelism: True parallelism is achieved because multiple processes or threads can be executed on different cores simultaneously.