Kaynağa Gözat

Minor code reorganization

Cédric Augonnet 15 yıl önce
ebeveyn
işleme
197d8bc68c
1 değiştirilmiş dosya ile 43 ekleme ve 35 silme
  1. 43 35
      src/datawizard/user_interactions.c

+ 43 - 35
src/datawizard/user_interactions.c

@@ -21,6 +21,8 @@
 #include <datawizard/write_back.h>
 #include <core/dependencies/data_concurrency.h>
 
+/* Explicitly ask StarPU to allocate room for a piece of data on the specified
+ * memory node. */
 int starpu_data_request_allocation(starpu_data_handle handle, uint32_t node)
 {
 	starpu_data_request_t r;
@@ -49,6 +51,9 @@ struct state_and_node {
 	void *callback_arg;
 };
 
+/*
+ *	Non Blocking data request from application
+ */
 /* put the current value of the data into RAM */
 static inline void _starpu_sync_data_with_mem_continuation_non_blocking(void *arg)
 {
@@ -74,44 +79,38 @@ static inline void _starpu_sync_data_with_mem_continuation_non_blocking(void *ar
 }
 
 /* The data must be released by calling starpu_data_release_from_mem later on */
-int starpu_data_sync_with_mem(starpu_data_handle handle, starpu_access_mode mode)
+int starpu_data_sync_with_mem_non_blocking(starpu_data_handle handle,
+		starpu_access_mode mode, void (*callback)(void *), void *arg)
 {
 	STARPU_ASSERT(handle);
 
-	/* it is forbidden to call this function from a callback or a codelet */
-	if (STARPU_UNLIKELY(!_starpu_worker_may_perform_blocking_calls()))
-		return -EDEADLK;
+	struct state_and_node *statenode = malloc(sizeof(struct state_and_node));
+	STARPU_ASSERT(statenode);
 
-	struct state_and_node statenode =
-	{
-		.state = handle,
-		.mode = mode,
-		.node = 0, // unused
-		.cond = PTHREAD_COND_INITIALIZER,
-		.lock = PTHREAD_MUTEX_INITIALIZER,
-		.finished = 0
-	};
+	statenode->state = handle;
+	statenode->mode = mode;
+	statenode->callback = callback;
+	statenode->callback_arg = arg;
+	PTHREAD_COND_INIT(&statenode->cond, NULL);
+	PTHREAD_MUTEX_INIT(&statenode->lock, NULL);
+	statenode->finished = 0;
 
 	/* we try to get the data, if we do not succeed immediately, we set a
  	* callback function that will be executed automatically when the data is
  	* available again, otherwise we fetch the data directly */
 	if (!_starpu_attempt_to_submit_data_request_from_apps(handle, mode,
-			_starpu_sync_data_with_mem_continuation, &statenode))
+			_starpu_sync_data_with_mem_continuation_non_blocking, statenode))
 	{
 		/* no one has locked this data yet, so we proceed immediately */
-		_starpu_sync_data_with_mem_continuation(&statenode);
-	}
-	else {
-		PTHREAD_MUTEX_LOCK(&statenode.lock);
-		while (!statenode.finished)
-			PTHREAD_COND_WAIT(&statenode.cond, &statenode.lock);
-		PTHREAD_MUTEX_UNLOCK(&statenode.lock);
+		_starpu_sync_data_with_mem_continuation_non_blocking(statenode);
 	}
 
 	return 0;
 }
 
-
+/*
+ *	Block data request from application
+ */
 static inline void _starpu_sync_data_with_mem_continuation(void *arg)
 {
 	int ret;
@@ -134,31 +133,40 @@ static inline void _starpu_sync_data_with_mem_continuation(void *arg)
 	PTHREAD_MUTEX_UNLOCK(&statenode->lock);
 }
 
+
 /* The data must be released by calling starpu_data_release_from_mem later on */
-int starpu_data_sync_with_mem_non_blocking(starpu_data_handle handle,
-		starpu_access_mode mode, void (*callback)(void *), void *arg)
+int starpu_data_sync_with_mem(starpu_data_handle handle, starpu_access_mode mode)
 {
 	STARPU_ASSERT(handle);
 
-	struct state_and_node *statenode = malloc(sizeof(struct state_and_node));
-	STARPU_ASSERT(statenode);
+	/* it is forbidden to call this function from a callback or a codelet */
+	if (STARPU_UNLIKELY(!_starpu_worker_may_perform_blocking_calls()))
+		return -EDEADLK;
 
-	statenode->state = handle;
-	statenode->mode = mode;
-	statenode->callback = callback;
-	statenode->callback_arg = arg;
-	PTHREAD_COND_INIT(&statenode->cond, NULL);
-	PTHREAD_MUTEX_INIT(&statenode->lock, NULL);
-	statenode->finished = 0;
+	struct state_and_node statenode =
+	{
+		.state = handle,
+		.mode = mode,
+		.node = 0, // unused
+		.cond = PTHREAD_COND_INITIALIZER,
+		.lock = PTHREAD_MUTEX_INITIALIZER,
+		.finished = 0
+	};
 
 	/* we try to get the data, if we do not succeed immediately, we set a
  	* callback function that will be executed automatically when the data is
  	* available again, otherwise we fetch the data directly */
 	if (!_starpu_attempt_to_submit_data_request_from_apps(handle, mode,
-			_starpu_sync_data_with_mem_continuation_non_blocking, statenode))
+			_starpu_sync_data_with_mem_continuation, &statenode))
 	{
 		/* no one has locked this data yet, so we proceed immediately */
-		_starpu_sync_data_with_mem_continuation_non_blocking(statenode);
+		_starpu_sync_data_with_mem_continuation(&statenode);
+	}
+	else {
+		PTHREAD_MUTEX_LOCK(&statenode.lock);
+		while (!statenode.finished)
+			PTHREAD_COND_WAIT(&statenode.cond, &statenode.lock);
+		PTHREAD_MUTEX_UNLOCK(&statenode.lock);
 	}
 
 	return 0;