1 //****************************************************************************** 2 // 3 // File: ParallelConstruct.java 4 // Package: edu.rit.pj 5 // Unit: Class edu.rit.pj.ParallelConstruct 6 // 7 // This Java source file is copyright (C) 2007 by Alan Kaminsky. All rights 8 // reserved. For further information, contact the author, Alan Kaminsky, at 9 // ark@cs.rit.edu. 10 // 11 // This Java source file is part of the Parallel Java Library ("PJ"). PJ is free 12 // software; you can redistribute it and/or modify it under the terms of the GNU 13 // General Public License as published by the Free Software Foundation; either 14 // version 3 of the License, or (at your option) any later version. 15 // 16 // PJ is distributed in the hope that it will be useful, but WITHOUT ANY 17 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 18 // A PARTICULAR PURPOSE. See the GNU General Public License for more details. 19 // 20 // Linking this library statically or dynamically with other modules is making a 21 // combined work based on this library. Thus, the terms and conditions of the GNU 22 // General Public License cover the whole combination. 23 // 24 // As a special exception, the copyright holders of this library give you 25 // permission to link this library with independent modules to produce an 26 // executable, regardless of the license terms of these independent modules, and 27 // to copy and distribute the resulting executable under terms of your choice, 28 // provided that you also meet, for each linked independent module, the terms 29 // and conditions of the license of that module. An independent module is a module 30 // which is not derived from or based on this library. If you modify this library, 31 // you may extend this exception to your version of the library, but you are not 32 // obligated to do so. If you do not wish to do so, delete this exception 33 // statement from your version. 34 // 35 // A copy of the GNU General Public License is provided in the file gpl.txt. You 36 // may also obtain a copy of the GNU General Public License on the World Wide 37 // Web at http://www.gnu.org/licenses/gpl.html. 38 // 39 //****************************************************************************** 40 package edu.rit.pj; 41 42 /** 43 * Class ParallelConstruct is the common base class for all parallel constructs 44 * that are executed by a {@linkplain ParallelTeam}. 45 * 46 * @author Alan Kaminsky 47 * @version 20-Dec-2007 48 */ 49 public abstract class ParallelConstruct { 50 51 // Hidden data members. 52 // 128 bytes of extra padding to avert cache interference. 53 private long p0, p1, p2, p3, p4, p5, p6, p7; 54 private long p8, p9, pa, pb, pc, pd, pe, pf; 55 56 // Parallel team that is executing this parallel construct, or null if none. 57 ParallelTeam myTeam; 58 59 // Exported constructors. 60 /** 61 * Construct a new parallel construct. 62 */ 63 public ParallelConstruct() { 64 } 65 66 // Exported operations. 67 /** 68 * Determine if a parallel team is executing this parallel construct. 69 * 70 * @return True if a parallel team is executing this parallel construct, 71 * false otherwise. 72 */ 73 public final boolean isExecutingInParallel() { 74 return myTeam != null; 75 } 76 77 /** 78 * Returns the parallel team that is executing this parallel construct. 79 * 80 * @return Parallel team. 81 * @exception IllegalStateException (unchecked exception) Thrown if no 82 * parallel team is executing this parallel construct. 83 */ 84 public final ParallelTeam team() { 85 if (myTeam == null) { 86 throw new IllegalStateException("ParallelConstruct.team(): No parallel team executing"); 87 } 88 return myTeam; 89 } 90 91 /** 92 * Returns the parallel region of code within which a parallel team is 93 * executing this parallel construct. 94 * 95 * @return Parallel region. 96 * @exception IllegalStateException (unchecked exception) Thrown if no 97 * parallel team is executing this parallel construct. 98 */ 99 public final ParallelRegion region() { 100 if (myTeam == null) { 101 throw new IllegalStateException("ParallelConstruct.region(): No parallel team executing"); 102 } 103 return myTeam.myRegion; 104 } 105 106 /** 107 * Determine the number of threads in the parallel team executing this 108 * parallel construct. 109 * 110 * @return Number of threads in the thread team. 111 * @exception IllegalStateException (unchecked exception) Thrown if no 112 * parallel team is executing this parallel construct. 113 */ 114 public final int getThreadCount() { 115 if (myTeam == null) { 116 throw new IllegalStateException("ParallelConstruct.getThreadCount(): No parallel team executing"); 117 } 118 return myTeam.K; 119 } 120 121 /** 122 * Determine the index of the calling thread in the parallel team executing 123 * this parallel construct. 124 * 125 * @return Index of the calling thread in the range 0 .. 126 * <code>getThreadCount()-1</code>. 127 * @exception IllegalStateException (unchecked exception) Thrown if no 128 * parallel team is executing this parallel construct. Thrown if the thread 129 * calling 130 * <code>getThreadIndex()</code> is not part of the parallel team executing this 131 * parallel construct. 132 */ 133 public final int getThreadIndex() { 134 return getCurrentThread().myIndex; 135 } 136 137 // Hidden operations. 138 /** 139 * Get the parallel team thread that is calling this method. 140 * 141 * @return Parallel team thread. 142 * 143 * @exception IllegalStateException (unchecked exception) Thrown if the 144 * calling thread is not one of the parallel team threads executing this 145 * parallel construct. 146 */ 147 ParallelTeamThread getCurrentThread() { 148 if (myTeam == null) { 149 throw new IllegalStateException("ParallelConstruct.getCurrentThread(): No parallel team executing"); 150 } 151 try { 152 ParallelTeamThread current = (ParallelTeamThread) Thread.currentThread(); 153 if (current.myTeam != this.myTeam) { 154 throw new IllegalStateException("ParallelConstruct.getCurrentThread(): Current thread is not executing this parallel construct"); 155 } 156 return current; 157 } catch (ClassCastException exc) { 158 throw new IllegalStateException("ParallelConstruct.getCurrentThread(): Current thread is not a parallel team thread", 159 exc); 160 } 161 } 162 163 }