AFEPack
Thread类参考

ĿΪܹȽϷAFEPackʹPOSIX߳ṩһӿڡ 更多...

#include <ThreadBase.h>


详细描述

ĿΪܹȽϷAFEPackʹPOSIX߳ṩһӿڡ

ʵֱȽϸӣҪǽdeal.IIйڶֵ֧߳ķdz ˽еļȥοĵѧϰһ¿C++ˮƽ пյʱȥһۡдñȽѶ鲻Ҫҵ룬˷ʱ䡣 ˣ˵ˮƽҸߣ;-)⣬Ҳṩκι̰߳ȫԷ ı֤ȫԼĴȥʵˡ

deal.IIеĶ߳ACE ֱԼдһȽϼ򵥵̹߳Ҫ˵һʹõķڴ û˵Ѿ㹻ˡ̵֧֣߳԰㷽IJһ̣߳Эͬ һȽϴ̵߳ڳԱҲԲԱ ڳԱ÷

 ̵߳ںiChebyshevʽڵ0.64ֵ
void fun(int i, const double& d)
{
  d = cos(i*acos(0.64));
};

int main(int argc, char * argv[])
{
  double val[5];
  ThreadManager th_man;         ̹߳
  for (int i = 0;i < 5;i ++) {  Ǵ5߳
     ̣߳ʵеܶһ
    th_man.start(encapsulate(&fun).collectArgs(i, val[i])); 
  }
  th_man.join(encapsulate(&fun));  ȴ߳̽
   һʵֵ÷dzѿʵûа취
   ʲôУθ
  for (int i = 0;i < 5;i ++) {  
    std::cout << "Chebyshev(" << i 
	      << ", 0.64) = " << val[i]
	      << std::endl;
  }
  return 0;
};

ԱںȻһһҪѶ ȥӣ

class Chebyshev {
 private:
  double x;
 public:  ̵߳ںǹе
  Chebyshev(const double& _x) : x(_x) {};
  void fun(int i, const double& d) {
    d = cos(i*acos(x));
  };
};

int main(int argc, char * argv[])
{
  double val[5];
  Chebyshev ch(0.64);           Ķ
  ThreadManager th_man;         ̹߳
  for (int i = 0;i < 5;i ++) {  Ǵ5߳
     ̣߳ҪѶ󴫽ȥ
    th_man.start(encapsulate(&Chebyshev::fun).collectArgs(&ch, i, val[i])); 
  }
  th_man.join(encapsulate(&Chebyshev::fun));  ȴ߳̽
  for (int i = 0;i < 5;i ++) {  
    std::cout << "Chebyshev(" << i 
	      << ", 0.64) = " << val[i]
	      << std::endl;
  }
  return 0;
};

ʵ֣ΨһĿΪʹ㡣

ڽԪʱʵֵһ

setThread(n);  n>1

ôڽԪռĹʱһִʹö߳̽УԺ һὫIJֽж̻߳Ҫʱ䣬ĵȴŶ

ʹAFEPackдԪij򣬳ĺ

void fun()
{
  FEMSpace<double,DIM>::ElementIterator the_ele = fem_space.beginElement();
  FEMSpace<double,DIM>::ElementIterator end_ele = fem_space.endElement();
  for (;the_ele != end_ele;++ the_ele) {
    ... ... ǻԪϽһȽϸӵֵֵIJ
  }
};

ĵطǵ
fun();

һγǾͿʹķʽж̻߳ˣ

void fun(int rank, int n_thread)
{
  int n_element = fem_space.n_elemen();
  FEMSpace<double,DIM>::ElementIterator the_ele = fem_space.beginElement();
  FEMSpace<double,DIM>::ElementIterator end_ele = fem_space.endElement();
  int stride = n_element/n_thread;
  the_ele += stride*rank;
  if (rank + 1 < n_thread)
    end_ele = the_ele + stride;
  for (;the_ele != end_ele;++ the_ele) { ֻһֵԪ
    ... ... ԪϽһȽϸӵֵֵIJ
  }
};

Ȼʱĵط΢һ
int n_thread = getThread();
ThreadManager th_man;
for (int rank = 1;rank < n_thread;rank ++) { һ߳
  th_man.start(encapsulate(&fun).collectArgs(rank, n_thread));
}
fun(0, n_thread); ԼҲҪ
th_man.join(encapsulate(&fun)); Ź

Լʹ÷ʽDZȽϷģ;-)


该类的文档由以下文件生成: