Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade

How to Implement Parallel Programming in Linux

Parallel programming is a technique that allows multiple processes to run simultaneously, significantly improving the performance and efficiency of applications, especially on multi-core systems. In the Linux environment, parallel programming is highly applicable and can be achieved using various tools and libraries such as Pthreads, OpenMP, and MPI. This article will guide you through the basics of parallel programming in Linux, demonstrating how to create and run parallel programs using these tools.

Examples:

Example 1: Parallel Programming with Pthreads

Pthreads (POSIX threads) is a POSIX standard for threads, providing a set of C programming language types, functions, and constants. Here is a simple example of a parallel program using Pthreads.

Step-by-Step Guide:

  1. Install the necessary development tools:

    sudo apt-get install build-essential
  2. Create a C program using Pthreads:

    #include <pthread.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    #define NUM_THREADS 5
    
    void *print_hello(void *threadid) {
        long tid;
        tid = (long)threadid;
        printf("Hello World! It's me, thread #%ld!\n", tid);
        pthread_exit(NULL);
    }
    
    int main(int argc, char *argv[]) {
        pthread_t threads[NUM_THREADS];
        int rc;
        long t;
        for(t=0; t<NUM_THREADS; t++) {
            printf("In main: creating thread %ld\n", t);
            rc = pthread_create(&threads[t], NULL, print_hello, (void *)t);
            if (rc) {
                printf("ERROR; return code from pthread_create() is %d\n", rc);
                exit(-1);
            }
        }
        pthread_exit(NULL);
    }
  3. Compile the program:

    gcc -o pthread_example pthread_example.c -lpthread
  4. Run the program:

    ./pthread_example

Example 2: Parallel Programming with OpenMP

OpenMP (Open Multi-Processing) is an API that supports multi-platform shared memory multiprocessing programming in C, C++, and Fortran. Here is a simple example using OpenMP.

Step-by-Step Guide:

  1. Install the necessary development tools:

    sudo apt-get install build-essential
  2. Create a C program using OpenMP:

    #include <omp.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[]) {
        #pragma omp parallel
        {
            int ID = omp_get_thread_num();
            printf("Hello(%d) ", ID);
            printf("World(%d)\n", ID);
        }
        return 0;
    }
  3. Compile the program:

    gcc -o openmp_example -fopenmp openmp_example.c
  4. Run the program:

    ./openmp_example

Example 3: Parallel Programming with MPI

MPI (Message Passing Interface) is a standardized and portable message-passing system designed to function on parallel computing architectures. Here is a simple example using MPI.

Step-by-Step Guide:

  1. Install the necessary development tools:

    sudo apt-get install mpich
  2. Create a C program using MPI:

    #include <mpi.h>
    #include <stdio.h>
    
    int main(int argc, char** argv) {
        MPI_Init(NULL, NULL);
    
        int world_size;
        MPI_Comm_size(MPI_COMM_WORLD, &world_size);
    
        int world_rank;
        MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
    
        printf("Hello world from processor %d of %d\n", world_rank, world_size);
    
        MPI_Finalize();
        return 0;
    }
  3. Compile the program:

    mpicc -o mpi_example mpi_example.c
  4. Run the program:

    mpirun -np 4 ./mpi_example

To share Download PDF

Gostou do artigo? Deixe sua avaliação!
Sua opinião é muito importante para nós. Clique em um dos botões abaixo para nos dizer o que achou deste conteúdo.