Global variables in MPI c++

Hello,

I am writing an mpi code. I want one variable to be writable and readable by all threads.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <mpi.h>
#include <stdio.h>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <stdlib.h>
#include <algorithm>
#include <unistd.h>

using namespace std;

//Defining the GLOBAL variable I need
int COMPLETE=0;

int main(int argc, char * argv[])
{
    
    MPI::Init(argc, argv);

    //MPI job

    //Updating GLOBAL variable
    COMPLETE++;

    if (COMPLETE==Vector.size())
    {
       //Do something
    }
    MPI::Finalize();

    return 0;

}


Code like above does not work since COMPLETE variable is used independantly in all classes. What I want is to update this variable by ++1 every time a thread finishes.

My question is:
1/ How can I set I global variable for MPI?
2/ How should I synchronyze writing permissions for this variable

Thank you for your time.
SDA
This is not really a "General C++ Programming" question. While you may get a useful answer here, you are much more likely to get such questions answered on a forum dedicated to MPI programming.
Topic archived. No new replies allowed.