adpvm_manager::adpvm_manager(int _mode) { cout << "calling load library" << endl; if (load_adpvm_library() < 0) { cerr << "error loading pvm library" << endl; exit(1); } pvm_setopt(PvmRoute, PvmRouteDirect); /* channel for communication */ /* get and display configuration of the parallel machine */ int status=pvm_config( &nhost, &narch, &hostp ); /* get configuration */ if (status<0) { cerr << "error trying to get configuration of pvm (virtual machine)" << endl; if (status == PvmSysErr) cerr << " PVM Daemon not responing -- maybe it is not started" << endl; ad_exit(1); } printf("I found the following hosts in your virtual machine\n"); int i; for (i = 0; i < nhost; i++) { printf(" %s\n", hostp[i].hi_name); } //id.allocate(0,nhost); mode=_mode; if (mode == 1) // master { slave_argv = new adpvm_slave_args(20,20); int ierr=pvm_catchout(stdout); if (ierr<0) { cerr << "Error in pvm_catchout" << endl; } strcpy(*slave_argv,"progname"); int on1,nopt1; if ( (on1=option_match(ad_comm::argc,ad_comm::argv,"-exec",nopt1))>-1) { if (nopt1 !=1) { cerr << "Wrong number of options to -exec -- must be 1" " you have " << nopt1 << endl; ad_exit(1); } slave_names+= ad_comm::argv[on1+1]; } else { slave_names+="test"; } } timing_flag=0; int on1,nopt1; if ( (on1=option_match(ad_comm::argc,ad_comm::argv,"-pvmtime",nopt1))>-1) { timing_flag=1; } }
value Pvm_catchout(value catching) { int res,val; switch (Int_val(catching)) { case 0 : res = pvm_catchout(NULL); break; case 1 : res = pvm_catchout(stdout); break; default: fprintf(stderr,"Erreur dans Pvm_catchout en C\n"); exit(-1); } if (res<0) TreatError(res); return; }
void BBSDirect::start() { char* client = 0; int tid, host_mytid; int i, n, ncpu, nncpu; struct pvmhostinfo* hostp; if (started_) { return; } BBSImpl::start(); mytid_ = pvm_mytid(); nrnmpi_myid = 0; if (mytid_ < 0) { perror("start"); } host_mytid = pvm_tidtohost(mytid_); tid = pvm_parent(); if (tid == PvmSysErr) { perror("start"); }else if (tid == PvmNoParent) { is_master_ = true; pvm_catchout(stdout); pvm_setopt(PvmRoute, PvmRouteDirect); pvm_config(&n, NULL, &hostp); nncpu = 0; for (i=0; i < n; ++i) { ncpu = hostp[i].hi_speed; if (ncpu%1000) { hoc_warning(hostp[i].hi_name, " speed in pvm configuration file is not a multiple of 1000. Assuming 1000."); ncpu = 1000; } nncpu += ncpu/1000; } nrnmpi_numprocs = nncpu; ncids = 0; }else{ // a worker, impossible assert(false); } if (nrnmpi_numprocs > 1 && tid == PvmNoParent) { char ** sargv; // args are workingdirectory specialOrNrniv -bbs_nhost nhost args sargv = new char*[nrn_global_argc + 4]; for (i=1; i < nrn_global_argc; ++i) { sargv[i+3] = nrn_global_argv[i]; } sargv[nrn_global_argc + 3] = 0; sargv[0] = rel_working_dir(); //printf("sargv[0]=|%s|\n", sargv[0]); sargv[1] = nrn_global_argv[0]; sargv[2] = "-bbs_nhost"; sargv[3] = new char[10]; sprintf(sargv[3], "%d", nrnmpi_numprocs); cids = new int[nrnmpi_numprocs-1]; if (nrn_global_argv[nrn_global_argc] != 0) { printf("argv not null terminated\n"); exit(1); } BBSDirectServer::server_->start(); bbs_sig_set(); bbs_handle(); //spawn according to number of cpu's (but master has one less) //printf("%d total number of cpus on %d machines\n", nncpu, n); int icid = 0; bool first = true; while (icid < nrnmpi_numprocs - 1) { for (i=0; i < n; ++i) { ncpu = hostp[i].hi_speed; if (ncpu%1000) { ncpu = 1000; } ncpu /= 1000; //printf("%d cpu for machine %d (%s)\n", ncpu, i, hostp[i].hi_name); if (first && hostp[i].hi_tid == host_mytid) { // spawn one fewer on master first time through --ncpu; } if (icid + ncpu >= nrnmpi_numprocs) { ncpu = nrnmpi_numprocs - icid - 1; } //printf("before spawn %d processes (icid=%d) for machine %d (%s)\n", ncpu, icid, i, hostp[i].hi_name); if (ncpu) { ncids = pvm_spawn("bbswork.sh", sargv, PvmTaskHost, hostp[i].hi_name, ncpu, cids + icid); if (ncids != ncpu) { fprintf(stderr, "Tried to spawn %d tasks, only %d succeeded on %s\n", ncpu, ncids, hostp[i].hi_name); hoc_execerror("Could not spawn all the requested tasks for", hostp[i].hi_name); } //printf("spawned %d for %s with cids starting at %d\n", ncpu, hostp[i].hi_name, icid); icid += ncpu; } if (icid >= nrnmpi_numprocs) { break; } } first = false; } ncids = icid; printf("spawned %d more %s on %d cpus on %d machines\n", ncids, nrn_global_argv[0], nncpu, n); delete [] sargv[3]; delete [] sargv; } }
int main(int argc, char *argv[]) { /* argv[1] - ilość narciarzy */ time_t t; srand((unsigned)time(&t)); int number_of_skiers = 0; if (argc >= 2) { number_of_skiers = atoi(argv[1]); } else { number_of_skiers = rand() % 50 + 10; } printf("Liczba narciarzy: %d\n", number_of_skiers); int *skiers_weights; skiers_weights = malloc(sizeof(int) * number_of_skiers); int min_skier_weight = SKIERS_MAX_WEIGHT; int sum_skiers_weights = 0; int i; int weights_arg_index = -1; for (i = 0; i < argc; i++) { if (!strcmp(argv[i], "weights")) { weights_arg_index = i; break; } } for(i=0; i<number_of_skiers; i++) { if (weights_arg_index == -1) skiers_weights[i] = rand() % (SKIERS_MAX_WEIGHT - SKIERS_MIN_WEIGHT + 1) + SKIERS_MIN_WEIGHT; else skiers_weights[i] = atoi(argv[weights_arg_index + 1 + i]); sum_skiers_weights += skiers_weights[i]; if (skiers_weights[i] < min_skier_weight) { min_skier_weight = skiers_weights[i]; } } for(i=0; i<number_of_skiers; i++) { printf("Narciarz %d ma wage: %d\n", i, skiers_weights[i]); } printf("Min waga: %d\n", min_skier_weight); printf("Sum waga: %d\n", sum_skiers_weights); int min_perc_sum = 0.2 * sum_skiers_weights; int max_perc_sum = 0.4 * sum_skiers_weights; int first_lift_capacity; int second_lift_capacity; do { first_lift_capacity = rand() % (max_perc_sum - min_perc_sum) + min_perc_sum; } while (first_lift_capacity < min_skier_weight); do { second_lift_capacity = rand() % (max_perc_sum - min_perc_sum) + min_perc_sum; } while (second_lift_capacity < min_skier_weight); // read lifts capacities from console int lifts_arg_index = -1; for (i = 0; i < argc; i++) { if (!strcmp(argv[i], "lifts")) { lifts_arg_index = i; break; } } if (lifts_arg_index != -1) { first_lift_capacity = atoi(argv[lifts_arg_index + 1]); second_lift_capacity = atoi(argv[lifts_arg_index + 2]); } printf("Pierwszy wyciag: %d\n", first_lift_capacity); printf("Drugi wyciag: %d\n", second_lift_capacity); int mytid; int tids[number_of_skiers]; /* slave task ids */ char slave_name[NAMESIZE]; int nproc, j, who; mytid = pvm_mytid(); pvm_catchout(stdout); nproc=pvm_spawn(SLAVENAME, NULL, PvmTaskDefault, "", number_of_skiers, tids); for( i=0 ; i<nproc ; i++ ) { pvm_initsend(PvmDataDefault); pvm_pkint(&mytid, 1, 1); pvm_pkint(&number_of_skiers, 1, 1); for (j=0; j<nproc; j++) { pvm_pkint(&tids[j], 1, 1); } for (j=0; j<nproc; j++) { pvm_pkint(&skiers_weights[j], 1, 1); } pvm_pkint(&first_lift_capacity, 1, 1); pvm_pkint(&second_lift_capacity, 1, 1); pvm_send(tids[i], MSG_MSTR); } int current_skier_weight; for( i=0 ; i<nproc ; i++ ) { pvm_recv( -1, MSG_SLV ); pvm_upkint(&who, 1, 1 ); pvm_upkstr(slave_name ); pvm_upkint(¤t_skier_weight, 1, 1); printf("%d: weight %d\n",who, current_skier_weight); } int msgnum = 1; while (1) { pvm_recv(-1, MSG_DIAG); char diag_str[200]; pvm_upkstr(diag_str); printf("[%d] %s\n", msgnum, diag_str); msgnum++; } pvm_exit(); }
/* Function: main_loop_pvm() * Date: SRE, Wed Aug 19 13:59:54 1998 [St. Louis] * * Purpose: Given an HMM and parameters for synthesizing random * sequences; return a histogram of scores. * (PVM version) * * Args: hmm - an HMM to calibrate. * seed - random number seed * nsample - number of seqs to synthesize * lumpsize- # of seqs per slave exchange; controls granularity * lenmean - mean length of random sequence * lensd - std dev of random seq length * fixedlen- if nonzero, override lenmean, always this len * hist - RETURN: the score histogram * ret_max - RETURN: highest score seen in simulation * extrawatch - RETURN: total CPU time spend in slaves. * ret_nslaves- RETURN: number of PVM slaves run. * * Returns: (void) * hist is alloc'ed here, and must be free'd by caller. */ static void main_loop_pvm(struct plan7_s *hmm, int seed, int nsample, int lumpsize, float lenmean, float lensd, int fixedlen, struct histogram_s **ret_hist, float *ret_max, Stopwatch_t *extrawatch, int *ret_nslaves) { struct histogram_s *hist; int master_tid; int *slave_tid; int nslaves; int nsent; /* # of seqs we've asked for so far */ int ndone; /* # of seqs we've got results for so far */ int packet; /* # of seqs to have a slave do */ float max; int slaveidx; /* id of a slave */ float *sc; /* scores returned by a slave */ Stopwatch_t slavewatch; int i; StopwatchZero(extrawatch); hist = AllocHistogram(-200, 200, 100); max = -FLT_MAX; /* Initialize PVM */ if ((master_tid = pvm_mytid()) < 0) Die("pvmd not responding -- do you have PVM running?"); #if DEBUGLEVEL >= 1 pvm_catchout(stderr); /* catch output for debugging */ #endif PVMSpawnSlaves("hmmcalibrate-pvm", &slave_tid, &nslaves); /* Initialize the slaves */ pvm_initsend(PvmDataDefault); pvm_pkfloat(&lenmean, 1, 1); pvm_pkfloat(&lensd, 1, 1); pvm_pkint( &fixedlen, 1, 1); pvm_pkint( &Alphabet_type, 1, 1); pvm_pkint( &seed, 1, 1); if (! PVMPackHMM(hmm)) Die("Failed to pack the HMM"); pvm_mcast(slave_tid, nslaves, HMMPVM_INIT); SQD_DPRINTF1(("Initialized %d slaves\n", nslaves)); /* Confirm slaves' OK status. */ PVMConfirmSlaves(slave_tid, nslaves); SQD_DPRINTF1(("Slaves confirm that they're ok...\n")); /* Load the slaves */ nsent = ndone = 0; for (slaveidx = 0; slaveidx < nslaves; slaveidx++) { packet = (nsample - nsent > lumpsize ? lumpsize : nsample - nsent); pvm_initsend(PvmDataDefault); pvm_pkint(&packet, 1, 1); pvm_pkint(&slaveidx, 1, 1); pvm_send(slave_tid[slaveidx], HMMPVM_WORK); nsent += packet; } SQD_DPRINTF1(("Loaded %d slaves\n", nslaves)); /* Receive/send loop */ sc = MallocOrDie(sizeof(float) * lumpsize); while (nsent < nsample) { /* integrity check of slaves */ PVMCheckSlaves(slave_tid, nslaves); /* receive results */ SQD_DPRINTF2(("Waiting for results...\n")); pvm_recv(-1, HMMPVM_RESULTS); pvm_upkint(&slaveidx, 1, 1); pvm_upkint(&packet, 1, 1); pvm_upkfloat(sc, packet, 1); SQD_DPRINTF2(("Got results.\n")); ndone += packet; /* store results */ for (i = 0; i < packet; i++) { AddToHistogram(hist, sc[i]); if (sc[i] > max) max = sc[i]; } /* send new work */ packet = (nsample - nsent > lumpsize ? lumpsize : nsample - nsent); pvm_initsend(PvmDataDefault); pvm_pkint(&packet, 1, 1); pvm_pkint(&slaveidx, 1, 1); pvm_send(slave_tid[slaveidx], HMMPVM_WORK); SQD_DPRINTF2(("Told slave %d to do %d more seqs.\n", slaveidx, packet)); nsent += packet; } /* Wait for the last output to come in. */ while (ndone < nsample) { /* integrity check of slaves */ PVMCheckSlaves(slave_tid, nslaves); /* receive results */ SQD_DPRINTF1(("Waiting for final results...\n")); pvm_recv(-1, HMMPVM_RESULTS); pvm_upkint(&slaveidx, 1, 1); pvm_upkint(&packet, 1, 1); pvm_upkfloat(sc, packet, 1); SQD_DPRINTF2(("Got some final results.\n")); ndone += packet; /* store results */ for (i = 0; i < packet; i++) { AddToHistogram(hist, sc[i]); if (sc[i] > max) max = sc[i]; } } /* Shut down the slaves: send -1,-1,-1. */ pvm_initsend(PvmDataDefault); packet = -1; pvm_pkint(&packet, 1, 1); pvm_pkint(&packet, 1, 1); pvm_pkint(&packet, 1, 1); pvm_mcast(slave_tid, nslaves, HMMPVM_WORK); /* Collect stopwatch results; quit the VM; return. */ for (i = 0; i < nslaves; i++) { pvm_recv(-1, HMMPVM_RESULTS); pvm_upkint(&slaveidx, 1, 1); StopwatchPVMUnpack(&slavewatch); SQD_DPRINTF1(("Slave %d finished; says it used %.2f cpu, %.2f sys\n", slaveidx, slavewatch.user, slavewatch.sys)); StopwatchInclude(extrawatch, &slavewatch); } free(slave_tid); free(sc); pvm_exit(); *ret_hist = hist; *ret_max = max; *ret_nslaves = nslaves; return; }
int main (int argc, char* argv[]) { if(argc !=4) { printf("usage : ./craquage p r m\n"); return EXIT_FAILURE; } //Initialisation des variables int nb_esclaves = atoi(argv[1]); int* tids = (int*) calloc(nb_esclaves, sizeof(int)); int longueur_mdp = atoi(argv[2]); char* mdp = (char*) calloc(strlen(argv[3])+1, sizeof(char)); strcpy(mdp, argv[3]); //declaration de type de tres long entiers (avec bibliotheque GMP) mpz_t debut_sequence, pas_reel, pas, fin_exec; mpz_init(debut_sequence); mpz_init(pas_reel); mpz_init(pas); mpz_init(fin_exec); //recuperation du chemin de l executable char* chemin = getcwd(NULL, 1000); strcat(chemin, "/craquage_esclave"); //creation des arguments pour l esclave char *argv_esclave[3]; argv_esclave[2]=NULL; argv_esclave[0] = (char*) calloc(strlen(argv[2])+1, sizeof(char)); strcpy(argv_esclave[0],argv[2]); argv_esclave[1] = (char*) calloc(strlen(argv[3])+1, sizeof(char)); strcpy(argv_esclave[1],argv[3]); //printf("strlen %lu, %lu\n", (long unsigned)strlen(argv[2]),(long unsigned) strlen(argv[3])); //printf("nb_esclaves %d\n", nb_esclaves); int i; int trouve = 0; int fini = 0; int size; int nb_envoi = 0; int nb_pas = nb_esclaves*longueur_mdp; int nb_changement = 0; char* envoi_char; int bufid, info, bytes, type, source; char * solution; pvm_catchout(stderr); struct timeval tv1, tv2; gettimeofday(&tv1, NULL); pvm_spawn(chemin, argv_esclave, PvmTaskDefault,"", nb_esclaves, tids); //calcul du pas, fin_exec (= fin execution) mpz_set_ui(debut_sequence, 0); mpz_ui_pow_ui(fin_exec, 15, longueur_mdp+1); mpz_sub_ui(fin_exec, fin_exec, 15); mpz_cdiv_q_ui(fin_exec, fin_exec, 14); mpz_set(pas, fin_exec); mpz_cdiv_q_ui(pas, pas, nb_pas); if(mpz_cmp_ui(pas, 0)==0) { mpz_set_ui(pas,1); } //gmp_printf("fin_exec: %Zd\npas:%Zd\ndebut_sequence:%Zd\n",fin_exec, pas, debut_sequence); //boucle principale while(!trouve && fini!=nb_esclaves) { //Attente de reception de donnees d un esclave bufid = pvm_recv( -1, -1 ); info = pvm_bufinfo( bufid, &bytes, &type, &source ); if (info < 0) { printf("Erreur de reception : %d\n", info); exit(1); } //selon le tag du message, demande de donnees ou solution trouvee switch(type) { case(0)://mot de passe trouve solution = calloc(bytes, sizeof(char)); pvm_upkstr(solution); printf("\nLa solution est : %s\n\n", solution); trouve = 1; break; case(1)://esclave veut plus de donnees //prendre en compte la fin des donnees dans le calcul du pas if(nb_changement <= 2 && nb_envoi>=(3*nb_pas/4)) { mpz_cdiv_q_ui(pas, pas, 2); nb_envoi = 0; nb_pas/=2; nb_changement++; } //gmp_printf("fin_exec: %Zd pas:%Zd debut_sequence:%Zd\n",fin_exec, pas, debut_sequence); if(mpz_cmp(debut_sequence, fin_exec)< 0){ mpz_sub(pas_reel, fin_exec, debut_sequence); if(mpz_cmp(pas, pas_reel)<0) { mpz_set(pas_reel, pas); } //envoi des donnes a l esclave pvm_initsend(PvmDataDefault); size = gmp_asprintf(&envoi_char, "%Zd", debut_sequence); pvm_pkint(&size, 1, 1); pvm_pkbyte(envoi_char,size+1, 1); free(envoi_char); size = gmp_asprintf(&envoi_char, "%Zd", pas_reel); pvm_pkint(&size, 1, 1); pvm_pkbyte(envoi_char,size+1 , 1); free(envoi_char); pvm_send(source,0); if(mpz_cmp(pas_reel,pas)!=0) { mpz_set(debut_sequence,fin_exec); } else { mpz_add(debut_sequence, debut_sequence,pas); } nb_envoi++; } else{ fini++ ; printf("Pas de solution pour %d esclave(s)\n", fini); } break; default: break; } } // suppression des esclave for(i=0; i<nb_esclaves;i++) { info = pvm_kill(tids[i]); //printf("Suppression de l esclave %d: retour de pvm_kill: %d\n",i ,info); } pvm_exit(); gettimeofday(&tv2, NULL); printf("%d %ld\n",longueur_mdp,(tv2.tv_sec-tv1.tv_sec)*1000 + (tv2.tv_usec-tv1.tv_usec)/1000); mpz_clear(debut_sequence); mpz_clear(pas_reel); mpz_clear(pas); mpz_clear(fin_exec); free(tids); free(mdp); free(argv_esclave[0]); free(argv_esclave[1]); return EXIT_SUCCESS; }