在集群上跑程序的人都知道要编辑一个节点列表文件来说明在哪些机器上启动进程,以及在某个机器上启动的进程的数量,最典型的情况是mpi。但是如果机器的数量过大,比如有500个节点的机群,这样要维护这个文件也是一件比较头疼的事情。
维护节点列表文件,主要是进行三个操作:创建列表,删除某些节点,修改某些节点。接下来就可以用C或者Python实现一个具备这三种功能的小程序了。今天上午实现了一个,代码贴在这里。
/************************************************************************************************************************** * Name: md * * Author: Chaos Lee * * Date: 2012-06-07 * * Description: This program is used to create and edit a host file. It provides three simple functions to accomplish this.* * (1) To create a host file: mh -p prefix_name -f file_name -l num1-num2 num3 num4-num5 * * Above command will create a host file whose name is file_name,containing a machine name list in format * * as bellow: * * prefix_namenum1 * * ... * * prefix_namenum2 * * prefix_namenum3 * * prefix_namenum4 * * ... * * prefix_namenum5 * * (2) To delete some machine names from the host file: mh -f file_name -d num1-num2 num3 num4-num5. * * Above command will delete prefix_namenum1,...,prefix_namenume2,prefix_namenum3,prefix_namenum4,... * * ,prefix_namenum5 from the file indicated by the file_name * * (3) To modify some machine names from the host file: mh -f file_name -m num1 num2 num3 num4 * * Above command will change prefix_namenum1 to prefix_namenum2,prefix_namenum3 to prefix_namenum4. * **************************************************************************************************************************/ #include<stdio.h> #include<string.h> #include<ctype.h> #include<stdlib.h> typedef enum com_type_tag {create,delete,modify} com_type; typedef int status; #define false 0 #define true 1 #define EXIT_SUCCESS 0 #define BAD_FORMAT -1 #define MAX_NODE_NUMBER 999 char machine_names[MAX_NODE_NUMBER][20]; int names_number = 0; #define XXXX do{\ printf("FILE:%s LINE:%d\n",__FILE__,__LINE__); \ fflush(stdout); \ }while(0); int find_minus(char *nums) { int len = strlen(nums); int i; for(i=0;i<len;i++) { if(nums[i]=='-') { return i; } } return -1; } status check_cd_num(char * nums) { int minus_pos = find_minus(nums); int i = 0; if(-1 == minus_pos) return check_m_num(nums); nums[minus_pos] = '\0'; if(false == check_m_num(nums) || false == check_m_num(nums+i+1) ) return false; nums[minus_pos] = '-'; return true; } status check_m_num(char * num) { int i=0; int len = strlen(num); if(len>3 || len==0) return false; for(i=0;i<len;i++) { if( !isdigit(num[i]) ) return false; } return true; } status check_num_list(int type,int argc,char *argv[]) { int i; int start=4; if(type == create || type == delete) { if(type == create) start = 6; for(i=start;i<argc;i++) { status res = check_cd_num(argv[i]); if(!res) return res; } } else if(type == modify) { int counter = 0; for(i=start;i<argc;i++) { status res = check_m_num(argv[i]); if(!res) return res; counter++; } if(counter & 1) return false; } else { printf("command type is wrong.\n"); return false; } return true; } com_type analyze_command(int argc,char *argv[]) { int i=0; com_type type; if( 0 == strcmp(argv[1],"-p") && 0 == strcmp(argv[3],"-f") && 0 == strcmp(argv[5],"-l") ) type = create; else if(0 == strcmp(argv[1],"-f") && 0 == strcmp(argv[3],"-d") ) type = delete; else if(0 == strcmp(argv[1],"-f") && 0 == strcmp(argv[3],"-m")) type = modify; else { printf("command in bad format.\n"); exit(BAD_FORMAT); } if(false == check_num_list(type,argc,argv)) { printf("number list in bad format\n"); exit(BAD_FORMAT); } return type; } void get_range(char *nums,int * num_min,int * num_max) { int minus_pos = find_minus(nums); if( -1 == minus_pos ) *num_min = *num_max = atoi(nums); else { *num_min = atoi(nums); *num_max = atoi(nums+minus_pos+1); } } status create_list(int argc,char *argv[]) { int i,num_min,num_max,j; FILE * fp; //printf("filename = %s\n",argv[4]); fp = fopen(argv[4],"w"); if(NULL == fp) { printf("creating file %s error.\n",argv[4]); return false; } for(i = 6;i<argc;i++) { get_range(argv[i],&num_min,&num_max); //printf("num_min=%d num_max=%d\n",num_min,num_max); for(j=num_min;j<=num_max;j++) sprintf(machine_names[names_number++],"%s%03d",argv[2],j); } for(i=0;i<names_number;i++) { fprintf(fp,"%s\n",machine_names[i]); } fclose(fp); return true; } status delete_list(int argc,char *argv[]) { status delete_flag_array[MAX_NODE_NUMBER]; int i,num_min,num_max,j; FILE * fp; int minus_pos,id; char prefix[20]; for(i = 0;i<MAX_NODE_NUMBER;i++) delete_flag_array[i] = false; for(i = 4;i<argc;i++) { get_range(argv[i],&num_min,&num_max); for(j=num_min;j<=num_max;j++) delete_flag_array[j] = true; } fp = fopen(argv[2],"r"); if(NULL == fp) { printf("opening file %s error.\n",argv[2]); return false; } while(!feof(fp)) { fscanf(fp,"%s",machine_names[names_number++]); if(strcmp("",machine_names[names_number])!=0) names_number++; } fclose(fp); fp = fopen(argv[2],"w"); if(NULL == fp) { printf("recreating file %s error.\n",argv[2]); return false; } if(0 == names_number) return false; minus_pos = find_minus(machine_names[0]); if(-1 == minus_pos) return false; for(i=0;i<names_number;i++) { id = atoi(machine_names[i]+minus_pos+1); if(0 == id) continue; if(true != delete_flag_array[id]) { fprintf(fp,"%s\n",machine_names[i]); } } fclose(fp); return true; } status modify_list(int argc,char *argv[]) { int id_map[MAX_NODE_NUMBER],i=0; FILE *fp; int minus_pos,id; char prefix[20]; for(i=0;i<MAX_NODE_NUMBER;i++) { id_map[i] = -1; } i=4; while(i<argc) { id_map[atoi(argv[i])] = atoi(argv[i+1]); i+=2; } fp = fopen(argv[2],"r"); if(NULL == fp) { printf("opening file %s error.\n",argv[2]); return false; } while(!feof(fp)) { fscanf(fp,"%s",machine_names[names_number]); if(strcmp("",machine_names[names_number])!=0) names_number++; } fclose(fp); fp = fopen(argv[2],"w"); if(NULL == fp) { printf("recreating file %s error.\n",argv[2]); return false; } if(0 == names_number) return false; minus_pos = find_minus(machine_names[0]); if(-1 == minus_pos) return false; strncpy(prefix,machine_names[0],minus_pos+1); for(i=0;i<names_number;i++) { id = atoi(machine_names[i]+minus_pos+1); if(0 == id) continue; if(-1 != id_map[id]) { fprintf(fp,"%s%03d\n",prefix,id_map[id]); } else { fprintf(fp,"%s\n",machine_names[i]); } } fclose(fp); return true; } int main(int argc,char *argv[]) { com_type type; status res; type = analyze_command(argc,argv); switch(type) { case create: res = create_list(argc,argv); if(false == res) { printf("creating list error.\n"); exit(EXIT_FAILURE); } break; case delete: res = delete_list(argc,argv); if(false == res) { printf("deleting list error.\n"); exit(EXIT_FAILURE); } break; case modify: res = modify_list(argc,argv); if(false == res) { printf("modifying list error.\n"); exit(EXIT_FAILURE); } break; default: printf("Panic:type error.\n"); exit(BAD_FORMAT); } return EXIT_SUCCESS; }