Why MPI doesn't work in Visual Studio 2015? - c++

I downloaded and installed mpich2-1.0.8p1-win-x86-64.msi from the console with the administrator rights. I created empty win32 console project, I created file code.cpp and I pasted this example code.
#include <stdio.h>
#include "mpi.h"
int main(int argc, char* argv[])
{
int ProcNum, ProcRank, RecvRank;
MPI_Status Status;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &ProcNum);
MPI_Comm_rank(MPI_COMM_WORLD, &ProcRank);
if (ProcRank == 0)
{
printf("\n Hello from process %3d", ProcRank);
for (int i = 1; i < ProcNum; i++)
{
MPI_Recv(&RecvRank, 1, MPI_INT, MPI_ANY_SOURCE,
MPI_ANY_TAG, MPI_COMM_WORLD, &Status);
printf("\n Hello from process %3d", RecvRank);
}
}
else
MPI_Send(&ProcRank, 1, MPI_INT, 0, 0, MPI_COMM_WORLD);
MPI_Finalize();
return 0;
}
Later I went to project properties to VC++ Directories and added include directories and library directories. In Linker/Input/Additional Dependencies I wrote mpi.lib and in C/C++/Language I allowed Open MP Support. When I compiled my project I have got strange errors. Can you help me? I can't understand what I made wrong, because I did it with tutorials.

Your first (and only) warning states that you are linking a 64-bit library with a 32-bit build. You need to either provide a 32-bit library or build for a 64-bit architecture to get rid of the linker errors.

Related

MPI only compiles in Visual Studio debug section

I have this program
#include <stdio.h>
#include <mpi.h>
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
int size;
int rank;
MPI_Init(NULL, NULL);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
printf("Hello, World!");
cout << MPI_Comm_rank << " " << MPI_Comm_size << endl;
MPI_Finalize();
return 0;
}
When I debug this in Microsoft Visual Studios it works just fine. But when I try it in the command prompt or any terminal I get this error,
Project_1.cpp:2:10: fatal error: mpi.h: No such file or directory
2 | #include <mpi.h>
| ^~~~~~~
compilation terminated.
I have g++ installed already and it works with other programs just fine. I have MPI installed too. Was able to link it to my Microsoft Visual Studio but it only works in its debug section. I am using a windows computer. Not really sure what to do. Any help would be great thanks.
There are some problem with your program:
You don't define MPI_Comm_rank and MPI_Comm_size but you try to print them in cout. In the following piece of code, I've rewritten your code.
#include <stdio.h>
#include <mpi.h>
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
int size;
int rank;
MPI_Init(NULL, NULL);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
printf("Hello, World!");
cout << rank << " " << size << endl;
MPI_Finalize();
return 0;
}
You also need to add the location of mpiexec.exe to your system PATH by :
1- Open Window’s System Properties dialog box => click on Environment Variables
2- Click edit for the Path variable => add C: Program Files\Microsoft MPI\Bin \ (Assuming the default install)
To include the appropriate libraries.
1- The MPI code should be compiled as a Console Application
2- Once you have created the project you need to add the appropriate library
Right-click on the project in the Solution Explorer and go to Properties
Click on VC++ Directories and (assuming the MPI SDK is installed in the default directory) add:
C:/ Program Files (x86)/Microsoft SDKs/Include to the Include Directories
C:/Program Files (x86)/Microsoft SDKs/Lib/x64 or C:/Program Files (x86)/Microsoft SDKs/Lib/x86 to the Library Directories
Depends on whether you wish to compile your code as 32bit or 64bit
3- Click on Linker =>Input
Under Additional Dependencies add msmpi.lib

Why does my MPI application trigger a breakpoint?

