Running a function on two signals in parallel

Hello
Recently, I wrote a function that finds an optimum value of a signal's parameter. More specifically, suppose that the name of my function is Erf and gets a signal, x, as an input- Erf(x). When I run Erf (x), it returns a value, say 0.64.
My question is how I can run this function on different signals in parallel?
I mean, assume that I have two signals, x and y. I want to find Erf (x) and Erf (y) in parallel. To date, I have run the Erf function on x first and then on y to find the optimum value of a parameter for each signal.
Is there any method to achieve the mentioned aim simultaneously?
I should mention that I have an Nvidia GPU , and therefore if there exist any than can address the problem using GPU, please mention it and provide some detail.
Many Thanks

 Respuesta aceptada

Walter Roberson
Walter Roberson el 29 de Mayo de 2021

1 voto

Yes, it is possible. Much of the time it is not beneficial.
You need the Parallel Computing Toolbox. You would create a parpool with two members. You would use one of:
  • spmd
  • parfor
  • parfeval or parfevalOnAll
If you use spmd then the two workers can communicate with each other using labSend() and labReceive(), but you cannot communicate with the controller until the spmd completely finishes.
If you use parfor then the workers cannot directly communicate with each other. It is possible to create parallel data queues to send results back from the worker to the controller, and it is possible (but usually awkward) to have the workers create parallel data queues and send those back to the controller and that permits the controller to send data to the workers. The workers cannot communicate directly: they would have to send data to the controller and the controller would have to send it to the other workers. Normal control over activity in the controller does not resume until the parfor completely finishes on all workers.
If you use parfeval or parfevalOnAll, then you can continue on in the controller while a worker processes the task, asynchronous execution. However, it becomes awkward to communicate with the workers during execution.
A lot of the time it turns out that the overhead of sending data to the workers and getting results back, and any communication in the meanwhile, adds up to make using parallel processing slower in many cases. Also, if the individual tasks involve heavy mathematical calculation, then unless you allocate a bunch of cores to each worker, then the workers default to running on a single core each, and so cannot take advantage of the high performance built-in parallel operations such as matrix multiplication that is tuned to be cache-friendly.
Only one worker at a time can use a GPU. and a worker can only use one GPU at a time. If you only have one GPU, then if both workers tried to access it, MATLAB would need to continually steal it away from the other worker, forcing a full state synchronization each time, which is one of the most expensive GPU operations.

5 comentarios

Erfanandoaut
Erfanandoaut el 29 de Mayo de 2021
Editada: Erfanandoaut el 29 de Mayo de 2021
Thanks for your answer, but there is only one worker, and that's me.
By running in parallel, I aim to obtain the Erf (x) and Erf (y) faster. Since computing Erf (x) is not dependant on computing Erf (y), their implementation can be done more quickly with parallel computation, like the computation in a neural network, which is done faster using GPU these days.
Walter Roberson
Walter Roberson el 29 de Mayo de 2021
worker is a technical term here for the process (or thread) that is performing an independent computation under the control of something else (the controller.) You might have heard of these concepts referenced under the name "slave" for the worker and "master" for the controller. Because you want Erf(x) and Erf(y) to be computed simultaneously, you would need two workers: one worker to compute Erf(x) and the other to compute Erf(y)
Since computing Erf (x) is not dependant on computing Erf (y), their implementation can be done more quickly with parallel computation,
I hire a residential snow removal company during the winter (it is common for winters here to have days below -30C). The company brings in a team of one to six people, depending on the amount of snow, and when the team works together with all the proper equipment, they can get the task done much faster than I could possibly do the work myself. And meantime while they are working, I can be doing whatever else I need to be doing. Efficient parallel processing, right?
But every year I have to contact the company and arrange for their services, and negotiate what areas they are going to plow, and negotiate a price: these are "overhead" that would not exist if I were doing the snow removal myself. And when it is snowing and I need the services of the company, the company takes their own time arriving, often not arriving until about 8 hours after the snow stops. This is "latency" of service: it takes time for orders to go out, for the company to organize itself and wake the people up, and meet together and travel to the various clients. Indeed, except for the heavier snows, it would almost always be faster for me to do the shoveling myself. The company also has a policy that they do not come out unless there has been at least 1 cm of snow. It is amazing to see the company go after a deep snow, with snow blower, power blower, and multiple people shoveling. But except for the deepest snow, the team ends up spending more time traveling between clients than it ends up spending at any one client.
So... NO. Just because you compute things in parallel does not mean that it will be faster to do so than to compute in serial. There are cases where it can be faster to compute in parallel. When the computing snow is deep enough. But that does not always happen. In parallel computing, much of the time it does not happen: parallel is slower in a lot of the cases. It just happens to be faster in some useful cases.
Erfanandoaut
Erfanandoaut el 30 de Mayo de 2021
Again thanks, but I think that I could not have expressed what I mean correctly.
Assume that you have two signals x,y. How do you compute the FFT of these signals? You compute the FFT of x and later the one for y? As you probably know, we can put each of these signals in a row of a matrix, e.g., X, and then compute FFT(X). Matlab returns a matrix in that the FFT(x) is placed in the first row and FFT(y) in the second row. The later way of computing is faster than the former.
I think this is also a kind of parallelization. Is there such a method for my problem?
Walter Roberson
Walter Roberson el 30 de Mayo de 2021
What you describe for fft is vectorization. It is present for most build-in operations. Whether it works for your code depends upon how you write your code.
Erfanandoaut
Erfanandoaut el 30 de Mayo de 2021
Editada: Erfanandoaut el 30 de Mayo de 2021
Thanks Walter.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Startup and Shutdown en Centro de ayuda y File Exchange.

Preguntada:

el 28 de Mayo de 2021

Editada:

el 9 de Dic. de 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by