Explorar o código

added example for the hypervisor for app_driven strategy

Andra Hugo %!s(int64=12) %!d(string=hai) anos
pai
achega
9886bbcc15

+ 8 - 1
sched_ctx_hypervisor/examples/Makefile.am

@@ -15,7 +15,8 @@
 
 if !NO_BLAS_LIB
 noinst_PROGRAMS =				\
-	cholesky/cholesky_implicit
+	cholesky/cholesky_implicit  \
+	app_driven_test/app_driven_test
 
 noinst_HEADERS = 				\
 	cholesky/cholesky.h			\
@@ -47,4 +48,10 @@ cholesky_cholesky_implicit_LDADD =		\
 	$(top_builddir)/sched_ctx_hypervisor/src/libsched_ctx_hypervisor.la \
 	$(STARPU_BLAS_LDFLAGS)
 
+app_driven_test_app_driven_test_SOURCES =		\
+	app_driven_test/app_driven_test.c		
+
+app_driven_test_app_driven_test_LDADD =		\
+	$(top_builddir)/sched_ctx_hypervisor/src/libsched_ctx_hypervisor.la 
+
 endif

+ 119 - 0
sched_ctx_hypervisor/examples/app_driven_test/app_driven_test.c

@@ -0,0 +1,119 @@
+#include <stdio.h>
+#include <stdint.h>
+#include <starpu.h>
+#include <sched_ctx_hypervisor.h>
+
+#include <pthread.h>
+
+#define FPRINTF(ofile, fmt, args ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ##args); }} while(0)
+
+
+/* Every implementation of a codelet must have this prototype, the first                                                                                                                                             * argument (buffers) describes the buffers/streams that are managed by the                                                                                                                                       
+ * DSM; the second arguments references read-only data that is passed as an                                                                                                                                        
+ * argument of the codelet (task->cl_arg). Here, "buffers" is unused as there                                                                                                                                      
+ * are no data input/output managed by the DSM (cl.nbuffers = 0) */
+struct params
+{
+	unsigned sched_ctx;
+    int task_tag;
+};
+
+void cpu_func(void *buffers[], void *cl_arg)
+{
+    struct params *params = (struct params *) cl_arg;
+
+	int i;
+	for(i = 0; i < 1000; i++); 
+    FPRINTF(stdout, "Hello world sched_ctx = %d task_tag = %d \n", params->sched_ctx, params->task_tag);
+}
+
+struct starpu_codelet cl = {};
+
+int tag = 1;
+void* start_thread(void *arg)
+{
+	unsigned sched_ctx = *((unsigned*)arg);
+	starpu_set_sched_ctx(&sched_ctx);
+
+	struct starpu_task *task[10];
+    struct params params[10];
+	int i;
+	for(i = 0; i < 10; i++)
+	{
+		int j;
+		for(j = 0; j < 1000; j++);
+		task[i] = starpu_task_create();
+		
+		cl.where = STARPU_CPU;
+		cl.cpu_funcs[0] = cpu_func;
+		cl.nbuffers = 0;
+		
+		task[i]->cl = &cl;
+		
+		if(sched_ctx == 1 && i == 5)
+		{
+			task[i]->hypervisor_tag = tag;
+			sched_ctx_hypervisor_ioctl(sched_ctx,
+									   HYPERVISOR_TIME_TO_APPLY, tag,
+									   HYPERVISOR_MIN_WORKERS, 2,
+									   HYPERVISOR_MAX_WORKERS, 12,
+									   HYPERVISOR_NULL);
+			printf("require resize for sched_ctx %d at tag %d\n", sched_ctx, tag);
+			sched_ctx_hypervisor_resize(sched_ctx, tag);
+		}
+
+		params[i].sched_ctx = sched_ctx;
+		params[i].task_tag = task[i]->hypervisor_tag;
+
+		task[i]->cl_arg = &params[i];
+		task[i]->cl_arg_size = sizeof(params);
+		
+		starpu_task_submit(task[i]);
+	}
+	
+	starpu_task_wait_for_all();
+}
+
+int main()
+{
+	int ret = starpu_init(NULL);
+
+	if (ret == -ENODEV)
+        return 77;
+
+	int nres1 = 6;
+	int nres2 = 6;
+	int ressources1[nres1];
+	int ressources2[nres2];
+	int i;
+	for(i = 0; i < nres1; i++)
+		ressources1[i] = i;
+
+	for(i = 0; i < nres2; i++)
+		ressources2[i] = nres1+i;
+
+	unsigned sched_ctx1 = starpu_create_sched_ctx("heft", ressources1, nres1, "sched_ctx1");
+	unsigned sched_ctx2 = starpu_create_sched_ctx("heft", ressources2, nres2, "sched_ctx2");
+
+
+	struct hypervisor_policy policy;
+	policy.custom = 0;
+	policy.name = "app_driven";
+	void *perf_counters = sched_ctx_hypervisor_init(&policy);
+
+	starpu_set_perf_counters(sched_ctx1, (struct starpu_performance_counters*)perf_counters);
+	starpu_set_perf_counters(sched_ctx2, (struct starpu_performance_counters*)perf_counters);
+	sched_ctx_hypervisor_register_ctx(sched_ctx1, 0.0);
+	sched_ctx_hypervisor_register_ctx(sched_ctx2, 0.0);
+
+	pthread_t tid[2];
+
+	pthread_create(&tid[0], NULL, start_thread, (void*)&sched_ctx1);
+	pthread_create(&tid[1], NULL, start_thread, (void*)&sched_ctx2);
+
+	pthread_join(tid[0], NULL);
+	pthread_join(tid[1], NULL);
+
+	starpu_shutdown();
+	sched_ctx_hypervisor_shutdown();
+}

