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

📄 run.cpp

📁 MPICH是MPI的重要研究,提供了一系列的接口函数,为并行计算的实现提供了编程环境.
💻 CPP
📖 第 1 页 / 共 3 页
字号:
#include "stdafx.h"#include "guiMPIRun.h"#include "guiMPIRunDoc.h"#include "guiMPIRunView.h"#include "MPIJobDefs.h"#include "mpd.h"#include "global.h"#include "MPICH_pwd.h"#include "LaunchProcess.h"#include "WaitThread.h"#include "UserPwdDialog.h"#include "RedirectIO.h"#include <Winnetwk.h>#include "mpdutil.h"#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endifbool ReadMPDRegistry(char *name, char *value, DWORD *length = NULL);bool ReadMpichRegistry(char *name, char *value, DWORD *length = NULL){    HKEY tkey;    DWORD len, result;        // Open the root key    if ((result = RegOpenKeyEx(HKEY_LOCAL_MACHINE, MPICHKEY,	0, 	KEY_READ,	&tkey)) != ERROR_SUCCESS)    {	//printf("Unable to open the MPD registry key, error %d\n", result);	return false;    }        if (length == NULL)	len = MAX_CMD_LENGTH;    else	len = *length;    result = RegQueryValueEx(tkey, name, 0, NULL, (unsigned char *)value, &len);    if (result != ERROR_SUCCESS)    {	//printf("Unable to read the mpd registry key '%s', error %d\n", name, result);	RegCloseKey(tkey);	return false;    }    if (length != NULL)	*length = len;        RegCloseKey(tkey);    return true;}void WriteMpichRegistry(char *name, char *value){    HKEY tkey;    DWORD result;    // Open the root key    if (RegCreateKeyEx(HKEY_LOCAL_MACHINE, MPICHKEY,	0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &tkey, &result) != ERROR_SUCCESS)    {	return;    }    if ((result = RegSetValueEx(tkey, name, 0, REG_SZ, (const unsigned char *)value, strlen(value)+1)) != ERROR_SUCCESS)    {	//printf("WriteMpichRegistry failed to write '%s:%s', error %d\n", name, value, result);    }    RegCloseKey(tkey);}// Function name	: CtrlHandlerRoutine// Description	    : // Return type		: BOOL WINAPI // Argument         : DWORD dwCtrlTypevoid CGuiMPIRunView::OnBreakBtn(){    // Hit Break once and I'll try to kill the remote processes    if (m_bFirstBreak)    {	// Signal all the threads to stop	m_bNormalExit = false;	SetEvent(m_hAbortEvent);	if (easy_send(m_sockBreak, "x", 1) == SOCKET_ERROR)	{	    if (easy_send(m_sockStopIOSignalSocket, "x", 1) == SOCKET_ERROR)	    {		MessageBox("Break failed", "Error");	    }	}		m_bFirstBreak = false;	m_break_btn.SetWindowText("Abort");	return;    }        // Hit Break twice and I'll close all the connections to the remote processes    Abort();    m_break_btn.SetWindowText("Break");    m_break_btn.EnableWindow(FALSE);    return;}void CGuiMPIRunView::GetHosts(){    // Create a temporary list of all the hosts    HostNode *list = NULL, *p;    if (m_bAnyHosts)    {	list = p = new HostNode;	for (int i=0; i<m_host_list.GetCount(); i++)	{	    m_host_list.GetText(i, p->host);	    if (i<m_host_list.GetCount()-1)	    {		p->next = new HostNode;		p = p->next;	    }	    else		p->next = NULL;	}    }    else    {	int pIndices[1024];	int n;	list = p = new HostNode;	n = m_host_list.GetSelItems(1024, pIndices);	for (int i=0; i<n; i++)	{	    m_host_list.GetText(pIndices[i], p->host);	    if (i<n-1)	    {		p->next = new HostNode;		p = p->next;	    }	    else		p->next = NULL;	}    }    // make the list a loop    p->next = list;    // Delete the old application host list    if (m_pHosts)    {	while (m_pHosts)	{	    p = m_pHosts;	    m_pHosts = m_pHosts->next;	    delete p;	}    }    // Create a list of hosts for this application, looping through the host list if necessary    HostNode *pHost;    int n = m_nproc;    pHost = m_pHosts = new HostNode;    p = list;    while (n)    {	strcpy(pHost->host, p->host);	if (m_bUseSlaveProcess)	{	    strncpy(pHost->exe, m_SlaveProcess, MAX_CMD_LENGTH);	    pHost->exe[MAX_CMD_LENGTH-1] = '\0';	}	else	{	    strncpy(pHost->exe, m_app, MAX_CMD_LENGTH);	    pHost->exe[MAX_CMD_LENGTH-1] = '\0';	}	pHost->nSMPProcs = 1;	if (n>1)	{	    pHost->next = new HostNode;	    pHost = pHost->next;	}	else	    pHost->next = NULL;	p = p->next;	n--;    }    if (m_bUseSlaveProcess && m_pHosts)    {	strncpy(m_pHosts->exe, m_app, MAX_CMD_LENGTH);	m_pHosts->exe[MAX_CMD_LENGTH-1] = '\0';    }    // Delete the temporary host list    p = list->next;    list->next = NULL;    list = p;    while (list)    {	p = list;	list = list->next;	delete p;    }}// Function name	: CreateJobIDFromTemp// Description	    : // Return type		: void // Argument         : char * pszJobIDvoid CreateJobIDFromTemp(char * pszJobID){    // Use the name of a temporary file as the job id    char tBuffer[MAX_PATH], *pszFileName;    GetTempFileName(".", "mpi", 0, pszJobID);    GetFullPathName(pszJobID, 100, tBuffer, &pszFileName);    DeleteFile(pszJobID);    strcpy(pszJobID, pszFileName);}// Function name	: CreateJobID// Description	    : // Return type		: void // Argument         : char * pszJobIDvoid CreateJobID(char * pszJobID){    DWORD ret_val, job_number = 0, type, num_bytes = sizeof(DWORD);    HANDLE hMutex = CreateMutex(NULL, FALSE, "MPIJobNumberMutex");    char 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, "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, "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);        sprintf(pszJobID, "%s.%d", pszHost, job_number);}void CGuiMPIRunView::EnableRunning(){    m_nproc_edit.EnableWindow(FALSE);    m_nproc_spin.EnableWindow(FALSE);    m_app_combo.EnableWindow(FALSE);    m_app_browse_btn.EnableWindow(FALSE);    m_run_btn.EnableWindow(FALSE);    m_advanced_btn.EnableWindow(FALSE);    m_break_btn.EnableWindow();    ResetEvent(m_hJobFinished);    if (m_redirect)    {	//m_fout = fopen(m_output_filename, "a+");	m_fout = fopen(m_output_filename, "w");    }    m_output.SetFocus();}void CGuiMPIRunView::DisableRunning(){    if (!m_bUseConfigFile)    {	m_nproc_edit.EnableWindow();	m_nproc_spin.EnableWindow();	m_app_combo.EnableWindow();	m_app_browse_btn.EnableWindow();    }    m_run_btn.EnableWindow();    m_advanced_btn.EnableWindow();    m_break_btn.EnableWindow(FALSE);    m_break_btn.SetWindowText("Break");    SetEvent(m_hJobFinished);    if (m_redirect && m_fout)    {	fclose(m_fout);	m_fout = NULL;    }}static bool NeedToMap(CString pszFullPath, char *pDrive, CString &pszShare){    DWORD dwResult;    DWORD dwLength;    char pBuffer[4096];    REMOTE_NAME_INFO *info = (REMOTE_NAME_INFO*)pBuffer;    pszFullPath.Remove('"');    dwLength = 4096;    info->lpConnectionName = NULL;    info->lpRemainingPath = NULL;    info->lpUniversalName = NULL;    dwResult = WNetGetUniversalName(pszFullPath, REMOTE_NAME_INFO_LEVEL, info, &dwLength);    if (dwResult == NO_ERROR)    {	*pDrive = pszFullPath[0];	pszShare = info->lpConnectionName;	return true;    }    return false;}static void ExeToUnc(char *pszExe){    DWORD dwResult;    DWORD dwLength;    char pBuffer[4096];    REMOTE_NAME_INFO *info = (REMOTE_NAME_INFO*)pBuffer;    char pszTemp[MAX_CMD_LENGTH];    bool bQuoted = false;    char *pszOriginal;    pszOriginal = pszExe;    if (*pszExe == '"')    {	bQuoted = true;	strncpy(pszTemp, &pszExe[1], MAX_CMD_LENGTH);	pszTemp[MAX_CMD_LENGTH-1] = '\0';	if (pszTemp[strlen(pszTemp)-1] == '"')	    pszTemp[strlen(pszTemp)-1] = '\0';	pszExe = pszTemp;    }    dwLength = 4096;    info->lpConnectionName = NULL;    info->lpRemainingPath = NULL;    info->lpUniversalName = NULL;    dwResult = WNetGetUniversalName(pszExe, REMOTE_NAME_INFO_LEVEL, info, &dwLength);    if (dwResult == NO_ERROR)    {	if (bQuoted)	    sprintf(pszOriginal, "\"%s\"", info->lpUniversalName);	else	    strcpy(pszOriginal, info->lpUniversalName);    }}static void ExeToUnc(CString &pszExe){    DWORD dwResult;    DWORD dwLength;    char pBuffer[4096];    REMOTE_NAME_INFO *info = (REMOTE_NAME_INFO*)pBuffer;    bool bQuoted = false;    if (pszExe[0] == '"')    {	bQuoted = true;	pszExe.Remove('"');    }    dwLength = 4096;    info->lpConnectionName = NULL;    info->lpRemainingPath = NULL;    info->lpUniversalName = NULL;    dwResult = WNetGetUniversalName(pszExe, REMOTE_NAME_INFO_LEVEL, info, &dwLength);    if (dwResult == NO_ERROR)    {	if (bQuoted)	    pszExe = '"' + info->lpUniversalName + '"';	else	    pszExe = info->lpUniversalName;    }}static void SeparateCommand(CString pszApp, CString &pszExe, CString &pszArgs){    int n;    CString str;    char pszTempExe[MAX_CMD_LENGTH], *namepart;    if (GetFullPathName(pszApp, MAX_CMD_LENGTH, pszTempExe, &namepart) == 0)    {	pszArgs = "";	pszExe = pszApp;	return;    }    str = namepart;    *namepart = '\0';    pszExe = pszTempExe;    n = str.FindOneOf(" \t\n");    if (n > 0)    {	pszArgs = str.Right(str.GetLength() - n);	pszExe += str.Left(n);    }    else    {	pszArgs = "";

⌨️ 快捷键说明

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