0

I have multiple MPI processes running in parallel all these processes access a shared memory and performs following operations :

sem_wait(sem);   
shmid = shmget(key, sizeof(int), 0777)) < 0)        
shm = shmat(shmid, NULL, 0);
printf("process id = %d, shm = ",rank, *shm);//rank from MPI_Comm_rank()
new_val = *shm+1; 
*shm = new_val; 
sem_post(sem);

This code is a part of a much bigger code which I cannot put here so I have given only a small part which I think needs to be changed.

I get the follwing result (for first run):

process id = 1 shm = 1
process id = 2 shm = 2
process id = 3 shm = 3
process id = 4 shm = 4
process id = 5 shm = 5

I get the follwing result (for second run):

process id = 2 shm = 1
process id = 3 shm = 2
process id = 1 shm = 3
process id = 5 shm = 4
process id = 4 shm = 5

Similarly different results for different runs.

Is it possible to change the code in a way that it generates same value everytime?

I am working on C in linux environment.

I badly need repeatability in my code. Any kind of help would be greatly appreciated. Thanks !

4

1 回答 1

0

在不考虑这是否是一个好主意的情况下,您似乎希望在各个进程之间对系统调用进行确定的排序。使用 MPI 可以做到这一点——例如:

for (int ii = 0; ii < MPI_Comm_size(); ii++) {
    if ii == MPI_Comm_rank() {
        // attach shmem
    }
    MPI_Barrier(MPI_COMM_WORLD);  
}

这将使进程“轮流”附加到共享内存。

于 2013-07-04T14:17:23.390 回答