aboutsummaryrefslogtreecommitdiff
path: root/libgupc/portals4/gupcr_atomic.upc
blob: bb8cd2c473da5ec9534b33f462fe2ed7d4e09f59 (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
/* Copyright (C) 2013-2016 Free Software Foundation, Inc.
   This file is part of the UPC runtime Library.
   Written by Gary Funck <gary@intrepid.com>
   and Nenad Vukicevic <nenad@intrepid.com>

This file is part of GCC.

GCC 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.

GCC 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 <upc.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <upc_atomic.h>
#include <portals4.h>
#include "gupcr_gmem.h"
#include "gupcr_utils.h"
#include "gupcr_atomic_sup.h"

/**
 * @file gupcr_atomic.upc
 * GUPC Portals4 UPC atomics implementation.
 *
 * All UPC atomic operations and data types, with exception of UPC_PTS,
 * are almost completely matched to the corresponding Portals4 atomics.
 * The following exceptions are made:
 *
 * UPC_SUB  Converted into Portals4 atomic add of a negative number.
 * UPC_INC  Converted into Portals4 atomic add of one.
 * UPC_DEC  Converted into Portals4 atomic add of negative one.
 *
 * UPC_PTS data type does not use Portals4 atomic operations (even though
 * 64 bit pointer-to-shared can fit into the int64 container).  This is
 * mainly due to the fact that pointer-to-shared comparison has to
 * disregard the phase part of the pointer and Portals4 does not have
 * support for CSWAP with a mask.
 */

/**
 * @addtogroup ATOMIC GUPCR Atomics Support Functions
 * @{
 */

/** Atomic domain representation */
struct upc_atomicdomain_struct
{
  upc_lock_t *lock;
  upc_op_t ops;
  upc_type_t type;
};

/**
 * Convert UPC to Portals4 atomic data type.
 *
 * @param [in] upc_type UPC atomic data type
 * @retval Portals4 atomic data type
 */
static inline ptl_datatype_t
gupcr_atomic_to_ptl_type (upc_type_t upc_type)
{
  switch (upc_type)
    {
    case UPC_INT:
      return UPC_ATOMIC_TO_PTL_INT;
    case UPC_UINT:
      return UPC_ATOMIC_TO_PTL_UINT;
    case UPC_LONG:
      return UPC_ATOMIC_TO_PTL_LONG;
    case UPC_ULONG:
      return UPC_ATOMIC_TO_PTL_ULONG;
    case UPC_INT32:
      return UPC_ATOMIC_TO_PTL_INT32;
    case UPC_UINT32:
      return UPC_ATOMIC_TO_PTL_UINT32;
    case UPC_INT64:
      return UPC_ATOMIC_TO_PTL_INT64;
    case UPC_UINT64:
      return UPC_ATOMIC_TO_PTL_UINT64;
    case UPC_FLOAT:
      return UPC_ATOMIC_TO_PTL_FLOAT;
    case UPC_DOUBLE:
      return UPC_ATOMIC_TO_PTL_DOUBLE;
    default:
      gupcr_error ("invalid UPC atomic type %d", (int) upc_type);
    }
  return -1;
}

/**
 * Convert UPC to Portals4 atomic operation.
 *
 * @param [in] upc_op UPC atomic operation
 * @retval Portals4 atomic operation
 */
static inline ptl_op_t
gupcr_atomic_to_ptl_op (upc_op_t upc_op)
{
  switch (upc_op)
    {
    case UPC_ADD:
      return PTL_SUM;
    case UPC_MULT:
      return PTL_PROD;
    case UPC_MAX:
      return PTL_MAX;
    case UPC_MIN:
      return PTL_MIN;
    case UPC_AND:
      return PTL_BAND;
    case UPC_OR:
      return PTL_BOR;
    case UPC_XOR:
      return PTL_BXOR;
    default:
      gupcr_error ("invalid UPC atomic op %d", (int) upc_op);
    }
  return -1;
}

/**
 * Convert UPC atomic operation into a string.
 *
 * @param [in] upc_op UPC atomic operation
 * @retval Character string
 */
static const char *
gupcr_get_atomic_op_as_string (upc_op_t upc_op)
{
  switch (upc_op)
    {
    case UPC_ADD:
      return "UPC_ADD";
    case UPC_AND:
      return "UPC_AND";
    case UPC_CSWAP:
      return "UPC_CSWAP";
    case UPC_DEC:
      return "UPC_DEC";
    case UPC_INC:
      return "UPC_INC";
    case UPC_GET:
      return "UPC_GET";
    case UPC_MAX:
      return "UPC_MAX";
    case UPC_MIN:
      return "UPC_MIN";
    case UPC_MULT:
      return "UPC_MULT";
    case UPC_OR:
      return "UPC_OR";
    case UPC_SET:
      return "UPC_SET";
    case UPC_SUB:
      return "UPC_SUB";
    case UPC_XOR:
      return "UPC_XOR";
    }
  return "UNKNOWN ATOMIC OP";
}

/**
 * Convert UPC atomic type into a string.
 *
 * @param [in] upc_type UPC atomic type
 * @retval Character string
 */
static const char *
gupcr_get_atomic_type_as_string (upc_type_t upc_type)
{
  switch (upc_type)
    {
    case UPC_INT:
      return "UPC_INT";
    case UPC_UINT:
      return "UPC_UINT";
    case UPC_LONG:
      return "UPC_LONG";
    case UPC_ULONG:
      return "UPC_ULONG";
    case UPC_INT32:
      return "UPC_INT32";
    case UPC_UINT32:
      return "UPC_UINT32";
    case UPC_INT64:
      return "UPC_INT64";
    case UPC_UINT64:
      return "UPC_UINT64";
    case UPC_FLOAT:
      return "UPC_FLOAT";
    case UPC_DOUBLE:
      return "UPC_DOUBLE";
    case UPC_PTS:
      return "UPC_PTS";
    }
  return "UNKNOWN ATOMIC TYPE";
}

/** Set value by UPC atomic type macro */
#define FUNC_TYPE_SET(__name__,__type__)    \
	*(__type__ *) buf = (__type__) value

/**
 * Set buffer to the value of the particular UPC atomic type.
 *
 * @param [in] buf Pointer to the buffer to set
 * @param [in] type UPC atomic type
 * @param [in] value Value to be set
 */
static void
gupcr_set_optype_val (void *buf, upc_type_t type, int value)
{
  switch (type)
    {
    case UPC_INT:
      FUNC_TYPE_SET (UPC_INT, int);
      break;
    case UPC_UINT:
      FUNC_TYPE_SET (UPC_UINT, unsigned int);
      break;
    case UPC_LONG:
      FUNC_TYPE_SET (UPC_LONG, long);
      break;
    case UPC_ULONG:
      FUNC_TYPE_SET (UPC_ULONG, unsigned long);
      break;
    case UPC_INT32:
      FUNC_TYPE_SET (UPC_INT32, int32_t);
      break;
    case UPC_UINT32:
      FUNC_TYPE_SET (UPC_UINT32, uint32_t);
      break;
    case UPC_INT64:
      FUNC_TYPE_SET (UPC_INT64, int64_t);
      break;
    case UPC_UINT64:
      FUNC_TYPE_SET (UPC_UINT64, uint64_t);
      break;
    case UPC_FLOAT:
      FUNC_TYPE_SET (UPC_FLOAT, float);
      break;
    case UPC_DOUBLE:
      FUNC_TYPE_SET (UPC_DOUBLE, double);
      break;
    default:
      gupcr_error ("wrong UPC type (%d)", type);
    }
}

/** Negate value by UPC atomic type macro */
#define FUNC_TYPE_NEGATE(__name__,__type__)    \
      *(__type__ *) dbuf = - *(__type__*) sbuf

/**
 * Negate value of the particular UPC atomic type.
 *
 * @param [in] dbuf Pointer to negated value
 * @param [in] sbuf Pointer to original value
 * @param [in] type UPC atomic type
 */
static void
gupcr_negate_atomic_type (void *dbuf, const void *sbuf, upc_type_t type)
{
  switch (type)
    {
    case UPC_INT:
      FUNC_TYPE_NEGATE (UPC_INT, int);
      break;
    case UPC_UINT:
      FUNC_TYPE_NEGATE (UPC_UINT, unsigned int);
      break;
    case UPC_LONG:
      FUNC_TYPE_NEGATE (UPC_LONG, long);
      break;
    case UPC_ULONG:
      FUNC_TYPE_NEGATE (UPC_ULONG, unsigned long);
      break;
    case UPC_INT32:
      FUNC_TYPE_NEGATE (UPC_INT32, int32_t);
      break;
    case UPC_UINT32:
      FUNC_TYPE_NEGATE (UPC_UINT32, uint32_t);
      break;
    case UPC_INT64:
      FUNC_TYPE_NEGATE (UPC_INT64, int64_t);
      break;
    case UPC_UINT64:
      FUNC_TYPE_NEGATE (UPC_UINT64, uint64_t);
      break;
    case UPC_FLOAT:
      FUNC_TYPE_NEGATE (UPC_FLOAT, float);
      break;
    case UPC_DOUBLE:
      FUNC_TYPE_NEGATE (UPC_DOUBLE, double);
      break;
    default:
      gupcr_error ("wrong UPC type (%d)", type);
    }
}

/** @} */

/**
 * @addtogroup UPCATOMIC UPC Atomics Functions
 * @{
 */

/**
 * UPC atomic relaxed operation.
 *
 * @param [in] domain Atomic domain
 * @param [in] fetch_ptr Target of the update
 * @param [in] op Atomic operation
 * @param [in] target Target address of the operation
 * @param [in] operand1 Operation required argument
 * @param [in] operand2 Operation required argument
 */
void
upc_atomic_relaxed (upc_atomicdomain_t * domain,
		    void *restrict fetch_ptr, upc_op_t op,
		    shared void *restrict target,
		    const void *restrict operand1,
		    const void *restrict operand2)
{
  struct upc_atomicdomain_struct *ldomain;
  char cvt_buf[GUPC_MAX_ATOMIC_SIZE];

  /* Complete all strict operations.  Portals4 runtime allows only
     outstanding put operations.  */
  if (gupcr_pending_strict_put)
    gupcr_gmem_sync_puts ();

  if (domain == NULL)
    gupcr_fatal_error ("NULL atomic domain pointer specified");

  ldomain = (struct upc_atomicdomain_struct *) &domain[MYTHREAD];

  gupcr_trace (FC_ATOMIC, "ATOMIC ENTER %s %s",
	       gupcr_get_atomic_op_as_string (op),
	       gupcr_get_atomic_type_as_string (ldomain->type));

  if (target == NULL)
    gupcr_fatal_error ("NULL atomic target pointer specified");

  if (!(op && ldomain->ops))
    {
      gupcr_fatal_error ("invalid operation (%s) for specified domain",
			 gupcr_get_atomic_op_as_string (op));
    }

  /* Check arguments.  */
  switch (op)
    {
    case UPC_GET:
      if (fetch_ptr == NULL)
	gupcr_fatal_error (
	  "atomic operation (UPC_GET) requires a non-NULL fetch pointer");
    case UPC_INC:
    case UPC_DEC:
      if (operand1 != NULL)
	gupcr_error ("atomic operation (%s) requires a NULL operand1",
		     gupcr_get_atomic_op_as_string (op));
      if (operand2 != NULL)
	gupcr_error ("atomic operation (%s) requires a NULL operand2",
		     gupcr_get_atomic_op_as_string (op));
      break;
    case UPC_CSWAP:
      if (operand1 == NULL)
	gupcr_fatal_error (
	  "atomic operation (UPC_CSWAP) requires a non-NULL operand1");
      if (operand2 == NULL)
	gupcr_fatal_error (
	  "atomic operation (UPC_CSWAP) requires a non-NULL operand2");
      break;
    default:
      if (operand1 == NULL)
	gupcr_fatal_error (
		"atomic operation (%s) requires a non-NULL operand1",
		gupcr_get_atomic_op_as_string (op));
      if (operand2 != NULL)
	gupcr_error ("atomic operation (%s) requires a NULL operand2",
		     gupcr_get_atomic_op_as_string (op));
    }

  /* UPC_PTS data type does not use Portals4 atomic operations,
     even though 64 bit pointer-to-shared fits in the int64
     container.  UPC_PTS supports only access operations (get, set, cswap)
     and as pointer compare needs to disregards the phase during
     comparison we are unable to place the pointer in some integral
     container (e.g. int64) and use Portals4 atomic ops.  */
  if (ldomain->type == UPC_PTS)
    {
      upc_lock (ldomain->lock);
      switch (op)
	{
	case UPC_GET:
	  *(shared void **) fetch_ptr = *(shared void *shared *) target;
	  break;
	case UPC_SET:
	  if (fetch_ptr)
	    *(shared void **) fetch_ptr = *(shared void *shared *) target;
	  *(shared void *shared *) target = *(shared void **) operand1;
	  break;
	case UPC_CSWAP:
	  {
	    shared void *tmp = *(shared void *shared *) target;
	    if (*(shared void **) operand1 == tmp)
	      *(shared void *shared *) target = *(shared void **) operand2;
	    if (fetch_ptr)
	      *(shared void **) fetch_ptr = tmp;
	  }
	  break;
	default:
	  upc_unlock (ldomain->lock);
	  gupcr_fatal_error ("invalid atomic operation (%s) for UPC_PTS",
			      gupcr_get_atomic_op_as_string (op));
	}
      upc_unlock (ldomain->lock);
    }
  else
    {
      size_t dthread = upc_threadof (target);
      size_t doffset = upc_addrfield (target);

      switch (op)
	{
	case UPC_GET:
	  gupcr_atomic_get (dthread, doffset, fetch_ptr,
			    gupcr_atomic_to_ptl_type (ldomain->type));
	  break;
	case UPC_SET:
	  gupcr_atomic_set (dthread, doffset, fetch_ptr, operand1,
			    gupcr_atomic_to_ptl_type (ldomain->type));
	  break;
	case UPC_CSWAP:
	  gupcr_atomic_cswap (dthread, doffset, fetch_ptr, operand1, operand2,
			      gupcr_atomic_to_ptl_type (ldomain->type));
	  break;
	case UPC_AND:
	case UPC_OR:
	case UPC_XOR:
	  if (ldomain->type == UPC_PTS ||
	      ldomain->type == UPC_FLOAT || ldomain->type == UPC_DOUBLE)
	    {
	      gupcr_fatal_error (
			"invalid atomic operation (%s) for %s type",
			gupcr_get_atomic_op_as_string (op),
			gupcr_get_atomic_type_as_string (ldomain->type));
	    }
	  gupcr_atomic_op (dthread, doffset, fetch_ptr, operand1,
			   gupcr_atomic_to_ptl_op (op),
			   gupcr_atomic_to_ptl_type (ldomain->type));
	  break;
	case UPC_ADD:
	case UPC_MULT:
	case UPC_MIN:
	case UPC_MAX:
	  gupcr_atomic_op (dthread, doffset, fetch_ptr,
			   operand1, gupcr_atomic_to_ptl_op (op),
			   gupcr_atomic_to_ptl_type (ldomain->type));
	  break;
	case UPC_SUB:
	  /* As Portals4 does not have atomic subtract, UPC_SUB must be
	     converted into atomic add, UPC_ADD.  */
	  gupcr_negate_atomic_type (cvt_buf, operand1, ldomain->type);
	  gupcr_atomic_op (dthread, doffset, fetch_ptr,
			   cvt_buf, gupcr_atomic_to_ptl_op (UPC_ADD),
			   gupcr_atomic_to_ptl_type (ldomain->type));
	  break;
	case UPC_INC:
	case UPC_DEC:
	  if (op == UPC_INC)
	    gupcr_set_optype_val (cvt_buf, ldomain->type, 1);
	  else
	    gupcr_set_optype_val (cvt_buf, ldomain->type, -1);
	  gupcr_atomic_op (dthread, doffset, fetch_ptr, cvt_buf, PTL_SUM,
			   gupcr_atomic_to_ptl_type (ldomain->type));
	  break;
	default:
	  gupcr_fatal_error ("invalid atomic operation: %s",
			     gupcr_get_atomic_op_as_string (op));
	}
    }
  gupcr_trace (FC_ATOMIC, "ATOMIC EXIT");
}

/**
 * UPC atomic strict operation.
 *
 * @param [in] domain Atomic domain
 * @param [in] fetch_ptr Target of the update
 * @param [in] op Atomic operation
 * @param [in] target Target address of the operation
 * @param [in] operand1 Operation required argument
 * @param [in] operand2 Operation required argument
 */
void
upc_atomic_strict (upc_atomicdomain_t * domain,
		   void *restrict fetch_ptr,
		   upc_op_t op,
		   shared void *restrict target,
		   const void *restrict operand1,
		   const void *restrict operand2)
{
  upc_fence;
  upc_atomic_relaxed (domain, fetch_ptr, op, target, operand1, operand2);
  upc_fence;
}

/**
 * Collective allocation of atomic domain.
 *
 * Implementation uses native Portals4 atomic functions and the
 * hint field is ignored.
 *
 * @parm [in] type Atomic operation type
 * @parm [in] ops Atomic domain operations
 * @parm [in] hints Atomic operation hint
 * @retval Allocated atomic domain pointer
 */
upc_atomicdomain_t *
upc_all_atomicdomain_alloc (upc_type_t type,
			    upc_op_t ops,
			    upc_atomichint_t hints __attribute__ ((unused)))
{
  struct upc_atomicdomain_struct *ldomain;
  shared upc_atomicdomain_t *domain;

  gupcr_trace (FC_ATOMIC, "ATOMIC DOMAIN_ALLOC ENTER %s ops(%X)",
	       gupcr_get_atomic_type_as_string (type), (unsigned) ops);
  domain = (upc_atomicdomain_t *)
    upc_all_alloc (THREADS, sizeof (struct upc_atomicdomain_struct));
  gupcr_assert (domain != NULL);

  ldomain = (struct upc_atomicdomain_struct *) &domain[MYTHREAD];
  ldomain->lock = NULL;
  if (type == UPC_PTS)
    ldomain->lock = upc_all_lock_alloc ();
  ldomain->ops = ops;
  ldomain->type = type;
  gupcr_trace (FC_ATOMIC, "ATOMIC DOMAIN_ALLOC EXIT");
  return domain;
}

/**
 * Collective free of the atomic domain.
 *
 * @param [in] domain Pointer to atomic domain
 *
 * @ingroup UPCATOMIC UPC Atomic Functions
 */
void
upc_all_atomicdomain_free (upc_atomicdomain_t * domain)
{
  if (domain == NULL)
    gupcr_fatal_error ("NULL atomic domain pointer specified");
  upc_barrier;
  if (MYTHREAD == 0)
    {
      upc_lock_free (domain->lock);
      upc_free (domain);
    }
  upc_barrier;
}

/**
 * Query implementation for expected performance.
 *
 * @parm [in] ops Atomic domain operations
 * @parm [in] type Atomic operation type
 * @parm [in] addr Atomic address
 * @retval Expected performance
 */
int
upc_atomic_isfast (upc_type_t type __attribute__ ((unused)),
		   upc_op_t ops __attribute__ ((unused)),
		   shared void *addr __attribute__ ((unused)))
{
  if (type == UPC_PTS)
    return UPC_ATOMIC_PERFORMANCE_NOT_FAST;
  return UPC_ATOMIC_PERFORMANCE_FAST;
}

/** @} */