Home > Blockchain >  How to achieve parallelism in C?
How to achieve parallelism in C?

Time:10-27

This might be a dumb question, i'm very sorry if that's the case. But i'm struggling to take advantage of the multiple cores in my computer to perform multiple computations at the same time in my Quad-Core MacBook. This is not for any particular project, just a general question, since i want to learn for when i eventually do need to do this kind of things

I am aware of threads, but the seem to run in the same core, so i don't seem to gain any performance using them for compute-bound operations (They are very useful for socket based stuff tho!).

I'm also aware of processed that can be created with fork, but i'm nor sure they are guaranteed to use more CPU, or if they, like threads, just help with IO-bound operations.

Finally i'm aware of CUDA, allowing paralellism in the GPU (And i think OpenCL and Compute Shaders also allows my code to run in the CPU in parallel) but i'm currently looking for something that will allow me to take advantage of the multiple CPU cores that my computer has.

In python, i'm aware of the multiprocessing module, which seems to provide an API very similar to threads, but there i do seem to gain an edge by running multiple functions performing computations in parallel. I'm looking into how could i get this same advantage in C, but i don't seem to be able

Any help pointing me to the right direction would be very much appreciated

Note: I'm trying to achive true parallelism, not concurrency

Note 2: I'm only aware of threads and using multiple processes in C, with threads i don't seem to be able to win the performance boost i want. And i'm not very familiar with processes, but i'm still not sure if running multiple processes is guaranteed to give me the advantage i'm looking for.

CodePudding user response:

A simple program to heat up your CPU (100% utilization of all available cores).

Hint: The thread starting function does not return, program exit via [CTRL C]

#include <pthread.h>

void* func(void *arg)
{
    while (1);
}

int main()
{
    #define NUM_THREADS 4 //use the number of cores (if known)
    pthread_t threads[NUM_THREADS];

    for (int i=0; i < NUM_THREADS;   i)
        pthread_create(&threads[i], NULL, func, NULL);

    for (int i=0; i < NUM_THREADS;   i)
        pthread_join(threads[i], NULL);

    return 0;
}

Compilation:

gcc -pthread -o thread_test thread_test.c

If i start ./thread_test, all cores are at 100%.


A word to fork and pthread_create:

fork creates a new process (the current process image will be copied and executed in parallel), while pthread_create will create a new thread, sometimes called a lightweight process.

Both, processes and threads will run in 'parallel' to the parent process.

It depends, when to use a child process over a thread, e.g. a child is able to replace its process image (via exec family) and has its own address space, while threads are able to share the address space of the current parent process.

There are of course a lot more differences, for that i recommend to study the following pages:

CodePudding user response:

Parallelism in C can be achieved by using the fork() function. This function simulates a thread by allowing two threads to run simultaneously and share data. The first thread forks itself, and the second thread is then executed as if it was launched from main(). Forking allows multiple processes to be Run concurrently without conflicts arising. To make sure that data is shared appropriately between the two threads, use the wait() function before accessing shared resources. Wait will block execution of the current program until all database connections are closed or all I/O has been completed, whichever comes first.

  • Related