aboutsummaryrefslogtreecommitdiff
path: root/libgfortran/caf_shared/sync.c
blob: 71f7a34497ac5b7d75812ee62c1f41cf3032b015 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/* Copyright (C) 2019-2020 Free Software Foundation, Inc.
   Contributed by Nicolas Koenig

This file is part of the GNU Fortran Native Coarray Library (libnca).

Libnca is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.

Libnca is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

Under Section 7 of GPL version 3, you are granted additional
permissions described in the GCC Runtime Library Exception, version
3.1, as published by the Free Software Foundation.

You should have received a copy of the GNU General Public License and
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 <string.h>

static void
sync_all_init (counter_barrier *b)
{
  master_bind_active_image_barrier (this_image.m, b);
}

static inline void
lock_table (sync_iface *si)
{
  pthread_mutex_lock (&si->cis->table_lock);
}

static inline void
unlock_table (sync_iface *si)
{
  pthread_mutex_unlock (&si->cis->table_lock);
}

static inline void
wait_table_cond (sync_iface *si, pthread_cond_t *cond)
{
  pthread_cond_wait (cond, &si->cis->table_lock);
}

static int *
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 (sync_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->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);

  for (int i = 0; i < local->total_num_images; i++)
    initialize_shared_condition (&si->triggers[i]);
}

/* TODO: Maybe check whether synchronizing image is still alive.  */
void
sync_table (sync_iface *si, int *images, int 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, "%d ", images[d_i]);
  dprintf (2, "\n");
#endif
  int i;
  int done;
  int *table = get_locked_table (si);
  if (size > 0)
    {
      for (i = 0; i < size; i++)
	{
	  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];
	  if (done)
	    break;
	  wait_table_cond (si, &si->triggers[this_image.image_num]);
	}
    }
  else
    {
      size = local->total_num_images;
      for (i = 0; i < size; i++)
	{
	  table[i + local->total_num_images * this_image.image_num]++;
	  pthread_cond_signal (&si->triggers[i]);
	}
      for (;;)
	{
	  done = 1;
	  for (i = 0; i < size; i++)
	    done &= si->table[i + this_image.image_num * local->total_num_images]
	      == si->table[this_image.image_num
			   + i * local->total_num_images];
	  if (done)
	    break;
	  wait_table_cond (si, &si->triggers[this_image.image_num]);
	}
    }
  unlock_table (si);
}

void
sync_all (sync_iface *si)
{
  counter_barrier_wait (&si->cis->sync_all);
}