+ 88 - 94
sched_ctx_hypervisor/src/sched_ctx_hypervisor.c

@@ -38,24 +38,18 @@ extern struct hypervisor_policy lp2_policy;
 
 static struct hypervisor_policy *predefined_policies[] = {
         &idle_policy,
-	&app_driven_policy,
+		&app_driven_policy,
 #ifdef HAVE_GLPK_H
-	&lp_policy,
-	&lp2_policy,
+		&lp_policy,
+		&lp2_policy,
 #endif
-	&gflops_rate_policy
+		&gflops_rate_policy
 };
 
 static void _load_hypervisor_policy(struct hypervisor_policy *policy)
 {
-        STARPU_ASSERT(policy);
+	STARPU_ASSERT(policy);
 
-#ifdef STARPU_VERBOSE
-        if (policy->name)
-        {
-		_STARPU_DEBUG("Use %s hypervisor policy \n", policy->name);
-        }
-#endif
 	hypervisor.policy.name = policy->name;
 	hypervisor.policy.size_ctxs = policy->size_ctxs;
 	hypervisor.policy.handle_poped_task = policy->handle_poped_task;
@@ -70,56 +64,56 @@ static void _load_hypervisor_policy(struct hypervisor_policy *policy)
 static struct hypervisor_policy *_find_hypervisor_policy_from_name(const char *policy_name)
 {
 
-        if (!policy_name)
-                return NULL;
-
-        unsigned i;
-        for (i = 0; i < sizeof(predefined_policies)/sizeof(predefined_policies[0]); i++)
-        {
-                struct hypervisor_policy *p;
-                p = predefined_policies[i];
-                if (p->name)
-                {
-                        if (strcmp(policy_name, p->name) == 0) {
-                                /* we found a policy with the requested name */
-                                return p;
-                        }
-                }
-        }
-        fprintf(stderr, "Warning: hypervisor policy \"%s\" was not found, try \"help\" to get a list\n", policy_name);
-
-        /* nothing was found */
-        return NULL;
+	if (!policy_name)
+		return NULL;
+	
+	unsigned i;
+	for (i = 0; i < sizeof(predefined_policies)/sizeof(predefined_policies[0]); i++)
+	{
+		struct hypervisor_policy *p;
+		p = predefined_policies[i];
+		if (p->name)
+		{
+			if (strcmp(policy_name, p->name) == 0) {
+				/* we found a policy with the requested name */
+				return p;
+			}
+		}
+	}
+	fprintf(stderr, "Warning: hypervisor policy \"%s\" was not found, try \"help\" to get a list\n", policy_name);
+	
+	/* nothing was found */
+	return NULL;
 }
 
 static struct hypervisor_policy *_select_hypervisor_policy(struct hypervisor_policy* hypervisor_policy)
 {
 	struct hypervisor_policy *selected_policy = NULL;
-
+	
 	if(hypervisor_policy && hypervisor_policy->custom)
 		return hypervisor_policy;
-
-        /* we look if the application specified the name of a policy to load */
-        const char *policy_name;
-        if (hypervisor_policy && hypervisor_policy->name)
-        {
-                policy_name = hypervisor_policy->name;
-        }
-        else 
+	
+	/* we look if the application specified the name of a policy to load */
+	const char *policy_name;
+	if (hypervisor_policy && hypervisor_policy->name)
 	{
-                policy_name = getenv("HYPERVISOR_POLICY");
-        }
-
-        if (policy_name)
-                selected_policy = _find_hypervisor_policy_from_name(policy_name);
-
-        /* Perhaps there was no policy that matched the name */
-        if (selected_policy)
-                return selected_policy;
-
-        /* If no policy was specified, we use the idle policy as a default */
-
-        return &idle_policy;
+		policy_name = hypervisor_policy->name;
+	}
+	else 
+	{
+		policy_name = getenv("HYPERVISOR_POLICY");
+	}
+	
+	if (policy_name)
+		selected_policy = _find_hypervisor_policy_from_name(policy_name);
+	
+	/* Perhaps there was no policy that matched the name */
+	if (selected_policy)
+		return selected_policy;
+	
+	/* If no policy was specified, we use the idle policy as a default */
+	
+	return &idle_policy;
 }
 
 
