2022-09-13 15:36:55 +02:00
|
|
|
/* Copyright (c) 2022 Barcelona Supercomputing Center (BSC)
|
|
|
|
* SPDX-License-Identifier: GPL-3.0-or-later */
|
|
|
|
|
|
|
|
#define _POSIX_C_SOURCE 2
|
|
|
|
|
|
|
|
#include <nanos6.h>
|
|
|
|
#include <nanos6/debug.h>
|
|
|
|
#include <stdatomic.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
static int ncpus = -1;
|
|
|
|
static long nruns = 3L;
|
2022-09-29 12:32:56 +02:00
|
|
|
static long ntasks = 200L;
|
2022-09-13 15:36:55 +02:00
|
|
|
|
2022-09-29 12:32:56 +02:00
|
|
|
static atomic_int nhandles = 0;
|
2022-09-13 15:36:55 +02:00
|
|
|
static void **handle;
|
|
|
|
|
|
|
|
#pragma oss task
|
|
|
|
static void
|
|
|
|
do_task(int t)
|
|
|
|
{
|
2022-09-29 12:32:56 +02:00
|
|
|
handle[t] = nanos6_get_current_blocking_context();
|
|
|
|
if (handle[t] == NULL)
|
|
|
|
abort();
|
|
|
|
|
|
|
|
atomic_fetch_add(&nhandles, 1);
|
|
|
|
nanos6_block_current_task(handle[t]);
|
2022-09-13 15:36:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
do_run(void)
|
|
|
|
{
|
|
|
|
memset(handle, 0, ntasks * sizeof(void *));
|
2022-09-29 12:32:56 +02:00
|
|
|
atomic_store(&nhandles, 0);
|
2022-09-13 15:36:55 +02:00
|
|
|
|
2022-09-29 15:34:44 +02:00
|
|
|
for (int t = 0; t < ntasks; t++)
|
2022-09-13 15:36:55 +02:00
|
|
|
do_task(t);
|
|
|
|
|
2022-09-29 12:32:56 +02:00
|
|
|
/* Wait for all tasks to fill the handle */
|
2022-09-29 15:34:44 +02:00
|
|
|
while (atomic_load(&nhandles) < ntasks)
|
|
|
|
;
|
2022-09-13 15:36:55 +02:00
|
|
|
|
2022-09-29 12:32:56 +02:00
|
|
|
/* Is ok if we call unblock before the block happens */
|
2022-09-29 15:34:44 +02:00
|
|
|
for (int t = 0; t < ntasks; t++) {
|
2022-09-29 12:32:56 +02:00
|
|
|
if (handle[t] == NULL)
|
|
|
|
abort();
|
|
|
|
|
|
|
|
nanos6_unblock_task(handle[t]);
|
2022-09-13 15:36:55 +02:00
|
|
|
}
|
|
|
|
|
2022-09-29 15:34:44 +02:00
|
|
|
#pragma oss taskwait
|
2022-09-13 15:36:55 +02:00
|
|
|
}
|
|
|
|
|
2022-09-29 15:34:44 +02:00
|
|
|
static int
|
|
|
|
get_ncpus(void)
|
2022-09-13 15:36:55 +02:00
|
|
|
{
|
|
|
|
return (int) nanos6_get_num_cpus();
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(void)
|
|
|
|
{
|
|
|
|
ncpus = get_ncpus();
|
|
|
|
|
|
|
|
handle = calloc(ntasks, sizeof(void *));
|
|
|
|
|
2022-09-29 15:34:44 +02:00
|
|
|
if (handle == NULL) {
|
2022-09-13 15:36:55 +02:00
|
|
|
perror("calloc failed");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2022-09-29 15:34:44 +02:00
|
|
|
for (int run = 0; run < nruns; run++)
|
2022-09-13 15:36:55 +02:00
|
|
|
do_run();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|