I am doing some calculations on spectra, doing some maths on each partial. This is taking a lot of time, but since the partials are all calculated interdependently, I wonder how to do parallel processing in sclang.
If I just fork them into threads, will this distribute them across my CPU, or do I need to do something more explicit? Can I just have them write a variable when they finish (protected by a semaphore) or should they send an OSC message with their finished calculation?
In sclang, fork does not provide true parallel processing; it just creates multitasking within a single thread. Separate sclang processes and communicate via OSC, but I say this just as a possibility; I have no idea how it would work out in practice.
EDIT: Of course, another possibility is to use parallel C++ processes. It can be dope to do it that way, too.
For batch processing you can pass input and output through a temp file.
unixCmd runs asynchronously, so you could spawn multiple sclang processes in parallel, each with its own temp file. The subprocess reads the input data from the temp file and then overwrites the same file with the output. In the action function for unixCmd you can then get the output from the temp file.
If you do everything in a Routine, you can use a CondVar + a counter to wait for all subprocesses to finish. First you initialize the counter with the number of subprocesses. Each action function would decrement the counter and signal the CondVar. The routine would wait on the CondVar until the counter has become zero.
Of course, you don’t have to use sclang as the subprocess, you can just as well use a python script. However, communication via the temp file might be easier with sclang because you can easily serialize the data with writeArchive and readArchive.