void pvm_init(int argc, char *argv[]) { int mytid, mygid, ctid[MAXPROC]; int np, i; mytid = pvm_mytid(); if((argc != 2) && (argc != 1)) goto usage; if(argc == 1) np = 1; if(argc == 2) if((np = atoi(argv[1])) < 1) goto usage; if(np > MAXPROC) goto usage; mygid = pvm_joingroup(MPGROUP); if(np > 1) if (mygid == 0) i = pvm_spawn(argv[0], argv+1, 0, "", np-1, ctid); while(pvm_gsize(MPGROUP) < np) sleep(1); /* sync */ pvm_barrier(MPGROUP, np); printf("PVM initialization done!\n"); return; usage: fprintf(stderr, "usage: %s <nproc>\n", argv[0]); pvm_exit(); exit(-1); }
value Pvm_barrier(value groupname, value count) { int err = pvm_barrier(String_val(groupname), Int_val(count)); if (err < 0) TreatError(err); return; }
main() { int mytid, tid_master; char slave_name[NAMESIZE]; char current_pass[NAMESIZE]; char hash[NAMESIZE]; char current_hash[NAMESIZE]; char znalazl = 0 ; gethostname(slave_name, NAMESIZE+1); mytid = pvm_mytid(); int inst = pvm_joingroup(GRP) ; int tid = pvm_gettid(GRP ,inst); int info = pvm_barrier(GRP,SLAVENUM + 1); pvm_recv( -1, MSG_MSTR ); pvm_upkint(&tid_master, 1, 1 ); pvm_upkstr(hash); //pvm_upkint(&k, 1, 1 ); char poczatek = 97 + inst-1; char c1,c2,c3,c4,c5; for(c1 = poczatek ; c1 <= 'z' ; c1++) { for(c2 = 'a' ; c2 <= 'z' ; c2++) { for(c3 = 'a' ; c3 <= 'z' ; c3++) { for(c4 = 'a' ; c4 <= 'z' ; c4++) { for(c5 = 'a' ; c5 <= 'z' ; c5++) { current_hash[0] = c1 ; current_hash[1] = c2 ; current_hash[2] = c3 ; current_hash[3] = c4 ; current_hash[4] = c5 ; current_hash[6] = 0 ; current_hash = crypt(current_pass,"aa"); if(strcmp(current_hash,hash)==0) { znalazl = 1; break; } if(znalazl) break ; } if(znalazl) break ; } if(znalazl) break ; } if(znalazl) break ; } if(znalazl) break ; } pvm_initsend(PvmDataDefault); //pvm_pkint(&k, 1, 1); pvm_pkstr(slave_name); if(znalazl) { pvm_pkstr("znalazlem"); } else { pvm_pkstr("nie znalazlem"); } pvm_pkstr(slave_name); pvm_send(tid_master, MSG_SLV); pvm_exit(); }
/* Rank sort paralelizado*/ int main (int argc, char *argv[]){ int tam, i, j, rank, id, tag=100, processos, retorno; int *numeros, *rankeados, *tids, *ranks; char *grupo = "grupo"; srand(time(NULL)); printf("Digite numero de processos a serem disparados:\n"); scanf("%d", &processos); printf("Digite o tamanho do vetor a ser ordenado:\n"); scanf("%d", &tam ); retorno=(tam/processos); numeros=(int *) malloc (tam*sizeof(int)); rankeados=(int*) malloc (tam*sizeof(int)); tids=(int*) malloc (processos*sizeof(int)); ranks=(int*)malloc ((tam)*sizeof(int)); if((!numeros)||(!rankeados)||(!tids)||(!ranks)) { printf("Erro na alocacao de memoria\n"); return 1; } printf("Vetor não ordenado:\n"); printf("Números: "); for(i=0;i<tam;i++) { numeros[i]=rand() % 1001; printf(" %d ", numeros[i] ); } printf("\n"); pvm_initsend(PvmDataDefault); pvm_cachout(stdout); id = pvm_joingroup(grupo); if(id==0) { if(pvm_spawn("tarefas", NULL, PvmTaskDefault, "", processos-1,tids)!=(processos-1)) { printf("Erro: Não foi possível disparar os processos\n"); return 2; } else { for(i=0;i<tam;i++) { pvm_barrier(grupo, NPROCS); pvm_scatter(ranks,numeros,(tam/processos),PVM_INT,tag,grupo,0); rank=0; if((numeros[i]<numeros[j]) || ((numeros[i]==numeros[j])&&(j<i))) { rank++; } // rankeados[rank]=numeros[i]; printf("rank:%d ", rank); } // pvm_barrier(grupo, NPROCS); } } /* for(i=0;i<tam;i++) { rank=0; for(j=0;j<tam;j++) { if((numeros[i]<numeros[j]) || ((numeros[i]==numeros[j])&&(j<i))) { rank++; } } rankeados[rank]=numeros[i]; printf("rank:%d ", rank); } */ printf("Vetor ordenado:\n"); printf("Números: "); for(i=0;i<tam;i++) { printf(" %d ", rankeados[i] ); } printf("\n"); free(numeros); free(rankeados); return 0; }
main() { GHashTable* skiers_weights = g_hash_table_new(g_str_hash, g_str_equal); GQueue *waiting_req_q1 = g_queue_new(); GQueue *waiting_req_q2 = g_queue_new(); int first_lift_free, second_lift_free; int mytid, mstrtid, myind, number_of_skiers, *tids, first_lift_capacity, second_lift_capacity, i, phase, local_clock; time_t t; char slave_name[NAMESIZE]; char diag[200]; struct state_info info; mytid = pvm_mytid(); srand(time(NULL) + mytid); gethostname(slave_name, NAMESIZE); pvm_joingroup(GROUP); get_initial_values(&mstrtid, &number_of_skiers, &tids, &skiers_weights, &first_lift_capacity, &second_lift_capacity); info.mstrtid = mstrtid; first_lift_free = first_lift_capacity; second_lift_free = second_lift_capacity; int *my_weight_ptr = g_hash_table_lookup(skiers_weights, &mytid); int my_weight = *my_weight_ptr; int all_skiers_weight = 0; for(i=0; i<number_of_skiers; i++) { int *lookup = g_hash_table_lookup(skiers_weights, &tids[i]); all_skiers_weight += *lookup; } all_skiers_weight -= my_weight; sprintf(diag, "sum of all other skiers' weights = %d", all_skiers_weight); diag_msg(mstrtid, mytid, diag); i=0; while (tids[i] != mytid) { i++; } pvm_initsend(PvmDataDefault); pvm_pkint(&mytid, 1, 1); pvm_pkstr(slave_name); int *wat_to_send = g_hash_table_lookup(skiers_weights, &mytid); pvm_pkint(wat_to_send, 1, 1); pvm_send(mstrtid, MSG_SLV); // bariera local_clock = 0; pvm_barrier(GROUP, number_of_skiers); int time_to_wait; // main loop diag_msg(mstrtid, mytid, "entering main loop"); while (1) { int can_enter_lift = 0; int chosen_lift = -1; int accepts_received = 0; int my_request_timestamp = -1; info.accepts_received = &accepts_received; info.my_request_timestamp = &my_request_timestamp; phase = PHASE_DOWNHILL; struct timeval timeout; random_timeout(&timeout, 3, 10); struct timeval start_time; gettimeofday(&start_time, NULL); struct timeval elapsed; elapsed.tv_sec = 0; elapsed.tv_usec = 0; sprintf(diag, "entered %s, time=%zu.%zu", stringify(phase), timeout.tv_sec, timeout.tv_usec); diag_msg(mstrtid, mytid, diag); while (1) { int bufid = pvm_trecv(-1, -1, &timeout); if (bufid) { // diag_msg(mstrtid, mytid, "Got message in PHASE_DOWNHILL"); struct msg incoming_msg; unpack(&incoming_msg); int *sender_weight = g_hash_table_lookup(skiers_weights, &incoming_msg.sender_tid); // handle the message accordingly prepare_info(&info, mytid, &local_clock, phase, &chosen_lift, &can_enter_lift, &first_lift_free, &second_lift_free, incoming_msg, waiting_req_q1, waiting_req_q2); info.skiers_weights = skiers_weights; handle_message(incoming_msg, info); } // break the loop if time elapsed is more than the timeout, else wait for another message struct timeval current_time; gettimeofday(¤t_time, NULL); timeval_subtract(&elapsed, ¤t_time, &start_time); if (elapsed.tv_sec * 1000000 + elapsed.tv_usec >= timeout.tv_sec * 1000000 + timeout.tv_usec) break; else timeval_subtract(&timeout, &timeout, &elapsed); } // want to go up diag_msg(mstrtid, mytid, ""); phase = PHASE_WAIT_REQUEST; // choose the lift (1 or 2) // determine if we can fit on the lift (based on our knowledge from the accepts we had sent) sprintf(diag, "entered PHASE_WAIT_REQUEST, weight=%d, LIFT_1=%d, LIFT_2=%d",my_weight,first_lift_free,second_lift_free); diag_msg(mstrtid, mytid, diag); if (my_weight > first_lift_free && my_weight > second_lift_free) { // no lift for us // wait for RELEASE messages until we can fit in the lift // meanwhile: handle all incoming messages, responding accordingly (ALWAYS respond with accepts) while (1) { sprintf(diag, "no space in lifts, weight=%d, LIFT_1=%d, LIFT_2=%d",my_weight,first_lift_free,second_lift_free); diag_msg(mstrtid, mytid, diag); int bufid = pvm_recv(-1, -1); struct msg incoming_msg; unpack(&incoming_msg); prepare_info(&info, mytid, &local_clock, phase, &chosen_lift, &can_enter_lift, &first_lift_free, &second_lift_free, incoming_msg, waiting_req_q1, waiting_req_q2); info.skiers_weights = skiers_weights; handle_message(incoming_msg, info); // now check if we should break the loop and go to the next phase! if (chosen_lift == LIFT_1 || chosen_lift == LIFT_2) { sprintf(diag, "weight=%d, LIFT_1=%d, LIFT_2=%d, choosing %s", my_weight, first_lift_free, second_lift_free, stringify(chosen_lift)); diag_msg(mstrtid, mytid, diag); // broadcast a request with our chosen lift and go to (*) bcast_request_msg(info); break; } } } else { // if there is a fitting lift if (my_weight <= first_lift_free && my_weight > second_lift_free) { // can only go to first lift chosen_lift = LIFT_1; } else if (my_weight > first_lift_free && my_weight <= second_lift_free) { // can only go to second lift chosen_lift = LIFT_2; } else { // can go to either lift - worst fit if (first_lift_free > second_lift_free) chosen_lift = LIFT_1; else if (first_lift_free < second_lift_free) chosen_lift = LIFT_2; else // equal chosen_lift = (rand() % 2 == 1) ? LIFT_1 : LIFT_2; } info.my_lift_number = &chosen_lift; info.mytid = mytid; info.local_clock = &local_clock; info.skiers_weights = skiers_weights; // broadcast a request with our chosen lift and go to (*) bcast_request_msg(info); // sprintf(diag,"So I am here just before broadcasting REQUEST and I choose lift %d and in info %d",chosen_lift, *info.my_lift_number); sprintf(diag, "weight=%d, LIFT_1=%d, LIFT_2=%d, choosing %s", my_weight, first_lift_free, second_lift_free, stringify(chosen_lift)); diag_msg(mstrtid, mytid, diag); } sprintf(diag, "*** chosen_lift=%s", stringify(chosen_lift)); // waiting for accepts (*) // wait for enough accepts or just as much as required to be sure that we can get in // meanwhile: handle all incoming messages, responding accordingly (not send back accept only to those with worse priority that can't fit together with us) // diag_msg(mstrtid, mytid, "entering PHASE_WAIT_ACCEPTS"); phase = PHASE_WAIT_ACCEPTS; int pending = all_skiers_weight; info.pending_accepts_sum = &pending; sprintf(diag, "entered PHASE_WAIT_ACCEPTS, lift=%s", stringify(chosen_lift)); diag_msg(mstrtid, mytid, diag); while (1) { int bufid = pvm_recv(-1, -1); struct msg incoming_msg; unpack(&incoming_msg); prepare_info(&info, mytid, &local_clock, phase, &chosen_lift, &can_enter_lift, &first_lift_free, &second_lift_free, incoming_msg, waiting_req_q1, waiting_req_q2); handle_message(incoming_msg, info); // check if there are enough amount of acceptes - can break the loop and go to the critical section if (can_enter_lift) { break; } } // random waiting up to the hill - critical section phase = PHASE_CRITICAL; random_timeout(&timeout, 3, 10); gettimeofday(&start_time, NULL); elapsed.tv_sec = 0; elapsed.tv_usec = 0; sprintf(diag, "entered PHASE_CRITICAL, lift=%s, time=%zu.%zu", stringify(chosen_lift), timeout.tv_sec, timeout.tv_usec); diag_msg(mstrtid, mytid, diag); while (1) { int bufid = pvm_trecv(-1, -1, &timeout); if (bufid) { // unpack the received message struct msg incoming_msg; // int msgtag = -1, sender_tid = -1, lift_number = -1, timestamp = -1; unpack(&incoming_msg); // handle the message accordingly prepare_info(&info, mytid, &local_clock, phase, &chosen_lift, &can_enter_lift, &first_lift_free, &second_lift_free, incoming_msg, waiting_req_q1, waiting_req_q2); handle_message(incoming_msg, info); } // break the loop if time elapsed is more than the timeout, else wait for another message struct timeval current_time; gettimeofday(¤t_time, NULL); timeval_subtract(&elapsed, ¤t_time, &start_time); if (elapsed.tv_sec * 1000000 + elapsed.tv_usec >= timeout.tv_sec * 1000000 + timeout.tv_usec) break; else timeval_subtract(&timeout, &timeout, &elapsed); } // release - broadcast the RELEASE message to all others info.mytid = mytid; info.local_clock = &local_clock; info.my_lift_number = &chosen_lift; bcast_release_msg(info); // send all the requests stored in the queue (mcast automatically clears it) mcast_accept_msg(info); } pvm_exit(); }
int main(int argc, char **argv) { unsigned long i = 1; char Err[BUFSIZ]; #ifdef PVM unsigned int j = 0L; #endif /* There is no signal handling under parix. */ if(((int)signal(SIGINT, (void (*)(int)) myInterrupt) == -1) || ((int)signal(SIGFPE, (void (*)(int)) Arithmetic) == -1)) panic(A_FATAL, "child-main", "signal call went wrong :%s : %d", __FILE__, __LINE__); strcpy(hlp, argv[0]); program_name = hlp; initDef(&eps); /* initialize structure */ ipOptPrc(&eps, argc, argv); /* process options. */ #ifdef PVM eps.tid = pvm_mytid(); /* tid of process. */ strcpy(eps.Group, eps.Suffix); /* Create Group name. */ if((eps.inst = pvm_joingroup(eps.Group)) < 0) panic(A_FATAL, "child-main", "pvm grouping for group %s failed : %s : %d\n", eps.Group, __FILE__, __LINE__); printf("Slave with tid %d and inst %d started\n", eps.tid, eps.inst); /* To get more diversity, each Population gets other external parameters.*/ if(eps.SigDim > 0) eps.SigStart += (0.1 * (double) eps.inst); /* if(eps.SigDim > 0) { if(eps.SigDim == 1) eps.TauOne *= (0.1 * (double) eps.inst); else eps.TauLcl *= (0.1 * (double) eps.inst); } if(eps.PDim > 0) { if(eps.PDim == 1) { eps.GamOne *= (double) eps.inst; eps.PStart *= (double) eps.inst; } else eps.GamLcl *= (double) eps.inst; } #ifdef DEBUG fprintf(stderr, "%d:Tau = %g, Gamma = %g\n", eps.inst, eps.TauLcl, eps.GamOne); #endif */ sprintf(&hlp[strlen(hlp)], ".%d", eps.tid); if(pvm_barrier(eps.Group, eps.Tasks+1) < 0) panic(A_WARN, "child-main", "pvm barrier error : %s : %d\n", __FILE__, __LINE__); printf("\n%s as instance %d activ.\n", program_name, eps.inst); #endif /* PVM */ if(ipOptChk(&eps, Err)) { /* check parameter consistency */ fprintf(stderr,"%s",Err); /* print warning message */ panic(A_FATAL, "ipOptPrc", "Parameter inconsistency :\n%s\n%s : %d", Err, __FILE__, __LINE__); } #ifndef PVM printf("\n%s activ.\n", program_name); #endif #ifdef PVM if(NULL == (MigrationBuffer = (individual_tPtr *) calloc(eps.Neighbours, sizeof(individual_tPtr)))) panic(E_FATAL, "main-child", "not enough memory : %s : %d", __FILE__, __LINE__); for(j = 0; j < eps.Neighbours; j++) MigrationBuffer[j]= inNewIndividual(eps.XDim, eps.DDim, eps.SigDim, eps.AlpDim, eps.PDim); #endif initSuffix(&eps); /* initialize suffix, here string from ES.in*/ parsFormatString(&eps); /* pars format string */ pop = poNewPopulation(&eps); for (i = 1; i <= eps.TotExp; i++) { /* experiment loop */ ExpNbr = i; initPop(&eps, pop, i); #ifdef PVM /* Initialization of MigrationBuffer. */ for(j = 0; j < eps.Neighbours; j++) { if(j < pop->mu) inCopyIndividual(poGetIndividual(j+1, pop), MigrationBuffer[j]); else inCopyIndividual(poGetIndividual(pop->mu, pop), MigrationBuffer[j]); } #endif dataParLog(&eps); /* dump parameters */ fioTimeOut(&eps); /* get start time */ PopulationEVAL(&eps, pop, "parents"); dataCol(&eps, pop, i); if (eps.GfxFlg) gnInitGnuPipe(&eps); #ifdef PVM send_Best(&eps, pop); #endif while (!termExp(&eps)) { /* ES main loop */ eps.GenCnt++; PopulationCREATE(&eps, pop); /* recombination and mutation including the concept of letal mutations. */ PopulationEVAL(&eps, pop, "offspring"); PopulationSELECT(&eps, pop); dataCol(&eps, pop, i); if(eps.GfxFlg && (eps.GenCnt % eps.GfxIvl == 0)) gnPlotGnuPipe(&eps); /* plot data */ #ifdef PVM send_Best(&eps, pop); if(eps.GenCnt % eps.IsolationTime == 0) { send_Neighbours(&eps, pop); if(eps.Communication == COMMUNICATION_SYNCHRON) recv_sync(&eps, MigrationBuffer); } if(eps.Communication == COMMUNICATION_ASYNCHRON) recv_async(&eps, MigrationBuffer); for(j = 0; j < eps.Neighbours && j < pop->mu; j++) inCopyIndividual(MigrationBuffer[j], poGetIndividual(j+1, pop)); #endif } /* end ES main loop */ dataBst(&eps, pop, i); /* dump best ind. */ fioTimeOut(&eps); /* get end time */ } /* end experiment loop */ if(eps.GfxFlg) gnQuitGnuPipe(&eps); /* stop plotting */ reportExperiments(&eps); pop = poDeletePopulation(pop); #ifdef PVM if(pvm_initsend(PvmDataRaw) < 0) panic(A_FATAL,"child-main", "initsend for group %d failed : %s : %d", eps.inst, __FILE__, __LINE__); if((ptid = pvm_parent()) == PvmNoParent) panic(A_FATAL,"child-main", "pvm_parent for group %d failed : %s : %d", eps.inst, __FILE__, __LINE__); if(pvm_pkint(&(eps.tid), 1, 1) < 0) panic(A_FATAL,"child-main", "packing of tid failed : %s : %d", __FILE__, __LINE__); if(pvm_send(ptid, MSGTAG_END) < 0) panic(A_WARN, "child-main", "sending end message to master failed : %s : %d", __FILE__, __LINE__); pvm_barrier(eps.Group, eps.Tasks+1); pvm_lvgroup(eps.Group); for(j = 0; j < eps.Neighbours; j++) inDeleteIndividual(MigrationBuffer[j]); free(MigrationBuffer); #endif utGlobalTermination(&eps); printf("\n%s 1.0 terminated.\n", program_name); #ifdef PVM pvm_exit(); #endif exit(0); }
void myInterrupt(int dummy) { #ifdef PVM unsigned int j = 0; #endif #ifndef PVM char choice = 'c'; unsigned long trials = 0; bool flag = TRUE; printf("\nActual optimization situation :\n"); printf("\n-------------------------------\n\n"); printf("\nGenCnt\t= %ld", eps.GenCnt); printf("\nTrlCnt\t= %ld\n", eps.TrlCnt); if((eps.XDim != 0) || (eps.DDim != 0)) { printf("AllBest\t= %g\n",eps.AllBst); printf("CurBst\t= %g\n",eps.CurBst); printf("CurAvg\t= %g\n",eps.CurAvg); printf("CurWst\t= %g\n\n",eps.CurWst); } if(eps.SigDim != 0) { printf("SigMin\t= %g\n",eps.SigMin); printf("SigAvg\t= %g\n",eps.SigAvg); printf("SigMax\t= %g\n\n",eps.SigMax); } if(eps.AlpDim != 0) { printf("CorMin\t= %g\n",eps.CorMin); printf("CorAvg\t= %g\n",eps.CorAvg); printf("CorMax\t= %g\n\n",eps.CorMax); } if(eps.PDim != 0) { printf("PMin\t= %g\n",eps.PMin); printf("PAvg\t= %g\n",eps.PAvg); printf("PMax\t= %g\n\n",eps.PMax); } printf("\nTotTrl\t= %ld",eps.TotTrl); while(flag) { printf("\nMore trials ? (0 / new number of trials)\n"); scanf("%ld", &trials); if(trials != 0) { if(trials <= eps.TrlCnt) printf("\nNumber of trials below TrlCnt\n"); else { eps.TotTrl = trials; printf("\nTotTrl set to %ld\n",eps.TotTrl); flag = FALSE; } } else flag = FALSE; } while(TRUE) { printf("\nTermination/Continuation ? (t/c)\n"); scanf("%c", &choice); if((choice == 't') || (choice == 'c')) break; } if(choice == 't') { #endif fioTimeOut(&eps); dataBst(&eps, pop, ExpNbr); if(eps.GfxFlg) gnQuitGnuPipe(&eps); if(eps.ObjFlg) fclose(eps.FpObj); if(eps.DisFlg) fclose(eps.FpDis); if(eps.SigFlg) fclose(eps.FpSig); if(eps.AlpFlg) fclose(eps.FpAlp); if(eps.RatFlg) fclose(eps.FpRat); fclose(eps.FpOut); fclose(eps.FpLog); poDeletePopulation(pop); #ifdef PVM if(pvm_initsend(PvmDataRaw) < 0) panic(A_FATAL,"child-main", "initsend for group %d failed : %s : %d", eps.inst, __FILE__, __LINE__); if((ptid = pvm_parent()) == PvmNoParent) panic(A_FATAL,"child-main", "pvm_parent for group %d failed : %s : %d", eps.inst, __FILE__, __LINE__); if(pvm_pkint(&(eps.tid), 1, 1) < 0) panic(A_FATAL,"child-main", "packing of tid failed : %s : %d", __FILE__, __LINE__); if(pvm_send(ptid, MSGTAG_END) < 0) panic(A_WARN, "child-main", "sending end message to master failed : %s : %d", __FILE__, __LINE__); pvm_barrier(eps.Group, eps.Tasks+1); pvm_lvgroup(eps.Group); for(j = 0; j < eps.Neighbours; j++) inDeleteIndividual(MigrationBuffer[j]); free(MigrationBuffer); #endif utGlobalTermination(&eps); printf("\n%s 1.0 terminated.\n", program_name); #ifdef PVM pvm_exit(); #endif exit(0); #ifndef PVM } printf("\n%s 1.0 continued.\n", program_name); return; #endif }