1 /* Copyright (C) 2001-2014 Free Software Foundation, Inc.
2 This file is part of the UPC runtime Library.
3 Written by Gary Funck <gary@intrepid.com>
4 and Nenad Vukicevic <nenad@intrepid.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 Under Section 7 of GPL version 3, you are granted additional
19 permissions described in the GCC Runtime Library Exception, version
20 3.1, as published by the Free Software Foundation.
22 You should have received a copy of the GNU General Public License and
23 a copy of the GCC Runtime Library Exception along with this program;
24 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 <http://www.gnu.org/licenses/>. */
27 #include "upc_config.h"
28 #include "upc_sysdep.h"
33 __cvtaddr (upc_shared_ptr_t p
)
35 upc_info_p u
= __upc_info
;
38 __upc_fatal ("UPC runtime not initialized");
39 if (GUPCR_PTS_IS_NULL (p
))
41 addr
= __upc_sptr_to_addr (p
);
46 __getaddr (upc_shared_ptr_t p
)
48 upc_info_p u
= __upc_info
;
51 __upc_fatal ("UPC runtime not initialized");
52 if (GUPCR_PTS_IS_NULL (p
))
54 if ((int)GUPCR_PTS_THREAD(p
) != MYTHREAD
)
55 __upc_fatal ("Invalid conversion of shared address to local pointer;\nthread does not have affinity to shared address");
56 addr
= __upc_sptr_to_addr (p
);
61 upc_threadof (upc_shared_ptr_t p
)
63 upc_info_p u
= __upc_info
;
65 __upc_fatal ("UPC runtime not initialized");
66 if ((int)GUPCR_PTS_THREAD(p
) >= THREADS
)
67 __upc_fatal ("Thread number in shared address is out of range");
68 return GUPCR_PTS_THREAD (p
);
72 upc_phaseof (upc_shared_ptr_t p
)
74 upc_info_p u
= __upc_info
;
76 __upc_fatal ("UPC runtime not initialized");
77 if ((int)GUPCR_PTS_THREAD(p
) >= THREADS
)
78 __upc_fatal ("Thread number in shared address is out of range");
79 return GUPCR_PTS_PHASE (p
);
83 upc_resetphase (upc_shared_ptr_t p
)
85 upc_shared_ptr_t result
;
87 GUPCR_PTS_SET_PHASE (result
, 0);
92 upc_addrfield (upc_shared_ptr_t p
)
94 upc_info_p u
= __upc_info
;
96 __upc_fatal ("UPC runtime not initialized");
97 if ((int)GUPCR_PTS_THREAD(p
) >= THREADS
)
98 __upc_fatal ("Thread number in shared address is out of range");
99 return (size_t) GUPCR_PTS_VADDR (p
);
103 upc_affinitysize (size_t totalsize
, size_t nbytes
, size_t threadid
)
106 if (nbytes
== 0 || totalsize
== 0 || nbytes
>= totalsize
)
107 result
= (size_t) (threadid
== 0 ? totalsize
: 0);
110 size_t const nblocks
= (totalsize
/ nbytes
);
111 size_t const cutoff
= (nblocks
% THREADS
);
112 if (threadid
< cutoff
)
113 result
= (size_t) ((nblocks
+ THREADS
- 1) / THREADS
) * nbytes
;
114 else if (threadid
> cutoff
)
115 result
= (size_t) (nblocks
/ THREADS
) * nbytes
;
116 else /* threadid == cutoff */
117 result
= (size_t) ((nblocks
/ THREADS
) * nbytes
)
118 + totalsize
- nblocks
* nbytes
;