问题
I need to implement concurrent matrix multiplication in C using multiple processes. I understand that because each process has its own private address space, I will have to use some form of interprocess communication (IPC). I did some looking around and couldn't find many implementations that didn't use threads. I was wondering if anyone knew the most best way to go about this, either using shared memory, message passing, or pipes? I am not asking for a solution, but rather, if anyone knows, which of these methods will be more efficient with matrix multiplication. Or if there is a common standard way to do this with multiple processes?
回答1:
The most efficient way of concurrently processing matrix multiplications would be shared memory. In this way, you don't have to serialize your matrix through a pipe/message and you can directly apply your multiplications on your shared memory space.
回答2:
Shared memory will be a good solution for this problem, I think. The processes can compute their solutions and share one memory to take the solutions together.
#include <sys/ipc.h>
#include <sys/shm.h>
int shmget(key_t key, int size, int shmflg);
Is one of the C functions for shared memory (you'll need shmat()
and maybe shmdt()
and shmctl()
too).
Also you have to care about synchronization so that the processes does not manipulate the computation from each other.
I would use semaphores for that: see Semaphores in C and Semaphore Wikipedia
来源:https://stackoverflow.com/questions/12569785/most-efficient-matrix-multiplication-in-c-using-fork-and-ipc