Quellcode durchsuchen

Before ADAM extension

Your Name vor 7 Jahren
Ursprung
Commit
6c3ff5c41e
10 geänderte Dateien mit 1179 neuen und 607 gelöschten Zeilen
  1. 18 3
      Makefile
  2. 17 16
      apps.c
  3. 7 8
      common_core.c
  4. 1 1
      common_core.h
  5. 43 464
      my_rtrm.c
  6. 0 3
      my_rtrm.h
  7. 2 0
      noc_functions.h
  8. 959 0
      resource_negotiation.c
  9. 7 0
      resource_negotiation.h
  10. 125 112
      signal_handlers.c

+ 18 - 3
Makefile

@@ -18,7 +18,7 @@ else
 	PLATFORM_INCLUDES = variables.h structs.h macros.h
 endif
 
-OBJS=my_rtrm.o libfun.o nocfun.o sigaux.o controller.o common_node.o signal_handlers.o scc_signals.o idag_defs.o paxos_signal_handlers.o apps.o $(ARCHIVE)
+OBJS=my_rtrm.o libfun.o nocfun.o sigaux.o controller.o common_node.o signal_handlers.o scc_signals.o idag_defs.o paxos_signal_handlers.o apps.o resource_negotiation.o $(ARCHIVE)
 
 #
 #PAXOS OBJECTS
@@ -60,6 +60,14 @@ endif
 #
 #
 #
+ifeq ($(RESOURCE_ALGO), ORIGINAL)
+	MY_FLAGS += -DRESOURCE_ALGO_ORIG
+else ifeq ($(RESOURCE_ALGO), UPDATED)
+	MY_FLAGS += -DRESOURCE_ALGO_UPDATED
+else
+	MY_FLAGS += -DRESOURCE_ALGO_ORIG
+endif
+
 ifeq ($(SINGLE_WORKER),1)
   MY_FLAGS += -DSINGLE_WORKER
 endif
@@ -104,6 +112,10 @@ ifeq ($(LOW_VOLTAGE),5)
   MY_FLAGS += -DLOW_VOLTAGE_5
 endif
 
+ifeq ($(GREEDY_MANAGER),1)
+  MY_FLAGS += -DGREEDY_MANAGER
+endif
+
 my_rtrm:$(OBJS) 
 	cd ./../bin && $(CCOMPILE) $(CFLAGS) $(MY_FLAGS) -o my_rtrm $(OBJS) -pthread -lrt -lm
 
@@ -128,7 +140,7 @@ common_node.o: common_core.c common_core.h libfunctions.h my_rtrm.h noc_function
 my_rtrm.o: my_rtrm.c libfunctions.h my_rtrm.h noc_functions.h sig_aux.h controller_core.h common_core.h signal_handlers.h paxos_signal_handlers.h scc_signals.h idag_defs.h $(PLATFORM_INCLUDES)
 	$(CCOMPILE) $(CFLAGS) $(MY_FLAGS) -o ./../bin/my_rtrm.o -c my_rtrm.c
 
-signal_handlers.o: signal_handlers.c libfunctions.h my_rtrm.h noc_functions.h sig_aux.h scc_signals.h $(PLATFORM_INCLUDES)
+signal_handlers.o: signal_handlers.c libfunctions.h my_rtrm.h noc_functions.h sig_aux.h scc_signals.h resource_negotiation.h $(PLATFORM_INCLUDES)
 	$(CCOMPILE) $(CFLAGS) $(MY_FLAGS) -o ./../bin/signal_handlers.o -c signal_handlers.c
 
 paxos_signal_handlers.o: paxos_signal_handlers.c libfunctions.h my_rtrm.h noc_functions.h sig_aux.h scc_signals.h signal_handlers.h sig_aux.h $(PLATFORM_INCLUDES)
@@ -140,8 +152,11 @@ idag_defs.o: idag_defs.c idag_defs.h my_rtrm.h $(PLATFORM_INCLUDES)
 apps.o: apps.c apps.h scc_signals.h libfunctions.h $(PLATFORM_INCLUDES)
 	$(CCOMPILE) $(CFLAGS) $(MY_FLAGS) -o ./../bin/apps.o -c apps.c	
 
+resource_negotiation.o: resource_negotiation.c resource_negotiation.h apps.h noc_functions.h $(PLATFORM_INCLUDES)
+	$(CCOMPILE) $(CFLAGS) $(MY_FLAGS) -o ./../bin/resource_negotiation.o -c resource_negotiation.c
+
 clean:
-	rm -f ./../bin/my_rtrm ./../bin/my_rtrm.o ./../bin/libfun.o ./../bin/nocfun.o ./../bin/sigaux.o ./../bin/common_node.o ./../bin/controller.o ./../bin/signal_handlers.o ./../bin/paxos_signal_handlers.o ./../bin/scc_signals.o ./../bin/idag_defs.o ./../bin/apps.o
+	rm -f ./../bin/my_rtrm ./../bin/my_rtrm.o ./../bin/libfun.o ./../bin/nocfun.o ./../bin/sigaux.o ./../bin/common_node.o ./../bin/controller.o ./../bin/signal_handlers.o ./../bin/paxos_signal_handlers.o ./../bin/scc_signals.o ./../bin/idag_defs.o ./../bin/apps.o ./../bin/my_rtrm.resource_negotiation.o
 	rm -rf ./../scenaria/6x6/paxos_log_files/*
 	rm -rf ./../scenaria/6x6/log_files/*
 	rm -rf ./../scenaria/6x6/app_logs/*

+ 17 - 16
apps.c

@@ -612,23 +612,24 @@ void init_speedup_structs (void) {
 			exit(0);	
 		}
 	} else if (executed_app == SVM) {
+		/* N_sv 4096 D_sv 4096 */
 		Exec_Speedup[0] = 1.0; /* 1 worker */
-		Exec_Speedup[1] = 2.006;
-		Exec_Speedup[2] = 2.814;
-		Exec_Speedup[3] = 3.469;
-		Exec_Speedup[4] = 4.029;
-		Exec_Speedup[5] = 4.285;
-		Exec_Speedup[6] = 4.646;
+		Exec_Speedup[1] = 1.959;
+		Exec_Speedup[2] = 2.919;
+		Exec_Speedup[3] = 3.853;
+		Exec_Speedup[4] = 4.777;
+		Exec_Speedup[5] = 5.723;
+		Exec_Speedup[6] = 6.644;
 		Exec_Speedup[7] = 0.0;
 		