@@ -129,7 +123,7 @@ struct starpu_performance_counters* sched_ctx_hypervisor_init(struct hypervisor_
 	hypervisor.min_tasks = 0;
 	hypervisor.nsched_ctxs = 0;
 	pthread_mutex_init(&act_hypervisor_mutex, NULL);
-
+	
 	int i;
 	for(i = 0; i < STARPU_NMAX_SCHED_CTXS; i++)
 	{
@@ -243,12 +237,12 @@ void sched_ctx_hypervisor_register_ctx(unsigned sched_ctx, double total_flops)
 
 static int _get_first_free_sched_ctx(int *sched_ctxs, unsigned nsched_ctxs)
 {
-        int i;
-        for(i = 0; i < nsched_ctxs; i++)
-                if(sched_ctxs[i] == STARPU_NMAX_SCHED_CTXS)
-                        return i;
-
-        return STARPU_NMAX_SCHED_CTXS;
+	int i;
+	for(i = 0; i < nsched_ctxs; i++)
+		if(sched_ctxs[i] == STARPU_NMAX_SCHED_CTXS)
+			return i;
+	
+	return STARPU_NMAX_SCHED_CTXS;
 }
 
 /* rearange array of sched_ctxs in order not to have {MAXVAL, MAXVAL, 5, MAXVAL, 7}    
@@ -257,19 +251,19 @@ static int _get_first_free_sched_ctx(int *sched_ctxs, unsigned nsched_ctxs)
 */
 static void _rearange_sched_ctxs(int *sched_ctxs, int old_nsched_ctxs)
 {
-        int first_free_id = STARPU_NMAX_SCHED_CTXS;
-        int i;
-        for(i = 0; i < old_nsched_ctxs; i++)
-        {
-                if(sched_ctxs[i] != STARPU_NMAX_SCHED_CTXS)
-                {
-                        first_free_id = _get_first_free_sched_ctx(sched_ctxs, old_nsched_ctxs);
-                        if(first_free_id != STARPU_NMAX_SCHED_CTXS)
+	int first_free_id = STARPU_NMAX_SCHED_CTXS;
+	int i;
+	for(i = 0; i < old_nsched_ctxs; i++)
+	{
+		if(sched_ctxs[i] != STARPU_NMAX_SCHED_CTXS)
+		{
+			first_free_id = _get_first_free_sched_ctx(sched_ctxs, old_nsched_ctxs);
+			if(first_free_id != STARPU_NMAX_SCHED_CTXS)
 			{
-                                sched_ctxs[first_free_id] = sched_ctxs[i];
+				sched_ctxs[first_free_id] = sched_ctxs[i];
 				sched_ctxs[i] = STARPU_NMAX_SCHED_CTXS;
 			}
-                }
+		}
 	}
 }
 
@@ -277,23 +271,23 @@ static void _rearange_sched_ctxs(int *sched_ctxs, int old_nsched_ctxs)
 void sched_ctx_hypervisor_unregister_ctx(unsigned sched_ctx)
 {
 	pthread_mutex_lock(&act_hypervisor_mutex);
-        unsigned i;
-        for(i = 0; i < hypervisor.nsched_ctxs; i++)
-        {
-                if(hypervisor.sched_ctxs[i] == sched_ctx)
-                {
-                        hypervisor.sched_ctxs[i] = STARPU_NMAX_SCHED_CTXS;
+	unsigned i;
+	for(i = 0; i < hypervisor.nsched_ctxs; i++)
+	{
+		if(hypervisor.sched_ctxs[i] == sched_ctx)
+		{
+			hypervisor.sched_ctxs[i] = STARPU_NMAX_SCHED_CTXS;
 			break;
-                }
-        }
+		}
+	}
 
-        _rearange_sched_ctxs(hypervisor.sched_ctxs, hypervisor.nsched_ctxs);
+	_rearange_sched_ctxs(hypervisor.sched_ctxs, hypervisor.nsched_ctxs);
 	hypervisor.nsched_ctxs--;
 	hypervisor.sched_ctx_w[sched_ctx].sched_ctx = STARPU_NMAX_SCHED_CTXS;
 	_remove_config(sched_ctx);
-
-	free(hypervisor.configurations[sched_ctx]);
-	free(hypervisor.resize_requests[sched_ctx]);
+	
+/* 	free(hypervisor.configurations[sched_ctx]); */
+/* 	free(hypervisor.resize_requests[sched_ctx]); */
 	pthread_mutex_destroy(&hypervisor.conf_mut[sched_ctx]);
 	pthread_mutex_destroy(&hypervisor.resize_mut[sched_ctx]);
 	if(hypervisor.nsched_ctxs == 1)
@@ -358,12 +352,12 @@ void sched_ctx_hypervisor_move_workers(unsigned sender_sched_ctx, unsigned recei
 		for(j = 0; j < nworkers_to_move; j++)
 			printf(" %d", workers_to_move[j]);
 		printf("\n");
-
+		
 		int *cpus = (int*) malloc(nworkers_to_move * sizeof(int));
 		int ncpus;
-
+		
 		_get_cpus(workers_to_move, nworkers_to_move, cpus, &ncpus);
-
+		
 //		if(ncpus != 0)
 //			starpu_remove_workers_from_sched_ctx(cpus, ncpus, sender_sched_ctx);
 
@@ -371,13 +365,13 @@ void sched_ctx_hypervisor_move_workers(unsigned sender_sched_ctx, unsigned recei
 
 		if(now)
 		{
-				int j;
-				printf("remove from ctx %d:", sender_sched_ctx);
-				for(j = 0; j < nworkers_to_move; j++)
-					printf(" %d", workers_to_move[j]);
-				printf("\n");
-				
-				starpu_remove_workers_from_sched_ctx(workers_to_move, nworkers_to_move, sender_sched_ctx);
+			int j;
+			printf("remove from ctx %d:", sender_sched_ctx);
+			for(j = 0; j < nworkers_to_move; j++)
+				printf(" %d", workers_to_move[j]);
+			printf("\n");
+			
+			starpu_remove_workers_from_sched_ctx(workers_to_move, nworkers_to_move, sender_sched_ctx);
 		}
 		else
 		{
@@ -399,7 +393,7 @@ void sched_ctx_hypervisor_move_workers(unsigned sender_sched_ctx, unsigned recei
 				}
 				
 				hypervisor.resize[sender_sched_ctx] = 0;
-			
+				
 				pthread_mutex_unlock(&hypervisor.sched_ctx_w[sender_sched_ctx].mutex);
 			}
 		}
@@ -407,7 +401,7 @@ void sched_ctx_hypervisor_move_workers(unsigned sender_sched_ctx, unsigned recei
 		int i;
 		for(i = 0; i < nworkers_to_move; i++)
 			new_config->max_idle[workers_to_move[i]] = new_config->max_idle[workers_to_move[i]] !=MAX_IDLE_TIME ? new_config->max_idle[workers_to_move[i]] :  new_config->new_workers_max_idle;
-
+		
 	}
 	return;
 }