| 1 | // | 
|---|
| 2 | // util.cc | 
|---|
| 3 | // | 
|---|
| 4 | // Copyright (C) 1996 Limit Point Systems, Inc. | 
|---|
| 5 | // | 
|---|
| 6 | // Author: Curtis Janssen <cljanss@ca.sandia.gov> | 
|---|
| 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 | #include <typeinfo> | 
|---|
| 29 |  | 
|---|
| 30 | #include <math/scmat/util.h> | 
|---|
| 31 | #include <util/group/thread.h> | 
|---|
| 32 |  | 
|---|
| 33 | namespace sc { | 
|---|
| 34 |  | 
|---|
| 35 | class BlockOpThread: public Thread { | 
|---|
| 36 | int me_; | 
|---|
| 37 | int n_; | 
|---|
| 38 | Ref<SCElementOp> op_; | 
|---|
| 39 | Ref<SCMatrixBlockList> blocklist_; | 
|---|
| 40 | public: | 
|---|
| 41 | BlockOpThread(int me, | 
|---|
| 42 | int n, | 
|---|
| 43 | const Ref<SCElementOp>& op, | 
|---|
| 44 | const Ref<SCMatrixBlockList> &blocklist); | 
|---|
| 45 | void run(); | 
|---|
| 46 | }; | 
|---|
| 47 |  | 
|---|
| 48 | } | 
|---|
| 49 |  | 
|---|
| 50 | using namespace sc; | 
|---|
| 51 |  | 
|---|
| 52 | BlockOpThread::BlockOpThread(int me, | 
|---|
| 53 | int n, | 
|---|
| 54 | const Ref<SCElementOp>& op, | 
|---|
| 55 | const Ref<SCMatrixBlockList> &blocklist) | 
|---|
| 56 | { | 
|---|
| 57 | me_ = me; | 
|---|
| 58 | n_ = n; | 
|---|
| 59 | op_ = op; | 
|---|
| 60 | blocklist_ = blocklist; | 
|---|
| 61 | } | 
|---|
| 62 |  | 
|---|
| 63 | void | 
|---|
| 64 | BlockOpThread::run() | 
|---|
| 65 | { | 
|---|
| 66 | unsigned long count = 0; | 
|---|
| 67 | SCMatrixBlockListIter i; | 
|---|
| 68 | for (i = blocklist_->begin(); i != blocklist_->end(); i++,count++) { | 
|---|
| 69 | if (count%n_ == me_) { | 
|---|
| 70 | op_->process_base(i.block()); | 
|---|
| 71 | } | 
|---|
| 72 | } | 
|---|
| 73 | } | 
|---|
| 74 |  | 
|---|
| 75 | void | 
|---|
| 76 | sc::scmat_perform_op_on_blocks(const Ref<SCElementOp>& op, | 
|---|
| 77 | const Ref<SCMatrixBlockList> &blocklist) | 
|---|
| 78 | { | 
|---|
| 79 | Ref<ThreadGrp> thr = ThreadGrp::get_default_threadgrp(); | 
|---|
| 80 |  | 
|---|
| 81 | for (int i=0; i<thr->nthread(); i++) { | 
|---|
| 82 | thr->add_thread(i,0); | 
|---|
| 83 | } | 
|---|
| 84 |  | 
|---|
| 85 | Ref<SCElementOp> *ops = new Ref<SCElementOp>[thr->nthread()]; | 
|---|
| 86 |  | 
|---|
| 87 | int nthread; | 
|---|
| 88 | if (op->threadsafe()) { | 
|---|
| 89 | nthread = thr->nthread(); | 
|---|
| 90 | for (int i=0; i<nthread; i++) ops[i] = op; | 
|---|
| 91 | } | 
|---|
| 92 | else if (op->cloneable()) { | 
|---|
| 93 | nthread = thr->nthread(); | 
|---|
| 94 | ops[0] = op; | 
|---|
| 95 | for (int i=1; i<nthread; i++) ops[i] = op->clone(); | 
|---|
| 96 | } | 
|---|
| 97 | else { | 
|---|
| 98 | ops[0] = op; | 
|---|
| 99 | nthread = 1; | 
|---|
| 100 | } | 
|---|
| 101 |  | 
|---|
| 102 | for (int i=0; i<nthread; i++) { | 
|---|
| 103 | thr->add_thread(i, new BlockOpThread(i,nthread,ops[i],blocklist)); | 
|---|
| 104 | } | 
|---|
| 105 |  | 
|---|
| 106 | thr->start_threads(); | 
|---|
| 107 | thr->wait_threads(); | 
|---|
| 108 | thr->delete_threads(); | 
|---|
| 109 |  | 
|---|
| 110 | if (!op->threadsafe() && op->cloneable() && op->has_collect()) { | 
|---|
| 111 | for (int i = 1; i < nthread; i++) { | 
|---|
| 112 | ops[0]->collect(ops[i]); | 
|---|
| 113 | } | 
|---|
| 114 | } | 
|---|
| 115 |  | 
|---|
| 116 | delete[] ops; | 
|---|
| 117 | } | 
|---|
| 118 |  | 
|---|