View Javadoc
1   // ******************************************************************************
2   //
3   // Title:       Force Field X.
4   // Description: Force Field X - Software for Molecular Biophysics.
5   // Copyright:   Copyright (c) Michael J. Schnieders 2001-2024.
6   //
7   // This file is part of Force Field X.
8   //
9   // Force Field X is free software; you can redistribute it and/or modify it
10  // under the terms of the GNU General Public License version 3 as published by
11  // the Free Software Foundation.
12  //
13  // Force Field X is distributed in the hope that it will be useful, but WITHOUT
14  // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  // FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16  // details.
17  //
18  // You should have received a copy of the GNU General Public License along with
19  // Force Field X; if not, write to the Free Software Foundation, Inc., 59 Temple
20  // Place, Suite 330, Boston, MA 02111-1307 USA
21  //
22  // Linking this library statically or dynamically with other modules is making a
23  // combined work based on this library. Thus, the terms and conditions of the
24  // GNU General Public License cover the whole combination.
25  //
26  // As a special exception, the copyright holders of this library give you
27  // permission to link this library with independent modules to produce an
28  // executable, regardless of the license terms of these independent modules, and
29  // to copy and distribute the resulting executable under terms of your choice,
30  // provided that you also meet, for each linked independent module, the terms
31  // and conditions of the license of that module. An independent module is a
32  // module which is not derived from or based on this library. If you modify this
33  // library, you may extend this exception to your version of the library, but
34  // you are not obligated to do so. If you do not wish to do so, delete this
35  // exception statement from your version.
36  //
37  // ******************************************************************************
38  package ffx.potential.bonded;
39  
40  import java.io.Serial;
41  import java.util.Enumeration;
42  import java.util.List;
43  import java.util.Objects;
44  import java.util.logging.Logger;
45  import javax.swing.tree.TreeNode;
46  import org.jogamp.java3d.BranchGroup;
47  
48  /**
49   * The ROLSP class is used for Proof-Of-Concept Parallel Recusive Over Length Scales (ROLS) Methods
50   * (currently only on shared memory systems). Simply inserting a ParallelMSM node into the
51   * Hierarchy causes a seperate thread of execution to be created for all operations on nodes below
52   * the ROLSP node. This is very preliminary code, but a useful concept for parallelizing ROLS in
53   * ffe.lang.
54   *
55   * @author Michael J. Schnieders
56   * @since 1.0
57   */
58  public class ROLSP extends MSNode implements ROLS, Runnable {
59  
60    @Serial
61    private static final long serialVersionUID = 1L;
62  
63    private static final Logger logger = Logger.getLogger(ROLSP.class.getName());
64    /** Constant <code>GO_PARALLEL=false</code> */
65    public static boolean GO_PARALLEL;
66    /** Constant <code>parallelNotDone=0</code> */
67    public static int parallelNotDone = 0;
68  
69    static {
70      try {
71        GO_PARALLEL = Boolean.parseBoolean(System.getProperty("ffx.lang.parallel", "false"));
72      } catch (Exception e) {
73        GO_PARALLEL = false;
74      }
75    }
76  
77    private PARALLELMETHOD parallelMethod = PARALLELMETHOD.NONE;
78    private long startTime = 0;
79    private long threadTime = 0;
80    private RendererCache.ViewModel viewModel = null;
81    private List<BranchGroup> newShapes = null;
82  
83    /** Constructor for ROLSP. */
84    public ROLSP() {
85      super("Parallel Node");
86    }
87  
88    /**
89     * {@inheritDoc}
90     *
91     * <p>Overidden equals method.
92     */
93    @Override
94    public boolean equals(Object o) {
95      return this == o;
96    }
97  
98    /** {@inheritDoc} */
99    @Override
100   public int hashCode() {
101     MSNode child = (MSNode) getChildAt(0);
102     if (child == null) {
103       return Objects.hash("none");
104     }
105     return Objects.hash(child.hashCode());
106   }
107 
108   /** {@inheritDoc} */
109   @Override
110   public void run() {
111     switch (parallelMethod) {
112       case SETVIEW:
113         setView(viewModel, newShapes);
114         break;
115       default:
116         return;
117     }
118     threadTime = System.currentTimeMillis() - startTime;
119     logger.info("Start Time: " + startTime + " Total Time: " + threadTime);
120     parallelNotDone--;
121   }
122 
123   /** {@inheritDoc} */
124   @Override
125   public void setView(RendererCache.ViewModel viewModel, List<BranchGroup> newShapes) {
126     // Set Up the Parallel setView Method
127     if (parallelMethod == PARALLELMETHOD.NONE) {
128       startTime = System.currentTimeMillis();
129       this.viewModel = viewModel;
130       this.newShapes = newShapes;
131       parallelMethod = PARALLELMETHOD.SETVIEW;
132       Thread thread = new Thread(this);
133       thread.setName(getParent().toString() + ": Parallel setView MSM");
134       thread.setPriority(Thread.MAX_PRIORITY);
135       parallelNotDone++;
136       thread.start();
137     } else if (parallelMethod == PARALLELMETHOD.SETVIEW) {
138       // setView has been called from within the 'run' method of the
139       // "setView" thread
140       for (Enumeration<TreeNode> e = children(); e.hasMoreElements(); ) {
141         MSNode node = (MSNode) e.nextElement();
142         node.setView(viewModel, newShapes);
143       }
144       parallelMethod = PARALLELMETHOD.NONE;
145     } else {
146       logger.info(" Parallel setView method called by: " + parallelMethod);
147     }
148   }
149 
150   /** {@inheritDoc} */
151   @Override
152   public String toString() {
153     if (threadTime != 0) {
154       return "Parallel Node " + threadTime + " (msec)";
155     }
156     return "Parallel Node";
157   }
158 
159   public enum PARALLELMETHOD {
160     SETVIEW,
161     NONE
162   }
163 }