Answer the question
In order to leave comments, you need to log in
Analogue of global variables in MPI?
How can you organize something like a global variable in MPI ? Interested in any option (file system, interprocessor interaction...). Must be extensible. Testing on 4 processors, will run on 4096.
Program blank:
Launch:
What should be? The get_new_d function must return each time the value of d is 1 greater than the maximum d among all processes. Something like:
Who will tell you (or rather show) something like a semaphore (mutex) in MPI will also be happy.
#include <cstdio>
#include <vector>
#include <mpi.h>
#include "tools.h"
using namespace std;
unsigned long long d = 0;
unsigned long long get_new_d(int ccpu)
{
d++;
return d;
}
int main(int argc, char *argv[])
{
int ccpu, nprocs;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
MPI_Comm_rank(MPI_COMM_WORLD, &ccpu);
printf("%d: %lld\n",ccpu,get_new_d(ccpu));
MPI_Finalize();
return 0;
}
#!/bin/bash
N_PROCESSORS=4
mpiexec -n ${N_PROCESSORS} bent_mpi
3: 1
0: 2
1: 3
2: 4
Answer the question
In order to leave comments, you need to log in
The first thing that came to mind was to select a process (for example, 0), which changes and returns the value of the variable, the rest of the processes will request the value and change from the 0th process. Since the processes are independent, the semaphore is not needed. The process itself, of course, can also request the value of a variable from itself.
okazymyrov , you have a rather strange task.
Please tell me why and how often do you plan to use this global counter?
In fact, you are trying to add a strictly sequential section to a parallel system, which can lead to significant performance degradation, and the more cores, the greater the degradation. 4096 cores is already big enough to start looking at the performance penalty due to syncs.
Write the problem you are solving, maybe you can come up with a more efficient solution for it than the global counter.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question