I'm trying to start up another process that calls the first and joins it via MPI, but I'm getting an access violation I can't figure out. I think the code should be pretty self explanatory, the access violation hits on the MPI_COMM_ACCEPT line. I think everything looks more or less in line, it should work, but it won't.
If I'm going about this all wrong and there's a simpler way, let me know. I'm not using mpiexec as I'm trying to do this w/in a test framework that constructs the whole mess, but if that makes way more sense, then just tell me I've made a botch of it.
#include <windows.h>
#include <AtlBase.h>
#include <atlconv.h>
#include <iostream>
#include "mpi.h"
#include <string>
int main(int argc, char** argv)
{
MPI_Init(&argc, &argv);
MPI_Comm intercomm;
if (argc == 1)
{
PROCESS_INFORMATION pi;
STARTUPINFO si;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
std::string x = std::string(argv[0]);
x += " ";
x += std::to_string(1);
int res = CreateProcess(NULL, CA2T(x.c_str()), NULL, NULL, false, NORMAL_PRIORITY_CLASS | CREATE_NO_WINDOW, NULL, NULL, &si, &pi);
std::cout << res <<std::endl;
MPI_Open_port(MPI_INFO_NULL, "A");
MPI_Comm_accept("A", MPI_INFO_NULL, 0, MPI_COMM_SELF,&intercomm);
std::cout << MPI_Comm_size(intercomm, &res);
std::cout << res;
}
else
{
MPI_Comm_connect("A", MPI_INFO_NULL, 0, MPI_COMM_SELF, &intercomm);
}
MPI_Finalize();
// }
}
Edit: WORKS! THIS WAS VERY FRUSTRATING BUT IT WORKS!
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <mpi.h>
#include <windows.h>
#include <AtlBase.h>
#include <atlconv.h>
#include <iostream>
#include "mpi.h"
#include <string>
int main(int argc, char** argv)
{
char myPort[MPI_MAX_PORT_NAME];
MPI_Init(&argc, &argv);
MPI_Comm intercomm;
if (argc == 1)
{
PROCESS_INFORMATION pi;
STARTUPINFO si;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
MPI_Open_port(MPI_INFO_NULL, myPort);
std::string x = std::string(argv[0])+" \""+myPort+"\"";
std::cout <<"OLDPROCESS:" << x << std::endl;
int res = CreateProcess(NULL, CA2T(x.c_str()), NULL, NULL, false, NORMAL_PRIORITY_CLASS , NULL, NULL, &si, &pi);
MPI_Comm_accept(myPort, MPI_INFO_NULL, 0, MPI_COMM_SELF, &intercomm);
std::cout << MPI_Comm_size(intercomm, &res);
}
else
{
std::cout << "NEWPROCESS:"<<argv[1] << std::endl;
strcpy_s(myPort,argv[1]);
MPI_Comm_connect(myPort, MPI_INFO_NULL, 0, MPI_COMM_SELF, &intercomm);
}
MPI_Finalize();
// }
}