I have a simple MPI application that is used to pass values from process 0 to other processes upon entering the desired value but it is triggering a breakpoint on "else".
What am I missing?
#include "stdafx.h"
#include "mpi.h"
#include "stdio.h"
#include "stdlib.h"
int main(int argc, char* argv[])
{
int rank;
int value;
int size;
MPI_Status status;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
do
{
if (rank == 0)
{
printf("Enter the value: ");
scanf_s("%d", &value);
MPI_Send(&value, 1, MPI_INT, rank + 1, 0, MPI_COMM_WORLD);
}
else
{
MPI_Recv(&value, 1, MPI_INT, rank - 1, 0, MPI_COMM_WORLD,
&status);
if (rank < size - 1)
MPI_Send(&value, 1, MPI_INT, rank + 1, 0, MPI_COMM_WORLD);
}
printf("Process %d got %d ", rank, value);
} while (value >= 0);
MPI_Finalize();
return 0;
}
This is output from debug:
'MPIHelloWorld.exe' (Win32): Loaded 'C:\WINDOWS\SysWOW64\KernelBase.dll'. Cannot find or open the PDB file.
'MPIHelloWorld.exe' (Win32): Loaded 'C:\WINDOWS\SysWOW64\gdi32.dll'. Cannot find or open the PDB file.
'MPIHelloWorld.exe' (Win32): Loaded 'C:\Program Files (x86)\Bonjour\mdnsNSP.dll'. Cannot find or open the PDB file.
MPIHelloWorld.exe has triggered a breakpoint.
MPIHelloWorld.exe has triggered a breakpoint.
The thread 0x3308 has exited with code 0 (0x0).
The thread 0x1dfc has exited with code 0 (0x0).
The thread 0x2e5c has exited with code 0 (0x0).
The program '[4400] MPIHelloWorld.exe' has exited with code 0 (0x0).
UPDATE:
When I press CTRL + 5 I get this error written in my console:
job aborted:
[ ranks ] message
[0] fatal error
Fatal error in MPI_Send:
Invalid rank has value 1 but must be nonnegative and less than 1
Ok, I have finally found an answer!
I couldn't use Cluster Debugger for MPI since I am running Visual Studio 2015
and the problem was that only 1 process was running.
I opened CMD and ran my application using "mpiexec.exe -n 4 myMpiApp.exe" and it worked.

How to specify command line arguments for my C program when running it from the Eclipse IDE

