-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbigdata_heuri.cpp
More file actions
4234 lines (3413 loc) · 138 KB
/
bigdata_heuri.cpp
File metadata and controls
4234 lines (3413 loc) · 138 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "bigdata_heuri.h"
#define INFINITY_COST 99999999
//Inicializador
BDHeuri::BDHeuri(Data* data, int flag)
{
// flag que indica qual metodo vai ser executado (-1: Guloso Puro 0: Guloso com Rexec 1:Multi-Start 2: Grasp)
FLAG=flag;
// dimensoes
_n = data->n;
_m = data->m;
_d = data->d;
_t = data->t;
_t_max = _t;
// numero de acoes maximas em uma mesma maquina
if (5*(_n + _d) < _t)
_a_max = 5*(_n + _d);
else
_a_max = _t;
// tamanho do vetor da LC
max_sizeLC = 1000;
// aloca
TL = new struct bd_registro * [_a_max];
for ( int i = 0; i < _a_max; i++ ) {
TL[i] = new struct bd_registro [_m];
}
LC = new struct bd_movimento [max_sizeLC];
for ( int i = 0; i < max_sizeLC; i++ ) {
LC[i].r = new int [data->max_in];
LC[i].w = new int [data->max_out];
}
SITE_MIN_MAQ = new struct bd_site [data->num_site];
SITE_MIN_MAQ2 = new struct bd_site [data->num_site];
MS_M = new int [_m];
MS_M_t1 = new int [_m];
MS_M_t2 = new int [_m];
QTD_M = new int [_m];
D_WRITE = new int [data->max_out];
D_REEXC = new int [data->max_in];
D_TASK = new int [data->max_in];
FILE_MD = new int * [_m];
for ( int i = 0; i < _m; i++ ) {
FILE_MD[i] = new int [_d];
}
TASK_MAQ = new int * [_n];
for ( int i = 0; i < _n; i++ ) {
TASK_MAQ[i] = new int [_m];
}
FILE_IN = new int [_n];
TASK = new int [_n];
TASK_T_MIN = new int [_n];
TASK_RXC = new int [_n];
B_READ = new int * [_m];
for ( int i = 0; i < _m; i++ ) {
B_READ[i] = new int [_d];
}
B_WRITE = new int * [_m];
for ( int i = 0; i < _m; i++ ) {
B_WRITE[i] = new int [_d];
}
CAP_M = new double [_m];
CAP_M_t1 = new double [_m];
CAP_M_t2 = new double [_m];
DADO_T = new int [_d];
FILE_RXC = new int [_d];
/* ------- bl ----------- */
max_elem_bl = ((_n + _d)*_m);
max_order_bl = (_n * _m);
ELE = new struct bd_elem [max_elem_bl];
for (int i = 0; i < max_elem_bl; i++)
{
ELE[i].es = new int [data->max_out];
ELE[i].le = new int [data->max_in];
ELE[i].les = new int [data->max_in];
ELE[i].vms = new int [data->max_in];
ELE[i].pred = new int [max_order_bl];
ELE[i].suc = new int [max_order_bl];
}
ORDER_T = new struct bd_ord [max_order_bl];
altura_i_order = new int [max_order_bl];
altura_order = new int [max_order_bl];
/* ----------------------- */
}
BDHeuri::~BDHeuri()
{
//desaloca
for ( int i = 0; i < _a_max; i++ )
delete [] TL[i];
delete [] TL;
for ( int i = 0; i < max_sizeLC; i++ )
{
delete [] LC[i].w;
delete [] LC[i].r;
}
delete [] LC;
delete [] SITE_MIN_MAQ;
delete [] SITE_MIN_MAQ2;
delete [] MS_M;
delete [] MS_M_t1;
delete [] MS_M_t2;
delete [] QTD_M;
delete [] D_WRITE;
delete [] D_REEXC;
delete [] D_TASK;
for ( int i = 0; i < _m; i++ )
delete [] FILE_MD[i];
delete [] FILE_MD;
for ( int i = 0; i < _n; i++ )
delete [] TASK_MAQ[i];
delete [] TASK_MAQ;
delete [] FILE_IN;
delete [] TASK;
delete [] TASK_T_MIN;
delete [] TASK_RXC;
for ( int i = 0; i < _m; i++ )
delete [] B_READ[i];
delete [] B_READ;
for ( int i = 0; i < _m; i++ )
delete [] B_WRITE[i];
delete [] B_WRITE;
delete [] CAP_M;
delete [] CAP_M_t1;
delete [] CAP_M_t2;
delete [] DADO_T;
delete [] FILE_RXC;
/* ------- bl ----------- */
delete [] ELE;
for (int i = 0; i < max_elem_bl; i++)
{
delete [] ELE[i].es;
delete [] ELE[i].le;
delete [] ELE[i].les;
delete [] ELE[i].vms;
delete [] ELE[i].pred;
delete [] ELE[i].suc;
}
delete [] ORDER_T;
delete [] altura_i_order;
delete [] altura_order;
/* ----------------------- */
}
//FILE_MD
int BDHeuri::get_FILE_MD(int m, int d)
{
return FILE_MD[m][d];
}
void BDHeuri::put_FILE_MD(int m, int d, int val)
{
FILE_MD[m][d] = val;
}
//B_READ
int BDHeuri::get_B_READ(int m, int d)
{
return B_READ[m][d];
}
void BDHeuri::put_B_READ(int m, int d, int val)
{
B_READ[m][d] = val;
}
//B_WRITE
int BDHeuri::get_B_WRITE(int m, int d)
{
return B_WRITE[m][d];
}
void BDHeuri::put_B_WRITE(int m, int d, int val)
{
B_WRITE[m][d] = val;
}
// funcao objetivo
double BDHeuri::fobj(Data* data, int* makespam, double* custof, double* ms_part, double* cs_part)
{
int mksp=-1;
double cost=0;
double obj=0;
for ( int m = 0; m < _m; m++ )
{
if (MS_M[m] > mksp)
mksp = MS_M[m];
cost += MS_M[m] * data->m_cost[m];
}
* makespam = mksp;
* custof = cost;
* ms_part = ((data->alfa_t * mksp)/ data->max_time_wkf /*data->t*/);
* cs_part = ((data->alfa_b * cost) / data->max_cost_wkf /*data->c_max*/);
obj = (* ms_part) + (* cs_part);
return obj;
}
// atualiza vetor de dados de entrada que vao ser gerados
// vet --> D_REEXC
// Soh gera o dado se ele nao tiver na maquina de armazenamento do site
bool BDHeuri::atualiza_dados_gerados(Data* data, int* vet, int task, int i_pai_t, int maq)
{
int t_dad, dw_dad;
bool vaigerar=false;
int Maq_site, Maq_stg;
// Site da Maquina que esta sendo executada a tarefa E Maquina de armazenamento do site da maquina que esta executando a tarefa
Maq_site = data->m_site[maq];
Maq_stg = data->vm_st_site[Maq_site];
t_dad = data->t_in[task][i_pai_t];
for(int dw_dad, d=0; d < data->d_out_tam[t_dad]; d++)
{
dw_dad = data->d_out[t_dad][d];
if (get_FILE_MD(Maq_stg,dw_dad) == -1)
{
for(int d2=0; d2 < data->d_in_tam[task]; d2++)
if (data->d_in[task][d2] == dw_dad)
{
vet[d2] = 1;
vaigerar = true;
break;
}
}
}
return vaigerar;
}
// Dada um configuracao 01 de D_REEXC (que determina que dados de entrada seram lidos e quais gerados)
// determinar qual o novo periodo minimo que a tarefa pode comecar as acoes
int BDHeuri::shift_read(Data* data, int tam_D, int ti)
{
int min_time=0;
int Depu=0;
if (Depu) cout<<" ---- shift_read---- de t"<<ti<<endl;
// percorre os dados de entrada a serem lidos (nao re-executados)
for(int dl, d=0; d < data->d_in_tam[ti]; d++)
{
// dados lidos
if (D_REEXC[d]==0)
{
dl = data->d_in[ti][d]; // dado a ser lido
if (DADO_T[dl] > min_time)
{
min_time = DADO_T[dl];
}
if (Depu) cout<<" dado d"<<dl<<" aparece em tempo "<<DADO_T[dl]<<endl;
if (DADO_T[dl] == -1) { cout<<"**ERRO ERRO ERRO** leu dado "<<dl<<" que nao existia"<<endl; abort();}
}
}
return min_time;
}
// inicializa vetor de inteiros para enumerar as leituras/reexecucao
bool BDHeuri::ini_intvet_r(int* vet1, int* vet2, int tamV)
{
for(int i=0; i < tamV; i++)
{
vet1[i]=0;
vet2[i]=0;
}
return true;
}
// inicializa vetor de inteiros para enumerar as escritas
// encontra a primeira maquina do site inicial para tentar escrever
// vet -> D_WRITE
bool BDHeuri::ini_intvet_w(int* vet, int tamV, Data* data)
{
SITE_WRITE = 0;
for ( int j = 0; j < tamV; j++ )
vet[j]=data->vm_st_site[SITE_WRITE];
return true;
}
// incrementa vetor de inteiros (sequencia de valores) para enumerar a leitura/reexecucao
// retorna FALSE se acabou de iterar (tamanho eh tamV ou MaxTam_D)
// Ex: 0 0 0 0 -> 0 0 0 1 -> 0 0 1 0 -> 0 0 1 1 -> 0 1 0 0 -> 0 1 0 1 -> ...
// vet1 ---> D_TASK
// vet2 ---> D_REEXC
bool BDHeuri::inc_intvet_r(int* vet1, int* vet2, int i, int tamV1, int tamV2, int task, int maq, Data* data)
{
bool resp=false;
bool vaigerar;
if ((i < tamV1) and (i < MaxTam_D))
{
resp=inc_intvet_r(vet1, vet2, i+1, tamV1, tamV2, task, maq, data);
if (not resp)
{
if (vet1[i] == 1)
{
vet1[i]=0;
}
else
{
vet1[i]++;
// -------------------------- Atualiza D_REEXC -------------------------
for(int d=0; d < tamV2; d++)
vet2[d]=0;
for(int t=0; t < tamV1; t++)
if (vet1[t] == 1)
{
vaigerar = atualiza_dados_gerados(data, vet2, task, t, maq);
// a task a ser re-executada nao precisa, pois a propria maquina ja tem todos os arquivos que esta tarefa gera
if (vaigerar == false)
{
//cout<<"nao precisa D_TASK = "; for(int d=0; d < data->t_in_tam[task]; d++) if (D_TASK[d]==1) { cout<<data->t_in[task][d]<<", "; } cout<<". pois task "<<data->t_in[task][t]<<" eh desnecessaria "<<endl;
//cout<<"nao precisa D_TASK = "; for(int d=0; d < data->t_in_tam[task]; d++) { cout<<D_TASK[d]<<", "; } cout<<". pois task na pos "<<t<<" eh desnecessaria "<<endl;
// zera o vetor de tarefas da posicao atual ate o final, inutilizando esta configuracao
for (int j=t; (j<tamV1) and (j<MaxTam_D); j++)
vet1[j]=0;
return false;
}
}
// ----------------------------------------------------------------------
return true;
}// else
}//if (not resp)
}//if ((i < tamV1) and (i < MaxTam_D))
return resp;
}
// incrementa vetor de inteiros (sequencia de valores) para enumerar a leitura/reexecucao
// Ex 4 maq: 0 0 0 0 -> 0 0 0 1 -> 0 0 0 2 -> 0 0 0 3 -> 0 0 1 0 -> 0 0 1 1 -> 0 0 1 2 -> ...
// retorna FALSE se acabou de iterar (tamanho eh tamV ou MaxTam_D)
bool BDHeuri::inc_intvet_w(int* vet, int i, int tamV, int maxval)
{
bool resp=false;
if ((i < tamV) and (i < MaxTam_D))
{
resp=inc_intvet_w(vet, i+1, tamV, maxval);
if (not resp)
{
if (vet[i] == maxval)
vet[i]=0;
else
{
vet[i]++;
return true;
}
}
}
return resp;
}
// incrementa vetor de inteiros (sequencia de valores), mas nao enumera todas as combinacoes,
// agora apenas faz cada posicao isoladamente ser alterada
// Ex: 0 0 0 0 0 -> 1 0 0 0 0 -> 0 1 0 0 0 -> 0 0 1 0 0 -> ... -> 0 0 0 0 1 -> 1 1 1 1 1
// retorna FALSE se acabou de iterar (tamanho eh tamV ou MaxTam_D)
bool BDHeuri::inc_intvet_redux_r(int* vet1, int* vet2, int i, int tamV1, int tamV2, int task, int maq, Data* data)
{
int t_dad;
// condicao de parada 1: nao tem tarefas para serem re-executadas
if (tamV1 == 0)
return false;
// condicao de parada 2: ultimo elemento -> 1 1 1 1 1
if (tamV1 == 1)
{
if (vet1[0]==1)
return false;
}
else
if (tamV1 >= 2)
if (vet1[0]==1 and vet1[1]==1)
return false;
//proximo a ser incrementado
while ((i < tamV1) and (i < MaxTam_D))
{
if (vet1[i] != 0)
{
vet1[i] = 0;
if ((i+1 < tamV1) and (i+1 < MaxTam_D))
{
vet1[i+1] = 1;
// -------------------------- Atualiza D_REEXC -------------------------
for(int d=0; d < tamV2; d++)
vet2[d]=0;
atualiza_dados_gerados(data, vet2, task, i+1, maq);
// ----------------------------------------------------------------------
return true;
}
// ultimo elemento alcancou 1
else
{
// ultima possibilidade, re-executa todo mundo
// 0 0 0 0 1 -> 1 1 1 1 1
for (int j=0; (j<tamV1) and (j<MaxTam_D); j++)
vet1[j]=1;
// -------------------------- Atualiza D_REEXC -------------------------
for(int d=0; d < tamV2; d++)
vet2[d]=0;
for(int t=0; t < tamV1; t++)
atualiza_dados_gerados(data, vet2, task, t, maq);
// ----------------------------------------------------------------------
return true;
}
}
i++;
}// while ((i < tamV1) and (i < MaxTam_D))
// vetor nulo inicial, vai para o segundo elemento
vet1[0]++;
// -------------------------- Atualiza D_REEXC -------------------------
for(int d=0; d < tamV2; d++)
vet2[d]=0;
atualiza_dados_gerados(data, vet2, task, 0, maq);
// ----------------------------------------------------------------------
return true;
}
// incrementa vetor de inteiros (sequencia de valores), mas nao enumera todas as combinacoes,
// joga todas as saidas numa maquina soh por vez
// Ex: 0 0 0 0 -> 1 1 1 1 -> 2 2 2 2 -> ...
// retorna FALSE se acabou de iterar (tamanho eh tamV ou MaxTam_D)
// vet -> D_WRITE
bool BDHeuri::inc_intvet_redux_w(int* vet, int tamV, int maxval, Data* data)
{
SITE_WRITE++;
if (SITE_WRITE < data->num_site)
{
for ( int j = 0; j < tamV; j++ )
vet[j]=data->vm_st_site[SITE_WRITE];
return true;
}
else
return false;
}
// achou posicao inviavel no vetor de enumeracao
// incrementa aquela posicao e zera as da frente (-1 pois ainda vai ser incrementado)
bool BDHeuri::pos_inv_intvet_w(int* vet, int i, int tamV, int maxval)
{
// pois na proxima iteracao se os posteriores tiverem no valor maximo, o elemento da posicao i vai ser iterado
for (int j=i+1; (j<tamV) and (j<MaxTam_D); j++)
vet[j]=maxval;
return true;
}
// na iteracao reduzida nao faz nada em caso de inviabilidade na posicao
bool BDHeuri::pos_inv_intvet_redux_w(int* vet, int i, int tamV, int maxval)
{
return true;
}
// inicializa estruturas da heuristica
void BDHeuri::inicializa_estruturas(Data* data)
{
for ( int m = 0; m < _m; m++ )
{
MS_M[m] = 0;
CAP_M[m] = 0;
QTD_M[m] = 0;
}
for ( int m = 0; m < _m; m++ )
for ( int d = 0; d < _d; d++ )
put_FILE_MD(m,d,-1);
for ( int maq, ind, d = 0; d < _d; d++ )
{
for (int ind=0; ind < data->d_st_or_tam[d]; ind++)
{
maq = data->d_st_or[d][ind];
put_FILE_MD(maq,d,0);
CAP_M[maq] += data->d_size[d];
}
}
MS=0;
for ( int i = 0; i < _n; i++ )
TASK_T_MIN[i] = -1;
for ( int d = 0; d < _d; d++ )
DADO_T[d] = -1;
for(int d=0; d < _d; d++)
if (data->d_st_or_tam[d] > 0)
{
//dado estatico
for ( int i = 0; i < _n; i++ )
for(int d2=0; d2 < data->d_in_tam[i]; d2++)
if (data->d_in[i][d2] == d)
TASK_T_MIN[i] = 0;
DADO_T[d]=0;
}
// para toda tarefa obrigatoria (na heuristica todas sao obrigatorias)
for ( int i = 0; i < _n; i++ )
{
FILE_IN[i] = data->d_in_dd_tam[i];
TASK[i] = 0;
}
NTASK=0;
for ( int m = 0; m < _m; m++ )
for ( int d = 0; d < _d; d++ )
{
put_B_READ(m,d,-1);
put_B_WRITE(m,d,-1);
}
int b_t_djp;
for ( int m = 0; m < _m; m++ )
for(int ind,d=0; d < _d; d++)
{
b_t_djp = INFINITY_COST;
for (int ind=0; ind < data->d_st_or_tam[d]; ind++)
{
if (data->t_djp(d,m,data->d_st_or[d][ind]) < b_t_djp)
{
b_t_djp = data->t_djp(d,m,data->d_st_or[d][ind]);
put_B_READ(m,d,data->d_st_or[d][ind]);
}
}
}
for ( int m = 0; m < _m; m++ )
for(int ind,d=0; d < _d; d++)
{
b_t_djp = INFINITY_COST;
for ( int m2 = 0; m2 < _m; m2++ )
{
if (get_FILE_MD(m2,d) == -1)
{
if (data->t_djp(d,m,m2) < b_t_djp)
{
b_t_djp = data->t_djp(d,m,m2);
put_B_WRITE(m,d,m2);
}
}
}
}
FO = MS = CST = -1;
for ( int t = 0; t < _a_max; t++ )
for ( int m = 0; m < _m; m++ )
TL[t][m].vmo = TL[t][m].tp = TL[t][m].tk = TL[t][m].dt = TL[t][m].vm = TL[t][m].tm = TL[t][m].ti = -1 ;
for ( int i = 0; i < _n; i++ )
for ( int m = 0; m < _m; m++ )
TASK_MAQ[i][m] = 0;
}
// atualiza estruturas dado que um novo dado d foi escrito na maquina m e dispoivel no periodo t
void BDHeuri::atualiza_estruturas_escrita(Data* data, int n_d, int n_m, int d_time)
{
bool so_uma=true;
for ( int m = 0; m < _m; m++ )
{
// o dado nao existia
if (get_B_READ(m,n_d) == -1)
{
// ----------- atualiza B_READ
put_B_READ(m,n_d,n_m);
if (so_uma)
{
// ----------- atualiza TASK_T_MIN
for ( int i = 0; i < _n; i++ )
for(int d=0; d < data->d_in_tam[i]; d++)
if ((data->d_in[i][d] == n_d) and (TASK_T_MIN[i] < d_time))
{
TASK_T_MIN[i] = d_time;
break;
}
// ----------- atualiza FILE_IN
for (int i = 0; i < _n; i++ )
{
// checa se o dado eh de entrada da tarefa
for(int d=0; d < data->d_in_tam[i]; d++)
{
if (data->d_in[i][d] == n_d)
{
FILE_IN[i]--;
break;
}
}
}
//---------- atualiza DADO_T
DADO_T[n_d] = d_time;
so_uma = false;
}
// ---------------------------
}// if (get_B_READ(m,n_d) == -1)
else
// ----------- atualiza B_READ
if (data->t_djp(n_d,m,n_m) < data->t_djp(n_d,m,get_B_READ(m,n_d)))
put_B_READ(m,n_d,n_m);
// tem que escolher agora outra maquina para ser o melhor destino para gravar
if (get_B_WRITE(m,n_d) == n_m)
{
double b_t_djp = INFINITY_COST;
for ( int m2 = 0; m2 < _m; m2++ )
{
if (get_FILE_MD(m2,n_d) == -1)
{
if (data->t_djp(n_d,m,m2) < b_t_djp)
{
b_t_djp = data->t_djp(n_d,m,m2);
// ----------- atualiza B_WRITE
put_B_WRITE(m,n_d,m2);
}
}
}
}// if (get_B_WRITE(m,n_d) == n_m)
}// for ( int m = 0; m < _m; m++ )
}
// inicializa a lista de candidatos
void BDHeuri::inicializa_LC(Data* data)
{
for ( int i = 0; i < TMAX_LC; i++ )
{
LC[i].c = INFINITY_COST;
LC[i].t = -1;
LC[i].m = -1;
for ( int m = 0; m < data->max_in; m++ )
LC[i].r[m] =-1;
for ( int m = 0; m < data->max_out; m++ )
LC[i].w[m] =-1;
}
c_minLC = INFINITY_COST;
c_maxLC = -1;
tamLC = 0;
piorLC = -1;
melhorLC = -1;
}
// atualiza lista de candidatos
void BDHeuri::atualiza_LC(Data* data, int i, int m, double fo)
{
bool aDEPU = false;
// primeiro elemento
if (tamLC == 0)
{
c_minLC = fo;
c_maxLC = fo;
piorLC = 0;
melhorLC = 0;
LC[0].c = fo;
LC[0].t = i;
LC[0].m = m;
for(int d2=0; d2 < data->d_in_tam[i]; d2++)
LC[0].r[d2] = D_TASK[d2];
for(int d2=0; d2 < data->d_out_tam[i]; d2++)
LC[0].w[d2] = D_WRITE[d2];
tamLC++;
}
else
{
// lista nao esta cheia
if (tamLC < TMAX_LC)
{
if (fo < c_minLC)
{
c_minLC = fo;
melhorLC = tamLC;
}
if (fo > c_maxLC)
{
c_maxLC = fo;
piorLC = tamLC;
}
LC[tamLC].c = fo;
LC[tamLC].t = i;
LC[tamLC].m = m;
for(int d2=0; d2 < data->d_in_tam[i]; d2++)
LC[tamLC].r[d2] = D_TASK[d2];
for(int d2=0; d2 < data->d_out_tam[i]; d2++)
LC[tamLC].w[d2] = D_WRITE[d2];
tamLC++;
}
// lista cheia, checa o pior elemento da lista
else
{
// melhor que o pior elemento, troca
if (fo < c_maxLC)
{
if (fo < c_minLC)
{
c_minLC = fo;
melhorLC = piorLC;
}
LC[piorLC].c = fo;
LC[piorLC].t = i;
LC[piorLC].m = m;
for(int d2=0; d2 < data->d_in_tam[i]; d2++)
LC[piorLC].r[d2] = D_TASK[d2];
for(int d2=0; d2 < data->d_out_tam[i]; d2++)
LC[piorLC].w[d2] = D_WRITE[d2];
//encontra novo pior movimento
c_maxLC = -1;
for ( int i2 = 0; i2 < TMAX_LC; i2++ )
if (LC[i2].c > c_maxLC)
{
c_maxLC = LC[i2].c;
piorLC = i2;
}
}
}
}
if (aDEPU) { cout<<"------- LC ("<<tamLC<<") -------- ["<<c_minLC<<" , "<<c_maxLC<<"] best = "<<melhorLC<<" worst = "<<piorLC<<endl; for ( int i = 0; i < TMAX_LC; i++ ) cout<<i<<") "<<LC[i].c<<", "; cout<<"--------------------------------"<<endl; }
}
// funcao de comparacao de elementos da lista de candidatos para o qsort
int BDHeuri::compLC(const void* pp, const void* qq)
{
bd_movimento vpp,vqq;
vpp=*(bd_movimento*) pp;
vqq=*(bd_movimento*) qq;
if (vpp.c < vqq.c)
return -1;
else
if (vpp.c > vqq.c)
return 1;
else
return 0;
}
// atualiza vetor de tempos para re-executar uma tarefa a partir do D_REEXC
// Task - tarefa que disparou o processo com o objetivo de ter o dado de entrada gerado
// Maq - maquina em que as duplicacoes devem ocorrer
// mov - indica se eh movimento (true) ou se eh checagem de vizinho apenas (false)
// Vai alterando MS_M[Maq] e CAP_M[Maq]
bool BDHeuri::exec_read_reexc(Data* data, int Task, int Maq, bool mov)
{
int dl, tdl, cdl, br, min_lx;
bool viavel;
int Maq_stg, Maq_site;
// Site da Maquina que esta sendo executada a tarefa E Maquina de armazenamento do site da maquina que esta executando a tarefa
Maq_site = data->m_site[Maq];
Maq_stg = data->vm_st_site[Maq_site];
// inicializacao
for(int i=0; i < _n; i++)
TASK_RXC[i] = 0;
for(int d=0; d < _d; d++)
FILE_RXC[d] = 0;
min_lx = shift_read(data, data->d_in_tam[Task], Task);
if (DEPU) cout<<" shift de leitura = +"<<min_lx<<endl;
if (MS_M[Maq] < min_lx)
MS_M[Maq] = min_lx;
// ------ faz primeiro as re-execucoes para gerar os dados ----------
for(int d=0; d < data->d_in_tam[Task]; d++)
if ((D_REEXC[d]==1) && (FLAG != 0))
{
dl = data->d_in[Task][d]; // dado a ser lido
tdl = data->d_out_t[dl]; // tarefa a ser duplicada local na maquina Maq
/* Se tdl ja tiver sido executado em Maq retorne inviavel (false) */
if (TASK_MAQ[tdl][Maq] == 1)
{
//cout<<endl<<"---- tarefa a ser re-exec "<<tdl<<" na maq"<<Maq<<" ja foi executada na maquina"<<endl;
return false;
}
if (TASK_RXC[tdl] == 0)
{
// Erros
if (tdl == -1) { cout<<"ERRO (R) dado "<<dl<<" nao tem tarefa que o gere ERRO"<<endl; exit(0);}
if (get_FILE_MD(Maq_stg, dl) != -1) { cout<<"ERRO (R) dado a ser gerado "<<dl<<" ja existe na maquina ERRO"<<endl; exit(0);}
if ((TASK[tdl]==0) and (data->t_ob[tdl] == 1))
{ cout<<"ERRO (R) Task="<<Task<<", tentando re-executar tarefa "<<tdl<<" que eh obrigatoria e ainda nao foi executada ERRO (talvez existam duas tarefas gerando o mesmo arquivo de saida o que eh inconsistente do arquivo de entrada)"<<endl; exit(0);}
//re-exec tarefa na maquina maq
viavel = reexc(data, tdl, Maq, Maq_stg, 0, mov);
if (!viavel)
return false;
}
}//if (D_REEXC[d]==1) && (FLAG != 0)
// ------ faz agora a leitura dos dados gerados das reexecucoes
for(int d=0; d < data->d_in_tam[Task]; d++)
if ((D_REEXC[d]==1) && (FLAG != 0))
{
dl = data->d_in[Task][d]; // dado a ser lido
tdl = data->d_out_t[dl]; // tarefa a ser duplicada local
cdl = data->t_djp (dl,Maq,Maq_stg);
if (mov)
{
TL[QTD_M[Maq]][Maq].vmo = Maq;
TL[QTD_M[Maq]][Maq].tp = 1;
TL[QTD_M[Maq]][Maq].tk = Task;
TL[QTD_M[Maq]][Maq].dt = dl;
TL[QTD_M[Maq]][Maq].vm = Maq_stg;
TL[QTD_M[Maq]][Maq].tm = cdl;
TL[QTD_M[Maq]][Maq].ti = MS_M[Maq];
QTD_M[Maq]++;
if (QTD_M[Maq] >= _a_max) {cout<<"ERRO tamanho de TL passou _a_max = "<<_a_max<<endl; exit(0);}
}
MS_M[Maq] += cdl;
if (DEPU) cout<<" r"<<Task<<"("<<dl<<")<"<<Maq_stg<<"\t="<<cdl<<endl;
}//if (D_REEXC[d]==1) && (FLAG != 0)
// teste de limite de periodos
if (MS_M[Maq] >= _t_max)
{
if (DEPU) cout<<" cfg. inv. passou tempo maximo t_max, MS_M[Maq] = "<<MS_M[Maq]<<endl;
return false;
}
// ------ faz depois as leituras dos dados que nao foram gerados por reexecucoes ----------
for(int d=0; d < data->d_in_tam[Task]; d++)
if (D_REEXC[d]==0)
{
dl = data->d_in[Task][d]; // dado a ser lido
// checa se foi gerado localmente por alguma re-execucao, entao ele esta local
if (FILE_RXC[dl] == 1)
br = Maq_stg;
// senao pega a melhor maquina para ler o dado
else
br = get_B_READ(Maq, dl);
cdl = data->t_djp (dl,Maq, br);
if (mov)
{
TL[QTD_M[Maq]][Maq].vmo = Maq;
TL[QTD_M[Maq]][Maq].tp = 1;
TL[QTD_M[Maq]][Maq].tk = Task;
TL[QTD_M[Maq]][Maq].dt = dl;
TL[QTD_M[Maq]][Maq].vm = br;
TL[QTD_M[Maq]][Maq].tm = cdl;
TL[QTD_M[Maq]][Maq].ti = MS_M[Maq];
QTD_M[Maq]++;
if (QTD_M[Maq] >= _a_max) {cout<<"ERRO tamanho de TL passou _a_max. "<<_a_max<<endl; exit(0);}
}
MS_M[Maq] += cdl;
if (DEPU) cout<<" r"<<Task<<"("<<dl<<")<"<<br<<"\t="<<cdl<<endl;
if (br == -1) {cout<<"ERRO (R) arquivo de entrada "<<dl<<" nao existe ERRO"<<endl; exit(0);}
}// if (D_REEXC[d]==0)
// considera execucao
if (mov)
{
TL[QTD_M[Maq]][Maq].vmo = Maq;
TL[QTD_M[Maq]][Maq].tp = 0;
TL[QTD_M[Maq]][Maq].tk = Task;
TL[QTD_M[Maq]][Maq].tm = data->t_ij(Task,Maq);
TL[QTD_M[Maq]][Maq].ti = MS_M[Maq];
QTD_M[Maq]++;
if (QTD_M[Maq] >= _a_max) {cout<<"ERRO tamanho de TL passou _a_max. "<<_a_max<<endl; exit(0);}
}
MS_M[Maq] += data->t_ij(Task,Maq);
if (DEPU) cout<<" x"<<Task<<"\t="<<data->t_ij(Task,Maq)<<endl;
// teste de limite de periodos
if (MS_M[Maq] >= _t_max)
{
if (DEPU) cout<<" cfg. inv. passou tempo maximo t_max, MS_M[Maq] = "<<MS_M[Maq]<<endl;
return false;
}
else
return true;
}
// procedimento recurssivo para re-executar task em maq
// Vai alterando MS_M[maq] e CAP_M[maq]
bool BDHeuri::reexc(Data* data, int task, int maq, int Maq_stg, int level, bool mov)
{
int dl2, br2, tdl2, cd2;
bool viavel;
viavel = true;
if (level > MAX_LV_BK-1)
{
if (DEPU) cout<<" cfg. inv., passou nivel maximo de backtracking de reexecucao = "<<MAX_LV_BK<<endl;
return false;
}
if (level > bk_lv_max)
bk_lv_max = level;
if (TASK_RXC[task] == 0)
{
// ****************** leitura da entrada da tarefa duplicada **********************
for(int d2=0; d2 < data->d_in_tam[task]; d2++)
{
dl2 = data->d_in[task][d2]; // dado a ser lido pela tarefa duplicada