Divide and Conquer
As a final example (you are surely getting tired of juicy code and want to delve back into philological discussions about comparative abstractive norms), let’s do a little supercomputing. Then, coffee. Our supercomputing application is a fairly typical parallel processing model (Figure 1-5). We have:
A ventilator that produces tasks that can be done in parallel
A set of workers that processes tasks
A sink that collects results back from the worker processes

Figure 1-5. Parallel pipeline
In reality, workers run on superfast boxes, perhaps using GPUs (graphic processing units) to do the hard math. Example 1-8 shows the code for the ventilator. It generates 100 tasks, each one a message telling the worker to sleep for some number of milliseconds.
Example 1-8. Parallel task ventilator (taskvent.c)
//// Task ventilator// Binds PUSH socket to tcp://localhost:5557// Sends batch of tasks to workers via that socket//#include "zhelpers.h"intmain(void){void*context=zmq_ctx_new();// Socket to send messages onvoid*sender=zmq_socket(context,ZMQ_PUSH);zmq_bind(sender,"tcp://*:5557");// Socket to send start of batch message onvoid*sink=zmq_socket(context,ZMQ_PUSH);zmq_connect(sink,"tcp://localhost:5558");printf("Press Enter when the workers are ready: ");getchar();printf("Sending tasks to workers...\n");// The first message is "0" and signals start ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access