I have some code that is executed from the command line. It takes 3 parameters:
"example.txt" 3 s
I want to be able to run this program from inside of my Eclipse IDE instead of running it from the command line but I do not know how to assign the parameters without creating bugs int the program.
Here is the main method below:
int main(int argc, char **argv) {
if (argc != 4) {
fprintf(stderr, "Usage: %s <input file> <num clusters> "
"<linkage type>\n", argv[0]);
exit(1);
} else {
item_t *items = NULL;
int num_items = process_input(&items, argv[1]);
set_linkage(argv[3][0]);
if (num_items) {
cluster_t *cluster = agglomerate(num_items, items);
free(items);
if (cluster) {
fprintf(stdout, "CLUSTER HIERARCHY\n"
"--------------------\n");
print_cluster(cluster);
int k = atoi(argv[2]);
fprintf(stdout, "\n\n%d CLUSTERS\n"
"--------------------\n", k);
get_k_clusters(cluster, k);
free_cluster(cluster);
}
}
}
return 0;
}
I am using c++ and eclipse IDE.
You basically need to create a debug/run configuration for said project. Go to Run->Debug Configurations, select C/C++ Application, then create a new configuration. After that, you have to specify some information, like the application, the eclipse project, and your program`s arguments, on the Arguments Tab.
Screenshots from here are a bit old, but should give you the idea.
After that, hit Debug or Run, and Eclipse shoud start your program with the given parameters.

Running program through Visual Studio causes it to fail?

I have a problem with a program that I'm writing. It is a command line parser that parses bencode (used in torrent files). The program accepts a filename as it's command line. When I build and run the program in Microsoft Visual Studio 10.0 using the debugging Commmand Line arguments setting to input a command line the program tells me that it failed parsing.
If I open a command prompt and run the program from the command prompt with the same command line, the program works perfectly! What's going on? Is this a common problem with Visual Studio?
I used the debugger in Visual Studio to trace where the program fails and it appears that a call to the stat function ( http://msdn.microsoft.com/en-us/library/14h5k7ff.aspx ) used to get the length of the file returns an error in Visual Studio but works fine when run outside of Visual Studio.
The Code uses a Bencode parser which can be found here: http://funzix.git.sourceforge.net/git/gitweb.cgi?p=funzix/funzix;a=blob;f=bencode/bencode.c
And here is the code for the program:
#include <stdio.h>
#include "../Parse/bencode.h"
int main(int argc, char *argv[]){
if(argc != 2){
printf("Usage: whirlwind filename\n");
return 1;
}
char *buf;
long long len;
be_node *n;
//read the torrent file into a buffer and store at &buf
buf = read_file(argv[1], &len);
if(!buf){
buf = argv[1];
len = strlen(argv[1]);
}
printf("Decoding: %s\n", argv[1]);
n = be_decoden(buf, len);
if(!n){
printf("Parsing failed!\n");
return 1;
}
if(n->type != BE_DICT){
printf("This file is not a valid Bencoded Dictionary.\n");
return 1;
}
int i;
char* keyName;
for(i = 0; i < 10; i++){
keyName = n->val.d[i].key;
if(keyName == "announce"){
printf("\n\n");
}
printf("%s\n", keyName);
if(keyName == "announce"){
printf("\n\n");
}
}
return 0;
}
If you pass a relative path from Visual Studio you should be sure that it resolves correctly when your app runs inside the IDE. This problem arises because, when debugging, the current directory is usually \bin\debug.
To be on the safe side put a full pathname or read the location of your file from a configuration file.

Open MPI program not working when distributing processes among multiple hosts

My test program works fine when I run multiple processes on a single machine.
$ ./mpirun -np 2 ./mpi-test
Hi I'm A:0
Hi I'm A:1
A:1 sending 11...
A:1 sent 11
A:0 received 11 from 1
all workers checked in!
When I run the same program on multiple hosts the process is spawned on each host, but MPI_Send never returns.
$ ./mpirun -np 2 -host A,B ./mpi-test
Hi I'm A:0
Hi I'm B:1
B:1 sending 11...
I've tried a couple other sample MPI programs I found and I ran into the same problem. Any idea what is going wrong?
EDIT: this also runs on a remote machine if all the processes are spawned on that machine.
Code:
#include <mpi.h>
int main(int argc, char** argv)
{
MPI::Init();
int rank = MPI::COMM_WORLD.Get_rank();
int size = MPI::COMM_WORLD.Get_size();
char name[256];
int len;
MPI::Get_processor_name(name, len);
printf("Hi I'm %s:%d\n", name, rank);
if (rank == 0) {
while (size > 1) {
int val;
MPI::Status status;
MPI::COMM_WORLD.Recv(&val, 1, MPI::INT, MPI::ANY_SOURCE, MPI::ANY_TAG, status);
int source = status.Get_source();
printf("%s:0 received %d from %d\n", name, val, source);
size--;
}
printf("all workers checked in!\n");
}
else {
int val = rank + 10;
printf("%s:%d sending %d...\n", name, rank, val);
MPI::COMM_WORLD.Send(&val, 1, MPI::INT, 0, 0);
printf("%s:%d sent %d\n", name, rank, val);
}
MPI::Finalize();
return 0;
}
EDIT: ompi_info
$ ./mpirun --bynode -host A,B --tag-output ompi_info -v ompi full --parsable
[1,0]<stdout>:package:Open MPI user#A Distribution
[1,0]<stdout>:ompi:version:full:1.4.3
[1,0]<stdout>:ompi:version:svn:r23834
[1,0]<stdout>:ompi:version:release_date:Oct 05, 2010
[1,0]<stdout>:orte:version:full:1.4.3
[1,0]<stdout>:orte:version:svn:r23834
[1,0]<stdout>:orte:version:release_date:Oct 05, 2010
[1,0]<stdout>:opal:version:full:1.4.3
[1,0]<stdout>:opal:version:svn:r23834
[1,0]<stdout>:opal:version:release_date:Oct 05, 2010
[1,0]<stdout>:ident:1.4.3
[1,1]<stdout>:package:Open MPI user#B Distribution
[1,1]<stdout>:ompi:version:full:1.4.3
[1,1]<stdout>:ompi:version:svn:r23834
[1,1]<stdout>:ompi:version:release_date:Oct 05, 2010
[1,1]<stdout>:orte:version:full:1.4.3
[1,1]<stdout>:orte:version:svn:r23834
[1,1]<stdout>:orte:version:release_date:Oct 05, 2010
[1,1]<stdout>:opal:version:full:1.4.3
[1,1]<stdout>:opal:version:svn:r23834
[1,1]<stdout>:opal:version:release_date:Oct 05, 2010
[1,1]<stdout>:ident:1.4.3
I ended up upgrading to 1.5.3 on A and installing 1.5.3 on C. I'm not sure whether it was the upgrade, or an issue with B, but everything is working now.
For reference:
original setup: node A (arch linux, Open MPI 1.4.3), node B (ubuntu, Open MPI
1.4.3)
working setup: node A (arch linux, Open MPI 1.5.3), node C (arch linux,
Open MPI 1.5.3)
The usual reason for this is that something is not set up properly on the remote host; it could be login/network problems, or that the MPI libraries/executables or the program itself isn't found on the remote host.
What happens if you try
mpirun -np 2 -host A,B hostname
?