Browse Source

Avoid calling starpu_timing_now within a critical section or several times

Samuel Thibault 8 years ago
parent
commit
0192b78a52

+ 2 - 1
src/sched_policies/component_fifo.c

@@ -152,13 +152,14 @@ static struct starpu_task * fifo_pull_task(struct starpu_sched_component * compo
 	struct _starpu_fifo_data * data = component->data;
 	struct _starpu_fifo_taskq * fifo = data->fifo;
 	starpu_pthread_mutex_t * mutex = &data->mutex;
+	const double now = starpu_timing_now();
 	STARPU_COMPONENT_MUTEX_LOCK(mutex);
 	struct starpu_task * task = _starpu_fifo_pop_task(fifo, starpu_worker_get_id_check());
 	if(task)
 	{
 		if(!isnan(task->predicted))
 		{
-			fifo->exp_start = starpu_timing_now() + task->predicted;
+			fifo->exp_start = now + task->predicted;
 			fifo->exp_len -= task->predicted;
 		}
 		fifo->exp_end = fifo->exp_start + fifo->exp_len;

+ 4 - 3
src/sched_policies/component_prio.c

@@ -113,6 +113,7 @@ static int prio_push_local_task(struct starpu_sched_component * component, struc
 	struct _starpu_prio_deque * prio = &data->prio;
 	starpu_pthread_mutex_t * mutex = &data->mutex;
 	int ret;
+	const double now = starpu_timing_now();
 	STARPU_COMPONENT_MUTEX_LOCK(mutex);
 	double exp_len;
 	if(!isnan(task->predicted))
@@ -145,7 +146,7 @@ static int prio_push_local_task(struct starpu_sched_component * component, struc
 		
 		if(!isnan(task->predicted_transfer)) {
 			double end = prio_estimated_end(component); 
-			double tfer_end = starpu_timing_now() + task->predicted_transfer; 
+			double tfer_end = now + task->predicted_transfer; 
 			if(tfer_end < end) 
 				task->predicted_transfer = 0.0; 
 			else 
@@ -182,6 +183,7 @@ static struct starpu_task * prio_pull_task(struct starpu_sched_component * compo
 	struct _starpu_prio_data * data = component->data;
 	struct _starpu_prio_deque * prio = &data->prio;
 	starpu_pthread_mutex_t * mutex = &data->mutex;
+	const double now = starpu_timing_now();
 	STARPU_COMPONENT_MUTEX_LOCK(mutex);
 	struct starpu_task * task = _starpu_prio_deque_pop_task(prio);
 	if(task)
@@ -189,7 +191,6 @@ static struct starpu_task * prio_pull_task(struct starpu_sched_component * compo
 		if(!isnan(task->predicted))
 		{
 			const double exp_len = prio->exp_len - task->predicted;
-			const double now = starpu_timing_now();
 			prio->exp_start = now + task->predicted;
 			if (exp_len >= 0.0)
 			{
@@ -198,7 +199,7 @@ static struct starpu_task * prio_pull_task(struct starpu_sched_component * compo
 			else
 			{
 				/* exp_len can become negative due to rounding errors */
-				prio->exp_len = starpu_timing_now()-now;
+				prio->exp_len = 0.0;
 			}
 		}
 		STARPU_ASSERT_MSG(prio->exp_len>=0, "prio->exp_len=%lf\n",prio->exp_len);

+ 4 - 2
src/sched_policies/component_work_stealing.c

@@ -136,6 +136,7 @@ static struct starpu_task * pull_task(struct starpu_sched_component * component)
 	}
 	STARPU_ASSERT(i < component->nchildren);
 	struct _starpu_work_stealing_data * wsd = component->data;
+	const double now = starpu_timing_now();
 	STARPU_COMPONENT_MUTEX_LOCK(wsd->mutexes[i]);
 	struct starpu_task * task = _starpu_prio_deque_pop_task(wsd->fifos[i]);
 	if(task)
@@ -143,7 +144,7 @@ static struct starpu_task * pull_task(struct starpu_sched_component * component)
 		if(!isnan(task->predicted))
 		{
 			wsd->fifos[i]->exp_len -= task->predicted;
-			wsd->fifos[i]->exp_start = starpu_timing_now() + task->predicted;
+			wsd->fifos[i]->exp_start = now + task->predicted;
 		}
 	}
 	else
@@ -188,11 +189,12 @@ double _ws_estimated_end(struct starpu_sched_component * component)
 	double sum_len = 0.0;
 	double sum_start = 0.0;
 	int i;
+	const double now = starpu_timing_now();
 	for(i = 0; i < component->nchildren; i++)
 	{
 		STARPU_COMPONENT_MUTEX_LOCK(wsd->mutexes[i]);
 		sum_len += wsd->fifos[i]->exp_len;
-		wsd->fifos[i]->exp_start = STARPU_MAX(starpu_timing_now(), wsd->fifos[i]->exp_start);
+		wsd->fifos[i]->exp_start = STARPU_MAX(now, wsd->fifos[i]->exp_start);
 		sum_start += wsd->fifos[i]->exp_start;
 		STARPU_COMPONENT_MUTEX_UNLOCK(wsd->mutexes[i]);
 

+ 8 - 5
src/sched_policies/component_worker.c

@@ -225,11 +225,12 @@ static inline void _starpu_worker_task_list_add(struct _starpu_worker_task_list
 	double predicted = task->predicted;
 	double predicted_transfer = task->predicted_transfer;
 	double end = l->exp_end;
+	const double now = starpu_timing_now();
 
 	/* Sometimes workers didn't take the tasks as early as we expected */
-	l->exp_start = STARPU_MAX(l->exp_start, starpu_timing_now());
+	l->exp_start = STARPU_MAX(l->exp_start, now);
 
-	if (starpu_timing_now() + predicted_transfer < end)
+	if (now + predicted_transfer < end)
 	{
 		/* We may hope that the transfer will be finished by
 		 * the start of the task. */
@@ -239,7 +240,7 @@ static inline void _starpu_worker_task_list_add(struct _starpu_worker_task_list
 	{
 		/* The transfer will not be finished by then, take the
 		 * remainder into account */
-		predicted_transfer = (starpu_timing_now() + predicted_transfer) - end;
+		predicted_transfer = (now + predicted_transfer) - end;
 	}
 
 	if(!isnan(predicted_transfer))
@@ -438,11 +439,12 @@ static struct starpu_task * simple_worker_pull_task(struct starpu_sched_componen
 	int n_tries = 0;
 	do
 	{
+		const double now = starpu_timing_now();
 		/* do not reset state_keep_awake here has it may hide tasks in worker->local_tasks */
 		n_tries++;
 		STARPU_COMPONENT_MUTEX_LOCK(&list->mutex);
 		/* Take the opportunity to update start time */
-		data->list->exp_start = STARPU_MAX(starpu_timing_now(), data->list->exp_start);
+		data->list->exp_start = STARPU_MAX(now, data->list->exp_start);
 		data->list->exp_end = data->list->exp_start + data->list->exp_len;
 		task =  _starpu_worker_task_list_pop(list);
 		if(task)
@@ -787,10 +789,11 @@ int starpu_sched_component_worker_get_workerid(struct starpu_sched_component * w
 void starpu_sched_component_worker_pre_exec_hook(struct starpu_task * task, unsigned sched_ctx_id STARPU_ATTRIBUTE_UNUSED)
 {
 	struct _starpu_worker_task_list * list = _worker_get_list(sched_ctx_id);
+	const double now = starpu_timing_now();
 	STARPU_COMPONENT_MUTEX_LOCK(&list->mutex);
 	_starpu_worker_task_list_started(list, task);
 	/* Take the opportunity to update start time */
-	list->exp_start = STARPU_MAX(starpu_timing_now() + list->pipeline_len, list->exp_start);
+	list->exp_start = STARPU_MAX(now + list->pipeline_len, list->exp_start);
 	STARPU_COMPONENT_MUTEX_UNLOCK(&list->mutex);
 }
 

+ 2 - 1
src/sched_policies/deque_modeling_policy_data_aware.c

@@ -1118,6 +1118,7 @@ static void dmda_pre_exec_hook(struct starpu_task *task, unsigned sched_ctx_id)
 	unsigned workerid = starpu_worker_get_id_check();
 	struct _starpu_dmda_data *dt = (struct _starpu_dmda_data*)starpu_sched_ctx_get_policy_data(sched_ctx_id);
 	struct _starpu_fifo_taskq *fifo = dt->queue_array[workerid];
+	const double now = starpu_timing_now();
 
 	/* Once the task is executing, we can update the predicted amount
 	 * of work. */
@@ -1126,7 +1127,7 @@ static void dmda_pre_exec_hook(struct starpu_task *task, unsigned sched_ctx_id)
 	_starpu_fifo_task_started(fifo, task, dt->num_priorities);
 
 	/* Take the opportunity to update start time */
-	fifo->exp_start = STARPU_MAX(starpu_timing_now() + fifo->pipeline_len, fifo->exp_start);
+	fifo->exp_start = STARPU_MAX(now + fifo->pipeline_len, fifo->exp_start);
 	fifo->exp_end = fifo->exp_start + fifo->exp_len;
 
 	_starpu_worker_unlock_self();

+ 2 - 1
src/sched_policies/parallel_heft.c

@@ -81,6 +81,7 @@ static void parallel_heft_pre_exec_hook(struct starpu_task *task, unsigned sched
 	unsigned workerid = starpu_worker_get_id_check();
 	double model = task->predicted;
 	double transfer_model = task->predicted_transfer;
+	const double now = starpu_timing_now();
 
 	if (isnan(model))
 		model = 0.0;
@@ -92,7 +93,7 @@ static void parallel_heft_pre_exec_hook(struct starpu_task *task, unsigned sched
 	 * of work. */
 	_starpu_worker_lock_self();
 	worker_exp_len[workerid] -= model + transfer_model;
-	worker_exp_start[workerid] = starpu_timing_now() + model;
+	worker_exp_start[workerid] = now + model;
 	worker_exp_end[workerid] = worker_exp_start[workerid] + worker_exp_len[workerid];
 	ntasks[workerid]--;
 	_starpu_worker_unlock_self();