⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mpirun.cpp

📁 MPICH是MPI的重要研究,提供了一系列的接口函数,为并行计算的实现提供了编程环境.
💻 CPP
📖 第 1 页 / 共 3 页
字号:
		// Advance over white space		while (*pChar != _T('\0') && _istspace(*pChar))			pChar++;		// Get the number of SMP processes		if (*pChar != _T('\0'))		{			node->nSMPProcs = _ttoi(pChar);			if (node->nSMPProcs < 1)				node->nSMPProcs = 1;		}		// Advance over the number		while (*pChar != _T('\0') && _istdigit(*pChar))			pChar++;		// Advance over white space		while (*pChar != _T('\0') && _istspace(*pChar))			pChar++;		// Copy the executable		if (*pChar != _T('\0'))			_tcscpy(node->exe, pChar);	}	return node;}#define PARSE_ERR_NO_FILE  -1#define PARSE_SUCCESS       0// Function name	: ParseConfigFile// Description	    : // Return type		: void // Argument         : LPTSTR filenameint ParseConfigFile(LPTSTR filename){	FILE *fin;	TCHAR buffer[1024] = TEXT("");	fin = _tfopen(filename, TEXT("r"));	if (fin == NULL)	{		//_tprintf(TEXT("Unable to open file: %s\n"), filename);		return PARSE_ERR_NO_FILE;	}	while (_fgetts(buffer, 1024, fin))	{		// Check for the name of the executable		if (_tcsnicmp(buffer, TEXT("exe "), 4) == 0)		{			TCHAR *pChar = &buffer[4];			while (_istspace(*pChar))				pChar++;			_tcscpy(g_pszExe, pChar);			pChar = &g_pszExe[_tcslen(g_pszExe)-1];			while (_istspace(*pChar) && (pChar >= g_pszExe))			{				*pChar = '\0';				pChar--;			}		}		else		// Check for program arguments		if (_tcsnicmp(buffer, TEXT("args "), 5) == 0)		{			TCHAR *pChar = &buffer[5];			while (_istspace(*pChar))				pChar++;			_tcscpy(g_pszArgs, pChar);			pChar = &g_pszArgs[_tcslen(g_pszArgs)-1];			while (_istspace(*pChar) && (pChar >= g_pszArgs))			{				*pChar = '\0';				pChar--;			}		}		else		// Check for environment variables		if (_tcsnicmp(buffer, TEXT("env "), 4) == 0)		{			TCHAR *pChar = &buffer[4];			while (_istspace(*pChar))				pChar++;			_tcscpy(g_pszEnv, pChar);			pChar = &g_pszEnv[_tcslen(g_pszEnv)-1];			while (_istspace(*pChar) && (pChar >= g_pszEnv))			{				*pChar = '\0';				pChar--;			}		}		else		// Check for hosts		if (_tcsnicmp(buffer, TEXT("hosts"), 5) == 0)		{			g_nHosts = 0;			g_pHosts = NULL;			HostNode *node, dummy;			dummy.next = NULL;			node = &dummy;			while (_fgetts(buffer, 1024, fin))			{				node->next = ParseLineIntoHostNode(buffer);				if (node->next != NULL)				{					node = node->next;					g_nHosts++;				}			}			g_pHosts = dummy.next;			fclose(fin);			return PARSE_SUCCESS;		}	}	fclose(fin);	return PARSE_SUCCESS;}// Function name	: GetAccountAndPassword// Description	    : Attempts to read the password from the registry, //	                  upon failure it requests the user to provide one// Return type		: void void GetAccountAndPassword(){	TCHAR ch=0;	int index = 0;		do	{		_ftprintf(stderr, TEXT("account: "));		fflush(stderr);		_getts(g_pszAccount);	} while (_tcslen(g_pszAccount) == 0);		_ftprintf(stderr, TEXT("password: "));	fflush(stderr);		HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);	DWORD dwMode;	if (!GetConsoleMode(hStdin, &dwMode))		dwMode = ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT | ENABLE_MOUSE_INPUT;	SetConsoleMode(hStdin, dwMode & ~ENABLE_ECHO_INPUT);	_getts(g_pszPassword);	SetConsoleMode(hStdin, dwMode);		_ftprintf(stderr, TEXT("\n"));}// Function name	: CtrlHandlerRoutine// Description	    : // Return type		: BOOL WINAPI // Argument         : DWORD dwCtrlTypebool g_bFirst = true;BOOL WINAPI CtrlHandlerRoutine(DWORD dwCtrlType){	if (g_bFirst)	{		fprintf(stderr, "User break\n");		// Signal all the threads to stop		g_bNormalExit = false;		SetEvent(g_hAbortEvent);		g_bFirst = false;		return TRUE;	}	ExitProcess(1);	return TRUE;}// Function name	: CreateJobIDFromTemp// Description	    : // Return type		: void // Argument         : LPTSTR pszJobIDvoid CreateJobIDFromTemp(LPTSTR pszJobID){	// Use the name of a temporary file as the job id	TCHAR tBuffer[MAX_PATH], *pChar;	GetTempFileName(_T("."), _T("mpi"), 0, pszJobID);	GetFullPathName(pszJobID, 100, tBuffer, &pChar);	DeleteFile(pszJobID);	_tcscpy(pszJobID, pChar);}// Function name	: CreateJobID// Description	    : // Return type		: void // Argument         : LPTSTR pszJobIDvoid CreateJobID(LPTSTR pszJobID){	DWORD ret_val, job_number=0, type, num_bytes = sizeof(DWORD);	HANDLE hMutex = CreateMutex(NULL, FALSE, TEXT("MPIJobNumberMutex"));	TCHAR pszHost[100];	DWORD size = 100;	HKEY hKey;	// Synchronize access to the job number in the registry	if ((ret_val = WaitForSingleObject(hMutex, 3000)) != WAIT_OBJECT_0)	{		CloseHandle(hMutex);		CreateJobIDFromTemp(pszJobID);		return;	}	// Open the MPICH root key	if ((ret_val = RegOpenKeyEx(			HKEY_LOCAL_MACHINE, 			MPICHKEY,			0, KEY_READ | KEY_WRITE, &hKey)) != ERROR_SUCCESS)	{		ReleaseMutex(hMutex);		CloseHandle(hMutex);		CreateJobIDFromTemp(pszJobID);		return;	}	// Read the job number	if ((ret_val = RegQueryValueEx(hKey, TEXT("Job Number"), 0, &type, (BYTE *)&job_number, &num_bytes)) != ERROR_SUCCESS)	{		RegCloseKey(hKey);		ReleaseMutex(hMutex);		CloseHandle(hMutex);		CreateJobIDFromTemp(pszJobID);		return;	}	// Increment the job number and write it back to the registry	job_number++;	if ((ret_val = RegSetValueEx(hKey, TEXT("Job Number"), 0, REG_DWORD, (CONST BYTE *)&job_number, sizeof(DWORD))) != ERROR_SUCCESS)	{		RegCloseKey(hKey);		ReleaseMutex(hMutex);		CloseHandle(hMutex);		CreateJobIDFromTemp(pszJobID);		return;	}	RegCloseKey(hKey);	ReleaseMutex(hMutex);	CloseHandle(hMutex);	GetComputerName(pszHost, &size);	_stprintf(pszJobID, TEXT("%s.%d"), pszHost, job_number);}// Function name	: main// Description	    : // Return type		: void // Argument         : int argc// Argument         : TCHAR *argv[]void main(int argc, TCHAR *argv[]){	int i;	int iproc = 0;	TCHAR pszJobID[100];	TCHAR pszEnv[MAX_PATH] = TEXT("");	TCHAR pszDir[MAX_PATH] = TEXT(".");	HANDLE *pThread;	int nShmLow, nShmHigh;	DWORD dwThreadID;	HRESULT hr;	bool bLogon = false;	bool bUseBNRnp = false;	bool bUseMPICH2 = false;	SetConsoleCtrlHandler(CtrlHandlerRoutine, TRUE);#ifdef MULTI_COLOR_OUTPUT	CONSOLE_SCREEN_BUFFER_INFO info;	// Save the state of the console so it can be restored after each change	HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);	GetConsoleScreenBufferInfo(hStdout, &info);	g_ConsoleAttribute = info.wAttributes;#endif	if (argc < 2 || GetOpt(argc, argv, "-help") || GetOpt(argc, argv, "-?") || GetOpt(argc, argv, "/?"))	{		PrintOptions();		return;	}	g_bUseBNR = GetOpt(argc, argv, "-bnr");	bUseMPICH2 = GetOpt(argc, argv, "-mpich2");	if (bUseMPICH2)		g_bUseBNR = true;	if (g_bUseBNR)	{	    if (!LoadBNRFunctions())	    {		printf("Unable to load the BNR process managing dynamic library, exiting\n");		return;	    }		if (BNR_Init() == BNR_FAIL)			g_bUseBNR = false;	}	g_bNoMPI = GetOpt(argc, argv, "-nompi");	GetOpt(argc, argv, "-env", g_pszEnv);	bLogon = GetOpt(argc, argv, "-logon");	if (!GetOpt(argc, argv, "-dir", pszDir))		GetCurrentDirectory(MAX_PATH, pszDir);		if (argc < 2)	{	    PrintOptions();	    return;	}	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(" "));		}		//g_bNoMPI = true;		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;			}			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(" "));			}			if (g_bUseBNR)				bUseBNRnp = true;			else			{				if (!GetAvailableHosts())				{				    RunLocal(true);				    return;				}			}		}		else		if (GetOpt(argc, argv, "-localonly", &g_nHosts))		{

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -