📄 smpd_host_util.c
字号:
if (*iter != '\0') { *iter = '\0'; iter++; while (isspace(*iter)) iter++; nproc = 1; if (isdigit(*iter)) { nproc = atoi(iter); /* move over the number */ while (isdigit(*iter)) iter++; /* move over the space between the number and any other options */ while (isspace(*iter)) iter++; } if (nproc < 1) nproc = 1; } else { nproc = 1; } node = (smpd_host_node_t*)MPIU_Malloc(sizeof(smpd_host_node_t)); if (node == NULL) { smpd_err_printf("unable to allocate memory to parse the hosts string <%s>\n", host_str); MPIU_Free(str); smpd_exit_fn(FCNAME); return SMPD_FALSE; } strcpy(node->host, hostname); node->alt_host[0] = '\0'; node->connected = SMPD_FALSE; node->connect_cmd_tag = -1; node->id = -1; node->parent = -1; node->nproc = nproc; node->next = NULL; node->left = NULL; node->right = NULL; smpd_add_extended_host_to_default_list(node->host, node->alt_host, node->nproc); if (smpd_process.s_host_list == NULL) smpd_process.s_host_list = node; else { node_iter = smpd_process.s_host_list; while (node_iter->next != NULL) node_iter = node_iter->next; node_iter->next = node; } } } MPIU_Free(str); if (smpd_process.s_host_list != NULL) { node = smpd_process.s_host_list; while (node) { smpd_dbg_printf("host = %s, nproc = %d\n", node->host, node->nproc); node = node->next; } smpd_exit_fn(FCNAME); return SMPD_TRUE; } smpd_exit_fn(FCNAME); return SMPD_FALSE;}#undef FCNAME#define FCNAME "smpd_get_host_id"int smpd_get_host_id(char *host, int *id_ptr){ smpd_host_node_t *node; int bit, mask, temp; smpd_enter_fn(FCNAME); /* look for the host in the list */ node = smpd_process.host_list; while (node) { if (strcmp(node->host, host) == 0) { /* return the id */ *id_ptr = node->id; smpd_exit_fn(FCNAME); return SMPD_SUCCESS; } if (node->next == NULL) break; node = node->next; } /* allocate a new node */ if (node != NULL) { node->next = (smpd_host_node_t *)MPIU_Malloc(sizeof(smpd_host_node_t)); node = node->next; } else { node = (smpd_host_node_t *)MPIU_Malloc(sizeof(smpd_host_node_t)); smpd_process.host_list = node; } if (node == NULL) { smpd_err_printf("malloc failed to allocate a host node structure\n"); smpd_exit_fn(FCNAME); return SMPD_FAIL; } strcpy(node->host, host); node->alt_host[0] = '\0'; node->parent = smpd_process.tree_parent; node->id = smpd_process.tree_id; node->connected = SMPD_FALSE; node->connect_cmd_tag = -1; node->nproc = -1; node->next = NULL; node->left = NULL; node->right = NULL; /* move to the next id and parent */ smpd_process.tree_id++; temp = smpd_process.tree_id >> 2; bit = 1; while (temp) { bit <<= 1; temp >>= 1; } mask = bit - 1; smpd_process.tree_parent = bit | (smpd_process.tree_id & mask); /* return the id */ *id_ptr = node->id; smpd_exit_fn(FCNAME); return SMPD_SUCCESS;}#undef FCNAME#define FCNAME "smpd_get_next_host"int smpd_get_next_host(smpd_host_node_t **host_node_pptr, smpd_launch_node_t *launch_node){ int result; char host[SMPD_MAX_HOST_LENGTH]; char alt_host[SMPD_MAX_HOST_LENGTH]; smpd_host_node_t *host_node_ptr; smpd_enter_fn(FCNAME); if (host_node_pptr == NULL) { smpd_err_printf("invalid host_node_pptr argument.\n"); smpd_exit_fn(FCNAME); return SMPD_FAIL; } if (*host_node_pptr == NULL) { result = smpd_get_next_hostname(host, alt_host); if (result != SMPD_SUCCESS) { smpd_err_printf("unable to get the next available host name\n"); smpd_exit_fn(FCNAME); return SMPD_FAIL; } result = smpd_get_host_id(host, &launch_node->host_id); if (result != SMPD_SUCCESS) { smpd_err_printf("unable to get a id for host %s\n", host); smpd_exit_fn(FCNAME); return SMPD_FAIL; } MPIU_Strncpy(launch_node->hostname, host, SMPD_MAX_HOST_LENGTH); MPIU_Strncpy(launch_node->alt_hostname, alt_host, SMPD_MAX_HOST_LENGTH); smpd_exit_fn(FCNAME); return SMPD_SUCCESS; } host_node_ptr = *host_node_pptr; if (host_node_ptr->nproc == 0) { (*host_node_pptr) = (*host_node_pptr)->next; MPIU_Free(host_node_ptr); host_node_ptr = *host_node_pptr; if (host_node_ptr == NULL) { smpd_err_printf("no more hosts in the list.\n"); smpd_exit_fn(FCNAME); return SMPD_FAIL; } } result = smpd_get_host_id(host_node_ptr->host, &launch_node->host_id); if (result != SMPD_SUCCESS) { smpd_err_printf("unable to get a id for host %s\n", host_node_ptr->host); smpd_exit_fn(FCNAME); return SMPD_FAIL; } MPIU_Strncpy(launch_node->hostname, host_node_ptr->host, SMPD_MAX_HOST_LENGTH); MPIU_Strncpy(launch_node->alt_hostname, host_node_ptr->alt_host, SMPD_MAX_HOST_LENGTH); host_node_ptr->nproc--; if (host_node_ptr->nproc == 0) { (*host_node_pptr) = (*host_node_pptr)->next; MPIU_Free(host_node_ptr); } smpd_exit_fn(FCNAME); return SMPD_SUCCESS;}#undef FCNAME#define FCNAME "smpd_get_argcv_from_file"SMPD_BOOL smpd_get_argcv_from_file(FILE *fin, int *argcp, char ***argvp){ static char line[SMPD_MAX_LINE_LENGTH]; static char line_out[SMPD_MAX_LINE_LENGTH]; static char *argv[SMPD_MAX_ARGC]; char *iter_line, *iter_out, *last_position; int index; int num_remaining; smpd_enter_fn(FCNAME); argv[0] = "bogus.exe"; while (fgets(line, SMPD_MAX_LINE_LENGTH, fin)) { /* first strip off the \r\n at the end of the line */ if (line[0] != '\0') { iter_line = &line[strlen(line)-1]; while ((*iter_line == '\r' || *iter_line == '\n') && (iter_line >= line)) { *iter_line = '\0'; iter_line--; } } iter_out = line_out; line_out[0] = '\0'; iter_line = line; index = 1; num_remaining = SMPD_MAX_LINE_LENGTH; while (iter_line != NULL && (strlen(iter_line) > 0)) { last_position = iter_line; if (MPIU_Str_get_string(&iter_line, iter_out, num_remaining) == MPIU_STR_SUCCESS) { if (last_position == iter_line) { /* no string parsed */ break; } argv[index] = iter_out; index++; if (index == SMPD_MAX_ARGC) { argv[SMPD_MAX_ARGC-1] = NULL; break; } num_remaining = num_remaining - (int)strlen(iter_out) - 1; iter_out = &iter_out[strlen(iter_out)+1]; } else { break; } } if (index != 1) { if (index < SMPD_MAX_ARGC) argv[index] = NULL; *argcp = index; *argvp = argv; smpd_exit_fn(FCNAME); return SMPD_TRUE; } } smpd_exit_fn(FCNAME); return SMPD_FALSE;}#if 0 /* This way simply tokenizes by space characters without any escape capabilities */#undef FCNAME#define FCNAME "smpd_get_argcv_from_file"SMPD_BOOL smpd_get_argcv_from_file(FILE *fin, int *argcp, char ***argvp){ static char line[SMPD_MAX_LINE_LENGTH]; static char *argv[SMPD_MAX_ARGC]; char *token; int index; smpd_enter_fn(FCNAME); argv[0] = "bogus.exe"; while (fgets(line, SMPD_MAX_LINE_LENGTH, fin)) { index = 1; token = strtok(line, " \r\n"); while (token) { argv[index] = token; index++; if (index == SMPD_MAX_ARGC) { argv[SMPD_MAX_ARGC-1] = NULL; break; } token = strtok(NULL, " \r\n"); } if (index != 1) { if (index < SMPD_MAX_ARGC) argv[index] = NULL; *argcp = index; *argvp = argv; smpd_exit_fn(FCNAME); return SMPD_TRUE; } } smpd_exit_fn(FCNAME); return SMPD_FALSE;}#endif#undef FCNAME#define FCNAME "next_launch_node"static smpd_launch_node_t *next_launch_node(smpd_launch_node_t *node, int id){ smpd_enter_fn(FCNAME); while (node) { if (node->host_id == id) { smpd_exit_fn(FCNAME); return node; } node = node->next; } smpd_exit_fn(FCNAME); return NULL;}#undef FCNAME#define FCNAME "prev_launch_node"static smpd_launch_node_t *prev_launch_node(smpd_launch_node_t *node, int id){ smpd_enter_fn(FCNAME); while (node) { if (node->host_id == id) { smpd_exit_fn(FCNAME); return node; } node = node->prev; } smpd_exit_fn(FCNAME); return NULL;}#undef FCNAME#define FCNAME "smpd_create_cliques"int smpd_create_cliques(smpd_launch_node_t *list){ smpd_launch_node_t *iter, *cur_node; int cur_iproc, printed_iproc; char *cur_str; smpd_enter_fn(FCNAME); if (list == NULL) { smpd_exit_fn(FCNAME); return SMPD_SUCCESS; } if (list->iproc == 0) { /* in order */ cur_node = list; while (cur_node) { /* point to the current structures */ printed_iproc = cur_iproc = cur_node->iproc; cur_str = cur_node->clique; cur_str += sprintf(cur_str, "%d", cur_iproc); /* add the ranks of all other nodes with the same id */ iter = next_launch_node(cur_node->next, cur_node->host_id); while (iter) { if (iter->iproc == cur_iproc + 1) { cur_iproc = iter->iproc; iter = next_launch_node(iter->next, iter->host_id); if (iter == NULL) cur_str += sprintf(cur_str, "..%d", cur_iproc); } else { if (printed_iproc == cur_iproc) { cur_str += sprintf(cur_str, ",%d", iter->iproc); } else { cur_str += sprintf(cur_str, "..%d,%d", cur_iproc, iter->iproc); } printed_iproc = cur_iproc = iter->iproc; iter = next_launch_node(iter->next, iter->host_id); } } /* copy the clique string to all the nodes with the same id */ iter = next_launch_node(cur_node->next, cur_node->host_id); while (iter) { strcpy(iter->clique, cur_node->clique); iter = next_launch_node(iter->next, iter->host_id); } /* move to the next node that doesn't have a clique string yet */ cur_node = cur_node->next; while (cur_node && cur_node->clique[0] != '\0') cur_node = cur_node->next; } } else { /* reverse order */ cur_node = list; /* go to the end of the list */ while (cur_node->next) cur_node = cur_node->next; while (cur_node) { /* point to the current structures */ printed_iproc = cur_iproc = cur_node->iproc; cur_str = cur_node->clique; cur_str += sprintf(cur_str, "%d", cur_iproc); /* add the ranks of all other nodes with the same id */ iter = prev_launch_node(cur_node->prev, cur_node->host_id); while (iter) { if (iter->iproc == cur_iproc + 1) { cur_iproc = iter->iproc; iter = prev_launch_node(iter->prev, iter->host_id); if (iter == NULL) cur_str += sprintf(cur_str, "..%d", cur_iproc); } else { if (printed_iproc == cur_iproc) { cur_str += sprintf(cur_str, ",%d", iter->iproc); } else { cur_str += sprintf(cur_str, "..%d,%d", cur_iproc, iter->iproc); } printed_iproc = cur_iproc = iter->iproc; iter = prev_launch_node(iter->prev, iter->host_id); } } /* copy the clique string to all the nodes with the same id */ iter = prev_launch_node(cur_node->prev, cur_node->host_id); while (iter) { strcpy(iter->clique, cur_node->clique); iter = prev_launch_node(iter->prev, iter->host_id); } /* move to the next node that doesn't have a clique string yet */ cur_node = cur_node->prev; while (cur_node && cur_node->clique[0] != '\0') cur_node = cur_node->prev; } } /* iter = list; while (iter) { printf("clique: <%s>\n", iter->clique); iter = iter->next; } */ smpd_exit_fn(FCNAME); return SMPD_SUCCESS;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -