📄 mpirun.cpp
字号:
{ target->next = NULL; strncpy(target->host, n->host, MAX_HOST_LENGTH); target->host[MAX_HOST_LENGTH-1] = '\0'; strncpy(target->exe, g_pszExe, MAX_CMD_LENGTH); target->exe[MAX_CMD_LENGTH-1] = '\0'; if (num_left <= n->nSMPProcs) { target->nSMPProcs = num_left; num_left = 0; } else { target->nSMPProcs = n->nSMPProcs; num_left = num_left - n->nSMPProcs; } if (num_left) { target->next = new HostNode; target = target->next; } n = n->next; if (n == NULL) n = list; } // free the list while (list) { n = list; list = list->next; delete n; } return true;}// Function name : ParseLineIntoHostNode// Description : // Return type : HostNode* // Argument : char * lineHostNode* ParseLineIntoHostNode(char * line){ char buffer[1024]; char *pChar, *pChar2; HostNode *node = NULL; strncpy(buffer, line, 1024); buffer[1023] = '\0'; pChar = buffer; // Advance over white space while (*pChar != '\0' && isspace(*pChar)) pChar++; if (*pChar == '#' || *pChar == '\0') return NULL; // Trim trailing white space pChar2 = &buffer[strlen(buffer)-1]; while (isspace(*pChar2) && (pChar >= pChar)) { *pChar2 = '\0'; pChar2--; } // If there is anything left on the line, consider it a host name if (strlen(pChar) > 0) { node = new HostNode; node->nSMPProcs = 1; node->next = NULL; node->exe[0] = '\0'; // Copy the host name pChar2 = node->host; while (*pChar != '\0' && !isspace(*pChar)) { *pChar2 = *pChar; pChar++; pChar2++; } *pChar2 = '\0'; // Advance over white space while (*pChar != '\0' && isspace(*pChar)) pChar++; // Get the number of SMP processes if (*pChar != '\0') { node->nSMPProcs = atoi(pChar); if (node->nSMPProcs < 1) node->nSMPProcs = 1; } // Advance over the number while (*pChar != '\0' && isdigit(*pChar)) pChar++; // Advance over white space while (*pChar != '\0' && isspace(*pChar)) pChar++; // Copy the executable if (*pChar != '\0') { strncpy(node->exe, pChar, MAX_CMD_LENGTH); node->exe[MAX_CMD_LENGTH-1] = '\0'; ExeToUnc(node->exe); } } return node;}#define PARSE_ERR_NO_FILE -1#define PARSE_SUCCESS 0// Function name : ParseConfigFile// Description : // Return type : int // Argument : char * filenameint ParseConfigFile(char * filename){ FILE *fin; char buffer[1024] = ""; //dbg_printf("parsing configuration file '%s'\n", filename); fin = fopen(filename, "r"); if (fin == NULL) { return PARSE_ERR_NO_FILE; } while (fgets(buffer, 1024, fin)) { // Check for the name of the executable if (strnicmp(buffer, "exe ", 4) == 0) { char *pChar = &buffer[4]; while (isspace(*pChar)) pChar++; strncpy(g_pszExe, pChar, MAX_CMD_LENGTH); g_pszExe[MAX_CMD_LENGTH-1] = '\0'; pChar = &g_pszExe[strlen(g_pszExe)-1]; while (isspace(*pChar) && (pChar >= g_pszExe)) { *pChar = '\0'; pChar--; } ExeToUnc(g_pszExe); } else { // Check for program arguments if (strnicmp(buffer, "args ", 5) == 0) { char *pChar = &buffer[5]; while (isspace(*pChar)) pChar++; strncpy(g_pszArgs, pChar, MAX_CMD_LENGTH); g_pszArgs[MAX_CMD_LENGTH-1] = '\0'; pChar = &g_pszArgs[strlen(g_pszArgs)-1]; while (isspace(*pChar) && (pChar >= g_pszArgs)) { *pChar = '\0'; pChar--; } } else { // Check for environment variables if (strnicmp(buffer, "env ", 4) == 0) { char *pChar = &buffer[4]; while (isspace(*pChar)) pChar++; if (strlen(pChar) >= MAX_CMD_LENGTH) { printf("Warning: environment variables truncated.\n"); fflush(stdout); } strncpy(g_pszEnv, pChar, MAX_CMD_LENGTH); g_pszEnv[MAX_CMD_LENGTH-1] = '\0'; pChar = &g_pszEnv[strlen(g_pszEnv)-1]; while (isspace(*pChar) && (pChar >= g_pszEnv)) { *pChar = '\0'; pChar--; } } else { if (strnicmp(buffer, "map ", 4) == 0) { char *pszMap; pszMap = &buffer[strlen(buffer)-1]; while (isspace(*pszMap) && (pszMap >= buffer)) { *pszMap = '\0'; pszMap--; } pszMap = &buffer[4]; while (isspace(*pszMap)) pszMap++; if (*pszMap != '\0' && strlen(pszMap) > 6 && pszMap[1] == ':') { MapDriveNode *pNode = new MapDriveNode; pNode->cDrive = pszMap[0]; strcpy(pNode->pszShare, &pszMap[2]); pNode->pNext = g_pDriveMapList; g_pDriveMapList = pNode; } } else { if (strnicmp(buffer, "dir ", 4) == 0) { char *pChar = &buffer[4]; while (isspace(*pChar)) pChar++; strcpy(g_pszDir, pChar); pChar = &g_pszDir[strlen(g_pszDir)-1]; while (isspace(*pChar) && (pChar >= g_pszDir)) { *pChar = '\0'; pChar--; } } else { // Check for hosts if (strnicmp(buffer, "hosts", 5) == 0) { g_nHosts = 0; g_pHosts = NULL; HostNode *node, dummy; dummy.next = NULL; node = &dummy; while (fgets(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 : GetAccountAndPasswordFromFile// Description : Attempts to read the password from a file.// upon failure it exits// Return type : void // Argument : char pszFileNamevoid GetAccountAndPasswordFromFile(char *pszFileName){ char line[1024]; FILE *fin; // open the file fin = fopen(pszFileName, "r"); if (fin == NULL) { printf("Error, unable to open account file '%s'\n", pszFileName); exit(0); } // read the account if (!fgets(line, 1024, fin)) { printf("Error, unable to read the account in '%s'\n", pszFileName); exit(0); } // strip off the newline characters while (strlen(line) && (line[strlen(line)-1] == '\r' || line[strlen(line)-1] == '\n')) line[strlen(line)-1] = '\0'; if (strlen(line) == 0) { printf("Error, first line in password file must be the account name. (%s)\n", pszFileName); exit(0); } // save the account strcpy(g_pszAccount, line); // read the password if (!fgets(line, 1024, fin)) { printf("Error, unable to read the password in '%s'\n", pszFileName); exit(0); } // strip off the newline characters while (strlen(line) && (line[strlen(line)-1] == '\r' || line[strlen(line)-1] == '\n')) line[strlen(line)-1] = '\0'; // save the password if (strlen(line)) strcpy(g_pszPassword, line); else g_pszPassword[0] = '\0';}// 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(){ char ch = 0; int index = 0; fprintf(stderr, "Mpd needs an account to launch processes with:\n"); do { fprintf(stderr, "account (domain\\user): "); fflush(stderr); gets(g_pszAccount); } while (strlen(g_pszAccount) == 0); fprintf(stderr, "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); gets(g_pszPassword); SetConsoleMode(hStdin, dwMode); fprintf(stderr, "\n");}// Function name : GetMPDPassPhrase// Description : // Return type : void // Argument : char *phrasevoid GetMPDPassPhrase(char *phrase){ fprintf(stderr, "mpd 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); gets(phrase); SetConsoleMode(hStdin, dwMode); fprintf(stderr, "\n");}// 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], *pChar; GetTempFileName(".", "mpi", 0, pszJobID); GetFullPathName(pszJobID, 100, tBuffer, &pChar); DeleteFile(pszJobID); strcpy(pszJobID, pChar);}// 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);}// Function name : PrintDots// Description : // Return type : void // Argument : HANDLE hEventvoid PrintDots(HANDLE hEvent){ if (WaitForSingleObject(hEvent, 3000) == WAIT_TIMEOUT) { printf(".");fflush(stdout); while (WaitForSingleObject(hEvent, 1000) == WAIT_TIMEOUT) { printf(".");fflush(stdout); } } CloseHandle(hEvent);}// Function name : NeedToMap// Description : // Return type : bool // Argument : char *pszFullPath// Argument : char *pDrive// Argument : char *pszShare// Argument : char *pszDirbool NeedToMap(char *pszFullPath, char *pDrive, char *pszShare)//, char *pszDir){ DWORD dwResult; DWORD dwLength; char pBuffer[4096]; REMOTE_NAME_INFO *info = (REMOTE_NAME_INFO*)pBuffer; char pszTemp[MAX_CMD_LENGTH]; if (*pszFullPath == '"') { strncpy(pszTemp, &pszFullPath[1], MAX_CMD_LENGTH); pszTemp[MAX_CMD_LENGTH-1] = '\0'; if (pszTemp[strlen(pszTemp)-1] == '"') pszTemp[strlen(pszTemp)-1] = '\0'; pszFullPath = pszTemp; } 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; strcpy(pszShare, info->lpConnectionName); return true; } //printf("WNetGetUniversalName: '%s'\n error %d\n", pszExe, dwResult); return false;}// Function name : ExeToUnc// Description : // Return type : void // Argument : char *pszExevoid 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 StripArgs(int &argc, char **&argv, int n){ if (n+1 > argc) { printf("Error: cannot strip %d args, only %d left.\n", n, argc-1); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -