Merge trunk version 213968 into gupc branch.
[official-gcc.git] / libgupc / collectives / upc_coll_gather.upc
blobb3ddc9af33f9504a9beb13b89b7b480f0723cf8f
1 /* Copyright (C) 2012-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)
11 any later version.
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 /*****************************************************************************/
28 /*                                                                           */
29 /*  Copyright (c) 2004, Michigan Technological University                    */
30 /*  All rights reserved.                                                     */
31 /*                                                                           */
32 /*  Redistribution and use in source and binary forms, with or without       */
33 /*  modification, are permitted provided that the following conditions       */
34 /*  are met:                                                                 */
35 /*                                                                           */
36 /*  * Redistributions of source code must retain the above copyright         */
37 /*  notice, this list of conditions and the following disclaimer.            */
38 /*  * Redistributions in binary form must reproduce the above                */
39 /*  copyright notice, this list of conditions and the following              */
40 /*  disclaimer in the documentation and/or other materials provided          */
41 /*  with the distribution.                                                   */
42 /*  * Neither the name of the Michigan Technological University              */
43 /*  nor the names of its contributors may be used to endorse or promote      */
44 /*  products derived from this software without specific prior written       */
45 /*  permission.                                                              */
46 /*                                                                           */
47 /*  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS      */
48 /*  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT        */
49 /*  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A  */
50 /*  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER */
51 /*  OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, */
52 /*  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,      */
53 /*  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR       */
54 /*  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF   */
55 /*  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING     */
56 /*  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS       */
57 /*  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.             */
58 /*                                                                           */
59 /*****************************************************************************/
61 #include <upc.h>
62 #include <upc_collective.h>
63 #include <upc_coll.h>
65 /*****************************************************************************/
66 /*                                                                           */
67 /*        UPC collective function library, reference implementation          */
68 /*                                                                           */
69 /*   Steve Seidel, Dept. of Computer Science, Michigan Technological Univ.   */
70 /*   steve@mtu.edu                                        March 1, 2004      */
71 /*                                                                           */
72 /*****************************************************************************/
74 void
75 upc_all_gather (shared void *dst,
76                 shared const void *src, size_t nbytes, upc_flag_t sync_mode)
78 #ifndef PULL
79 #ifndef PUSH
80 #define PULL TRUE
81 #endif
82 #endif
84 #ifdef PULL
85   int i;
86 #endif
87   if (!upc_coll_init_flag)
88     upc_coll_init ();
90 #ifdef _UPC_COLL_CHECK_ARGS
91   upc_coll_err (dst, src, NULL, nbytes, sync_mode, 0, 0, 0, UPC_GATH);
92 #endif
93   // Synchronize using barriers in the cases of MYSYNC and ALLSYNC.
95   if (UPC_IN_MYSYNC & sync_mode || !(UPC_IN_NOSYNC & sync_mode))
97     upc_barrier;
99 #ifdef PULL
101   // The dst thread "pulls" a block of data from each src thread.
103   if ((int)upc_threadof ((shared void *) dst) == MYTHREAD)
104     {
105       for (i = 0; i < THREADS; ++i)
106         {
107           upc_memcpy ((shared char *) dst + nbytes * i * THREADS,
108                       (shared char *) src + i, nbytes);
109         }
110     }
111 #endif
113 #ifdef PUSH
115   // Each src thread "pushes" the data to the dst thread.
117   upc_memcpy ((shared char *) dst + MYTHREAD * THREADS * nbytes,
118               (shared char *) src + MYTHREAD, nbytes);
120 #endif
122   // Synchronize using barriers in the cases of MYSYNC and ALLSYNC.
124   if (UPC_OUT_MYSYNC & sync_mode || !(UPC_OUT_NOSYNC & sync_mode))
126     upc_barrier;