Ejemplo n.º 1
0
void    RunLocal_All(void)
{
  ibPort = 0;
  RunLocal();

  ibPort = 1;
  RunLocal();

  ibPort = 2;
  RunLocal();

  ibPort = 3;
  RunLocal();
}
Ejemplo n.º 2
0
void main(int argc, char *argv[])
{
	int error, i;
	DWORD dwNumWritten;
	char pszUserName[100], pszPipeName[MAX_PATH];
	DWORD length;
	char pBuffer[4096];
	int nGroupId;
	int nNproc = 1;
	bool bGetHosts = false;
	bool bUseNP = false;
	//char pszCmdLine[1024];
	WSADATA wsaData;
	int err;

	//TCHAR pszJobID[100];
	//TCHAR pszEnv[MAX_PATH] = TEXT("");
	TCHAR pszDir[MAX_PATH] = TEXT(".");

	// Start the Winsock dll.
	if ((err = WSAStartup( MAKEWORD( 2, 0 ), &wsaData )) != 0)
	{
		printf("Winsock2 dll not initialized, error: %d\n", err);
		return;
	}

	/*
	bGetHosts = !GetOpt(argc, argv, "-np", &nNproc);

	if (argc == 1)
	{
		printf("No command line specified\n");
		return;
	}
	//*/
	GetOpt(argc, argv, "-env", g_pszEnv);
	if (!GetOpt(argc, argv, "-dir", pszDir))
		GetCurrentDirectory(MAX_PATH, pszDir);

	SetCurrentDirectory(pszDir);
	
	DWORD dwType;
	if (GetBinaryType(argv[1], &dwType))
	{
		// The first argument is an executable so set things up to run one process
		g_nHosts = 1;
		TCHAR pszTempExe[MAX_PATH], *namepart;
		_tcscpy(g_pszExe, argv[1]);
		GetFullPathName(g_pszExe, MAX_PATH, pszTempExe, &namepart);
		// Quote the executable in case there are spaces in the path
		_stprintf(g_pszExe, TEXT("\"%s\""), pszTempExe);
		g_pszArgs[0] = TEXT('\0');
		for (int i=2; i<argc; i++)
		{
			_tcscat(g_pszArgs, argv[i]);
			if (i < argc-1)
				_tcscat(g_pszArgs, TEXT(" "));
		}
		RunLocal(true);
		return;
	}
	else
	{
		if (GetOpt(argc, argv, "-np", &g_nHosts))
		{
			if (g_nHosts < 1)
			{
				printf("Error: must specify a number greater than 0 after the -np option\n");
				return;
			}
			if (argc < 2)
			{
				printf("Error: not enough arguments.\n");
				return;
			}
			_tcscpy(g_pszExe, argv[1]);
			g_pszArgs[0] = TEXT('\0');
			for (int i=2; i<argc; i++)
			{
				_tcscat(g_pszArgs, argv[i]);
				if (i < argc-1)
					_tcscat(g_pszArgs, TEXT(" "));
			}
			bUseNP = true;
		}
		else
		if (GetOpt(argc, argv, "-localonly", &g_nHosts))
		{
			bool bDoSMP = !GetOpt(argc, argv, "-tcp");
			if (g_nHosts < 1)
			{
				printf("Error: must specify a number greater than 0 after the -localonly option\n");
				return;
			}
			if (argc < 2)
			{
				printf("Error: not enough arguments.\n");
				return;
			}
			TCHAR pszTempExe[MAX_PATH], *namepart;
			_tcscpy(g_pszExe, argv[1]);
			GetFullPathName(g_pszExe, MAX_PATH, pszTempExe, &namepart);
			// Quote the executable in case there are spaces in the path
			_stprintf(g_pszExe, TEXT("\"%s\""), pszTempExe);
			g_pszArgs[0] = TEXT('\0');
			for (int i=2; i<argc; i++)
			{
				_tcscat(g_pszArgs, argv[i]);
				if (i < argc-1)
					_tcscat(g_pszArgs, TEXT(" "));
			}
			RunLocal(bDoSMP);
			return;
		}
		else
		{
			ParseConfigFile(argv[1]);
			if ((_tcslen(g_pszArgs) > 0) && (argc > 2))
				_tcscat(g_pszArgs, TEXT(" "));
			for (int i=2; i<argc; i++)
			{
				_tcscat(g_pszArgs, argv[i]);
				if (i < argc-1)
					_tcscat(g_pszArgs, TEXT(" "));
			}
		}
	}

	TCHAR pszTempExe[MAX_PATH], *namepart;
	GetFullPathName(g_pszExe, MAX_PATH, pszTempExe, &namepart);
	// Quote the executable in case there are spaces in the path
	_stprintf(g_pszExe, TEXT("\"%s\""), pszTempExe);


	// Figure out how many processes to launch
	nNproc = 0;
	if (bUseNP)
		nNproc = g_nHosts;
	else
	{
		HostNode *n = g_pHosts;
		while (n)
		{
			nNproc += n->nSMPProcs;
			n = n->next;
		}
	}

	length = 100;
	if (GetUserName(pszUserName, &length))
		sprintf(pszPipeName, "\\\\.\\pipe\\mpd%s", pszUserName);
	else
		strcpy(pszPipeName, "\\\\.\\pipe\\mpdpipe");
	
	//printf("MPIRunMPD connecting to pipe '%s'\n", pszPipeName);
	HANDLE hPipe = CreateFile(
		pszPipeName,
		GENERIC_READ | GENERIC_WRITE,
		0, NULL,
		OPEN_EXISTING,
		0, NULL);
	
	if (hPipe != INVALID_HANDLE_VALUE)
	{
		HANDLE hOutputPipe;
		HANDLE hIOThread, hReadyEvent;
		
		strcat(pszPipeName, "out");
		hOutputPipe = CreateNamedPipe(
			pszPipeName,
			PIPE_ACCESS_DUPLEX | FILE_FLAG_WRITE_THROUGH,
			PIPE_TYPE_MESSAGE | PIPE_READMODE_BYTE | PIPE_WAIT,
			PIPE_UNLIMITED_INSTANCES,
			0,0,0, 
			NULL
			);
		
		if (hOutputPipe == INVALID_HANDLE_VALUE)
		{
			error = GetLastError();
			printf("Unable to create pipe: error %d on pipe '%s'\n", error, pszPipeName);
			CloseHandle(hPipe);
			ExitProcess(error);
		}
		
		WriteFile(hPipe, pszPipeName, strlen(pszPipeName)+1, &dwNumWritten, NULL);
		//printf("MPIRunMPD waiting for connection back on pipe '%s'\n", pszPipeName);
		if (ConnectNamedPipe(hOutputPipe, NULL))
		{
			strcpy(pBuffer, "create group\n");
			WriteFile(hPipe, pBuffer, strlen(pBuffer), &dwNumWritten, NULL);
			GetString(hOutputPipe, pBuffer);
			nGroupId = atoi(pBuffer);

			//printf("group id acquired: %d\n", nGroupId);

			LaunchNode *pList = NULL, *p;

			if (bUseNP)
			{
				p = pList = new LaunchNode;
				pList->pNext = NULL;

				sprintf(pBuffer, "next %d\n", nNproc);
				WriteFile(hPipe, pBuffer, strlen(pBuffer), &dwNumWritten, NULL);
				for (i=0; i<nNproc; i++)
				{
					GetString(hOutputPipe, pBuffer);
					//printf("host%d: %s\n", i, pBuffer);
					//strcpy(p->pszCmdLine, pszCmdLine);
					strcpy(p->pszCmdLine, g_pszExe);
					strcpy(p->pszArgs, g_pszArgs);
					strcpy(p->pszIPPort, pBuffer);
					strcpy(p->pszDir, ".");
					p->pszEnv[0] = '\0';
					p->nIP = 0;
					p->nPort = 0;
					p->pNext = NULL;
					if (i<nNproc-1)
					{
						p->pNext = new LaunchNode;
						p = p->pNext;
					}
				}
			}
			else
			{
				int iproc = 0;
				int nShmLow = 0, nShmHigh = 0;
				unsigned long nCurIP;
				int nCurPort;
				
				while (g_pHosts)
				{
					nShmLow = iproc;
					nShmHigh = iproc + g_pHosts->nSMPProcs - 1;

					sprintf(pBuffer, "find %s\n", g_pHosts->host);
					WriteFile(hPipe, pBuffer, strlen(pBuffer), &dwNumWritten, NULL);
					GetString(hOutputPipe, pBuffer);
					nCurPort = atoi(pBuffer);
					NT_get_ip(g_pHosts->host, &nCurIP);

					for (int i=0; i<g_pHosts->nSMPProcs; i++)
					{
						if (pList == NULL)
						{
							pList = p = new LaunchNode;
							pList->pNext = NULL;
						}
						else
						{
							p->pNext = new LaunchNode;
							p = p->pNext;
							p->pNext = NULL;
						}
						if (strlen(g_pHosts->exe) > 0)
							strcpy(p->pszCmdLine, g_pHosts->exe);
						else
							strcpy(p->pszCmdLine, g_pszExe);
						strcpy(p->pszArgs, g_pszArgs);
						p->nIP = nCurIP;
						p->nPort = nCurPort;
						
						sprintf(p->pszEnv, 
							"MPICH_USE_MPD=1|MPICH_JOBID=mpi%d|MPICH_NPROC=%d|MPICH_IPROC=%d|MPICH_SHM_LOW=%d|MPICH_SHM_HIGH=%d", 
							nGroupId, nNproc, iproc, nShmLow, nShmHigh);
						
						if (strlen(g_pszEnv) > 0)
						{
							strcat(p->pszEnv, "|");
							strcat(p->pszEnv, g_pszEnv);
						}
						iproc++;
					}
					
					HostNode *n = g_pHosts;
					g_pHosts = g_pHosts->next;
					delete n;
				}
			}

			hReadyEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
			DWORD dwThreadID;
			hIOThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)RedirectIOLoopThread, hReadyEvent, 0, &dwThreadID);
			if (WaitForSingleObject(hReadyEvent, 5000) != WAIT_OBJECT_0)
			{
				printf("Wait for hReadyEvent failed, error %d\n", GetLastError());
				ExitProcess(1);
			}

			//printf("IO loop waiting on socket: %s:%d\n", g_pszIOListenHost, g_nIOListenPort);

			// launch processes
			g_nConnectionsLeft = nNproc * 2; // 1 for stdout and 1 for stderr
			p = pList;
			for (i=0; i<nNproc; i++)
			{
				if (i == 0)
					sprintf(pBuffer, "launch h'%s'c'%s'a'%s'g'%d'r'%d'0'%s:%d'1'%s:%d'2'%s:%d'\n", 
						p->pszIPPort, p->pszCmdLine, p->pszArgs, nGroupId, i, 
						g_pszIOListenHost, g_nIOListenPort, 
						g_pszIOListenHost, g_nIOListenPort, 
						g_pszIOListenHost, g_nIOListenPort);
				else
					sprintf(pBuffer, "launch h'%s'c'%s'a'%s'g'%d'r'%d'1'%s:%d'2'%s:%d'\n", 
						p->pszIPPort, p->pszCmdLine, p->pszArgs, nGroupId, i, 
						g_pszIOListenHost, g_nIOListenPort, 
						g_pszIOListenHost, g_nIOListenPort);
				WriteFile(hPipe, pBuffer, strlen(pBuffer), &dwNumWritten, NULL);
				p = p->pNext;
				delete pList;
				pList = p;
			}

			strcpy(pBuffer, "done\n");
			WriteFile(hPipe, pBuffer, strlen(pBuffer), &dwNumWritten, NULL);

			CloseHandle(hPipe);
			CloseHandle(hOutputPipe);

			WaitForSingleObject(g_hNoMoreConnectionsEvent, INFINITE);
		}
		else
		{
			error = GetLastError();
			printf("unable to connect to client pipe: error %d\n", error);
			CloseHandle(hPipe);
			CloseHandle(hOutputPipe);
		}
	}
	WSACleanup();
}