コード例 #1
0
ファイル: mutex.c プロジェクト: saeidbarati157/linrar
void thread_func(void *arg)
{
    thread_arg_t *t_arg = (thread_arg_t *)arg;

    ABT_thread_yield();

    ABT_mutex_lock(t_arg->mutex);
    g_counter++;
    ABT_mutex_unlock(t_arg->mutex);
    ABT_test_printf(1, "[TH%d] increased\n", t_arg->id);

    ABT_thread_yield();
}
コード例 #2
0
/* Function to compute Fibonacci numbers */
void fibonacci_task(void *arguments)
{
    int n, result;
    task_args *a1, *a2, *parent, *temp;
    ABT_task t1, t2;

    task_args *args = (task_args *)arguments;
    n = args->n;
    parent = args->parent;

    /* checking for base cases */
    if (n <= 2) {
        args->result = 1;
        result = 1;
        int flag = 1;
        while (flag && parent != NULL) {
            ABT_mutex_lock(parent->mutex);
            parent->result += result;
            if (result == parent->result) flag = 0;
            ABT_mutex_unlock(parent->mutex);
            result = parent->result;
            temp = parent->parent;

            if (flag && temp) {
                ABT_mutex_free(&parent->mutex);
                free(parent);
            }

            parent = temp;
        }

        ABT_mutex_free(&args->mutex);
        if (args->parent) {
            free(args);
        }
    } else {
        a1 = (task_args *)malloc(sizeof(task_args));
        a1->n = n - 1;
        a1->result = 0;
        ABT_mutex_create(&a1->mutex);
        a1->parent = args;
        ABT_task_create(g_pool, fibonacci_task, a1, &t1);

        a2 = (task_args *)malloc(sizeof(task_args));
        a2->n = n - 2;
        a2->result = 0;
        ABT_mutex_create(&a2->mutex);
        a2->parent = args;
        ABT_task_create(g_pool, fibonacci_task, a2, &t2);
    }
}
コード例 #3
0
ファイル: dyn_event_rt1.c プロジェクト: JohnPJenkins/argobots
/* This function is the second callback function for the event,
 * ABT_EVENT_ADD_XSTREAM.  After all the first callback functions registered
 * for ABT_EVENT_ADD_XSTREAM return ABT_TRUE for the target ES, the Argobots
 * runtime will call this second callback function to notify that we have
 * reached an agreement. For now, the creation of the target ES needs to be
 * happened in this callback function.
 *
 * Parameters:
 *  [in] user_arg: user-provided argument
 *  [in] abt_arg : runtime-provided argument, the rank of ES to create
 */
