Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C11 atomic lock alignment in data_t #685

Merged
merged 2 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions parsec/class/parsec_datacopy_future.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018-2020 The University of Tennessee and The University
* Copyright (c) 2018-2024 The University of Tennessee and The University
* of Tennessee Research Foundation. All rights
* reserved.
* Copyright (c) 2023 NVIDIA CORPORATION. All rights reserved.
Expand Down Expand Up @@ -238,8 +238,7 @@ static parsec_future_fn_t parsec_datacopy_future_functions = {
*/
static void parsec_datacopy_future_construct(parsec_base_future_t* future)
{
parsec_atomic_lock_t temp = PARSEC_ATOMIC_UNLOCKED;
future->future_lock = temp;
parsec_atomic_lock_init(&future->future_lock);
parsec_datacopy_future_t* d_fut = (parsec_datacopy_future_t*)future;
d_fut->super.future_class = &parsec_datacopy_future_functions;
d_fut->super.status = 0;
Expand Down
8 changes: 3 additions & 5 deletions parsec/class/parsec_future.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018-2020 The University of Tennessee and The University
* Copyright (c) 2018-2024 The University of Tennessee and The University
* of Tennessee Research Foundation. All rights
* reserved.
* Copyright (c) 2023 NVIDIA CORPORATION. All rights reserved.
Expand Down Expand Up @@ -125,20 +125,18 @@ static void parsec_base_future_construct(parsec_base_future_t* future)
future->status = 0;
future->cb_fulfill = NULL;
future->tracked_data = NULL;
parsec_atomic_lock_t temp = PARSEC_ATOMIC_UNLOCKED;
future->future_lock = temp;
parsec_atomic_lock_init(&future->future_lock);
}

static void parsec_countable_future_construct(parsec_base_future_t* future)
{
parsec_atomic_lock_t temp = PARSEC_ATOMIC_UNLOCKED;
future->future_lock = temp;
parsec_countable_future_t* c_fut = (parsec_countable_future_t*)future;
c_fut->super.future_class = &parsec_countable_future_functions;
c_fut->super.status = 0;
c_fut->super.cb_fulfill = NULL;
c_fut->super.tracked_data = NULL;
c_fut->count = 1;
parsec_atomic_lock_init(&future->future_lock);
}

PARSEC_OBJ_CLASS_INSTANCE(parsec_base_future_t, parsec_list_item_t,/*parsec_object_t,*/
Expand Down
5 changes: 2 additions & 3 deletions parsec/data.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012-2019 The University of Tennessee and The University
* Copyright (c) 2012-2024 The University of Tennessee and The University
* of Tennessee Research Foundation. All rights
* reserved.
*/
Expand Down Expand Up @@ -60,15 +60,14 @@ PARSEC_OBJ_CLASS_INSTANCE(parsec_data_copy_t, parsec_list_item_t,

static void parsec_data_construct(parsec_data_t* obj )
{
parsec_atomic_lock_t unlocked = PARSEC_ATOMIC_UNLOCKED;
obj->owner_device = -1;
obj->preferred_device = -1;
obj->key = 0;
obj->nb_elts = 0;
for( uint32_t i = 0; i < parsec_nb_devices;
obj->device_copies[i] = NULL, i++ );
obj->dc = NULL;
obj->lock = unlocked; /* Can't directly assign to PARSEC_ATOMIC_UNLOCKED because of C syntax */
parsec_atomic_lock_init(&obj->lock);
PARSEC_DEBUG_VERBOSE(20, parsec_debug_output, "Allocate data %p", obj);
}

Expand Down
19 changes: 12 additions & 7 deletions parsec/include/parsec/sys/atomic-c11.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2023 The University of Tennessee and The University
* Copyright (c) 2016-2024 The University of Tennessee and The University
* of Tennessee Research Foundation. All rights
* reserved.
*/
Expand Down Expand Up @@ -183,37 +183,42 @@ __int128_t parsec_atomic_fetch_add_int128(volatile __int128_t* l, __int128_t v)
#endif

/* Locks */

typedef volatile atomic_flag parsec_atomic_lock_t;
typedef union {
Copy link
Contributor

@bosilca bosilca Oct 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is ugly. What if you remove the volatile completely ? Or move it outside the union as in

typedef volatile union parsec_atomic_lock_u {
    atomic_flag flag;
    int atomic_external_aligner;
} parsec_atomic_lock_t;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the volatile is not useful I removed it and it passes all tests

atomic_flag flag;
int atomic_external_aligner;
} parsec_atomic_lock_t;
static_assert(sizeof(volatile int) >= sizeof(volatile atomic_flag),
"The type size for atomic_flag is larger than expected. Please report this error to PaRSEC developpers."
"You may compile without C11 atomic support (-DSUPPORT_C11=OFF in cmake) to fallback on other atomic types.");

# define PARSEC_ATOMIC_HAS_ATOMIC_INIT
ATOMIC_STATIC_INLINE
void parsec_atomic_lock_init( parsec_atomic_lock_t* atomic_lock )
{
atomic_flag_clear_explicit(atomic_lock, memory_order_relaxed);
atomic_flag_clear_explicit(&atomic_lock->flag, memory_order_relaxed);
}

#define PARSEC_ATOMIC_HAS_ATOMIC_LOCK
ATOMIC_STATIC_INLINE
void parsec_atomic_lock( parsec_atomic_lock_t* atomic_lock )
{
struct timespec ts = { .tv_sec = 0, .tv_nsec = 100 };
while( atomic_flag_test_and_set_explicit(atomic_lock, memory_order_acquire) )
while( atomic_flag_test_and_set_explicit(&atomic_lock->flag, memory_order_acquire) )
nanosleep( &ts, NULL ); /* less bandwidth consuming */
}

#define PARSEC_ATOMIC_HAS_ATOMIC_UNLOCK
ATOMIC_STATIC_INLINE
void parsec_atomic_unlock( parsec_atomic_lock_t* atomic_lock )
{
atomic_flag_clear_explicit(atomic_lock, memory_order_release);
atomic_flag_clear_explicit(&atomic_lock->flag, memory_order_release);
}

#define PARSEC_ATOMIC_HAS_ATOMIC_TRYLOCK
ATOMIC_STATIC_INLINE
int parsec_atomic_trylock( parsec_atomic_lock_t* atomic_lock )
{
return !atomic_flag_test_and_set_explicit(atomic_lock, memory_order_acquire);
return !atomic_flag_test_and_set_explicit(&atomic_lock->flag, memory_order_acquire);
}

#define PARSEC_ATOMIC_UNLOCKED ATOMIC_FLAG_INIT
2 changes: 1 addition & 1 deletion parsec/mca/device/device.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ static int parsec_device_verbose = 0;
uint32_t parsec_nb_devices = 0;
static uint32_t parsec_nb_max_devices = 0;
static uint32_t parsec_mca_device_are_freezed = 0;
parsec_atomic_lock_t parsec_devices_mutex = PARSEC_ATOMIC_UNLOCKED;
static parsec_atomic_lock_t parsec_devices_mutex = PARSEC_ATOMIC_UNLOCKED;
static parsec_device_module_t** parsec_devices = NULL;

static parsec_device_module_t* parsec_device_cpus = NULL;
Expand Down
3 changes: 1 addition & 2 deletions parsec/mca/device/device.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013-2023 The University of Tennessee and The University
* Copyright (c) 2013-2024 The University of Tennessee and The University
* of Tennessee Research Foundation. All rights
* reserved.
*/
Expand Down Expand Up @@ -189,7 +189,6 @@ PARSEC_OBJ_CLASS_DECLARATION(parsec_device_module_t);

extern uint32_t parsec_nb_devices;
extern int parsec_device_output;
extern parsec_atomic_lock_t parsec_devices_mutex;

/**
* @brief Find the best device to execute the kernel based on the compute
Expand Down
Loading