source: ThirdParty/mpqc_open/src/lib/util/group/thread.cc@ 482400e

Action_Thermostats Add_AtomRandomPerturbation Add_RotateAroundBondAction Add_SelectAtomByNameAction Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_StructOpt_integration_tests Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_ChronosMutex Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion GeometryObjects Gui_displays_atomic_force_velocity IndependentFragmentGrids_IntegrationTest JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks RotateToPrincipalAxisSystem_UndoRedo StoppableMakroAction Subpackage_levmar Subpackage_vmg ThirdParty_MPQC_rebuilt_buildsystem TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps Ubuntu_1604_changes stable
Last change on this file since 482400e was 860145, checked in by Frederik Heber <heber@…>, 8 years ago

Merge commit '0b990dfaa8c6007a996d030163a25f7f5fc8a7e7' as 'ThirdParty/mpqc_open'

  • Property mode set to 100644
File size: 6.9 KB
Line 
1//
2// thread.cc
3//
4// Copyright (C) 1997 Limit Point Systems, Inc.
5//
6// Author: Edward Seidl <seidl@janed.com>
7// Maintainer: LPS
8//
9// This file is part of the SC Toolkit.
10//
11// The SC Toolkit is free software; you can redistribute it and/or modify
12// it under the terms of the GNU Library General Public License as published by
13// the Free Software Foundation; either version 2, or (at your option)
14// any later version.
15//
16// The SC Toolkit is distributed in the hope that it will be useful,
17// but WITHOUT ANY WARRANTY; without even the implied warranty of
18// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19// GNU Library General Public License for more details.
20//
21// You should have received a copy of the GNU Library General Public License
22// along with the SC Toolkit; see the file COPYING.LIB. If not, write to
23// the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24//
25// The U.S. Government is granted a limited license as per AL 91-7.
26//
27
28#ifdef __GNUC__
29#pragma implementation
30#endif
31
32#include <util/keyval/keyval.h>
33#include <util/group/thread.h>
34#include <util/misc/formio.h>
35#include <util/misc/exenv.h>
36
37// debug includes
38#include <sys/types.h>
39#include <unistd.h>
40
41#include <scconfig.h>
42#ifdef HAVE_PTHREAD
43# include <util/group/thpthd.h>
44#endif
45
46using namespace std;
47using namespace sc;
48
49// This has C linkage.
50void *
51Thread__run_Thread_run(void* vth)
52{
53 return Thread::run_Thread_run(vth);
54}
55
56namespace sc {
57
58/////////////////////////////////////////////////////////////////////////////
59
60ThreadLock::ThreadLock()
61{
62}
63
64ThreadLock::~ThreadLock()
65{
66}
67
68/////////////////////////////////////////////////////////////////////////////
69
70Thread::Thread()
71{
72}
73
74Thread::~Thread()
75{
76}
77
78void *
79Thread::run_Thread_run(void* vth)
80{
81 if (vth) ((Thread*)vth)->run();
82 return 0;
83}
84
85/////////////////////////////////////////////////////////////////////////////
86// ThreadGrp members
87
88static ClassDesc ThreadGrp_cd(typeid(ThreadGrp),"ThreadGrp",1,
89 "public DescribedClass");
90
91ThreadGrp::ThreadGrp() : threads_(0), nthread_(1)
92{
93 threads_ = new Thread*[nthread_];
94 for (int i = 0; i<nthread_; i++) threads_[i] = 0;
95}
96
97ThreadGrp::ThreadGrp(const ThreadGrp &tg, int nthread)
98{
99 if (nthread == -1) nthread_ = tg.nthread_;
100 else nthread_ = nthread;
101 threads_ = new Thread*[nthread_];
102 for (int i = 0; i<nthread_; i++) threads_[i] = 0;
103}
104
105ThreadGrp::ThreadGrp(const Ref<KeyVal>& keyval)
106{
107 int defaultnum = ExEnv::nproc();
108 if (defaultnum == 0) defaultnum = 1;
109 KeyValValueint num(defaultnum);
110 nthread_ = keyval->intvalue("num_threads",num);
111
112 threads_ = new Thread*[nthread_];
113 for (int i=0; i<nthread_; i++) threads_[i] = 0;
114}
115
116ThreadGrp::~ThreadGrp()
117{
118 if (nthread_) {
119 delete[] threads_;
120 nthread_=0;
121 threads_=0;
122 }
123}
124
125void
126ThreadGrp::delete_threads()
127{
128 for (int i=0; i<nthread_; i++) {
129 delete threads_[i];
130 threads_[i] = 0;
131 }
132}
133
134void
135ThreadGrp::add_thread(int i, Thread*t)
136{
137 if (i >= nthread_) {
138 ExEnv::err0() << indent
139 << "ThreadGrp::add_thread: trying to add too many threads"
140 << endl;
141 } else {
142 threads_[i] = t;
143 }
144}
145
146void
147ThreadGrp::add_thread(int i, Thread*t, int priority)
148{
149 add_thread(i,t);
150}
151
152static Ref<ThreadGrp> default_threadgrp;
153
154void
155ThreadGrp::set_default_threadgrp(const Ref<ThreadGrp>& grp)
156{
157 default_threadgrp = grp;
158}
159
160ThreadGrp*
161ThreadGrp::get_default_threadgrp()
162{
163 if (default_threadgrp.null()) {
164#ifdef HAVE_PTHREAD
165 default_threadgrp = new PthreadThreadGrp;
166#else
167 default_threadgrp = new ProcThreadGrp;
168#endif
169 }
170
171 return default_threadgrp;
172}
173
174ThreadGrp*
175ThreadGrp::initial_threadgrp(int& argc, char ** argv)
176{
177 ThreadGrp *grp = 0;
178 char * keyval_string = 0;
179
180 // see if a thread group is given on the command line
181 if (argc && argv) {
182 for (int i=0; i < argc; i++) {
183 if (argv[i] && !strcmp(argv[i], "-threadgrp")) {
184 char *threadgrp_string = argv[i];
185 i++;
186 if (i >= argc) {
187 ExEnv::errn() << "-threadgrp must be following by an argument"
188 << endl;
189 abort();
190 }
191 keyval_string = argv[i];
192 // move the threadgrp arguments to the end of argv
193 int j;
194 for (j=i+1; j<argc; j++) {
195 argv[j-2] = argv[j];
196 }
197 argv[j++] = threadgrp_string;
198 argv[j++] = keyval_string;
199 // decrement argc to hide the last two arguments
200 argc -= 2;
201 break;
202 }
203 }
204 }
205
206 if (!keyval_string) {
207 // find out if the environment gives the containing thread group
208 keyval_string = getenv("THREADGRP");
209 if (keyval_string) {
210 if (!strncmp("THREADGRP=", keyval_string, 11)) {
211 keyval_string = strchr(keyval_string, '=');
212 }
213 if (*keyval_string == '=') keyval_string++;
214 }
215 }
216
217 // if keyval input for a thread group was found, then
218 // create it.
219 if (keyval_string) {
220 if (keyval_string[0] == '\0') return 0;
221 Ref<ParsedKeyVal> strkv = new ParsedKeyVal();
222 strkv->parse_string(keyval_string);
223 Ref<DescribedClass> dc = strkv->describedclassvalue();
224 grp = dynamic_cast<ThreadGrp*>(dc.pointer());
225 if (dc.null()) {
226 ExEnv::errn() << "initial_threadgrp: couldn't find a ThreadGrp in "
227 << keyval_string << endl;
228 abort();
229 } else if (!grp) {
230 ExEnv::errn() << "initial_threadgrp: wanted ThreadGrp but got "
231 << dc->class_name() << endl;
232 abort();
233 }
234 // prevent an accidental delete
235 grp->reference();
236 strkv = 0;
237 dc = 0;
238 // accidental delete not a problem anymore since all smart pointers
239 // to grp are dead
240 grp->dereference();
241 return grp;
242 }
243
244 return 0;
245}
246
247ThreadGrp*
248ThreadGrp::clone(int nthread)
249{
250 ExEnv::errn() << "ThreadGrp::clone not supported for " << class_name()
251 << endl;
252 abort();
253 return 0;
254}
255
256/////////////////////////////////////////////////////////////////////////////
257// ProcThreadLock class
258
259class ProcThreadLock : public ThreadLock {
260 public:
261 ProcThreadLock() {}
262 ~ProcThreadLock() {}
263
264 void lock() {}
265 void unlock() {}
266};
267
268/////////////////////////////////////////////////////////////////////////////
269// ProcThreadGrp members
270
271static ClassDesc ProcThreadGrp_cd(
272 typeid(ProcThreadGrp),"ProcThreadGrp",1,"public ThreadGrp",
273 0, create<ProcThreadGrp>, 0);
274
275ProcThreadGrp::ProcThreadGrp()
276 : ThreadGrp()
277{
278}
279
280ProcThreadGrp::ProcThreadGrp(const Ref<KeyVal>& keyval)
281 : ThreadGrp(keyval)
282{
283 if (nthread_ > 1) {
284 delete[] threads_;
285 nthread_ = 1;
286 threads_ = new Thread*[nthread_];
287 }
288}
289
290ProcThreadGrp::~ProcThreadGrp()
291{
292}
293
294int
295ProcThreadGrp::start_threads()
296{
297 if (threads_[0]) threads_[0]->run();
298 return 0;
299}
300
301int
302ProcThreadGrp::wait_threads()
303{
304 return 0;
305}
306
307Ref<ThreadLock>
308ProcThreadGrp::new_lock()
309{
310 return new ProcThreadLock;
311}
312
313ThreadGrp*
314ProcThreadGrp::clone(int nthread)
315{
316 return new ProcThreadGrp;
317}
318
319/////////////////////////////////////////////////////////////////////////////
320
321}
322
323// Local Variables:
324// mode: c++
325// c-file-style: "ETS"
326// End:
Note: See TracBrowser for help on using the repository browser.