static ABT_bool rt1_act_add_xstream(void *user_arg, void *abt_arg)
{
    rt1_data_t *my_data = (rt1_data_t *)user_arg;
    int tar_rank = (int)(intptr_t)abt_arg;
    ABT_bool result = ABT_TRUE;
    ABT_pool pool;
    ABT_xstream tar_xstream;
    int rank, ret;

    /* Create a new ES */
    if (tar_rank == ABT_XSTREAM_ANY_RANK) {
        ret = ABT_xstream_create(ABT_SCHED_NULL, &tar_xstream);
        if (ret != ABT_SUCCESS) {
            result = ABT_FALSE;
            goto fn_exit;
        }
        ABT_xstream_get_rank(tar_xstream, &rank);
    } else {
        rank = tar_rank;
        ret = ABT_xstream_create_with_rank(ABT_SCHED_NULL, rank, &tar_xstream);
        if (ret != ABT_SUCCESS) {
            printf("ES%d: failed to create\n", rank);
            result = ABT_FALSE;
            goto fn_exit;
        }
    }
    ABT_mutex_spinlock(my_data->mutex);
    assert(rank < my_data->max_xstreams &&
           my_data->xstreams[rank] == ABT_XSTREAM_NULL);
    my_data->xstreams[rank] = tar_xstream;
    my_data->num_xstreams++;
    g_xstreams[rank] = tar_xstream;
    ABT_mutex_unlock(my_data->mutex);

    ABT_xstream_get_main_pools(tar_xstream, 1, &pool);
    ABT_thread_create(pool, rt1_launcher, (void *)(intptr_t)rank,
                      ABT_THREAD_ATTR_NULL, NULL);
    printf("ES%d: created\n", rank);

  fn_exit:
    return result;
}
コード例 #4
0
ファイル: dyn_app.c プロジェクト: saeidbarati157/linrar
static void abt_check_events(int idx)
{
    int n, ret;
    char recv_buf[RECV_BUF_LEN];

    if (!abt_alive) return;

    ret = ABT_mutex_trylock(g_mutex);
    if (ret == ABT_ERR_MUTEX_LOCKED) return;
    assert(ret == ABT_SUCCESS);

    ret = poll(&abt_pfd, 1, 1);
    if (ret == -1) {
        handle_error("ERROR: poll");
    } else if (ret != 0) {
        if (abt_pfd.revents & POLLIN) {
            bzero(recv_buf, RECV_BUF_LEN);
            n = read(abt_pfd.fd, recv_buf, RECV_BUF_LEN);
            if (n < 0) handle_error("ERROR: read");

            printf("\nES%d: received request '%c'\n", idx, recv_buf[0]);
            switch (recv_buf[0]) {
                case 'd': decrease_xstream(); break;
                case 'i': increase_xstream(); break;
                case 'n': send_num_xstream(); break;
                case 'q': abt_disconnect(); break;
                default:
                    printf("Unknown commend: %s\n", recv_buf);
                    break;
            }
        }

        if (abt_pfd.revents & POLLRDHUP) {
            abt_alive = 0;
            printf("Server disconnected...\n");
        }
        abt_pfd.revents = 0;
    }

    ABT_mutex_unlock(g_mutex);
}
コード例 #5
0
ファイル: rdb_rpc.c プロジェクト: daos-stack/daos
/* Daemon ULT for processing RPC replies */
void
rdb_recvd(void *arg)
{
	struct rdb *db = arg;

	D_DEBUG(DB_MD, DF_DB": recvd starting\n", DP_DB(db));
	for (;;) {
		struct rdb_raft_rpc    *rrpc = NULL;
		bool			stop;

		ABT_mutex_lock(db->d_mutex);
		for (;;) {
			stop = db->d_stop;
			if (!d_list_empty(&db->d_replies)) {
				rrpc = d_list_entry(db->d_replies.next,
						    struct rdb_raft_rpc,
						    drc_entry);
				d_list_del_init(&rrpc->drc_entry);
				break;
			}
			if (stop)
				break;
			ABT_cond_wait(db->d_replies_cv, db->d_mutex);
		}
		ABT_mutex_unlock(db->d_mutex);
		if (rrpc == NULL) {
			D_ASSERT(stop);
			/* The queue is empty and we are asked to stop. */
			break;
		}
		/*
		 * The queue has pending replies. If we are asked to stop, skip
		 * the processing but still free the RPCs until the queue
		 * become empty.
		 */
		if (!stop)
			rdb_raft_process_reply(db, rrpc->drc_rpc);
		rdb_raft_free_request(db, rrpc->drc_rpc);
		rdb_free_raft_rpc(rrpc);
		ABT_thread_yield();
	}
コード例 #6
0
ファイル: dyn_event_rt1.c プロジェクト: JohnPJenkins/argobots
void rt1_launcher(void *arg)
{
    int idx = (int)(intptr_t)arg;
    ABT_thread cur_thread;
    ABT_pool cur_pool;
    ABT_sched_config config;
    ABT_sched sched;
    size_t size;
    double t_start, t_end;

    ABT_sched_config_var cv_event_freq = {
        .idx = 0,
        .type = ABT_SCHED_CONFIG_INT
    };

    ABT_sched_config_var cv_idx = {
        .idx = 1,
        .type = ABT_SCHED_CONFIG_INT
    };

    ABT_sched_def sched_def = {
        .type = ABT_SCHED_TYPE_ULT,
        .init = sched_init,
        .run = sched_run,
        .free = sched_free,
        .get_migr_pool = NULL
    };

    /* Create a scheduler */
    ABT_sched_config_create(&config,
                            cv_event_freq, 10,
                            cv_idx, idx,
                            ABT_sched_config_var_end);
    ABT_sched_create(&sched_def, 1, &rt1_data->pool, config, &sched);

    /* Push the scheduler to the current pool */
    ABT_thread_self(&cur_thread);
    ABT_thread_get_last_pool(cur_thread, &cur_pool);
    ABT_pool_add_sched(cur_pool, sched);

    /* Free */
    ABT_sched_config_free(&config);

    t_start = ABT_get_wtime();
    while (1) {
        rt1_app(idx);

        ABT_pool_get_total_size(cur_pool, &size);
        if (size == 0) {
            ABT_sched_free(&sched);
            int rank;
            ABT_xstream_self_rank(&rank);
            printf("ES%d: finished\n", rank);
            ABT_mutex_lock(rt1_data->mutex);
            rt1_data->xstreams[rank] = ABT_XSTREAM_NULL;
            rt1_data->num_xstreams--;
            ABT_mutex_unlock(rt1_data->mutex);
            break;
        }

        t_end = ABT_get_wtime();
        if ((t_end - t_start) > g_timeout) {
            ABT_sched_finish(sched);
        }
    }
}

static void rt1_app(int eid)
{
    int i, num_comps;
    size_t size;
    ABT_thread cur_thread;
    ABT_pool cur_pool;

    ABT_thread_self(&cur_thread);
    ABT_thread_get_last_pool(cur_thread, &cur_pool);

    if (eid == 0) ABT_event_prof_start();

    num_comps = rt1_data->num_comps;
    for (i = 0; i < num_comps * 2; i += 2) {
        ABT_thread_create(rt1_data->pool, rt1_app_compute,
                          (void *)(intptr_t)(eid * num_comps * 2 + i),
                          ABT_THREAD_ATTR_NULL, NULL);
        ABT_task_create(rt1_data->pool, rt1_app_compute,
                        (void *)(intptr_t)(eid * num_comps * 2 + i + 1),
                        NULL);
    }

    do {
        ABT_thread_yield();

        /* If the size of cur_pool is zero, it means the stacked scheduler has
         * been terminated because of the shrinking event. */
        ABT_pool_get_total_size(cur_pool, &size);
        if (size == 0) break;

        ABT_pool_get_total_size(rt1_data->pool, &size);
    } while (size > 0);

    if (eid == 0) {
        ABT_event_prof_stop();

        int cnt = __atomic_exchange_n(&rt1_data->cnt, 0, __ATOMIC_SEQ_CST);
        double local_work = (double)(cnt * rt1_data->num_iters);
        ABT_event_prof_publish("ops", local_work, local_work);
    }
}

static void rt1_app_compute(void *arg)
{
    int pos = (int)(intptr_t)arg;
    int i;

    rt1_data->app_data[pos] = 0;
    for (i = 0; i < rt1_data->num_iters; i++) {
        rt1_data->app_data[pos] += sin((double)pos);
    }

    __atomic_fetch_add(&rt1_data->cnt, 1, __ATOMIC_SEQ_CST);
}
コード例 #7
0
ファイル: glt_mutex.c プロジェクト: adcastel/GLT
GLT_func_prefix void glt_mutex_unlock(GLT_mutex mutex) {
    CHECK(ABT_mutex_unlock(mutex),ABT_SUCCESS);
}