Browse Source

multiple regression: writing calibration observations in the list and reading the values

Luka Stanisic 9 years ago
parent
commit
33a4dd7d0c

+ 3 - 0
include/starpu_perfmodel.h

@@ -60,6 +60,9 @@ struct starpu_perfmodel_history_entry
 	uint32_t footprint;
 	size_t size;
 	double flops;
+
+	double duration;
+	double *parameters;
 };
 
 struct starpu_perfmodel_history_list

+ 23 - 2
src/core/perfmodel/perfmodel_history.c

@@ -293,7 +293,7 @@ static void dump_reg_model(FILE *f, struct starpu_perfmodel *model, int comb, in
 
 	reg_model->coeff = (double *) malloc(reg_model->ncoeff*sizeof(double));
 	if (model->type == STARPU_MULTIPLE_REGRESSION_BASED)
-		_starpu_multiple_regression(per_arch_model->list, reg_model->coeff, reg_model->ncoeff);
+		_starpu_multiple_regression(per_arch_model->list, reg_model->coeff, reg_model->ncoeff, model->nparameters, model->combinations);
 
 	fprintf(f, "# n\tintercept");
 	for (int i=1; i < reg_model->ncoeff; i++){
@@ -1295,7 +1295,7 @@ double _starpu_multiple_regression_based_job_expected_perf(struct starpu_perfmod
 	for (int i=0; i < model->ncombinations; i++){
 		parameter_value=1.;
 		for (int j=0; j < model->nparameters; j++)
-			parameter_value *= model->parameters[j]*model->combinations[i][j];
+			parameter_value *= pow(model->parameters[j],model->combinations[i][j]);
 
 		expected_duration += reg_model->coeff[i+1]*parameter_value;
 	}
@@ -1551,6 +1551,27 @@ void _starpu_update_perfmodel_history(struct _starpu_job *j, struct starpu_perfm
 			}
 		}
 
+		if (model->type == STARPU_MULTIPLE_REGRESSION_BASED)
+		{
+			struct starpu_perfmodel_history_entry *entry;
+			struct starpu_perfmodel_history_list **list;
+			list = &per_arch_model->list;
+
+			entry = (struct starpu_perfmodel_history_entry *) calloc(1, sizeof(struct starpu_perfmodel_history_entry));
+			STARPU_ASSERT(entry);
+
+			entry->parameters = (double *) malloc(model->nparameters*sizeof(double));
+			for(int i=0; i < model->nparameters; i++)
+				entry->parameters[i] = model->parameters[i];
+			entry->duration = measured;
+
+			struct starpu_perfmodel_history_list *link;
+			link = (struct starpu_perfmodel_history_list *) malloc(sizeof(struct starpu_perfmodel_history_list));
+			link->next = *list;
+			link->entry = entry;
+			*list = link;
+		}
+
 #ifdef STARPU_MODEL_DEBUG
 		struct starpu_task *task = j->task;
 		starpu_perfmodel_debugfilepath(model, arch_combs[comb], per_arch_model->debug_path, 256, impl);

+ 44 - 31
src/core/perfmodel/regression.c

@@ -225,6 +225,27 @@ int _starpu_regression_non_linear_power(struct starpu_perfmodel_history_list *pt
 
 /* Code for computing multiple linear regression */
 
+static void dump_multiple_regression_list(double *mx, double *my, unsigned ncoeff, unsigned nparameters, unsigned **combinations, struct starpu_perfmodel_history_list *list_history)
+{
+	struct starpu_perfmodel_history_list *ptr = list_history;
+	unsigned i = 0;
+
+	while (ptr)
+	{
+		mx[i*ncoeff] = 1.;
+		for(int j=0; j<ncoeff-1; j++)
+		{
+			mx[i*ncoeff+j+1] = 1.;
+			for(int k=0; k < nparameters; k++)
+				mx[i*ncoeff+j+1] *= pow(ptr->entry->parameters[k],combinations[j][k]);
+		}
+		my[i] = ptr->entry->duration;
+
+		ptr = ptr->next;
+		i++;
+	}
+}
+
 typedef struct { int h, w; double *x;} matrix_t, *matrix;
 
 double dot(double *a, double *b, int len, int step)
@@ -291,10 +312,11 @@ matrix transpose(matrix src)
 }
 
 // Inspired from: http://www.programming-techniques.com/2011/09/numerical-methods-inverse-of-nxn-matrix.html
-matrix mat_inv(matrix src, int n)
+matrix mat_inv(matrix src)
 {
+	int n = src->h;
 	int n2=2*n;
-        int i,j, k;
+    int i,j, k;
 	double ratio, a;
 	matrix r, dst;
 	r = mat_new(n, n2);
@@ -316,12 +338,10 @@ matrix mat_inv(matrix src, int n)
 	for(i = 0; i < n; i++){
 	  for(j = 0; j < n; j++){
 	    if(i!=j){
-               ratio = r->x[j*n2+i] / r->x[i*n2+i];
                for(k = 0; k < 2*n; k++){
-                   r->x[j*n2+k] -= ratio * r->x[i*n2+k];
-
-	       }
-            }
+                   r->x[j*n2+k] -= (r->x[j*n2+i] / r->x[i*n2+i]) * r->x[i*n2+k];
+               }
+        }
 	  }
 	}
 
@@ -352,12 +372,10 @@ void multiple_reg_coeff(double *mx, double *my, int n, int k, double *coeff)
 	mcoeff = mat_mul(
 			mat_mul(
 				mat_inv(
-					mat_mul(transpose(X), X),
-				        k),
+					mat_mul(transpose(X), X)),
 				transpose(X)),
 			Y);
 
