Hello,

A few days ago I visited a lecture of a teacher in an university that was about parallel processing.

He explained why it's good etc, and how you can make programs that use multiple cpus in C.
He used MPI and the code looked nice, wasn't too hard and got the job done. But in order to compile the program u needed to use mpicc (so not gcc, vcc or bcc)
and to run the program you had to write something like:

shell$ /opt/openmpi-1.3.1/bin/mpirun -np 4 a.out

in that line, you also specify how many cores you want to use.

So my question is: Is there any other multiprocessing api that uses local variables, and messages to coordinate the work of the cpus that can be compiled by visual c++ or devc++ and doesn't need that weird command line to execute. that you can choose the amount of cores inside the program?

I hope that makes sense, else, please reply for a detailed explanation.

Thanks in advance,
Tigran

Dani AI

Generated

Quick summary and practical direction for : MPI is designed for running multiple processes (often across machines) so it’s commonly started with an external launcher. For a single multi‑core PC you’ll usually want threads or a task library instead — they let you start and control worker count from inside your program and compile fine with Visual C++ or MinGW/Dev‑C++.

Options that fit your requirement (and compile with MSVC or GCC):

  • Use C++11 threads (std::thread) or Boost.Thread for low‑level control and portability. See the std::thread reference for behavior and helpers: std::thread (cppreference).
  • Use OpenMP for easy data‑parallel loops and simple pragmas; thread count can be set in code or via OMP_NUM_THREADS. More at OpenMP.
  • Use a task scheduler like oneTBB (Intel TBB) for scalable task-based work-stealing scheduling: oneTBB.
  • If you want message semantics rather than shared memory, consider a lightweight messaging lib such as ZeroMQ for in‑process or interprocess messaging: ZeroMQ.

A minimal pattern with C++11 (choose worker count, spawn and join) looks like this:

#include <thread>
#include <vector>
#include <iostream>

unsigned n = std::thread::hardware_concurrency();
if (n == 0) n = 2; // fallback
std::vector<std::thread> workers;
for (unsigned i = 0; i < n; ++i)
    workers.emplace_back([i]{ /* worker work here */ });
for (auto &t : workers) t.join();

Practical tips: for CPU‑bound tasks set threads ≈ physical cores; for I/O‑bound, more threads can help. Use a thread pool or task library instead of creating/destroying threads per task. Watch for data races, false sharing and locking overhead; profile and tune. MPI is still worth learning if you plan to scale to clusters, but for local multicore work start with threads/OpenMP/TBB.

Recommended Answers

All 6 Replies

What about POSIX Threads ?
's a tutorial about it ...

Hope this helps !

Thanks for the reply,

But why are there terms like multithreading and multiprocessing?
For what i've heared (but i'm not sure of) multithreading organises the tasks in one core. But i'm probably wrong :P

Are these POSIX threads also usable for windows? and are they as fast as the threads in the windows api?

You're right, but normally it's the Operating System's task to distribute all the threads over the available CPU-cores ...

POSIX Threads is also available for Windows here ...
If you want to use Windows' native API you might want to check ...

MPI is likely not what you want if you simply want to utilize multiple cores/CPUs on a single computer.

Learn multithreading first.

So why would i even bother learning MPI if multithreading does all the multicore stuff.

I think that if you're dealing with normal computers multithreading will speed-up your program more than enough, but it's still the operating system which has the task to distribute those threads among all the available CPU-cores, so the efficiency also depends on what os you are using ...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.