diff options
author | Nicolas Koenig <koenigni@student.ethz.ch> | 2020-11-15 12:40:05 +0100 |
---|---|---|
committer | Nicolas Koenig <koenigni@student.ethz.ch> | 2020-11-15 12:41:22 +0100 |
commit | 649754c5b4a888c2c69c1a9cbeb1c356899934c1 (patch) | |
tree | 7c2b6fa53d32a8a44d9b7607f012b8197f01a5cb /libgfortran | |
parent | dd06247d91866b75c2f5512320737a31da7a6e16 (diff) |
Do not do overflow checks on allocating shared coarrays; reformatting.
gcc/fortran/ChangeLog:
PR fortran/97589
* trans-array.c (gfc_array_allocate): Do not do overflow checks
for shared coarrays.
libgfortran/ChangeLog:
* nca/alloc.c: Reformat.
* nca/allocator.c: Reformat.
* nca/coarraynative.c: Reformat.
* nca/collective_subroutine.c Reformat.
* nca/counter_barrier.c: Reformat.
* nca/hashmap.c: Reformat.
* nca/shared_memory.c: Reformat.
* nca/sync.c: Reformat.
* nca/util.c: Reformat.
* nca/wrapper.c: Reformat.
Diffstat (limited to 'libgfortran')
-rw-r--r-- | libgfortran/nca/alloc.c | 30 | ||||
-rw-r--r-- | libgfortran/nca/allocator.c | 20 | ||||
-rw-r--r-- | libgfortran/nca/coarraynative.c | 124 | ||||
-rw-r--r-- | libgfortran/nca/collective_subroutine.c | 98 | ||||
-rw-r--r-- | libgfortran/nca/counter_barrier.c | 23 | ||||
-rw-r--r-- | libgfortran/nca/hashmap.c | 206 | ||||
-rw-r--r-- | libgfortran/nca/shared_memory.c | 68 | ||||
-rw-r--r-- | libgfortran/nca/sync.c | 49 | ||||
-rw-r--r-- | libgfortran/nca/util.c | 102 | ||||
-rw-r--r-- | libgfortran/nca/wrapper.c | 125 |
10 files changed, 434 insertions, 411 deletions
diff --git a/libgfortran/nca/alloc.c b/libgfortran/nca/alloc.c index b6246d9ed08..ae457295324 100644 --- a/libgfortran/nca/alloc.c +++ b/libgfortran/nca/alloc.c @@ -50,7 +50,7 @@ get_memory_by_id_internal (alloc_iface *iface, size_t size, memid id, void *ret; pthread_mutex_lock (&iface->as->lock); - shared_memory_prepare(iface->mem); + shared_memory_prepare (iface->mem); res = hashmap_get (&iface->hm, id); @@ -59,12 +59,14 @@ get_memory_by_id_internal (alloc_iface *iface, size_t size, memid id, size_t found_size; found_size = hm_search_result_size (&res); if (found_size != size) - { - dprintf (2, "Size mismatch for coarray allocation id %p: " - "found = %lu != size = %lu\n", (void *) id, found_size, size); - pthread_mutex_unlock (&iface->as->lock); + { + dprintf (2, + "Size mismatch for coarray allocation id %p: found = %lu " + "!= size = %lu\n", + (void *)id, found_size, size); + pthread_mutex_unlock (&iface->as->lock); exit (1); - } + } shared_ptr = hm_search_result_ptr (&res); hashmap_inc (&iface->hm, id, &res); } @@ -98,21 +100,21 @@ get_memory_by_id_zero (alloc_iface *iface, size_t size, memid id) holds that memory segment, decrease the reference count otherwise. */ void -free_memory_with_id (alloc_iface* iface, memid id) +free_memory_with_id (alloc_iface *iface, memid id) { hashmap_search_result res; int entries_left; pthread_mutex_lock (&iface->as->lock); - shared_memory_prepare(iface->mem); + shared_memory_prepare (iface->mem); res = hashmap_get (&iface->hm, id); if (!hm_search_result_contains (&res)) { pthread_mutex_unlock (&iface->as->lock); char buffer[100]; - snprintf (buffer, sizeof(buffer), "Error in free_memory_with_id: " - "%p not found", (void *) id); + snprintf (buffer, sizeof (buffer), + "Error in free_memory_with_id: %p not found", (void *)id); /* FIXME: For some reason, internal_error (NULL, buffer) fails to link, * so we use dprintf at the moment. */ dprintf (2, buffer); @@ -120,12 +122,12 @@ free_memory_with_id (alloc_iface* iface, memid id) } entries_left = hashmap_dec (&iface->hm, id, &res); - assert (entries_left >=0); + assert (entries_left >= 0); if (entries_left == 0) { shared_free (&iface->alloc, hm_search_result_ptr (&res), - hm_search_result_size (&res)); + hm_search_result_size (&res)); } pthread_mutex_unlock (&iface->as->lock); @@ -138,7 +140,7 @@ free_memory_with_id (alloc_iface* iface, memid id) void alloc_iface_init (alloc_iface *iface, shared_memory *mem) { - + iface->as = SHARED_MEMORY_RAW_ALLOC_PTR (mem, alloc_iface_shared); iface->mem = mem; initialize_shared_mutex (&iface->as->lock); @@ -147,7 +149,7 @@ alloc_iface_init (alloc_iface *iface, shared_memory *mem) } allocator * -get_allocator (alloc_iface * iface) +get_allocator (alloc_iface *iface) { return &iface->alloc; } diff --git a/libgfortran/nca/allocator.c b/libgfortran/nca/allocator.c index e7aa9fdefd0..e31b66a807e 100644 --- a/libgfortran/nca/allocator.c +++ b/libgfortran/nca/allocator.c @@ -29,13 +29,14 @@ see the files COPYING3 and COPYING.RUNTIME respectively. If not, see #include "shared_memory.h" #include "allocator.h" -typedef struct { +typedef struct +{ shared_mem_ptr next; } bucket; /* Initialize the allocator. */ -void +void allocator_init (allocator *a, allocator_shared *s, shared_memory *sm) { a->s = s; @@ -49,7 +50,7 @@ allocator_init (allocator *a, allocator_shared *s, shared_memory *sm) #define MAX_ALIGN 16 -shared_mem_ptr +shared_mem_ptr shared_malloc (allocator *a, size_t size) { shared_mem_ptr ret; @@ -59,22 +60,23 @@ shared_malloc (allocator *a, size_t size) sz = next_power_of_two (size); act_size = sz > sizeof (bucket) ? sz : sizeof (bucket); - bucket_list_index = __builtin_clzl(act_size); + bucket_list_index = __builtin_clzl (act_size); if (SHMPTR_IS_NULL (a->s->free_bucket_head[bucket_list_index])) return shared_memory_get_mem_with_alignment (a->shm, act_size, MAX_ALIGN); ret = a->s->free_bucket_head[bucket_list_index]; a->s->free_bucket_head[bucket_list_index] - = (SHMPTR_AS (bucket *, ret, a->shm)->next); - assert(ret.offset != 0); + = (SHMPTR_AS (bucket *, ret, a->shm)->next); + assert (ret.offset != 0); return ret; } /* Free memory. */ void -shared_free (allocator *a, shared_mem_ptr p, size_t size) { +shared_free (allocator *a, shared_mem_ptr p, size_t size) +{ bucket *b; size_t sz; int bucket_list_index; @@ -82,9 +84,9 @@ shared_free (allocator *a, shared_mem_ptr p, size_t size) { sz = next_power_of_two (size); act_size = sz > sizeof (bucket) ? sz : sizeof (bucket); - bucket_list_index = __builtin_clzl(act_size); + bucket_list_index = __builtin_clzl (act_size); - b = SHMPTR_AS(bucket *, p, a->shm); + b = SHMPTR_AS (bucket *, p, a->shm); b->next = a->s->free_bucket_head[bucket_list_index]; a->s->free_bucket_head[bucket_list_index] = p; } diff --git a/libgfortran/nca/coarraynative.c b/libgfortran/nca/coarraynative.c index c177fe8cea7..821d69b3db9 100644 --- a/libgfortran/nca/coarraynative.c +++ b/libgfortran/nca/coarraynative.c @@ -22,7 +22,6 @@ a copy of the GCC Runtime Library Exception along with this program; see the files COPYING3 and COPYING.RUNTIME respectively. If not, see <http://www.gnu.org/licenses/>. */ - #include "libgfortran.h" #include "libcoarraynative.h" #include "allocator.h" @@ -39,7 +38,7 @@ see the files COPYING3 and COPYING.RUNTIME respectively. If not, see nca_local_data *local = NULL; -image this_image = {-1, NULL}; +image this_image = { -1, NULL }; /* Get image number from environment or sysconf. */ @@ -62,46 +61,46 @@ static master * get_master (void) { master *m; - m = SHMPTR_AS (master *, - shared_memory_get_mem_with_alignment - (&local->sm, - sizeof (master) + sizeof(image_status) * local->total_num_images, - __alignof__(master)), &local->sm); + m = SHMPTR_AS ( + master *, + shared_memory_get_mem_with_alignment ( + &local->sm, + sizeof (master) + sizeof (image_status) * local->total_num_images, + __alignof__(master)), + &local->sm); m->has_failed_image = 0; m->finished_images = 0; waitable_counter_init (&m->num_active_images, local->total_num_images); return m; } - /* Ensure things are initialized. */ -void -ensure_initialization(void) +void +ensure_initialization (void) { if (local) return; - local = malloc(sizeof(nca_local_data)); // Is malloc already init'ed at that - // point? Maybe use mmap(MAP_ANON) - // instead - pagesize = sysconf (_SC_PAGE_SIZE); + local = malloc (sizeof (nca_local_data)); // Is malloc already init'ed at + // that point? Maybe use + // mmap(MAP_ANON) instead + pagesize = sysconf (_SC_PAGE_SIZE); local->total_num_images = get_environ_image_num (); shared_memory_init (&local->sm); shared_memory_prepare (&local->sm); - if (this_image.m == NULL) /* A bit of a hack, but we - need the master early. */ - this_image.m = get_master(); + if (this_image.m == NULL) /* A bit of a hack, but we + need the master early. */ + this_image.m = get_master (); alloc_iface_init (&local->ai, &local->sm); collsub_iface_init (&local->ci, &local->ai, &local->sm); sync_iface_init (&local->si, &local->ai, &local->sm); } - /* Test for failed or stopped images. */ int -test_for_cas_errors (int *stat, char *errmsg, size_t errmsg_length) +test_for_cas_errors (int *stat, char *errmsg, size_t errmsg_length) { size_t errmsg_written_bytes; if (!stat) @@ -113,33 +112,33 @@ test_for_cas_errors (int *stat, char *errmsg, size_t errmsg_length) *stat = CAS_STAT_STOPPED_IMAGE; if (errmsg) { - errmsg_written_bytes - = snprintf(errmsg, errmsg_length, - "Stopped images present (currently %d)", - this_image.m->finished_images); + errmsg_written_bytes = snprintf (errmsg, errmsg_length, + "Stopped images present (currently " + "%d)", + this_image.m->finished_images); if (errmsg_written_bytes > errmsg_length - 1) errmsg_written_bytes = errmsg_length - 1; - - memset(errmsg + errmsg_written_bytes, ' ', - errmsg_length - errmsg_written_bytes); + + memset (errmsg + errmsg_written_bytes, ' ', + errmsg_length - errmsg_written_bytes); } } else if (this_image.m->has_failed_image) { *stat = CAS_STAT_FAILED_IMAGE; - if (errmsg) - { - errmsg_written_bytes - = snprintf(errmsg, errmsg_length, - "Failed images present (currently %d)", - this_image.m->has_failed_image); - if (errmsg_written_bytes > errmsg_length - 1) - errmsg_written_bytes = errmsg_length - 1; - - memset(errmsg + errmsg_written_bytes, ' ', - errmsg_length - errmsg_written_bytes); + if (errmsg) + { + errmsg_written_bytes = snprintf (errmsg, errmsg_length, + "Failed images present (currently " + "%d)", + this_image.m->has_failed_image); + if (errmsg_written_bytes > errmsg_length - 1) + errmsg_written_bytes = errmsg_length - 1; + + memset (errmsg + errmsg_written_bytes, ' ', + errmsg_length - errmsg_written_bytes); + } } - } else { *stat = 0; @@ -150,8 +149,8 @@ test_for_cas_errors (int *stat, char *errmsg, size_t errmsg_length) /* Check if an image is active. */ -int -master_is_image_active (master *m, int image_num) +int +master_is_image_active (master *m, int image_num) { return m->images[image_num].status == IMAGE_OK; } @@ -159,27 +158,27 @@ master_is_image_active (master *m, int image_num) /* Get number of active images. */ int -master_get_num_active_images (master *m) +master_get_num_active_images (master *m) { - return waitable_counter_get_val(&m->num_active_images); + return waitable_counter_get_val (&m->num_active_images); } /* Bind barrier to counter. */ void -master_bind_active_image_barrier(master *m, counter_barrier *b) +master_bind_active_image_barrier (master *m, counter_barrier *b) { - bind_counter_barrier(b, &m->num_active_images); + bind_counter_barrier (b, &m->num_active_images); } /* Main wrapper. */ -static void __attribute__((noreturn)) +static void __attribute__ ((noreturn)) image_main_wrapper (void (*image_main) (void), image *this) { this_image = *this; - sync_all(&local->si); + sync_all (&local->si); image_main (); @@ -187,41 +186,44 @@ image_main_wrapper (void (*image_main) (void), image *this) } void -error_on_missing_images(void) { - if (master_get_num_active_images(this_image.m) != local->total_num_images) - exit(1); +error_on_missing_images (void) +{ + if (master_get_num_active_images (this_image.m) != local->total_num_images) + exit (1); } /* This is called from main, with a pointer to the user's program as argument. It forks the images and waits for their completion. */ void -cas_master (void (*image_main) (void)) { +cas_master (void (*image_main) (void)) +{ master *m; int i, j; pid_t new; image im; int exit_code = 0; int chstatus; - ensure_initialization(); + ensure_initialization (); m = this_image.m; im.m = m; - for (im.image_num = 0; im.image_num < local->total_num_images; im.image_num++) + for (im.image_num = 0; im.image_num < local->total_num_images; + im.image_num++) { - if ((new = fork())) - { + if ((new = fork ())) + { if (new == -1) { - dprintf(2, "error spawning child\n"); + dprintf (2, "error spawning child\n"); exit_code = 1; } m->images[im.image_num].pid = new; m->images[im.image_num].status = IMAGE_OK; - } + } else - image_main_wrapper(image_main, &im); + image_main_wrapper (image_main, &im); } for (i = 0; i < local->total_num_images; i++) { @@ -229,14 +231,16 @@ cas_master (void (*image_main) (void)) { if (WIFEXITED (chstatus) && !WEXITSTATUS (chstatus)) { j = 0; - for (; j < local->total_num_images && m->images[j].pid != new; j++); + for (; j < local->total_num_images && m->images[j].pid != new; j++) + ; m->images[j].status = IMAGE_SUCCESS; m->finished_images++; /* FIXME: Needs to be atomic, probably. */ } else if (!WIFEXITED (chstatus) || WEXITSTATUS (chstatus)) { j = 0; - for (; j < local->total_num_images && m->images[j].pid != new; j++); + for (; j < local->total_num_images && m->images[j].pid != new; j++) + ; m->images[j].status = IMAGE_FAILED; m->has_failed_image++; /* FIXME: Needs to be atomic, probably. */ for (; j < local->total_num_images; j++) @@ -244,7 +248,7 @@ cas_master (void (*image_main) (void)) { dprintf (2, "ERROR: Image %d(%#x) failed\n", j, new); exit_code = 1; } - waitable_counter_add(&m->num_active_images, -1); + waitable_counter_add (&m->num_active_images, -1); } exit (exit_code); } diff --git a/libgfortran/nca/collective_subroutine.c b/libgfortran/nca/collective_subroutine.c index f4d9b589e4d..875eb946e60 100644 --- a/libgfortran/nca/collective_subroutine.c +++ b/libgfortran/nca/collective_subroutine.c @@ -51,7 +51,6 @@ get_collsub_buf (collsub_iface *ci, size_t size) return ret; } - /* This function syncs all images with one another. It will only return once all images have called it. */ @@ -61,19 +60,19 @@ collsub_sync (collsub_iface *ci) counter_barrier_wait (&ci->s->barrier); } - /* assign_function is needed since we only know how to assign the type inside the compiler. It should be implemented as follows: - - void assign_function (void *a, void *b) + + void assign_function (void *a, void *b) { *((t *) a) = reduction_operation ((t *) a, (t *) b); } - + */ void -collsub_reduce_array (collsub_iface *ci, gfc_array_char *desc, int *result_image, +collsub_reduce_array (collsub_iface *ci, gfc_array_char *desc, + int *result_image, void (*assign_function) (void *, void *)) { void *buffer; @@ -85,7 +84,7 @@ collsub_reduce_array (collsub_iface *ci, gfc_array_char *desc, int *result_image index_type this_image_size_bytes; char *this_image_buf; - error_on_missing_images(); + error_on_missing_images (); packed = pack_array_prepare (&pi, desc); if (pi.num_elem == 0) @@ -94,7 +93,8 @@ collsub_reduce_array (collsub_iface *ci, gfc_array_char *desc, int *result_image elem_size = GFC_DESCRIPTOR_SIZE (desc); this_image_size_bytes = elem_size * pi.num_elem; - buffer = get_collsub_buf (ci, this_image_size_bytes * local->total_num_images); + buffer + = get_collsub_buf (ci, this_image_size_bytes * local->total_num_images); this_image_buf = buffer + this_image_size_bytes * this_image.image_num; if (packed) @@ -103,31 +103,33 @@ collsub_reduce_array (collsub_iface *ci, gfc_array_char *desc, int *result_image pack_array_finish (&pi, desc, this_image_buf); collsub_sync (ci); - - for (; ((this_image.image_num >> cbit) & 1) == 0 && (local->total_num_images >> cbit) != 0; cbit++) + for (; ((this_image.image_num >> cbit) & 1) == 0 + && (local->total_num_images >> cbit) != 0; + cbit++) { imoffset = 1 << cbit; if (this_image.image_num + imoffset < local->total_num_images) /* Reduce arrays elementwise. */ - for (ssize_t i = 0; i < pi.num_elem; i++) + for (ssize_t i = 0; i < pi.num_elem; i++) assign_function (this_image_buf + elem_size * i, - this_image_buf + this_image_size_bytes * imoffset + elem_size * i); - + this_image_buf + this_image_size_bytes * imoffset + + elem_size * i); + collsub_sync (ci); } - for ( ; (local->total_num_images >> cbit) != 0; cbit++) + for (; (local->total_num_images >> cbit) != 0; cbit++) collsub_sync (ci); - + if (!result_image || *result_image == this_image.image_num) - { + { if (packed) - memcpy (GFC_DESCRIPTOR_DATA (desc), buffer, this_image_size_bytes); + memcpy (GFC_DESCRIPTOR_DATA (desc), buffer, this_image_size_bytes); else - unpack_array_finish(&pi, desc, buffer); + unpack_array_finish (&pi, desc, buffer); } - finish_collective_subroutine (ci); + finish_collective_subroutine (ci); } void @@ -140,32 +142,36 @@ collsub_reduce_scalar (collsub_iface *ci, void *obj, index_type elem_size, int imoffset; char *this_image_buf; - error_on_missing_images(); + error_on_missing_images (); - buffer = get_collsub_buf (ci, elem_size * master_get_num_active_images(this_image.m)); + buffer = get_collsub_buf ( + ci, elem_size * master_get_num_active_images (this_image.m)); this_image_buf = buffer + elem_size * this_image.image_num; memcpy (this_image_buf, obj, elem_size); - collsub_sync (ci); - for (; ((this_image.image_num >> cbit) & 1) == 0 && (local->total_num_images >> cbit) != 0; cbit++) + for (; ((this_image.image_num >> cbit) & 1) == 0 + && (local->total_num_images >> cbit) != 0; + cbit++) { imoffset = 1 << cbit; - - if (this_image.image_num + imoffset < local->total_num_images) { - /* Reduce arrays elementwise. */ - assign_function (this_image_buf, this_image_buf + elem_size*imoffset); - } + + if (this_image.image_num + imoffset < local->total_num_images) + { + /* Reduce arrays elementwise. */ + assign_function (this_image_buf, + this_image_buf + elem_size * imoffset); + } collsub_sync (ci); } - for ( ; (master_get_num_active_images(this_image.m) >> cbit) != 0; cbit++) + for (; (master_get_num_active_images (this_image.m) >> cbit) != 0; cbit++) collsub_sync (ci); - + if (!result_image || *result_image == this_image.image_num) - memcpy(obj, buffer, elem_size); + memcpy (obj, buffer, elem_size); - finish_collective_subroutine (ci); + finish_collective_subroutine (ci); } /* Do not use sync_all(), because the program should deadlock in the case that @@ -175,13 +181,13 @@ collsub_reduce_scalar (collsub_iface *ci, void *obj, index_type elem_size, void collsub_iface_init (collsub_iface *ci, alloc_iface *ai, shared_memory *sm) { - ci->s = SHARED_MEMORY_RAW_ALLOC_PTR(sm, collsub_iface_shared); + ci->s = SHARED_MEMORY_RAW_ALLOC_PTR (sm, collsub_iface_shared); - ci->s->collsub_buf = shared_malloc(get_allocator(ai), - sizeof(double)*local->total_num_images); - ci->s->curr_size = sizeof(double)*local->total_num_images; + ci->s->collsub_buf = shared_malloc ( + get_allocator (ai), sizeof (double) * local->total_num_images); + ci->s->curr_size = sizeof (double) * local->total_num_images; ci->sm = sm; - ci->a = get_allocator(ai); + ci->a = get_allocator (ai); master_bind_active_image_barrier (this_image.m, &ci->s->barrier); initialize_shared_mutex (&ci->s->mutex); @@ -189,7 +195,7 @@ collsub_iface_init (collsub_iface *ci, alloc_iface *ai, shared_memory *sm) void collsub_broadcast_scalar (collsub_iface *ci, void *obj, index_type elem_size, - int source_image /* Adjusted in the wrapper. */) + int source_image /* Adjusted in the wrapper. */) { void *buffer; @@ -205,12 +211,12 @@ collsub_broadcast_scalar (collsub_iface *ci, void *obj, index_type elem_size, collsub_sync (ci); memcpy (obj, buffer, elem_size); } - - finish_collective_subroutine (ci); + + finish_collective_subroutine (ci); } void -collsub_broadcast_array (collsub_iface *ci, gfc_array_char *desc, +collsub_broadcast_array (collsub_iface *ci, gfc_array_char *desc, int source_image) { void *buffer; @@ -231,19 +237,19 @@ collsub_broadcast_array (collsub_iface *ci, gfc_array_char *desc, if (source_image == this_image.image_num) { if (packed) - memcpy (buffer, GFC_DESCRIPTOR_DATA (desc), size_bytes); + memcpy (buffer, GFC_DESCRIPTOR_DATA (desc), size_bytes); else - pack_array_finish (&pi, desc, buffer); + pack_array_finish (&pi, desc, buffer); collsub_sync (ci); } - else + else { collsub_sync (ci); if (packed) memcpy (GFC_DESCRIPTOR_DATA (desc), buffer, size_bytes); else - unpack_array_finish(&pi, desc, buffer); + unpack_array_finish (&pi, desc, buffer); } - finish_collective_subroutine (ci); + finish_collective_subroutine (ci); } diff --git a/libgfortran/nca/counter_barrier.c b/libgfortran/nca/counter_barrier.c index 3f15ba460c5..acf94d9761d 100644 --- a/libgfortran/nca/counter_barrier.c +++ b/libgfortran/nca/counter_barrier.c @@ -46,7 +46,7 @@ unlock_counter_barrier (counter_barrier *b) /* Wait on the barrier. */ -void +void counter_barrier_wait (counter_barrier *b) { int initial_count; @@ -55,7 +55,7 @@ counter_barrier_wait (counter_barrier *b) assert (b->count); lock_counter_barrier (b); - + wait_group_beginning = b->curr_wait_group; if (--b->wait_count <= 0) @@ -63,7 +63,7 @@ counter_barrier_wait (counter_barrier *b) else { while (b->wait_count > 0 && b->curr_wait_group == wait_group_beginning) - pthread_cond_wait(&b->cond, &b->count->m); + pthread_cond_wait (&b->cond, &b->count->m); } if (b->wait_count <= 0) @@ -92,12 +92,12 @@ waitable_counter_add (waitable_counter *c, int val) { counter_barrier *curr; int ret; - pthread_mutex_lock(&c->m); + pthread_mutex_lock (&c->m); ret = (c->val += val); - for (curr = c->b; curr; curr = curr->next) - change_internal_barrier_count(curr, val); + for (curr = c->b; curr; curr = curr->next) + change_internal_barrier_count (curr, val); - pthread_mutex_unlock(&c->m); + pthread_mutex_unlock (&c->m); return ret; } @@ -107,15 +107,15 @@ int waitable_counter_get_val (waitable_counter *c) { int ret; - pthread_mutex_lock(&c->m); + pthread_mutex_lock (&c->m); ret = c->val; - pthread_mutex_unlock(&c->m); + pthread_mutex_unlock (&c->m); return ret; } /* Initialize waitable counter. */ -void +void waitable_counter_init (waitable_counter *c, int val) { initialize_shared_mutex (&c->m); @@ -125,7 +125,7 @@ waitable_counter_init (waitable_counter *c, int val) /* Bind a barrier to a counter. */ -void +void bind_counter_barrier (counter_barrier *b, waitable_counter *c) { initialize_shared_condition (&b->cond); @@ -137,5 +137,4 @@ bind_counter_barrier (counter_barrier *b, waitable_counter *c) b->wait_count = c->val; c->b = b; pthread_mutex_unlock (&c->m); - } diff --git a/libgfortran/nca/hashmap.c b/libgfortran/nca/hashmap.c index 61fe966a3f0..2d38c568076 100644 --- a/libgfortran/nca/hashmap.c +++ b/libgfortran/nca/hashmap.c @@ -29,16 +29,17 @@ see the files COPYING3 and COPYING.RUNTIME respectively. If not, see #include <string.h> #define INITIAL_BITNUM (5) -#define INITIAL_SIZE (1<<INITIAL_BITNUM) +#define INITIAL_SIZE (1 << INITIAL_BITNUM) #define CRITICAL_LOOKAHEAD (16) static ssize_t n_ent; -typedef struct { +typedef struct +{ memid id; shared_mem_ptr p; /* If p == SHMPTR_NULL, the entry is empty. */ size_t s; - int max_lookahead; + int max_lookahead; int refcnt; } hashmap_entry; @@ -50,7 +51,7 @@ num_entries (hashmap_entry *data, size_t size) for (i = 0; i < size; i++) { if (!SHMPTR_IS_NULL (data[i].p)) - ret ++; + ret++; } return ret; } @@ -58,27 +59,27 @@ num_entries (hashmap_entry *data, size_t size) /* 64 bit to 64 bit hash function. */ /* -static inline uint64_t +static inline uint64_t hash (uint64_t x) { return x * 11400714819323198485lu; } */ -#define ASSERT_HM(hm, cond) assert_hashmap(hm, cond, #cond) +#define ASSERT_HM(hm, cond) assert_hashmap (hm, cond, #cond) -static void -assert_hashmap(hashmap *hm, bool asserted, const char *cond) +static void +assert_hashmap (hashmap *hm, bool asserted, const char *cond) { if (!asserted) { - dprintf(2, cond); - dump_hm(hm); + dprintf (2, cond); + dump_hm (hm); } - assert(asserted); + assert (asserted); } -static inline uint64_t +static inline uint64_t hash (uint64_t key) { key ^= (key >> 30); @@ -90,16 +91,14 @@ hash (uint64_t key) return key; } - /* Gets a pointer to the current data in the hashmap. */ static inline hashmap_entry * -get_data(hashmap *hm) +get_data (hashmap *hm) { - return SHMPTR_AS (hashmap_entry *, hm->s->data, hm->sm); + return SHMPTR_AS (hashmap_entry *, hm->s->data, hm->sm); } - /* Generate mask from current number of bits. */ static inline intptr_t @@ -108,36 +107,34 @@ gen_mask (hashmap *hm) return (1 << hm->s->bitnum) - 1; } - /* Add with wrap-around at hashmap size. */ static inline size_t -hmiadd (hashmap *hm, size_t s, ssize_t o) { +hmiadd (hashmap *hm, size_t s, ssize_t o) +{ return (s + o) & gen_mask (hm); } - /* Get the expected offset for entry id. */ static inline ssize_t get_expected_offset (hashmap *hm, memid id) { - return hash(id) >> (PTR_BITS - hm->s->bitnum); + return hash (id) >> (PTR_BITS - hm->s->bitnum); } - /* Initialize the hashmap. */ -void +void hashmap_init (hashmap *hm, hashmap_shared *hs, allocator *a, - shared_memory *mem) + shared_memory *mem) { hashmap_entry *data; hm->s = hs; hm->sm = mem; - hm->s->data = shared_malloc (a, INITIAL_SIZE * sizeof(hashmap_entry)); + hm->s->data = shared_malloc (a, INITIAL_SIZE * sizeof (hashmap_entry)); data = get_data (hm); - memset(data, '\0', INITIAL_SIZE*sizeof(hashmap_entry)); + memset (data, '\0', INITIAL_SIZE * sizeof (hashmap_entry)); for (int i = 0; i < INITIAL_SIZE; i++) data[i].p = SHMPTR_NULL; @@ -147,11 +144,10 @@ hashmap_init (hashmap *hm, hashmap_shared *hs, allocator *a, hm->a = a; } - /* This checks if the entry id exists in that range the range between the expected position and the maximum lookahead. */ -static ssize_t +static ssize_t scan_inside_lookahead (hashmap *hm, ssize_t expected_off, memid id) { ssize_t lookahead; @@ -162,14 +158,13 @@ scan_inside_lookahead (hashmap *hm, ssize_t expected_off, memid id) ASSERT_HM (hm, lookahead < CRITICAL_LOOKAHEAD); for (int i = 0; i <= lookahead; i++) /* For performance, this could - iterate backwards. */ + iterate backwards. */ if (data[hmiadd (hm, expected_off, i)].id == id) return hmiadd (hm, expected_off, i); return -1; } - /* Scan for the next empty slot we can use. Returns offset relative to the expected position. */ @@ -178,22 +173,21 @@ scan_empty (hashmap *hm, ssize_t expected_off) { hashmap_entry *data; - data = get_data(hm); - for (int i = 0; i < CRITICAL_LOOKAHEAD; i++) + data = get_data (hm); + for (int i = 0; i < CRITICAL_LOOKAHEAD; i++) if (SHMPTR_IS_NULL (data[hmiadd (hm, expected_off, i)].p)) return i; return -1; } - /* Search the hashmap for id. */ -hashmap_search_result +hashmap_search_result hashmap_get (hashmap *hm, memid id) { hashmap_search_result ret; - hashmap_entry *data; + hashmap_entry *data; size_t expected_offset; ssize_t res; @@ -202,15 +196,14 @@ hashmap_get (hashmap *hm, memid id) res = scan_inside_lookahead (hm, expected_offset, id); if (res != -1) - ret = ((hashmap_search_result) - { .p = data[res].p, .size=data[res].s, .res_offset = res }); + ret = ((hashmap_search_result){ + .p = data[res].p, .size = data[res].s, .res_offset = res }); else ret.p = SHMPTR_NULL; return ret; } - /* Return size of a hashmap search result. */ size_t @@ -219,25 +212,22 @@ hm_search_result_size (hashmap_search_result *res) return res->size; } - /* Return pointer of a hashmap search result. */ -shared_mem_ptr +shared_mem_ptr hm_search_result_ptr (hashmap_search_result *res) { return res->p; } - /* Return pointer of a hashmap search result. */ -bool +bool hm_search_result_contains (hashmap_search_result *res) { - return !SHMPTR_IS_NULL(res->p); + return !SHMPTR_IS_NULL (res->p); } - /* Enlarge hashmap memory. */ static void @@ -249,19 +239,19 @@ enlarge_hashmap_mem (hashmap *hm, hashmap_entry **data, bool f) old_data_p = hm->s->data; old_size = hm->s->size; - hm->s->data = shared_malloc (hm->a, (hm->s->size *= 2)*sizeof(hashmap_entry)); + hm->s->data + = shared_malloc (hm->a, (hm->s->size *= 2) * sizeof (hashmap_entry)); hm->s->bitnum++; - *data = get_data(hm); + *data = get_data (hm); for (size_t i = 0; i < hm->s->size; i++) - (*data)[i] = ((hashmap_entry) { .id = 0, .p = SHMPTR_NULL, .s=0, - .max_lookahead = 0, .refcnt=0 }); + (*data)[i] = ((hashmap_entry){ + .id = 0, .p = SHMPTR_NULL, .s = 0, .max_lookahead = 0, .refcnt = 0 }); if (f) - shared_free(hm->a, old_data_p, old_size); + shared_free (hm->a, old_data_p, old_size); } - /* Resize hashmap. */ static void @@ -276,64 +266,66 @@ resize_hm (hashmap *hm, hashmap_entry **data) /* old_data points to the old block containing the hashmap. We redistribute the data from there into the new block. */ - + old_data_p = hm->s->data; old_data = *data; old_size = hm->s->size; - enlarge_hashmap_mem (hm, &new_data, false); - retry_resize: + enlarge_hashmap_mem (hm, &new_data, false); +retry_resize: for (size_t i = 0; i < old_size; i++) { if (SHMPTR_IS_NULL (old_data[i].p)) - continue; + continue; id = old_data[i].id; inital_index = get_expected_offset (hm, id); new_offset = scan_empty (hm, inital_index); /* If we didn't find a free slot, just resize the hashmap - again. */ + again. */ if (new_offset == -1) - { - enlarge_hashmap_mem (hm, &new_data, true); - goto retry_resize; /* Sue me. */ - } + { + enlarge_hashmap_mem (hm, &new_data, true); + goto retry_resize; /* Sue me. */ + } ASSERT_HM (hm, new_offset < CRITICAL_LOOKAHEAD); new_index = hmiadd (hm, inital_index, new_offset); max_lookahead = new_data[inital_index].max_lookahead; new_data[inital_index].max_lookahead - = new_offset > max_lookahead ? new_offset : max_lookahead; - - new_data[new_index] = ((hashmap_entry) {.id = id, .p = old_data[i].p, - .s = old_data[i].s, - .max_lookahead = new_data[new_index].max_lookahead, - .refcnt = old_data[i].refcnt}); + = new_offset > max_lookahead ? new_offset : max_lookahead; + + new_data[new_index] = ((hashmap_entry){ + .id = id, + .p = old_data[i].p, + .s = old_data[i].s, + .max_lookahead = new_data[new_index].max_lookahead, + .refcnt = old_data[i].refcnt }); } shared_free (hm->a, old_data_p, old_size); *data = new_data; } - /* Set an entry in the hashmap. */ -void +void hashmap_set (hashmap *hm, memid id, hashmap_search_result *hsr, - shared_mem_ptr p, size_t size) + shared_mem_ptr p, size_t size) { hashmap_entry *data; ssize_t expected_offset, lookahead; ssize_t empty_offset; ssize_t delta; - data = get_data(hm); + data = get_data (hm); - if (hsr) { - data[hsr->res_offset].s = size; - data[hsr->res_offset].p = p; - return; - } + if (hsr) + { + data[hsr->res_offset].s = size; + data[hsr->res_offset].p = p; + return; + } expected_offset = get_expected_offset (hm, id); while ((delta = scan_empty (hm, expected_offset)) == -1) @@ -345,20 +337,23 @@ hashmap_set (hashmap *hm, memid id, hashmap_search_result *hsr, empty_offset = hmiadd (hm, expected_offset, delta); lookahead = data[expected_offset].max_lookahead; data[expected_offset].max_lookahead = delta > lookahead ? delta : lookahead; - data[empty_offset] = ((hashmap_entry) {.id = id, .p = p, .s = size, - .max_lookahead = data[empty_offset].max_lookahead, - .refcnt = 1}); - - n_ent ++; + data[empty_offset] + = ((hashmap_entry){ .id = id, + .p = p, + .s = size, + .max_lookahead = data[empty_offset].max_lookahead, + .refcnt = 1 }); + + n_ent++; /* TODO: Shouldn't reset refcnt, but this doesn't matter at the moment because of the way the function is used. */ } /* Change the refcount of a hashmap entry. */ -static int +static int hashmap_change_refcnt (hashmap *hm, memid id, hashmap_search_result *res, - int delta) + int delta) { hashmap_entry *data; hashmap_search_result r; @@ -368,7 +363,7 @@ hashmap_change_refcnt (hashmap *hm, memid id, hashmap_search_result *res, data = get_data (hm); - if (res) + if (res) pr = res; else { @@ -389,22 +384,20 @@ hashmap_change_refcnt (hashmap *hm, memid id, hashmap_search_result *res, return ret; } - /* Increase hashmap entry refcount. */ -void -hashmap_inc (hashmap *hm, memid id, hashmap_search_result * res) +void +hashmap_inc (hashmap *hm, memid id, hashmap_search_result *res) { int ret; ret = hashmap_change_refcnt (hm, id, res, 1); ASSERT_HM (hm, ret > 0); } - /* Decrease hashmap entry refcount. */ -int -hashmap_dec (hashmap *hm, memid id, hashmap_search_result * res) +int +hashmap_dec (hashmap *hm, memid id, hashmap_search_result *res) { int ret; ret = hashmap_change_refcnt (hm, id, res, -1); @@ -412,34 +405,39 @@ hashmap_dec (hashmap *hm, memid id, hashmap_search_result * res) return ret; } -#define PE(str, ...) fprintf(stderr, INDENT str, ##__VA_ARGS__) +#define PE(str, ...) fprintf (stderr, INDENT str, ##__VA_ARGS__) #define INDENT "" void -dump_hm(hashmap *hm) { +dump_hm (hashmap *hm) +{ hashmap_entry *data; size_t exp; size_t occ_num = 0; - PE("h %p (size: %lu, bitnum: %d)\n", hm, hm->s->size, hm->s->bitnum); + PE ("h %p (size: %lu, bitnum: %d)\n", hm, hm->s->size, hm->s->bitnum); data = get_data (hm); - fprintf (stderr,"offset = %lx data = %p\n", (unsigned long) hm->s->data.offset, data); + fprintf (stderr, "offset = %lx data = %p\n", + (unsigned long)hm->s->data.offset, data); #undef INDENT #define INDENT " " - for (size_t i = 0; i < hm->s->size; i++) { - exp = get_expected_offset(hm, data[i].id); - if (!SHMPTR_IS_NULL(data[i].p)) { - PE("%2lu. (exp: %2lu w la %d) id %#-16lx p %#-14lx s %-7lu -- la %u ref %u %-16p\n", - i, exp, data[exp].max_lookahead, data[i].id, data[i].p.offset, data[i].s, - data[i].max_lookahead, data[i].refcnt, data + i); - occ_num++; + for (size_t i = 0; i < hm->s->size; i++) + { + exp = get_expected_offset (hm, data[i].id); + if (!SHMPTR_IS_NULL (data[i].p)) + { + PE ("%2lu. (exp: %2lu w la %d) id %#-16lx p %#-14lx s %-7lu -- la " + "%u ref %u %-16p\n", + i, exp, data[exp].max_lookahead, data[i].id, data[i].p.offset, + data[i].s, data[i].max_lookahead, data[i].refcnt, data + i); + occ_num++; + } + else + PE ("%2lu. empty -- la %u " + " %p\n", + i, data[i].max_lookahead, data + i); } - else - PE("%2lu. empty -- la %u %p\n", i, data[i].max_lookahead, - data + i); - - } #undef INDENT #define INDENT "" - PE("occupancy: %lu %f\n", occ_num, ((double) occ_num)/hm->s->size); + PE ("occupancy: %lu %f\n", occ_num, ((double)occ_num) / hm->s->size); } diff --git a/libgfortran/nca/shared_memory.c b/libgfortran/nca/shared_memory.c index 7e959f23124..b64e40a3ded 100644 --- a/libgfortran/nca/shared_memory.c +++ b/libgfortran/nca/shared_memory.c @@ -64,7 +64,8 @@ typedef struct shared_memory_act size_t num_local_allocs; - struct local_alloc { + struct local_alloc + { void *base; size_t size; } allocs[]; @@ -76,11 +77,12 @@ typedef struct shared_memory_act static inline void * map_memory (int fd, size_t size, off_t offset) { - void *ret = mmap (NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, offset); + void *ret + = mmap (NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, offset); if (ret == MAP_FAILED) { - perror("mmap failed"); - exit(1); + perror ("mmap failed"); + exit (1); } return ret; } @@ -90,7 +92,7 @@ map_memory (int fd, size_t size, off_t offset) static inline size_t get_shared_memory_act_size (int nallocs) { - return sizeof(shared_memory_act) + nallocs*sizeof(struct local_alloc); + return sizeof (shared_memory_act) + nallocs * sizeof (struct local_alloc); } /* When the shared memory block is enlarged, we need to map it into @@ -103,10 +105,9 @@ new_base_mapping (shared_memory_act *mem) /* We need another entry in the alloc table. */ mem->num_local_allocs++; newmem = realloc (mem, get_shared_memory_act_size (mem->num_local_allocs)); - newmem->allocs[newmem->num_local_allocs - 1] - = ((struct local_alloc) - {.base = map_memory (newmem->meta->fd, newmem->meta->size, 0), - .size = newmem->meta->size}); + newmem->allocs[newmem->num_local_allocs - 1] = ((struct local_alloc){ + .base = map_memory (newmem->meta->fd, newmem->meta->size, 0), + .size = newmem->meta->size }); newmem->last_seen_size = newmem->meta->size; return newmem; } @@ -124,7 +125,7 @@ last_base (shared_memory_act *mem) shared_mem_ptr shared_memory_get_mem_with_alignment (shared_memory_act **pmem, size_t size, - size_t align) + size_t align) { shared_memory_act *mem = *pmem; size_t new_size; @@ -135,21 +136,22 @@ shared_memory_get_mem_with_alignment (shared_memory_act **pmem, size_t size, if (used_wa + size <= mem->meta->size) { - memset(last_base(mem) + mem->meta->used, 0xCA, used_wa - mem->meta->used); - memset(last_base(mem) + used_wa, 0x42, size); + memset (last_base (mem) + mem->meta->used, 0xCA, + used_wa - mem->meta->used); + memset (last_base (mem) + used_wa, 0x42, size); mem->meta->used = used_wa + size; - return (shared_mem_ptr) {.offset = used_wa}; + return (shared_mem_ptr){ .offset = used_wa }; } /* We need to enlarge the memory segment. Double the size if that is big enough, otherwise get what's needed. */ - + if (mem->meta->size * 2 > used_wa + size) new_size = mem->meta->size * 2; else new_size = round_to_pagesize (used_wa + size); - + orig_used = mem->meta->used; mem->meta->size = new_size; mem->meta->used = used_wa + size; @@ -160,23 +162,23 @@ shared_memory_get_mem_with_alignment (shared_memory_act **pmem, size_t size, mem = new_base_mapping (mem); *pmem = mem; - assert(used_wa != 0); + assert (used_wa != 0); - memset(last_base(mem) + orig_used, 0xCA, used_wa - orig_used); - memset(last_base(mem) + used_wa, 0x42, size); + memset (last_base (mem) + orig_used, 0xCA, used_wa - orig_used); + memset (last_base (mem) + used_wa, 0x42, size); - return (shared_mem_ptr) {.offset = used_wa}; + return (shared_mem_ptr){ .offset = used_wa }; } /* If another image changed the size, update the size accordingly. */ -void +void shared_memory_prepare (shared_memory_act **pmem) { shared_memory_act *mem = *pmem; if (mem->meta->size == mem->last_seen_size) return; - mem = new_base_mapping(mem); + mem = new_base_mapping (mem); *pmem = mem; } @@ -190,19 +192,20 @@ shared_memory_init (shared_memory_act **pmem) int fd; size_t initial_size = round_to_pagesize (sizeof (global_shared_memory_meta)); - mem = malloc (get_shared_memory_act_size(1)); - fd = get_shmem_fd(); + mem = malloc (get_shared_memory_act_size (1)); + fd = get_shmem_fd (); - ftruncate(fd, initial_size); + ftruncate (fd, initial_size); mem->meta = map_memory (fd, initial_size, 0); - *mem->meta = ((global_shared_memory_meta) {.size = initial_size, - .used = sizeof(global_shared_memory_meta), - .fd = fd}); + *mem->meta = ((global_shared_memory_meta){ + .size = initial_size, + .used = sizeof (global_shared_memory_meta), + .fd = fd }); mem->last_seen_size = initial_size; mem->num_local_allocs = 1; - mem->allocs[0] = ((struct local_alloc) {.base = mem->meta, - .size = initial_size}); - + mem->allocs[0] + = ((struct local_alloc){ .base = mem->meta, .size = initial_size }); + *pmem = mem; } @@ -210,8 +213,7 @@ shared_memory_init (shared_memory_act **pmem) memory block) to a pointer. */ void * -shared_mem_ptr_to_void_ptr(shared_memory_act **pmem, shared_mem_ptr smp) +shared_mem_ptr_to_void_ptr (shared_memory_act **pmem, shared_mem_ptr smp) { - return last_base(*pmem) + smp.offset; + return last_base (*pmem) + smp.offset; } - diff --git a/libgfortran/nca/sync.c b/libgfortran/nca/sync.c index c7ae0679e5e..851bf759a1d 100644 --- a/libgfortran/nca/sync.c +++ b/libgfortran/nca/sync.c @@ -22,7 +22,6 @@ a copy of the GCC Runtime Library Exception along with this program; see the files COPYING3 and COPYING.RUNTIME respectively. If not, see <http://www.gnu.org/licenses/>. */ - #include "libgfortran.h" #include "libcoarraynative.h" @@ -31,7 +30,7 @@ see the files COPYING3 and COPYING.RUNTIME respectively. If not, see static void sync_all_init (counter_barrier *b) { - master_bind_active_image_barrier(this_image.m, b); + master_bind_active_image_barrier (this_image.m, b); } static inline void @@ -49,35 +48,35 @@ unlock_table (sync_iface *si) static inline void wait_table_cond (sync_iface *si, pthread_cond_t *cond) { - pthread_cond_wait (cond,&si->cis->table_lock); + pthread_cond_wait (cond, &si->cis->table_lock); } static int * -get_locked_table(sync_iface *si) { - lock_table(si); +get_locked_table (sync_iface *si) +{ + lock_table (si); return si->table; } void sync_iface_init (sync_iface *si, alloc_iface *ai, shared_memory *sm) { - si->cis = SHMPTR_AS (sync_iface_shared *, - shared_malloc (get_allocator(ai), - sizeof(collsub_iface_shared)), - sm); + si->cis = SHMPTR_AS ( + sync_iface_shared *, + shared_malloc (get_allocator (ai), sizeof (collsub_iface_shared)), sm); sync_all_init (&si->cis->sync_all); initialize_shared_mutex (&si->cis->table_lock); si->sm = sm; - si->a = get_allocator(ai); + si->a = get_allocator (ai); - si->cis->table = - shared_malloc(si->a, sizeof(int)*local->total_num_images * local->total_num_images); - si->cis->triggers = - shared_malloc(si->a, sizeof(pthread_cond_t)*local->total_num_images); + si->cis->table = shared_malloc (si->a, sizeof (int) * local->total_num_images + * local->total_num_images); + si->cis->triggers = shared_malloc (si->a, sizeof (pthread_cond_t) + * local->total_num_images); - si->table = SHMPTR_AS(int *, si->cis->table, si->sm); - si->triggers = SHMPTR_AS(pthread_cond_t *, si->cis->triggers, si->sm); + si->table = SHMPTR_AS (int *, si->cis->table, si->sm); + si->triggers = SHMPTR_AS (pthread_cond_t *, si->cis->triggers, si->sm); for (int i = 0; i < local->total_num_images; i++) initialize_shared_condition (&si->triggers[i]); @@ -88,25 +87,29 @@ void sync_table (sync_iface *si, int *images, size_t size) { #if defined(DEBUG_NATIVE_COARRAY) && DEBUG_NATIVE_COARRAY - dprintf (2, "Image %d waiting for these %ld images: ", this_image.image_num + 1, size); - for (int d_i = 0; (size_t) d_i < size; d_i++) + dprintf (2, + "Image %d waiting for these %ld images: ", this_image.image_num + 1, + size); + for (int d_i = 0; (size_t)d_i < size; d_i++) dprintf (2, "%d ", images[d_i]); dprintf (2, "\n"); #endif size_t i; int done; - int *table = get_locked_table(si); + int *table = get_locked_table (si); for (i = 0; i < size; i++) { - table[images[i] - 1 + local->total_num_images*this_image.image_num]++; + table[images[i] - 1 + local->total_num_images * this_image.image_num]++; pthread_cond_signal (&si->triggers[images[i] - 1]); } for (;;) { done = 1; for (i = 0; i < size; i++) - done &= si->table[images[i] - 1 + this_image.image_num*local->total_num_images] - == si->table[this_image.image_num + (images[i] - 1)*local->total_num_images]; + done &= si->table[images[i] - 1 + + this_image.image_num * local->total_num_images] + == si->table[this_image.image_num + + (images[i] - 1) * local->total_num_images]; if (done) break; wait_table_cond (si, &si->triggers[this_image.image_num]); @@ -117,5 +120,5 @@ sync_table (sync_iface *si, int *images, size_t size) void sync_all (sync_iface *si) { - counter_barrier_wait(&si->cis->sync_all); + counter_barrier_wait (&si->cis->sync_all); } diff --git a/libgfortran/nca/util.c b/libgfortran/nca/util.c index 758e87f7313..0b77a527f8a 100644 --- a/libgfortran/nca/util.c +++ b/libgfortran/nca/util.c @@ -12,26 +12,29 @@ #include <assert.h> /* Shared Memory objects live in their own namspace (usually found under - * /dev/shm/), so the "/" is needed. It is for some reason impossible to create - * a shared memory object without name. */ + * /dev/shm/), so the "/" is needed. It is for some reason impossible to + * create a shared memory object without name. */ #define MEMOBJ_NAME "/gfortran_coarray_memfd" size_t -alignto(size_t size, size_t align) { - return align*((size + align - 1)/align); +alignto (size_t size, size_t align) +{ + return align * ((size + align - 1) / align); } size_t pagesize; size_t -round_to_pagesize(size_t s) { - return alignto(s, pagesize); +round_to_pagesize (size_t s) +{ + return alignto (s, pagesize); } size_t -next_power_of_two(size_t size) { - assert(size); - return 1 << (PTR_BITS - __builtin_clzl(size-1)); +next_power_of_two (size_t size) +{ + assert (size); + return 1 << (PTR_BITS - __builtin_clzl (size - 1)); } void @@ -57,13 +60,13 @@ initialize_shared_condition (pthread_cond_t *cond) int get_shmem_fd (void) { - char buffer[1<<10]; + char buffer[1 << 10]; int fd, id; id = random (); do { - snprintf (buffer, sizeof (buffer), - MEMOBJ_NAME "_%u_%d", (unsigned int) getpid (), id++); + snprintf (buffer, sizeof (buffer), MEMOBJ_NAME "_%u_%d", + (unsigned int)getpid (), id++); fd = shm_open (buffer, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR); } while (fd == -1); @@ -72,7 +75,8 @@ get_shmem_fd (void) } bool -pack_array_prepare (pack_info * restrict pi, const gfc_array_char * restrict source) +pack_array_prepare (pack_info *restrict pi, + const gfc_array_char *restrict source) { index_type dim; bool packed; @@ -89,18 +93,18 @@ pack_array_prepare (pack_info * restrict pi, const gfc_array_char * restrict sou span = source->span != 0 ? source->span : type_size; for (index_type n = 0; n < dim; n++) { - pi->stride[n] = GFC_DESCRIPTOR_STRIDE (source,n) * span; - pi->extent[n] = GFC_DESCRIPTOR_EXTENT (source,n); + pi->stride[n] = GFC_DESCRIPTOR_STRIDE (source, n) * span; + pi->extent[n] = GFC_DESCRIPTOR_EXTENT (source, n); if (pi->extent[n] <= 0) - { - /* Do nothing. */ - packed = 1; + { + /* Do nothing. */ + packed = 1; pi->num_elem = 0; - break; - } + break; + } if (ssize != pi->stride[n]) - packed = 0; + packed = 0; pi->num_elem *= pi->extent[n]; ssize *= pi->extent[n]; @@ -110,8 +114,9 @@ pack_array_prepare (pack_info * restrict pi, const gfc_array_char * restrict sou } void -pack_array_finish (pack_info * const restrict pi, const gfc_array_char * const restrict source, - char * restrict dest) +pack_array_finish (pack_info *const restrict pi, + const gfc_array_char *const restrict source, + char *restrict dest) { index_type dim; const char *restrict src; @@ -129,7 +134,7 @@ pack_array_finish (pack_info * const restrict pi, const gfc_array_char * const r while (src) { /* Copy the data. */ - memcpy(dest, src, size); + memcpy (dest, src, size); /* Advance to the next element. */ dest += size; src += stride0; @@ -137,35 +142,35 @@ pack_array_finish (pack_info * const restrict pi, const gfc_array_char * const r /* Advance to the next source element. */ index_type n = 0; while (count[n] == pi->extent[n]) - { - /* When we get to the end of a dimension, reset it and increment - the next dimension. */ - count[n] = 0; - /* We could precalculate these products, but this is a less - frequently used path so probably not worth it. */ - src -= pi->stride[n] * pi->extent[n]; - n++; - if (n == dim) - { - src = NULL; - break; - } - else - { - count[n]++; - src += pi->stride[n]; - } - } + { + /* When we get to the end of a dimension, reset it and increment + the next dimension. */ + count[n] = 0; + /* We could precalculate these products, but this is a less + frequently used path so probably not worth it. */ + src -= pi->stride[n] * pi->extent[n]; + n++; + if (n == dim) + { + src = NULL; + break; + } + else + { + count[n]++; + src += pi->stride[n]; + } + } } } void -unpack_array_finish (pack_info * const restrict pi, - const gfc_array_char * restrict d, +unpack_array_finish (pack_info *const restrict pi, + const gfc_array_char *restrict d, const char *restrict src) { index_type stride0; - char * restrict dest; + char *restrict dest; index_type size; index_type count[GFC_MAX_DIMENSIONS]; index_type dim; @@ -175,8 +180,7 @@ unpack_array_finish (pack_info * const restrict pi, dest = d->base_addr; dim = GFC_DESCRIPTOR_RANK (d); - - memset(count, '\0', sizeof(count) * dim); + memset (count, '\0', sizeof (count) * dim); while (dest) { memcpy (dest, src, size); @@ -196,7 +200,7 @@ unpack_array_finish (pack_info * const restrict pi, } else { - count[n] ++; + count[n]++; dest += pi->stride[n]; } } diff --git a/libgfortran/nca/wrapper.c b/libgfortran/nca/wrapper.c index 2e0b53bb27c..471619441c0 100644 --- a/libgfortran/nca/wrapper.c +++ b/libgfortran/nca/wrapper.c @@ -31,13 +31,14 @@ see the files COPYING3 and COPYING.RUNTIME respectively. If not, see static inline int div_ru (int divident, int divisor) { - return (divident + divisor - 1)/divisor; + return (divident + divisor - 1) / divisor; } /* Need to keep this in sync with trans-array.h:gfc_coarray_allocation_type. */ -enum gfc_coarray_allocation_type { +enum gfc_coarray_allocation_type +{ GFC_NCA_NORMAL_COARRAY = 1, GFC_NCA_LOCK_COARRAY, GFC_NCA_EVENT_COARRAY, @@ -46,8 +47,7 @@ enum gfc_coarray_allocation_type { void cas_coarray_alloc (gfc_array_void *, int, int, int); export_proto (cas_coarray_alloc); -void -cas_coarray_free (gfc_array_void *, int); +void cas_coarray_free (gfc_array_void *, int); export_proto (cas_coarray_free); int cas_coarray_this_image (int); @@ -59,7 +59,7 @@ export_proto (cas_coarray_num_images); void cas_coarray_sync_all (int *); export_proto (cas_coarray_sync_all); -void cas_sync_images (size_t, int *, int*, char *, size_t); +void cas_sync_images (size_t, int *, int *, char *, size_t); export_proto (cas_sync_images); void cas_lock (void *); @@ -69,20 +69,20 @@ void cas_unlock (void *); export_proto (cas_unlock); void cas_collsub_reduce_array (gfc_array_char *, void (*) (void *, void *), - int *, int*, char *, size_t); + int *, int *, char *, size_t); export_proto (cas_collsub_reduce_array); void cas_collsub_reduce_scalar (void *, index_type, void (*) (void *, void *), - int *, int*, char *, size_t); + int *, int *, char *, size_t); export_proto (cas_collsub_reduce_scalar); -void cas_collsub_broadcast_array (gfc_array_char * restrict, int, int *, char *, - size_t); +void cas_collsub_broadcast_array (gfc_array_char *restrict, int, int *, char *, + size_t); export_proto (cas_collsub_broadcast_array); -void cas_collsub_broadcast_scalar (void * restrict, size_t, int, int *, char *, - size_t); -export_proto(cas_collsub_broadcast_scalar); +void cas_collsub_broadcast_scalar (void *restrict, size_t, int, int *, char *, + size_t); +export_proto (cas_collsub_broadcast_scalar); void cas_coarray_alloc (gfc_array_void *desc, int elem_size, int corank, @@ -95,32 +95,31 @@ cas_coarray_alloc (gfc_array_void *desc, int elem_size, int corank, size_t last_lbound; size_t size_in_bytes; - ensure_initialization(); /* This function might be the first one to be - called, if it is called in a constructor. */ + ensure_initialization (); /* This function might be the first one to be + called, if it is called in a constructor. */ if (alloc_type == GFC_NCA_LOCK_COARRAY) elem_size = sizeof (pthread_mutex_t); else if (alloc_type == GFC_NCA_EVENT_COARRAY) - elem_size = sizeof(char); /* replace with proper type. */ + elem_size = sizeof (char); /* replace with proper type. */ - last_rank_index = GFC_DESCRIPTOR_RANK(desc) + corank -1; + last_rank_index = GFC_DESCRIPTOR_RANK (desc) + corank - 1; num_elems = 1; num_coarray_elems = 1; - for (i = 0; i < GFC_DESCRIPTOR_RANK(desc); i++) - num_elems *= GFC_DESCRIPTOR_EXTENT(desc, i); - for (i = GFC_DESCRIPTOR_RANK(desc); i < last_rank_index; i++) + for (i = 0; i < GFC_DESCRIPTOR_RANK (desc); i++) + num_elems *= GFC_DESCRIPTOR_EXTENT (desc, i); + for (i = GFC_DESCRIPTOR_RANK (desc); i < last_rank_index; i++) { - num_elems *= GFC_DESCRIPTOR_EXTENT(desc, i); - num_coarray_elems *= GFC_DESCRIPTOR_EXTENT(desc, i); + num_elems *= GFC_DESCRIPTOR_EXTENT (desc, i); + num_coarray_elems *= GFC_DESCRIPTOR_EXTENT (desc, i); } extent_last_codimen = div_ru (local->total_num_images, num_coarray_elems); - last_lbound = GFC_DIMENSION_LBOUND(desc->dim[last_rank_index]); - GFC_DIMENSION_SET(desc->dim[last_rank_index], last_lbound, - last_lbound + extent_last_codimen - 1, - num_elems); + last_lbound = GFC_DIMENSION_LBOUND (desc->dim[last_rank_index]); + GFC_DIMENSION_SET (desc->dim[last_rank_index], last_lbound, + last_lbound + extent_last_codimen - 1, num_elems); size_in_bytes = elem_size * num_elems * extent_last_codimen; if (alloc_type == GFC_NCA_LOCK_COARRAY) @@ -129,18 +128,17 @@ cas_coarray_alloc (gfc_array_void *desc, int elem_size, int corank, int expected = 0; /* Allocate enough space for the metadata infront of the lock array. */ - addr = get_memory_by_id_zero (&local->ai, size_in_bytes - + sizeof (lock_array), - (intptr_t) desc); + addr = get_memory_by_id_zero ( + &local->ai, size_in_bytes + sizeof (lock_array), (intptr_t)desc); /* Use of a traditional spin lock to avoid race conditions with the initization of the mutex. We could alternatively put a global lock around allocate, but that would probably be slower. */ while (!__atomic_compare_exchange_n (&addr->owner, &expected, - this_image.image_num + 1, - false, __ATOMIC_SEQ_CST, - __ATOMIC_SEQ_CST)); + this_image.image_num + 1, false, + __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) + ; if (!addr->initialized++) { for (i = 0; i < local->total_num_images; i++) @@ -150,10 +148,10 @@ cas_coarray_alloc (gfc_array_void *desc, int elem_size, int corank, desc->base_addr = &addr->arr; } else if (alloc_type == GFC_NCA_EVENT_COARRAY) - (void) 0; // TODO + (void)0; // TODO else - desc->base_addr = get_memory_by_id (&local->ai, size_in_bytes, - (intptr_t) desc); + desc->base_addr + = get_memory_by_id (&local->ai, size_in_bytes, (intptr_t)desc); } void @@ -167,33 +165,33 @@ cas_coarray_free (gfc_array_void *desc, int alloc_type) la = desc->base_addr - offsetof (lock_array, arr); /* TODO: Fix this, replace with some kind of atomic initilization. */ while (!__atomic_compare_exchange_n (&la->owner, &expected, - this_image.image_num+1, - false, __ATOMIC_SEQ_CST, - __ATOMIC_SEQ_CST)); + this_image.image_num + 1, false, + __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) + ; if (!--la->initialized) - { + { /* Coarray locks can be removed and just normal pthread_mutex can be used. */ - for (i = 0; i < local->total_num_images; i++) - pthread_mutex_destroy (&la->arr[i]); - } + for (i = 0; i < local->total_num_images; i++) + pthread_mutex_destroy (&la->arr[i]); + } __atomic_store_n (&la->owner, 0, __ATOMIC_SEQ_CST); } else if (alloc_type == GFC_NCA_EVENT_COARRAY) - (void) 0; //TODO + (void)0; // TODO - free_memory_with_id (&local->ai, (intptr_t) desc); + free_memory_with_id (&local->ai, (intptr_t)desc); desc->base_addr = NULL; } int -cas_coarray_this_image (int distance __attribute__((unused))) +cas_coarray_this_image (int distance __attribute__ ((unused))) { return this_image.image_num + 1; } int -cas_coarray_num_images (int distance __attribute__((unused))) +cas_coarray_num_images (int distance __attribute__ ((unused))) { return local->total_num_images; } @@ -201,14 +199,15 @@ cas_coarray_num_images (int distance __attribute__((unused))) void cas_coarray_sync_all (int *stat) { - STAT_ERRMSG_ENTRY_CHECK(stat, NULL, 0); + STAT_ERRMSG_ENTRY_CHECK (stat, NULL, 0); sync_all (&local->si); } void -cas_sync_images (size_t s, int *images, int *stat, char *error, size_t err_size) +cas_sync_images (size_t s, int *images, int *stat, char *error, + size_t err_size) { - STAT_ERRMSG_ENTRY_CHECK(stat, error, err_size); + STAT_ERRMSG_ENTRY_CHECK (stat, error, err_size); sync_table (&local->si, images, s); } @@ -225,35 +224,39 @@ cas_unlock (void *lock) } void -cas_collsub_reduce_array (gfc_array_char *desc, void (*assign_function) (void *, void *), - int *result_image, int *stat, char *errmsg, size_t errmsg_len) +cas_collsub_reduce_array (gfc_array_char *desc, + void (*assign_function) (void *, void *), + int *result_image, int *stat, char *errmsg, + size_t errmsg_len) { - STAT_ERRMSG_ENTRY_CHECK(stat, errmsg, errmsg_len); + STAT_ERRMSG_ENTRY_CHECK (stat, errmsg, errmsg_len); collsub_reduce_array (&local->ci, desc, result_image, assign_function); } void cas_collsub_reduce_scalar (void *obj, index_type elem_size, void (*assign_function) (void *, void *), - int *result_image, - int *stat, char *errmsg, size_t errmsg_len) + int *result_image, int *stat, char *errmsg, + size_t errmsg_len) { - STAT_ERRMSG_ENTRY_CHECK(stat, errmsg, errmsg_len); - collsub_reduce_scalar (&local->ci, obj, elem_size, result_image, assign_function); + STAT_ERRMSG_ENTRY_CHECK (stat, errmsg, errmsg_len); + collsub_reduce_scalar (&local->ci, obj, elem_size, result_image, + assign_function); } void -cas_collsub_broadcast_array (gfc_array_char * restrict a, int source_image, - int *stat, char *errmsg, size_t errmsg_len) +cas_collsub_broadcast_array (gfc_array_char *restrict a, int source_image, + int *stat, char *errmsg, size_t errmsg_len) { - STAT_ERRMSG_ENTRY_CHECK(stat, errmsg, errmsg_len); + STAT_ERRMSG_ENTRY_CHECK (stat, errmsg, errmsg_len); collsub_broadcast_array (&local->ci, a, source_image - 1); } void -cas_collsub_broadcast_scalar (void * restrict obj, size_t size, int source_image, - int *stat, char *errmsg, size_t errmsg_len) +cas_collsub_broadcast_scalar (void *restrict obj, size_t size, + int source_image, int *stat, char *errmsg, + size_t errmsg_len) { - STAT_ERRMSG_ENTRY_CHECK(stat, errmsg, errmsg_len); + STAT_ERRMSG_ENTRY_CHECK (stat, errmsg, errmsg_len); collsub_broadcast_scalar (&local->ci, obj, size, source_image - 1); } |