-
 	for(int i=0; i<k; i++)
 		coeff[i] = mcoeff->x[i];
 
@@ -394,7 +412,7 @@ int test_multiple_regression()
 	matrix_t A = { 3, 3, dA };
 	mat_show(&A);
 	matrix Ainv;
-	Ainv = mat_inv(&A, 3);
+	Ainv = mat_inv(&A);
 	mat_show(Ainv);
 
 	// Multiple regression test: http://www.biddle.com/documents/bcg_comp_chapter4.pdf
@@ -418,13 +436,13 @@ int test_multiple_regression()
 	coeff = mat_mul(
 			mat_mul(
 				mat_inv(
-					mat_mul(transpose(&X), &X),
-				        k),
+					mat_mul(transpose(&X), &X)
+				        ),
 				transpose(&X)),
 			&Y);
 	mat_show(coeff);
 
-	double *results;
+	double *results=NULL;
 	multiple_reg_coeff(dX, dY, n, k, results);
 	printf("\nFinal coefficients:\n");
 	for(int i=0; i<k; i++)
@@ -433,25 +451,20 @@ int test_multiple_regression()
 
 }
 
-int _starpu_multiple_regression(struct starpu_perfmodel_history_list *ptr, double *coeff, unsigned ncoeff)
+int _starpu_multiple_regression(struct starpu_perfmodel_history_list *ptr, double *coeff, unsigned ncoeff, unsigned nparameters, unsigned **combinations)
 {
+	unsigned n = find_list_size(ptr);
+	STARPU_ASSERT(n);
 
-	double dX[] = {	1, 12, 32,
-			1, 14, 35,
-			1, 15, 45,
-			1, 16, 45,
-			1, 18, 50 };
+	double *mx = (double *) malloc(ncoeff*n*sizeof(double));
+	STARPU_ASSERT(mx);
+
+	double *my = (double *) malloc(n*sizeof(double));
+	STARPU_ASSERT(my);
+
+	dump_multiple_regression_list(mx, my, ncoeff, nparameters, combinations, ptr);
+
+	multiple_reg_coeff(mx, my, n, ncoeff, coeff);
 
-	double dY[] = {	350000, 399765, 429000, 435000, 433000};
-	int n = 5;
-	int k = 3;
-	multiple_reg_coeff(dX, dY, n, k, coeff);
-	//coeff[3] = 0.99;
-/*
-	coefficients[0]=0.664437;
-	coefficients[1]=0.0032;
-	coefficients[2]=0.0041;
-	coefficients[3]=0.0044;
-*/
 	return 0;
 }

+ 1 - 1
src/core/perfmodel/regression.h

@@ -25,6 +25,6 @@
 #include <starpu.h>
 
 int _starpu_regression_non_linear_power(struct starpu_perfmodel_history_list *ptr, double *a, double *b, double *c);
-int _starpu_multiple_regression(struct starpu_perfmodel_history_list *ptr, double *coefficients, unsigned ncoeff);
+int _starpu_multiple_regression(struct starpu_perfmodel_history_list *ptr, double *coeff, unsigned ncoeff, unsigned nparameters, unsigned **combinations);
 
 #endif // __REGRESSION_H__