-		Exec_Latencies[0] = 28;
-		Exec_Latencies[1] = 14;
-		Exec_Latencies[2] = 10;
-		Exec_Latencies[3] = 8;
-		Exec_Latencies[4] = 7;
-		Exec_Latencies[5] = 7;
-		Exec_Latencies[6] = 6;
-		Exec_Latencies[7] = 6;
+		Exec_Latencies[0] = 578;
+		Exec_Latencies[1] = 295;
+		Exec_Latencies[2] = 198;
+		Exec_Latencies[3] = 150;
+		Exec_Latencies[4] = 121;
+		Exec_Latencies[5] = 101;
+		Exec_Latencies[6] = 87;
+		Exec_Latencies[7] = 6; /* Irrelevant */
 	} else if (executed_app == FFT) {
 		Exec_Speedup[0] = 1.0; /* 1 worker */
 		Exec_Speedup[1] = 1.55;
@@ -933,7 +934,7 @@ float Speedup(app cur_app, int num_of_cores) {
 
 	return matr_speedup[type][num_of_cores-1];
 	*/
-	if (num_of_cores > get_max_cores_count(cur_app)) {
+	if ((num_of_cores < 2) || (num_of_cores > get_max_cores_count(cur_app))) {
                 return 0;
         } else {
 		return Exec_Speedup[num_of_cores-2];
@@ -1054,7 +1055,7 @@ void matrix_transpose(int n1, float *src, float *dest, int node_id, int myFirst,
 void FFT1D(int direction, int M, int N, float *x, float *scratch, float *upriv, float *umain2, int node_id, int myFirst, int myLast, int pad_length, int P){
 	int j, m1, n1;
 	
-	printf("I am %d and I am inside FFT1D\n",node_id);
+	//printf("I am %d and I am inside FFT1D\n",node_id);
 	//fprintf(log_file,"I am inside FFT1D-A myFirst=%d myLast=%d\n",myFirst,myLast);
 	
 	m1 = M/2;

+ 7 - 8
common_core.c

@@ -58,7 +58,7 @@ FILE *app_log_file;
 
 app_exec executed_app;
 
-void common_node_actions(char scen_directory[SCEN_DIR_SIZE], char scen_num[SCEN_NUM_SIZE]) {
+void common_node_actions(char scen_directory[SCEN_DIR_SIZE], char scen_num[SCEN_NUM_SIZE], int Selfopt_Radius, int Max_SelfOpt_Interval_MS) {
   
 	int i;
 	int j;
@@ -66,7 +66,6 @@ void common_node_actions(char scen_directory[SCEN_DIR_SIZE], char scen_num[SCEN_
 	int one_core;
 	int new_agent;
 	int min_dist;
-	int selfopt_r;
 	int row_reached;
 	int per_core_rows;
 	int rows_left;
@@ -161,7 +160,6 @@ void common_node_actions(char scen_directory[SCEN_DIR_SIZE], char scen_num[SCEN_
 		pending_agent.work_bounds[1]=0;
 		
 		my_cores=NULL;
-		selfopt_r = 3; /* (int) (1.5 * (X_max / num_idags_x)); */
 		
 		install_signal_handlers();
 		
@@ -180,7 +178,7 @@ void common_node_actions(char scen_directory[SCEN_DIR_SIZE], char scen_num[SCEN_
 		
 		cur_time = time(NULL);
 		cur_t = localtime(&cur_time);
-		fprintf(log_file, "[%d:%d:%d]: Initialisation complete Selfopt R = %d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,selfopt_r);
+		fprintf(log_file, "[%d:%d:%d]: Initialisation complete Selfopt R = %d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,Selfopt_Radius);
 		
 		#ifdef PLAT_SCC
 		RCCE_barrier(&RCCE_COMM_WORLD);
@@ -286,7 +284,7 @@ void common_node_actions(char scen_directory[SCEN_DIR_SIZE], char scen_num[SCEN_
 					free(tmp_offer_list);
 				}
 			
-			fprintf(log_file, "near init area (%d,%d)\n",node_id,selfopt_r);
+			fprintf(log_file, "near init area (%d,%d)\n",node_id,Selfopt_Radius);
 			if (core_inter_head[my_idag] == NULL){
 				core_inter_head[my_idag] = (inter_list *) malloc(sizeof(inter_list));
 				core_inter_tail[my_idag] = core_inter_head[my_idag];
@@ -297,7 +295,7 @@ void common_node_actions(char scen_directory[SCEN_DIR_SIZE], char scen_num[SCEN_
 
 			core_inter_tail[my_idag]->type = IDAG_FIND_IDAGS_PENDING;
 			core_inter_tail[my_idag]->data.reg.C = node_id-1;
-			core_inter_tail[my_idag]->data.reg.r = selfopt_r;
+			core_inter_tail[my_idag]->data.reg.r = Selfopt_Radius;
 			core_inter_tail[my_idag]->next = NULL;
 
 			if (core_inter_head[my_idag]->next == NULL) {
@@ -815,7 +813,7 @@ void common_node_actions(char scen_directory[SCEN_DIR_SIZE], char scen_num[SCEN_
 
 				core_inter_tail[my_idag]->type = SELFOPT_IDAG_FIND_IDAGS_PENDING;
 				core_inter_tail[my_idag]->data.reg.C = node_id;
-				core_inter_tail[my_idag]->data.reg.r = selfopt_r;
+				core_inter_tail[my_idag]->data.reg.r = Selfopt_Radius;
 				core_inter_tail[my_idag]->next = NULL;
 
 				if (core_inter_head[my_idag]->next == NULL)	{
@@ -827,7 +825,7 @@ void common_node_actions(char scen_directory[SCEN_DIR_SIZE], char scen_num[SCEN_
 					fprintf(log_file,"common_core.c: Did not send sig_find_idags with inter1 = %s, inter2 = %s\n",inter2string(core_inter_head[my_idag]->type),inter2string(core_inter_head[my_idag]->next->type));
 				}
 					
-				if (selfopt_interval != MAX_SELF_OPT_INTERVAL_MS) selfopt_interval = 2 * selfopt_interval;
+				if (selfopt_interval <= Max_SelfOpt_Interval_MS) selfopt_interval = 2 * selfopt_interval;
 				else {
 					selfopt_interval = -1;
 					/*fprintf(log_file,"I invalidate selfopt process here\n");
@@ -1360,6 +1358,7 @@ void common_node_actions(char scen_directory[SCEN_DIR_SIZE], char scen_num[SCEN_
 				cur_t = localtime(&time_val.tv_sec);				
 				fprintf(app_log_file, "[%d:%d:%d:%ld]: App ended pending_state=%s\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,time_val.tv_usec,id2string(pending_state));
 				fclose(app_log_file);			
+				app_log_file = NULL; /* Added on 5.5.2017 */
 
 				if (pending_state == INIT_MANAGER || pending_state == IDLE_INIT_MAN || pending_state == INIT_MAN_CHK_OFFERS || pending_state == INIT_MANAGER_SEND_OFFERS) {
 					state = pending_state;

+ 1 - 1
common_core.h

@@ -2,7 +2,7 @@
 #define __COMMON_NODE_H__
 
 #include "variables.h"
-void common_node_actions(char scen_directory[SCEN_DIR_SIZE], char scen_num[SCEN_NUM_SIZE]);
+void common_node_actions(char scen_directory[SCEN_DIR_SIZE], char scen_num[SCEN_NUM_SIZE], int Selfopt_Radius, int Max_SelfOpt_Interval_MS);
 void my_settimer(int msec);
 int my_gettimer(void);
 #endif

+ 43 - 464
my_rtrm.c

@@ -134,7 +134,8 @@ int main(int argc, char *argv[]) {
 	int timer_init_null = 0;
 	int executed_app_type_number = 0; /* Used for the initialization of init_app type */
 	int executed_app_array_size = 0; /* Used for the initialization of init_app type */
-	
+	int Selfopt_Radius,Selfopt_Rounds,Max_SelfOpt_Interval_MS;
+
 	float avg_cluster_util;
 	
 	char scen_directory[MAX_STR_NAME_SIZE];
@@ -160,7 +161,7 @@ int main(int argc, char *argv[]) {
 
 	setvbuf(stdout, NULL, _IONBF, 0);
 	
-	if (argc < 17) {
+	if (argc < 21) {
 		printf("USAGE: ./my_rtrm <options>\n\n");
 		printf("options:\n");
 		printf("\t-d <scen_dir>    : <scen_dir> = Directory of all scenarios.\n");
@@ -171,10 +172,13 @@ int main(int argc, char *argv[]) {
 		printf("\t-x X             : X = X * 6 will be the horizontal dimension of the grid.\n"); 
 		printf("\t-y Y             : Y = Y * 8 will be the vertical dimension of the grid.\n");
 		printf("\t-t <app_type>    : <app_type> M for MATRIX_MUL, S for SVM, F for FFT \n");
+		printf("\t-r <region_r>    : Selfopt radius \n");
+		printf("\t-u <rounds>	   : Selfopt rounds \n");
 		exit(0);
 	}
 
-	while ((c = getopt(argc, argv, "d:n:i:x:y:a:p:t:h")) != -1){
+	/* Put h last in getopt in order to work correctly */
+	while ((c = getopt(argc, argv, "d:n:i:x:y:a:p:t:r:u:h")) != -1){
 		switch(c){
 			case 'd':
 				strcpy(scen_directory, optarg);
@@ -197,8 +201,7 @@ int main(int argc, char *argv[]) {
 				#endif
 				if (node_id == 0)
 					printf("X = %d...\n",X_max);
-				break;
-				
+				break;		
 			case 'y':
 				#ifdef PLAT_LINUX
 				Y_max = atoi(optarg) * 8;
@@ -206,6 +209,20 @@ int main(int argc, char *argv[]) {
 				if (node_id == 0)
 					printf("Y = %d...\n",Y_max);
 				break;
+			case 'r':
+                                Selfopt_Radius = atoi(optarg);
+                                if (node_id == 0)
+                                        printf("Selfopt_Radius = %d...\n",Selfopt_Radius);
+                                break;
+			case 'u':
+                                Selfopt_Rounds = atoi(optarg);
+                                if (node_id == 0)
+                                        printf("Selfopt_Rounds = %d...\n",Selfopt_Rounds);
+				Max_SelfOpt_Interval_MS = LEAST_SELF_OPT_INTERVAL_MS;
+				for (i=1; i<Selfopt_Rounds; i++) {
+					Max_SelfOpt_Interval_MS = 2 * Max_SelfOpt_Interval_MS;
+				}
+                                break;
 			case 'a':
 				strcpy(app_input_file, optarg);
 				if (node_id == 0)
@@ -256,6 +273,8 @@ int main(int argc, char *argv[]) {
 				printf("\t-x X             : X = X * 6 will be the horizontal dimension of the grid.\n"); 
 				printf("\t-y Y             : Y = Y * 8 will be the vertical dimension of the grid.\n");
 				printf("\t-t <app_type>    : <app_type> M for MATRIX_MUL, S for SVM, F for FFT \n");
+				printf("\t-r <region_r>    : Selfopt radius \n");
+		                printf("\t-u <rounds>      : Selfopt rounds \n");
 				exit(0);
 				break;
 		}
@@ -399,7 +418,7 @@ int main(int argc, char *argv[]) {
 		core_inter_tail[i] = NULL;
 		timer_schedule[i] = 0;
 	}
-	
+
 	read_idag_defs(scen_directory, scen_num, idag_defs_file_name, paxos_scen);
 	global_idag_defs();
 	/* Initialise structs for mapping of scc coordinates to normal ones*/
@@ -434,7 +453,7 @@ int main(int argc, char *argv[]) {
 	if ((node_id != idag_id_arr[0]) && (is_core_idag(node_id) == 1)) {
 		idle_agent_actions(scen_directory,scen_num); //dimos comment out
 	} else if (node_id != idag_id_arr[0]) {
-		common_node_actions(scen_directory,scen_num);
+		common_node_actions(scen_directory,scen_num,Selfopt_Radius,Max_SelfOpt_Interval_MS);
 	} else {
 
 	 index_bottom[node_id] = 0;
@@ -491,7 +510,21 @@ int main(int argc, char *argv[]) {
 	DDS->num_of_cores = my_cores_count;
 	log_file = create_log_file(node_id, 0, scen_directory, scen_num);
 	setbuf(log_file, NULL);
-	 
+	        
+	fprintf(log_file,"Selfopt_Radius = %d Selfopt_Rounds = %d Max_SelfOpt_Interval_MS = %d\n",Selfopt_Radius,Selfopt_Rounds,Max_SelfOpt_Interval_MS);
+	if (node_id == 0) {
+#ifdef RESOURCE_ALGO_ORIG
+		printf("Resource algo is original\n");
+		fprintf(log_file,"Resource algo is original\n");
+#elif RESOURCE_ALGO_UPDATED
+		printf("Resource algo is updated\n");
+		fprintf(log_file,"Resource algo is updated\n");
+#else
+		printf("Resource algo not chosen. Fallback to original\n");
+		fprintf(log_file,"Resource algo not chosen. Fallback to original\n");
+#endif
+	}
+ 
 	alive = (int *)malloc(X_max*Y_max*sizeof(int));
 	suspected = (int *)malloc(X_max*Y_max*sizeof(int));
 	for (i = 0; i < X_max*Y_max; i++){
@@ -596,8 +629,7 @@ int main(int argc, char *argv[]) {
 		printf("Cannot open input file with file path = %s ",init_ack_file_name);
 		perror("open app_input");
 	}
-	 
-	 
+	  
 	 /* Open the Application Input File*/
 #ifdef PLAT_SCC
 	 strcpy(app_input_file_name, "/shared/herc/");
@@ -629,8 +661,7 @@ int main(int argc, char *argv[]) {
 	 its.it_value.tv_sec = 0;
 	 its.it_value.tv_nsec = 10 * MS;
 	 if (timer_settime(timerid, 0, &its, NULL) == -1) perror("timer_settime error9");
-	 
-	 
+	  
 	 gettimeofday(&time_val, NULL);
 	 cur_t = localtime(&time_val.tv_sec);
 	 start_time_of_day = *cur_t;
@@ -1048,455 +1079,3 @@ int main(int argc, char *argv[]) {
   
   return 0;
 }
-
-/* Speedup is calculated correclty inside Speedup function */
-int offer_cores(core_list *cores, app req_app, region req_reg, int *Offered_cores, int req_id) {
-	int Of_cores_num=0, min_dist=0, cur_dist=0;
-	float gain_total=0.1, base_receiver=0.0, base_giver=0.0, gain_receiver=0.0, loss_giver=0.0, share_giver=0.0, new_gain=0.0;
-	int Cores_receiver = req_app.num_of_cores, Cores_giver = my_app.num_of_cores;
-	core_list *tmp, *GreedyChoice;
-	int offered_cnt=0, counted_cores=0;
-
-	for (tmp=cores; tmp!=NULL; tmp=tmp->next) {
-		if (distance(req_reg.C, tmp->core_id) <= req_reg.r) share_giver++;
-		counted_cores++;
-		if (tmp->offered_to != -1) offered_cnt++;
-		fprintf(log_file,"Core %d is offered to %d\n",tmp->core_id,tmp->offered_to);	
-	}
-	fprintf(log_file,"Proceeding\n");
-	fflush(log_file);
-
-	if (offered_cnt == (counted_cores-2) && my_idag != -1) {
-		fprintf(log_file,"I did not give up my only not offered core\n");
-		fflush(log_file);	
-		return 0;
-	}
-	
-	share_giver = share_giver / (float) region_count(req_reg);
-
-	if (my_idag == -1) {
-		while (gain_total > 0.0) {
-			gain_total = 0.0;
-			GreedyChoice = NULL;//-1;
-			min_dist = -1;
-			base_giver = 0; 		
-			tmp = cores->next;//very important!!! that way i avoid giving up my agent core
-			
-			while (tmp != NULL) {
-				cur_dist = distance(req_reg.C, tmp->core_id);
-				if (tmp->offered_to == -1 && cur_dist <= req_reg.r) {
-					//Of_cores_num == 0 to be the first offered core
-					//Cores_receiver == 0 to avoid providing the core to an non-initial core search
-					if (low_voltage_core[tmp->core_id] && Of_cores_num == 0 && Cores_receiver == 0) {
-						if ((Cores_receiver + Of_cores_num) == 0) {
-							gain_receiver = 1000; //0 sto init_app
-						} else if ((Cores_receiver + Of_cores_num) == 1) {
-							gain_receiver = 100; //no worker cores	
-						} else { /* (Cores_receiver + Of_cores_num) > 1 */
-							base_receiver = Speedup(req_app, Cores_receiver + Of_cores_num);
-							//gain_receiver = share_giver * (Speedup(req_app, Cores_receiver + Of_cores_num + 1) - base_receiver); 
-							gain_receiver = share_giver * (Speedup(req_app, Cores_receiver + Of_cores_num + 1) - base_receiver); 
-						}
-						
-						loss_giver = 0;
-						new_gain = gain_receiver - loss_giver;
-						gain_total = new_gain;
-						GreedyChoice = tmp;//->core_id;
-						break;
-					#ifdef LOW_VOLTAGE_ISLANDS_4
-					} else if (low_voltage_core[tmp->core_id] && Of_cores_num == 0 && Cores_receiver == 1) {
-							if (Cores_receiver == 0 && Of_cores_num == 0) gain_receiver = 1000; //0 sto init_app                            
-							else gain_receiver = share_giver * (Speedup(req_app, Cores_receiver + Of_cores_num + 1) - base_receiver);
-
-							loss_giver = 0;
-
-							new_gain = gain_receiver - loss_giver;
-							gain_total = new_gain;
-							GreedyChoice = tmp;//->core_id;
-							break;
-					#endif
-					} else if (low_voltage_core[tmp->core_id] == 0) {  
-						if ((Cores_receiver + Of_cores_num) == 0) {
-							gain_receiver = 1000; //0 sto init_app
-						} else if ((Cores_receiver + Of_cores_num) == 1) {
-							gain_receiver = 100; //no worker cores	
-						} else { /* (Cores_receiver + Of_cores_num) > 1 */
-							base_receiver = Speedup(req_app, Cores_receiver + Of_cores_num);
-							gain_receiver = share_giver * (Speedup(req_app, Cores_receiver + Of_cores_num + 1) - base_receiver);
-						}			
-					
-						loss_giver = 0;
-						new_gain = gain_receiver - loss_giver;
-						if (new_gain > gain_total){
-							gain_total = new_gain;
-							min_dist = cur_dist;
-							GreedyChoice = tmp;//->core_id;
-						} else if (new_gain == gain_total && cur_dist < min_dist) {
-							//printf("I am %d and i change offer to %d with cores %d->%d with distances %d->%d\n",
-							//	node_id,req_id,GreedyChoice->core_id,tmp->core_id,min_dist,cur_dist);
-							min_dist = cur_dist;
-							GreedyChoice = tmp;
-						}
-					}	
-				}
-
-				tmp = tmp->next;
-			}
-
-			if (gain_total > 0.0) {
-				Offered_cores[Of_cores_num++] = GreedyChoice->core_id;
-				GreedyChoice->offered_to = req_id;
-			}
-		}
-	}
-	#ifndef GREEDY_MANAGER
-	else {
-		while (gain_total > 0.0) {
-			gain_total = 0.0;
-			GreedyChoice = NULL;//-1;
-			min_dist = -1;
-			base_giver = Speedup(my_app, Cores_giver - Of_cores_num);
-		
-			tmp = cores->next->next;//very important!!! that way i avoid giving up my only working core
-	
-			while (tmp != NULL) {
-				if (core_inter_head[tmp->core_id] != NULL && 
-					(core_inter_head[tmp->core_id]->type == INIT_WORK_NODE_PENDING || core_inter_head[tmp->core_id]->type == INIT_WORK_NODE)) { 
-					fprintf(log_file,"Core %d is about to start work type = %d\n",tmp->core_id,core_inter_head[tmp->core_id]->type);
-					fflush(log_file);	
-					tmp = tmp->next;
-				} else {
-					cur_dist = distance(req_reg.C, tmp->core_id);
-					if (tmp->offered_to == -1 && cur_dist <= req_reg.r) {
-						if ((Cores_receiver + Of_cores_num) == 0) {
-							gain_receiver = 1000; //0 sto init_app
-						} else if ((Cores_receiver + Of_cores_num) == 1) {
-							gain_receiver = 100; //no worker cores -- in case I have only one worker core then I should not be here	
-						} else { /* (Cores_receiver + Of_cores_num) > 1 */
-							base_receiver = Speedup(req_app, Cores_receiver + Of_cores_num);
-							gain_receiver = share_giver * (Speedup(req_app, Cores_receiver + Of_cores_num + 1) - base_receiver); /*  + 1 is ommited due to workload convention */
-						}
-						
-						loss_giver = base_giver - Speedup(my_app, Cores_giver - (Of_cores_num + 1));
-					
-						new_gain = gain_receiver - loss_giver;
-						if (new_gain > gain_total){
-							gain_total = new_gain;
-							min_dist = cur_dist;
-							GreedyChoice = tmp;//->core_id;
-						} else if (new_gain == gain_total && cur_dist < min_dist) {
-							//printf("I am %d and i change offer to %d with cores %d->%d with distances %d->%d\n",
-							//	node_id,req_id,GreedyChoice->core_id,tmp->core_id,min_dist,cur_dist);
-							min_dist = cur_dist;
-							GreedyChoice = tmp;
-						}
-					}
-
-					tmp = tmp->next;
-				}
-			}
-
-			if (gain_total > 0.0) {
-				Offered_cores[Of_cores_num++] = GreedyChoice->core_id;
-				GreedyChoice->offered_to = req_id;
-			}
-		}
-	}
-	#endif
-
-	fprintf(log_file,"I will offer %d cores\n",Of_cores_num);
-	fflush(log_file);
-	return Of_cores_num;
-}
-
-// int offer_cores_fft(core_list *cores, app req_app, region req_reg, int *Offered_cores, int req_id) {
-// 	int Of_cores_num=0, min_dist=0, cur_dist=0;
-// 	float gain_total=0.1,base_receiver=0.0,base_giver=0.0,gain_receiver=0.0,loss_giver=0.0,share_giver=0.0,new_gain=0.0;
-// 	int Cores_receiver = req_app.num_of_cores, Workers_giver = my_app.num_of_cores-1;
-// 	core_list *tmp, *GreedyChoice;
-// 	int offered_cnt=0, counted_cores=0;
-// 
-// 	for (tmp=cores; tmp!=NULL; tmp=tmp->next) {
-// 		if (distance(req_reg.C, tmp->core_id) <= req_reg.r) share_giver++;
-// 		counted_cores++;
-// 		if (tmp->offered_to != -1) offered_cnt++;
-// 		fprintf(log_file,"Core %d is offered to %d\n",tmp->core_id,tmp->offered_to);	
-// 	}
-// 	fflush(log_file);
-// 
-// 	if (offered_cnt == (counted_cores-2) && my_idag != -1) {
-// 		fprintf(log_file,"I did not give up my only not offered core\n");
-// 		fflush(log_file);	
-// 		return 0;
-// 	}
-// 	share_giver = share_giver / (float) region_count(req_reg);
-// 
-// 	if (my_idag == -1) {
-// 		while (gain_total > 0.0) {
-// 			gain_total = 0.0;
-// 			GreedyChoice = NULL;//-1;
-// 			min_dist = -1;
-// 			base_giver = 0; 		
-// 			tmp = cores->next;//very important!!! that way i avoid giving up my agent core
-// 			
-// 			while (tmp != NULL) {
-// 				cur_dist = distance(req_reg.C, tmp->core_id);
-// 				if (tmp->offered_to == -1 && cur_dist <= req_reg.r) {
-// 					//Of_cores_num == 0 to be the first offered core
-// 					//Cores_receiver == 0 to avoid providing the core to an non-initial core search
-// 					if (low_voltage_core[tmp->core_id] && Of_cores_num == 0 && Cores_receiver == 0) {
-// 						if ((Cores_receiver + Of_cores_num) == 0) {
-// 							gain_receiver = 1000; //0 sto init_app
-// 						} else if ((Cores_receiver + Of_cores_num) == 1) {
-// 							gain_receiver = 100; //no worker cores -- in case I have only one worker core then I should not be here	
-// 						} else { /* (Cores_receiver + Of_cores_num) > 1 */
-// 							base_receiver = Speedup(req_app, Cores_receiver + Of_cores_num - 1);
-// 							gain_receiver = share_giver * (Speedup(req_app, Cores_receiver + Of_cores_num) - base_receiver); /*  + 1 is ommited due to workload convention */
-// 						}
-// 					
-// 						loss_giver = 0;
-// 						new_gain = gain_receiver - loss_giver;
-// 						gain_total = new_gain;
-// 						GreedyChoice = tmp;//->core_id;
-// 						break;
-// 					#ifdef LOW_VOLTAGE_ISLANDS_4
-// 					} else if (low_voltage_core[tmp->core_id] && Of_cores_num == 0 && Cores_receiver == 1) {
-// 							if (Cores_receiver == 0 && Of_cores_num == 0) gain_receiver = 1000; //0 sto init_app                            
-// 							else gain_receiver = share_giver * (Speedup(req_app, Cores_receiver + Of_cores_num + 1) - base_receiver); /* +1 stands for the possibly offered core */
-// 
-// 							loss_giver = 0;
-// 							new_gain = gain_receiver - loss_giver;
-// 							gain_total = new_gain;
-// 							GreedyChoice = tmp;//->core_id;
-// 							break;
-// 					#endif
-// 					} else if (low_voltage_core[tmp->core_id] == 0) {  
-// 						if ((Cores_receiver + Of_cores_num) == 0) {
-// 							gain_receiver = 1000; //0 sto init_app
-// 						} else if ((Cores_receiver + Of_cores_num) == 1) {
-// 							gain_receiver = 100; //no worker cores -- in case I have only one worker core then I should not be here	
-// 						} else { /* (Cores_receiver + Of_cores_num) > 1 */
-// 							base_receiver = Speedup(req_app, Cores_receiver + Of_cores_num - 1);
-// 							gain_receiver = share_giver * (Speedup(req_app, Cores_receiver + Of_cores_num) - base_receiver); /*  + 1 is ommited due to workload convention */
-// 						}
-// 					
-// 						loss_giver = 0;
-// 						new_gain = gain_receiver - loss_giver;
-// 						if (new_gain > gain_total){
-// 							gain_total = new_gain;
-// 							min_dist = cur_dist;
-// 							GreedyChoice = tmp;//->core_id;
-// 						} else if (new_gain == gain_total && cur_dist < min_dist) {
-// 							//printf("I am %d and i change offer to %d with cores %d->%d with distances %d->%d\n",
-// 							//	node_id,req_id,GreedyChoice->core_id,tmp->core_id,min_dist,cur_dist);
-// 							min_dist = cur_dist;
-// 							GreedyChoice = tmp;
-// 						}
-// 					}	
-// 				}
-// 
-// 				tmp = tmp->next;
-// 			}
-// 
-// 			if (gain_total > 0.0) {
-// 				Offered_cores[Of_cores_num++] = GreedyChoice->core_id;
-// 				GreedyChoice->offered_to = req_id;
-// 			}
-// 		}
-// 		
-// 		/* FFT app requires only power of 2 exec cores plus its manager 
-// 		 * I do not include higher than 5 because it will create no speedup
-// 		 */
-// 		if ((Cores_receiver + Of_cores_num) == 4) {
-// 			for (tmp = my_cores->next; tmp!=NULL; tmp=tmp->next) {
-// 				if (tmp->core_id == Offered_cores[Of_cores_num-1]) {
-// 						fprintf(log_file,"Abandoning offered core %d because FFT needs 2 cores\n",tmp->core_id);
-// 						tmp->offered_to = -1;
-// 						Of_cores_num--;
-// 						break;
-// 				}	
-// 			}	
-// 		} 
-// 		/*
-// 		else if (Of_cores_num > 4) {
-// 			
-// 		}
-// 		*/
-// 	}
-// 	#ifndef GREEDY_MANAGER
-// 	else {
-// 		
-// 		if (((Workers_giver == 4) && (Cores_receiver < 3)) || ((Workers_giver == 2) && (Cores_receiver < 2))) {
-// 		
-// 			while (gain_total > 0.0) {
-// 				gain_total = 0.0;
-// 				GreedyChoice = NULL;//-1;
-// 				min_dist = -1;
-// 				base_giver = Speedup(my_app, Workers_giver - Of_cores_num);
-// 			
-// 				tmp = cores->next->next;//very important!!! that way i avoid giving up my only working core
-// 		
-// 				while (tmp != NULL) {
-// 					if (core_inter_head[tmp->core_id] != NULL && 
-// 						(core_inter_head[tmp->core_id]->type == INIT_WORK_NODE_PENDING || core_inter_head[tmp->core_id]->type == INIT_WORK_NODE)) { 
-// 						fprintf(log_file,"Core %d is about to start work type = %d\n",tmp->core_id,core_inter_head[tmp->core_id]->type);
-// 						fflush(log_file);	
-// 						tmp = tmp->next;
-// 					} else {
-// 						cur_dist = distance(req_reg.C, tmp->core_id);
-// 						if (tmp->offered_to == -1 && cur_dist <= req_reg.r) {
-// 							if ((Cores_receiver + Of_cores_num) == 0) {
-// 								gain_receiver = 1000; //0 sto init_app
-// 							} else if ((Cores_receiver + Of_cores_num) == 1) {
-// 								gain_receiver = 100; //no worker cores -- in case I have only one worker core then I should not be here	
-// 							} else { /* (Cores_receiver + Of_cores_num) > 1 */
-// 								base_receiver = Speedup(req_app, Cores_receiver + Of_cores_num - 1);
-// 								gain_receiver = share_giver * (Speedup(req_app, Cores_receiver + Of_cores_num) - base_receiver); /*  + 1 is ommited due to workload convention */
-// 							}			
-// 						
-// 							loss_giver = base_giver - Speedup(my_app, Workers_giver - Of_cores_num - 1);
-// 						
-// 							new_gain = gain_receiver - loss_giver;
-// 							if (new_gain > gain_total){
-// 								gain_total = new_gain;
-// 								min_dist = cur_dist;
-// 								GreedyChoice = tmp;//->core_id;
-// 							} else if (new_gain == gain_total && cur_dist < min_dist) {
-// 								//printf("I am %d and i change offer to %d with cores %d->%d with distances %d->%d\n",
-// 								//	node_id,req_id,GreedyChoice->core_id,tmp->core_id,min_dist,cur_dist);
-// 								min_dist = cur_dist;
-// 								GreedyChoice = tmp;
-// 							}
-// 						}
-// 
-// 						tmp = tmp->next;
-// 					}
-// 				}
-// 
-// 				if (gain_total > 0.0) {
-// 					Offered_cores[Of_cores_num++] = GreedyChoice->core_id;
-// 					GreedyChoice->offered_to = req_id;
-// 				}
-// 			}
-// 		
-// 			if ((Cores_receiver + Of_cores_num) == 4) {
-// 				for (tmp = my_cores->next; tmp!=NULL; tmp=tmp->next) {
-// 					if (tmp->core_id == Offered_cores[Of_cores_num-1]) {
-// 							fprintf(log_file,"Abandoning offered core %d because FFT needs 2 cores\n",tmp->core_id);
-// 							tmp->offered_to = -1;
-// 							Of_cores_num--;
-// 							break;
-// 					}	
-// 				}	
-// 			}
-// 			
-// 		}	
-// 	}	
-// 	#endif
-// 
-// 	/* FFT app requires only power of 2 exec cores plus its manager */
-// 	/*
-// 	if (my_idag == -1) {
-// 	
-// 	if (executed_app == FFT) {
-// 		if (Of_cores_num == 3) {
-// 			for (tmp = my_cores->next; tmp!=NULL; tmp=tmp->next) {
-// 				if (tmp->core_id == Offered_cores[Of_cores_num-1]) {
-// 						fprintf(log_file,"Abandoning offered core %d because FFT needs 2 cores\n",tmp->core_id);
-// 						tmp->offered_to = -1;
-// 						Of_cores_num--;
-// 						break;
-// 				}	
-// 			}	
-// 		} else if (Of_cores_num > 4) {
-// 			
-// 		}	
-// 	}	
-// 	*/
-// 	return Of_cores_num;
-// }
-
-void send_next_signal(inter_list *head, int node_num){
-  
-	inter_list *tmp_inter_list = NULL;
-
-	signals_disable();
-	
-	if (head->type == IDAG_FIND_IDAGS ||
-	    head->type == SELFOPT_IDAG_FIND_IDAGS ||
-	    head->type == REP_IDAG_FIND_IDAGS ||
-	    head->type == SELFOPT_IDAG_FIND_IDAGS_PENDING || 
-	    head->type == IDAG_FIND_IDAGS_PENDING){
-	  
-			scc_kill(node_num, SIG_IDAG_FIND_IDAGS, head);
-			
-	}else if (head->type == IDAG_REQ_DDS ||
-		  head->type == SELFOPT_IDAG_REQ_DDS ||
-		  head->type == DEBUG_IDAG_REQ_DDS ||
-		  head->type == SELFOPT_IDAG_REQ_DDS_PENDING ||
-		  head->type == IDAG_REQ_DDS_PENDING){
-	  
-			scc_kill(node_num, SIG_REQ_DDS, head);
-			
-	}else if (head->type == AGENT_REQ_CORES ||
-		  head->type == SELFOPT_REQ_CORES ||
-		  head->type == AGENT_REQ_CORES_PENDING ||
-		  head->type == SELFOPT_REQ_CORES_PENDING){
-	  
-			scc_kill(node_num, SIG_REQ_CORES, head);
-	}else if (head->type == IDAG_ADD_CORES_DDS){
-	  
-			scc_kill(node_num, SIG_ADD_CORES_DDS, head);
-			
-	}else if (head->type == IDAG_REM_CORES_DDS){
-	  
-			scc_kill(node_num, SIG_REM_CORES_DDS, head);
-			
-	}else if (head->type == INIT_WORK_NODE ||
-		  head->type == APPOINT_WORK_NODE){
-	  
-			scc_kill(node_num, SIG_APPOINT_WORK, head);
-			
-	}else if (head->type == REMOVE_APP){
-	  
-			scc_kill(node_num, SIG_FINISH, head);
-			
-	}else if (head->type == INIT_APP){
-	  
-			scc_kill(node_num, SIG_INIT_APP, head);
-			
-	}else if (head->type == REP_AGENT_REQ_CORES){
-	  
-			scc_kill(node_num, SIG_REQ_CORES, head);
-			
-	}else if (head->type == INIT_AGENT){
-	  
-			scc_kill(node_num, SIG_INIT_AGENT, head);
-	
-	}else if (head->type == APPOINT_WORK_NODE_PENDING){
-
-			fprintf(log_file,"\nI have unpredictable interaction with node %d with interaction = %d\n",node_num,head->type);
-			
-			tmp_inter_list = core_inter_head[node_num];
-			core_inter_head[node_num] = core_inter_head[node_num]->next;
-			
-			if (core_inter_head[node_num] == NULL){
-				core_inter_tail[node_num] = NULL;
-			}else{
-				send_next_signal(core_inter_head[node_num], node_num);
-			}
-			free(tmp_inter_list);
-			
-	}else if (head->type == DECLARE_INIT_AVAILABILITY) {
-		scc_kill(node_num, SIG_INIT_APP,head);
-		tmp_inter_list = core_inter_head[node_num];
-		core_inter_head[node_num] = core_inter_head[node_num]->next;
-		if (core_inter_head[node_num] == NULL) core_inter_tail[node_num] = NULL;
-		else send_next_signal(core_inter_head[node_num], node_num);
-		free(tmp_inter_list);
-	}
-	my_stats.msg_count++;
-	my_stats.distance += distance(node_id,node_num);
-	
-	signals_enable();
-}

+ 0 - 3
my_rtrm.h

@@ -16,9 +16,6 @@
 #define MAX_SIGNAL_LIST_LEN 256
 #endif
 
-int offer_cores(core_list *cores, app req_app, region req_reg, int *Offered_cores, int req_id);
-void send_next_signal(inter_list *head, int node_num);
-
 int PREPARE_ACCEPT_SENT;
 int CORE_HAS_ACCEPTED_VALUE;
 int SIG_LEARN_SENT;

+ 2 - 0
noc_functions.h

@@ -1,6 +1,8 @@
 #ifndef __NOC_FUNCTIONS_H__
 #define __NOC_FUNCTIONS_H__
 
+#include "variables.h"
+
 int distance(int core_start, int core_fin);
 void get_reg_idags(region cur_reg, int *region_idags);
 int region_count(region cur_reg);

+ 959 - 0
resource_negotiation.c

@@ -0,0 +1,959 @@
+#include "resource_negotiation.h"
+#include "noc_functions.h"
+#include "apps.h"
+
+extern app my_app;
+int offer_cores_original(core_list *cores, app req_app, region req_reg, int *Offered_cores, int req_id);
+int offer_cores_updated(core_list *cores, app req_app, region req_reg, int *Offered_cores, int req_id);
+int offer_cores_fft_original(core_list *cores, app req_app, region req_reg, int *Offered_cores, int req_id);
+
+int offer_cores(core_list *cores, app req_app, region req_reg, int *Offered_cores, int req_id) {
+
+#ifdef RESOURCE_ALGO_ORIG
+	if (executed_app != FFT) {
+		return offer_cores_original(cores, req_app, req_reg, Offered_cores, req_id);
+	} else {
+		return offer_cores_fft_original(cores, req_app, req_reg, Offered_cores, req_id);
+	}
+#elif RESOURCE_ALGO_UPDATED
+	return offer_cores_updated(cores, req_app, req_reg, Offered_cores, req_id);
+#else
+	return offer_cores_original(cores, req_app, req_reg, Offered_cores, req_id);
+#endif
+}
+
+/* Speedup is calculated correclty inside Speedup function */
+int offer_cores_original(core_list *cores, app req_app, region req_reg, int *Offered_cores, int req_id) {
+        int Of_cores_num=0, min_dist=0, cur_dist=0;
+        float gain_total=0.1, base_receiver=0.0, base_giver=0.0, gain_receiver=0.0, loss_giver=0.0, share_giver=0.0, new_gain=0.0;
+        int Cores_receiver = req_app.num_of_cores, Cores_giver = my_app.num_of_cores;
+        core_list *tmp, *GreedyChoice;
+        int offered_cnt=0, counted_cores=0;
+
+        for (tmp=cores; tmp!=NULL; tmp=tmp->next) {
+                if (distance(req_reg.C, tmp->core_id) <= req_reg.r) share_giver++;
+                counted_cores++;
+                if (tmp->offered_to != -1) offered_cnt++;
+                fprintf(log_file,"Core %d is offered to %d\n",tmp->core_id,tmp->offered_to);
+        }
+        fprintf(log_file,"Proceeding\n");
+        fflush(log_file);
+
+        if (offered_cnt == (counted_cores-2) && my_idag != -1) {
+                fprintf(log_file,"I did not give up my only not offered core\n");
+                fflush(log_file);
+                return 0;
+        }
+
+        share_giver = share_giver / (float) region_count(req_reg);
+
+        if (my_idag == -1) {
+                while (gain_total > 0.0) {
+                        gain_total = 0.0;
+                        GreedyChoice = NULL;//-1;
+                        min_dist = -1;
+                        base_giver = 0;
+                        tmp = cores->next;//very important!!! that way i avoid giving up my agent core
+
+                        while (tmp != NULL) {
+                                cur_dist = distance(req_reg.C, tmp->core_id);
+                                if (tmp->offered_to == -1 && cur_dist <= req_reg.r) {
+                                        //Of_cores_num == 0 to be the first offered core
+                                        //Cores_receiver == 0 to avoid providing the core to an non-initial core search
+                                        if (low_voltage_core[tmp->core_id] && Of_cores_num == 0 && Cores_receiver == 0) {
+                                                if ((Cores_receiver + Of_cores_num) == 0) {
+                                                        gain_receiver = 1000; //0 sto init_app
+                                                } else if ((Cores_receiver + Of_cores_num) == 1) {
+                                                        gain_receiver = 100; //no worker cores  
+                                                } else { /* (Cores_receiver + Of_cores_num) > 1 */
+                                                        base_receiver = Speedup(req_app, Cores_receiver + Of_cores_num);
+                                                        //gain_receiver = share_giver * (Speedup(req_app, Cores_receiver + Of_cores_num + 1) - base_receiver); 
+                                                        gain_receiver = share_giver * (Speedup(req_app, Cores_receiver + Of_cores_num + 1) - base_receiver);
+                                                }
+
+                                                loss_giver = 0;
+                                                new_gain = gain_receiver - loss_giver;
+                                                gain_total = new_gain;
+                                                GreedyChoice = tmp;//->core_id;
+                                                break;
+					#ifdef LOW_VOLTAGE_ISLANDS_4
+                                        } else if (low_voltage_core[tmp->core_id] && Of_cores_num == 0 && Cores_receiver == 1) {
+                                                        if (Cores_receiver == 0 && Of_cores_num == 0) gain_receiver = 1000; //0 sto init_app                            
+                                                        else gain_receiver = share_giver * (Speedup(req_app, Cores_receiver + Of_cores_num + 1) - base_receiver);
+
+                                                        loss_giver = 0;
+
+                                                        new_gain = gain_receiver - loss_giver;
+                                                        gain_total = new_gain;
+                                                        GreedyChoice = tmp;//->core_id;
+                                                        break;
+                                        #endif
+                                        } else if (low_voltage_core[tmp->core_id] == 0) {
+                                                if ((Cores_receiver + Of_cores_num) == 0) {
+                                                        gain_receiver = 1000; //0 sto init_app
+                                                } else if ((Cores_receiver + Of_cores_num) == 1) {
+                                                        gain_receiver = 100; //no worker cores  
+                                                } else { /* (Cores_receiver + Of_cores_num) > 1 */
+                                                        base_receiver = Speedup(req_app, Cores_receiver + Of_cores_num);
+                                                        gain_receiver = share_giver * (Speedup(req_app, Cores_receiver + Of_cores_num + 1) - base_receiver);
+                                                }
+
+                                                loss_giver = 0;
+                                                new_gain = gain_receiver - loss_giver;
+                                                if (new_gain > gain_total){
+                                                        gain_total = new_gain;
+                                                        min_dist = cur_dist;
+                                                        GreedyChoice = tmp;//->core_id;
+                                                } else if (new_gain == gain_total && cur_dist < min_dist) {
+                                                        //printf("I am %d and i change offer to %d with cores %d->%d with distances %d->%d\n",
+                                                        //      node_id,req_id,GreedyChoice->core_id,tmp->core_id,min_dist,cur_dist);
+                                                        min_dist = cur_dist;
+                                                        GreedyChoice = tmp;
+                                                }
+                                        }
+                                }
+
+                                tmp = tmp->next;
+                        }
+
+                        if (gain_total > 0.0) {
+                                Offered_cores[Of_cores_num++] = GreedyChoice->core_id;
+                                GreedyChoice->offered_to = req_id;
+                        }
+                }
+        }
+	#ifndef GREEDY_MANAGER
+        else {
+                while (gain_total > 0.0) {
+                        gain_total = 0.0;
+                        GreedyChoice = NULL;//-1;
+                        min_dist = -1;
+                        base_giver = Speedup(my_app, Cores_giver - Of_cores_num);
+
+                        tmp = cores->next->next;//very important!!! that way i avoid giving up my only working core
+
+                        while (tmp != NULL) {
+                                if (core_inter_head[tmp->core_id] != NULL &&
+                                        (core_inter_head[tmp->core_id]->type == INIT_WORK_NODE_PENDING || core_inter_head[tmp->core_id]->type == INIT_WORK_NODE)) {
+                                        fprintf(log_file,"Core %d is about to start work type = %d\n",tmp->core_id,core_inter_head[tmp->core_id]->type);
+                                        fflush(log_file);
+                                        tmp = tmp->next;
+                                } else {
+                                        cur_dist = distance(req_reg.C, tmp->core_id);
+                                        if (tmp->offered_to == -1 && cur_dist <= req_reg.r) {
+                                                if ((Cores_receiver + Of_cores_num) == 0) {
+                                                        gain_receiver = 1000; //0 sto init_app
+                                                } else if ((Cores_receiver + Of_cores_num) == 1) {
+                                                        gain_receiver = 100; //no worker cores -- in case I have only one worker core then I should not be here 
+                                                } else { /* (Cores_receiver + Of_cores_num) > 1 */
+                                                        base_receiver = Speedup(req_app, Cores_receiver + Of_cores_num);
+                                                        gain_receiver = share_giver * (Speedup(req_app, Cores_receiver + Of_cores_num + 1) - base_receiver); /*  + 1 is ommited due to workload convention */
+                                                }
+
+                                                loss_giver = base_giver - Speedup(my_app, Cores_giver - (Of_cores_num + 1));
+
+                                                new_gain = gain_receiver - loss_giver;
+                                                if (new_gain > gain_total){
+                                                        gain_total = new_gain;
+                                                        min_dist = cur_dist;
+                                                        GreedyChoice = tmp;//->core_id;
+                                                } else if (new_gain == gain_total && cur_dist < min_dist) {
+                                                        //printf("I am %d and i change offer to %d with cores %d->%d with distances %d->%d\n",
+                                                        //      node_id,req_id,GreedyChoice->core_id,tmp->core_id,min_dist,cur_dist);
+                                                        min_dist = cur_dist;
+                                                        GreedyChoice = tmp;
+                                                }
+                                        }
+
+                                        tmp = tmp->next;
+                                }
+                        }
+
+                        if (gain_total > 0.0) {
+                                Offered_cores[Of_cores_num++] = GreedyChoice->core_id;
+                                GreedyChoice->offered_to = req_id;
+                        }
+                }
+        }
+        #endif
+        fprintf(log_file,"I will offer %d cores\n",Of_cores_num);
+        fflush(log_file);
+        return Of_cores_num;
+}
+       
+
+/* Speedup is calculated correclty inside Speedup function */
+int offer_cores_updated(core_list *cores, app req_app, region req_reg, int *Offered_cores, int req_id) {
+	int Of_cores_num=0, min_dist=0, cur_dist=0, offered_cnt=0, counted_cores=0;
+	float gain_total=0.1, base_receiver=0.0, base_giver=0.0, gain_receiver=0.0, loss_giver=0.0, new_gain=0.0;
+	int Cores_receiver = req_app.num_of_cores, Cores_giver = my_app.num_of_cores;
+	core_list *tmp, *GreedyChoice;
+
+	if (my_idag != -1) {
+		for (tmp=cores; tmp!=NULL; tmp=tmp->next) {
+			counted_cores++;
+			if (tmp->offered_to != -1) offered_cnt++;
+			fprintf(log_file,"Core %d is offered to %d\n",tmp->core_id,tmp->offered_to);	
+		}
+
+		if (offered_cnt == (counted_cores-2)) {
+			fprintf(log_file,"I did not give up my only not offered core\n");
+			fflush(log_file);	
+			return 0;
+		}
+	}
+
+	if (my_idag == -1) {
+		while (gain_total > 0.0) {
+			gain_total = 0.0;
+			GreedyChoice = NULL;//-1;
+			min_dist = -1;
+			base_giver = 0; 		
+			tmp = cores->next;//very important!!! that way i avoid giving up my agent core
+			
+			while (tmp != NULL) {
+				cur_dist = distance(req_reg.C, tmp->core_id);
+				if (tmp->offered_to == -1 && cur_dist <= req_reg.r) {
+					//Of_cores_num == 0 to be the first offered core
+					//Cores_receiver == 0 to avoid providing the core to an non-initial core search
+					if (low_voltage_core[tmp->core_id] && Of_cores_num == 0 && Cores_receiver == 0) {
+						if ((Cores_receiver + Of_cores_num) == 0) {
+							gain_receiver = 1000; //0 sto init_app
+						} else if ((Cores_receiver + Of_cores_num) == 1) {
+							gain_receiver = 100; //no worker cores	
+						} else { /* (Cores_receiver + Of_cores_num) > 1 */
+							base_receiver = Speedup(req_app, Cores_receiver + Of_cores_num);
+							gain_receiver = Speedup(req_app, Cores_receiver + Of_cores_num + 1) - base_receiver; 
+						}
+						
+						loss_giver = 0;
+						new_gain = gain_receiver - loss_giver;
+						gain_total = new_gain;
+						GreedyChoice = tmp;//->core_id;
+						break;
+					} else if (low_voltage_core[tmp->core_id] == 0) {  
+						if ((Cores_receiver + Of_cores_num) == 0) {
+							gain_receiver = 1000; //0 sto init_app
+						} else if ((Cores_receiver + Of_cores_num) == 1) {
+							gain_receiver = 100; //no worker cores	
+						} else { /* (Cores_receiver + Of_cores_num) > 1 */
+							base_receiver = Speedup(req_app, Cores_receiver + Of_cores_num);
+							gain_receiver = Speedup(req_app, Cores_receiver + Of_cores_num + 1) - base_receiver;
+						}			
+					
+						loss_giver = 0;
+						new_gain = gain_receiver - loss_giver;
+						if (new_gain > gain_total){
+							gain_total = new_gain;
+							min_dist = cur_dist;
+							GreedyChoice = tmp;
+						} else if (new_gain == gain_total && cur_dist < min_dist) {
+							min_dist = cur_dist;
+							GreedyChoice = tmp;
+						}
+					}	
+				}
+
+				tmp = tmp->next;
+			}
+
+			if (gain_total > 0.0) {
+				Offered_cores[Of_cores_num++] = GreedyChoice->core_id;
+				GreedyChoice->offered_to = req_id;
+			}
+		}
+	}
+	#ifndef GREEDY_MANAGER
+	else {
+		while (gain_total > 0.0) {
+			gain_total = 0.0;
+			GreedyChoice = NULL;//-1;
+			min_dist = -1;
+			base_giver = Speedup(my_app, Cores_giver - Of_cores_num);
+		
+			tmp = cores->next->next;//very important!!! that way i avoid giving up my only working core
+	
+			while (tmp != NULL) {
+				if (core_inter_head[tmp->core_id] != NULL && 
+					(core_inter_head[tmp->core_id]->type == INIT_WORK_NODE_PENDING || core_inter_head[tmp->core_id]->type == INIT_WORK_NODE)) { 
+					fprintf(log_file,"Core %d is about to start work type = %d\n",tmp->core_id,core_inter_head[tmp->core_id]->type);
+					fflush(log_file);	
+					tmp = tmp->next;
+				} else {
+					cur_dist = distance(req_reg.C, tmp->core_id);
+					if (tmp->offered_to == -1 && cur_dist <= req_reg.r) {
+						if ((Cores_receiver + Of_cores_num) == 0) {
+							gain_receiver = 1000; //0 sto init_app
+						} else if ((Cores_receiver + Of_cores_num) == 1) {
+							gain_receiver = 100; //no worker cores -- in case I have only one worker core then I should not be here	
+						} else { /* (Cores_receiver + Of_cores_num) > 1 */
+							base_receiver = Speedup(req_app, Cores_receiver + Of_cores_num);
+							gain_receiver = Speedup(req_app, Cores_receiver + Of_cores_num + 1) - base_receiver; /*  + 1 is ommited due to workload convention */
+						}
+						
+						loss_giver = base_giver - Speedup(my_app, Cores_giver - (Of_cores_num + 1));
+					
+						new_gain = gain_receiver - loss_giver;
+						if (new_gain > gain_total){
+							gain_total = new_gain;
+							min_dist = cur_dist;
+							GreedyChoice = tmp;
+						} else if (new_gain == gain_total && cur_dist < min_dist) {
+							min_dist = cur_dist;
+							GreedyChoice = tmp;
+						}
+					}
+
+					tmp = tmp->next;
+				}
+			}
+
+			if (gain_total > 0.0) {
+				if (get_times(my_app, Cores_giver - (Of_cores_num + 1)) > get_times(req_app, Cores_receiver + Of_cores_num + 1)) { 
+					Offered_cores[Of_cores_num++] = GreedyChoice->core_id;
+					GreedyChoice->offered_to = req_id;
+					fprintf(log_file,"Accepted bargain with giver_times %d receiver_times %d\n",get_times(my_app, Cores_giver - (Of_cores_num + 1)),get_times(req_app, Cores_receiver + Of_cores_num + 1));
+                                        fflush(log_file);
+				} else {
+					gain_total = 0.0;
+					fprintf(log_file,"Abandoning because I will finish earlier!\n");
+					fflush(log_file);
+				}
+			}
+		}
+	}
+	#endif
+
+	fprintf(log_file,"I will offer %d cores\n",Of_cores_num);
+	fflush(log_file);
+	return Of_cores_num;
+}
+
+// int offer_cores_fft(core_list *cores, app req_app, region req_reg, int *Offered_cores, int req_id) {
+// 	int Of_cores_num=0, min_dist=0, cur_dist=0;
+// 	float gain_total=0.1,base_receiver=0.0,base_giver=0.0,gain_receiver=0.0,loss_giver=0.0,share_giver=0.0,new_gain=0.0;
+// 	int Cores_receiver = req_app.num_of_cores, Workers_giver = my_app.num_of_cores-1;
+// 	core_list *tmp, *GreedyChoice;
+// 	int offered_cnt=0, counted_cores=0;
+// 
+// 	for (tmp=cores; tmp!=NULL; tmp=tmp->next) {
+// 		if (distance(req_reg.C, tmp->core_id) <= req_reg.r) share_giver++;
+// 		counted_cores++;
+// 		if (tmp->offered_to != -1) offered_cnt++;
+// 		fprintf(log_file,"Core %d is offered to %d\n",tmp->core_id,tmp->offered_to);	
+// 	}
+// 	fflush(log_file);
+// 
+// 	if (offered_cnt == (counted_cores-2) && my_idag != -1) {
+// 		fprintf(log_file,"I did not give up my only not offered core\n");
+// 		fflush(log_file);	
+// 		return 0;
+// 	}
+// 	share_giver = share_giver / (float) region_count(req_reg);
+// 
+// 	if (my_idag == -1) {
+// 		while (gain_total > 0.0) {
+// 			gain_total = 0.0;
+// 			GreedyChoice = NULL;//-1;
+// 			min_dist = -1;
+// 			base_giver = 0; 		
+// 			tmp = cores->next;//very important!!! that way i avoid giving up my agent core
+// 			
+// 			while (tmp != NULL) {
+// 				cur_dist = distance(req_reg.C, tmp->core_id);
+// 				if (tmp->offered_to == -1 && cur_dist <= req_reg.r) {
+// 					//Of_cores_num == 0 to be the first offered core
+// 					//Cores_receiver == 0 to avoid providing the core to an non-initial core search
+// 					if (low_voltage_core[tmp->core_id] && Of_cores_num == 0 && Cores_receiver == 0) {
+// 						if ((Cores_receiver + Of_cores_num) == 0) {
+// 							gain_receiver = 1000; //0 sto init_app
+// 						} else if ((Cores_receiver + Of_cores_num) == 1) {
+// 							gain_receiver = 100; //no worker cores -- in case I have only one worker core then I should not be here	
+// 						} else { /* (Cores_receiver + Of_cores_num) > 1 */
+// 							base_receiver = Speedup(req_app, Cores_receiver + Of_cores_num - 1);
+// 							gain_receiver = share_giver * (Speedup(req_app, Cores_receiver + Of_cores_num) - base_receiver); /*  + 1 is ommited due to workload convention */
+// 						}
+// 					
+// 						loss_giver = 0;
+// 						new_gain = gain_receiver - loss_giver;
+// 						gain_total = new_gain;
+// 						GreedyChoice = tmp;//->core_id;
+// 						break;
+// 					#ifdef LOW_VOLTAGE_ISLANDS_4
+// 					} else if (low_voltage_core[tmp->core_id] && Of_cores_num == 0 && Cores_receiver == 1) {
+// 							if (Cores_receiver == 0 && Of_cores_num == 0) gain_receiver = 1000; //0 sto init_app                            
+// 							else gain_receiver = share_giver * (Speedup(req_app, Cores_receiver + Of_cores_num + 1) - base_receiver); /* +1 stands for the possibly offered core */
+// 
+// 							loss_giver = 0;
+// 							new_gain = gain_receiver - loss_giver;
+// 							gain_total = new_gain;
+// 							GreedyChoice = tmp;//->core_id;
+// 							break;
+// 					#endif
+// 					} else if (low_voltage_core[tmp->core_id] == 0) {  
+// 						if ((Cores_receiver + Of_cores_num) == 0) {
+// 							gain_receiver = 1000; //0 sto init_app
+// 						} else if ((Cores_receiver + Of_cores_num) == 1) {
+// 							gain_receiver = 100; //no worker cores -- in case I have only one worker core then I should not be here	
+// 						} else { /* (Cores_receiver + Of_cores_num) > 1 */
+// 							base_receiver = Speedup(req_app, Cores_receiver + Of_cores_num - 1);
+// 							gain_receiver = share_giver * (Speedup(req_app, Cores_receiver + Of_cores_num) - base_receiver); /*  + 1 is ommited due to workload convention */
+// 						}
+// 					
+// 						loss_giver = 0;
+// 						new_gain = gain_receiver - loss_giver;
+// 						if (new_gain > gain_total){
+// 							gain_total = new_gain;
+// 							min_dist = cur_dist;
+// 							GreedyChoice = tmp;//->core_id;
+// 						} else if (new_gain == gain_total && cur_dist < min_dist) {
+// 							//printf("I am %d and i change offer to %d with cores %d->%d with distances %d->%d\n",
+// 							//	node_id,req_id,GreedyChoice->core_id,tmp->core_id,min_dist,cur_dist);
+// 							min_dist = cur_dist;
+// 							GreedyChoice = tmp;
+// 						}
+// 					}	
+// 				}
+// 
+// 				tmp = tmp->next;
+// 			}
+// 
+// 			if (gain_total > 0.0) {
+// 				Offered_cores[Of_cores_num++] = GreedyChoice->core_id;
+// 				GreedyChoice->offered_to = req_id;
+// 			}
+// 		}
+// 		
+// 		/* FFT app requires only power of 2 exec cores plus its manager 
+// 		 * I do not include higher than 5 because it will create no speedup
+// 		 */
+// 		if ((Cores_receiver + Of_cores_num) == 4) {
+// 			for (tmp = my_cores->next; tmp!=NULL; tmp=tmp->next) {
+// 				if (tmp->core_id == Offered_cores[Of_cores_num-1]) {
+// 						fprintf(log_file,"Abandoning offered core %d because FFT needs 2 cores\n",tmp->core_id);
+// 						tmp->offered_to = -1;
+// 						Of_cores_num--;
+// 						break;
+// 				}	
+// 			}	
+// 		} 
+// 		/*
+// 		else if (Of_cores_num > 4) {
+// 			
+// 		}
+// 		*/
+// 	}
+// 	#ifndef GREEDY_MANAGER
+// 	else {
+// 		
+// 		if (((Workers_giver == 4) && (Cores_receiver < 3)) || ((Workers_giver == 2) && (Cores_receiver < 2))) {
+// 		
+// 			while (gain_total > 0.0) {
+// 				gain_total = 0.0;
+// 				GreedyChoice = NULL;//-1;
+// 				min_dist = -1;
+// 				base_giver = Speedup(my_app, Workers_giver - Of_cores_num);
+// 			
+// 				tmp = cores->next->next;//very important!!! that way i avoid giving up my only working core
+// 		
+// 				while (tmp != NULL) {
+// 					if (core_inter_head[tmp->core_id] != NULL && 
+// 						(core_inter_head[tmp->core_id]->type == INIT_WORK_NODE_PENDING || core_inter_head[tmp->core_id]->type == INIT_WORK_NODE)) { 
+// 						fprintf(log_file,"Core %d is about to start work type = %d\n",tmp->core_id,core_inter_head[tmp->core_id]->type);
+// 						fflush(log_file);	
+// 						tmp = tmp->next;
+// 					} else {
+// 						cur_dist = distance(req_reg.C, tmp->core_id);
+// 						if (tmp->offered_to == -1 && cur_dist <= req_reg.r) {
+// 							if ((Cores_receiver + Of_cores_num) == 0) {
+// 								gain_receiver = 1000; //0 sto init_app
+// 							} else if ((Cores_receiver + Of_cores_num) == 1) {
+// 								gain_receiver = 100; //no worker cores -- in case I have only one worker core then I should not be here	
+// 							} else { /* (Cores_receiver + Of_cores_num) > 1 */
+// 								base_receiver = Speedup(req_app, Cores_receiver + Of_cores_num - 1);
+// 								gain_receiver = share_giver * (Speedup(req_app, Cores_receiver + Of_cores_num) - base_receiver); /*  + 1 is ommited due to workload convention */
+// 							}			
+// 						
+// 							loss_giver = base_giver - Speedup(my_app, Workers_giver - Of_cores_num - 1);
+// 						
+// 							new_gain = gain_receiver - loss_giver;
+// 							if (new_gain > gain_total){
+// 								gain_total = new_gain;
+// 								min_dist = cur_dist;
+// 								GreedyChoice = tmp;//->core_id;
+// 							} else if (new_gain == gain_total && cur_dist < min_dist) {
+// 								//printf("I am %d and i change offer to %d with cores %d->%d with distances %d->%d\n",
+// 								//	node_id,req_id,GreedyChoice->core_id,tmp->core_id,min_dist,cur_dist);
+// 								min_dist = cur_dist;
+// 								GreedyChoice = tmp;
+// 							}
+// 						}
+// 
+// 						tmp = tmp->next;
+// 					}
+// 				}
+// 
+// 				if (gain_total > 0.0) {
+// 					Offered_cores[Of_cores_num++] = GreedyChoice->core_id;
+// 					GreedyChoice->offered_to = req_id;
+// 				}
+// 			}
+// 		
+// 			if ((Cores_receiver + Of_cores_num) == 4) {
+// 				for (tmp = my_cores->next; tmp!=NULL; tmp=tmp->next) {
+// 					if (tmp->core_id == Offered_cores[Of_cores_num-1]) {
+// 							fprintf(log_file,"Abandoning offered core %d because FFT needs 2 cores\n",tmp->core_id);
+// 							tmp->offered_to = -1;
+// 							Of_cores_num--;
+// 							break;
+// 					}	
+// 				}	
+// 			}
+// 			
+// 		}	
+// 	}	
+// 	#endif
+// 
+// 	/* FFT app requires only power of 2 exec cores plus its manager */
+// 	/*
+// 	if (my_idag == -1) {
+// 	
+// 	if (executed_app == FFT) {
+// 		if (Of_cores_num == 3) {
+// 			for (tmp = my_cores->next; tmp!=NULL; tmp=tmp->next) {
+// 				if (tmp->core_id == Offered_cores[Of_cores_num-1]) {
+// 						fprintf(log_file,"Abandoning offered core %d because FFT needs 2 cores\n",tmp->core_id);
+// 						tmp->offered_to = -1;
+// 						Of_cores_num--;
+// 						break;
+// 				}	
+// 			}	
+// 		} else if (Of_cores_num > 4) {
+// 			
+// 		}	
+// 	}	
+// 	*/
+// 	return Of_cores_num;
+// }
+int offer_cores_fft_original(core_list *cores, app req_app, region req_reg, int *Offered_cores, int req_id) {
+	int Of_cores_num=0, min_dist=0, cur_dist=0;
+	float gain_total=0.1,base_receiver=0.0,base_giver=0.0,gain_receiver=0.0,loss_giver=0.0,share_giver=0.0,new_gain=0.0;
+	int Cores_receiver = req_app.num_of_cores, Workers_giver = my_app.num_of_cores-1;
+	core_list *tmp, *GreedyChoice;
+	int offered_cnt=0, counted_cores=0;
+
+	for (tmp=cores; tmp!=NULL; tmp=tmp->next) {
+		if (distance(req_reg.C, tmp->core_id) <= req_reg.r) share_giver++;
+		counted_cores++;
+		if (tmp->offered_to != -1) offered_cnt++;
+		fprintf(log_file,"Core %d is offered to %d\n",tmp->core_id,tmp->offered_to);	
+	}
+	fflush(log_file);
+
+	if (offered_cnt == (counted_cores-2) && my_idag != -1) {
+		fprintf(log_file,"I did not give up my only not offered core\n");
+		fflush(log_file);	
+		return 0;
+	}
+
+	if (Cores_receiver == 2) {
+		fprintf(log_file,"Receiver already has two cores\n");
+                fflush(log_file);
+                return 0;
+	}
+
+	share_giver = share_giver / (float) region_count(req_reg);
+
+	if (my_idag == -1) {
+		while ((gain_total > 0.0) && ((Cores_receiver + Of_cores_num) <= 1)) {
+			gain_total = 0.0;
+			GreedyChoice = NULL;//-1;
+			min_dist = -1;
+			base_giver = 0; 		
+			tmp = cores->next;//very important!!! that way i avoid giving up my agent core
+			
+			while (tmp != NULL) {
+				cur_dist = distance(req_reg.C, tmp->core_id);
+				if (tmp->offered_to == -1 && cur_dist <= req_reg.r) {
+					//Of_cores_num == 0 to be the first offered core
+					//Cores_receiver == 0 to avoid providing the core to an non-initial core search
+					if (low_voltage_core[tmp->core_id] && Of_cores_num == 0 && Cores_receiver == 0) {
+						if ((Cores_receiver + Of_cores_num) == 0) {
+							gain_receiver = 1000; //0 sto init_app
+						} else if ((Cores_receiver + Of_cores_num) == 1) {
+							gain_receiver = 100; //no worker cores -- in case I have only one worker core then I should not be here	
+						} else { /* (Cores_receiver + Of_cores_num) > 1 */
+							base_receiver = Speedup(req_app, Cores_receiver + Of_cores_num);
+                            				//gain_receiver = share_giver * (Speedup(req_app, Cores_receiver + Of_cores_num + 1) - base_receiver); 
+                            				gain_receiver = share_giver * (Speedup(req_app, Cores_receiver + Of_cores_num + 1) - base_receiver);						
+						}
+					
+						loss_giver = 0;
+						new_gain = gain_receiver - loss_giver;
+						gain_total = new_gain;
+						GreedyChoice = tmp;//->core_id;
+						break;
+					#ifdef LOW_VOLTAGE_ISLANDS_4
+					} else if (low_voltage_core[tmp->core_id] && Of_cores_num == 0 && Cores_receiver == 1) {
+							if (Cores_receiver == 0 && Of_cores_num == 0) gain_receiver = 1000; //0 sto init_app                            
+							else gain_receiver = share_giver * (Speedup(req_app, Cores_receiver + Of_cores_num + 1) - base_receiver); /* +1 stands for the possibly offered core */
+
+							loss_giver = 0;
+							new_gain = gain_receiver - loss_giver;
+							gain_total = new_gain;
+							GreedyChoice = tmp;//->core_id;
+							break;
+					#endif
+					} else if (low_voltage_core[tmp->core_id] == 0) {  
+						if ((Cores_receiver + Of_cores_num) == 0) {
+							gain_receiver = 1000; //0 sto init_app
+						} else if ((Cores_receiver + Of_cores_num) == 1) {
+							gain_receiver = 100; //no worker cores -- in case I have only one worker core then I should not be here	
+						} else { /* (Cores_receiver + Of_cores_num) > 1 */
+							base_receiver = Speedup(req_app, Cores_receiver + Of_cores_num);
+                            				gain_receiver = share_giver * (Speedup(req_app, Cores_receiver + Of_cores_num + 1) - base_receiver);
+						}
+
+						loss_giver = 0;
+						new_gain = gain_receiver - loss_giver;
+						if (new_gain > gain_total){
+							gain_total = new_gain;
+							min_dist = cur_dist;
+							GreedyChoice = tmp;//->core_id;
+						} else if (new_gain == gain_total && cur_dist < min_dist) {
+							//printf("I am %d and i change offer to %d with cores %d->%d with distances %d->%d\n",
+							//	node_id,req_id,GreedyChoice->core_id,tmp->core_id,min_dist,cur_dist);
+							min_dist = cur_dist;
+							GreedyChoice = tmp;
+						}
+					}	
+				}
+
+				tmp = tmp->next;
+			}
+
+			if (gain_total > 0.0) {
+				Offered_cores[Of_cores_num++] = GreedyChoice->core_id;
+				GreedyChoice->offered_to = req_id;
+			}
+		}
+		
+		/* FFT app requires only power of 2 exec cores plus its manager 
+		 * I do not include higher than 5 because it will create no speedup
+		 */
+		if ((Cores_receiver + Of_cores_num) == 4) {
+			for (tmp = my_cores->next; tmp!=NULL; tmp=tmp->next) {
+				if (tmp->core_id == Offered_cores[Of_cores_num-1]) {
+						fprintf(log_file,"Abandoning offered core %d because FFT needs 2 cores\n",tmp->core_id);
+						tmp->offered_to = -1;
+						Of_cores_num--;
+						break;
+				}	
+			}	
+		} 
+		/*
+		else if (Of_cores_num > 4) {
+			
+		}
+		*/
+	}
+	#ifndef GREEDY_MANAGER
+	else {
+		
+		if (((Workers_giver == 4) && (Cores_receiver < 3)) || ((Workers_giver == 2) && (Cores_receiver < 2))) {
+		
+			while (gain_total > 0.0) {
+				gain_total = 0.0;
+				GreedyChoice = NULL;//-1;
+				min_dist = -1;
+				base_giver = Speedup(my_app, Workers_giver - Of_cores_num);
+			
+				tmp = cores->next->next;//very important!!! that way i avoid giving up my only working core
+		
+				while (tmp != NULL) {
+					if (core_inter_head[tmp->core_id] != NULL && 
+						(core_inter_head[tmp->core_id]->type == INIT_WORK_NODE_PENDING || core_inter_head[tmp->core_id]->type == INIT_WORK_NODE)) { 
+						fprintf(log_file,"Core %d is about to start work type = %d\n",tmp->core_id,core_inter_head[tmp->core_id]->type);
+						fflush(log_file);	
+						tmp = tmp->next;
+					} else {
+						cur_dist = distance(req_reg.C, tmp->core_id);
+						if (tmp->offered_to == -1 && cur_dist <= req_reg.r) {
+							if ((Cores_receiver + Of_cores_num) == 0) {
+								gain_receiver = 1000; //0 sto init_app
+							} else if ((Cores_receiver + Of_cores_num) == 1) {
+								gain_receiver = 100; //no worker cores -- in case I have only one worker core then I should not be here	
+							} else { /* (Cores_receiver + Of_cores_num) > 1 */
+								base_receiver = Speedup(req_app, Cores_receiver + Of_cores_num);
+                                                        	gain_receiver = share_giver * (Speedup(req_app, Cores_receiver + Of_cores_num + 1) - base_receiver); /*  + 1 is ommited due to workload convention */
+							}			
+						
+							loss_giver = base_giver - Speedup(my_app, Workers_giver - (Of_cores_num + 1));
+						
+							new_gain = gain_receiver - loss_giver;
+							if (new_gain > gain_total){
+								gain_total = new_gain;
+								min_dist = cur_dist;
+								GreedyChoice = tmp;//->core_id;
+							} else if (new_gain == gain_total && cur_dist < min_dist) {
+								//printf("I am %d and i change offer to %d with cores %d->%d with distances %d->%d\n",
+								//	node_id,req_id,GreedyChoice->core_id,tmp->core_id,min_dist,cur_dist);
+								min_dist = cur_dist;
+								GreedyChoice = tmp;
+							}
+						}
+
+						tmp = tmp->next;
+					}
+				}
+
+				if (gain_total > 0.0) {
+					Offered_cores[Of_cores_num++] = GreedyChoice->core_id;
+					GreedyChoice->offered_to = req_id;
+				}
+			}
+		
+			if ((Cores_receiver + Of_cores_num) == 4) {
+				for (tmp = my_cores->next; tmp!=NULL; tmp=tmp->next) {
+					if (tmp->core_id == Offered_cores[Of_cores_num-1]) {
+							fprintf(log_file,"Abandoning offered core %d because FFT needs 2 cores\n",tmp->core_id);
+							tmp->offered_to = -1;
+							Of_cores_num--;
+							break;
+					}	
+				}	
+			}
+			
+		}	
+	}	
+	#endif
+
+	/* FFT app requires only power of 2 exec cores plus its manager */
+	/*
+	if (my_idag == -1) {
+	
+	if (executed_app == FFT) {
+		if (Of_cores_num == 3) {
+			for (tmp = my_cores->next; tmp!=NULL; tmp=tmp->next) {
+				if (tmp->core_id == Offered_cores[Of_cores_num-1]) {
+						fprintf(log_file,"Abandoning offered core %d because FFT needs 2 cores\n",tmp->core_id);
+						tmp->offered_to = -1;
+						Of_cores_num--;
+						break;
+				}	
+			}	
+		} else if (Of_cores_num > 4) {
+			
+		}	
+	}	
+	*/
+	return Of_cores_num;
+}
+
+int offer_cores_fft_updated(core_list *cores, app req_app, region req_reg, int *Offered_cores, int req_id) { /* 26.5.2017 - I am not sure if it is correct. Has not been used to measure F_min_max0 */
+	int Of_cores_num=0, min_dist=0, cur_dist=0;
+	float gain_total=0.1,base_receiver=0.0,base_giver=0.0,gain_receiver=0.0,loss_giver=0.0,share_giver=0.0,new_gain=0.0;
+	int Cores_receiver = req_app.num_of_cores, Workers_giver = my_app.num_of_cores-1;
+	core_list *tmp, *GreedyChoice;
+	int offered_cnt=0, counted_cores=0;
+
+	for (tmp=cores; tmp!=NULL; tmp=tmp->next) {
+		if (distance(req_reg.C, tmp->core_id) <= req_reg.r) share_giver++;
+		counted_cores++;
+		if (tmp->offered_to != -1) offered_cnt++;
+		fprintf(log_file,"Core %d is offered to %d\n",tmp->core_id,tmp->offered_to);	
+	}
+	fflush(log_file);
+
+	if (offered_cnt == (counted_cores-2) && my_idag != -1) {
+		fprintf(log_file,"I did not give up my only not offered core\n");
+		fflush(log_file);	
+		return 0;
+	}
+	share_giver = share_giver / (float) region_count(req_reg);
+
+	if (my_idag == -1) {
+		while ((gain_total > 0.0) && ((Cores_receiver + Of_cores_num) <= 2)) {
+			gain_total = 0.0;
+			GreedyChoice = NULL;//-1;
+			min_dist = -1;
+			base_giver = 0; 		
+			tmp = cores->next;//very important!!! that way i avoid giving up my agent core
+			
+			while (tmp != NULL) {
+				cur_dist = distance(req_reg.C, tmp->core_id);
+				if (tmp->offered_to == -1 && cur_dist <= req_reg.r) {
+					//Of_cores_num == 0 to be the first offered core
+					//Cores_receiver == 0 to avoid providing the core to an non-initial core search
+					if (low_voltage_core[tmp->core_id] && Of_cores_num == 0 && Cores_receiver == 0) {
+						if ((Cores_receiver + Of_cores_num) == 0) {
+							gain_receiver = 1000; //0 sto init_app
+						} else if ((Cores_receiver + Of_cores_num) == 1) {
+							gain_receiver = 100; //no worker cores -- in case I have only one worker core then I should not be here	
+						} else { /* (Cores_receiver + Of_cores_num) > 1 */
+							base_receiver = Speedup(req_app, Cores_receiver + Of_cores_num);
+                            				//gain_receiver = share_giver * (Speedup(req_app, Cores_receiver + Of_cores_num + 1) - base_receiver); 
+                            				gain_receiver = share_giver * (Speedup(req_app, Cores_receiver + Of_cores_num + 1) - base_receiver);						
+						}
+					
+						loss_giver = 0;
+						new_gain = gain_receiver - loss_giver;
+						gain_total = new_gain;
+						GreedyChoice = tmp;//->core_id;
+						break;
+					#ifdef LOW_VOLTAGE_ISLANDS_4
+					} else if (low_voltage_core[tmp->core_id] && Of_cores_num == 0 && Cores_receiver == 1) {
+							if (Cores_receiver == 0 && Of_cores_num == 0) gain_receiver = 1000; //0 sto init_app                            
+							else gain_receiver = share_giver * (Speedup(req_app, Cores_receiver + Of_cores_num + 1) - base_receiver); /* +1 stands for the possibly offered core */
+
+							loss_giver = 0;
+							new_gain = gain_receiver - loss_giver;
+							gain_total = new_gain;
+							GreedyChoice = tmp;//->core_id;
+							break;
+					#endif
+					} else if (low_voltage_core[tmp->core_id] == 0) {  
+						if ((Cores_receiver + Of_cores_num) == 0) {
+							gain_receiver = 1000; //0 sto init_app
+						} else if ((Cores_receiver + Of_cores_num) == 1) {
+							gain_receiver = 100; //no worker cores -- in case I have only one worker core then I should not be here	
+						} else { /* (Cores_receiver + Of_cores_num) > 1 */
+							base_receiver = Speedup(req_app, Cores_receiver + Of_cores_num);
+                            				gain_receiver = share_giver * (Speedup(req_app, Cores_receiver + Of_cores_num + 1) - base_receiver);
+						}
+
+						loss_giver = 0;
+						new_gain = gain_receiver - loss_giver;
+						if (new_gain > gain_total){
+							gain_total = new_gain;
+							min_dist = cur_dist;
+							GreedyChoice = tmp;//->core_id;
+						} else if (new_gain == gain_total && cur_dist < min_dist) {
+							//printf("I am %d and i change offer to %d with cores %d->%d with distances %d->%d\n",
+							//	node_id,req_id,GreedyChoice->core_id,tmp->core_id,min_dist,cur_dist);
+							min_dist = cur_dist;
+							GreedyChoice = tmp;
+						}
+					}	
+				}
+
+				tmp = tmp->next;
+			}
+
+			if (gain_total > 0.0) {
+				Offered_cores[Of_cores_num++] = GreedyChoice->core_id;
+				GreedyChoice->offered_to = req_id;
+			}
+		}
+		
+		/* FFT app requires only power of 2 exec cores plus its manager 
+		 * I do not include higher than 5 because it will create no speedup
+		 */
+		if ((Cores_receiver + Of_cores_num) == 4) {
+			for (tmp = my_cores->next; tmp!=NULL; tmp=tmp->next) {
+				if (tmp->core_id == Offered_cores[Of_cores_num-1]) {
+						fprintf(log_file,"Abandoning offered core %d because FFT needs 2 cores\n",tmp->core_id);
+						tmp->offered_to = -1;
+						Of_cores_num--;
+						break;
+				}	
+			}	
+		} 
+		/*
+		else if (Of_cores_num > 4) {
+			
+		}
+		*/
+	}
+	#ifndef GREEDY_MANAGER
+	else {
+		
+		if (((Workers_giver == 4) && (Cores_receiver < 3)) || ((Workers_giver == 2) && (Cores_receiver < 2))) {
+		
+			while (gain_total > 0.0) {
+				gain_total = 0.0;
+				GreedyChoice = NULL;//-1;
+				min_dist = -1;
+				base_giver = Speedup(my_app, Workers_giver - Of_cores_num);
+			
+				tmp = cores->next->next;//very important!!! that way i avoid giving up my only working core
+		
+				while (tmp != NULL) {
+					if (core_inter_head[tmp->core_id] != NULL && 
+						(core_inter_head[tmp->core_id]->type == INIT_WORK_NODE_PENDING || core_inter_head[tmp->core_id]->type == INIT_WORK_NODE)) { 
+						fprintf(log_file,"Core %d is about to start work type = %d\n",tmp->core_id,core_inter_head[tmp->core_id]->type);
+						fflush(log_file);	
+						tmp = tmp->next;
+					} else {
+						cur_dist = distance(req_reg.C, tmp->core_id);
+						if (tmp->offered_to == -1 && cur_dist <= req_reg.r) {
+							if ((Cores_receiver + Of_cores_num) == 0) {
+								gain_receiver = 1000; //0 sto init_app
+							} else if ((Cores_receiver + Of_cores_num) == 1) {
+								gain_receiver = 100; //no worker cores -- in case I have only one worker core then I should not be here	
+							} else { /* (Cores_receiver + Of_cores_num) > 1 */
+								base_receiver = Speedup(req_app, Cores_receiver + Of_cores_num);
+								gain_receiver = share_giver * (Speedup(req_app, Cores_receiver + Of_cores_num + 1) - base_receiver); /*  + 1 is ommited due to workload convention */
+							}			
+						
+							loss_giver = base_giver - Speedup(my_app, Workers_giver - (Of_cores_num + 1));
+						
+							new_gain = gain_receiver - loss_giver;
+							if (new_gain > gain_total){
+								gain_total = new_gain;
+								min_dist = cur_dist;
+								GreedyChoice = tmp;//->core_id;
+							} else if (new_gain == gain_total && cur_dist < min_dist) {
+								//printf("I am %d and i change offer to %d with cores %d->%d with distances %d->%d\n",
+								//	node_id,req_id,GreedyChoice->core_id,tmp->core_id,min_dist,cur_dist);
+								min_dist = cur_dist;
+								GreedyChoice = tmp;
+							}
+						}
+
+						tmp = tmp->next;
+					}
+				}
+
+				if (gain_total > 0.0) {
+					Offered_cores[Of_cores_num++] = GreedyChoice->core_id;
+					GreedyChoice->offered_to = req_id;
+				}
+			}
+		
+			if ((Cores_receiver + Of_cores_num) == 4) {
+				for (tmp = my_cores->next; tmp!=NULL; tmp=tmp->next) {
+					if (tmp->core_id == Offered_cores[Of_cores_num-1]) {
+							fprintf(log_file,"Abandoning offered core %d because FFT needs 2 cores\n",tmp->core_id);
+							tmp->offered_to = -1;
+							Of_cores_num--;
+							break;
+					}	
+				}	
+			}
+			
+		}	
+	}	
+	#endif
+
+	/* FFT app requires only power of 2 exec cores plus its manager */
+	/*
+	if (my_idag == -1) {
+	
+	if (executed_app == FFT) {
+		if (Of_cores_num == 3) {
+			for (tmp = my_cores->next; tmp!=NULL; tmp=tmp->next) {
+				if (tmp->core_id == Offered_cores[Of_cores_num-1]) {
+						fprintf(log_file,"Abandoning offered core %d because FFT needs 2 cores\n",tmp->core_id);
+						tmp->offered_to = -1;
+						Of_cores_num--;
+						break;
+				}	
+			}	
+		} else if (Of_cores_num > 4) {
+			
+		}	
+	}	
+	*/
+	return Of_cores_num;
+}

+ 7 - 0
resource_negotiation.h

@@ -0,0 +1,7 @@
+#ifndef __RESOURCE_NEGOTIATION_H__
+#define __RESOURCE_NEGOTIATION_H__
+
+#include "structs.h"
+
+int offer_cores(core_list *cores, app req_app, region req_reg, int *Offered_cores, int req_id);
+#endif

+ 125 - 112
signal_handlers.c

@@ -8,7 +8,7 @@
 #include "idag_defs.h"
 #include "apps.h"
 #include "macros.h"
-
+#include "resource_negotiation.h"
 #include "variables.h"
 
 int init_DDS_replies;
@@ -33,6 +33,7 @@ target_list *selfopt_targets_tail;
 
 agent_info pending_agent;
 
+void send_next_signal(inter_list *head, int node_num);
 
 #ifdef PLAT_LINUX
 void new_RCCE_get(int *target, int *src, int index, int num_of_ints, int ID) {
@@ -184,6 +185,91 @@ void trigger_shit(int failed_core){
 	}
 }
 
+void send_next_signal(inter_list *head, int node_num){
+  
+	inter_list *tmp_inter_list = NULL;
+
+	/* signals_disable(); */ /* Commented out on 5.5.2017 */
+	
+	if (head->type == IDAG_FIND_IDAGS ||
+	    head->type == SELFOPT_IDAG_FIND_IDAGS ||
+	    head->type == REP_IDAG_FIND_IDAGS ||
+	    head->type == SELFOPT_IDAG_FIND_IDAGS_PENDING || 
+	    head->type == IDAG_FIND_IDAGS_PENDING){
+	  
+			scc_kill(node_num, SIG_IDAG_FIND_IDAGS, head);
+			
+	}else if (head->type == IDAG_REQ_DDS ||
+		  head->type == SELFOPT_IDAG_REQ_DDS ||
+		  head->type == DEBUG_IDAG_REQ_DDS ||
+		  head->type == SELFOPT_IDAG_REQ_DDS_PENDING ||
+		  head->type == IDAG_REQ_DDS_PENDING){
+	  
+			scc_kill(node_num, SIG_REQ_DDS, head);
+			
+	}else if (head->type == AGENT_REQ_CORES ||
+		  head->type == SELFOPT_REQ_CORES ||
+		  head->type == AGENT_REQ_CORES_PENDING ||
+		  head->type == SELFOPT_REQ_CORES_PENDING){
+	  
+			scc_kill(node_num, SIG_REQ_CORES, head);
+	}else if (head->type == IDAG_ADD_CORES_DDS){
+	  
+			scc_kill(node_num, SIG_ADD_CORES_DDS, head);
+			
+	}else if (head->type == IDAG_REM_CORES_DDS){
+	  
+			scc_kill(node_num, SIG_REM_CORES_DDS, head);
+			
+	}else if (head->type == INIT_WORK_NODE ||
+		  head->type == APPOINT_WORK_NODE){
+	  
+			scc_kill(node_num, SIG_APPOINT_WORK, head);
+			
+	}else if (head->type == REMOVE_APP){
+	  
+			scc_kill(node_num, SIG_FINISH, head);
+			
+	}else if (head->type == INIT_APP){
+	  
+			scc_kill(node_num, SIG_INIT_APP, head);
+			
+	}else if (head->type == REP_AGENT_REQ_CORES){
+	  
+			scc_kill(node_num, SIG_REQ_CORES, head);
+			
+	}else if (head->type == INIT_AGENT){
+	  
+			scc_kill(node_num, SIG_INIT_AGENT, head);
+	
+	}else if (head->type == APPOINT_WORK_NODE_PENDING){
+
+			fprintf(log_file,"\nI have unpredictable interaction with node %d with interaction = %d\n",node_num,head->type);
+			
+			tmp_inter_list = core_inter_head[node_num];
+			core_inter_head[node_num] = core_inter_head[node_num]->next;
+			
+			if (core_inter_head[node_num] == NULL){
+				core_inter_tail[node_num] = NULL;
+			}else{
+				send_next_signal(core_inter_head[node_num], node_num);
+			}
+			free(tmp_inter_list);
+			
+	}else if (head->type == DECLARE_INIT_AVAILABILITY) {
+		scc_kill(node_num, SIG_INIT_APP,head);
+		tmp_inter_list = core_inter_head[node_num];
+		core_inter_head[node_num] = core_inter_head[node_num]->next;
+		if (core_inter_head[node_num] == NULL) core_inter_tail[node_num] = NULL;
+		else send_next_signal(core_inter_head[node_num], node_num);
+		free(tmp_inter_list);
+	}
+	my_stats.msg_count++;
+	my_stats.distance += distance(node_id,node_num);
+	
+	/* signals_enable(); */ /* Commented out on 5.5.2017 */
+}
+
 void sig_TERMINATE_handler(int sender_id){
 
 	inter_list *tmp_inter_list;
@@ -610,6 +696,7 @@ void sig_ACK_handler(int sender_id){
 			fprintf(log_file, "\t\tnode_id=%d\n",node_id);
 			my_stats.message_size += sizeof(int);
 			nodes_initialised++;
+			fprintf(log_file, "\t\tnodes_initialised=%d\n",nodes_initialised);
 		} else if (tmp_inter_list->type == INIT_APP) {
 			gettimeofday(&time_val_ack, NULL);
 			cur_t_ack = localtime(&time_val_ack.tv_sec);
@@ -1741,9 +1828,14 @@ void sig_REQ_CORES_handler(int sender_id){
 				non_zero_offers++;
 				fprintf(log_file,"I offered %d cores with spd_loss = %0.2f and %0.2f gain for the req_app\n",tmp_int,off_arr.offer_arr[i].spd_loss,req_gain);
 				fflush(log_file);
-				fprintf(app_log_file,"I offered %d cores with spd_loss = %0.2f and %0.2f gain for the req_app\n"
-					,tmp_int,off_arr.offer_arr[i].spd_loss,req_gain);
-				fflush(app_log_file);
+				if (app_log_file != NULL) { /* Added on 5.5.2017 */
+					fprintf(app_log_file,"I offered %d cores with spd_loss = %0.2f and %0.2f gain for the req_app\n"
+						,tmp_int,off_arr.offer_arr[i].spd_loss,req_gain);
+					fflush(app_log_file);
+				} else {
+					fprintf(log_file,"App log file is NULL\n");
+                                	fflush(log_file);
+				}
 			}
 		} else {
 			off_arr.offer_arr[i].num_of_cores = 0;
@@ -2839,7 +2931,7 @@ void sig_REM_CORES_DDS_handler(int sender_id, int *inc_cnt, int cur_index_top){
 				if (tmp_DDS == NULL) printf("Agent does not exist in my DDS\n");
 				else if (tmp_DDS == DDS) printf("I am removing from myself in REM?\n");
 				else {
-					tmp_DDS->num_of_cores -= nodes_cnt;
+					tmp_DDS->num_of_cores -= tmp_cnt; /* was nodes_cnt but this seems totally wrong. 5.5.2017 */
 					if (tmp_DDS->num_of_cores == 0){
 						DDS_count--;
 						if (tmp_DDS == DDS_tail){
@@ -3208,9 +3300,7 @@ void sig_FINISH_handler(int sender_id, int *inc_cnt, int cur_index_top){
 
 		/* I am worker and the application has finished */
 		if (cur_agent.my_agent != -1 && cur_agent.my_agent == sender_id) {
-
 			base_offset = -1;
-			
 			cur_agent.my_agent = -1;
 			cur_agent.array_size = -1;
 			cur_agent.work_bounds[0] = 0;
@@ -3263,7 +3353,7 @@ void sig_FINISH_handler(int sender_id, int *inc_cnt, int cur_index_top){
 			if (my_app.workld <= 0) {
 				fprintf(log_file,"In strange finish with agent %d and workld=%d\n",cur_agent.my_agent,my_app.workld);
 				fflush(log_file);
-			} else if (app_state == RESIZING) {
+			} else {
 				if ((++nodes_ended_cnt) == active_working_cores) {
 					if (--my_app.workld == 0) {
 						gettimeofday(&time_val, NULL);
@@ -3297,7 +3387,18 @@ void sig_FINISH_handler(int sender_id, int *inc_cnt, int cur_index_top){
 							row_reached = 0;
 							per_core_rows = my_app.array_size / active_working_cores;
 							rows_left = my_app.array_size % active_working_cores;
-						}
+						} else if (my_cores_count == 2) { //I have only one working core, I know(?) that i wiil not give him up
+                                                	fprintf(log_file,"Case of only one core\n");
+                                                	fflush(log_file);
+                                                	active_working_cores = 1;
+                                                	one_core = my_cores->next->core_id;
+                                                	core_inter_tail[one_core]->next = (inter_list *) malloc(sizeof(inter_list));
+                                                	core_inter_tail[one_core] = core_inter_tail[one_core]->next;
+                                                	core_inter_tail[one_core]->type = APPOINT_WORK_NODE;
+                                                	core_inter_tail[one_core]->data.work_bounds[0] = 0;
+                                                	core_inter_tail[one_core]->data.work_bounds[1] = my_app.array_size-1;
+                                                	core_inter_tail[one_core]->next = NULL;
+                                        	}
 				
 						gettimeofday(&time_val, NULL);
 						cur_t = localtime(&time_val.tv_sec);
@@ -3306,14 +3407,6 @@ void sig_FINISH_handler(int sender_id, int *inc_cnt, int cur_index_top){
 						//printf("--%d-- Remaining workload is %d active cores = %d\n",my_app.id,my_app.workld,active_working_cores);
 						//fflush(app_log_file);
 
-						/*time_val.tv_sec = slowest_node[2*node_id];
-						time_val.tv_usec = slowest_node[(2*node_id) + 1];
-						cur_t = localtime(&time_val.tv_sec);
-						fprintf(app_log_file,"[%d:%d:%d:%ld] Actual finish time\n",
-							cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,time_val.tv_usec);
-						fflush(app_log_file);
-						slowest_node[2*node_id] = 0;*/
-
 						tmp_cores_list = my_cores->next;
 						while (tmp_cores_list != NULL) {
 							one_core = tmp_cores_list->core_id;
@@ -3321,13 +3414,18 @@ void sig_FINISH_handler(int sender_id, int *inc_cnt, int cur_index_top){
 								core_inter_head[one_core] = (inter_list *) malloc(sizeof(inter_list));
 								core_inter_tail[one_core] = core_inter_head[one_core];
 								core_inter_tail[one_core]->type = APPOINT_WORK_NODE;
-							} else if (core_inter_head[one_core]->type == INIT_WORK_NODE_PENDING) //{
+							} else if (core_inter_head[one_core]->type == INIT_WORK_NODE_PENDING) {//{
 								core_inter_tail[one_core]->type = INIT_WORK_NODE;
-							/*} else {
+							} else {
+								fprintf(log_file,"I am doing smth else with my work node %d in resize appoint inter = %d\n",
+                                                        		one_core,core_inter_head[one_core]->type);
+                                                        	fflush(log_file);
+								/*
 								core_inter_tail[one_core]->next = (inter_list *) malloc(sizeof(inter_list));
 								core_inter_tail[one_core] = core_inter_tail[one_core]->next;
 								core_inter_tail[one_core]->type = APPOINT_WORK_NODE;
-							}*/
+								*/
+							}
 
 							//core_inter_tail[one_core]->type = APPOINT_WORK_NODE;
 							if (core_inter_tail[one_core]->type == APPOINT_WORK_NODE || core_inter_tail[one_core]->type == INIT_WORK_NODE) {
@@ -3363,96 +3461,9 @@ void sig_FINISH_handler(int sender_id, int *inc_cnt, int cur_index_top){
 						old_cores_cnt = 0;
 						nodes_ended_cnt = 0;
 					}
-				}
-			} else if ((++nodes_ended_cnt) == active_working_cores) {
-				nodes_ended_cnt = 0;
-				if (--my_app.workld == 0) {
-					gettimeofday(&time_val, NULL);
-					cur_t = localtime(&time_val.tv_sec);
-					fprintf(log_file,"[%d:%d:%d:%ld] Matrix mul is over.\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,time_val.tv_usec);
-					fprintf(app_log_file,"[%d:%d:%d:%ld] Matrix mul is over.\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,time_val.tv_usec);
-					app_state = APP_TERMINATED;
 				} else {
-					fprintf(log_file,"A matrix mul is over. Remaining workload is %d\n",my_app.workld);
-
-					active_working_cores = 0;
-					for (tmp_cores_list = my_cores->next; tmp_cores_list != NULL; tmp_cores_list = tmp_cores_list->next) {
-						
-						one_core = tmp_cores_list->core_id;
-						if (core_inter_head[one_core] == NULL){
-							active_working_cores++;
-						//} else if (core_inter_head[one_core]->type == INIT_WORK_NODE_PENDING) {
-						//	active_working_cores++;
-						} else {
-							fprintf(log_file,"I am doing smth else with my work node %d in simple appoint inter = %d\n",
-							one_core,core_inter_head[one_core]->type);
-							fflush(log_file);
-						}
-					}
-
-					if (active_working_cores > 0) {
-						//active_working_cores = my_cores_count - 1;
-						row_reached = 0;
-						per_core_rows = my_app.array_size / active_working_cores;
-						rows_left = my_app.array_size % active_working_cores;
-					} else if (my_cores_count == 2) { //I have only one working core, I know(?) that i wiil not give him up
-						  fprintf(log_file,"Case of only one core\n");
-						  fflush(log_file);
-						  active_working_cores = 1;
-						  one_core = my_cores->next->core_id;
-						  core_inter_tail[one_core]->next = (inter_list *) malloc(sizeof(inter_list));
-						  core_inter_tail[one_core] = core_inter_tail[one_core]->next;
-						  core_inter_tail[one_core]->type = APPOINT_WORK_NODE;
-						  core_inter_tail[one_core]->data.work_bounds[0] = 0;
-						  core_inter_tail[one_core]->data.work_bounds[1] = my_app.array_size-1;
-						  core_inter_tail[one_core]->next = NULL;
-					}
-			
-					gettimeofday(&time_val, NULL);
-					cur_t = localtime(&time_val.tv_sec);
-					fprintf(app_log_file,"[%d:%d:%d:%ld] A matrix mul is over. Remaining workload is %d active cores = %d\n",
-						cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,time_val.tv_usec,my_app.workld,active_working_cores);
-					//printf("--%d-- Remaining workload is %d active cores = %d\n",my_app.id,my_app.workld,active_working_cores);
-					fprintf(log_file,"Remaining workload is %d active cores = %d\n",my_app.workld,active_working_cores);
-					//fflush(app_log_file);
-
-					/*time_val.tv_sec = slowest_node[2*node_id];
-					  time_val.tv_usec = slowest_node[(2*node_id) + 1];
-					  cur_t = localtime(&time_val.tv_sec);
-					  fprintf(app_log_file,"[%d:%d:%d:%ld] Actual finish time\n",
-						  cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,time_val.tv_usec);
-					  fflush(app_log_file);
-					  slowest_node[2*node_id] = 0;
-					*/
-					tmp_cores_list = my_cores->next;
-					while (tmp_cores_list != NULL) {
-						one_core = tmp_cores_list->core_id;
-						if (core_inter_head[one_core] == NULL) {
-							core_inter_head[one_core] = (inter_list *) malloc(sizeof(inter_list));
-							core_inter_tail[one_core] = core_inter_head[one_core];
-						/*} else {
-							core_inter_tail[one_core]->next = (inter_list *) malloc(sizeof(inter_list));
-							core_inter_tail[one_core] = core_inter_tail[one_core]->next;
-						}*/
-
-							core_inter_tail[one_core]->type = APPOINT_WORK_NODE;
-							core_inter_tail[one_core]->data.work_bounds[0] = row_reached;
-							tmp_cores_list->workload[0] = row_reached; /* 27.6.2016 Added by dimos. If worker fails i have to know the workload given in order to reappoint */
-							row_reached += per_core_rows;
-							if (rows_left > 0) {
-								row_reached++;
-								rows_left--;
-							}
-							core_inter_tail[one_core]->data.work_bounds[1] = row_reached-1;
-							tmp_cores_list->workload[0] = row_reached-1; /* 27.6.2016 Added by dimos. If worker fails i have to know the workload given in order to reappoint */
-							core_inter_tail[one_core]->next = NULL;
-
-							scc_kill(one_core, SIG_APPOINT_WORK, core_inter_head[one_core]);
-							my_stats.msg_count++;
-							my_stats.distance += distance(node_id,one_core);
-						}
-						tmp_cores_list = tmp_cores_list->next;
-					}
+					fprintf(log_file,"Just a plain node finish\n");
+					fflush(log_file);
 				}
 			}
 		}
@@ -3653,13 +3664,15 @@ void sig_FINISH_handler(int sender_id, int *inc_cnt, int cur_index_top){
 			free(tmp_DDS);
 		} else {
 			printf("--%d-- Fuck agent %d that finished is not in my DDS\n", node_id, old_agent_id);
-			printf("My current DDS list:\n");
+			
+			fprintf(log_file,"Fuck agent %d that finished is not in my DDS\n", old_agent_id);
+			fprintf(log_file,"My current DDS list:\n");
 			tmp_DDS = DDS;
 			while (tmp_DDS != NULL){
-				printf("Agend_id : %d | Cores in my cluster : %d\n",tmp_DDS->agent_id,tmp_DDS->num_of_cores);
+				fprintf(log_file,"Agend_id : %d | Cores in my cluster : %d\n",tmp_DDS->agent_id,tmp_DDS->num_of_cores);
 				tmp_DDS = tmp_DDS->next;
 			}
-		  
+		  	fflush(log_file);
 		}
 		
 		free(tmp_list);