int user_send_lp_data(void *user, void **user_lp) { /* This gives you access to the user data structure. */ spp_problem *spp = (spp_problem *) user; col_ordered *m = spp->cmatrix; #if defined(COMPILE_IN_TM) && defined(COMPILE_IN_LP) spp_lp_problem *spp_lp = (spp_lp_problem *) calloc(1, sizeof(spp_lp_problem)); *user_lp = (void *) spp_lp; spp_lp->par = spp->lp_par; spp_lp->cmatrix = m; /* initialize some data structures in spp */ spp_init_lp(spp_lp); #else /* Here, we send that data using message passing and the rest is done in user_receive_lp_data() in the LP process */ send_char_array((char *)spp->lp_par, sizeof(spp_lp_params)); send_int_array(&m->colnum, 1); send_int_array(&m->rownum, 1); send_int_array(&m->nzcnt, 1); send_int_array(m->colnames, m->colnum); send_dbl_array(m->obj, m->colnum); send_int_array(m->matbeg, (m->colnum + 1)); send_char_array((char *)m->matind, m->nzcnt * sizeof(row_ind_type)); #endif return(USER_SUCCESS); }
int user_send_cg_data(void *user, void **user_cg) { spp_problem *spp = (spp_problem *)user; col_ordered *m = spp->cmatrix; int colnum = m->colnum; #if defined(COMPILE_IN_TM) && defined(COMPILE_IN_LP) && defined (COMPILE_IN_CG) spp_cg_problem *spp_cg = (spp_cg_problem *)calloc(1,sizeof(spp_cg_problem)); *user_cg = (void *) spp_cg; spp_cg->par = spp->cg_par; spp_cg->cmatrix = m; /* allocate space for tmp arrays */ spp_cg->tmp = (spp_cg_tmp *) calloc(1, sizeof(spp_cg_tmp)); spp_cg->tmp->itmp_m = (int *) malloc(m->rownum * ISIZE); spp_cg->tmp->istartmp_m = (int **) malloc(m->rownum * sizeof(int *)); spp_cg->tmp->cuttmp = (cut_data *) calloc(1, sizeof(cut_data)); /* initialize cg data structures */ spp_cg->fgraph = (frac_graph *) calloc(1, sizeof(frac_graph)); spp_cg->cfgraph = (frac_graph *) calloc(1, sizeof(frac_graph)); spp_cg->cm_frac = (col_ordered *) calloc(1, sizeof(col_ordered)); spp_cg->rm_frac = (row_ordered *) calloc(1, sizeof(row_ordered)); spp_cg->rm_frac->rmatbeg = (int *) malloc((m->rownum+1) * ISIZE); spp_cg->lgraph = (level_graph *) calloc(1, sizeof(level_graph)); allocate_var_length_structures(spp_cg, spp_cg->max_sol_length); /* cut collection is a local cut pool that contains the cuts that have been sent back to the lp */ spp_cg->cut_coll = (cut_collection *) calloc(1, sizeof(cut_collection)); spp_cg->cut_coll->max_size = 1000; spp_cg->cut_coll->cuts = (cut_data **) calloc(spp_cg->cut_coll->max_size, sizeof(cut_data *)); spp_cg->cut_coll->violation = (double *) malloc(spp_cg->cut_coll->max_size * DSIZE); spp_cg->cut_coll->mult = (int *) malloc(spp_cg->cut_coll->max_size * ISIZE); #else int info; send_char_array((char *)spp->cg_par, sizeof(spp_cg_params)); send_int_array(&colnum, 1); send_int_array(&m->rownum, 1); send_int_array(&m->nzcnt, 1); send_int_array(m->colnames, colnum); send_dbl_array(m->obj, colnum); send_int_array(m->matbeg, colnum + 1); send_char_array((char *)m->matind, m->nzcnt * sizeof(row_ind_type)); #endif return(USER_SUCCESS); }
void pack_double_array_desc(double_array_desc *dad, char explicit_packing) { send_char_array(&dad->type, 1); send_int_array(&dad->size, 1); if (dad->size > 0){ if (!explicit_packing && dad->type == WRT_PARENT) send_int_array(dad->list, dad->size); send_int_array(dad->stat, dad->size); } }
int main(int argc, char *argv[]) { session *s; session_init(&argc, &argv, &s, "Pingpong_A.spr"); if (argc < 3) return EXIT_FAILURE; int M = atoi(argv[1]); int N = atoi(argv[2]); printf("M: %d, N: %d\n", M, N); role *B = s->r(s, "B"); int val[M]; size_t sz = M; long long start_time = sc_time(); int i; for (i=0; i<N; i++) { memset(val, i, M * sizeof(int)); send_int_array(val, (size_t)M, B); sz = M; recv_int_array(val, &sz, B); } long long end_time = sc_time(); printf("%s: Time elapsed: %f sec\n", s->name, sc_time_diff(start_time, end_time)); session_end(s); return EXIT_SUCCESS; }
int send_cg_data_u(sym_environment *env, int sender) { #if defined(COMPILE_IN_TM) && defined(COMPILE_IN_LP) && defined(COMPILE_IN_CG) int i; tm_prob *tm = env->tm; tm->cgp = (cg_prob **) malloc(tm->par.max_active_nodes*sizeof(cg_prob *)); #pragma omp parallel for for (i = 0; i < tm->par.max_active_nodes; i++){ tm->lpp[i]->cgp = tm->cgp[i] = (cg_prob *) calloc(1, sizeof(cg_prob)); tm->cgp[i]->par = env->par.cg_par; tm->cgp[i]->draw_graph = env->dg_tid; #ifdef USE_SYM_APPLICATION CALL_USER_FUNCTION( user_send_cg_data(env->user, &(tm->lpp[i]->cgp->user)) ); #endif } #else int s_bufid; s_bufid = init_send(DataInPlace); send_char_array((char *)(&env->par.cg_par), sizeof(cg_params)); send_int_array(&env->dg_tid, 1); #ifdef USE_SYM_APPLICATION CALL_USER_FUNCTION( user_send_cg_data(env->user, NULL) ); #endif send_msg(sender, CG_DATA); freebuf(s_bufid); #endif return(FUNCTION_TERMINATED_NORMALLY); }
void INTERMED_ERROR(char *window_name, int old_msgtag, int receiver, int msgtag) { int s_bufid; s_bufid = init_send(DataInPlace); send_char_array(window_name, MAX_NAME_LENGTH); send_int_array(&old_msgtag, 1); send_msg(receiver, msgtag); freebuf(s_bufid); }
void broadcast(vrp_problem *vrp, int *tids, int jobs) { int s_bufid; if (jobs == 0) return; s_bufid = init_send(DataInPlace); send_int_array(&vrp->dist.wtype, 1); send_int_array(&vrp->vertnum, 1); send_int_array(&vrp->depot, 1); send_int_array(&vrp->capacity, 1); send_int_array(vrp->demand, (int)vrp->vertnum); if (vrp->dist.wtype != _EXPLICIT){ /* not EXPLICIT */ send_dbl_array(vrp->dist.coordx, vrp->vertnum); send_dbl_array(vrp->dist.coordy, vrp->vertnum); if ((vrp->dist.wtype == _EUC_3D) || (vrp->dist.wtype == _MAX_3D) || (vrp->dist.wtype == _MAN_3D)) send_dbl_array(vrp->dist.coordz, vrp->vertnum); } else{ /* EXPLICIT */ send_int_array(vrp->dist.cost, (int)vrp->edgenum); } msend_msg(tids, jobs, VRP_BROADCAST_DATA); freebuf(s_bufid); }
int main(void) { int r_bufid = 0, s_bufid = 0; cg_prob *p; int num_cuts = 0; double elapsed; struct timeval tout = {15, 0}; p = (cg_prob *) calloc(1, sizeof(cg_prob)); cg_initialize(p, 0); /*------------------------------------------------------------------------*\ * The main loop -- executes continuously until the program exits \*------------------------------------------------------------------------*/ while (TRUE){ /* Wait until a message arrives */ do{ r_bufid = treceive_msg(ANYONE, ANYTHING, &tout); if (!r_bufid){ if (pstat(p->tree_manager) != PROCESS_OK){ printf("TM has died -- CG exiting\n\n"); exit(-401); } } }while (!r_bufid); if (cg_process_message(p, r_bufid) == USER_ERROR) p->msgtag = USER_ERROR; /* If there is still something in the queue, process it */ do{ r_bufid = nreceive_msg(ANYONE, ANYTHING); if (r_bufid > 0) if (cg_process_message(p, r_bufid) == USER_ERROR) p->msgtag = USER_ERROR; }while (r_bufid != 0); /*--------------------------------------------------------------------- * Now the message queue is empty. If the last message was NOT some * kind of LP_SOLUTION then we can't generate solutions now. * Otherwise, generate solutions! *---------------------------------------------------------------------*/ if (p->msgtag == LP_SOLUTION_NONZEROS || p->msgtag == LP_SOLUTION_USER || p->msgtag == LP_SOLUTION_FRACTIONS){ if (p->par.do_findcuts) if ((termcode = find_cuts_u(p, NULL, &num_cuts)) < 0) printf("Warning: User error detected in cut generator\n\n"); /*-- send signal back to the LP that the cut generator is done -----*/ s_bufid = init_send(DataInPlace); send_int_array(&num_cuts, 1); elapsed = used_time(&p->tt); send_dbl_array(&elapsed, 1); send_int_array(&p->cur_sol.xindex, 1); send_int_array(&p->cur_sol.xiter_num, 1); send_msg(p->cur_sol.lp, NO_MORE_CUTS); freebuf(s_bufid); FREE(p->cur_sol.xind); FREE(p->cur_sol.xval); } } return(0); }
int main(int argc, char **argv){ /* Variables declaration */ int i; int j; /* Number of rounds to run the algorithm */ int rounds = 0; /* Total size of the array */ int total_size = (rows * columns) / participants; printf("Total amount of work is %d\n", total_size); /* the amount of work that is assigned to worker1 */ int amount_of_work = floor( (worker1 + 1) * columns / participants ) - floor( worker1 * columns / participants ); printf("amount of work is %d\n", amount_of_work); /* Session start */ session *s; join_session(&argc, &argv, &s, "worker1.spr"); role *master = s->get_role(s, "master"); /* Dynamic memory allocation of array C */ int *C = NULL; C = (int *) malloc(columns * sizeof(int)); if(C == NULL) { /* Terminate program if there is not enough memory */ fprintf(stderr, "out of memory \n"); exit(0); } /* Declaration of the main array */ int *Beta_worker1 = NULL; /* Dynamic memory allocation */ Beta_worker1 = (int *) malloc( total_size * sizeof(int) ); /* Abort if there is not enough memory */ if(Beta_worker1 == NULL){ fprintf(stderr, "out of memory\n"); exit(-1); } /* The array that will hold the results */ int *worker1_results = NULL; worker1_results = (int *) malloc( amount_of_work * sizeof(int) ); /* Fill with zeros the result array */ fill_with_zeros(worker1_results, amount_of_work); /* The size of the arrays that master waits from the workers */ size_t array_C = columns; size_t array_Beta = total_size; /* worker1 receives the arrays in order to do the computation */ recv_int_array(master, C, &array_C); recv_int_array(master, Beta_worker1, &array_Beta); /* Run for 1000 rounds */ while(rounds++ < 1000){ /* Main computation from master */ for(i = 0; i < amount_of_work; i++) for(j = 0; j < columns; j++) worker1_results[i] += Beta_worker1[i * amount_of_work + j] * C[j]; }//End of while /* worker1 sends the results to the master */ send_int_array(master, worker1_results, amount_of_work); /* End Session */ end_session(s); /* Deallocate memmory */ free(C); C = NULL; free(Beta_worker1); Beta_worker1 = NULL; free(worker1_results); worker1_results = NULL; return EXIT_SUCCESS; }
JNIEXPORT jshortArray JNICALL Java_cs_ucla_edu_bwaspark_jni_SWExtendFPGAJNI_swExtendFPGAJNI (JNIEnv *env, jobject thisObj, jint retTaskNum, jbyteArray arrayIn) { // shared memory int shmid; char *shm_addr = NULL; // polling setting struct timespec deadline; deadline.tv_sec = 0; deadline.tv_nsec = 100; printf("Get JNI data\n"); jbyte* dataArray = (jbyte*) (*env)->GetByteArrayElements(env, arrayIn, NULL); jsize dataArraySize = (*env)->GetArrayLength(env, arrayIn); // shared memory setup if((shmid = shmget(IPC_PRIVATE, FLAG_NUM * sizeof(int) + dataArraySize * sizeof(jbyte), IPC_CREAT | 0666)) < 0) { perror("shmget failed."); exit(1); } else printf("Create shared memory: %d\n", shmid); if((shm_addr = (char*) shmat(shmid, NULL, 0)) == (char *) -1) { perror("Client: shmat failed."); exit(1); } else printf("Client: attach shared memory: %p\n", shm_addr); // initialize the flags of shared memory *((int*)shm_addr) = NOT_READY; *((int*)(shm_addr + sizeof(int))) = NOT_READY; // put input data printf("Client: put input data\n"); //printf("Shmid: %d, Dataarray size: %d\n", shmid, dataArraySize); memcpy(shm_addr + FLAG_NUM * sizeof(int), dataArray, (int) dataArraySize * sizeof(jbyte)); *((int*)shm_addr) = DONE; //printf("After memcpy\n", shmid, dataArraySize); // send a request (shmid) and data array size to the FPGA host thread int int_buf[2]; int_buf[0] = shmid; int_buf[1] = dataArraySize / 4; //printf("Shmid: %d, Data size (# of int): %d, Task Num: %d\n", int_buf[0], int_buf[1], retTaskNum); printf("Send shmid and data size through socket\n"); while(send_int_array(int_buf, 2)) clock_nanosleep(CLOCK_REALTIME, 0, &deadline, NULL); // poll the shared memory printf("Poll\n"); volatile int done = 0; while(done == 0) { done = (int) *((int*)(shm_addr + sizeof(int))); clock_nanosleep(CLOCK_REALTIME, 0, &deadline, NULL); //usleep(1); } //printf("fill data\n"); jshortArray ret = (*env)->NewShortArray(env, retTaskNum); // copy data from the shared memory int i; jshort* fill = (jshort*) malloc(retTaskNum * sizeof(jshort)); for(i = 0; i < retTaskNum; i++) fill[i] = *((jshort*)(shm_addr + (FLAG_NUM * 2 + i) * sizeof(jshort))); // free the shared memory shmdt(shm_addr); shmctl(shmid, IPC_RMID, 0); (*env)->SetShortArrayRegion(env, ret, 0, retTaskNum, fill); (*env)->ReleaseByteArrayElements(env, arrayIn, dataArray, 0); free(fill); printf("done\n"); return ret; }
int send_lp_data_u(sym_environment *env, int sender) { #if defined(COMPILE_IN_TM) && defined(COMPILE_IN_LP) int i; tm_prob *tm = env->tm; tm->par.max_active_nodes = env->par.tm_par.max_active_nodes; #ifdef _OPENMP omp_set_dynamic(FALSE); omp_set_num_threads(tm->par.max_active_nodes); #else tm->par.max_active_nodes = 1; #endif tm->lpp = (lp_prob **) malloc(tm->par.max_active_nodes * sizeof(lp_prob *)); #pragma omp parallel for for (i = 0; i < tm->par.max_active_nodes; i ++){ tm->lpp[i] = (lp_prob *) calloc(1, sizeof(lp_prob)); tm->lpp[i]->proc_index = i; tm->lpp[i]->par = env->par.lp_par; if ((tm->lpp[i]->has_ub = env->has_ub)){ tm->lpp[i]->ub = env->ub; }else{ env->ub = - (MAXDOUBLE / 2); } if (env->par.multi_criteria){ if ((tm->lpp[i]->has_mc_ub = env->has_mc_ub)){ tm->lpp[i]->mc_ub = env->mc_ub; tm->lpp[i]->obj[0] = env->obj[0]; tm->lpp[i]->obj[1] = env->obj[1]; }else{ env->mc_ub = - (MAXDOUBLE / 2); } tm->lpp[i]->utopia[0] = env->utopia[0]; tm->lpp[i]->utopia[1] = env->utopia[1]; } tm->lpp[i]->draw_graph = env->dg_tid; tm->lpp[i]->base = *(env->base); tm->lpp[i]->mip = env->mip; #ifdef USE_SYM_APPLICATION CALL_USER_FUNCTION( user_send_lp_data(env->user, &(tm->lpp[i]->user)) ); #endif } #else int s_bufid; s_bufid = init_send(DataInPlace); send_char_array((char *)(&env->par.lp_par), sizeof(lp_params)); send_char_array(&env->has_ub, 1); if (env->has_ub) send_dbl_array(&env->ub, 1); if (env->par.multi_crtieria){ send_char_array(&env->has_mc_ub, 1); if (env->has_mc_ub){ send_dbl_array(&env->mc_ub, 1); send_dbl_array(env->obj, 2); } send_dbl_array(env->utopia, 2); } send_int_array(&env->dg_tid, 1); send_int_array(&env->base->varnum, 1); if (env->base->varnum){ send_int_array(env->base->userind, env->base->varnum); } send_int_array(&env->base->cutnum, 1); if (env->mip){ MIPdesc *mip = env->mip; char has_desc = TRUE; char has_colnames = FALSE; send_char_array(&has_desc, 1); send_int_array(&(mip->m), 1); send_int_array(&(mip->n), 1); send_int_array(&(mip->nz), 1); send_char_array(&(mip->obj_sense), 1); send_dbl_array(&(mip->obj_offset), 1); send_int_array(mip->matbeg, mip->n); send_int_array(mip->matind, mip->nz); send_dbl_array(mip->matval, mip->nz); send_dbl_array(mip->obj, mip->n); if (env->par.multi_criteria){ send_dbl_array(mip->obj, mip->n); send_dbl_array(mip->obj2, mip->n); } send_dbl_array(mip->rhs, mip->m); send_char_array(mip->sense, mip->m); send_dbl_array(mip->rngval, mip->m); send_dbl_array(mip->ub, mip->n); send_dbl_array(mip->lb, mip->n); send_char_array(mip->is_int, mip->n); if (mip->colname){ int i; has_colnames = TRUE; send_char_array(&has_colnames, 1); for (i = 0; i < mip->n; i++){ send_char_array(mip->colname[i], 8); } }else{ send_char_array(&has_colnames, 1); } }else{ char has_desc = FALSE; send_char_array(&has_desc, 1); } #ifdef USE_SYM_APPLICATION CALL_USER_FUNCTION( user_send_lp_data(env->user, NULL) ); #endif send_msg(sender, LP_DATA); freebuf(s_bufid); #endif return(FUNCTION_TERMINATED_NORMALLY); }
node_desc *create_explicit_node_desc(lp_prob *p) { LPdata *lp_data = p->lp_data; int m = lp_data->m, n = lp_data->n; int bvarnum = p->base.varnum; var_desc **extravars = lp_data->vars + bvarnum; int extravarnum = n - bvarnum; int bcutnum = p->base.cutnum; row_data *rows = lp_data->rows; int extrarownum = m - bcutnum; int cutindsize; node_desc *desc = (node_desc *) calloc(1, sizeof(node_desc)); /* Will need these anyway for basis */ int *rstat = (int *) malloc(m * ISIZE); int *cstat = (int *) malloc(n * ISIZE); int *erstat = (extrarownum == 0) ? NULL : (int *) malloc(extrarownum*ISIZE); int *ecstat = (extravarnum == 0) ? NULL : (int *) malloc(extravarnum*ISIZE); int *ulist, *clist; /* this later uses tmp.i1 */ int cutcnt, i, j; #ifndef COMPILE_IN_LP int s_bufid, r_bufid; #endif get_basis(lp_data, cstat, rstat); if (extrarownum > 0) memcpy(erstat, rstat + bcutnum, extrarownum * ISIZE); if (extravarnum > 0) memcpy(ecstat, cstat + bvarnum, extravarnum * ISIZE); /* To start with, send the non-indexed cuts (only those which will be saved) to the treemanager and ask for names */ for (cutcnt = cutindsize = 0, i = bcutnum; i < m; i++){ if ((rows[i].cut->branch & CUT_BRANCHED_ON) || !rows[i].free || (rows[i].free && rstat[i] != SLACK_BASIC)){ cutindsize++; if (rows[i].cut->name < 0) cutcnt++; } } if (cutcnt > 0){ #ifdef COMPILE_IN_LP row_data *tmp_rows = (row_data *) malloc(cutcnt*sizeof(row_data)); for (j = 0, i = bcutnum; j < cutcnt; i++){ if (rows[i].cut->name < 0 && (!rows[i].free || (rows[i].free && rstat[i] != SLACK_BASIC))) tmp_rows[j++] = rows[i]; } unpack_cut_set(p->tm, 0, cutcnt, tmp_rows); FREE(tmp_rows); #else s_bufid = init_send(DataInPlace); send_int_array(&cutcnt, 1); for (i = bcutnum; i < m; i++){ if (rows[i].cut->name < 0 && (!rows[i].free || (rows[i].free && rstat[i] != SLACK_BASIC))) pack_cut(rows[i].cut); } send_msg(p->tree_manager, LP__CUT_NAMES_REQUESTED); freebuf(s_bufid); #endif } /* create the uind list and the extravars basis description */ desc->uind.type = EXPLICIT_LIST; desc->uind.added = 0; desc->uind.size = extravarnum; desc->basis.extravars.type = EXPLICIT_LIST; desc->basis.extravars.size = extravarnum; desc->basis.extravars.list = NULL; if (extravarnum > 0){ desc->uind.list = ulist = (int *) malloc(extravarnum * ISIZE); desc->basis.extravars.stat = ecstat; for (i = extravarnum - 1; i >= 0; i--) ulist[i] = extravars[i]->userind; if (lp_data->ordering == COLIND_ORDERED) qsortucb_ii(ulist, ecstat, extravarnum); }else{ desc->uind.list = NULL; desc->basis.extravars.stat = NULL; } /* create the basevars basis description */ desc->basis.basevars.type = EXPLICIT_LIST; desc->basis.basevars.size = bvarnum; desc->basis.basevars.list = NULL; if (bvarnum) desc->basis.basevars.stat = cstat; else FREE(cstat); /* create the not_fixed list */ desc->nf_status = lp_data->nf_status; if (desc->nf_status == NF_CHECK_AFTER_LAST || desc->nf_status == NF_CHECK_UNTIL_LAST){ desc->not_fixed.type = EXPLICIT_LIST; desc->not_fixed.added = 0; if ((desc->not_fixed.size = lp_data->not_fixed_num) > 0){ desc->not_fixed.list = (int *) malloc(desc->not_fixed.size * ISIZE); memcpy(desc->not_fixed.list, lp_data->not_fixed, lp_data->not_fixed_num * ISIZE); }else{ desc->not_fixed.list = NULL; } } #ifndef COMPILE_IN_LP /* At this point we will need the missing names */ if (cutcnt > 0){ static struct timeval tout = {15, 0}; int *names = lp_data->tmp.i1; /* m */ double start = wall_clock(NULL); do{ r_bufid = treceive_msg(p->tree_manager, LP__CUT_NAMES_SERVED, &tout); if (! r_bufid){ if (pstat(p->tree_manager) != PROCESS_OK){ printf("TM has died -- LP exiting\n\n"); exit(-301); } } }while (! r_bufid); p->comp_times.idle_names += wall_clock(NULL) - start; receive_int_array(names, cutcnt); for (j = 0, i = bcutnum; j < cutcnt; i++){ if (rows[i].cut->name < 0 && (!rows[i].free || (rows[i].free && rstat[i] != SLACK_BASIC))) rows[i].cut->name = names[j++]; } } #endif /* create the cutind list and the extrarows basis description */ desc->cutind.type = EXPLICIT_LIST; desc->cutind.added = 0; desc->cutind.size = cutindsize; desc->basis.extrarows.type = EXPLICIT_LIST; desc->basis.extrarows.list = NULL; desc->basis.extrarows.size = cutindsize; if (cutindsize > 0){ desc->cutind.list = clist = (int *) malloc(cutindsize * ISIZE); desc->basis.extrarows.stat = erstat; for (cutindsize = 0, i = bcutnum; i < m; i++){ if ((rows[i].cut->branch & CUT_BRANCHED_ON) || !rows[i].free || (rows[i].free && rstat[i] != SLACK_BASIC)){ clist[cutindsize] = rows[i].cut->name; erstat[cutindsize++] = rstat[i]; } } qsortucb_ii(clist, erstat, cutindsize); }else{ desc->cutind.list = NULL; desc->basis.extrarows.stat = NULL; } /* create the baserows basis description */ desc->basis.baserows.type = EXPLICIT_LIST; desc->basis.baserows.size = bcutnum; desc->basis.baserows.list = NULL; if (bcutnum) desc->basis.baserows.stat = rstat; else FREE(rstat); /* Mark that there is a basis */ desc->basis.basis_exists = TRUE; /* Add user description */ add_to_desc_u(p, desc); return(desc); }
/* Main function */ int main(int argc, char **argv){ /* Variables declaration */ int i; int j; /* The number of non zero elements */ int *num_of_no_zeros = NULL; /* number of rounds */ int rounds = 0; /* Session start */ session *s; join_session(&argc, &argv, &s, "worker3.spr"); role *Master = s->get_role(s, "Master"); printf("I am here\n"); /* Receive number of non zero elements */ receive_int(Master, &num_of_no_zeros); printf("NUm of zeros is %d\n", *num_of_no_zeros); /* Number of non zero elements */ int no_zeros = *num_of_no_zeros; /* Declaration and dynamic memory allocation af array row_ptr */ int *row_ptr = (int *) malloc((nrows + 1) * sizeof(int)); if(row_ptr == NULL){ fprintf(stderr, "Out of memory, aborting program...\n"); exit(-1); } /* Decalration and dynamic memory allocation af array values */ int *values = (int *) malloc(no_zeros * sizeof(int)); if(values == NULL){ fprintf(stderr, "Out of memory, aborting program...\n"); exit(-1); } /* Declaration and dynamic memory allocation af col_ind array */ int *col_ind = (int *) malloc(no_zeros * sizeof(int)); if(col_ind == NULL){ fprintf(stderr, "Out of memory, aborting program...\n"); exit(-1); } /* Declaration and dynamic memory allocation af x array */ int *x = (int *) malloc(ncolumns * sizeof(int)); if(x == NULL){ fprintf(stderr, "Out of memory, aborting program...\n"); exit(-1); } /* Expected size of array */ size_t sz_rows = 1001; size_t sz_cols = 1000; size_t sz_no_zeros = no_zeros; /* Receive row_ptr from master */ recv_int_array(Master, row_ptr, &sz_rows); /* Receive values array from master */ recv_int_array(Master, values, &sz_no_zeros); /* Receive col_ind array from master */ recv_int_array(Master, col_ind, &sz_no_zeros); /* Receive x array from master */ recv_int_array(Master, x, &sz_cols); printf("value[%d] = %d\n", 0, values[0]); /* The array that will hold the results of the main computation */ int *results = NULL; /* Define the amount of work that worker 1 will do */ int start_work; int end_work; int amount_of_work; /* The amount of work that the master will do */ start_work = floor((worker3 * nrows)/participants) / 10; end_work = floor(((worker3 + 1) * nrows)/participants) / 10; amount_of_work = (end_work - start_work); printf("start = %d - end = %d - amount of work = %d\n", start_work, end_work, amount_of_work); /* Declaration and dynamic memory allocation af x array */ results = (int *) malloc( 2 * amount_of_work * sizeof(int)); if(results == NULL){ fprintf(stderr, "Out of memory, aborting program...\n"); exit(-1); } /* Run for 100 rounds */ while(rounds++ < 10000){ /* Main computation of the result. Worker1 computes the work that is assigned to it*/ for(i = start_work; i < end_work; i++){ for(j = row_ptr[i]; j < row_ptr[i + 1]; j++) results[i] += values[j] * x[col_ind[j]]; } } int array[amount_of_work]; for(i = 0; i < amount_of_work; i++) array[i] = 0; //send_int_array(Master, array, amount_of_work); //printf("here i am \n"); /* Send the results to the master */ //send_int_array(Master, values, amount_of_work); send_int_array(Master, results, amount_of_work); //send_int(Master, amount_of_work); //recv_int_array(Master, col_ind, &sz_no_zeros); printf("gamw!!!\n"); /* End current session */ end_session(s); /* Free memory */ free(row_ptr); row_ptr = NULL; free(values); values = NULL; free(col_ind); col_ind = NULL; free(x); x = NULL; free(results); results = NULL; return EXIT_SUCCESS; }
int main(void) { dg_prob *dgp; dg_params *par; FILE *read_from, *write_to; int childpid, sender; char tcl_msg[MAX_LINE_LENGTH +1]; char name[MAX_NAME_LENGTH +1], name2[MAX_NAME_LENGTH +1]; char source[MAX_NAME_LENGTH +1], target[MAX_NAME_LENGTH +1]; char title[MAX_TITLE_LENGTH +1], title2[MAX_TITLE_LENGTH +1]; char fname[MAX_FILE_NAME_LENGTH +1]; char old_weight[MAX_WEIGHT_LENGTH +1], new_weight[MAX_WEIGHT_LENGTH +1]; char new_label[MAX_LABEL_LENGTH +1]; char new_dash[MAX_DASH_PATTERN_LENGTH +1]; char *str; int msgtag, keyword, key, r_bufid, s_bufid, bufid, bytes, len; int i, j, k, number, add_nodenum, change_nodenum, delete_nodenum; int add_edgenum, change_edgenum, delete_edgenum; int nodenum, new_nodenum, edgenum, new_edgenum, node_id, edge_id; int new_radius, old_deleted_nodenum; unsigned int id; win_desc *desc; dg_graph *g; dg_node *nodes, *nod; dg_edge *edges, *edg; window *win, *new_win, *source_win, *target_win; register_process(); dgp = (dg_prob *) calloc(1, sizeof(dg_prob)); /* receive parameters from the master */ r_bufid = receive_msg(ANYONE, DG_DATA); bufinfo(r_bufid, &bytes, &msgtag, &dgp->master); receive_char_array((char *)&dgp->par, sizeof(dg_params)); freebuf(r_bufid); par = &(dgp->par); echo_commands = par->echo_commands; /* fork the wish shell */ childpid = start_child((char *)"wish", &read_from, &write_to); /* Source the tcl scripts into wish and invoke startUp*/ spprint(write_to, "source %s/Init.tcl\n", par->source_path); spprint(write_to, "source %s/Tools.tcl\n", par->source_path); spprint(write_to, "source %s/NodeEdgeBasics.tcl\n", par->source_path); spprint(write_to, "source %s/FileMenu.tcl\n", par->source_path); spprint(write_to, "source %s/WindowMenu.tcl\n", par->source_path); spprint(write_to, "source %s/NodeMenu.tcl\n", par->source_path); spprint(write_to, "source %s/EdgeMenu.tcl\n", par->source_path); spprint(write_to, "source %s/CAppl.tcl\n", par->source_path); spprint(write_to, "Igd_StartUp\n"); /* set application defaults to those stored in par */ spprint(write_to, "Igd_SetApplDefaults %i %i %i %i %i %i %i {%s} {%s} %i %i %i %f {%s} {%s} {%s}\n", par->canvas_width, par->canvas_height, par->viewable_width, par->viewable_height, par->disp_nodelabels, par->disp_nodeweights, par->disp_edgeweights, par->node_dash, par->edge_dash, par->node_radius, par->interactive_mode, par->mouse_tracking, par->scale_factor, par->nodelabel_font, par->nodeweight_font, par->edgeweight_font); /* invoke user initialization */ #ifdef USE_SYM_APPLICATION CALL_USER_FUNCTION( user_initialize_dg(&dgp->user) ); #endif while(TRUE){ msgtag = 0; if (dgp->waiting_to_die){ for ( i = 0; i < dgp->window_num; ){ if ( ! dgp->windows[i]->wait_for_click ){ spprint(write_to, "Igd_QuitWindow %u\n",dgp->windows[i]->id); free_window(&dgp->window_num, dgp->windows, i); }else{ i++; } } if ( ! dgp->window_num ) wait_for_you_can_die(dgp, write_to); } /* Interpret message coming from the tcl application. */ if (fgets(tcl_msg, 80, read_from) != NULL) { sscanf(tcl_msg, "%i", &msgtag); switch(msgtag){ case IGDTOI_CLICK_HAPPENED: /* if wait_for_click is 2, send a message to the owner */ fgets(name2, MAX_NAME_LENGTH +1, read_from); sscanf(name2, "%u", &id); for (i = dgp->window_num - 1; i >= 0; i-- ) if ( dgp->windows[i]->id == id ) break; if ( i < 0 ) { /* this should never happen */ printf("Window of id %u is not found\n", id); break; } if ( dgp->windows[i]->wait_for_click == 2 ) { s_bufid = init_send(DataInPlace); send_str(name); send_msg(dgp->windows[i]->owner_tid, ITOC_CLICK_HAPPENED); freebuf(s_bufid); } dgp->windows[i]->wait_for_click = 0; break; case IGDTOI_QUIT_WINDOW: /* delete data structure corresponding to this window */ fgets(name2, MAX_NAME_LENGTH +1, read_from); sscanf(name2, "%u", &id); for (i = dgp->window_num - 1; i >= 0; i-- ) if ( dgp->windows[i]->id == id ) break; if ( i < 0 ) { /* this should never happen */ printf("Window of id %u is not found\n", id); break; } spprint(write_to, "Igd_QuitWindow %u\n", id); free_window(&dgp->window_num, dgp->windows, i); break; case IGDTOI_QUIT_APPLICATION: /* delete all windows */ for ( i = 0; i < dgp->window_num; ){ if ( ! dgp->windows[i]->wait_for_click ){ spprint(write_to, "Igd_QuitWindow %u\n",dgp->windows[i]->id); free_window(&dgp->window_num, dgp->windows, i); }else{ i++; } } dgp->waiting_to_die = TRUE; break; case IGDTOI_TEXT_ENTERED: fgets(name2, MAX_NAME_LENGTH +1, read_from); sscanf(name2, "%u", &id); for (i = dgp->window_num - 1; i >= 0; i-- ) if ( dgp->windows[i]->id == id ) break; win = dgp->windows[i]; if ( i < 0 ) { /* this should never happen */ printf("Window of id %u is not found\n", id); break; } fgets(tcl_msg, MAX_LINE_LENGTH +1, read_from); sscanf(tcl_msg, "%i", &win->text_length); win->text = (char *) malloc( (win->text_length + 1) * CSIZE); fread(win->text, CSIZE, win->text_length, read_from); win->text[win->text_length] = 0; /* invoke function that interprets the message */ #ifdef USE_SYM_APPLICATION CALL_USER_FUNCTION( user_interpret_text(dgp->user, win->text_length, win->text, win->owner_tid) ); #endif break; case IGDTOI_REQUEST_GRAPH: fgets(name2, MAX_NAME_LENGTH +1, read_from); sscanf(name2, "%u", &id); for (i = dgp->window_num - 1; i >= 0; i-- ) if ( dgp->windows[i]->id == id ) break; if ( i < 0 ) { /* this should never happen */ printf("Window of id %u is not found\n", id); break; } display_graph_on_canvas(dgp->windows[i], write_to); break; default: printf("Unknown message type from IGD to I (%i)\n", msgtag); break; } /* end switch */ } /* end if */ if (dgp->waiting_to_die) continue; /* Interpret the message coming from the C application. All the messages except INITIALIZE_WINDOW and COPY_GRAPH and QUIT will be put on the pipe corresponding to the appropriate window (messages are processed in FIFO order In case of INITIALIZE_WINDOW the data structure associated with a winow is created (including the pipes. In case of COPY_GRAPH a message must be placed on both the source and the target window's pipe. In case of QUIT all data structures are disassembled and then the tcl application is killed. */ r_bufid = nreceive_msg(ANYONE, ANYTHING); if (r_bufid > 0){ bufinfo(r_bufid, &bytes, &msgtag, &sender); switch (msgtag){ case CTOI_INITIALIZE_WINDOW: /* get the name of the new window */ receive_str(name); receive_str(title); /* if a window with this name already exists: error */ i = find_window(dgp->window_num, dgp->windows, name); if ( i >= 0 ) { INTERMED_ERROR(name, msgtag, sender,ITOC_WINDOW_ALREADY_EXISTS); freebuf(r_bufid); break; } /* allocate space for the new window */ win = init_dgwin(dgp, sender, name, title); /* set up the window description */ receive_int_array(&number, 1); copy_win_desc_from_par(win, &dgp->par); for ( ; number > 0; number-- ) { /* read out the key - value pairs */ receive_int_array(&key, 1); set_window_desc_pvm(key, win); } freebuf(r_bufid); break; case CTOI_COPY_GRAPH: /* Copy source's graph into target's window. Here a message is placed onto both target's and source's pipe so that they can wait for each other before the actual copying happens (shake hands) */ receive_str(target); receive_str(source); i = find_window(dgp->window_num, dgp->windows, target); if (i < 0) { /* target doesn't exist, send error message */ INTERMED_ERROR(target, msgtag, sender,ITOC_WINDOW_DOESNT_EXIST); freebuf(r_bufid); break; } j = find_window(dgp->window_num, dgp->windows, source); if (j < 0) { /* source doesn't exist, send error message */ INTERMED_ERROR(source, msgtag, sender,ITOC_WINDOW_DOESNT_EXIST); freebuf(r_bufid); break; } bufid = init_send(DataInPlace); msgtag = WAITING_TO_GET_A_COPY; send_int_array(&msgtag, 1); send_str(source); add_msg(dgp->windows[i], bufid); setsbuf(0); bufid = init_send(DataInPlace); msgtag = WAITING_TO_BE_COPIED; send_int_array(&msgtag, 1); send_str(target); add_msg(dgp->windows[j], bufid); setsbuf(0); freebuf(r_bufid); break; case CTOI_QUIT: /* quit from all windows, disassemble data structures. * (actually, this will happen on the top of the while loop...) */ if (! dgp->waiting_to_die) dgp->waiting_to_die = TRUE; freebuf(r_bufid); break; case CTOI_YOU_CAN_DIE: /* quit from all windows, disassemble data structures. * (actually, this will happen on the top of the while loop...) * and die */ dgp->waiting_to_die = 2 * TRUE; freebuf(r_bufid); break; default: /* Check if window with name exists. If not, send back error message. If yes, copy the message over to window's pipe. */ receive_str(name); len = strlen(name); i = find_window(dgp->window_num, dgp->windows, name); if (i < 0){ /* there is no window of that name: send error message */ INTERMED_ERROR(name, msgtag, sender,ITOC_WINDOW_DOESNT_EXIST); freebuf(r_bufid); break; } add_msg(dgp->windows[i], r_bufid); setrbuf(0); break; } /* end switch */ } /* endif r_bufid > 0 */ if (dgp->waiting_to_die) continue; /* Process one message from each window's pipe. */ for ( i = 0; i < dgp->window_num; i++ ) { win = dgp->windows[i]; /* if wait_for_click is set, skip */ if ( win->wait_for_click ) continue; /* if window is waiting to be copied or waiting to get a copy, skip */ if ( win->copy_status ) continue; /* if no message in the pipe, skip */ if (win->buf.bufread == -1) continue; /* else: process the message .... */ msgtag = 0; r_bufid = get_next_msg(win); setrbuf(r_bufid); bufinfo(r_bufid, &bytes, &msgtag, &sender); if (msgtag == 0){ /* This means that the message was locally 'hand-packed' */ receive_int_array(&msgtag, 1); } switch ( msgtag ) { case CTOI_USER_MESSAGE: #ifdef USE_SYM_APPLICATION user_dg_process_message(win->user, win, write_to); #endif break; case CTOI_QUIT_WINDOW: /* delete this window */ spprint(write_to, "Igd_QuitWindow %u\n", win->id); free_window(&dgp->window_num, dgp->windows, i); i--; break; case CTOI_CHANGE_WINDOW_DESC: /* change window descriptions */ receive_int_array(&number, 1); for ( ; number > 0; number-- ) { /* read out the key - value pairs */ receive_int_array(&key, 1); set_window_desc_pvm(key, win); } desc = &(win->desc); if ( win->window_displayed ) { spprint(write_to, "Igd_SetAndExecuteWindowDesc %u %i %i %i %i %i %i %i {%s} {%s} %i %i %i %f {%s} {%s} {%s}\n", win->id, desc->canvas_width, desc->canvas_height, desc->viewable_width, desc->viewable_height, desc->disp_nodelabels, desc->disp_nodeweights, desc->disp_edgeweights, desc->node_dash, desc->edge_dash, desc->node_radius, desc->interactive_mode, desc->mouse_tracking, desc->scale_factor, desc->nodelabel_font, desc->nodeweight_font, desc->edgeweight_font); } break; case CTOI_SET_GRAPH: case CTOI_SET_AND_DRAW_GRAPH: /* define the graph corresponding to this window */ g = &(win->g); FREE(g->nodes); FREE(g->edges); receive_int_array(&g->nodenum, 1); if ( g->nodenum ) { nodes = g->nodes = (dg_node *) malloc(g->nodenum * sizeof(dg_node)); for ( j = 0; j < g->nodenum; j++ ) { read_node_desc_from_pvm(nodes+j, win); } } receive_int_array(&g->edgenum, 1); if ( g->edgenum ) { edges = g->edges = (dg_edge *) malloc(g->edgenum * sizeof(dg_edge)); for ( j = 0; j < g->edgenum; j++ ) { read_edge_desc_from_pvm(edges+j, win); } } if ( msgtag == CTOI_SET_AND_DRAW_GRAPH || win->window_displayed ) display_graph_on_canvas(win, write_to); break; case CTOI_DRAW_GRAPH: /* first erase/create the window itself, then display all the nodes and edges */ display_graph_on_canvas(win, write_to); break; case CTOI_DELETE_GRAPH: /* delete the data structure of the graph and erase its window if open */ FREE(win->g.nodes); FREE(win->g.edges); win->g.nodenum = win->g.deleted_nodenum = 0; win->g.edgenum = win->g.deleted_edgenum = 0; if ( win->window_displayed ){ spprint(write_to, "Igd_EraseWindow %u\n", win->id); } break; case CTOI_WAIT_FOR_CLICK_NO_REPORT: /* window will not get any messages until the Continue button is pressed. the window has to be open to have an effect */ if ( win->window_displayed ) { win->wait_for_click = 1; spprint(write_to, "Igd_CApplWaitForClick %u\n", win->id); } else { INTERMED_ERROR(win->name, msgtag, win->owner_tid, ITOC_WINDOW_ISNT_DISPLAYED); } break; case CTOI_WAIT_FOR_CLICK_AND_REPORT: /* window will not get any messages until the Continue button is pressed. the window has to be open to have an effect. the owner gets a message */ if ( win->window_displayed ) { win->wait_for_click = 2; spprint(write_to, "Igd_CApplWaitForClick %u\n", win->id); } else { INTERMED_ERROR(win->name, msgtag, win->owner_tid, ITOC_WINDOW_ISNT_DISPLAYED); } break; case CTOI_SAVE_GRAPH_TO_FILE: /* save the graph into a file (only if it is displayed!) */ receive_str(fname); if ( win->window_displayed ) { spprint(write_to, "Igd_SaveGraph %u {%s}\n", win->id, fname); } else { INTERMED_ERROR(win->name, msgtag, win->owner_tid, ITOC_WINDOW_ISNT_DISPLAYED); } break; case CTOI_SAVE_GRAPH_PS_TO_FILE: /* save postscript of the picture displayed. works only if window is displayed. */ receive_str(fname); if ( win->window_displayed ) { spprint(write_to, "Igd_SavePs %u {%s}\n", win->id, fname); } else { INTERMED_ERROR(win->name, msgtag, win->owner_tid, ITOC_WINDOW_ISNT_DISPLAYED); } break; case CTOI_CLONE_WINDOW: /* clone this window. if window is not displayed, only the graph data structure will be copied over. */ /* wait_for_click, copy_status and text will not be copied over. */ receive_str(name2); receive_str(title2); if ( find_window(dgp->window_num, dgp->windows, name2) >= 0 ) { INTERMED_ERROR(win->name, msgtag, sender, ITOC_WINDOW_ALREADY_EXISTS); break; } new_win = init_dgwin(dgp, sender, name2, title2); copy_window_structure(new_win, win); if ( win->window_displayed ) { spprint(write_to, "Igd_CopyWindowDesc %u %u\n", new_win->id, win->id); spprint(write_to, "Igd_InitWindow %u {%s}\n", new_win->id, title2); spprint(write_to, "Igd_DisplayWindow %u\n", new_win->id); spprint(write_to, "Igd_EnableCAppl %u\n", new_win->id); spprint(write_to, "Igd_CopyGraph %u %u\n", new_win->id,win->id); new_win->window_displayed = 1; } break; case CTOI_RENAME_WINDOW: /* change the title of the window */ receive_str(win->title); if ( win->window_displayed ){ spprint(write_to, "Igd_RenameWindow %u {%s}\n", win->id, win->title); } break; case CTOI_RESIZE_VIEWABLE_WINDOW: /* change the sizes of canvas */ receive_int_array(&win->desc.viewable_width, 1); receive_int_array(&win->desc.viewable_height, 1); if ( win->window_displayed ){ spprint(write_to, "Igd_ResizeViewableWindow %u %i %i\n", win->id, win->desc.viewable_width, win->desc.viewable_height); } break; case CTOI_RESIZE_CANVAS: /* change the size of the canvas */ receive_int_array(&win->desc.canvas_width, 1); receive_int_array(&win->desc.canvas_height, 1); if ( win->window_displayed ){ spprint(write_to, "Igd_ResizeCanvas %u %i %i\n", win->id, win->desc.canvas_width, win->desc.canvas_height); } break; case WAITING_TO_GET_A_COPY: /* Read out the name of the source-graph from the pipe. If the source-graph is waiting to be copied, source and target have found each other */ receive_str(win->source) ; win->copy_status = 2; j = find_window(dgp->window_num, dgp->windows, win->source); if ( j >= 0 && dgp->windows[j]->copy_status == 1 ) { /* source graph exists and it is waiting to be copied */ source_win = dgp->windows[j]; /* copy the data structure */ copy_window_structure(win, source_win); /* if the window is displayed, change picture */ if ( win->window_displayed ) { display_graph_on_canvas(win, write_to); } /* zero out the copy stati */ win->copy_status = 0; win->source[0] = 0; source_win->copy_status = 0; source_win->target[0] = 0; } break; case WAITING_TO_BE_COPIED: /* Read out the name of the target graph from the pipe. If the target-graph is waiting to get a copy, source and target have found each other. */ receive_str(win->target); win->copy_status = 1; j = find_window(dgp->window_num, dgp->windows, win->target); if ( j >= 0 && dgp->windows[j]->copy_status == 2 ) { /* target exists and waiting for a copy */ target_win = dgp->windows[j]; /* copy the data structure */ copy_window_structure(target_win, win); /* if the target window is displayed, update the picture */ if ( target_win->window_displayed ) { display_graph_on_canvas(target_win, write_to); } /* zero out the copy stati */ win->copy_status = 0; win->target[0] = 0; target_win->copy_status = 0; target_win->source[0] = 0; } break; case CTOI_MODIFY_GRAPH: /* Make changes in the graph. The data structure is updated, and if the window is displayed, the picture gets updated, too */ /* The message is in keyword - description pairs, with the END_OF_MESSAGE keyword at the end. We switch on the keyword */ do { receive_int_array(&keyword, 1); switch ( keyword ) { case MODIFY_ADD_NODES: /* same format as in SET_GRAPH */ receive_int_array(&add_nodenum, 1); if ( add_nodenum ) { g = &(win->g); nodenum = g->nodenum; nodes = g->nodes = (dg_node *) realloc(g->nodes, (nodenum + add_nodenum) * sizeof(dg_node)); for (j = 0, new_nodenum = nodenum; j < add_nodenum; j++) { read_node_desc_from_pvm(nodes+new_nodenum, win); if (find_node(nodes[new_nodenum].node_id, g) < 0) new_nodenum++; } g->nodenum = new_nodenum; if ( win->window_displayed ) { for ( j = nodenum; j < new_nodenum; j++ ) { nod = nodes + j; spprint(write_to, "Igd_MakeNode %u %i %i %i {%s} {%s} %i\n", win->id, nod->node_id, nod->posx, nod->posy, nod->label, nod->dash, nod->radius); if ( *nod->weight != 0 ){ spprint(write_to, "Igd_MakeNodeWeight %u %i {%s}\n", win->id, nod->node_id, nod->weight); } } } } break; case MODIFY_CHANGE_WEIGHTS_OF_NODES: /* change weights of nodes. nodes not in the graph or nodes already deleted are skipped, no error message is given. */ g = &(win->g); receive_int_array(&change_nodenum, 1); for ( j = 0; j < change_nodenum; j++ ) { receive_int_array(&node_id, 1); receive_str(new_weight); if ( (k = find_node(node_id, g)) >= 0 ) { strcpy(g->nodes[k].weight, new_weight); if ( win->window_displayed ) { strcpy(old_weight, g->nodes[k].weight); if ( *old_weight != 0 ) { if ( *new_weight != 0 ) { spprint(write_to, "Igd_ChangeOneNodeWeight %u %i {%s}\n" , win->id, node_id, new_weight); } else { /* new weight == 0 */ spprint(write_to, "Igd_DeleteNodeWeight %u %i\n", win->id, node_id); } } else { /* no weight before */ if ( *new_weight != 0 ) { spprint(write_to, "Igd_MakeNodeWeight %u %i {%s}\n", win->id, node_id, new_weight); } } } } } break; case MODIFY_CHANGE_LABELS_OF_NODES: /* change labels of nodes. nodes not in the graph or nodes already deleted are skipped, no error message is given */ g = &(win->g); receive_int_array(&change_nodenum, 1); for ( j = 0; j < change_nodenum; j++ ) { receive_int_array(&node_id, 1); receive_str(new_label); if ( (k = find_node(node_id, g)) >= 0 ) { strcpy(g->nodes[k].label, new_label); if ( win->window_displayed ) { spprint(write_to, "Igd_ChangeOneNodeLabel %u %i {%s}\n", win->id, node_id, new_label); } } } break; case MODIFY_CHANGE_DASH_OF_NODES: /* change dash pattern of individual nodes. nodes not in the graph will not cause error messages */ g = &(win->g); receive_int_array(&change_nodenum, 1); for ( j = 0; j < change_nodenum; j++ ) { receive_int_array(&node_id, 1); receive_str(new_dash); if ( (k = find_node(node_id, g)) >= 0 ) { strcpy(g->nodes[k].dash, new_dash); if ( win->window_displayed ){ spprint(write_to, "Igd_ChangeOneNodeDash %u %i {%s}\n", win->id, node_id, new_dash); } } } break; case MODIFY_CHANGE_RADII_OF_NODES: /* change radii of individual nodes. nodes not in the graph will not cause error messages */ g = &(win->g); receive_int_array(&change_nodenum, 1); for ( j = 0; j < change_nodenum; j++ ) { receive_int_array(&node_id, 1); receive_int_array(&new_radius, 1); if ( (k = find_node(node_id, g)) >= 0 ) { g->nodes[k].radius = new_radius; if ( win->window_displayed ){ spprint(write_to, "Igd_ChangeOneNodeRadius %u %i %i\n", win->id, node_id, new_radius); } } } break; case MODIFY_DELETE_NODES: /* nodes not in the graph will not cause error messages */ receive_int_array(&delete_nodenum, 1); if ( delete_nodenum ) { g = &(win->g); old_deleted_nodenum = g->deleted_nodenum; for ( j = 0; j < delete_nodenum; j++ ) { receive_int_array(&node_id, 1); if ( (k = find_node(node_id, g)) >= 0 ) { g->nodes[k].deleted = 1; g->deleted_nodenum++; if ( win->window_displayed ){ spprint(write_to, "Igd_DeleteNode %u %i\n", win->id, node_id); } } } if ( g->deleted_nodenum > old_deleted_nodenum ) { /* mark edges that have at least one deleted endpoint to be deleted. Igd_DeleteNode already took care of deleting these edges from the picture */ for (k=g->edgenum-1, edg=g->edges; k >= 0; k--, edg++) if ( ! edg->deleted && ((find_node(edg->tail, g) < 0) || (find_node(edg->head, g) < 0))){ edg->deleted = 1; g->deleted_edgenum++; } } /* if too many nodes and/or edges have been deleted, compress the graph */ if ( g->deleted_nodenum > 0.1 * g->nodenum || g->deleted_edgenum > 0.1 * g->edgenum ) compress_graph(g); } break; case MODIFY_ADD_EDGES: /* same format as in SET_GRAPH. Nonvalid edges (one or both endpoints is not in the graph will not cause an error message. */ receive_int_array(&add_edgenum, 1); if ( add_edgenum ) { g = &(win->g); edgenum = g->edgenum; edges = g->edges = (dg_edge *) realloc(g->edges, (edgenum+add_edgenum)*sizeof(dg_edge)); for (j = 0, new_edgenum = edgenum; j < add_edgenum; j++) { edg = edges + new_edgenum; read_edge_desc_from_pvm(edg, win); if ((find_edge(edg->edge_id, g) < 0) && (find_node(edg->tail, g) >= 0) && (find_node(edg->head, g) >= 0)) new_edgenum++; } g->edgenum = new_edgenum; if ( win->window_displayed ) { for ( j = edgenum; j < new_edgenum; j++ ) { edg = edges + j; spprint(write_to, "Igd_MakeEdge %u %i %i %i {%s}\n", win->id, edg->edge_id, edg->tail, edg->head, edg->dash); if ( *edg->weight != 0 ){ spprint(write_to, "Igd_MakeEdgeWeight %u %i {%s}\n", win->id, edg->edge_id, edg->weight); } } } } break; case MODIFY_CHANGE_WEIGHTS_OF_EDGES: /* change weights of edges. edges not in the graph or edges already deleted are skipped, no error message is given. */ g = &(win->g); receive_int_array(&change_edgenum, 1); for ( j = 0; j < change_edgenum; j++ ) { receive_int_array(&edge_id, 1); receive_str(new_weight); if ( (k = find_edge(edge_id, g)) >= 0 ) { strcpy(g->edges[k].weight, new_weight); if ( win->window_displayed ) { strcpy(old_weight, g->edges[k].weight); if ( *old_weight != 0 ) { if ( *new_weight != 0 ) { spprint(write_to, "Igd_ChangeOneEdgeWeight %u %i {%s}\n" , win->id, edge_id, new_weight); } else { /* new weight : 0 */ spprint(write_to, "Igd_DeleteEdgeWeight %u %i\n", win->id, edge_id); } } else { /* no weight before */ if ( *new_weight != 0 ) { spprint(write_to, "Igd_MakeEdgeWeight %u %i {%s}\n", win->id, edge_id, new_weight); } } } } } break; case MODIFY_CHANGE_DASH_OF_EDGES: /* change dash pattern of individual edges. edges not in the graph will not cause error messages */ g = &(win->g); receive_int_array(&change_edgenum, 1); for ( j = 0; j < change_edgenum; j++ ) { receive_int_array(&edge_id, 1); receive_str(new_dash); if ( (k = find_edge(edge_id, g)) >= 0 ) { strcpy(g->edges[k].dash, new_dash); if ( win->window_displayed ){ spprint(write_to, "Igd_ChangeOneEdgeDash %u %i {%s}\n", win->id, edge_id, new_dash); } } } break; case MODIFY_DELETE_EDGES: /* edges not in the graph will not cause error messages */ g = &(win->g); receive_int_array(&delete_edgenum, 1); for ( j = 0; j < delete_edgenum; j++ ) { receive_int_array(&edge_id, 1); if ( (k = find_edge(edge_id, g)) >= 0 ) { g->edges[k].deleted = 1; g->deleted_edgenum++; if ( win->window_displayed ) { spprint(write_to, "Igd_DeleteEdge %u %i\n", win->id, edge_id); } } } /* if too many edges have been deleted, compress the graph */ if ( g->deleted_edgenum > 0.1 * g->edgenum ) compress_graph(g); break; case MODIFY_DELETE_ALL_EDGES: /* will delete all edges from the graph */ g = &(win->g); if ( win->window_displayed ) { for ( j = 0; j < g->edgenum; j++ ) if ( ! g->edges[j].deleted ){ spprint(write_to, "Igd_DeleteEdge %u %i\n", win->id, g->edges[j].edge_id); } } FREE(g->edges); g->edgenum = 0; break; case MODIFY_END_OF_MESSAGE: break; default: printf("Unrecognized keyword %i\n", keyword); break; } /* end switch (keyword) */ } while ( keyword != MODIFY_END_OF_MESSAGE ); break; case CTOI_CLEAR_MESSAGE: if ( win->window_displayed ) { spprint(write_to, "Igd_CApplClearCmsg %u\n", win->id); } break; case CTOI_PRINT_MESSAGE: if ( win->window_displayed ) { str = malloc(bytes); receive_str(str); spprint(write_to, "Igd_CApplSetCmsg %u {%s}\n", win->id, str); FREE(str); } break; case CTOI_APPEND_MESSAGE: if ( win->window_displayed ) { str = malloc(bytes); receive_str(str); spprint(write_to, "Igd_CApplAppendCmsg %u {%s}\n", win->id,str); FREE(str); } break; default: printf("Unknown message tag: %i\n", msgtag); break; } /* end switch (msgtag) */ freebuf(r_bufid); } /* end for */ } /* end while */ return(0); }
inline int send_int(int val, role *r, const char *label) { return send_int_array(&val, 1, r, label); }
inline int bcast_int_array(const int arr[], size_t count, session *s) { return send_int_array(arr, count, s->r(s, "_Others"), NULL); }
inline int bcast_int(int val, session *s) { return send_int_array(&val, 1, s->r(s, "_Others"), NULL); }
void lower_bound(vrp_problem *vrp, lb_params *lb_par, heurs *lh, int ub, int jobs, int *tids, int *sent) { int s_bufid, dummy; int y, i, alpha, interval; int *sorted_demand, trials; int m1, numroutes = vrp->numroutes, capacity = vrp->capacity; if (!lb_par->lower_bound) return; if (vrp->par.verbosity > 1) printf("\nNow beginning lower bounding ....\n\n"); /*Calculate m1 = maximum # of single vehicle routes*/ sorted_demand = (int *) calloc (vrp->vertnum, sizeof(int)); memcpy (sorted_demand, vrp->demand, vrp->vertnum*sizeof(int)); qsort (sorted_demand+1, vrp->vertnum-1, sizeof(int), intcompar); for (m1 = 0;; m1++) if (!(capacity*(numroutes-m1-1) >= sum(sorted_demand, m1+2, vrp->vertnum-1))) break; trials = (lb_par->lower_bound)*(numroutes-m1); lh->tids = tids; lh->jobs = jobs; if (!jobs) { fprintf(stderr, "\nNo jobs started .... \n\n"); return; } else if (vrp->par.verbosity >2) printf("\n%i jobs started ...\n\n", jobs); /*-----------------------------------------------------------------------*\ | Broadcast data to the lower bounding procedure | \*-----------------------------------------------------------------------*/ for(i=0; i<trials; i++) { s_bufid = init_send(DataInPlace); send_int_array(&dummy, 1); send_msg(tids[i%jobs], MST); sent[i%jobs]++; broadcast(vrp, tids+(i%jobs), 1); s_bufid = init_send(DataInPlace); send_int_array(&numroutes, 1); send_int_array(&ub, 1); send_int_array(&lb_par->lb_max_iter, 1); send_int_array(&m1, 1); send_msg(tids[i%jobs], VRP_LB_DATA); } interval = lb_par->lb_penalty_mult/lb_par->lower_bound; for (i=trials-1, y = m1, alpha = lb_par->lb_penalty_mult; i>=0; i--, y++) { s_bufid = init_send(DataInPlace); send_int_array(&y, 1); send_int_array(&alpha, 1); send_msg(tids[i%jobs], VRP_LB_DATA2); if (y == numroutes) { y = m1-1; alpha -= interval; } } freebuf(s_bufid); FREE(sorted_demand); }
void pack_array_desc(array_desc *adesc) { send_char_array((char *)adesc, sizeof(array_desc)); if (adesc->type != NO_DATA_STORED && adesc->size > 0) send_int_array(adesc->list, adesc->size); }