O
O
okazymyrov2012-06-19 20:57:10
C++ / C#
okazymyrov, 2012-06-19 20:57:10

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

3 answer(s)
M
Maxim Pelevin, 2012-06-20
@okazymyrov

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.

V
Vladislav, 2012-06-20
@phprus

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.

I
icc, 2012-06-20
@icc

In MPI, it is better to use MPI_Bcast for such purposes. If you need global variables in their purest form, use the OpenMP + MPI bundle.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question