Revision 1053 branches/threaded2/examples/threaded/multisimple/msimple.tpl

msimple.tpl (revision 1053)
4 4
  #include <pthread.h>
5 5
  #include <fvar.hpp>
6 6
  #include <adthread.h>
7
  //#include "pt_trace.h"
8
  pthread_mutex_t trace_mutex= PTHREAD_MUTEX_INITIALIZER;
9
  ofstream clogf;
10 7

  
11
  int NSLAVES = 7;
8
 
9
  // write the function to run on the threads
10
  void simple_thread(void * ptr)
11
  {
12
      // cast the pointer to the propper type
13
      new_thread_data * tptr = (new_thread_data *) ptr;
12 14

  
13
  void * mp_ptr = NULL;
14
  //adpthread_manager * ad_comm::pthread_manager= NULL;
15
  void* simple_thread(void * ptr);
16
  //int __thread thread_data::id=-1;
15
      // set up the gradient stack for the thread
16
      gradient_structure::set_MAX_NVAR_OFFSET(10000);
17
      gradient_structure::set_GRADSTACK_BUFFER_SIZE(20000000);
18
      gradient_structure::set_CMPDIF_BUFFER_SIZE   (20000000);
19
      gradient_structure gs(20000000);
20
   
21
      // get the thread number
22
      ad_comm::pthread_manager->set_slave_number(tptr->thread_no);
17 23

  
24
    
25
      // take control of the constant buffer for reading
26
      ad_comm::pthread_manager->cread_lock_buffer(0);
27
      // read the independent variables IN THE SAME ORDER AS THEY ARE SENT
28
      dvector x = ad_comm::pthread_manager->get_dvector(0);
29
      dvector Y = ad_comm::pthread_manager->get_dvector(0);
30
     // release the constant buffer
31
      ad_comm::pthread_manager->cread_unlock_buffer(0);
18 32

  
33
      do
34
      {
35
         // take control of the variable buffer for reading
36
         ad_comm::pthread_manager->read_lock_buffer(0);
37
         // read the finished signal
38
         int lflag=ad_comm::pthread_manager->get_int(0);
39
         if (lflag == 0) break;
40
         // read current values of parameters
41
         dvariable a =ad_comm::pthread_manager->get_dvariable(0);
42
         dvariable b =ad_comm::pthread_manager->get_dvariable(0);
43
         // release the variable buffer
44
         ad_comm::pthread_manager->read_unlock_buffer(0);
19 45

  
46
         // allocate the dependent variable to be the same shape as x
47
         dvar_vector pred_Y(x.indexmin(),x.indexmax());
48

  
49
         // compute dependent variable
50
         pred_Y = a * x +  b;
51
         // compute likelihood
52
         dvariable f=norm2(pred_Y-Y); 
53
   
54
         // send results to master
55
         // take control of the variable buffer for sending
56
         ad_comm::pthread_manager->write_lock_buffer(0);
57
         // send f
58
         ad_comm::pthread_manager->send_dvariable_to_master(f, 0);
59
         // release the variable buffer
60
         ad_comm::pthread_manager->write_unlock_buffer(0);
61
   
62
         // compute derivative contribution for this thread 
63
         slave_gradcalc();
64
      }
65
      while (1);
66
      // close thread
67
      pthread_exit(ptr);
68
   }
69

  
70

  
20 71
DATA_SECTION
21 72
  init_int nobs
22
  int nslaves
23
  !! nslaves = NSLAVES;
24
  matrix Y(1,nslaves,1,nobs)
25
  matrix x(1,nslaves,1,nobs)
73
  init_int nrow
74
  matrix Y(1,nrow,1,nobs)
75
  matrix x(1,nrow,1,nobs)
26 76
  number A
27 77
  number B
28 78

  
29 79
 LOCAL_CALCS
30
    //TTRACE(nobs,nslaves)
31 80
    A = 2.0;
32 81
    B = 4.0;
33 82
    random_number_generator rng(101);
34 83
    dvector err(1,nobs);
35
    for (int i = 1; i <= nslaves; i++)
84
    for (int i = 1; i <= nrow; i++)
36 85
    {
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff