void comm_init() { int i; static int firsttime=1; if (!firsttime){ return; } firsttime = 0; gethostname(hostname, 128); MPI_Comm_size(MPI_COMM_WORLD, &size); MPI_Comm_rank(MPI_COMM_WORLD, &rank); int gpus_per_node = getGpuCount(); comm_partition(); back_nbr = (rank -1 + size)%size; fwd_nbr = (rank +1)%size; num_nodes=size / getGpuCount(); if(num_nodes ==0) { num_nodes=1; } //determine which gpu this MPI process is going to use char* hostname_recv_buf = (char*)malloc(128*size); if(hostname_recv_buf == NULL){ printf("ERROR: malloc failed for host_recv_buf\n"); comm_exit(1); } gethostname(hostname, 128); int rc = MPI_Allgather(hostname, 128, MPI_CHAR, hostname_recv_buf, 128, MPI_CHAR, MPI_COMM_WORLD); if (rc != MPI_SUCCESS){ printf("ERROR: MPI_Allgather failed for hostname\n"); comm_exit(1); } which_gpu=0; for(i=0;i < size; i++){ if (i == rank){ break; } if (strncmp(hostname, hostname_recv_buf + 128*i, 128) == 0){ which_gpu ++; } } if (which_gpu >= gpus_per_node){ printf("ERROR: invalid gpu(%d) to use in rank=%d mpi process\n", which_gpu, rank); comm_exit(1); } srand(rank*999); free(hostname_recv_buf); return; }
unsigned long comm_recv(void* buf, int len, int src) { MPI_Request* request = (MPI_Request*)malloc(sizeof(MPI_Request)); if (request == NULL){ printf("ERROR: malloc failed for mpi request\n"); comm_exit(1); } int srcproc=-1; int recvtag=99; //recvtag is opposite to the sendtag if (src == BACK_NBR){ srcproc = back_nbr; recvtag = FWD_NBR; }else if (src == FWD_NBR){ srcproc = fwd_nbr; recvtag = BACK_NBR; }else{ printf("ERROR: invalid source\n"); comm_exit(1); } MPI_Irecv(buf, len, MPI_BYTE, srcproc, recvtag, MPI_COMM_WORLD, request); return (unsigned long)request; }
unsigned long comm_send(void* buf, int len, int dst) { MPI_Request* request = (MPI_Request*)malloc(sizeof(MPI_Request)); if (request == NULL){ printf("ERROR: malloc failed for mpi request\n"); comm_exit(1); } int dstproc; int sendtag=99; if (dst == BACK_NBR){ dstproc = back_nbr; sendtag = BACK_NBR; }else if (dst == FWD_NBR){ dstproc = fwd_nbr; sendtag = FWD_NBR; }else{ printf("ERROR: invalid dest\n"); comm_exit(1); } MPI_Isend(buf, len, MPI_BYTE, dstproc, sendtag, MPI_COMM_WORLD, request); return (unsigned long)request; }
int find_neighbor_proc(int which) { int proc = -1; switch(which){ case X_BACK_NBR: proc = x_back_nbr; break; case X_FWD_NBR: proc = x_fwd_nbr; break; case Y_BACK_NBR: proc = y_back_nbr; break; case Y_FWD_NBR: proc = y_fwd_nbr; break; case Z_BACK_NBR: proc = z_back_nbr; break; case Z_FWD_NBR: proc = z_fwd_nbr; break; case T_BACK_NBR: proc = t_back_nbr; break; case T_FWD_NBR: proc = t_fwd_nbr; break; default: printf("ERROR: invalid dest, line %d, file %s\n", __LINE__, __FILE__); comm_exit(1); } return proc; }
int comm_dim_partitioned(int dir) { int ret = 0; switch(dir){ case 0: ret = (xgridsize > 1); break; case 1: ret = (ygridsize > 1); break; case 2: ret = (zgridsize > 1); break; case 3: ret = (tgridsize > 1); break; default: printf("ERROR: invalid direction\n"); comm_exit(1); } if( manual_set_partition[dir]){ ret = manual_set_partition[dir]; } return ret; }
unsigned long comm_send_to_rank(void* buf, int len, int dst_rank) { MPI_Request* request = (MPI_Request*)malloc(sizeof(MPI_Request)); if (request == NULL){ printf("ERROR: malloc failed for mpi request\n"); comm_exit(1); } if(dst_rank < 0 || dst_rank >= comm_size()){ printf("ERROR: Invalid dst rank(%d)\n", dst_rank); comm_exit(1); } int sendtag = 99; MPI_Isend(buf, len, MPI_BYTE, dst_rank, sendtag, MPI_COMM_WORLD, request); return (unsigned long)request; }
unsigned long comm_send_with_tag(void* buf, int len, int dst, int tag) { MPI_Request* request = (MPI_Request*)malloc(sizeof(MPI_Request)); if (request == NULL){ printf("ERROR: malloc failed for mpi request\n"); comm_exit(1); } int dstproc = -1; switch(dst){ case X_BACK_NBR: dstproc = x_back_nbr; break; case X_FWD_NBR: dstproc = x_fwd_nbr; break; case Y_BACK_NBR: dstproc = y_back_nbr; break; case Y_FWD_NBR: dstproc = y_fwd_nbr; break; case Z_BACK_NBR: dstproc = z_back_nbr; break; case Z_FWD_NBR: dstproc = z_fwd_nbr; break; case T_BACK_NBR: dstproc = t_back_nbr; break; case T_FWD_NBR: dstproc = t_fwd_nbr; break; default: printf("ERROR: invalid dest, line %d, file %s\n", __LINE__, __FILE__); comm_exit(1); } MPI_Isend(buf, len, MPI_BYTE, dstproc, tag, MPI_COMM_WORLD, request); return (unsigned long)request; }
unsigned long comm_recv_with_tag(void* buf, int len, int src, int tag, void* _request) { MPI_Request* request = (MPI_Request*)_request; if (request == NULL){ printf("ERROR: malloc failed for mpi request\n"); comm_exit(1); } int srcproc=-1; switch (src){ case X_BACK_NBR: srcproc = x_back_nbr; break; case X_FWD_NBR: srcproc = x_fwd_nbr; break; case Y_BACK_NBR: srcproc = y_back_nbr; break; case Y_FWD_NBR: srcproc = y_fwd_nbr; break; case Z_BACK_NBR: srcproc = z_back_nbr; break; case Z_FWD_NBR: srcproc = z_fwd_nbr; break; case T_BACK_NBR: srcproc = t_back_nbr; break; case T_FWD_NBR: srcproc = t_fwd_nbr; break; default: printf("ERROR: invalid source, line %d, file %s\n", __LINE__, __FILE__); comm_exit(1); } MPI_Irecv(buf, len, MPI_BYTE, srcproc, tag, MPI_COMM_WORLD, request); return (unsigned long)request; }
void comm_start(void *request) { int rc = MPI_Start( (MPI_Request*)request); if (rc != MPI_SUCCESS) { printf("ERROR: MPI_Test failed\n"); comm_exit(1); } return; }
int procesar(char ** trozos){ if (trozos[0]==NULL) return 0; else if (strcmp(trozos[0],"exit")==0) comm_exit(); else if (strcmp(trozos[0],"quit")==0) comm_exit(); else if (strcmp(trozos[0],"autores")==0) comm_autores(); else if (strcmp(trozos[0],"chdir")==0) comm_chdir(trozos); else if (strcmp(trozos[0],"prompt")==0) comm_prompt(trozos); else if (strcmp(trozos[0],"pid")==0) comm_pid(trozos); else if (strcmp(trozos[0],"fork")==0) comm_fork(); else if (strcmp(trozos[0],"malloc")==0) comm_malloc(trozos); else if (strcmp(trozos[0],"display")==0) comm_display(trozos); else if (strcmp(trozos[0],"free")==0) comm_free(trozos); else if (strcmp(trozos[0],"stat")==0) comm_stat(trozos); else if (strcmp(trozos[0],"delete")==0) comm_delete(trozos); else if (strcmp(trozos[0],"list")==0) comm_list(trozos); else if (strcmp(trozos[0],"mmap")==0) comm_mmap(trozos); else if (strcmp(trozos[0],"munmap")==0) comm_munmap(trozos); else if (strcmp(trozos[0],"read")==0) comm_read(trozos); else if (strcmp(trozos[0],"write")==0) comm_write(trozos); else if (strcmp(trozos[0],"rec")==0) comm_rec(trozos); else if (strcmp(trozos[0],"shared-creat")==0) comm_sharedcreat(trozos); else if (strcmp(trozos[0],"shared-del")==0) comm_shareddel(trozos); else if (strcmp(trozos[0],"detach")==0) comm_detach(trozos); else if (strcmp(trozos[0],"shared-info")==0) comm_sharedinfo(trozos); else if (strcmp(trozos[0],"shared-cp")==0) comm_sharedcp(trozos); else if (strcmp(trozos[0],"shared-rm")==0) comm_sharedrm(trozos[1]); else if (strcmp(trozos[0],"direcciones")==0) comm_direcciones(trozos); else if (strcmp(trozos[0],"shared-mv")==0){ if (trozos[1]==NULL) {printf("Numero de parametros incorrectos.\n"); return -1;} char arg[strlen(trozos[1])]; strcpy(arg,trozos[1]); if ((comm_sharedcp(trozos))==0) comm_sharedrm(arg); } // Comandos de p2 else if (strcmp(trozos[0],"path")==0) comm_path(trozos); else if (strcmp(trozos[0],"exec")==0) comm_exec(trozos+1); else if (strcmp(trozos[0],"getpriority")==0) comm_getpriority(trozos); else if (strcmp(trozos[0],"setpriority")==0) comm_setpriority(trozos); else if (strcmp(trozos[0],"jobs")==0) comm_jobs(trozos,IMPRIMIR); else if (strcmp(trozos[0],"cleanjobs")==0) comm_jobs(trozos,BORRAR); else comm_com(trozos); }
void lp_exit(lp_prob *p) { int s_bufid; s_bufid = init_send(DataInPlace); send_msg(p->tree_manager, SOMETHING_DIED); freebuf(s_bufid); comm_exit(); exit(-1); }
unsigned long comm_recv_from_rank(void* buf, int len, int src_rank) { MPI_Request* request = (MPI_Request*)malloc(sizeof(MPI_Request)); if (request == NULL){ printf("ERROR: malloc failed for mpi request\n"); comm_exit(1); } if(src_rank < 0 || src_rank >= comm_size()){ printf("ERROR: Invalid src rank(%d)\n", src_rank); comm_exit(1); } int recvtag = 99; MPI_Irecv(buf, len, MPI_BYTE, src_rank, recvtag, MPI_COMM_WORLD, request); return (unsigned long)request; }
void comm_allreduce_int(int* data) { int recvbuf; int rc = MPI_Allreduce(data, &recvbuf, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD); if(rc!=MPI_SUCCESS){ printf("ERROR: MPI_Allreduce failed\n"); comm_exit(1); } *data = recvbuf; return; }
/** * 清理窗口管理器 * 就不写注释了 函数名写的已经很明白。。 **/ si_t window_manager_exit() { application_exit(); cursor_exit(); graph_exit(); comm_exit(); event_exit(); config_exit(); terminal_exit(); return 0; }
int comm_query(void* request) { MPI_Status status; int query; int rc = MPI_Test( (MPI_Request*)request, &query, &status); if (rc != MPI_SUCCESS) { printf("ERROR: MPI_Test failed\n"); comm_exit(1); } return query; }
//this request should be some return value from comm_recv void comm_wait(void* request) { MPI_Status status; int rc = MPI_Wait( (MPI_Request*)request, &status); if (rc != MPI_SUCCESS){ printf("ERROR: MPI_Wait failed\n"); comm_exit(1); } return; }
//we always reduce one double value void comm_allreduce_max(double* data) { double recvbuf; int rc = MPI_Allreduce ( data, &recvbuf,1,MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD); if (rc != MPI_SUCCESS){ printf("ERROR: MPI_Allreduce failed\n"); comm_exit(1); } *data = recvbuf; return; }
//reduce n double value void comm_allreduce_array(double* data, size_t size) { double recvbuf[size]; int rc = MPI_Allreduce ( data, &recvbuf,size,MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD); if (rc != MPI_SUCCESS){ printf("ERROR: MPI_Allreduce failed\n"); comm_exit(1); } memcpy(data, recvbuf, sizeof(recvbuf)); return; }
unsigned long comm_send_to_rank(void* buf, int len, int dst_rank, void* _request) { MPI_Request* request = (MPI_Request*)_request; if(dst_rank < 0 || dst_rank >= comm_size()){ printf("ERROR: Invalid dst rank(%d)\n", dst_rank); comm_exit(1); } int sendtag = 99; MPI_Isend(buf, len, MPI_BYTE, dst_rank, sendtag, MPI_COMM_WORLD, request); return (unsigned long)request; }
unsigned long comm_recv_from_rank(void* buf, int len, int src_rank, void* _request) { MPI_Request* request = (MPI_Request*)_request; if(src_rank < 0 || src_rank >= comm_size()){ printf("ERROR: Invalid src rank(%d)\n", src_rank); comm_exit(1); } int recvtag = 99; MPI_Irecv(buf, len, MPI_BYTE, src_rank, recvtag, MPI_COMM_WORLD, request); return (unsigned long)request; }
int comm_coords(int dir) { int i; switch(dir) { case 0: i = xgridid; break; case 1: i = ygridid; break; case 2: i = zgridid; break; case 3: i = tgridid; break; default: printf("Cannot get direction %d", dir); comm_exit(1); } return i; }
static void comm_partition(void) { /* printf("xgridsize=%d\n", xgridsize); printf("ygridsize=%d\n", ygridsize); printf("zgridsize=%d\n", zgridsize); printf("tgridsize=%d\n", tgridsize); */ if(xgridsize*ygridsize*zgridsize*tgridsize != size){ if (rank ==0){ printf("ERROR: Invalid configuration (t,z,y,x gridsize=%d %d %d %d) " "but # of MPI processes is %d\n", tgridsize, zgridsize, ygridsize, xgridsize, size); } comm_exit(1); } int leftover; #if 0 tgridid = rank/(zgridsize*ygridsize*xgridsize); leftover = rank%(zgridsize*ygridsize*xgridsize); zgridid = leftover/(ygridsize*xgridsize); leftover = leftover%(ygridsize*xgridsize); ygridid = leftover/xgridsize; xgridid = leftover%xgridsize; #define GRID_ID(xid,yid,zid,tid) (tid*zgridsize*ygridsize*xgridsize+zid*ygridsize*xgridsize+yid*xgridsize+xid) #else xgridid = rank/(ygridsize*zgridsize*tgridsize); leftover = rank%(ygridsize*zgridsize*tgridsize); ygridid = leftover/(zgridsize*tgridsize); leftover = leftover%(zgridsize*tgridsize); zgridid = leftover/tgridsize; tgridid = leftover%tgridsize; #define GRID_ID(xid,yid,zid,tid) (xid*ygridsize*zgridsize*tgridsize+yid*zgridsize*tgridsize+zid*tgridsize+tid) #endif printf("My rank: %d, gridid(t,z,y,x): %d %d %d %d\n", rank, tgridid, zgridid, ygridid, xgridid); int xid, yid, zid, tid; //X direction neighbors yid =ygridid; zid =zgridid; tid =tgridid; xid=(xgridid +1)%xgridsize; x_fwd_nbr = GRID_ID(xid,yid,zid,tid); xid=(xgridid -1+xgridsize)%xgridsize; x_back_nbr = GRID_ID(xid,yid,zid,tid); //Y direction neighbors xid =xgridid; zid =zgridid; tid =tgridid; yid =(ygridid+1)%ygridsize; y_fwd_nbr = GRID_ID(xid,yid,zid,tid); yid=(ygridid -1+ygridsize)%ygridsize; y_back_nbr = GRID_ID(xid,yid,zid,tid); //Z direction neighbors xid =xgridid; yid =ygridid; tid =tgridid; zid =(zgridid+1)%zgridsize; z_fwd_nbr = GRID_ID(xid,yid,zid,tid); zid=(zgridid -1+zgridsize)%zgridsize; z_back_nbr = GRID_ID(xid,yid,zid,tid); //T direction neighbors xid =xgridid; yid =ygridid; zid =zgridid; tid =(tgridid+1)%tgridsize; t_fwd_nbr = GRID_ID(xid,yid,zid,tid); tid=(tgridid -1+tgridsize)%tgridsize; t_back_nbr = GRID_ID(xid,yid,zid,tid); printf("MPI rank: rank=%d, hostname=%s, x_fwd_nbr=%d, x_back_nbr=%d\n", rank, hostname, x_fwd_nbr, x_back_nbr); printf("MPI rank: rank=%d, hostname=%s, y_fwd_nbr=%d, y_back_nbr=%d\n", rank, hostname, y_fwd_nbr, y_back_nbr); printf("MPI rank: rank=%d, hostname=%s, z_fwd_nbr=%d, z_back_nbr=%d\n", rank, hostname, z_fwd_nbr, z_back_nbr); printf("MPI rank: rank=%d, hostname=%s, t_fwd_nbr=%d, t_back_nbr=%d\n", rank, hostname, t_fwd_nbr, t_back_nbr); }
int main(int argc, char **argv) { cl_options_t cl_options = {0}; dbg_init(); DBG_LOG("Version %s", g_version); ui = &ui_sdlgl; printf("DreamChess %s\n", g_version); parse_options(argc, argv, &ui, &cl_options); config_init(); set_cl_options(&cl_options); if (!ui) { DBG_ERROR("Failed to find a user interface driver"); exit(1); } ui->init(); init_resolution(); while (1) { board_t board; int pgn_slot; option_t *option; if (!(config = ui->config(&pgn_slot))) break; ch_userdir(); option = config_get_option("first_engine"); #ifdef __APPLE__ char temp1[200]; char temp2[200]; if (!strcmp(option->string, "dreamer") || !strcmp(option->string, "Dreamer")) { CFBundleRef mainBundle = CFBundleGetMainBundle(); CFURLRef bundledir = CFBundleCopyBundleURL(mainBundle); CFStringRef stringref = CFURLCopyFileSystemPath(bundledir, kCFURLPOSIXPathStyle); CFStringGetCString(stringref, temp1, 200, kCFStringEncodingMacRoman); snprintf(temp2, sizeof(temp2), "%s/contents/MacOS/dreamer", temp1); game_set_engine_error(comm_init(temp2)); } else game_set_engine_error(comm_init(option->string)); #else game_set_engine_error(comm_init(option->string)); #endif comm_send("xboard\n"); comm_send("new\n"); comm_send("random\n"); comm_send("sd %i\n", config->cpu_level); comm_send("depth %i\n", config->cpu_level); if (config->difficulty == 0) comm_send("noquiesce\n"); if (config->player[WHITE] == PLAYER_UI && config->player[BLACK] == PLAYER_UI) comm_send("force\n"); if (config->player[WHITE] == PLAYER_ENGINE) comm_send("go\n"); in_game = 1; board_setup(&board); history = history_init(&board); move_list_init(&san_list); move_list_init(&fan_list); move_list_init(&fullalg_list); if (pgn_slot >= 0) if (game_load(pgn_slot)) { DBG_ERROR("Failed to load savegame in slot %i", pgn_slot); exit(1); } ui->update(history->view->board, NULL); while (in_game) { char *s; if ((s = comm_poll())) { DBG_LOG("Message from engine: '%s'", s); if (!history->result) { if ((!strncmp(s, "move ", 4) || strstr(s, "... ")) && config->player[history->last->board->turn] == PLAYER_ENGINE) { char *move_str = strrchr(s, ' ') + 1; board_t new_board = *history->last->board; move_t *engine_move; DBG_LOG("Parsing move string '%s'", move_str); engine_move = san_to_move(&new_board, move_str); if (!engine_move) engine_move = fullalg_to_move(&new_board, move_str); if (engine_move) { audio_play_sound(AUDIO_MOVE); do_move(engine_move, 1); free(engine_move); } else DBG_ERROR("Failed to parse move string '%s'", move_str); } else if (strstr(s, "llegal move")) game_undo(); /* Ignore result message if we've already determined a result ourselves. */ else { char *start = strchr(s, '{'); char *end = strchr(s, '}'); if (start && end && end > start) { char *comment = malloc(end - start); history->result = malloc(sizeof(result_t)); strncpy(comment, start + 1, end - start - 1); comment[end - start - 1] = '\0'; history->result->reason = comment; if (strstr(s, "1-0")) { history->result->code = RESULT_WHITE_WINS; ui->show_result(history->result); } else if (strstr(s, "1/2-1/2")) { history->result->code = RESULT_DRAW; ui->show_result(history->result); } else if (strstr(s, "0-1")) { history->result->code = RESULT_BLACK_WINS; ui->show_result(history->result); } else { free(history->result->reason); free(history->result); history->result = NULL; } } } } free(s); } ui->poll(); } comm_send("quit\n"); comm_exit(); history_exit(history); move_list_exit(&san_list); move_list_exit(&fan_list); move_list_exit(&fullalg_list); } ui->exit(); dbg_exit(); return 0; }