(C++MPI )error: reference to ‘rank’ is ambiguous

Hi,

I am trying to compile a MPI C++ Code:

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
35
36

#include <iostream>
#include <cmath>
#include <mpi.h>
#include <ctime>
#include <vector>

using namespace std;

// Sorts the input row into chunks to be scattered two all the processors.
void sortByProcess(vector<double> list1, double* list2, int count);

// Swaps two rows.
void swap(double** list, int count, int row1, int row2);



int rank, size;
int main(int argc, char * argv[])
{
  double sTime, eTime, rTime;
  ifstream inFile;
  int num_rows = 3200;
  int num_cols = 3200;
  int cur_control = 0;
  double * send_buffer = NULL;
  double * recv_buffer = NULL;
  double ** data = NULL;
  double determinant;
  vector<double> file_buffer;

  // Just get the initialization of the program going.
  MPI_Init(&argc, &argv);
  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  MPI_Comm_size(MPI_COMM_WORLD, &size);


I am getting following error:


$ mpic++ gaussian.cpp
gaussian.cpp: In function ‘int main(int, char**)’:
gaussian.cpp:28:12: error: aggregate ‘std::ifstream inFile’ has incomplete type and cannot be defined
ifstream inFile;
^~~~~~
gaussian.cpp:40:34: error: reference to ‘rank’ is ambiguous
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
^~~~
gaussian.cpp:24:5: note: candidates are: int rank
int rank, size;
^~~~
In file included from /usr/include/c++/7/bits/move.h:54:0,
from /usr/include/c++/7/bits/nested_exception.h:40,
from /usr/include/c++/7/exception:143,
from /usr/include/c++/7/ios:39,
from /usr/include/c++/7/ostream:38,
from /usr/include/c++/7/iostream:39,
from gaussian.cpp:8:
/usr/include/c++/7/type_traits:1468:12: note: template<class> struct std::rank
struct rank
^~~~
gaussian.cpp:52:7: error: reference to ‘rank’ is ambiguous
if(!rank)
^~~~
gaussian.cpp:24:5: note: candidates are: int rank
int rank, size;
^~~~
In file included from /usr/include/c++/7/bits/move.h:54:0,
from /usr/include/c++/7/bits/nested_exception.h:40,
from /usr/include/c++/7/exception:143,
from /usr/include/c++/7/ios:39,
from /usr/include/c++/7/ostream:38,
from /usr/include/c++/7/iostream:39,
from gaussian.cpp:8:
/usr/include/c++/7/type_traits:1468:12: note: template<class> struct std::rank
struct rank
^~~~
gaussian.cpp:76:9: error: reference to ‘rank’ is ambiguous
if(!rank)
^~~~
gaussian.cpp:24:5: note: candidates are: int rank
int rank, size;
^~~~
In file included from /usr/include/c++/7/bits/move.h:54:0,
from /usr/include/c++/7/bits/nested_exception.h:40,
from /usr/include/c++/7/exception:143,
from /usr/include/c++/7/ios:39,
from /usr/include/c++/7/ostream:38,
from /usr/include/c++/7/iostream:39,
from gaussian.cpp:8:
/usr/include/c++/7/type_traits:1468:12: note: template<class> struct std::rank
struct rank
^~~~
gaussian.cpp:121:23: error: reference to ‘rank’ is ambiguous
if(cur_control == rank)
^~~~
gaussian.cpp:24:5: note: candidates are: int rank
int rank, size;
^~~~
In file included from /usr/include/c++/7/bits/move.h:54:0,
from /usr/include/c++/7/bits/nested_exception.h:40,
from /usr/include/c++/7/exception:143,
from /usr/include/c++/7/ios:39,
from /usr/include/c++/7/ostream:38,
from /usr/include/c++/7/iostream:39,
from gaussian.cpp:8:
/usr/include/c++/7/type_traits:1468:12: note: template<class> struct std::rank
struct rank
^~~~
gaussian.cpp:144:23: error: reference to ‘rank’ is ambiguous
if(cur_control == rank)
^~~~
gaussian.cpp:24:5: note: candidates are: int rank
int rank, size;
^~~~
In file included from /usr/include/c++/7/bits/move.h:54:0,
from /usr/include/c++/7/bits/nested_exception.h:40,
from /usr/include/c++/7/exception:143,
from /usr/include/c++/7/ios:39,
from /usr/include/c++/7/ostream:38,
from /usr/include/c++/7/iostream:39,
from gaussian.cpp:8:
/usr/include/c++/7/type_traits:1468:12: note: template<class> struct std::rank
struct rank
^~~~
gaussian.cpp:162:23: error: reference to ‘rank’ is ambiguous
if(cur_control == rank)
^~~~
gaussian.cpp:24:5: note: candidates are: int rank
int rank, size;
^~~~
In file included from /usr/include/c++/7/bits/move.h:54:0,
from /usr/include/c++/7/bits/nested_exception.h:40,
from /usr/include/c++/7/exception:143,
from /usr/include/c++/7/ios:39,
from /usr/include/c++/7/ostream:38,
from /usr/include/c++/7/iostream:39,
from gaussian.cpp:8:
/usr/include/c++/7/type_traits:1468:12: note: template<class> struct std::rank
struct rank
^~~~
gaussian.cpp:196:7: error: reference to ‘rank’ is ambiguous
if(!rank)
^~~~
gaussian.cpp:24:5: note: candidates are: int rank
int rank, size;
^~~~
In file included from /usr/include/c++/7/bits/move.h:54:0,
from /usr/include/c++/7/bits/nested_exception.h:40,
from /usr/include/c++/7/exception:143,
from /usr/include/c++/7/ios:39,
from /usr/include/c++/7/ostream:38,
from /usr/include/c++/7/iostream:39,
from gaussian.cpp:8:
/usr/include/c++/7/type_traits:1468:12: note: template<class> struct std::rank
struct rank


Some body please guide me.

Zulfi.
> error: aggregate ‘std::ifstream inFile’ has incomplete type and cannot be defined
> ifstream inFile;
#include <fstream>

> error: reference to ‘rank’ is ambiguous
> candidates are: int rank
> template<class> struct std::rank
using namespace std; pollutes your naming, don't use it.
Hi,
I have made the changes:
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
 #include <fstream>
#include <cmath>
#include <mpi.h>
#include <ctime>
#include <vector>

//using namespace std;

// Sorts the input row into chunks to be scattered two all the processors.
void sortByProcess(vector<double> list1, double* list2, int count);

// Swaps two rows.
void swap(double** list, int count, int row1, int row2);



int rank, size;
int main(int argc, char * argv[])
{
  double sTime, eTime, rTime;
  ifstream inFile;
  int num_rows = 3200;
  int num_cols = 3200;
  int cur_control = 0;
  double * send_buffer = NULL;
  double * recv_buffer = NULL;
  double ** data = NULL;
  double determinant;
  vector<double> file_buffer;

  // Just get the initialization of the program going.
  MPI_Init(&argc, &argv);
  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  MPI_Comm_size(MPI_COMM_WORLD, &size);

  // If the input file is not given, print message and exit.
  if(argc < 2)
  {
    cout << "No input file given." << endl;
    MPI_Finalize();
    return 0;
  }
  // If the root node (0), then open the input file and read in the
  // number of rows.
  if(!rank)
  {
    inFile.open(argv[1]);
    inFile >> num_rows;
    file_buffer.resize(num_rows);
  }

  send_buffer = new double[num_rows];
  // Broadcasts the number of rows to each processor.
  MPI_Bcast (&num_rows, 1, MPI_INT, 0, MPI_COMM_WORLD);
  num_cols = num_rows / size;
  // Allocate the memory on each processor.
  data = new double*[num_cols];
  for(int i = 0; i < num_cols; i++)
    data[i] = new double[num_rows];
  for(int i = 0; i < num_cols; i++)
  {
    for(int j = 0; j < num_rows; j++)
      data[i][j] = 0;
  }


But I am getting following errors:

mpic++ gaussian.cpp
gaussian.cpp:17:20: error: variable or field ‘sortByProcess’ declared void
void sortByProcess(vector<double> list1, double* list2, int count);
^~~~~~
gaussian.cpp:17:20: error: ‘vector’ was not declared in this scope
gaussian.cpp:17:20: note: suggested alternative:
In file included from /usr/include/c++/7/vector:64:0,
from gaussian.cpp:12:
/usr/include/c++/7/bits/stl_vector.h:216:11: note: ‘std::vector’
class vector : protected _Vector_base<_Tp, _Alloc>
^~~~~~
gaussian.cpp:17:27: error: expected primary-expression before ‘double’
void sortByProcess(vector<double> list1, double* list2, int count);
^~~~~~
gaussian.cpp:17:42: error: expected primary-expression before ‘double’
void sortByProcess(vector<double> list1, double* list2, int count);
^~~~~~
gaussian.cpp:17:57: error: expected primary-expression before ‘int’
void sortByProcess(vector<double> list1, double* list2, int count);
^~~
gaussian.cpp: In function ‘int main(int, char**)’:
gaussian.cpp:28:3: error: ‘ifstream’ was not declared in this scope
ifstream inFile;
^~~~~~~~
gaussian.cpp:28:3: note: suggested alternative:
In file included from /usr/include/c++/7/ios:38:0,
from /usr/include/c++/7/istream:38,
from /usr/include/c++/7/fstream:38,
from gaussian.cpp:8:
/usr/include/c++/7/iosfwd:162:34: note: ‘std::ifstream’
typedef basic_ifstream<char> ifstream;
^~~~~~~~
gaussian.cpp:36:3: error: ‘vector’ was not declared in this scope
vector<double> file_buffer;
^~~~~~
gaussian.cpp:36:3: note: suggested alternative:
In file included from /usr/include/c++/7/vector:64:0,
from gaussian.cpp:12:
/usr/include/c++/7/bits/stl_vector.h:216:11: note: ‘std::vector’
class vector : protected _Vector_base<_Tp, _Alloc>
^~~~~~
gaussian.cpp:36:10: error: expected primary-expression before ‘double’
vector<double> file_buffer;
^~~~~~
gaussian.cpp:46:5: error: ‘cout’ was not declared in this scope
cout << "No input file given." << endl;
^~~~
gaussian.cpp:46:5: note: suggested alternative:
In file included from /usr/lib/x86_64-linux-gnu/openmpi/include/openmpi/ompi/mpi/cxx/mpicxx.h:55:0,
from /usr/lib/x86_64-linux-gnu/openmpi/include/mpi.h:2704,
from gaussian.cpp:10:
/usr/include/c++/7/iostream:61:18: note: ‘std::cout’
extern ostream cout; /// Linked to standard output
^~~~
gaussian.cpp:46:39: error: ‘endl’ was not declared in this scope
cout << "No input file given." << endl;


Some body please guide me.

Zulfi.
Hi,
I am able to remove all the errors.


Zulfi.
Most problems will go away when you put 'std::' in front of the standard components like

1
2
std::vector<double>
std::cout
Most problems will go away when you stop using rank and size as global variables. They should be defined in int main(). If you absolutely need to make them global variables then wrap all your MPI parameters up in a struct.

This code WILL compile.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "mpi.h"
#include <iostream>
using namespace std;

//int rank, size;

int main( int argc, char* argv[] )
{
   int rank, size;
   MPI_Init( &argc, &argv );
   MPI_Comm_rank( MPI_COMM_WORLD, &rank );
   MPI_Comm_size( MPI_COMM_WORLD, &size );
   cout << "Processor " << rank << " of " << size << " is alive\n";
   MPI_Finalize();
}
C:\test>"C:\Program Files\Microsoft MPI\bin"\mpiexec -n 16 try
Processor 12 of 16 is alive
Processor 7 of 16 is alive
Processor 14 of 16 is alive
Processor 10 of 16 is alive
Processor 9 of 16 is alive
Processor 1 of 16 is alive
Processor 13 of 16 is alive
Processor 3 of 16 is alive
Processor 8 of 16 is alive
Processor 5 of 16 is alive
Processor 0 of 16 is alive
Processor 11 of 16 is alive
Processor 6 of 16 is alive
Processor 4 of 16 is alive
Processor 15 of 16 is alive
Processor 2 of 16 is alive



This code WON'T.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include "mpi.h"
#include <iostream>
using namespace std;

   int rank, size;

int main( int argc, char* argv[] )
{
// int rank, size;
   MPI_Init( &argc, &argv );
   MPI_Comm_rank( MPI_COMM_WORLD, &rank );
   MPI_Comm_size( MPI_COMM_WORLD, &size );
   cout << "Processor " << rank << " of " << size << " is alive\n";
   MPI_Finalize();
}
Last edited on
Topic archived. No new replies allowed.